-
Notifications
You must be signed in to change notification settings - Fork 2
/
loader.h
652 lines (533 loc) · 14.8 KB
/
loader.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* See LICENSE for licence details. */
/* for jpeg */
#include <jpeglib.h>
/* for png */
#include <png.h>
/* for tiff */
#include <tiffio.h>
/* for gif/bmp/(ico not supported) */
#include "libnsgif.h"
#include "libnsbmp.h"
//#define STB_IMAGE_IMPLEMENTATION
/* to remove math.h dependency */
//#define STBI_NO_HDR
//#include "stb_image.h"
enum {
CHECK_HEADER_SIZE = 8,
BYTES_PER_PIXEL = 4,
PNG_HEADER_SIZE = 8,
MAX_FRAME_NUM = 128, /* limit of gif frames */
};
enum filetype_t {
TYPE_JPEG,
TYPE_PNG,
TYPE_TIFF,
TYPE_BMP,
TYPE_GIF,
TYPE_PNM,
TYPE_UNKNOWN,
};
struct image_t {
/* normally use data[0], data[n] (n > 1) for animanion gif */
uint8_t *data[MAX_FRAME_NUM];
int width;
int height;
int channel;
bool alpha;
/* for animation gif */
int delay[MAX_FRAME_NUM];
int frame_count; /* normally 1 */
int loop_count;
int current_frame; /* for yaimgfb */
};
/* libjpeg functions */
struct my_jpeg_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
void my_jpeg_exit(j_common_ptr cinfo)
{
char last_msg[JMSG_LENGTH_MAX];
struct my_jpeg_error_mgr *myerr = (struct my_jpeg_error_mgr *) cinfo->err;
(*cinfo->err->format_message)(cinfo, last_msg);
logging(ERROR, "libjpeg: %s\n", last_msg);
longjmp(myerr->setjmp_buffer, 1);
}
void my_jpeg_error(j_common_ptr cinfo)
{
char last_msg[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, last_msg);
logging(ERROR, "libjpeg: %s\n", last_msg);
}
void my_jpeg_warning(j_common_ptr cinfo, int msg_level)
{
char last_msg[JMSG_LENGTH_MAX];
(*cinfo->err->format_message)(cinfo, last_msg);
if (msg_level < 0) { /* warning */
if (cinfo->err->num_warnings == 0 || cinfo->err->trace_level >= 3)
logging(WARN, "libjpeg: %s\n", last_msg);
cinfo->err->num_warnings++;
} else { /* trace */
if (cinfo->err->trace_level >= msg_level)
logging(WARN, "libjpeg: %s\n", last_msg);
}
}
bool load_jpeg(const char *path, FILE *fp, struct image_t *img)
{
int row_stride, size;
JSAMPARRAY buffer;
struct jpeg_decompress_struct cinfo;
struct my_jpeg_error_mgr jerr;
(void) path;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_jpeg_exit;
jerr.pub.emit_message = my_jpeg_warning;
jerr.pub.output_message = my_jpeg_error;
if (setjmp(jerr.setjmp_buffer)) {
jpeg_destroy_decompress(&cinfo);
return false;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, TRUE);
/* disable colormap (indexed color), grayscale -> rgb */
cinfo.quantize_colors = FALSE;
cinfo.out_color_space = JCS_RGB;
jpeg_start_decompress(&cinfo);
img->width = cinfo.output_width;
img->height = cinfo.output_height;
img->channel = cinfo.output_components;
size = img->width * img->height * img->channel;
if ((img->data[0] = (uint8_t *) ecalloc(1, size)) == NULL) {
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return false;
}
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(img->data[0] + (cinfo.output_scanline - 1) * row_stride, buffer[0], row_stride);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return true;
}
/* libpng function */
void my_png_error(png_structp png_ptr, png_const_charp error_msg)
{
logging(ERROR, "libpng: %s\n", error_msg);
if (png_ptr)
longjmp(png_jmpbuf(png_ptr), 1);
}
void my_png_warning(png_structp png_ptr, png_const_charp warning_msg)
{
(void) png_ptr;
logging(WARN, "libpng: %s\n", warning_msg);
}
bool load_png(const char *path, FILE *fp, struct image_t *img)
{
int row_stride, size;
png_bytep *row_pointers = NULL;
unsigned char header[PNG_HEADER_SIZE];
png_structp png_ptr;
png_infop info_ptr;
(void) path;
if (fread(header, 1, PNG_HEADER_SIZE, fp) != PNG_HEADER_SIZE)
return false;
if (png_sig_cmp(header, 0, PNG_HEADER_SIZE))
return false;
if ((png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, my_png_error, my_png_warning)) == NULL)
return false;
if ((info_ptr = png_create_info_struct(png_ptr)) == NULL) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
return false;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return false;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, PNG_HEADER_SIZE);
/* force 3 bytes per pixel image
(- strip alpha)
- 6 bytes per pixel -> 3 bytes per pixel
- 1,2,4 bits per color -> 8 bits per color
- grayscale -> rgb
- perform set_expand() */
png_read_png(png_ptr, info_ptr,
PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING
| PNG_TRANSFORM_GRAY_TO_RGB | PNG_TRANSFORM_EXPAND, NULL);
//PNG_TRANSFORM_STRIP_ALPHA
//png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING, NULL);
img->width = png_get_image_width(png_ptr, info_ptr);
img->height = png_get_image_height(png_ptr, info_ptr);
img->channel = png_get_channels(png_ptr, info_ptr);
size = img->width * img->height * img->channel;
if ((img->data[0] = (uint8_t *) ecalloc(1, size)) == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return false;
}
row_stride = png_get_rowbytes(png_ptr, info_ptr);
row_pointers = png_get_rows(png_ptr, info_ptr);
for (int i = 0; i < img->height; i++)
memcpy(img->data[0] + row_stride * i, row_pointers[i], row_stride);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return true;
}
/* libtiff functions */
bool load_tiff(const char *path, FILE *fp, struct image_t *img)
{
TIFF *tiff;
/*
uint16_t bps, type, planar, orient;
uint32_t rps;
*/
int size;
(void) fp;
if ((tiff = TIFFOpen(path, "r")) == NULL)
return false;
if (!TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img->width)
|| !TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img->height))
/*
|| !TIFFGetField(tiff, TIFFTAG_SAMPLESPERPIXEL, &img->channel)
|| !TIFFGetField(tiff, TIFFTAG_BITSPERSAMPLE, &bps)
|| !TIFFGetField(tiff, TIFFTAG_PHOTOMETRIC, &type)
|| !TIFFGetField(tiff, TIFFTAG_PLANARCONFIG, &planar)
|| !TIFFGetField(tiff, TIFFTAG_ROWSPERSTRIP, &rps)
|| !TIFFGetField(tiff, TIFFTAG_ORIENTATION, &orient))
*/
return false;
logging(DEBUG, "width:%d height:%d\n", img->width, img->height);
/*
logging(DEBUG, "bps:%d type:%d planar:%d rps:%d orient:%d\n",
bps, type, planar, rps, orient);
*/
img->channel = 4; /* because TIFFReadRGBAImage() converts image channel == 4 */
size = img->width * img->height * img->channel;
if ((img->data[0] = (uint8_t *) ecalloc(1, size)) == NULL
|| !TIFFReadRGBAImageOriented(tiff, img->width, img->height, (uint32_t *) img->data[0], ORIENTATION_TOPLEFT, 0)) {
TIFFClose(tiff);
return false;
}
TIFFClose(tiff);
return true;
}
/* libns{gif,bmp} functions */
unsigned char *file_into_memory(FILE *fp, size_t *data_size)
{
unsigned char *buffer;
size_t n, size;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
if ((buffer = ecalloc(1, size)) == NULL)
return NULL;
fseek(fp, 0L, SEEK_SET);
if ((n = fread(buffer, 1, size, fp)) != size) {
free(buffer);
return NULL;
}
*data_size = size;
return buffer;
}
void *gif_bitmap_create(int width, int height)
{
return calloc(width * height, BYTES_PER_PIXEL);
}
void gif_bitmap_set_opaque(void *bitmap, bool opaque)
{
(void) opaque; /* unused */
(void) bitmap;
}
bool gif_bitmap_test_opaque(void *bitmap)
{
(void) bitmap; /* unused */
return false;
}
unsigned char *gif_bitmap_get_buffer(void *bitmap)
{
return bitmap;
}
void gif_bitmap_destroy(void *bitmap)
{
free(bitmap);
}
void gif_bitmap_modified(void *bitmap)
{
(void) bitmap; /* unused */
return;
}
bool load_gif(const char *path, FILE *fp, struct image_t *img)
{
gif_bitmap_callback_vt gif_callbacks = {
gif_bitmap_create,
gif_bitmap_destroy,
gif_bitmap_get_buffer,
gif_bitmap_set_opaque,
gif_bitmap_test_opaque,
gif_bitmap_modified
};
size_t size;
gif_result code;
unsigned char *mem;
gif_animation gif;
int i;
(void) path;
gif_create(&gif, &gif_callbacks);
if ((mem = file_into_memory(fp, &size)) == NULL)
return false;
code = gif_initialise(&gif, size, mem);
if (code != GIF_OK && code != GIF_WORKING)
goto error_initialize_failed;
img->width = gif.width;
img->height = gif.height;
img->channel = BYTES_PER_PIXEL; /* libnsgif always return 4bpp image */
size = img->width * img->height * img->channel;
/* read animation gif */
img->frame_count = (gif.frame_count < MAX_FRAME_NUM) ? gif.frame_count: MAX_FRAME_NUM - 1;
img->loop_count = gif.loop_count;
for (i = 0; i < img->frame_count; i++) {
code = gif_decode_frame(&gif, i);
if (code != GIF_OK)
goto error_decode_failed;
if ((img->data[i] = (uint8_t *) ecalloc(1, size)) == NULL)
goto error_decode_failed;
memcpy(img->data[i], gif.frame_image, size);
img->delay[i] = gif.frames[i].frame_delay;
}
gif_finalise(&gif);
free(mem);
return true;
error_decode_failed:
img->frame_count = i;
for (i = 0; i < img->frame_count; i++) {
free(img->data[i]);
img->data[i] = NULL;
}
gif_finalise(&gif);
error_initialize_failed:
free(mem);
return false;
}
void *bmp_bitmap_create(int width, int height, unsigned int state)
{
(void) state; /* unused */
return calloc(width * height, BYTES_PER_PIXEL);
}
unsigned char *bmp_bitmap_get_buffer(void *bitmap)
{
return bitmap;
}
void bmp_bitmap_destroy(void *bitmap)
{
free(bitmap);
}
size_t bmp_bitmap_get_bpp(void *bitmap)
{
(void) bitmap; /* unused */
return BYTES_PER_PIXEL;
}
bool load_bmp(const char *path, FILE *fp, struct image_t *img)
{
bmp_bitmap_callback_vt bmp_callbacks = {
bmp_bitmap_create,
bmp_bitmap_destroy,
bmp_bitmap_get_buffer,
bmp_bitmap_get_bpp
};
bmp_result code;
size_t size;
unsigned char *mem;
bmp_image bmp;
(void) path;
bmp_create(&bmp, &bmp_callbacks);
if ((mem = file_into_memory(fp, &size)) == NULL)
return false;
code = bmp_analyse(&bmp, size, mem);
if (code != BMP_OK)
goto error_analyse_failed;
code = bmp_decode(&bmp);
if (code != BMP_OK)
goto error_decode_failed;
img->width = bmp.width;
img->height = bmp.height;
img->channel = BYTES_PER_PIXEL; /* libnsbmp always return 4bpp image */
size = img->width * img->height * img->channel;
if ((img->data[0] = (uint8_t *) ecalloc(1, size)) == NULL)
goto error_decode_failed;
memcpy(img->data[0], bmp.bitmap, size);
bmp_finalise(&bmp);
free(mem);
return true;
error_decode_failed:
bmp_finalise(&bmp);
error_analyse_failed:
free(mem);
return false;
}
/* pnm functions */
int getint(FILE *fp)
{
int c, n = 0;
do {
c = fgetc(fp);
} while (isspace(c));
while (isdigit(c)) {
n = n * 10 + c - '0';
c = fgetc(fp);
}
return n;
}
uint8_t pnm_normalize(int c, int type, int max_value)
{
if (type == 1 || type == 4)
return (c == 0) ? 0: 0xFF;
else
return 0xFF * c / max_value;
}
bool load_pnm(const char *path, FILE *fp, struct image_t *img)
{
int size, type, c, count, max_value = 0;
(void) path;
if (fgetc(fp) != 'P')
return false;
type = fgetc(fp) - '0';
img->channel = (type == 1 || type == 2 || type == 4 || type == 5) ? 1:
(type == 3 || type == 6) ? 3: -1;
if (img->channel == -1)
return false;
/* read header */
while ((c = fgetc(fp)) != EOF) {
if (c == '#')
while ((c = fgetc(fp)) != '\n');
if (isspace(c))
continue;
if (isdigit(c)) {
ungetc(c, fp);
img->width = getint(fp);
img->height = getint(fp);
if (type != 1 && type != 4)
max_value = getint(fp);
break;
}
}
size = img->width * img->height * img->channel;
if ((img->data[0] = ecalloc(1, size)) == NULL)
return false;
/* read data */
count = 0;
if (1 <= type && type <= 3) {
while ((c = fgetc(fp)) != EOF) {
if (c == '#')
while ((c = fgetc(fp)) != '\n');
if (isspace(c))
continue;
if (isdigit(c)) {
ungetc(c, fp);
*(img->data[0] + count++) = pnm_normalize(getint(fp), type, max_value);
}
}
}
else {
while ((c = fgetc(fp)) != EOF)
*(img->data[0] + count++) = pnm_normalize(c, type, max_value);
}
return true;
}
enum filetype_t check_filetype(FILE *fp)
{
/*
JPEG(JFIF): FF D8
PNG : 89 50 4E 47 0D 0A 1A 0A (0x89 'P' 'N' 'G' '\r' '\n' 0x1A '\n')
GIF : 47 49 46 (ASCII 'G' 'I' 'F')
BMP : 42 4D (ASCII 'B' 'M')
PNM : 50 [31|32|33|34|35|36] ('P' ['1' - '6'])
*/
uint8_t header[CHECK_HEADER_SIZE];
static uint8_t jpeg_header[] = {0xFF, 0xD8};
static uint8_t png_header[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
/* little endian and big endian: BigTiff not supported */
static uint8_t tiff_header1[] = {0x49, 0x49, 0x2A, 0x00},
tiff_header2[] = {0x4D, 0x4D, 0x00, 0x2A};
static uint8_t gif_header[] = {0x47, 0x49, 0x46};
static uint8_t bmp_header[] = {0x42, 0x4D};
size_t size;
if ((size = fread(header, 1, CHECK_HEADER_SIZE, fp)) != CHECK_HEADER_SIZE) {
logging(ERROR, "couldn't read header\n");
return TYPE_UNKNOWN;
}
fseek(fp, 0L, SEEK_SET);
if (memcmp(header, jpeg_header, 2) == 0)
return TYPE_JPEG;
else if (memcmp(header, png_header, 8) == 0)
return TYPE_PNG;
else if (memcmp(header, tiff_header1, 4) == 0 || memcmp(header, tiff_header2, 4) == 0)
return TYPE_TIFF;
else if (memcmp(header, gif_header, 3) == 0)
return TYPE_GIF;
else if (memcmp(header, bmp_header, 2) == 0)
return TYPE_BMP;
else if (header[0] == 'P' && ('0' <= header[1] && header[1] <= '6'))
return TYPE_PNM;
else
return TYPE_UNKNOWN;
}
void init_image(struct image_t *img)
{
for (int i = 0; i < MAX_FRAME_NUM; i++) {
img->data[i] = NULL;
img->delay[i] = 0;
}
img->width = 0;
img->height = 0;
img->channel = 0;
img->alpha = false;
/* for animation gif */
img->frame_count = 1;
img->loop_count = 0;
img->current_frame = 0;
}
void free_image(struct image_t *img)
{
for (int i = 0; i < img->frame_count; i++) {
free(img->data[i]);
img->data[i] = NULL;
}
}
bool load_image(const char *path, struct image_t *img)
{
int i;
enum filetype_t type;
FILE *fp;
init_image(img);
static bool (*loader[])(const char *path, FILE *fp, struct image_t *img) = {
[TYPE_JPEG] = load_jpeg,
[TYPE_PNG] = load_png,
[TYPE_TIFF] = load_tiff,
[TYPE_GIF] = load_gif,
[TYPE_BMP] = load_bmp,
[TYPE_PNM] = load_pnm,
};
if ((fp = efopen(path, "r")) == NULL)
return false;
if ((type = check_filetype(fp)) == TYPE_UNKNOWN) {
logging(ERROR, "unknown file type: %s\n", path);
goto image_load_error;
}
if (loader[type](path, fp, img)) {
img->alpha = (img->channel == 2 || img->channel == 4) ? true: false;
logging(DEBUG, "image width:%d height:%d channel:%d alpha:%s\n",
img->width, img->height, img->channel, (img->alpha) ? "true": "false");
if (img->frame_count > 1) {
logging(DEBUG, "frame:%d loop:%d\n", img->frame_count, img->loop_count);
for (i = 0; i < img->frame_count; i++)
logging(DEBUG, "delay[%u]:%u\n", i, img->delay[i]);
}
efclose(fp);
return true;
}
image_load_error:
logging(ERROR, "image load error: %s\n", path);
efclose(fp);
return false;
}