forked from Salmanahmed22/assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS213-2023-20221072-20220195-20220326-A1-FULL.cpp
More file actions
734 lines (664 loc) · 19.5 KB
/
Copy pathCS213-2023-20221072-20220195-20220326-A1-FULL.cpp
File metadata and controls
734 lines (664 loc) · 19.5 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
/*بسم الله الرحمن الرحيم
اللهم أشرح لي صدري
ويسر لي أمري
واحلل عقدة من لساني
*/
// FCAI – OOP Programming – 2023 - Assignment 1
// Program Name: CS213-2023-20221072-20220195-20220326-A1-FULL.cpp
// Last Modification Date: 9/10/2023
// Author1 and ID and Group: Abdelrahman Mohamed Ahmed 20220195 boodymohammed26@gmail.com
// Author2 and ID and Group: Salman Ahmed Hamed 20221072 solom.9850@gmail.com
// Author3 and ID and Group: Marwan Mohamed Abdellatif 20220326 marwanmohamed200415@gmail.com
//include file and libraries
#include <bits/stdc++.h>
#include "bmplib.h"
#include "bmplib.cpp"
using namespace std;
//defines
#define ll long long
#define e '\n'
#define sp ' '
#define END return 0;
//variable initialization
const int N = 1e5 + 5;
unsigned char img[SIZE][SIZE];
//directional array
int dx[] = {0,1,0,-1,0,1,1,-1,-1};
int dy[] = {0,0,1,0,-1,1,-1,1,-1};
//functions declaration
void menu();
void load_image();
void filters();
void black_white();
void invert();
void merge();
void flip();
void rotate();
void dark_light();
void detect_edges();
void enlarge();
void shrink();
void mirror();
void shuffle();
void skew_right();
void blur();
void crop();
void skew_up();
void save();
bool valid(char c);
void save_choice();
int main(){
menu();
load_image();
filters();
END
}
//function initialization
void menu(){
//program interface
cout<<"_______________________________________________________"<<e;
cout<<"| Welcome to our photo editor program for (GS images) |"<<e;
cout<<"_______________________________________________________"<<e;
}
//______________________________________________________________________________//
void load_image(){
char text[1000];
//input image
cout<<"Enter image name you want to edit: ";
cin>>text;
strcat(text,".bmp");
readGSBMP(text,img);
}
//______________________________________________________________________________//
void filters(){
//choose filter
char c;
do {
cout << "1- Black & White Filter\n"
"2- Invert Filter\n"
"3- Merge Filter \n"
"4- Flip Image\n"
"5- Darken and Lighten Image \n"
"6- Rotate Image\n"
"7- Detect Image Edges \n"
"8- Enlarge Image\n"
"9- Shrink Image\n"
"a- Mirror 1/2 Image\n"
"b- Shuffle Image\n"
"c- Blur Image\n"
"d- Crop Image\n"
"e- Skew Image Right\n"
"f- Skew Image Up\n"
"s- Save the image to a file\n"
"0- Exit" << e << e;
cout << "Choose filter to Apply OR s to Save OR 0 to Exit: ";
cin >> c;
while (!valid(c)) {
cout << "Please Enter a valid letter: ";
cin >> c;
}
if (c == '1')
black_white();
else if (c == '2')
invert();
else if (c == '3')
merge();
else if (c == '4')
flip();
else if (c == '5')
dark_light();
else if (c == '6')
rotate();
else if (c == '7')
detect_edges();
else if (c == '8')
enlarge();
else if (c == '9')
shrink();
else if (c == 'a')
mirror();
else if (c == 'b')
shuffle();
else if (c == 'c')
blur();
else if (c == 'd')
crop();
else if (c == 'e')
skew_right();
else if (c == 'f')
skew_up();
else if (c == 's')
save();
else
break;
}while(c != '0');
}
//______________________________________________________________________________//
void black_white(){
float sum = 0, cnt = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j< SIZE; j++) {
sum += img[i][j];
cnt++;
}
}
float avr = sum / cnt;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j< SIZE; j++) {
if (img[i][j] > avr)
img[i][j] = 255.0;
else
img[i][j] = 0.0;
}
}
save_choice();
}
//______________________________________________________________________________//
void invert(){
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = (255 - img[i][j]);
}
}
save_choice();
}
//______________________________________________________________________________//
void merge(){
unsigned char img1[SIZE][SIZE];
char text2[1000];
cout<<"Enter image name you want to merge:"<<e;
cin>>text2;
strcat(text2,".bmp");
readGSBMP(text2,img1);
for(int i = 0; i < SIZE ; i++) {
for (int j = 0; j < SIZE; j++) {
img[i][j] = (img[i][j] + img1[i][j]) / 2;
}
}
save_choice();
}
//______________________________________________________________________________//
void flip(){
unsigned char img1[SIZE][SIZE];
cout<< "To Flip the image Horizontally Enter (h)\n"
"To Flip the image Vertically Enter (v): ";
char c1; cin>>c1;
while (c1 != 'h' and c1 != 'v'){
cout<< "Please Enter a Valid Letter:"
"Horizontally (h)\n"
"Vertically (v): ";
cin>> c1;
}
if (c1 == 'h') {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[i][j] = img[i][SIZE-1-j];
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
}
else if (c1 == 'v') {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[i][j] = img[SIZE - 1 - i][j];
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
}
save_choice();
}
//______________________________________________________________________________//
void rotate(){
unsigned char img1[SIZE][SIZE];
cout<<"To Rotate 90 Degrees Enter (90) : \n"
"To Rotate 180 Degrees Enter (180) : \n"
"To Rotate 270 Degrees Enter (270) : \n";
int n;cin>>n;
while(n != 90 and (n != 180 and n != 270)){
cout<<"Please Enter a Valid Number";
cin>>n;
}
if(n==90){
for (int i = 0; i <SIZE ; ++i) {
for (int j = 0 ; j < SIZE ; j++){
img1[j][SIZE-1-i]=img[i][j];
}
}
for (int i = 0; i <SIZE ; ++i) {
for (int j = 0 ; j < SIZE ; j++){
img[i][j]=img1[i][j];
}
}
}else if(n==180){
for (int i = 0; i <SIZE ; ++i) {
for (int j = 0 ; j < SIZE ; j++){
img1[SIZE-1-i][SIZE-1-j]=img[i][j];
}
}
for (int i = 0; i <SIZE ; ++i) {
for (int j = 0 ; j < SIZE ; j++){
img[i][j]=img1[i][j];
}
}
}else if (n==270){
for(int i = 0;i < SIZE ; i++){
for (int j = SIZE-1; j >= 0 ; j--){
img1[SIZE-1-j][i]=img[i][j];
}
}
for (int i = 0; i <SIZE ; ++i) {
for (int j = 0 ; j < SIZE ; j++){
img[i][j]=img1[i][j];
}
}
}
save_choice();
}
//______________________________________________________________________________//
void dark_light(){
char c;
cout<<"To Darken Enter (d)\n"
"To Lighten Enter (l): ";
cin>>c;
while(c != 'd' && c != 'l'){
cout<<"Please Enter a Valid Letter\n"
"Darken (d) , Lighten (l): ";
cin>>c;
}
if(c == 'd'){
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] /= 2;
}
}
}
else{
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] += (255 - img[i][j])/2;
}
}
}
save_choice();
}
//______________________________________________________________________________//
void detect_edges(){
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (i != (SIZE-1) and j != (SIZE-1)) {
if (abs(img[i][j] - img[i][j + 1]) > 30 || abs(img[i][j] - img[i+1][j]) > 30 )
img[i][j] = 0;
else
img[i][j] = 255;
}
}
}
save_choice();
}
//______________________________________________________________________________//
void enlarge(){
char img1[SIZE][SIZE];
cout<<"Enter the quadrant you want to Enlarge (1,2,3,4): ";
int n;cin>>n;
while(n > 4 || n < 1){
cout<<"Please Enter a Valid number (1,2,3,4): ";
cin>>n;
}
switch (n) {
case 1:{
for (int i = 0; i < SIZE/2 ; ++i) {
for (int j = 0 ; j < SIZE/2; ++j ) {
img1[2*i][2*j] = img[i][j];
img1[2*i][2*j+1] = img[i][j];
img1[2*i+1][2*j] = img[i][j];
img1[2*i+1][2*j+1] = img[i][j];
}
}
break;
}
case 2:{
for (int i = 0; i < SIZE/2 ; ++i) {
for (int j = SIZE/2,g = 0 ; j < SIZE; ++j ,++g) {
img1[2*i][2*g] = img[i][j];
img1[2*i][2*g+1] = img[i][j];
img1[2*i+1][2*g] = img[i][j];
img1[2*i+1][2*g+1] = img[i][j];
}
}
break;
}
case 3:{
for (int i = SIZE/2 ,g = 0; i < SIZE ; ++i ,++g) {
for (int j = 0 ; j < SIZE/2; ++j ) {
img1[2*g][2*j] = img[i][j];
img1[2*g][2*j+1] = img[i][j];
img1[2*g+1][2*j] = img[i][j];
img1[2*g+1][2*j+1] = img[i][j];
}
}
break;
}
case 4:{
for (int i = SIZE/2 ,g = 0; i < SIZE ; ++i ,++g) {
for (int j = SIZE/2 , k = 0 ; j < SIZE; ++j , k++ ) {
img1[2*g][2*k] = img[i][j];
img1[2*g][2*k+1] = img[i][j];
img1[2*g+1][2*k] = img[i][j];
img1[2*g+1][2*k+1] = img[i][j];
}
}
break;
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
save_choice();
}
//______________________________________________________________________________//
void shrink(){
unsigned char img1[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
img1[i][j] = 255;
}
}
cout << "Enter The Shrink Size Between 1/2 or 1/3 or 1/4 : \n";
string n;
cin >> n;
while (n != "1/2" and (n != "1/3" and n != "1/4")) {
cout << "Please Enter Valid Size : \n";
cin >> n;
}
if (n == "1/2") {
for (int i = 2; i < SIZE; i++) {
for (int j = 2; j < SIZE; j++) {
unsigned int sum = 0;
for (int k = i - 2; k < i; k++) {
for (int l = j - 2; l < j; l++) {
sum += img[k][l];
}
}
img1[i / 2][j / 2] = sum / 4;
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
} else if (n == "1/3") {
for (int i = 3; i < SIZE; i++) {
for (int j = 3; j < SIZE; j++) {
unsigned int sum = 0;
for (int k = i - 3; k < i; k++) {
for (int l = j - 3; l < j; l++) {
sum += img[k][l];
}
}
img1[i / 3][j / 3] = sum / 9;
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
} else if (n == "1/4") {
for (int i = 4; i < SIZE; i++) {
for (int j = 4; j < SIZE; j++) {
unsigned int sum = 0;
for (int k = i - 4; k < i; k++) {
for (int l = j - 4; l < j; l++) {
sum += img[k][l];
}
}
img1[i / 4][j / 4] = sum / 16;
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
}
}
//______________________________________________________________________________//
void shuffle(){
int a[4];
cout << "Enter the order you want to shuffle: ";
for (int i = 0; i < 4; ++i) {
cin >> a[i];
}
unsigned char img1[SIZE][SIZE];
for (int k = 0; k < 4; ++k) {
int i, j, i1, j1;
if (k == 0)
i1 = 0, j1 = 0;
else if (k == 1)
i1 = 0, j1 = SIZE / 2;
else if (k == 2)
i1 = SIZE / 2, j1 = 0;
else if (k == 3)
i1 = SIZE / 2, j1 = SIZE / 2;
//______________________________________//
if (a[k] == 1)
i = 0, j = 0;
else if (a[k] == 2)
i = 0, j = SIZE / 2;
else if (a[k] == 3)
i = SIZE / 2, j = 0;
else if (a[k] == 4)
i = SIZE / 2, j = SIZE / 2;
//______________________________________//
for (int l = 0; l < SIZE / 2; ++l) {
for (int m = 0; m < SIZE / 2; ++m) {
img1[i1 + l][j1 + m] = img[i + l][j + m];
}
}
}
for (int x = 0; x < SIZE; ++x) {
for (int z = 0; z < SIZE; ++z) {
img[x][z] = img1[x][z];
}
}
save_choice();
}
//______________________________________________________________________________//
void mirror(){
cout<<"Enter (l) for left side mirroring:"
"Enter (r) for right side mirroring:"
"Enter (u) for up side mirroring:"
"Enter (d) for down side mirroring:";
char c; cin>> c;
while (c != 'l' and c != 'r' and c != 'u' and c != 'd'){
cout<< "Enter a valid letter: ";
cin >> c;
}
unsigned char img1[SIZE][SIZE];
// copying image in image1
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[i][j] = img[i][j];
}
}
if (c == 'l'){
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE / 2; ++j) {
img1[i][SIZE-1-j] = img[i][j];
}
}
}else if (c == 'r'){
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE / 2; ++j) {
img1[i][j] = img[i][SIZE-1-j];
}
}
}else if (c == 'u'){
for (int i = 0; i < SIZE / 2; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[SIZE-1-i][j] = img[i][j];
}
}
}else if (c == 'd'){
for (int i = 0; i < SIZE / 2; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[i][j] = img[SIZE-1-i][j];
}
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
save_choice();
}
//______________________________________________________________________________//
void skew_right(){
double seta , y;
cout<<"Enter degree you want to skew with: ";
cin>>seta;
y = SIZE * tan((seta*22)/(7*180));
int x = SIZE - y;
unsigned char img1[SIZE][SIZE];
//shrink the image
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < x; ++j) {
img1[i][j] = img[i][j*255/(x-1)];
}
}
//make the wallpaper white
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = 255;
}
}
//shifting the image
double step = y/256;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < x; ++j) {
img[i][j+(int)y] = img1[i][j];
}
y -= step;
}
save_choice();
}
//______________________________________________________________________________//
void skew_up(){
unsigned char img1[SIZE][SIZE];
unsigned char img2[SIZE][SIZE];
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[i][j] = 255;
img2[i][j] = 255;
}
}
double ang;
cout<<"Enter degree you want to skew with: ";
cin >> ang;
ang = (ang * 22) / (180 * 7);
int x = (int)(SIZE / ( 1 + tan(ang)));
double index = SIZE - x;
double mv = index/ SIZE;
for (int i = 0; i < SIZE ; ++i) {
for (int j = 0; j < SIZE ; ++j) {
img1[(i*x)/SIZE][j] = img[i][j];
}
}
for (int i = 0; i <SIZE ; ++i) {
for (int j = (int)index; j < index+x ; ++j) {
img2[j][i] = img1[(int)(j-index)][i];
}
index -= mv;
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img2[i][j];
}
}
save_choice();
}
//______________________________________________________________________________//
void blur(){
unsigned char img1[SIZE][SIZE];
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
int total = 0 , count = 0;
for (int k = 0; k < 9; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if(ny >= 0 && ny < SIZE && nx >= 0 && nx < SIZE){
total += img[nx][ny];
count++;
}
}
img1[i][j] = total / count;
}
}
for (int i = 0; i <SIZE ; ++i) {
for (int j = 0; j <SIZE ; ++j) {
img[i][j]=img1[i][j];
}
}
save_choice();
}
//______________________________________________________________________________//
void crop(){
unsigned char img1[SIZE][SIZE];
int x,y,l,w;
cout<< "Enter the point you want to crop from: "; cin>> x>> y;
cout<< endl;
cout<< "Enter the Length and Width of the part you want to crop: "; cin>> l >> w;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img1[i][j] = 255;
}
}
for (int i = x; i <= l+x; ++i) {
for (int j = y; j <= w+y; ++j) {
img1[i][j] = img[i][j];
}
}
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
img[i][j] = img1[i][j];
}
}
}
//______________________________________________________________________________//
void save(){
char imageFileName[100];
// Get gray scale image target file name
cout << "Enter image file name to save: ";
cin >> imageFileName;
// Add to it .bmp extension and load_image image
strcat (imageFileName, ".bmp");
writeGSBMP(imageFileName, img);
}
//______________________________________________________________________________//
bool valid(char c){
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || c == 's');
}
//______________________________________________________________________________//
void save_choice(){
cout<<"Do you Want to Save? (y/n): ";
char c; cin>>c;
while(c != 'y' and c != 'n'){
cout<<"Please Enter a Valid letter (y/n): ";
cin>>c;
}
if(c == 'y'){
save();
}
}