-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_processing.cpp
456 lines (427 loc) · 11.6 KB
/
image_processing.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
#include <bits/stdc++.h>
#include "bmplib.cpp"
#include <conio.h>
using namespace std;
void loadImage();
void saveImage();
void black_whiteImage();
void darken_lighten_Image();
void Enlargeimage1();
void Enlargeimage2();
void Enlargeimage3();
void Enlargeimage4();
void invertimage();
void rotate_image90();
void rotate_image180();
void rotate_image270();
void shuffle();
unsigned char image[256][256];
int main()
{
while(true){
loadImage();
cout << "Select the filter you want to apply or 0 to exist. \n1-Black and White filter. \n2-Invert filter. \n3-Darken and Lighten Image. \n4-Rotate image. \n5-Enlarge image. \n6-Shuffle image" << endl;
char mode;
cin >> mode;
switch(mode){
case '1':
black_whiteImage();
saveImage();
break;
case '2':
invertimage();
saveImage();
break;
case '3':
char operation;
cout<<" choice 1 - if you want to darken "<<endl;
cout<<" choice 2 - if you want to lighten "<<endl;
cin>>operation;
darken_lighten_Image();
break;
case '4':
int rotateimage;
cout<<"how do you want to rotate the image??\n1-rotate 90 degree.\n2-rotate 180 degree.\n3-rotate 270 degree."<<endl;
cin>>rotateimage;
if(rotateimage==1)
{
rotate_image90();
saveImage();
}
else if (rotateimage==2)
{
rotate_image180();
saveImage();
}
else if(rotateimage==3)
{
rotate_image270();
saveImage();
}
break;
case '5':
int enlargeimage;
cout<< "choose \n"<< "1-first quarter \n"<< "2-second\n"<< "3-third \n"<< "4-fourth \n";
cin>> enlargeimage;
if(enlargeimage==1)
{
Enlargeimage1();
saveImage();
}
if(enlargeimage==2)
{
Enlargeimage2();
saveImage();
}
if(enlargeimage==3)
{
Enlargeimage3();
saveImage();
}
if(enlargeimage==4)
{
Enlargeimage4();
saveImage();
}
//goto loop;
break;
case '6':
shuffle();
saveImage();
//goto loop;
break;
case '0':
return 0;
}
cout << "\nPress any key to continue... ";
getch();
system("cls");
}
}
//________________________________________________
void loadImage(){
char imageFileName[100];
cout << "Enter the name of the image: ";
cin >> imageFileName;
strcat (imageFileName, ".bmp");
readGSBMP(imageFileName, image);
}
//_________________________________________________
void saveImage(){
char imageFileName[100];
cout << "Enter the new image name: ";
cin >> imageFileName;
strcat (imageFileName, ".bmp");
writeGSBMP(imageFileName, image);
}
//__________________________________________________
void invertimage() {
for (int i = 0; i < 256; i++) {
for (int j = 0; j< 256; j++) {
// Example code to invert the image
image[i][j]=255-image[i][j];
// do something with the image
}
}
}
//__________________________________________________
void blackAndWhite(){
int sum=0;
for(int i=0 ; i<256 ; i++){
for(int j=0 ; j<256 ; j++){
sum += (int)image[i][j];
}
}
int average = sum / (256*256);
for(int x=0 ; x<256 ; x++){
for(int y=0 ; y<256 ; y++){
if((int)image[x][y] > average){
image[x][y] = (char)(255);
}else if((int)image[x][y] < average){
image[x][y] = (char)(0);
}
}
}
}
//__________________________________________________
void black_whiteImage() {
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j< SIZE; j++)
{
// do something with the image
if (image[i][j]>=127)
image[i][j]=255;
else
image[i][j]=0;
}
}
}
//__________________________________________________
void darken_lighten_Image() {
int operation;
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j< SIZE; j++)
{
if (operation==1)
{
image[i][j]=(image[i][j])/2;
}
if (operation==2)
{
if (image[i][j]<=127 )
{
image[i][j]=(image[i][j])+70;
}
else if (image[i][j]<=255)
{
image[i][j]=(image[i][j]);
}
}
}
}
}
//___________________________________________________
void rotate_image90 ()
{
int x ;
for (int i = 0; i < 256; i++){
for (int j =i+1; j< 256; j++) {
swap(image[i][j],image[j][i]);
}
}
for (int i = 0; i < 256; i++ ){
for (int j = 0; j < 256/2; j += 1){
swap(image[j][i], image[256-j-1][i]);
}
}
}
//________________________________________________
void rotate_image180 ()
{
int x;
for(int i=0 ;i<256/2 ;i++)
{
for(int j=0 ;j<256 ;j++)
{
x=image[i][j];
image[i][j]=image[256-1-i][256-1-j];
image[256-1-i][256-1-j]=x;
}
}
}
//________________________________________________
void rotate_image270()
{
int x;
for (int i = 0; i < (256); i++){
for (int j =i+1; j< (256); j++) {
swap(image[i][j],image[j][i]);
}
}
for (int i = 0; i < 256; i++ ){
for (int j = 0; j < 256/2; j += 1){
swap(image[j][i], image[256-j-1][i]);
}
}
for(int i=0 ;i<256/2 ;i++)
{
for(int j=0 ;j<256 ;j++)
{
x=image[i][j];
image[i][j]=image[256-1-i][256-1-j];
image[256-1-i][256-1-j]=x;
}
}
}
//_______________________________________________
void Enlargeimage1()
{
unsigned char temp[256][256];
for(int i=0 ,k=0 ; i<256/2 ; i++ , k+=2)
{
for(int j=0,n=0;j<256/2 ; j++ ,n+=2)
{
temp[k][n]=image[i][j];
temp[k+1][n]=image[i][j];
temp[k][n+1]=image[i][j];
temp[k+1][n+1]=image[i][j];
}
}
}
//_______________________________________________
void Enlargeimage2()
{
unsigned char temp[256][256];
for(int i=0 ,k=0 ; i<256/2 ; i++ , k+=2)
{
for(int j=256/2,n=0;j<256 ; j++ ,n+=2)
{
temp[k][n]=image[i][j];
temp[k+1][n]=image[i][j];
temp[k][n+1]=image[i][j];
temp[k+1][n+1]=image[i][j];
}
}
}
//_______________________________________________
void Enlargeimage3()
{
unsigned char temp[256][256];
for(int i=256/2 ,k=0 ; i<256 ; i++ , k+=2)
{
for(int j=0,n=0;j<256/2 ; j++ ,n+=2)
{
temp[k][n]=image[i][j];
temp[k+1][n]=image[i][j];
temp[k][n+1]=image[i][j];
temp[k+1][n+1]=image[i][j];
}
}
}
//_________________________________________________
void Enlargeimage4()
{
unsigned char temp[256][256];
for(int i=256/2 ,k=0 ; i<256 ; i++ , k+=2)
{
for(int j=256/2,n=0;j<256 ; j++ ,n+=2)
{
temp[k][n]=image[i][j];
temp[k+1][n]=image[i][j];
temp[k][n+1]=image[i][j];
temp[k+1][n+1]=image[i][j];
}
}
for(int i=0 ; i<256 ; i++){
for(int j=0 ; j<256 ; j++){
image[i][j] = temp[i][j];
}
}
}
//__________________________________________
void shuffle(){
loop:
int x1, x2, x3, x4;
cout << "Enter the order of the shuffle that you want" << endl;
cin >> x1 >> x2 >> x3 >> x4;
unsigned char temp[256][256];
if(x1 == 1){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j] = image[i][j];
}
}
}else if(x1 == 2){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j] = image[i][j+128];
}
}
}else if(x1 == 3){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j] = image[i+128][j];
}
}
}else if(x1 == 4){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j] = image[i+128][j+128];
}
}
}else{
cout << "Out of range, please try again" << endl;
goto loop;
}
if(x2 == 1){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j+128] = image[i][j];
}
}
}else if(x2 == 2){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j+128] = image[i][j+128];
}
}
}else if(x2 == 3){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][j+128] = image[i+128][j];
}
}
}else if(x2 == 4){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i][128] = image[i+128][j+128];
}
}
}else{
cout << "Out of range, please try again" << endl;
goto loop;
}
if(x3 == 1){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j] = image[i][j];
}
}
}else if(x3 == 2){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j] = image[i][j+128];
}
}
}else if(x3 == 3){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j] = image[i+128][j];
}
}
}else if(x3 == 4){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[+128][j] = image[i+128][j+128];
}
}
}else{
cout << "Out of range, please try again" << endl;
goto loop;
}
if(x4 == 1){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j+128] = image[i][j];
}
}
}else if(x4 == 2){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j+128] = image[i][j+128];
}
}
}else if(x4 == 3){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j+128] = image[i+128][j];
}
}
}else if(x4 == 4){
for(int i=0 ; i<128 ; i++){
for(int j=0 ; j<128 ; j++){
temp[i+128][j+128] = image[i+128][j+128];
}
}
}else{
cout << "Out of range, please try again" << endl;
goto loop;
}
for(int i=0 ; i<256 ; i++){
for(int j=0 ; j<256 ; j++){
image[i][j] = temp[i][j];
}
}
}