-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.c
118 lines (105 loc) · 2.87 KB
/
image.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
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/glcorearb.h>
#include "common.h"
#include "image.h"
static unsigned char *_get_raw_image()
{
glPixelStorei(GL_PACK_ALIGNMENT,1);
unsigned char *image;
unsigned int size = ia_cfg.screen_width * ia_cfg.screen_height * 3;
image = calloc(1, size);
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, ia_cfg.screen_width, ia_cfg.screen_height,
GL_RGB, GL_UNSIGNED_BYTE, image);
return image;
}
static void _flip(unsigned char *image)
{
int count = 0, idx;
unsigned char temp_image[ia_cfg.screen_width * 3];
memset(&temp_image, 0, sizeof(temp_image));
for(idx = ia_cfg.screen_height - 1; idx >= 0 && count < idx; idx--) {
memcpy(&temp_image,
image + (ia_cfg.screen_width * 3 * count),
ia_cfg.screen_width * 3);
memcpy(image + (ia_cfg.screen_width * 3 * count),
image + (ia_cfg.screen_width * 3 * idx),
ia_cfg.screen_width * 3);
memcpy(image + (ia_cfg.screen_width * 3 * idx),
&temp_image,
ia_cfg.screen_width * 3);
count++;
}
//printf("idx = %d\ncount = %d\n", idx, count);
}
void img_free(struct img_bitmap *bmp)
{
al_destroy_bitmap(bmp->bmp);
free(bmp);
}
struct img_bitmap *img_clone(struct img_bitmap *bmp)
{
struct img_bitmap *new_bmp
= malloc(sizeof(struct img_bitmap));
new_bmp->score = bmp->score;
new_bmp->bmp = al_clone_bitmap(bmp->bmp);
return new_bmp;
}
struct img_bitmap *img_load(const char *filename)
{
struct img_bitmap *image = calloc(1, sizeof(struct img_bitmap));
if(image == NULL) {
printfd("memory allocation error");
return NULL;
}
image->bmp = al_load_bitmap(filename);
if(image->bmp == NULL) {
printfd("unable to open %s", filename);
free(image);
image = NULL;
}
return image;
}
struct img_bitmap *img_from_GL()
{
unsigned char *raw = _get_raw_image();
struct img_bitmap *image = calloc(1, sizeof(struct img_bitmap));
if(!image) {
return NULL;
}
_flip(raw);
ALLEGRO_BITMAP *bmp;
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
bmp = al_create_bitmap(ia_cfg.screen_width, ia_cfg.screen_height);
al_set_target_bitmap(bmp);
ALLEGRO_LOCKED_REGION *locked;
locked = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_BGR_888,
ALLEGRO_LOCK_WRITEONLY);
al_free(locked->data);
locked->data = raw;
al_unlock_bitmap(bmp);
image->bmp = bmp;
return image;
}
void img_assign_score(struct img_bitmap *img, struct img_bitmap *ref)
{
int idx, jdx, diff = 0;
int64_t total_difference = 0;
ALLEGRO_COLOR bpc, rpc;
for(idx = 0; idx < ia_cfg.screen_width; idx++) {
for(jdx = 0; jdx < ia_cfg.screen_height; jdx++) {
unsigned char br, bg, bb, rr, rg, rb;
bpc = al_get_pixel(img->bmp, idx, jdx);
rpc = al_get_pixel(ref->bmp, idx, jdx);
al_unmap_rgb(bpc, &br, &bg, &bb);
al_unmap_rgb(rpc, &rr, &rg, &rb);
diff = abs(br - rr) +
abs(bg - rg) +
abs(bb - rb);
total_difference += diff;
}
}
img->score = total_difference;
}