-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGB.cpp
More file actions
586 lines (575 loc) · 18.1 KB
/
Copy pathRGB.cpp
File metadata and controls
586 lines (575 loc) · 18.1 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
// Created by fatma on 09/10/2023.
//FCAI - OOP Programming - 2023 - Assignment 1
//Program Name: main.cpp
//Last Modification Date: 10/19/2023
//Purpose: Using a bitmap library to convert an image into an array in order to apply some filters on it
//version 2.0 RGB
//Fatma Mahmoud Atta 20220510
//Shahd Mohammed Ahmed 20220533
//Reham Fawzy 20220141
//fatmamaali@gmail.com
//shahdelnassag@gmail.com
//fwrymw@gmail.com
#include <bits/stdc++.h>
#include <fstream>
#include "bmplib.h"
#include "RGB.h"
using namespace std;
unsigned char imageRGBBMP[SIZE][SIZE][3]; //the image to be processed
unsigned char mergeRGBBMP[SIZE][SIZE][3]; // the image that will be merged
unsigned char imageRGBT[SIZE][SIZE][3]; //transposed image used for the rotate filter
unsigned char flipRGBBMP[SIZE][SIZE][3]; // the image that will be flipped
unsigned char shrinkRGBBMP[SIZE][SIZE][3]; // the image that will be shrunken
unsigned char imageRGBblur[SIZE][SIZE][3]; //stores the blurred image
unsigned char croppedRGBImage[SIZE][SIZE][3]; //stores the cropped image
unsigned char imageRGBSkew[SIZE][SIZE][3]; //array to store the skewed image
unsigned char q1RGB[SIZE / 2][SIZE / 2][3]; //2d arrays to store the image divided into quarters
unsigned char q2RGB[SIZE / 2][SIZE / 2][3]; //
unsigned char q3RGB[SIZE / 2][SIZE / 2][3]; //
unsigned char q4RGB[SIZE / 2][SIZE / 2][3]; //
char basePath2[]="./Images/";
//function to display the choices
void RGBdisplayChoices(){
cout<<" 1- Black & White Filter\n "
"2- Invert Colors\n "
"3- Merge Two Images\n "
"4- Flip Image \n "
"5- Darken or Lighten \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 New Image \n "
"0- Exit \n";
}
//function to choose
void RGBinitChoice(char choice){
switch (choice){
case '1':
RGBBlackWhite();
break;
case '2':
RGBInvertImage();
break;
case '3':
RGBMergeImage();
break;
case '4':
char hv;
cout<<"Flip (h)orizontally or (v)ertically? \n";
cin>>hv;
if(hv=='h'){
RGBFlipImageHorizontally();
}else{
RGBFlipImageVertically();
};
break;
case '5':
char dl;
cout<<"(d)arken or (l)ighten? \n";
cin>>dl;
if(dl=='d'){
RGBDarken();
}else{
RGBLighten();
};
break;
case '6':
int degree;
cout<<"Rotate (90) or (180) or (270)? \n"; // if 180 use flip, also the 270 is flip of 90
cin>>degree;
RGBRotateImage(degree);
break;
case '7':
RGBDetectImageEdges();
break;
case '8':
int quarter;
cout<<"Enlarge quarter 1, 2, 3, or 4?\n";
cin>>quarter;
RGBEnlargeImage(quarter);
break;
case '9':
int sh;
cout<<"Shrink to half (2) or third (3) or quarter (4)?\n";
cin>>sh;
RGBShrink(sh);
break;
case 'a':
RGBmirrorImage();
break;
case 'b':
RGBShuffleImage();
break;
case 'c':
RGBblurImage();
break;
case 'd':
RGBcropImage();
break;
case 'e':
RGBskewHorizontally();
break;
case 'f':
RGBskewVertically();
break;
case 's':
RGBsaveImage();
break;
}
}
//function to load the image
void RGBloadImage(){
char fileName[100];
cout<<"Hello Please enter the image file name to process: \n";
cin>>fileName;
strcat(basePath2,fileName); //concatenating the base path to the file name to the extension
strcat(basePath2,".bmp");
readRGBBMP(basePath2,imageRGBBMP); //loads the image into imageRGBBMP 2d array
}
//funciton to save the image
void RGBsaveImage(){
char basePath2[]="./Images/";
char newFileName[100];
cout<<"Enter the new file name\n";
cin>>newFileName;
strcat(basePath2,newFileName);
strcat(basePath2,".bmp");
writeRGBBMP(basePath2,imageRGBBMP); //saves the image in imageRGBBMP 2d array
}
//function that copies the elements of a 3d array to the imageRGBBMP array
void RGBToImage(unsigned char arr[SIZE][SIZE][3]){
for(int i=0;i<3;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
imageRGBBMP[j][k][i]=arr[j][k][i];
}
}
}
}
//function that sets the background of an image to white
void RGBWhiteBackground(unsigned char arr[SIZE][SIZE][3]){
for(int i=0;i<3;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
arr[j][k][i]=255;
}
}
}
}
//function used in the rotate function
void RGBTranspose(){
for(int i=0;i<3;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
imageRGBT[j][k][i]=imageRGBBMP[k][j][i];
}
}
}
RGBToImage(imageRGBT);
}
//function that divides the image array into 4 quarters to be used in shuffle and enlarge and shrink filters
void RGBDivide4(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
if(i<(SIZE/2)&&j<(SIZE/2)){
q1RGB[i][j][k]=imageRGBBMP[i][j][k];
}
else if(i<(SIZE/2)&&j>=(SIZE/2)){
q2RGB[i][j - (SIZE / 2)][k]= imageRGBBMP[i][j][k];
}
else if(i>=(SIZE/2)&&j<(SIZE/2)){
q3RGB[i - (SIZE / 2)][j][k]= imageRGBBMP[i][j][k];
}
else if(i>=(SIZE/2)&&j>=(SIZE/2)){
q4RGB[i - (SIZE / 2)][j - (SIZE / 2)][k]= imageRGBBMP[i][j][k];
}
}
}
}
}
//function that turns the image into black and white
void RGBBlackWhite(){
int sum=0,average;
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
sum=0;
for(int k=0;k<3;k++){
sum+=imageRGBBMP[i][j][k];
}
average=sum/3;
if(average<128){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=0;
}
}
else
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=255;
}
}
}
}
//function to invert the image colors
void RGBInvertImage(){
for(int i=0;i<3;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
imageRGBBMP[j][k][i]= 255 - imageRGBBMP[j][k][i];
}
}
}
}
//function to merge 2 images together
void RGBMergeImage(){
char basePath2[]="./Images/";
char mergeName[100];
cout<<"Please enter the name of image file to merge with:\n";
cin>>mergeName;
strcat(basePath2,mergeName);
strcat(basePath2,".bmp");
readRGBBMP(basePath2,mergeRGBBMP);
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=(imageRGBBMP[i][j][k]+mergeRGBBMP[i][j][k])/2;
}
}
}
}
//function that flips the image vertically
void RGBFlipImageVertically(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
flipRGBBMP[i][j][k]=imageRGBBMP[SIZE-i][j][k];
}
}
}
RGBToImage(flipRGBBMP);
}
//function that flips the image horizontally
void RGBFlipImageHorizontally(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
flipRGBBMP[i][j][k]=imageRGBBMP[i][SIZE-j][k];
}
}
}
RGBToImage(flipRGBBMP);
}
//function that darkens the image by 50%
void RGBDarken(){
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]-=imageRGBBMP[i][j][k]/2;
}
}
}
}
//function that lightens the image by 50%
void RGBLighten(){
for(int i=0;i<3;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<SIZE;k++){
imageRGBBMP[i][j][k]+=imageRGBBMP[i][j][k]/2;
}
}
}
}
//function that rotates images by 270 degrees
void RGBRotate270(){
RGBTranspose();
RGBFlipImageVertically();
}
//function that rotates an image by an inputted degree
void RGBRotateImage(int degree){
if(degree==90){
RGBRotate270();
RGBFlipImageVertically();
RGBFlipImageHorizontally();
}else if(degree==180){
RGBFlipImageVertically();
RGBFlipImageHorizontally();
}else if(degree==270){
RGBRotate270();
}
}
//function that detects the image edges
void RGBDetectImageEdges(){
RGBBlackWhite();
for(int i=0;i<SIZE-1;i++){
for(int j=0;j<SIZE-1;j++){
for(int k=0;k<3;k++){
if(imageRGBBMP[i+1][j+1][k]!=imageRGBBMP[i][j][k]||imageRGBBMP[i+1][j][k]!=imageRGBBMP[i][j][k]||imageRGBBMP[i][j+1][k]!=imageRGBBMP[i][j][k]){
imageRGBBMP[i][j][k]=0;
}
else{
imageRGBBMP[i][j][k]=255;
}
}
}
}
}
//function that enlarges a quarter of the image
void RGBEnlargeImage(int quarter){
RGBDivide4();
switch(quarter){
case 1:
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=q1RGB[i / 2][j / 2][k];
}
}
}
break;
case 2:
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=q2RGB[i / 2][j / 2][k];
}
}
}
break;
case 3:
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=q3RGB[i / 2][j / 2][k];
}
}
}
break;
case 4:
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k]=q4RGB[i / 2][j / 2][k];
}
}
}
break;
}
}
//function that shrinks the image to a specific size
void RGBShrink(int sh){
RGBWhiteBackground(shrinkRGBBMP);
for(int i=0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
shrinkRGBBMP[i / sh][j / sh][k]= (imageRGBBMP[i][j][k] + imageRGBBMP[i][j + 1][k] + imageRGBBMP[i + 1][j][k] + imageRGBBMP[i + 1][j + 1][k]) / 4;
}
}
}
RGBToImage(shrinkRGBBMP);
}
//function that mirrors a half of the image
void RGBmirrorImage(){
char a;
cout<<"Mirror (l)eft, (r)ight, (u)pper or (d)own\n";
cin>>a;
if(a=='r'||a=='R') {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE/2; ++j) {
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k] = imageRGBBMP[i][ 255-j][k];
}
}
}
}
if(a=='l'||a=='L') {
for (int i = 0; i < SIZE; ++i) {
for (int j = 255; j > SIZE/2; --j) {
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k] = imageRGBBMP[i][255-j][k];
}
}
}
}
if(a=='d'||a=='D') {
for (int i = 0; i < SIZE/2; ++i) {
for (int j = 0; j < SIZE; ++j) {
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k] = imageRGBBMP[255-i][ j][k];
}
}
}
}
if(a=='u'||a=='U') {
for (int i = 255; i > SIZE/2; --i) {
for (int j = 0; j < SIZE; ++j) {
for(int k=0;k<3;k++){
imageRGBBMP[i][j][k] = imageRGBBMP[255-i][j][k];
}
}
}
}
}
//function that shuffles the quarters of an image
void RGBShuffleImage(){
int order[4];
cout<<"Enter the new order of quarters\n";
for(int i =0;i<4;i++){
cin>>order[i];
}
RGBDivide4();
for(int i =0;i<SIZE;i++){
for(int j=0;j<SIZE;j++){
for(int k=0;k<3;k++){
if(i<(SIZE/2)&&j<(SIZE/2)){ //1st quarter
switch(order[0]){
case 1:
imageRGBBMP[i][j][k]=q1RGB[i][j][k];
break;
case 2:
imageRGBBMP[i][j][k]=q2RGB[i][j][k];
break;
case 3:
imageRGBBMP[i][j][k]=q3RGB[i][j][k];
break;
case 4:
imageRGBBMP[i][j][k]=q4RGB[i][j][k];
break;
}
}
else if(i<(SIZE/2)&&j>=(SIZE/2)){ //2nd quarter
switch(order[1]){
case 1:
imageRGBBMP[i][j][k]=q1RGB[i][j - 128][k];
break;
case 2:
imageRGBBMP[i][j][k]=q2RGB[i][j - 128][k];
break;
case 3:
imageRGBBMP[i][j][k]=q3RGB[i][j - 128][k];
break;
case 4:
imageRGBBMP[i][j][k]=q4RGB[i][j - 128][k];
break;
}
}
else if(i>=(SIZE/2)&&j<(SIZE/2)){ //3rd quarter
switch(order[2]){
case 1:
imageRGBBMP[i][j][k]=q1RGB[i - 128][j][k];
break;
case 2:
imageRGBBMP[i][j][k]=q2RGB[i - 128][j][k];
break;
case 3:
imageRGBBMP[i][j][k]=q3RGB[i - 128][j][k];
break;
case 4:
imageRGBBMP[i][j][k]=q4RGB[i - 128][j][k];
break;
}
}
else if(i>=(SIZE/2)&&j>=(SIZE/2)){ //4th quarter
switch(order[3]){
case 1:
imageRGBBMP[i][j][k]=q1RGB[i - 128][j - 128][k];
break;
case 2:
imageRGBBMP[i][j][k]=q2RGB[i - 128][j - 128][k];
break;
case 3:
imageRGBBMP[i][j][k]=q3RGB[i - 128][j - 128][k];
break;
case 4:
imageRGBBMP[i][j][k]=q4RGB[i - 128][j - 128][k];
break;
}
}
}
}
}
}
//function that blurs the image
void RGBblurImage(){
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
for(int k=0;k<3;k++){
imageRGBblur[i][j][k] = ( imageRGBBMP[i-1][j-1][k] + imageRGBBMP[i-1][j][k]+ imageRGBBMP[i-1][j+1][k] +
imageRGBBMP[i][j-1][k] + imageRGBBMP[i][j][k] + imageRGBBMP[i][j+1][k] +
imageRGBBMP[i+1][j-1][k] + imageRGBBMP[i+1][j][k] +imageRGBBMP[i+1][j+1][k] ) /9;
}
}
}
RGBToImage(imageRGBblur);
}
//function that crops the image
void RGBcropImage(){
RGBWhiteBackground(croppedRGBImage);
int x,y,l,w;
cout<<"Please enter x, y, l, w:\n";
cin >> x >> y>>l>>w;
for(int i=x; i < SIZE-x; ++i){
for(int j=y; j <SIZE-y; ++j){
for(int k=0;k<3;k++){
croppedRGBImage[i][j][k]=imageRGBBMP[i][j][k];
}
}
}
RGBToImage(croppedRGBImage);
}
//function that skews the image horizontally
void RGBskewHorizontally(){
// convert angle to radian
double rad;
cout << "Enter the angle: ";
cin >> rad;
rad = (rad * 22) / (180 * 7);
// factor that stretches the image in the horizontal direction
double skewfactor = tan(rad);
double scalefactor = 2.0;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
for(int k=0;k<3;k++){
//calculate the new y-coordinate using the following formula
int new_j = j * scalefactor - skewfactor * i; // 1*2.0 - tan(30)*1
if (new_j >= 0 && new_j < SIZE) {
imageRGBSkew[i][j][k] = imageRGBBMP[i][new_j][k];
} else {
imageRGBSkew[i][j][k] = 255;
}
}
}
}
RGBToImage(imageRGBSkew);
}
//function that skews the image horizontally
void RGBskewVertically(){
// convert angle to radian
double rad;
cout << "Enter the angle: ";
cin >> rad;
rad = (rad * 22) / (180 * 7);
double skew_factor = tan(rad);
// factor that stretches the image in the verticall direction
double scale_factor = 2.0;
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
for(int k=0;k<3;k++){
// calculate the new x-coordinate using the following formula//
int new_i = i * scale_factor - skew_factor * j;
if (new_i >= 0 && new_i < SIZE) {
imageRGBSkew[i][j][k] = imageRGBBMP[new_i][j][k];
} else {
imageRGBSkew[i][j][k] = 255;
}
}
}
}
RGBToImage(imageRGBSkew);
}