-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1462 lines (1409 loc) · 58.1 KB
/
main.cpp
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// FCAI – Structured Programming – 2024 - Assignment 3
// File: CS112_A3_Part1_S3_20230309_S4_20230381_20230554.cpp
// Purpose: Baby Photoshop for Image Processing
// app map link: https://drive.google.com/file/d/1efY7Dc3V8tU-0ygiAyMTlVMV-9hApo94/view?usp=sharing
// Github Repo: https://github.com/mohamedtarikz/Assignment-3-Little-photoshop-
// ****************************************************************************************Authors****************************************************************************************
// Mazen nasser >> Group A >> S3 >> 20230309 >> mazen.nasser143@gmail.com >> greyscale, merge, Edit Brightness, Detect image edges, Channel Swap, Wano Day.
// Marwan Hussein Galal >> Group A >> S4 >> 20230381 >> marwanhussein@gmail.com >> black and white, Flip, Crop, infera red, wano TV-NIGHT, Skew, menus, saving system, error handling.
// Mohamed tarek >> Group A >> S4 >> 20230554 >> mohamedtarik06@gmail.com >> Invert, rotate, resize, blur, pixelate, frames, Oil Painting.
// ***************************************************************************************End Authors*************************************************************************************
// TA: Ahmed Fouad
// Version: 2.0
// Last Modification Date: 17/04/2024
// ================================================================================================================================================================================= //
#include "library\encryption-decryption.h"
Image img_in;
Image img_filter;
string imginput = "NULL";
string saveindicator = "N";
// Function to check if a string is numeric
bool isNumeric(string str) {
for (int i = 0; i < str.length(); i++) {
// Check each character of the string
if (isdigit(str[i]) == false)
return false; // Return false if a non-numeric character is found
}
// Return true if all characters are numeric
return true;
}
// Function to convert the input image to grayscale
void greyscale() {
// Iterate through each pixel in the image
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
int av = 0; // Initialize average value
// Calculate average pixel value across RGB channels
for (int k = 0; k < 3; k++) {
av += img_filter(i, j, k);
}
av /= 3; // Compute the average
// Set each RGB channel to the average value to convert to grayscale
for (int k = 0; k < 3; k++) {
img_filter(i, j, k) = av;
}
}
}
}
// Function to convert the input image to black and white
void black_and_white() {
// Convert the input image to grayscale
greyscale();
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
// Set the color of the pixel to max white or black
for (int k = 0; k < 3; k++) {
if (img_filter(i, j, k) < 128) {
img_filter(i, j, k) = 0;
} else {
img_filter(i, j, k) = 255;
}
}
}
}
}
// Function to crop an image
void crop_image() {
// Declare variables to store user input for coordinates and dimensions
string X, Y, W, H;
int x, y, w, h;
// Continue prompting for input until valid values are provided
while (true) {
cout << "\ncurrent possetion is: ( 0 , 0 )" << endl;
cout << "current Image Size is: " << img_filter.width << " X " << img_filter.height << endl;
// Prompt user to enter starting X coordinate
cout << "Enter starting X coordinate: ";
cin >> X;
// Prompt user to enter starting Y coordinate
cout << "Enter starting Y Coordinate: ";
cin >> Y;
// Prompt user to enter the width of the crop area
cout << "Enter the width of the cropped picture: ";
cin >> W;
// Prompt user to enter the height of the crop area
cout << "Enter the height of the cropped picture: ";
cin >> H;
// Check if any input is not numeric
if (!isNumeric(X) || !isNumeric(Y) || !isNumeric(W) || !isNumeric(H)) {
cout << "Invalid input! Please enter positive integer values." << endl;
continue;
}
// Convert input strings to integers
try {
x = stoi(X), y = stoi(Y), w = stoi(W), h = stoi(H);
} catch (out_of_range) {
cout << "Coordinates are out of bounds." << endl;
}
// Check if the specified crop area is within the bounds of the image
if (x > img_filter.width || y > img_filter.height || w > img_filter.width - x || h > img_filter.height - y) {
cout << "Coordinates are out of bounds." << endl;
continue;
} else {
// Break out of the loop if input is valid
break;
}
}
// Initialize variables for iterating through cropped image
int M = 0, N = 0;
// Create a new image to store the cropped region
Image img_cropped(w, h);
// Iterate over the specified region of the input image and copy pixels to cropped image
for (int i = x; i < w + x; i++) {
for (int j = y; j < h + y; j++) {
for (int k = 0; k < 3; k++) {
// Copy pixel values from input image to cropped image
img_cropped(M, N, k) = img_filter(i, j, k);
}
N++;
}
M++;
N = 0;
}
img_filter = img_cropped;
}
// Function to darken or lighten an image based on user choice
int edit_brightness() {
string DorL_choice;
// Loop until a valid choice is entered
while (true) {
// Display menu options to the user
cout << "\n*** What do you want to do with your image? ***\n";
cout << "================================================\n";
cout << "A) Darken\n";
cout << "B) Brighten\n";
cout << "C) Back to filters menu\n";
cout << "================================================\n";
cout << "Enter your choice: ";
cin >> DorL_choice;
// Check if the choice is valid, if not, prompt the user to enter a valid choice
transform(DorL_choice.begin(), DorL_choice.end(), DorL_choice.begin(), ::toupper);
if (DorL_choice != "A" && DorL_choice != "B" && DorL_choice != "C") {
cout << "Please enter a valid choice" << endl;
continue;
}
// If the user chooses to go back to the filters menu, return 0 to exit the function
if (DorL_choice == "C") {
return 0;
}
break;
}
// Loop through each pixel in the image
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
for (int k = 0; k < 3; k++) {
if (DorL_choice == "A") {
img_filter(i, j, k) /= 2; // Divide pixel value by 2 to darken
} else if (DorL_choice == "B") {
img_filter(i, j, k) = min(int(img_filter(i, j, k) * 1.5), 255); // Multiply pixel value by 1.5 to brighten
}
}
}
}
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return 0;
}
// Function to invert the colors of the input image
void invert_image() {
// Iterate through each pixel in the image
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
// Invert each RGB channel by subtracting from 255
for (int k = 0; k < 3; k++) {
img_filter(i, j, k) = 255 - (img_filter(i, j, k));
}
}
}
}
// Filter to detect image edges
void detect_edge() {
// Convert input image to black and white
black_and_white();
Image img_Detected_edges(img_filter.width, img_filter.height);
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
for (int k = 0; k < 3; k++) {
// Check if the current pixel is not on the image border
if (i != 0 && i != img_filter.width - 1 && j != 0 && j != img_filter.height - 1) {
// Check if the current pixel is black and any of its neighboring pixels are white
if ((img_filter(i, j, k) == 0 && img_filter(i - 1, j, k) == 255) ||
(img_filter(i, j, k) == 0 && img_filter(i + 1, j, k) == 255) ||
(img_filter(i, j, k) == 0 && img_filter(i, j - 1, k) == 255) ||
(img_filter(i, j, k) == 0 && img_filter(i, j + 1, k) == 255)) {
// If any of the neighboring pixels are white, set the corresponding pixel in Detected_edges to black
img_Detected_edges(i, j, k) = 0;
} else {
// If none of the neighboring pixels are white, set the corresponding pixel in Detected_edges to white
img_Detected_edges(i, j, k) = 255;
}
} else {
// If the pixel is on the image border, retain its original color in Detected_edges
img_Detected_edges(i, j, k) = img_filter(i, j, k);
}
}
}
}
img_filter = img_Detected_edges;
}
// Filter to apply blur effect
void blur() {
// Declare variables
string R;
int r, sr, er, sc, ec;
long long int sum, area;
// Create a new image for the blurred result with the same dimensions as the input image
Image blur_img(img_filter.width, img_filter.height);
// Prompt the user to enter the blur radius
while (true) {
// Prompt user to enter starting X coordinate
cout << "Enter the strength of the blur (3 - 100): ";
cin >> R;
// Convert input strings to integers
// Check if any input is not numeric
if (!isNumeric(R)) {
cout << "Invalid input! Please enter positive integer values." << endl;
continue;
}
try {
r = stoi(R);
} catch (out_of_range) {
cout << "Invalid input! Please enter a number within the range." << endl;
}
if (r > 100 || r < 3) {
cout << "Invalid input! Please enter a number within the range." << endl;
continue;
} else {
// Break out of the loop if input is valid
break;
}
}
// Store the width and height of the input image
int w = img_filter.width;
int h = img_filter.height;
// Initialize cumulative arrays for each color channel using dynamic memory allocation
long long int*** cmlt = new long long int** [w + 1];
long long int*** row = new long long int** [w + 1];
for (int i = 0; i <= w; ++i) {
cmlt[i] = new long long int* [h + 1];
row[i] = new long long int* [h + 1];
for (int j = 0; j <= h; ++j) {
cmlt[i][j] = new long long int[3];
row[i][j] = new long long int[3];
memset(cmlt[i][j], 0, sizeof(long long int) * 3); // Initialize cumulative arrays with zeros
memset(row[i][j], 0, sizeof(long long int) * 3); // Initialize row arrays with zeros
}
}
// Copy pixel values from input image to cumulative and row arrays
for (int i = 1; i <= w; ++i) {
for (int j = 1; j <= h; ++j) {
for (int k = 0; k < 3; ++k) {
cmlt[i][j][k] = row[i][j][k] = img_filter(i - 1, j - 1, k); // Copy pixel values
}
}
}
// Calculate cumulative sum for each row
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= h; j++) {
for (int k = 0; k < 3; k++) {
row[i][j][k] += row[i - 1][j][k]; // Add previous pixel value in row
}
}
}
// Calculate cumulative sum for each column
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
for (int k = 0; k < 3; k++) {
if (!i)
cmlt[j][i][k] = row[j][i][k]; // First row cumulative sum is same as row value
else if (!j) {
cmlt[j][i][k] += cmlt[j][i - 1][k]; // Add previous column value
} else {
cmlt[j][i][k] = cmlt[j][i - 1][k] + row[j][i][k]; // Add previous row and column value
}
}
}
}
// Apply blur to each pixel
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= h; j++) {
for (int k = 0; k < 3; k++) {
// Define region of pixels to be blurred
sc = max(i - r, 1); ec = min(i + r, w);
sr = max(j - r, 1); er = min(j + r, h);
area = ((er - sr) * (ec - sc));
// Calculate sum of pixel values within the region
sum = cmlt[ec][er][k] - cmlt[ec][sr - 1][k] - cmlt[sc - 1][er][k] + cmlt[sc - 1][sr - 1][k];
// Calculate average pixel value within the region
sum /= area;
// Set blurred pixel value in the output image
blur_img(i - 1, j - 1, k) = (sum <= 255 ? sum : 255); // Clamp to maximum pixel value (255)
}
}
}
// Update the input image with the blurred image
img_filter = blur_img;
// Deallocate memory
for (int i = 0; i <= w; ++i) {
for (int j = 0; j <= h; ++j) {
delete[] cmlt[i][j];
delete[] row[i][j];
}
delete[] cmlt[i];
delete[] row[i];
}
delete[] cmlt;
delete[] row;
}
// Function to resize the image
Image resize(Image img_resize, int newWidth, int newHeight) {
int width = img_resize.width, height = img_resize.height;
// Calculate scaling factors for width
int scale1, scale2;
scale1 = max(width, newWidth) / min(newWidth, width);
scale2 = scale1 + 1;
// Calculate division of the new image width into two parts
int FirstPart = scale2 * min(newWidth, width) - max(width, newWidth);
int SecondPart = (max(width, newWidth) - scale1 * FirstPart) / scale2;
int StartofSecondpart = SecondPart * scale2;
// Create a new image with the desired width and original height
Image Resized(newWidth, height);
if (newWidth <= width) {
// If the new width is less than or equal to the original width
// Create two images representing the first and second parts of the resized image
Image FirstImage(FirstPart, height);
Image SecondImage(SecondPart, height);
int idx, fixed, avrg;
// For each row in the image
for (int j = 0; j < height; j++) {
idx = 0;
fixed = StartofSecondpart;
// Process the first part of the resized image
for (int h = 0; h < FirstPart; h++) {
for (int k = 0; k < 3; ++k) {
avrg = 0;
idx = fixed;
// Calculate average pixel value for each channel
for (int i = 0; i < scale1; i++) {
avrg += img_resize(idx, j, k);
idx++;
if (idx >= width) {
break;
}
}
avrg /= scale1;
// Assign the average pixel value to the corresponding position in the FirstImage
FirstImage(h, j, k) = min(avrg, 255);
}
fixed += scale1;
}
// Process the second part of the resized image
idx = 0;
fixed = 0;
for (int h = 0; h < SecondPart; h++) {
for (int k = 0; k < 3; ++k) {
avrg = 0;
idx = fixed;
for (int i = 0; i < scale2; i++) {
avrg += img_resize(idx, j, k);
idx++;
if (idx >= width) {
break;
}
}
avrg /= scale2;
// Assign the average pixel value to the corresponding position in the SecondImage
SecondImage(h, j, k) = min(avrg, 255);
}
fixed += scale2;
}
}
// Merge the two parts into the final resized image
for (int i = 0; i < Resized.height; i++) {
for (int j = 0; j < Resized.width; ++j) {
for (int k = 0; k < 3; ++k) {
Resized(j, i, k) = (j >= SecondPart ? FirstImage(j - SecondPart, i, k) : SecondImage(j, i, k));
}
}
}
} else {
// If the new width is greater than the original width
// Process each row and scale the pixels accordingly
int idx;
for (int j = 0; j < height; j++) {
idx = 0;
for (int i = 0; i < width; i++) {
if (i >= FirstPart) {
// Scale the pixels in the first part
for (int h = 0; h < scale2; h++) {
for (int k = 0; k < 3; ++k) {
Resized(idx, j, k) = img_resize(i, j, k);
}
idx++;
}
} else {
// Scale the pixels in the second part
for (int h = 0; h < scale1; h++) {
for (int k = 0; k < 3; ++k) {
Resized(idx, j, k) = img_resize(i, j, k);
}
idx++;
}
}
}
}
}
// Update the input image to the resized image
img_resize = Resized;
width = img_resize.width;
height = img_resize.height;
// Calculate scaling factors for height
scale1 = max(height, newHeight) / min(newHeight, height);
scale2 = scale1 + 1;
// Calculate division of the new image height into two parts
FirstPart = scale2 * min(newHeight, height) - max(height, newHeight);
SecondPart = (max(height, newHeight) - scale1 * FirstPart) / scale2;
StartofSecondpart = SecondPart * scale2;
// Create a new image with the resized width and desired height
Image Resizedall(width, newHeight);
if (newHeight <= height) {
// If the new height is less than or equal to the original height
// Create two images representing the first and second parts of the resized image
Image FirstImage(width, FirstPart);
Image SecondImage(width, SecondPart);
int idx, fixed, avrg;
// For each column in the image
for (int j = 0; j < width; j++) {
idx = 0;
fixed = StartofSecondpart;
// Process the first part of the resized image
for (int h = 0; h < FirstPart; h++) {
for (int k = 0; k < 3; ++k) {
avrg = 0;
idx = fixed;
// Calculate average pixel value for each channel
for (int i = 0; i < scale1; i++) {
avrg += img_resize(j, idx, k);
idx++;
if (idx >= height) {
break;
}
}
avrg /= scale1;
// Assign the average pixel value to the corresponding position in the FirstImage
FirstImage(j, h, k) = min(avrg, 255);
}
fixed += scale1;
}
// Process the second part of the resized image
idx = 0;
fixed = 0;
for (int h = 0; h < SecondPart; h++) {
for (int k = 0; k < 3; ++k) {
avrg = 0;
idx = fixed;
for (int i = 0; i < scale2; i++) {
avrg += img_resize(j, idx, k);
idx++;
if (idx >= height) {
break;
}
}
avrg /= scale2;
// Assign the average pixel value to the corresponding position in the SecondImage
SecondImage(j, h, k) = min(avrg, 255);
}
fixed += scale2;
}
}
// Merge the two parts into the final resized image
for (int j = 0; j < Resizedall.width; ++j) {
for (int i = 0; i < Resizedall.height; i++) {
for (int k = 0; k < 3; ++k) {
Resizedall(j, i, k) = (i >= SecondPart ? FirstImage(j, i - SecondPart, k) : SecondImage(j, i, k));
}
}
}
} else {
// If the new height is greater than the original height
// Process each column and scale the pixels accordingly
int idx;
for (int j = 0; j < width; j++) {
idx = 0;
for (int i = 0; i < height; i++) {
if (i >= FirstPart) {
// Scale the pixels in the first part
for (int h = 0; h < scale2; h++) {
for (int k = 0; k < 3; ++k) {
Resizedall(j, idx, k) = img_resize(j, i, k);
}
idx++;
}
} else {
// Scale the pixels in the second part
for (int h = 0; h < scale1; h++) {
for (int k = 0; k < 3; ++k) {
Resizedall(j, idx, k) = img_resize(j, i, k);
}
idx++;
}
}
}
}
}
// Return the resized image
return Resizedall;
}
// Function to handle resizing options for the image
void resize_menu() {
string resizechoice; // Variable to store user's resize menu choice
int newWidth, newHeight; // Variables to store new dimensions for the image
Image img_resize = img_filter; // Create a copy of the filtered image to work with
while (true) {
cout << "\n*** How do you want to Resize your image? ***\n";
cout << "==============================================\n";
cout << "A) Resize by dimensions\n";
cout << "B) Resize by scale\n";
cout << "C) Back to filters menu\n";
cout << "==============================================\n";
cout << "Enter your choice: ";
cin >> resizechoice;
transform(resizechoice.begin(), resizechoice.end(), resizechoice.begin(), ::toupper); // Convert choice to uppercase
if (resizechoice == "A") { // If user chooses to resize by dimensions
while (true) {
cout << "Enter your new desired dimensions(\"Width Height\"): ";
cin >> newWidth >> newHeight; // Read new dimensions from user input
if (newWidth <= 0 || newHeight <= 0) {
cout << "Please enter a width and hight grater than 0" << endl;
continue;
}
}
img_filter = resize(img_filter, newWidth, newHeight); // Resize the image
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (resizechoice == "B") { // If user chooses to resize by scale
double scalew, scaleh; // Variables to store scaling factors
while (true) {
cout << "Enter your desired scale for each dimension(\"ScaleWidth ScaleHeight\"): ";
cin >> scalew >> scaleh; // Read scaling factors from user input
if (scalew <= 0 || scaleh <= 0) {
cout << "Please enter a scale grater than 0" << endl;
continue;
}
if (scalew > 10 || scaleh > 10) {
cout << "Please enter a scale less than 10" << endl;
continue;
} else {
break;
}
}
// Calculate new dimensions based on the scaling factors
newWidth = (double)(img_resize.width * scalew);
newHeight = (double)(img_resize.height * scaleh);
img_filter = resize(img_filter, newWidth, newHeight); // Resize the image
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (resizechoice == "C") { // If user chooses to go back to filters menu
return;
} else {
cout << "Please enter a valid choice" << endl;
}
}
}
// Function to merge the input image with another image
void merge_images(Image img_first, Image img_second) {
// Create a new image for merging with dimensions equal to the minimum of the two input images
Image img_merged(min(img_first.width, img_second.width), min(img_first.height, img_second.height));
// Iterate through each pixel in the merged image
for (int i = 0; i < min(img_first.width, img_second.width); i++) {
for (int j = 0; j < min(img_first.height, img_second.height); j++) {
// Average the RGB values of corresponding pixels from both images and assign to the merged image
for (int k = 0; k < 3; k++) {
img_merged(i, j, k) = (img_first(i, j, k) + img_second(i, j, k)) / 2;
}
}
}
img_filter = img_merged;
}
// Function to handle the merging of images and provide options for the user
void merge_images_menu() {
string mergechoice; // Variable to store user's merge menu choice
Image img_one, img_two; // Variables to store the two images to be merged
int max_width, max_height, min_width, min_height; // Variables to store dimensions of the images
img_one = img_filter; // Set the first image to the filtered image
while (true) {
cout << "\n**what do you like to enter?**" << endl;
cout << "==============================" << endl;
cout << "A) Resize to the smallest dimension" << endl;
cout << "B) Resize to the greatest dimension" << endl;
cout << "C) Merge common area" << endl;
cout << "D) Back to filters menu" << endl;
cout << "==============================" << endl;
cout << "Enter your choice: ";
cin >> mergechoice;
transform(mergechoice.begin(), mergechoice.end(), mergechoice.begin(), ::toupper); // Convert choice to uppercase
if (mergechoice != "D") { // If the user didn't choose to go back to filters menu
string secinputimage; // Variable to store the name of the second image
// Loop until a valid second image is loaded
while (true) {
cout << "Please enter second image name with its extension: ";
cin >> secinputimage;
try {
img_two.loadNewImage(secinputimage); // Attempt to load the second image
break;
} catch (invalid_argument) {
continue;
}
}
}
// Determine the maximum and minimum dimensions of the two images
if (img_one.width > img_two.width) {
max_width = img_one.width;
min_width = img_two.width;
} else {
max_width = img_two.width;
min_width = img_one.width;
}
if (img_one.height > img_two.height) {
max_height = img_one.height;
min_height = img_two.height;
} else {
max_height = img_two.height;
min_height = img_one.height;
}
// Process user's merge choice
if (mergechoice == "A") {
img_one = resize(img_one, min_width, min_height); // Resize the first image to the smallest dimensions
img_two = resize(img_two, min_width, min_height); // Resize the second image to the smallest dimensions
merge_images(img_one, img_two); // Merge the images
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (mergechoice == "B") {
img_one = resize(img_one, max_width, max_height); // Resize the first image to the greatest dimensions
img_two = resize(img_two, max_width, max_height); // Resize the second image to the greatest dimensions
merge_images(img_one, img_two); // Merge the images
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (mergechoice == "C") {
merge_images(img_one, img_two); // Merge the images without resizing
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (mergechoice == "D") {
return; // Return to the filters menu
} else {
cout << "Please enter a valid choice" << endl;
}
}
}
// Function to flip the input image horizontally
void flip_horizontally() {
// Create a new image to store the horizontally flipped image
Image img_flipped(img_filter.width, img_filter.height);
// Iterate through each pixel in the image
for (int i = img_filter.width - 1; i >= 0; i--) {
for (int j = 0; j < img_filter.height; j++) {
// Flip the image in the x-direction
for (int k = 0; k < 3; k++) {
img_flipped(img_filter.width - 1 - i, j, k) = img_filter(i, j, k);
}
}
}
img_filter = img_flipped;
}
// Function to flip the input image vertically
void flip_vertically() {
// Create a new image to store the vertically flipped image
Image img_flipped(img_filter.width, img_filter.height);
// Iterate through each pixel in the image
for (int j = img_filter.height - 1; j >= 0; j--) {
for (int i = 0; i < img_filter.width; i++) {
// Flip the image in the y-direction
for (int k = 0; k < 3; k++) {
img_flipped(i, img_filter.height - 1 - j, k) = img_filter(i, j, k);
}
}
}
img_filter = img_flipped;
}
// flipping an image using user choice of direction menu
void flip_image_menu() {
string flipchoice;
while (true) {
cout << "\n*** How do you want to flip your image? ***\n";
cout << "===========================================\n";
cout << "A) Flip horizontally\n";
cout << "B) Flip vertically\n";
cout << "C) Flip horizontally and vertically\n";
cout << "D) Back to filters menu\n";
cout << "===========================================\n";
cout << "Enter your choice: ";
cin >> flipchoice;
transform(flipchoice.begin(), flipchoice.end(), flipchoice.begin(), ::toupper);
if (flipchoice == "A") {
flip_horizontally();
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (flipchoice == "B") {
flip_vertically();
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (flipchoice == "C") {
flip_horizontally();
flip_vertically();
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (flipchoice == "D") {
return;
} else {
cout << "Please enter a valid choice" << endl;
}
}
}
// This function rotates an image 90 degrees clockwise.
void rotate_image_90deg() {
// Create a new Image object with dimensions swapped to accommodate rotation.
Image img_rotated(img_filter.height, img_filter.width);
// Iterate over the pixels of the original image.
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
// Iterate over the color channels (assuming RGB here).
for (int k = 0; k < 3; k++) {
// Assign pixel value from original image to the corresponding position
// in the rotated image, swapping x and y coordinates.
img_rotated(j, i, k) = img_filter(i, j, k);
}
}
}
img_filter = img_rotated;
}
// This function provides options to rotate the image based on user input.
void rotate_image_menu() {
string flipchoice; // Variable to store user's choice
while (true) { // Infinite loop to repeatedly prompt the user until a valid choice is made
// Display menu options
cout << "\n*** How do you want to rotate your image? ***\n";
cout << "==============================================\n";
cout << "A) Rotate 90\n";
cout << "B) Rotate 180\n";
cout << "C) Rotate 270\n";
cout << "D) Back to filters menu\n";
cout << "==============================================\n";
cout << "Enter your choice: ";
cin >> flipchoice; // Read user's choice
transform(flipchoice.begin(), flipchoice.end(), flipchoice.begin(), ::toupper); // Convert user's choice to uppercase
// Process user's choice
if (flipchoice == "A") {
// Rotate the image 90 degrees clockwise
flip_vertically();
rotate_image_90deg();
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (flipchoice == "B") {
// Rotate the image 180 degrees
flip_horizontally();
flip_vertically();
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (flipchoice == "C") {
// Rotate the image 270 degrees clockwise
flip_horizontally();
rotate_image_90deg();
saveindicator = "N";
cout << "Operation completed successfully!" << endl;
return;
} else if (flipchoice == "D") {
return;
} else {
cout << "Please enter a valid choice" << endl;
}
}
}
// Function to apply a "Wanno Day" filter to the input image
void Wanno_Day() {
// Create a new image with the same dimensions as the input image
Image Wanno_Day_img(img_filter.width, img_filter.height);
// Loop through each pixel in the input image
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
Wanno_Day_img(i, j, 0) = min((img_filter(i, j, 0) + 20), 255);
Wanno_Day_img(i, j, 1) = min((img_filter(i, j, 1) + 20), 255);
Wanno_Day_img(i, j, 2) = max((img_filter(i, j, 2) - 40), 0);
}
}
img_filter = Wanno_Day_img;
}
// Function to apply a "Wanno Night" filter to the input image
void Wanno_Night() {
// Create a new image with the same dimensions as the input image
Image Wanno_Night_img(img_filter.width, img_filter.height);
// Loop through each pixel in the input image
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
Wanno_Night_img(i, j, 0) = min((img_filter(i, j, 0) + 20), 255);
Wanno_Night_img(i, j, 1) = max((img_filter(i, j, 1) - 40), 0);
Wanno_Night_img(i, j, 2) = min((img_filter(i, j, 2) + 20), 255);
}
}
img_filter = Wanno_Night_img;
}
// Function to apply noise "Wanno TV" filter to the input image
void Wanno_TV() {
srand(unsigned(time(0)));
// Create a new image with the same dimensions as the input image
Image Wanno_TV_img(img_filter.width, img_filter.height);
// Loop through each pixel in the input image
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
for (int k = 0; k < 3; k++) {
int randomvalue = rand() % 21;
if (img_filter(i, j, k) > 20) { // If pixel has vale more than 225 set it to its original value
Wanno_TV_img(i, j, k) = min(int((img_filter(i, j, k) - randomvalue) * 1.07), 255); // subtract a random value between 1 and 20 to make noise
} else {
Wanno_TV_img(i, j, k) = img_filter(i, j, k) + 1;
}
}
}
}
img_filter = Wanno_TV_img;
}
// Function to invert the red channel of the input image
void infera_red() {
for (int i = 0; i < img_filter.width; i++) {
for (int j = 0; j < img_filter.height; j++) {
for (int k = 0; k < 3; k++) {
// If the current channel is the red channel (k = 0)
if (k == 0) {
// Set the red channel to the maximum value of 255
img_filter(i, j, k) = 255;
} else {
// Otherwise, invert the current channel (subtract the value of the current channel from 255)
img_filter(i, j, k) = 255 - img_filter(i, j, k);
}
}
}
}
}
// Filter to pixelate the image
void pixelate() {
// Prompt user for pixelation radius
int r;
string R;
while (true) {
cout << "Enter radius of pixelation (the more the stronger the effect) (0 - 10): ";
cin >> R;
if (!isNumeric(R)){
cout << "Invalid radius. Please enter a value between 0 and 10" << endl;
continue;
}
r = stoi(R);
if (r < 0 && r > 10) {
cout << "Invalid radius. Please enter a value between 0 and 10";
continue;
}
else {
break;
}
}
// Create a new image to store the pixelated result
Image pixels(img_filter.width, img_filter.height);
// Iterate through the image pixels with step size of r
for (int i = 0; i < img_filter.width; i += r) {
for (int j = 0; j < img_filter.height; j += r) {
for (int k = 0; k < 3; k++) {
unsigned char x = img_filter(i, j, k);
// Find the maximum color value within the pixel block
for (int l = 0; l < r; ++l) {
for (int m = 0; m < r; ++m) {
x = max((int)x, (int)img_filter(min(i + l, img_filter.width - 1), min(j + m, img_filter.height - 1), k));
}
}
// Set the pixel block to the maximum color value
for (int l = 0; l < r; ++l) {
for (int m = 0; m < r; ++m) {
pixels(min(i + l, img_filter.width - 1), min(j + m, img_filter.height - 1), k) = x;
}
}
}
}
}
// Update the input image with the pixelated result
img_filter = pixels;
}
// Filter to make Oil Painting effect
void oil_painted() {
// Create a copy of the input image to store the oil-painted effect
Image img_oil_paint(img_filter);
// Reduce the color precision of each pixel
for (int i = 0; i < img_oil_paint.width; i++) {
for (int j = 0; j < img_oil_paint.height; j++) {
for (int k = 0; k < 3; k++) {
// Divide the color value of each channel by 40 to reduce precision
img_oil_paint(i, j, k) /= 40;
// Multiply the result by 40 to bring it closer to its original value
img_oil_paint(i, j, k) *= 40;
}
}
}
// Update the input image with the oil-painted result
img_filter = img_oil_paint;
}
void normal_frame() {
// Prompt user for frame thickness and desired RGB values
int thck, r, g, b;
cout << "Enter frame thickness: ";
cin >> thck;
cout << "Enter the desired RGB values (\"Rval Gval Bval\"): ";
cin >> r >> g >> b;
// Create a new image for the framed result
Image framed(img_filter.width + 2 * thck, img_filter.height + 2 * thck);
// Fill the frame area with the desired RGB values
for (int i = 0; i < framed.width; i++) {
for (int j = 0; j < framed.height; j++) {
framed(i, j, 0) = r;
framed(i, j, 1) = g;
framed(i, j, 2) = b;
}
}
// Copy the original image into the frame area
for (int i = thck; i < thck + img_filter.width; i++) {
for (int j = thck; j < thck + img_filter.height; j++) {
for (int k = 0; k < 3; k++) {
framed(i, j, k) = img_filter(i - thck, j - thck, k);
}
}
}
// Save the framed image and update the input image
framed.saveImage("framed.png");
img_filter = framed;
}
void fancy_frame() {
// Prompt user for frame thickness and desired RGB values for both frames
int thck, r1, g1, b1, r2, g2, b2;
cout << "Enter frame Thickness: ";
cin >> thck;
cout << "Enter first desired RGB values (\"Rval Gval Bval\"): ";
cin >> r1 >> g1 >> b1;
cout << "Enter second desired RGB values (\"Rval Gval Bval\"): ";