This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageomatic.c
457 lines (363 loc) · 9.63 KB
/
Imageomatic.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
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
/*
Linguagens e Ambientes de Programação - Projeto de 2020/2021
Imageomatic module body
largura maxima = 100 colunas
tab = 4 espaços
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
Este ficheiro constitui apenas um ponto de partida para o
seu trabalho. Todo este ficheiro pode e deve ser alterado
à vontade, a começar por este comentário.
IDENTIFICAÇÃO DOS AUTORES -
Aluno 1: 57706, Raquel Melo
Aluno 2: 57882, Luis Tripa
Comentarios:
Todos os comandos foram implementados.
*/
#include "Imageomatic.h"
/*** TYPE Int2 ***/
/* More Int2 functions, in case you need them */
/*** TYPE Pixel ***/
/* More Pixel functions, in case you need them */
/*** TYPE Image ***/
void initialization(void)
{
// This function is automatically called when the interpreter starts.
// If you need to perform some initialization, this is the place
// to write the initialization code.
}
Int2 imageCopy(Image img, Int2 n, Image res)
{
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
res[i.x][i.y] = img[i.x][i.y];
}
return n;
}
Int2 imageGrayscale(Image img, Int2 n, Image res) {
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
Pixel p = img[i.x][i.y];
int gray = pixelGrayAverage(p);
res[i.x][i.y] = pixelGray(gray);
}
return n;
}
Int2 imageNegative(Image img, Int2 n, Image res) {
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
Pixel p = img[i.x][i.y];
Pixel new = pixel(MAX_COLOR - p.red, MAX_COLOR - p.green , MAX_COLOR - p.blue);
res[i.x][i.y] = new;
}
return n;
}
//nao funciona para imagens de tamanho impar
Int2 imageHalf(Image img, Int2 n, Image res) {
Int2 i;
for(i.y = 0; i.y < n.y; i.y = i.y+2)
for(i.x = 0; i.x < n.x; i.x = i.x+2) {
res[i.x/2][i.y/2] = img[i.x][i.y];
}
i.y = n.y/2; i.x = n.x/2;
return i;
}
Int2 imagePaint(String cor, Int2 n, Image res) {
FILE * file = fopen(colorsFileName, "r");
Pixel p = black;
String color_name;
int hex = 0;
int color = 0;
bool hasColor = false;
// Iterate over the entire file
while (!hasColor && fscanf(file, "%x %s", &color, color_name) != EOF) {
if (strcmp(cor, color_name) == 0) {
hasColor = true;
break;
}
}
// Check if cor is an hex string
if (strlen(cor) == 6 && sscanf(cor, "%x", &color) != EOF)
hasColor = true;
if (hasColor) {
int red = (color & 0xFF0000) >> 16;
int green = (color & 0x00FF00) >> 8;
int blue = color & 0x0000FF;
p = pixel(red, green, blue);
}
// Paint the entire thing
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
res[i.x][i.y] = p;
}
return n;
}
Int2 imageRotation90(Image img, Int2 n, Image res)
{
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
res[(n.y-1) - i.y][i.x] = img[i.x][i.y];
}
return int2(n.y, n.x);
}
Int2 imagePosterize(Image img, Int2 n, int factor, Image res)
{
int G = 256 / (1 << factor);
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
Pixel p = img[i.x][i.y];
Pixel new = pixel(G*(p.red / G), G*(p.green / G), G*(p.blue / G));
res[i.x][i.y] = new;
}
return n;
}
Int2 imageDroplet(Int2 n, Image res) {
Int2 i;
Int2 center = int2Half(n);
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
double dist = int2Distance(center, i);
int gray = 0.7 * MAX_COLOR + 0.3 * sin(dist / 20.0) * MAX_COLOR;
res[i.x][i.y] = pixelGray(gray);
}
return n;
}
Int2 cropToImageBoundaries(Int2 current, Int2 limits)
{
if (current.x < 0)
current.x = 0;
else if (current.x > limits.x)
current.x = limits.x;
if (current.y < 0)
current.y = 0;
else if (current.y > limits.y)
current.y = limits.y;
return current;
}
Int2 imageBlur(Image img, Int2 n, int nivel, Image res)
{
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
Pixel p = img[i.x][i.y];
// Generate matrix Origin, LimitX and LimitY
int levels = 2 * nivel + 1;
Int2 O;
int LX, LY;
O = int2(i.x - nivel, i.y - nivel);
LX = O.x + levels;
LY = O.y + levels;
if (LX > n.x)
LX = n.x;
if (LY > n.y)
LY = n.y;
// Verify if origin is within the image and cut it to fit it, if needed
O = cropToImageBoundaries(O, n);
// Iterate over all pixels in the sub-matrix
Int2 j;
int pixel_count = 0;
int avgR = 0, avgG = 0, avgB = 0;
for (j.y = O.y; j.y < LY; j.y++)
for (j.x = O.x; j.x < LX; j.x++) {
Pixel c = img[j.x][j.y];
avgR = avgR + c.red;
avgG = avgG + c.green;
avgB = avgB + c.blue;
pixel_count++;
}
// Replace with the average value
Pixel new = pixel(avgR / pixel_count, avgG / pixel_count, avgB / pixel_count);
res[i.x][i.y] = new;
}
return n;
}
Int2 imageMask(Image img1, Int2 n1, Image img2, Int2 n2, Image res) // pre: int2Equals(n1, n2)
{
Int2 i;
for(i.y = 0; i.y < n1.y; i.y++)
for(i.x = 0; i.x < n1.x; i.x++) {
Pixel a = img1[i.x][i.y];
Pixel b = img2[i.x][i.y];
int red = a.red * (b.red/255.0);
int green = a.green * (b.green/255.0);
int blue = a.blue * (b.blue/255.0);
res[i.x][i.y] = pixel(red, green, blue);
}
return n1;
}
Int2 imageFunctionPlotting(DoubleFun fun, int scale, Int2 n, Image res)
{
Int2 center = int2Half(n);
// Draw the x and y axis and white background
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
if (i.x == center.x || i.y == center.y)
res[i.x][i.y] = black;
else
res[i.x][i.y] = white;
}
// Draw the function
for (int x=-center.x; x<center.x; x++) {
double value = scale*fun((double)x/scale);
int position = fabs(value - center.y);
if (value < center.y && value > -center.y)
res[x+center.x][position] = black;
}
return n;
}
Int2 imageOrderedDithering(Image img, Int2 n, Image res)
{
#define INDEX_SIDE 8
Byte indexMatrix[INDEX_SIDE][INDEX_SIDE] = {
{ 0, 32, 8, 40, 2, 34, 10, 42},
{48, 16, 56, 24, 50, 18, 58, 26},
{12, 44, 4, 36, 14, 46, 6, 28},
{60, 28, 52, 20, 62, 30, 54, 22},
{ 3, 35, 11, 43, 1, 33, 9, 41},
{51, 19, 59, 27, 49, 17, 57, 25},
{15, 47, 7, 39, 13, 45, 5, 37},
{63, 31, 55, 23, 61, 29, 53, 21}
};
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
Pixel p = img[i.x][i.y];
double average = pixelGrayAverage(p)/4.0;
int x = i.x % INDEX_SIDE;
int y = i.y % INDEX_SIDE;
if (average > (double) indexMatrix[x][y])
res[i.x][i.y] = white;
else {
res[i.x][i.y] = black;
}
}
return n;
}
char convertToSixBits(char c) {
if (c == '@')
return '?';
else if (c >= 'a' && c <= 'z')
return c - 96;
else if (c >= 'A' && c <= 'Z')
return c - 64;
else if (c >= ' ' && c <= '?')
return c;
else
return '?';
}
Int2 imageSteganography(Image img, Int2 n, String s, Image res)
{
int stringIndex = 0;
int maxLen = (n.x * n.y)-1;
if (strlen(s) < maxLen)
maxLen = strlen(s);
Int2 i;
for(i.y = 0; i.y < n.y; i.y++)
for(i.x = 0; i.x < n.x; i.x++) {
Pixel p = img[i.x][i.y];
if (stringIndex < maxLen) {
char c = s[stringIndex++];
c = convertToSixBits(c);
int red = (p.red & 0b11111100) + ((0b00110000 & c)>>4);
int green = (p.green & 0b11111100) + ((0b00001100 & c)>>2);
int blue = (p.blue & 0b11111100) + (0b00000011 & c);
res[i.x][i.y] = pixel(red, green, blue);
} else if (stringIndex == maxLen) {
int red = (p.red & 0b11111100);
int green = (p.green & 0b11111100);
int blue = (p.blue & 0b11111100);
res[i.x][i.y] = pixel(red, green, blue);
stringIndex++;
} else
res[i.x][i.y] = p;
}
return n;
}
Int2 imageCompare(Image imgA, Int2 nA, Image imgB, Int2 nB, Image res)
{
bool isDifferent = false;
if (int2Equals(nA, nB) == false) {
printf("Images are different in size!\n");
printf("Size A\n\tx: %d y: %d", nA.x, nA.y);
printf("Size B\n\tx: %d y: %d", nB.x, nB.y);
return nA;
}
Int2 i;
for(i.y = 0; i.y < nA.y; i.y++)
for(i.x = 0; i.x < nA.x; i.x++) {
Pixel a = imgA[i.x][i.y];
Pixel b = imgB[i.x][i.y];
if (!pixelEquals(a, b)) {
res[i.x][i.y] = pixel(255, 0, 255);
isDifferent = true;
}
}
if (isDifferent) {
printf("Images are different! Stored in A...\n");
return nA;
} else {
printf("Images are the same!\n");
res = imgA;
return nA;
}
}
void imageTests(void)
{
static Image img, img2, res;
Int2 n;
// Q
n = imageLoad("img/frutos.png", img);
n = imageGrayscale(img, n, res);
imageStore("img/cinzento.png", res, n);
// N
n = imageLoad("img/frutos.png", img);
n = imageNegative(img, n, res);
imageStore("img/negativo.png", res, n);
// H
n = imageLoad("img/frutos.png", img);
n = imageHalf(img, n, res);
imageStore("img/metade.png", res, n);
// P
n = int2(512, 512);
n = imagePaint("green", n, res);
imageStore("img/pintar.png", res, n);
// R
n = imageLoad("img/frutos.png", img);
n = imageRotation90(img, n, res);
imageStore("img/rotacao_90.png", res, n);
// O
n = imageLoad("img/frutos.png", img);
n = imagePosterize(img, n, 3, res);
imageStore("img/poster.png", res, n);
// G
n = int2(512, 512);
n = imageDroplet(n, res);
imageStore("img/goticula.png", res, n);
// D
n = imageLoad("img/frutos.png", img);
n = imageBlur(img, n, 5, res);
imageStore("img/desfocado.png", res, n);
// M
n = imageLoad("img/frutos.png", img);
n = imageDroplet(n, img2);
n = imageMask(img, n, img2, n, res);
imageStore("img/mascarar.png", res, n);
// F
n = int2(512, 512);
n = imageFunctionPlotting(sin, 50, n, res);
imageStore("img/funcao.png", res, n);
// T
n = imageLoad("img/frutos.png", img);
n = imageOrderedDithering(img, n, res);
imageStore("img/matizacao.png", res, n);
// E
n = imageLoad("img/frutos.png", img);
n = imageSteganography(img, n, "atacamos ao amanhecer", res);
imageStore("img/esteganografia.png", res, n);
}