-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
986 lines (816 loc) · 32.4 KB
/
main.cpp
File metadata and controls
986 lines (816 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/*
Program Name: Image Filter Service
Description: This program provides various image processing operations such as grayscale conversion, black and white conversion, image inversion, image merging, and image flipping.
Authors:
Author 1: Anoud Mohammed Ahmed ===>> solved { Filter 1 (Gray Scale Filter) , Filter 4 (MergeImages), filter 7 (Darken and Lighten Image), filter 10 (Detect Image Edges), filter 13 (good natural sunlight), filter 16 (Apply Purple Filter) }
Author 2: Nayera Shaaban Rashad ===>> solved { Filter 3 (Invert Image), filter 6 (Rotated Image), filter 9 (ِAdding a Frame to the Picture), filter 12 (Blur Images), filter 17 (infrared photography) }
Author 3: Moaaz Ashraf Mahmoud ===>> solved { Filter 2 (BlackAndWhite) , Filter 5 (FlippingImage), filter 8 (Crop Images), filter 11 (Resizing Images) }
IDs:
20231090 ===>> solved { Filter 1 (Gray Scale Filter) , Filter 4 (MergeImages), Filter 7 (Darken and Lighten Image) , filter 10 (Detect Image Edges), filter 13 (good natural sunlight), filter 16 (Apply Purple Filter) }
20231196 ===>> solved { Filter 3 (Invert Image), filter 6 (Rotated Image), filter 9 (ِAdding a Frame to the Picture), filter 12 (Blur Images), filter 17 (infrared photography) }
20231169 ===>> solved { Filter 2 (BlackAndWhite) , Filter 5 (FlippingImage), filter 8 (Crop Images), filter 11 (Resizing Images) }
Date: 15/4/2024
Section: S11,12
diagram link : https://drive.google.com/file/d/1j24jQc1La0XOz9S13qNHh2Ur1YCEsOP9/view?usp=sharing
*/
#include "Image_Class.h"
#include <iostream>
#include <string>
#include <limits>
#include <algorithm> // Include algorithm library for std::find
#include <vector> // Include vector library for std::vector
using namespace std;
void GrayScaleFilter() {
string filename;
cout << "Please enter your image name and extension to apply the Gray Scale filter on it: ";
cin >> filename;
Image photo(filename);
for (int i = 0; i < photo.width; ++i) {
for (int j = 0; j < photo.height; ++j) {
unsigned int avg = 0;
for (int k = 0; k < 3; ++k) {
avg += photo(i, j, k);
}
avg /= 3;
photo(i, j, 0) = avg;
photo(i, j, 1) = avg;
photo(i, j, 2) = avg;
}
}
cout << "Please enter the image name to store your new image" << endl;
cout << "and specify the extension: .jpg, .bmp, .png, .tga: ";
cin >> filename;
photo.saveImage(filename);
system(filename.c_str());
}
void GrayscaleConversion() {
GrayScaleFilter();
}
void BlackAndWhite() {
string filename;
cout << "Please enter colored image name to turn to a black and white image: ";
cin >> filename;
Image image(filename);
for (int i = 0; i < image.width; ++i) {
for (int j = 0; j < image.height; ++j) {
unsigned int avg = 0;
for (int k = 0; k < 3; ++k) {
avg += image(i, j, k);
}
avg /= 3;
for(int k = 0; k < 3; k++) {
if(avg >= 127) {
image(i, j, k) = 255;
}
else {
image(i, j, k) = 0;
}
}
}
}
cout << "Please enter image name to store new image\n";
cout << "and specify extension .jpg, .bmp, .png, .tga: ";
cin >> filename;
image.saveImage(filename);
}
void InvertImage() {
string filename;
cout << "Please enter colored image name to turn to reversed: ";
cin >> filename;
Image image(filename);
for (int i = 0; i < image.width; ++i) {
for (int j = 0; j < image.height; ++j) {
image(i, j, 0) = 255 - image(i, j, 0);
image(i, j, 1) = 255 - image(i, j, 1);
image(i, j, 2) = 255 - image(i, j, 2);
}
}
cout << "Please enter image name to store new image\n";
cout << "and specify extension .jpg, .bmp, .png, .tga: ";
cin >> filename;
image.saveImage(filename);
}
void MergeImage(){
try {
string filename1, filename2;
cout << "Please enter the name and extension of the first image file: ";
cin >> filename1;
cout << "Please enter the name and extension of the second image file: ";
cin >> filename2;
// Load the first image
Image image1(filename1);
// Load the second image
Image image2(filename2);
// Check if the dimensions of the two images are equal
if (image1.width != image2.width || image1.height != image2.height) {
cout << "The dimensions of the two images are not equal." << endl;
cout << "Do you want to resize the images to make them equal? (y/n): ";
char resizeChoice;
cin >> resizeChoice;
if (resizeChoice == 'y' || resizeChoice == 'Y') {
// Determine the new dimensions based on the minimum dimensions of the two images
int newWidth = min(image1.width, image2.width);
int newHeight = min(image1.height, image2.height);
// Resize image1
Image resizedImage1(newWidth, newHeight);
for (int i = 0; i < newWidth; ++i) {
for (int j = 0; j < newHeight; ++j) {
for (int k = 0; k < 3; ++k) {
resizedImage1(i, j, k) = image1(i * image1.width / newWidth, j * image1.height / newHeight, k);
}
}
}
image1 = resizedImage1;
// Resize image2
Image resizedImage2(newWidth, newHeight);
for (int i = 0; i < newWidth; ++i) {
for (int j = 0; j < newHeight; ++j) {
for (int k = 0; k < 3; ++k) {
resizedImage2(i, j, k) = image2(i * image2.width / newWidth, j * image2.height / newHeight, k);
}
}
}
image2 = resizedImage2;
} else {
cout << "Cannot proceed with the merge due to differing image dimensions." << endl;
return;
}
}
// Create a new image with the dimensions of the merged images
Image mergedImage(image1.width, image1.height);
// Merge the images
for (int i = 0; i < image1.width; ++i) {
for (int j = 0; j < image1.height; ++j) {
for (int k = 0; k < 3; ++k) {
// Take the average of the pixel values from the two images
int averageValue = (image1(i, j, k) + image2(i, j, k)) / 2;
// Store the average value in the merged image
mergedImage(i, j, k) = averageValue;
}
}
}
// Ask the user for the filename to save the merged image
string mergedFilename;
cout << "Please enter the name and extension to save your merged image: ";
cin >> mergedFilename;
// Save the merged image
mergedImage.saveImage(mergedFilename);
cout << "Merged image saved successfully!" << endl;
} catch (const exception &e) {
cout << "Error: " << e.what() << endl;
}
}
void FlippingImage(){
string filename;
cout << "Please enter the image filename: ";
cin >> filename;
Image image(filename);
cout<<"Do you want to flip the image 1) Horizontally or 2) Vertically? (1/2)"<<endl;
int choice;
cin>>choice;
if(choice==1){
for (int i = 0; i < image.width/2; ++i) {
for (int j = 0; j < image.height; ++j) {
for(int k = 0; k<3 ; ++k){
int temp = image(i,j,k);
image(i,j,k) = image(image.width-1-i,j,k);
image(image.width-1-i,j,k) = temp;
}
}
}
cout << "Enter image name to store new image\n";
cout << "and specify extension .jpg, .bmp, .png, .tga: ";
cin >> filename;
image.saveImage(filename);
}
else if (choice==2){
for (int i = 0; i < image.width; ++i) {
for (int j = 0; j < image.height/2; ++j) {
for(int k = 0; k<3 ; ++k){
int temp = image(i,j,k);
image(i,j,k) = image(i,image.height-1-j,k);
image(i,image.height-1-j,k) = temp;
}
}
}
cout << "Enter image name to store new image\n";
cout << "and specify extension .jpg, .bmp, .png, .tga: ";
cin >> filename;
image.saveImage(filename);
}
else{
cout<< "Invalid choice"<<endl;
}
}
void rotateImage(Image & image, const string & newImageName, int angle) {
int rotatedWidth, rotatedHeight;
if (angle == 90 || angle == 270) {
rotatedWidth = image.height;
rotatedHeight = image.width;
} else {
rotatedWidth = image.width;
rotatedHeight = image.height;
}
Image rotatedImage(rotatedWidth, rotatedHeight); // Create a new image for rotation
for (int i = 0; i < image.width; ++i) {
for (int j = 0; j < image.height; ++j) {
for (int k = 0; k < image.channels; ++k) {
int new_i, new_j;
if (angle == 90) {
new_i = rotatedWidth - 1 - j;
new_j = i;
} else if (angle == 180) {
new_i = rotatedWidth - 1 - i;
new_j = rotatedHeight - 1 - j;
} else if (angle == 270) {
new_i = j;
new_j = rotatedHeight - 1 - i;
}
rotatedImage(new_i, new_j, k) = image(i, j, k);
}
}
}
image = rotatedImage; // Update the original image with the rotated image
image.saveImage(newImageName); // Save the rotated image
}
void darkenImage(Image& image, int factor) {
for (int i = 0; i < image.width; ++i) {
for (int j = 0; j < image.height; ++j) {
for (int k = 0; k < 3; ++k) {
int newValue = image(i, j, k) - factor;
if (newValue < 0) {
newValue = 0;
}
image(i, j, k) = static_cast<unsigned char>(newValue);
}
}
}
}
void lightenImage(Image& image, int factor) {
for (int i = 0; i < image.width; ++i) {
for (int j = 0; j < image.height; ++j) {
for (int k = 0; k < 3; ++k) {
int newValue = image(i, j, k) + factor;
if (newValue > 255) {
newValue = 255;
}
image(i, j, k) = static_cast<unsigned char>(newValue);
}
}
}
}
void DarkenandLightenImage(){
string filename;
cout << "Please enter the filename of the image to apply the filter: ";
cin >> filename;
try {
Image photo(filename);
// Ask the user to choose between darkening and lightening
char choice;
cout << "Do you want to darken (D) or lighten (L) the image? ";
cin >> choice;
if (toupper(choice) == 'D') {
// Darkening the image
int darkenFactor;
cout << "Enter the darken factor (0-255): ";
cin >> darkenFactor;
darkenImage(photo, darkenFactor); // Apply darkening filter
} else if (toupper(choice) == 'L') {
// Lightening the image
int lightenFactor;
cout << "Enter the lighten factor (0-255): ";
cin >> lightenFactor;
lightenImage(photo, lightenFactor); // Apply lightening filter
} else {
cout << "Invalid choice. Please enter 'D' for darkening or 'L' for lightening." << endl;
return;
}
// Saving the modified image
string modifiedFilename;
cout << "Enter the filename to save the modified image: ";
cin >> modifiedFilename;
photo.saveImage(modifiedFilename);
} catch (const exception& e) {
cout << "Error: Please enter a valid image name. " << e.what() << endl;
}
}
void CropImage(){
string filename;
cout << "Pls enter the image you want to crop: ";
cin>>filename;
Image image(filename);
int A,B,W,H;
cout<< " please enter the starting points "<<endl;
// get tghe starting points of tghe old image ;
cin>> A >> B;
cout<< " please enter the new width and height "<<endl;
//get diemansions;
cin>> W >> H;
Image image2(W , H);
for (int i = 0;i < W; ++i) {
for (int j = 0; j < H; ++j) {
for (int k = 0; k < 3 ; ++k) {
// every point of new width will be plused by the starting point of the old width;
// and every point of the new height will be plused by the starting point of the old height;
image2 (i,j,k) = image(i+A , j+B , k);
}
}
}
cout << "Pls enter image name to store new image\n";
cout << "and specify extension .jpg, .bmp, .png, .tga: ";
cin >> filename;
image2.saveImage(filename);
}
void addSimpleFrame(Image& image, int red, int green, int blue) {
// Implementation of addSimpleFrame function...
cout << "Adding simple frame to the photo" << endl;
int frameWidth = 40; // Width of the simple frame (adjust as needed)
int height = image.height;
int width = image.width;
// Add top frame
for (int x = 0; x < width; ++x) {
for (int y = 0; y < frameWidth; ++y) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
// Add bottom frame
for (int x = 0; x < width; ++x) {
for (int y = height - frameWidth; y < height; ++y) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
// Add left frame
for (int y = 0; y < height; ++y) {
for (int x = 0; x < frameWidth; ++x) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
// Add right frame
for (int y = 0; y < height; ++y) {
for (int x = width - frameWidth; x < width; ++x) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
}
// Function to add a fancy frame to the image with customizable color
void addFancyFrame(Image& image, int red, int green, int blue) {
// Implementation of addFancyFrame function...
cout << "Adding fancy frame to the photo" << endl;
int frameWidth = 40; // Width of the fancy frame (adjust as needed)
int innerFrameWidth = 20; // Width of the inner white frame (adjust as needed)
int height = image.height;
int width = image.width;
// Add outer frame with customizable color
for (int x = 0; x < width; ++x) {
for (int y = 0; y < frameWidth; ++y) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
for (int x = 0; x < width; ++x) {
for (int y = height - frameWidth; y < height; ++y) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
for (int y = 0; y < height; ++y) {
for (int x = 0; x < frameWidth; ++x) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
for (int y = 0; y < height; ++y) {
for (int x = width - frameWidth; x < width; ++x) {
image(x, y, 0) = red; // Red component
image(x, y, 1) = green; // Green component
image(x, y, 2) = blue; // Blue component
}
}
// Add inner white square frame
int innerXStart = frameWidth + innerFrameWidth;
int innerXEnd = width - frameWidth - innerFrameWidth;
int innerYStart = frameWidth + innerFrameWidth;
int innerYEnd = height - frameWidth - innerFrameWidth;
for (int x = innerXStart; x < innerXEnd; ++x) {
for (int y = innerYStart; y < innerYEnd; ++y) {
if (x >= innerXStart && x < innerXStart + innerFrameWidth) {
// Draw top inner frame
image(x, y, 0) = 255; // Red component (high)
image(x, y, 1) = 255; // Green component (high)
image(x, y, 2) = 255; // Blue component (high)
}
if (x >= innerXEnd - innerFrameWidth && x < innerXEnd) {
// Draw bottom inner frame
image(x, y, 0) = 255; // Red component (high)
image(x, y, 1) = 255; // Green component (high)
image(x, y, 2) = 255; // Blue component (high)
}
if (y >= innerYStart && y < innerYStart + innerFrameWidth) {
// Draw left inner frame
image(x, y, 0) = 255; // Red component (high)
image(x, y, 1) = 255; // Green component (high)
image(x, y, 2) = 255; // Blue component (high)
}
if (y >= innerYEnd - innerFrameWidth && y < innerYEnd) {
// Draw right inner frame
image(x, y, 0) = 255; // Red component (high)
image(x, y, 1) = 255; // Green component (high)
image(x, y, 2) = 255; // Blue component (high)
}
}
}
}
void AddFrame() {
string filename;
string frameType;
string frameColor;
cout << "Please enter the filename: ";
cin >> filename;
cout << "Choose the frame type (simple/fancy): ";
cin >> frameType;
// Ask for frame color
cout << "Choose the frame color (blue/green/red): ";
cin >> frameColor;
Image image(filename);
if (frameType == "simple") {
if (frameColor == "blue") {
addSimpleFrame(image, 0, 0, 255); // Blue frame
} else if (frameColor == "green") {
addSimpleFrame(image, 0, 255, 0); // Green frame
} else if (frameColor == "red") {
addSimpleFrame(image, 255, 0, 0); // Red frame
} else {
cout << "Invalid frame color!" << endl;
return;
}
} else if (frameType == "fancy") {
if (frameColor == "blue") {
addFancyFrame(image, 0, 0, 255); // Blue frame
} else if (frameColor == "green") {
addFancyFrame(image, 0, 255, 0); // Green frame
} else if (frameColor == "red") {
addFancyFrame(image, 255, 0, 0); // Red frame
} else {
cout << "Invalid frame color!" << endl;
return;
}
} else {
cout << "Invalid frame type!" << endl;
return;
}
string outputFileName;
cout << "Enter the name of the output file: ";
cin >> outputFileName;
if (!image.saveImage(outputFileName)) {
cerr << "Error: Failed to save the image." << endl;
return;
}
cout << "Frame added successfully. Image saved as " << outputFileName << endl;
}
// Sobel kernel for horizontal and vertical gradients
const int sobelKernelX[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
const int sobelKernelY[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
// Convert the input image to grayscale
void convertToGrayscale(Image& inputImage) {
for (int i = 0; i < inputImage.width; ++i) {
for (int j = 0; j < inputImage.height; ++j) {
// Compute the average of red, green, and blue channels
int grayValue = (inputImage(i, j, 0) + inputImage(i, j, 1) + inputImage(i, j, 2)) / 3;
// Set the grayscale value for all channels
inputImage(i, j, 0) = grayValue;
inputImage(i, j, 1) = grayValue;
inputImage(i, j, 2) = grayValue;
}
}
}
// Apply Sobel edge detection and convert the image to white with black edges
void applyAndConvert(Image& inputImage) {
// Create a new image to store the edge detection results
Image edgeImage(inputImage.width, inputImage.height);
// Apply Sobel edge detection
for (int i = 1; i < inputImage.width - 1; ++i) {
for (int j = 1; j < inputImage.height - 1; ++j) {
int sumX = 0;
int sumY = 0;
for (int m = -1; m <= 1; ++m) {
for (int n = -1; n <= 1; ++n) {
sumX += inputImage(i + m, j + n, 0) * sobelKernelX[m + 1][n + 1];
sumY += inputImage(i + m, j + n, 0) * sobelKernelY[m + 1][n + 1];
}
}
// Calculate the magnitude
int magnitude = static_cast<int>(sqrt(pow(sumX, 2) + pow(sumY, 2)));
// Set the edge pixel value in the edge image
edgeImage(i, j, 0) = (magnitude > 128) ? 0 : 255; // Black edges and white otherwise
}
}
for (int i = 0; i < inputImage.width; ++i) {
for (int j = 0; j < inputImage.height; ++j) {
int edgePixel = edgeImage(i, j, 0);
// If the edge is detected, set the original image pixel to black
// Otherwise, set it to white
if (edgePixel == 0) {
inputImage(i, j, 0) = 0;
inputImage(i, j, 1) = 0;
inputImage(i, j, 2) = 0;
} else {
inputImage(i, j, 0) = 255;
inputImage(i, j, 1) = 255;
inputImage(i, j, 2) = 255;
}
}
}
}
// Function for edge detection
void EdgeDetection() {
try {
string filename;
cout << "Please enter the name and extension of your image file: ";
cin >> filename;
// Load the input image
Image inputImage(filename);
// Convert the input image to grayscale
convertToGrayscale(inputImage);
// Apply Sobel edge detection and convert the image to white with black edges
applyAndConvert(inputImage);
string outputFilename;
cout << "Please enter the name and extension of the new image file: ";
cin >> outputFilename;
// Save the new image
inputImage.saveImage(outputFilename);
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
}
void ResizingImage() {
string filename;
cout << "Pls enter the image you want to crop: ";
cin >> filename;
Image image(filename);
int W, H;
// Get dimensions of the new image
cout << "Please enter the new dimensions: " << endl;
cin >> W >> H;
Image image2(W, H);
float changing_width = 0;
for (int i = 0; i < W; ++i) {
float changing_height = 0;
for (int j = 0; j < H; ++j) {
for (int k = 0; k < 3; ++k) {
image2(i, j, k) = image(changing_width, changing_height, k);
}
changing_height += (float) image.height / (float) H;
// the value of increasing changing_height is the height of the image divided by the height of the new image
}
changing_width += (float) image.width / (float) W;
// the value of increasing changing_width is the width of the image divided by the width of the new image
}
cout << "Please enter image name to store new image\n";
cout << "and specify extension .jpg, .bmp, .png, .tga: ";
cin >> filename;
image2.saveImage(filename);
}
void applyBoxBlur(Image& image, int blurRadius) {
Image tempImage(image.width, image.height);
for (int y = 0; y < image.height; ++y) {
for (int x = 0; x < image.width; ++x) {
for (int c = 0; c < image.channels; ++c) {
int sum = 0;
int count = 0;
// Calculate average of neighboring pixels
for (int dy = -blurRadius; dy <= blurRadius; ++dy) {
for (int dx = -blurRadius; dx <= blurRadius; ++dx) {
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < image.width && ny >= 0 && ny < image.height) {
sum += image(nx, ny, c);
count++;
}
}
}
tempImage(x, y, c) = static_cast<unsigned char>(sum / count);
}
}
}
// Copy blurred image back to original image
for (int i = 0; i < image.width * image.height * image.channels; ++i) {
image.imageData[i] = tempImage.imageData[i];
}
}
// Function to apply multiple passes of box blur to increase blur effect
void applyMultiplePassesOfBlur(Image& image, int blurRadius, int passes) {
for (int i = 0; i < passes; ++i) {
applyBoxBlur(image, blurRadius);
}
}
// Function to apply the purple filter to an image
void ApplyPurpleFilter() {
string filename;
cout << "Please enter the name and extension of your image file to apply the purple filter: ";
cin >> filename;
try {
Image photo(filename);
for (int i = 0; i < photo.width; ++i) {
for (int j = 0; j < photo.height; ++j) {
unsigned int avg = 0;
for (int k = 0; k < 3; ++k) {
avg += photo(i, j, k);
}
avg /= 3;
photo(i, j, 0) = avg; // Red component
photo(i, j, 2) = avg; // Blue component
photo(i, j, 1) = static_cast<unsigned int>(avg *0.65); // Green component set to half the average for lower intensity
}
}
cout << "Please enter the name and extension to save your new purple image: ";
cin >> filename;
photo.saveImage(filename);
} catch (const exception &e) {
cout << "Error: Please enter a valid image name. " << e.what() << endl;
}
}
void SunlightImage(){
string filename;
cout << "Please enter the name and extension of your image file to apply the sunlight filter filter: ";
cin >> filename;
try {
Image photo(filename);
for (int i = 0; i < photo.width; ++i) {
for (int j = 0; j < photo.height; ++j) {
unsigned int avg = 0;
for (int k = 0; k < 3; ++k) {
avg += photo(i, j, k);
}
avg /= 3;
photo(i, j, 0) = avg;
photo(i, j, 1) = avg;
photo(i, j, 2) = static_cast<unsigned int>(avg *0.65);
}
}
cout << "Please enter the name and extension to save your new sunlight image: ";
cin >> filename;
photo.saveImage(filename);
} catch (const exception &e) {
cout << "Error: Please enter a valid image name. " << e.what() << endl;
}
}
// Function to convert image to infrared
void convertToInfrared(Image& image) {
// Loop through each pixel
for (int y = 0; y < image.height; ++y) {
for (int x = 0; x < image.width; ++x) {
// Get pixel values (RGB)
unsigned char& red = image(x, y, 0);
unsigned char& green = image(x, y, 1);
unsigned char& blue = image(x, y, 2);
// Update pixel values
red = 255 ;
green = 255 - green;
blue = 255 - blue;
}
}
}
int main() {
cout << "* Welcome to the filter service *" << endl;
while (true) {
cout << "1. Grayscale Conversion\n2. Black and White\n3. Invert Image\n4. Merge Images\n5. Flip Image\n6. Rotated Image\n7. Dark and Lighten Image\n8. Crop Image\n9. Add frame\n10. Edge Detection\n11. Resizing Image\n12. Blur Image\n13. Apply Purple\n14. Sunlight Image\n15. Convert To Infrared\n16. Exit\n";
cout << "Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16): ";
int choice;
if (!(cin >> choice)) {
cout << "Invalid input. Please enter an integer number." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
continue;
}
switch (choice) {
case 1:
GrayscaleConversion();
break;
case 2:
BlackAndWhite();
break;
case 3:
InvertImage();
break;
case 4:
MergeImage();
break;
case 5:
FlippingImage();
break;
case 6:
{
string filename;
cout << "Please enter the name and extension of the image file you want to rotate: ";
cin >> filename;
Image image(filename);
string newImageName;
cout << "Enter the name for the rotated image: ";
cin >> newImageName;
int angle;
cout << "Enter the angle to rotate the image (90, 180, or 270 degrees): ";
cin >> angle;
vector<int> angles = {90, 180, 270}; // Store angles as integers
// Use std::find to check if the entered angle is valid
if (find(angles.begin(), angles.end(), angle) != angles.end()) {
rotateImage(image, newImageName, angle);
cout << "Image rotated by " << angle << " degrees saved as '" << newImageName << "'" << endl;
} else {
cout << "Invalid angle. Please enter either 90, 180, or 270." << endl;
}
}
break;
case 7:
DarkenandLightenImage();
break;
case 8:
CropImage();
break;
case 9:
AddFrame();
break;
case 10:
EdgeDetection();
break;
case 11:
ResizingImage();
break;
case 12: {
string filename;
cout << "Please enter file name: ";
cin >> filename;
Image image(filename); // Load the image from file
string newImageName;
cout << "Enter the name for the blurred image: ";
cin >> newImageName;
int blurIntensity;
cout << "Enter blur intensity (1: weak, 2: medium, 3: strong): ";
cin >> blurIntensity;
int blurRadius;
int passes;
// Set blur radius and passes based on blur intensity
switch (blurIntensity) {
case 1:
blurRadius = 5;
passes = 1;
break;
case 2:
blurRadius = 10;
passes = 3;
break;
case 3:
blurRadius = 20;
passes = 5;
break;
default:
cout << "Invalid blur intensity. Using medium intensity blur.";
blurRadius = 10;
passes = 3;
}
applyMultiplePassesOfBlur(image, blurRadius, passes);
image.saveImage(newImageName);
cout << "Image created successfully!\n";
}
break;
case 13:
ApplyPurpleFilter();
break;
case 14:
SunlightImage();
break;
case 15: {
string filename;
cout << "Enter the filename of the image: ";
cin >> filename;
// Load image
Image image(filename);
// Convert to infrared
convertToInfrared(image);
string newImageName;
cout << "Enter the name for the infrared image: ";
cin >> newImageName;
// Save the modified image
image.saveImage(newImageName);
cout << "Image created successfully!\n";
}
break;
case 16:
exit(0); // Exit the program
default:
cout << "Invalid choice. Please select a valid option (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 or 16)." << endl;
break;
}
}
return 0;
}