unsigned int encode_rec struct image img unsigned int unsigned int uns

 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
unsigned int encode_rec (
struct image *img,
unsigned int x,
unsigned int y,
unsigned int divider,
unsigned int comp,
float quality,
FILE *file)
{
unsigned int i, j;
unsigned int square_size = img->width >> divider;
if (square_size == 1) {
fputc(getComp(img, x, y, comp), file);
return;
}
float average = 0.0;
for (i = 0; i < square_size; i++)
for (j = 0; j < square_size; j++)
average += getComp(img, x + i, y + j, comp);
average /= i * j;
for (i = 0; i < square_size; i++) {
for (j = 0; j < square_size; j++) {
if ((abs(average - getComp(img, x + i, y + j, comp)) / average) > (1 - quality)) {
unsigned int new_size = square_size >> 1;
if(new_size == 0) return;
else {
fputc(0x00, file);
for (i = 0; i <= new_size; i+=new_size)
for (j = 0; j <= new_size; j+=new_size)
encode_rec(img, x + j, y + i, divider + 1, comp, quality, file);
return;
}
}
}
}
uint8_t val = ceil(average);
fputc(val == 0x00 ? 0x01 : val, file);
}