forked from mackstann/mona
-
Notifications
You must be signed in to change notification settings - Fork 2
/
diff.c
executable file
·60 lines (46 loc) · 1.43 KB
/
diff.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
#include <stdlib.h>
#include <cairo.h>
#include "mona.h"
int MAX_FITNESS = -1;
unsigned char *goal_data = NULL;
int difference_init()
{
return 0;
}
void difference_clean()
{
}
int difference(cairo_surface_t * test_surf, cairo_surface_t * goal_surf)
{
unsigned char *test_data = cairo_image_surface_get_data(test_surf);
if (!goal_data)
goal_data = cairo_image_surface_get_data(goal_surf);
int difference = 0;
int my_max_fitness = 0;
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int thispixel = y * WIDTH * 4 + x * 4;
// On little-endian machine test_data and goal_data have BGRA bytes order.
// Since pre-multiplied alpha is used, we don`t need alpha byte at all.
// FIXME: will not work on big-endian, need to fix.
unsigned char test_b = test_data[thispixel];
unsigned char test_g = test_data[thispixel + 1];
unsigned char test_r = test_data[thispixel + 2];
unsigned char goal_b = goal_data[thispixel];
unsigned char goal_g = goal_data[thispixel + 1];
unsigned char goal_r = goal_data[thispixel + 2];
if (MAX_FITNESS == -1)
my_max_fitness += goal_r + goal_g + goal_b;
difference += ABS(test_r - goal_r);
difference += ABS(test_g - goal_g);
difference += ABS(test_b - goal_b);
}
}
if (MAX_FITNESS == -1)
MAX_FITNESS = my_max_fitness;
return difference;
}
int get_max_fitness(void)
{
return MAX_FITNESS;
}