-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.h
370 lines (313 loc) · 10.8 KB
/
image.h
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
/* See LICENSE for licence details. */
/* this header file depends loader.h */
/* inline functions:
never access member of struct image_t directly */
static inline int get_frame_count(struct image_t *img)
{
return img->frame_count;
}
static inline uint8_t *get_current_frame(struct image_t *img)
{
return img->data[img->current_frame];
}
static inline int get_current_delay(struct image_t *img)
{
return img->delay[img->current_frame];
}
static inline void increment_frame(struct image_t *img)
{
img->current_frame = (img->current_frame + 1) % img->frame_count;
}
static inline int get_image_width(struct image_t *img)
{
return img->width;
}
static inline int get_image_height(struct image_t *img)
{
return img->height;
}
static inline int get_image_channel(struct image_t *img)
{
return img->channel;
}
static inline void get_rgb(struct image_t *img, uint8_t *data, int x, int y, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
{
uint8_t *ptr;
ptr = data + img->channel * (y * img->width + x);
if (img->channel <= 2) { /* grayscale (+ alpha) */
*r = *g = *b = *ptr;
if (img->alpha && a)
*a = *(ptr + 1);
} else { /* rgb (+ alpha) */
*r = *ptr; *g = *(ptr + 1); *b = *(ptr + 2);
if (img->alpha && a)
*a = *(ptr + 3);
}
}
static inline void get_average(struct image_t *img, uint8_t *data, int x_from, int y_from, int x_to, int y_to, uint8_t pixel[])
{
int cell_num;
uint8_t r, g, b;
uint16_t rsum, gsum, bsum;
rsum = gsum = bsum = 0;
for (int y = y_from; y < y_to; y++) {
for (int x = x_from; x < x_to; x++) {
get_rgb(img, data, x, y, &r, &g, &b, NULL);
rsum += r; gsum += g; bsum += b;
}
}
cell_num = (y_to - y_from) * (x_to - x_from);
if (cell_num > 1) {
rsum /= cell_num; gsum /= cell_num; bsum /= cell_num;
}
if (img->channel <= 2)
*pixel++ = rsum;
else {
*pixel++ = rsum; *pixel++ = gsum; *pixel++ = bsum;
}
if (img->alpha)
*pixel = 0;
}
/* some image proccessing functions:
never use *_single functions directly */
uint8_t *rotate_image_single(struct image_t *img, uint8_t *data, int angle)
{
int x1, x2, y1, y2, r, dst_width, dst_height;
uint8_t *rotated_data;
long offset_dst, offset_src;
static const int cos[3] = {0, -1, 0};
static const int sin[3] = {1, 0, -1};
int shift[3][3] = {
/* x_shift, y_shift, sign */
{img->height - 1, 0 , -1},
{img->width - 1, img->height - 1, 1},
{ 0, img->width - 1, -1}
};
if (angle != 90 && angle != 180 && angle != 270)
return NULL;
/* r == 0: clockwise : (angle 90) */
/* r == 1: upside down : (angle 180) */
/* r == 2: counter clockwise: (angle 270) */
r = angle / 90 - 1;
if (angle == 90 || angle == 270) {
dst_width = img->height;
dst_height = img->width;
} else {
dst_width = img->width;
dst_height = img->height;
}
if ((rotated_data = (uint8_t *) ecalloc(dst_width * dst_height, img->channel)) == NULL)
return NULL;
logging(DEBUG, "rotated image: %dx%d size:%d\n",
dst_width, dst_height, dst_width * dst_height * img->channel);
for (y2 = 0; y2 < dst_height; y2++) {
for (x2 = 0; x2 < dst_width; x2++) {
x1 = ((x2 - shift[r][0]) * cos[r] - (y2 - shift[r][1]) * sin[r]) * shift[r][2];
y1 = ((x2 - shift[r][0]) * sin[r] + (y2 - shift[r][1]) * cos[r]) * shift[r][2];
offset_src = img->channel * (y1 * img->width + x1);
offset_dst = img->channel * (y2 * dst_width + x2);
memcpy(rotated_data + offset_dst, data + offset_src, img->channel);
}
}
free(data);
img->width = dst_width;
img->height = dst_height;
return rotated_data;
}
void rotate_image(struct image_t *img, int angle, bool rotate_all)
{
uint8_t *rotated_data;
if (rotate_all) {
for (int i = 0; i < img->frame_count; i++)
if ((rotated_data = rotate_image_single(img, img->data[i], angle)) != NULL)
img->data[i] = rotated_data;
} else {
if ((rotated_data = rotate_image_single(img, img->data[img->current_frame], angle)) != NULL)
img->data[img->current_frame] = rotated_data;
}
}
uint8_t *resize_image_single(struct image_t *img, uint8_t *data, int disp_width, int disp_height)
{
/* TODO: support enlarge */
int width_rate, height_rate, resize_rate;
int dst_width, dst_height, y_from, x_from, y_to, x_to;
uint8_t *resized_data, pixel[img->channel];
long offset_dst;
width_rate = MULTIPLER * disp_width / img->width;
height_rate = MULTIPLER * disp_height / img->height;
resize_rate = (width_rate < height_rate) ? width_rate: height_rate;
logging(DEBUG, "width_rate:%.2d height_rate:%.2d resize_rate:%.2d\n",
width_rate, height_rate, resize_rate);
/* only support shrink */
if ((resize_rate / MULTIPLER) >= 1)
return NULL;
/* FIXME: let the same num (img->width == fb->info.width), if it causes SEGV, remove "+ 1" */
dst_width = resize_rate * img->width / MULTIPLER + 1;
dst_height = resize_rate * img->height / MULTIPLER;
if ((resized_data = (uint8_t *) ecalloc(dst_width * dst_height, img->channel)) == NULL)
return NULL;
logging(DEBUG, "resized image: %dx%d size:%d\n",
dst_width, dst_height, dst_width * dst_height * img->channel);
for (int y = 0; y < dst_height; y++) {
y_from = MULTIPLER * y / resize_rate;
y_to = MULTIPLER * (y + 1) / resize_rate;
for (int x = 0; x < dst_width; x++) {
x_from = MULTIPLER * x / resize_rate;
x_to = MULTIPLER * (x + 1) / resize_rate;
get_average(img, data, x_from, y_from, x_to, y_to, pixel);
offset_dst = img->channel * (y * dst_width + x);
memcpy(resized_data + offset_dst, pixel, img->channel);
}
}
free(data);
img->width = dst_width;
img->height = dst_height;
return resized_data;
}
void resize_image(struct image_t *img, int disp_width, int disp_height, bool resize_all)
{
uint8_t *resized_data;
if (resize_all) {
for (int i = 0; i < img->frame_count; i++)
if ((resized_data = resize_image_single(img, img->data[i], disp_width, disp_height)) != NULL)
img->data[i] = resized_data;
} else {
if ((resized_data = resize_image_single(img, img->data[img->current_frame], disp_width, disp_height)) != NULL)
img->data[img->current_frame] = resized_data;
}
}
uint8_t *normalize_bpp_single(struct image_t *img, uint8_t *data, int bytes_per_pixel)
{
uint8_t *normalized_data, *src, *dst, r, g, b;
if ((normalized_data = (uint8_t *)
ecalloc(img->width * img->height, bytes_per_pixel)) == NULL)
return NULL;
if (img->channel <= 2) { /* grayscale (+ alpha) */
for (int y = 0; y < img->height; y++) {
for (int x = 0; x < img->width; x++) {
src = data + img->channel * (y * img->width + x);
dst = normalized_data + bytes_per_pixel * (y * img->width + x);
*dst = *src; *(dst + 1) = *src; *(dst + 2) = *src;
}
}
} else { /* rgb (+ alpha) */
for (int y = 0; y < img->height; y++) {
for (int x = 0; x < img->width; x++) {
get_rgb(img, data, x, y, &r, &g, &b, NULL);
dst = normalized_data + bytes_per_pixel * (y * img->width + x);
*dst = r; *(dst + 1) = g; *(dst + 2) = b;
}
}
}
free(data);
return normalized_data;
}
void normalize_bpp(struct image_t *img, int bytes_per_pixel, bool normalize_all)
{
uint8_t *normalized_data;
/* XXX: now only support bytes_per_pixel == 3 */
if (bytes_per_pixel != 3)
return;
if (normalize_all) {
for (int i = 0; i < img->frame_count; i++)
if ((normalized_data = normalize_bpp_single(img, img->data[i], bytes_per_pixel)) != NULL)
img->data[i] = normalized_data;
} else {
if ((normalized_data = normalize_bpp_single(img, img->data[img->current_frame], bytes_per_pixel)) != NULL)
img->data[img->current_frame] = normalized_data;
}
}
void draw_image_single(struct framebuffer_t *fb, struct image_t *img, uint8_t *data,
int offset_x, int offset_y, int shift_x, int shift_y, int width, int height, uint8_t alpha_background)
{
int offset, size;
uint8_t r, g, b, a;
uint32_t color, pixel, br, bg, bb;
for (int y = 0; y < height; y++) {
if (y >= fb->info.height)
break;
for (int x = 0; x < width; x++) {
if (x >= fb->info.width)
break;
if (img->alpha) { /* alpha brend */
get_rgb(img, data, x + shift_x, y + shift_y, &r, &g, &b, &a);
//logging(WARN, "r:0x%.2X g:0x%.2X b:0x%.2X a:0x%.2X\n", r, g, b, a);
br = (((uint32_t) r * a) + alpha_background * (0xFF - a)) / 0xFF;
bg = (((uint32_t) g * a) + alpha_background * (0xFF - a)) / 0xFF;
bb = (((uint32_t) b * a) + alpha_background * (0xFF - a)) / 0xFF;
//logging(WARN, "br:0x%.2X bg:0x%.2X bb:0x%.2X\n", br, bg, bb);
color = (br << 16) + (bg << 8) + bb;
} else {
get_rgb(img, data, x + shift_x, y + shift_y, &r, &g, &b, NULL);
color = (r << 16) + (g << 8) + b;
}
pixel = color2pixel(&fb->info, color);
/* update copy buffer */
offset = (y + offset_y) * fb->info.line_length + (x + offset_x) * fb->info.bytes_per_pixel;
memcpy(fb->buf + offset, &pixel, fb->info.bytes_per_pixel);
}
/* draw each scanline */
if (width < fb->info.width) {
offset = (y + offset_y) * fb->info.line_length + offset_x * fb->info.bytes_per_pixel;
size = width * fb->info.bytes_per_pixel;
memcpy(fb->fp + offset, fb->buf + offset, size);
}
}
/* we can draw all image data at once! */
if (width >= fb->info.width) {
size = (height > fb->info.height) ? fb->info.height: height;
size *= fb->info.line_length;
memcpy(fb->fp, fb->buf, size);
}
}
void draw_image(struct framebuffer_t *fb, struct image_t *img,
int offset_x, int offset_y, int shift_x, int shift_y, int width, int height,
uint8_t alpha_background, bool enable_anim)
{
/*
+- screen -----------------+
| ^ |
| | offset_y |
| v |
| +- image --+ |
|<------>| | |
|offset_x| | |
| | | |
| +----------+ |
+--------------|-----------+
|
v
+- image ----------------------+
| ^ |
| | shift_y |
| v |
| +- view port + ^ |
|<----->| | | |
|shift_x| | | height|
| | | | |
| +------------+ v |
| <- width -> |
+------------------------------+
*/
int loop_count = 0;
if (shift_x + width > img->width)
width = img->width - shift_x;
if (shift_y + height > img->height)
height = img->height - shift_y;
if (offset_x + width > fb->info.width)
width = fb->info.width - offset_x;
if (offset_y + height > fb->info.height)
height = fb->info.height - offset_y;
/* XXX: ignore img->loop_count, force 1 loop */
if (enable_anim) {
while (loop_count < img->frame_count) {
draw_image_single(fb, img, img->data[loop_count],
offset_x, offset_y, shift_x, shift_y, width, height, alpha_background);
usleep(img->delay[loop_count] * 10000); /* gif delay 1 == 1/100 sec */
loop_count++;
}
} else {
draw_image_single(fb, img, img->data[img->current_frame],
offset_x, offset_y, shift_x, shift_y, width, height, alpha_background);
}
}