forked from TecProg-20181/01--ritzare-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
267 lines (220 loc) · 7.6 KB
/
main.c
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
#include <stdio.h>
typedef struct _pixel {
unsigned short int r;
unsigned short int g;
unsigned short int b;
} Pixel;
typedef struct _image {
// [width][height][rgb]
// 0 -> r
// 1 -> g
// 2 -> b
unsigned short int pixel[512][512][3];
unsigned int w;
unsigned int h;
} Image;
int max(int a, int b) {
if (a > b)
return a;
return b;
}
int pixel_igual(Pixel p1, Pixel p2) {
if (p1.r == p2.r &&
p1.g == p2.g &&
p1.b == p2.b)
return 1;
return 0;
}
Image escala_de_cinza(Image img) {
/*for (unsigned int i = 0; i < img.h; ++i) {
for (unsigned int j = 0; j < img.w; ++j) {
print("%u", img.pixel[i][j][0] + img.pixel[i][j][1] + img.pixel[i][j][2]);
}
}*/
for (unsigned int i = 0; i < img.h; ++i) {
for (unsigned int j = 0; j < img.w; ++j) {
int media = img.pixel[i][j][0] +
img.pixel[i][j][1] +
img.pixel[i][j][2];
media /= 3;
img.pixel[i][j][0] = media;
img.pixel[i][j][1] = media;
img.pixel[i][j][2] = media;
}
}
return img;
}
void blur(unsigned int h, unsigned short int pixel[512][512][3], int T, unsigned int w) {
for (unsigned int i = 0; i < h; ++i) {
for (unsigned int j = 0; j < w; ++j) {
Pixel media = {0, 0, 0};
int menor_h = (h - 1 > i + T/2) ? i + T/2 : h - 1;
int min_w = (w - 1 > j + T/2) ? j + T/2 : w - 1;
for(int x = (0 > i - T/2 ? 0 : i - T/2); x <= menor_h; ++x) {
for(int y = (0 > j - T/2 ? 0 : j - T/2); y <= min_w; ++y) {
media.r += pixel[x][y][0];
media.g += pixel[x][y][1];
media.b += pixel[x][y][2];
}
}
// printf("%u", media.r)
media.r /= T * T;
media.g /= T * T;
media.b /= T * T;
pixel[i][j][0] = media.r;
pixel[i][j][1] = media.g;
pixel[i][j][2] = media.b;
}
}
}
Image rotacionar90direita(Image img) {
Image rotacionada;
rotacionada.w = img.h;
rotacionada.h = img.w;
for (unsigned int i = 0, y = 0; i < rotacionada.h; ++i, ++y) {
for (int j = rotacionada.w - 1, x = 0; j >= 0; --j, ++x) {
rotacionada.pixel[i][j][0] = img.pixel[x][y][0];
rotacionada.pixel[i][j][1] = img.pixel[x][y][1];
rotacionada.pixel[i][j][2] = img.pixel[x][y][2];
}
}
return rotacionada;
}
void inverter_cores(unsigned short int pixel[512][512][3],
unsigned int w, unsigned int h) {
for (unsigned int i = 0; i < h; ++i) {
for (unsigned int j = 0; j < w; ++j) {
pixel[i][j][0] = 255 - pixel[i][j][0];
pixel[i][j][1] = 255 - pixel[i][j][1];
pixel[i][j][2] = 255 - pixel[i][j][2];
}
}
}
Image cortar_imagem(Image img, int x, int y, int w, int h) {
Image cortada;
cortada.w = w;
cortada.h = h;
for(int i = 0; i < h; ++i) {
for(int j = 0; j < w; ++j) {
cortada.pixel[i][j][0] = img.pixel[i + y][j + x][0];
cortada.pixel[i][j][1] = img.pixel[i + y][j + x][1];
cortada.pixel[i][j][2] = img.pixel[i + y][j + x][2];
}
}
return cortada;
}
int main() {
Image img;
// read type of image
char p3[4];
scanf("%s", p3);
// read width height and color of image
int max_color;
scanf("%u %u %d", &img.w, &img.h, &max_color);
// read all pixels of image
for (unsigned int i = 0; i < img.h; ++i) {
for (unsigned int j = 0; j < img.w; ++j) {
scanf("%hu %hu %hu", &img.pixel[i][j][0],
&img.pixel[i][j][1],
&img.pixel[i][j][2]);
}
}
int n_opcoes;
scanf("%d", &n_opcoes);
for(int i = 0; i < n_opcoes; ++i) {
int opcao;
scanf("%d", &opcao);
switch(opcao) {
case 1: { // Escala de Cinza
img = escala_de_cinza(img);
break;
}
case 2: { // Filtro Sepia
for (unsigned int x = 0; x < img.h; ++x) {
for (unsigned int j = 0; j < img.w; ++j) {
unsigned short int pixel[3];
pixel[0] = img.pixel[x][j][0];
pixel[1] = img.pixel[x][j][1];
pixel[2] = img.pixel[x][j][2];
int p = pixel[0] * .393 + pixel[1] * .769 + pixel[2] * .189;
int menor_r = (255 > p) ? p : 255;
img.pixel[x][j][0] = menor_r;
p = pixel[0] * .349 + pixel[1] * .686 + pixel[2] * .168;
menor_r = (255 > p) ? p : 255;
img.pixel[x][j][1] = menor_r;
p = pixel[0] * .272 + pixel[1] * .534 + pixel[2] * .131;
menor_r = (255 > p) ? p : 255;
img.pixel[x][j][2] = menor_r;
}
}
break;
}
case 3: { // Blur
int tamanho = 0;
scanf("%d", &tamanho);
blur(img.h, img.pixel, tamanho, img.w);
break;
}
case 4: { // Rotacao
int quantas_vezes = 0;
scanf("%d", &quantas_vezes);
quantas_vezes %= 4;
for (int j = 0; j < quantas_vezes; ++j) {
img = rotacionar90direita(img);
}
break;
}
case 5: { // Espelhamento
int horizontal = 0;
scanf("%d", &horizontal);
int w = img.w, h = img.h;
if (horizontal == 1) w /= 2;
else h /= 2;
for (int i2 = 0; i2 < h; ++i2) {
for (int j = 0; j < w; ++j) {
int x = i2, y = j;
if (horizontal == 1) y = img.w - 1 - j;
else x = img.h - 1 - i2;
Pixel aux1;
aux1.r = img.pixel[i2][j][0];
aux1.g = img.pixel[i2][j][1];
aux1.b = img.pixel[i2][j][2];
img.pixel[i2][j][0] = img.pixel[x][y][0];
img.pixel[i2][j][1] = img.pixel[x][y][1];
img.pixel[i2][j][2] = img.pixel[x][y][2];
img.pixel[x][y][0] = aux1.r;
img.pixel[x][y][1] = aux1.g;
img.pixel[x][y][2] = aux1.b;
}
}
break;
}
case 6: { // Inversao de Cores
inverter_cores(img.pixel, img.w, img.h);
break;
}
case 7: { // Cortar Imagem
int x, y;
scanf("%d %d", &x, &y);
int w, h;
scanf("%d %d", &w, &h);
img = cortar_imagem(img, x, y, w, h);
break;
}
}
}
// print type of image
printf("P3\n");
// print width height and color of image
printf("%u %u\n255\n", img.w, img.h);
// print pixels of image
for (unsigned int i = 0; i < img.h; ++i) {
for (unsigned int j = 0; j < img.w; ++j) {
printf("%hu %hu %hu ", img.pixel[i][j][0],
img.pixel[i][j][1],
img.pixel[i][j][2]);
}
printf("\n");
}
return 0;
}