-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageEditor.c
More file actions
804 lines (688 loc) · 25.8 KB
/
ImageEditor.c
File metadata and controls
804 lines (688 loc) · 25.8 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
typedef struct {
int r, g, b;
}Pixel;
typedef struct {
int w, h;
int format; // 2 pentru PGM (P2, P5), 3 pentru PPM (P3, P6)
union {
int **pgm_pixels; // pentru imagini PGM
Pixel **ppm_pixels; // pentru imagini PPM
}; //matricele sunt stocate la aceeasi adresa
}Image;
void free_image(Image *img) {
if (img->format == 2 && img->pgm_pixels != NULL) { //pgm
for (int i = 0; i < img->h; i++) {
free(img->pgm_pixels[i]);
}
free(img->pgm_pixels);
img->pgm_pixels = NULL;
} else if (img->format == 3 && img->ppm_pixels != NULL) { //ppm
for (int i = 0; i < img->h; i++) {
free(img->ppm_pixels[i]);
}
free(img->ppm_pixels);
img->ppm_pixels = NULL;
}
}
int load(const char *file_name, Image *img) {
if (img) {
free_image(img);
}
FILE *file = fopen(file_name, "rb"); // deschide fișierul pt citire
if (file == NULL) {
return 1;
}
//citirea datelor dinaintea matricei de pixeli
char header[3];
fscanf(file, " %s", header);
header[2] = '\0';
int max_val;
fscanf(file, " %d%d%d", &img->w, &img->h, &max_val);
//consuma caracterul de newlinw
while (fgetc(file) != '\n');
// Alocare memorie pentru matricea de pixeli
if (header[1] == '2' || header[1] == '5') {
// PGM - alocare pentru o singura valoare per pixel
// setez formatul la 2 pentru PGM si la 3 pentru PPM
img->format = 2;
img->pgm_pixels = malloc(img->h * sizeof(int *));
for (int i = 0; i < img->h; i++) {
img->pgm_pixels[i] = malloc(img->w * sizeof(int));
}
} else if (header[1] == '3' || header[1] == '6') {
// PPM - alocare pentru 3 valori per pixel
img->format = 3;
img->ppm_pixels = malloc(img->h * sizeof(Pixel *));
for (int i = 0; i < img->h; i++) {
img->ppm_pixels[i] = malloc(img->w * sizeof(Pixel));
}
} else {
printf("Invalid format.\n");
fclose(file);
return 1;
}
// citirea matricei de pixeli
if(header[1] == '2') {
//citire text
for (int i = 0; i < img->h; i++)
for (int j = 0; j < img->w; j++)
fscanf(file, "%d", &img->pgm_pixels[i][j]);
} else if (header[1] == '5') {
// P5 - citire binar (PGM)
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
img->pgm_pixels[i][j] = fgetc(file); // citire ca byte
}
}
} else if (header[1] == '3') {
// P3 - citire text (PPM)
unsigned char x, y, z;
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
fscanf(file, "%hhu %hhu %hhu", &x, &y, &z); // Citire ca unsigned char
img->ppm_pixels[i][j].r = (int)x;
img->ppm_pixels[i][j].g = (int)y;
img->ppm_pixels[i][j].b = (int)z;
}
}
} else if (header[1] == '6') {
// P6 - citire binar (PPM)
unsigned char x, y, z;
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
fread(&x, sizeof(unsigned char), 1, file);
fread(&y, sizeof(unsigned char), 1, file);
fread(&z, sizeof(unsigned char), 1, file);
img->ppm_pixels[i][j].r = (int)x;
img->ppm_pixels[i][j].g = (int)y;
img->ppm_pixels[i][j].b = (int)z;
}
}
}
fclose(file);
return 0; //succes
}
int save(const char *file_name, Image *img, int ascii) {
// eroare daca nu este nicio imagine incarcata
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return 1;
}
// ascii = 1 => deschid fisierul pt scriere in mod text
// ascii = 0 => deschid fisierul pt scriere in mod binar
FILE *file = fopen(file_name, ascii ? "w" : "wb");
if (!file) {
printf("Failed to open file for saving: %s\n", file_name);
return 1;
}
// scriere format
if (img->format == 2) { //PGM
fprintf(file, ascii ? "P2\n" : "P5\n");
} else if (img->format == 3) { //PPM
fprintf(file, ascii ? "P3\n" : "P6\n");
}
// scrie latimea, inaltimea si max_val
fprintf(file, "%d %d\n255\n", img->w, img->h);
// scriere pixeli
if (img->format == 2) { // PGM
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
if (ascii == 1) {
// PGM scriere text
fprintf(file, "%d ", img->pgm_pixels[i][j]);
} else {
// PGM scriere binar
fputc(img->pgm_pixels[i][j], file);
}
}
if (ascii) fprintf(file, "\n");
}
} else if (img->format == 3) { // PPM
unsigned char x, y, z;
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
//stocare ca octeti
x = (unsigned char)img->ppm_pixels[i][j].r;
y = (unsigned char)img->ppm_pixels[i][j].g;
z = (unsigned char)img->ppm_pixels[i][j].b;
if (ascii) {
// PPM scriere text
fprintf(file, "%hhu %hhu %hhu ", x, y, z);
} else {
//PPM scriere binar
fwrite(&x, sizeof(unsigned char), 1, file);
fwrite(&y, sizeof(unsigned char), 1, file);
fwrite(&z, sizeof(unsigned char), 1, file);
}
}
if (ascii) fprintf(file, "\n");
}
}
fclose(file);
return 0; //succes
}
void select_image(const char *command, Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if (strncmp(command, "SELECT ALL", 10) == 0) {
*selected_x1 = 0;
*selected_y1 = 0;
*selected_x2 = img->w;
*selected_y2 = img->h;
printf("Selected ALL\n");
return;
}
//verificare comanda are formatul corect
int x1, y1, x2, y2;
if (sscanf(command, "SELECT %d%d%d%d", &x1, &y1, &x2, &y2) != 4) {
printf("Invalid command\n");
return;
}
// verificare coordonatele nu sunt inafara imaginii
if (x1 < 0 || x2 < 0 || y1 < 0 || y2 < 0 ||
x1 > img->w || x2 > img->w || y1 > img->h || y2 > img->h ||
(x1 == x2 && y1 == y2)) {
printf("Invalid set of coordinates\n");
return;
}
// ma asigur ca x1 < x2 si y1 < y2
int temp;
if (x1 > x2) {
temp = x1; x1 = x2; x2 = temp;
}
if (y1 > y2) {
temp = y1; y1 = y2; y2 = temp;
}
if (x2 <= 0 || y2 <= 0) {
printf("Invalid set of coordinates\n");
return;
}
if (x1 >= img->w || y1 >= img->h) {
printf("Invalid set of coordinates\n");
return;
}
*selected_x1 = x1;
*selected_y1 = y1;
*selected_x2 = x2;
*selected_y2 = y2;
printf("Selected %d %d %d %d\n", x1, y1, x2, y2);
}
int find_max(int *arr, int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void histogram(Image *img, int x, int y, int x1, int y1, int x2, int y2) {
// verificare cazuri de eroare
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if(img->format != 2) {
printf("Black and white image needed\n");
return;
}
// calcul bin-uri
// stocheaza in *frecv nr de aparitii al valorilor din fiecare interval
int *frecv = calloc(y, sizeof(int));
int bin_size = 256 / y;
for (int i = y1; i < y2; i++) {
for (int j = x1; j < x2; j++) {
int val = img->pgm_pixels[i][j];
int bin_index = val / bin_size;
frecv[bin_index]++;
}
}
// calcul stelute
int max_frecv = find_max(frecv, y);
for (int i = 0; i < y; i++) {
int nr_stars;
float rez = ((float)frecv[i] / (float)max_frecv) * x;
// prin conversia la int, rez este trunchiat in jos
nr_stars = (int)rez;
printf("%d\t|\t", nr_stars);
for (int j = 0; j < nr_stars; j++) {
printf("*");
}
printf("\n");
}
free(frecv);
}
void crop(Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
// calcul noile dimensiuni ale imaginii
int crop_w = *selected_x2 - *selected_x1;
int crop_h = *selected_y2 - *selected_y1;
if (img->format == 2) { // PGM
// initializare matrice noua si alocare memorie
// se copiaza rand cu rand din selectia imaginii originale in noua imagine
// numarul de elemente copiate corespunde latimii noii imagini
int **new_pgm_pixels = malloc(crop_h * sizeof(int *));
for (int i = 0; i < crop_h; i++) {
new_pgm_pixels[i] = malloc(crop_w * sizeof(int));
memcpy(new_pgm_pixels[i], &img->pgm_pixels[*selected_y1 + i][*selected_x1], crop_w * sizeof(int));
}
// eliberarea memoriei vechii mattice de pixeli
// actualizeaza pointerul imaginii sa indice catre noua matrice
free_image(img);
img->pgm_pixels = new_pgm_pixels;
} else if (img->format == 3) { // PPM
Pixel **new_ppm_pixels = malloc(crop_h * sizeof(Pixel *));
for (int i = 0; i < crop_h; i++) {
new_ppm_pixels[i] = malloc(crop_w * sizeof(Pixel));
memcpy(new_ppm_pixels[i], &img->ppm_pixels[*selected_y1 + i][*selected_x1], crop_w * sizeof(Pixel));
}
free_image(img);
img->ppm_pixels = new_ppm_pixels;
}
// actualizeaza dimensiuni si selectie dupa crop
img->w = crop_w;
img->h = crop_h;
*selected_x1 = 0;
*selected_y1 = 0;
*selected_x2 = crop_w;
*selected_y2 = crop_h;
printf("Image cropped\n");
}
void rotate_entire_image(Image *img) {
int new_w = img->h;
int new_h = img->w;
if (img->format == 2) { // PGM
// Aloca memorie pentru noua matrice de pixeli
int **new_pixels = malloc(new_h * sizeof(int *));
for (int i = 0; i < new_h; i++)
new_pixels[i] = malloc(new_w * sizeof(int));
// Populeaza noua matrice cu valorile rotite (la dreapta)
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
new_pixels[j][img->h - i - 1] = img->pgm_pixels[i][j];
}
}
// Elibereaza memoria matricei vechi
for (int i = 0; i < img->h; i++)
free(img->pgm_pixels[i]);
free(img->pgm_pixels);
// Actualizează imaginea
img->pgm_pixels = new_pixels;
} else if (img->format == 3) { // PPM
Pixel **new_pixels = malloc(new_h * sizeof(Pixel *));
for (int i = 0; i < new_h; i++)
new_pixels[i] = malloc(new_w * sizeof(Pixel));
// Populează noua matrice cu valorile rotite (la dreapta)
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
new_pixels[j][img->h - i - 1] = img->ppm_pixels[i][j];
}
}
// Elibereaza memoria matricei vechi
for (int i = 0; i < img->h; i++)
free(img->ppm_pixels[i]);
free(img->ppm_pixels);
// Actualizeaza imaginea
img->ppm_pixels = new_pixels;
}
// Actualizeaza dimensiunile imaginii
img->w = new_w;
img->h = new_h;
}
void rotate_right_once(Image *img, int x1, int y1, int sel_w, int sel_h) {
int i, j;
// aloca memorie pentru matricea temporara
if (img->format == 2) { // PGM
int **temp = malloc(sel_w * sizeof(int *));
for (i = 0; i < sel_w; i++)
temp[i] = malloc(sel_h * sizeof(int));
// populeaza temp cu valori
for (i = 0; i < sel_h; i++) {
for (j = 0; j < sel_w; j++) {
temp[j][sel_h - i - 1] = img->pgm_pixels[y1 + i][x1 + j];
}
}
// copiaza valorile în matricea originala
for (i = 0; i < sel_w; i++) {
for (j = 0; j < sel_h; j++) {
img->pgm_pixels[y1 + i][x1 + j] = temp[i][j];
}
}
// elibereaza memoria matricei temporare
for (i = 0; i < sel_w; i++)
free(temp[i]);
free(temp);
}
else if (img->format == 3) { // PPM
Pixel **temp = malloc(sel_w * sizeof(Pixel *));
for (i = 0; i < sel_w; i++)
temp[i] = malloc(sel_h * sizeof(Pixel));
// populeaza temp cu valori
for (i = 0; i < sel_h; i++) {
for (j = 0; j < sel_w; j++) {
temp[j][sel_h - i - 1] = img->ppm_pixels[y1 + i][x1 + j];
}
}
// copiaza valorile în matricea originală
for (i = 0; i < sel_w; i++) {
for (j = 0; j < sel_h; j++) {
img->ppm_pixels[y1 + i][x1 + j] = temp[i][j];
}
}
// elibereaza memoria matricei temporare
for (i = 0; i < sel_w; i++)
free(temp[i]);
free(temp);
}
}
void rotate_selection(int angle, Image *img, int x1, int y1, int x2, int y2) {
//verificare cazuri de eroare
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
int sel_w = x2 - x1;
int sel_h = y2 - y1;
int entire_image_selected = ((x1 == 0 && y1 == 0 && x2 == img->w && y2 == img->h) ||
(x1 == 0 && y1 == 0 && x2 == img->h && y2 == img->w) );
if (!entire_image_selected && sel_w != sel_h) {
printf("The selection must be square\n");
return;
}
if (angle % 90 != 0 || angle > 360 || angle < -360) {
printf("Unsupported rotation angle\n");
return;
}
// rotatia selectiei
// reduce cazurile la [0, 3] rotații
int num_rotations = (angle / 90 + 4) % 4;
if (entire_image_selected) {
for (int r = 0; r < num_rotations; r++) {
rotate_entire_image(img);
}
} else {
for (int r = 0; r < num_rotations; r++) {
rotate_right_once(img, x1, y1, sel_w, sel_h);
}
}
printf("Rotated %d\n", angle);
}
void sharpen(Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if (img->format == 2) {
printf("Easy, Charlie Chaplin\n");
return;
}
int kernel[3][3] = { {0, -1, 0}, {-1, 5, -1}, {0, -1, 0} };
Pixel **original_data = img->ppm_pixels;
Pixel **new_data = (Pixel**)malloc(img->h * sizeof(Pixel*));
for (int i = 0; i < img->h; i++) {
new_data[i] = (Pixel*)malloc(img->w * sizeof(Pixel));
memmove(new_data[i], original_data[i], img->w * sizeof(Pixel)); // copy original pixel data
}
for (int i = max(1, *selected_y1); i < min(img->h - 1, *selected_y2); i++) {
for (int j = max(1, *selected_x1); j < min(img->w - 1, *selected_x2); j++) {
int r = 0, g = 0, b = 0;
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
r += original_data[i + k - 1][j + l - 1].r * kernel[k][l];
g += original_data[i + k - 1][j + l - 1].g * kernel[k][l];
b += original_data[i + k - 1][j + l - 1].b * kernel[k][l];
}
}
new_data[i][j].r = min(255, max(0, r));
new_data[i][j].g = min(255, max(0, g));
new_data[i][j].b = min(255, max(0, b));
}
}
free_image(img);
img->ppm_pixels = new_data;
printf("APPLY SHARPEN done\n");
}
void edge(Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if (img->format == 2) {
printf("Easy, Charlie Chaplin\n");
return;
}
int kernel[3][3] = { {-1, -1, -1}, {-1, 8, -1}, {-1, -1, -1} };
Pixel **original_data = img->ppm_pixels;
Pixel **new_data = (Pixel**)malloc(img->h * sizeof(Pixel*));
for (int i = 0; i < img->h; i++) {
new_data[i] = (Pixel*)malloc(img->w * sizeof(Pixel));
memmove(new_data[i], original_data[i], img->w * sizeof(Pixel)); // copy original pixel data
}
for (int i = max(1, *selected_y1); i < min(img->h - 1, *selected_y2); i++) {
for (int j = max(1, *selected_x1); j < min(img->w - 1, *selected_x2); j++) {
int r = 0, g = 0, b = 0;
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
r += original_data[i + k - 1][j + l - 1].r * kernel[k][l];
g += original_data[i + k - 1][j + l - 1].g * kernel[k][l];
b += original_data[i + k - 1][j + l - 1].b * kernel[k][l];
}
}
new_data[i][j].r = min(255, max(0, r));
new_data[i][j].g = min(255, max(0, g));
new_data[i][j].b = min(255, max(0, b));
}
}
free_image(img);
img->ppm_pixels = new_data;
printf("APPLY EDGE done\n");
}
void blur(Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if (img->format == 2) {
printf("Easy, Charlie Chaplin\n");
return;
}
float kernel[3][3] = { {1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f},
{1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f},
{1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f} };
Pixel **original_data = img->ppm_pixels;
Pixel **new_data = (Pixel**)malloc(img->h * sizeof(Pixel*));
for (int i = 0; i < img->h; i++) {
new_data[i] = (Pixel*)malloc(img->w * sizeof(Pixel));
memmove(new_data[i], original_data[i], img->w * sizeof(Pixel)); // copy original pixel data
}
for (int i = max(1, *selected_y1); i < min(img->h - 1, *selected_y2); i++) {
for (int j = max(1, *selected_x1); j < min(img->w - 1, *selected_x2); j++) {
float r = 0, g = 0, b = 0;
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
r += original_data[i + k - 1][j + l - 1].r * kernel[k][l];
g += original_data[i + k - 1][j + l - 1].g * kernel[k][l];
b += original_data[i + k - 1][j + l - 1].b * kernel[k][l];
}
}
new_data[i][j].r = min(255, max(0, (int)r));
new_data[i][j].g = min(255, max(0, (int)g));
new_data[i][j].b = min(255, max(0, (int)b));
}
}
free_image(img);
img->ppm_pixels = new_data;
printf("APPLY BLUR done\n");
}
void gaussian_blur(Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if (img->format == 2) {
printf("Easy, Charlie Chaplin\n");
return;
}
float kernel[3][3] = {
{1 / 16.0, 2 / 16.0, 1 / 16.0},
{2 / 16.0, 4 / 16.0, 2 / 16.0},
{1 / 16.0, 2 / 16.0, 1 / 16.0}
};
Pixel **original_data = img->ppm_pixels;
Pixel **new_data = (Pixel**)malloc(img->h * sizeof(Pixel*));
for (int i = 0; i < img->h; i++) {
new_data[i] = (Pixel*)malloc(img->w * sizeof(Pixel));
memmove(new_data[i], original_data[i], img->w * sizeof(Pixel)); // copy original pixel data
}
for (int i = max(1, *selected_y1); i < min(img->h - 1, *selected_y2); i++) {
for (int j = max(1, *selected_x1); j < min(img->w - 1, *selected_x2); j++) {
float r = 0, g = 0, b = 0;
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
r += original_data[i + k - 1][j + l - 1].r * kernel[k][l];
g += original_data[i + k - 1][j + l - 1].g * kernel[k][l];
b += original_data[i + k - 1][j + l - 1].b * kernel[k][l];
}
}
new_data[i][j].r = min(255, max(0, (int)r));
new_data[i][j].g = min(255, max(0, (int)g));
new_data[i][j].b = min(255, max(0, (int)b));
}
}
free_image(img);
img->ppm_pixels = new_data;
printf("APPLY GAUSSIAN_BLUR done\n");
}
void equalize(Image *img) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
if (img->format != 2) {
printf("Black and white image needed\n");
return;
}
// histogram
int histogram[256] = {0};
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
histogram[img->pgm_pixels[i][j]]++;
}
}
// CDF
double cdf[256] = {0};
int area = img->h * img->w;
cdf[0] = (double)histogram[0] / area;
for (int i = 1; i < 256; i++) {
cdf[i] = cdf[i-1] + (double)histogram[i] / area;
}
for (int i = 0; i < img->h; i++) {
for (int j = 0; j < img->w; j++) {
double new_intensity = 255 * cdf[img->pgm_pixels[i][j]];
img->pgm_pixels[i][j] = round(fmin(fmax(new_intensity, 0), 255));
}
}
printf("Equalize done\n");
}
void exit_program(Image *img) {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
// elibereaza toate resursele asociate imaginii
free_image(img);
// opreste programul
exit(0);
}
void process_command(const char *command, Image *img, int *selected_x1, int *selected_y1, int *selected_x2, int *selected_y2) {
if (strncmp(command, "LOAD ", 5) == 0) {
const char *file_name = command + 5;
if (load(file_name, img) == 0) {
printf("Loaded %s\n", file_name);
// selecteaza intreaga suprafata a imaginii
*selected_x1 = 0;
*selected_y1 = 0;
*selected_x2 = img->w;
*selected_y2 = img->h;
} else {
printf("Failed to load %s\n", file_name);
// programul va reveni la starea în care nu are niciun fisier încărcat
free_image(img);
}
} else if (strncmp(command, "SAVE ", 5) == 0) {
char file_name[100];
// formatul este text("ascii") sau binar("")
// citeste numele fisierului și formatul din comanda primita
// verifica daca formatul este "ascii" si seteaza ascii corespunzator
char format[10] = "";
int params = sscanf(command + 5, "%s %s", file_name, format);
int ascii = 0;
if (params == 2 && strcmp(format, "ascii") == 0)
ascii = 1;
if (save(file_name, img, ascii) == 0) //succes
printf("Saved %s\n", file_name);
} else if (strncmp(command, "SELECT", 6) == 0) {
select_image(command, img, selected_x1, selected_y1, selected_x2, selected_y2);
} else if (strncmp(command, "HISTOGRAM", 9) == 0){
int x, y;
if (sscanf(command + 10, "%d %d", &x, &y) != 2){
printf("Invalid command\n");
} else
histogram(img, x, y, *selected_x1, *selected_y1, *selected_x2, *selected_y2);
} else if (strcmp(command, "CROP") == 0) {
crop(img, selected_x1, selected_y1, selected_x2, selected_y2);
} else if (strncmp(command, "ROTATE", 6) == 0) {
int angle;
sscanf(command + 6, " %d", &angle);
rotate_selection(angle, img, *selected_x1, *selected_y1, *selected_x2, *selected_y2);
} else if (strncmp(command, "APPLY ", 6) == 0) {
const char* parameter = command + 6;
if (strcmp(parameter, "SHARPEN") == 0) {
sharpen(img, selected_x1, selected_y1, selected_x2, selected_y2);
}
else if (strcmp(parameter, "EDGE") == 0) {
edge(img, selected_x1, selected_y1, selected_x2, selected_y2);
}
else if (strcmp(parameter, "BLUR") == 0) {
blur(img, selected_x1, selected_y1, selected_x2, selected_y2);
}
else if (strcmp(parameter, "GAUSSIAN_BLUR") == 0) {
gaussian_blur(img, selected_x1, selected_y1, selected_x2, selected_y2);
}
else {
printf("APPLY parameter invalid\n");
}
} else if (strncmp(command, "EQUALIZE", 8) == 0) {
equalize(img);
} else if (strcmp(command, "EXIT") == 0) {
exit_program(img);
} else {
if (img == NULL || img->format == 0 || (img->ppm_pixels == NULL && img->pgm_pixels == NULL)) {
printf("No image loaded\n");
return;
}
printf("Invalid command\n");
}
}
int main (void) {
Image img = {0};
char command[100];
int selected_x1, selected_y1, selected_x2, selected_y2;
while (fgets(command, sizeof(command), stdin)){
//elimina caracterul \n citit de fgets()
command[strcspn(command, "\n")] = '\0';
process_command(command, &img, &selected_x1, &selected_y1, &selected_x2, &selected_y2);
}
return 0;
}