-
Notifications
You must be signed in to change notification settings - Fork 81
/
fromgif.c
736 lines (649 loc) · 21.4 KB
/
fromgif.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
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
* This file is derived from "stb_image.h" that is in public domain.
* https://github.com/nothings/stb
*
* Hayaki Saito <saitoha@me.com> modified this and re-licensed
* it under the MIT license.
*
* Copyright (c) 2014-2018 Hayaki Saito
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "frame.h"
#include "fromgif.h"
/*
* gif_context_t struct and start_xxx functions
*
* gif_context_t structure is our basic context used by all images, so it
* contains all the IO context, plus some basic image information
*/
typedef struct
{
unsigned int img_x, img_y;
int img_n, img_out_n;
int buflen;
unsigned char buffer_start[128];
unsigned char *img_buffer, *img_buffer_end;
unsigned char *img_buffer_original;
} gif_context_t;
typedef struct
{
signed short prefix;
unsigned char first;
unsigned char suffix;
} gif_lzw;
enum {
gif_lzw_max_code_size = 12
};
typedef struct
{
int w, h;
unsigned char *out; /* output buffer (always 4 components) */
int flags, bgindex, ratio, transparent, eflags;
unsigned char pal[256][3];
unsigned char lpal[256][3];
gif_lzw codes[1 << gif_lzw_max_code_size];
unsigned char *color_table;
int parse, step;
int lflags;
int start_x, start_y;
int max_x, max_y;
int cur_x, cur_y;
int actual_width, actual_height;
int line_size;
int loop_count;
int delay;
int is_multiframe;
int is_terminated;
} gif_t;
/* initialize a memory-decode context */
static unsigned char
gif_get8(gif_context_t *s)
{
if (s->img_buffer < s->img_buffer_end) {
return *s->img_buffer++;
}
return 0;
}
static int
gif_get16le(gif_context_t *s)
{
int z = gif_get8(s);
return z + (gif_get8(s) << 8);
}
static void
gif_parse_colortable(
gif_context_t /* in */ *s,
unsigned char /* in */ pal[256][3],
int /* in */ num_entries)
{
int i;
for (i = 0; i < num_entries; ++i) {
pal[i][2] = gif_get8(s);
pal[i][1] = gif_get8(s);
pal[i][0] = gif_get8(s);
}
}
static SIXELSTATUS
gif_load_header(
gif_context_t /* in */ *s,
gif_t /* in */ *g)
{
SIXELSTATUS status = SIXEL_FALSE;
unsigned char version;
if (gif_get8(s) != 'G') {
goto end;
}
if (gif_get8(s) != 'I') {
goto end;
}
if (gif_get8(s) != 'F') {
goto end;
}
if (gif_get8(s) != '8') {
goto end;
}
version = gif_get8(s);
if (version != '7' && version != '9') {
goto end;
}
if (gif_get8(s) != 'a') {
goto end;
}
g->w = gif_get16le(s);
g->h = gif_get16le(s);
g->flags = gif_get8(s);
g->bgindex = gif_get8(s);
g->ratio = gif_get8(s);
g->transparent = (-1);
g->loop_count = (-1);
if (g->flags & 0x80) {
gif_parse_colortable(s, g->pal, 2 << (g->flags & 7));
}
status = SIXEL_OK;
end:
return status;
}
static SIXELSTATUS
gif_init_frame(
sixel_frame_t /* in */ *frame,
gif_t /* in */ *pg,
unsigned char /* in */ *bgcolor,
int /* in */ reqcolors,
int /* in */ fuse_palette)
{
SIXELSTATUS status = SIXEL_OK;
int i;
int ncolors;
frame->delay = pg->delay;
ncolors = 2 << (((pg->lflags & 0x80) ? pg->lflags : pg->flags) & 7);
if (frame->palette == NULL) {
frame->palette = (unsigned char *)sixel_allocator_malloc(frame->allocator, (size_t)(ncolors * 3));
} else if (frame->ncolors < ncolors) {
sixel_allocator_free(frame->allocator, frame->palette);
frame->palette = (unsigned char *)sixel_allocator_malloc(frame->allocator, (size_t)(ncolors * 3));
}
if (frame->palette == NULL) {
sixel_helper_set_additional_message(
"gif_init_frame: sixel_allocator_malloc() failed.");
status = SIXEL_BAD_ALLOCATION;
goto end;
}
frame->ncolors = ncolors;
if (frame->ncolors <= reqcolors && fuse_palette) {
frame->pixelformat = SIXEL_PIXELFORMAT_PAL8;
sixel_allocator_free(frame->allocator, frame->pixels);
frame->pixels = (unsigned char *)sixel_allocator_malloc(frame->allocator,
(size_t)(frame->width * frame->height));
if (frame->pixels == NULL) {
sixel_helper_set_additional_message(
"sixel_allocator_malloc() failed in gif_init_frame().");
status = SIXEL_BAD_ALLOCATION;
goto end;
}
memcpy(frame->pixels, pg->out, (size_t)(frame->width * frame->height));
for (i = 0; i < frame->ncolors; ++i) {
frame->palette[i * 3 + 0] = pg->color_table[i * 3 + 2];
frame->palette[i * 3 + 1] = pg->color_table[i * 3 + 1];
frame->palette[i * 3 + 2] = pg->color_table[i * 3 + 0];
}
if (pg->lflags & 0x80) {
if (pg->eflags & 0x01) {
if (bgcolor) {
frame->palette[pg->transparent * 3 + 0] = bgcolor[0];
frame->palette[pg->transparent * 3 + 1] = bgcolor[1];
frame->palette[pg->transparent * 3 + 2] = bgcolor[2];
} else {
frame->transparent = pg->transparent;
}
}
} else if (pg->flags & 0x80) {
if (pg->eflags & 0x01) {
if (bgcolor) {
frame->palette[pg->transparent * 3 + 0] = bgcolor[0];
frame->palette[pg->transparent * 3 + 1] = bgcolor[1];
frame->palette[pg->transparent * 3 + 2] = bgcolor[2];
} else {
frame->transparent = pg->transparent;
}
}
}
} else {
frame->pixelformat = SIXEL_PIXELFORMAT_RGB888;
frame->pixels = (unsigned char *)sixel_allocator_malloc(frame->allocator,
(size_t)(pg->w * pg->h * 3));
if (frame->pixels == NULL) {
sixel_helper_set_additional_message(
"sixel_allocator_malloc() failed in gif_init_frame().");
status = SIXEL_BAD_ALLOCATION;
goto end;
}
for (i = 0; i < pg->w * pg->h; ++i) {
frame->pixels[i * 3 + 0] = pg->color_table[pg->out[i] * 3 + 2];
frame->pixels[i * 3 + 1] = pg->color_table[pg->out[i] * 3 + 1];
frame->pixels[i * 3 + 2] = pg->color_table[pg->out[i] * 3 + 0];
}
}
frame->multiframe = (pg->loop_count != (-1));
status = SIXEL_OK;
end:
return status;
}
static void
gif_out_code(
gif_t /* in */ *g,
unsigned short /* in */ code
)
{
/* recurse to decode the prefixes, since the linked-list is backwards,
and working backwards through an interleaved image would be nasty */
if (g->codes[code].prefix >= 0) {
gif_out_code(g, (unsigned short)g->codes[code].prefix);
}
if (g->cur_y >= g->max_y) {
return;
}
g->out[g->cur_x + g->cur_y * g->max_x] = g->codes[code].suffix;
if (g->cur_x >= g->actual_width) {
g->actual_width = g->cur_x + 1;
}
if (g->cur_y >= g->actual_height) {
g->actual_height = g->cur_y + 1;
}
g->cur_x++;
if (g->cur_x >= g->max_x) {
g->cur_x = g->start_x;
g->cur_y += g->step;
while (g->cur_y >= g->max_y && g->parse > 0) {
g->step = 1 << g->parse;
g->cur_y = g->start_y + (g->step >> 1);
--g->parse;
}
}
}
static SIXELSTATUS
gif_process_raster(
gif_context_t /* in */ *s,
gif_t /* in */ *g
)
{
SIXELSTATUS status = SIXEL_FALSE;
unsigned char lzw_cs;
signed int len, code;
signed int codesize, codemask, avail, oldcode, bits, valid_bits, clear;
gif_lzw *p;
/* LZW Minimum Code Size */
lzw_cs = gif_get8(s);
if (lzw_cs > gif_lzw_max_code_size) {
sixel_helper_set_additional_message(
"Unsupported GIF (LZW code size)");
status = SIXEL_RUNTIME_ERROR;
goto end;
}
clear = 1 << lzw_cs;
codesize = lzw_cs + 1;
codemask = (1 << codesize) - 1;
bits = 0;
valid_bits = 0;
for (code = 0; code < clear; code++) {
g->codes[code].prefix = -1;
g->codes[code].first = (unsigned char) code;
g->codes[code].suffix = (unsigned char) code;
}
/* support no starting clear code */
avail = clear + 2;
oldcode = (-1);
len = 0;
for(;;) {
if (valid_bits < codesize) {
if (len == 0) {
len = gif_get8(s); /* start new block */
if (len == 0) {
return SIXEL_OK;
}
}
--len;
bits |= (signed int) gif_get8(s) << valid_bits;
valid_bits += 8;
} else {
code = bits & codemask;
bits >>= codesize;
valid_bits -= codesize;
/* @OPTIMIZE: is there some way we can accelerate the non-clear path? */
if (code == clear) { /* clear code */
codesize = lzw_cs + 1;
codemask = (1 << codesize) - 1;
avail = clear + 2;
oldcode = -1;
} else if (code == clear + 1) { /* end of stream code */
s->img_buffer += len;
while ((len = gif_get8(s)) > 0) {
s->img_buffer += len;
}
return SIXEL_OK;
} else if (code <= avail) {
if (oldcode >= 0) {
if (avail < (1 << gif_lzw_max_code_size)) {
p = &g->codes[avail++];
p->prefix = (signed short) oldcode;
p->first = g->codes[oldcode].first;
p->suffix = (code == avail) ? p->first : g->codes[code].first;
}
} else if (code == avail) {
sixel_helper_set_additional_message(
"corrupt GIF (reason: illegal code in raster).");
status = SIXEL_RUNTIME_ERROR;
goto end;
}
gif_out_code(g, (unsigned short) code);
if ((avail & codemask) == 0 && avail <= 0x0FFF) {
codesize++;
codemask = (1 << codesize) - 1;
}
oldcode = code;
} else {
sixel_helper_set_additional_message(
"corrupt GIF (reason: illegal code in raster).");
status = SIXEL_RUNTIME_ERROR;
goto end;
}
}
}
status = SIXEL_OK;
end:
return status;
}
/* this function is ported from stb_image.h */
static SIXELSTATUS
gif_load_next(
gif_context_t /* in */ *s,
gif_t /* in */ *g,
unsigned char /* in */ *bgcolor
)
{
SIXELSTATUS status = SIXEL_FALSE;
unsigned char buffer[256];
unsigned char c;
int x;
int y;
int w;
int h;
int len;
for (;;) {
switch ((c = gif_get8(s))) {
case 0x2C: /* Image Separator (1 byte) */
x = gif_get16le(s); /* Image Left Position (2 bytes)*/
y = gif_get16le(s); /* Image Top Position (2 bytes) */
w = gif_get16le(s); /* Image Width (2 bytes) */
h = gif_get16le(s); /* Image Height (2 bytes) */
if (x >= g->w || y >= g->h || x + w > g->w || y + h > g->h) {
sixel_helper_set_additional_message(
"corrupt GIF (reason: bad Image Separator).");
status = SIXEL_RUNTIME_ERROR;
goto end;
}
g->line_size = g->w;
g->start_x = x;
g->start_y = y;
g->max_x = g->start_x + w;
g->max_y = g->start_y + h;
g->cur_x = g->start_x;
g->cur_y = g->start_y;
g->actual_width = g->start_x;
g->actual_height = g->start_y;
/* Packed Fields (1 byte)
* +-+-+-+--+---+
* | | | | | |
* +-+-+-+--+---+
* | | | | |
* | | | | +- Size of Local Color Table (3 bits)
* | | | +- Reserved (2 bits)
* | | +- Sort Flag (1 bit)
* | +- Interlace Flag (1 bit)
* +- Local Color Table Flag (1 bit)
*/
g->lflags = gif_get8(s);
/* Interlace Flag */
if (g->lflags & 0x40) {
g->step = 8; /* first interlaced spacing */
g->parse = 3;
} else {
g->step = 1;
g->parse = 0;
}
/* Local Color Table Flag */
if (g->lflags & 0x80) {
gif_parse_colortable(s,
g->lpal,
2 << (g->lflags & 7));
g->color_table = (unsigned char *) g->lpal;
} else if (g->flags & 0x80) {
if (g->transparent >= 0 && (g->eflags & 0x01)) {
if (bgcolor) {
g->pal[g->transparent][0] = bgcolor[2];
g->pal[g->transparent][1] = bgcolor[1];
g->pal[g->transparent][2] = bgcolor[0];
}
}
g->color_table = (unsigned char *)g->pal;
} else {
sixel_helper_set_additional_message(
"corrupt GIF (reason: missing color table).");
status = SIXEL_RUNTIME_ERROR;
goto end;
}
status = gif_process_raster(s, g);
if (SIXEL_FAILED(status)) {
goto end;
}
goto end;
case 0x21: /* Comment Extension. */
switch (gif_get8(s)) {
case 0x01: /* Plain Text Extension */
break;
case 0x21: /* Comment Extension */
break;
case 0xF9: /* Graphic Control Extension */
len = gif_get8(s); /* block size */
if (len == 4) {
g->eflags = gif_get8(s);
g->delay = gif_get16le(s); /* delay */
g->transparent = gif_get8(s);
} else {
if (s->img_buffer + len > s->img_buffer_end) {
status = SIXEL_RUNTIME_ERROR;
goto end;
}
s->img_buffer += len;
break;
}
break;
case 0xFF: /* Application Extension */
len = gif_get8(s); /* block size */
if (s->img_buffer + len > s->img_buffer_end) {
status = SIXEL_RUNTIME_ERROR;
goto end;
}
memcpy(buffer, s->img_buffer, (size_t)len);
s->img_buffer += len;
buffer[len] = 0;
if (len == 11 && strcmp((char *)buffer, "NETSCAPE2.0") == 0) {
if (gif_get8(s) == 0x03) {
/* loop count */
switch (gif_get8(s)) {
case 0x00:
g->loop_count = 1;
break;
case 0x01:
g->loop_count = gif_get16le(s);
break;
default:
g->loop_count = 1;
break;
}
}
}
break;
default:
len = gif_get8(s); /* block size */
if (s->img_buffer + len > s->img_buffer_end) {
status = SIXEL_RUNTIME_ERROR;
goto end;
}
memcpy(buffer, s->img_buffer, (size_t)len);
s->img_buffer += len;
break;
}
if ((c = gif_get8(s)) != 0x00) {
sprintf((char *)buffer, "missing valid block terminator (unknown code %02x).", c);
sixel_helper_set_additional_message((char *)buffer);
status = SIXEL_RUNTIME_ERROR;
goto end;
}
break;
case 0x3B: /* gif stream termination code */
g->is_terminated = 1;
status = SIXEL_OK;
goto end;
default:
sprintf((char *)buffer, "corrupt GIF (reason: unknown code %02x).", c);
sixel_helper_set_additional_message((char *)buffer);
status = SIXEL_RUNTIME_ERROR;
goto end;
}
}
status = SIXEL_OK;
end:
return status;
}
typedef union _fn_pointer {
sixel_load_image_function fn;
void * p;
} fn_pointer;
SIXELSTATUS
load_gif(
unsigned char /* in */ *buffer,
int /* in */ size,
unsigned char /* in */ *bgcolor,
int /* in */ reqcolors,
int /* in */ fuse_palette,
int /* in */ fstatic,
int /* in */ loop_control,
void /* in */ *fn_load, /* callback */
void /* in */ *context, /* private data for callback */
sixel_allocator_t /* in */ *allocator) /* allocator object */
{
gif_context_t s;
gif_t g;
SIXELSTATUS status = SIXEL_FALSE;
sixel_frame_t *frame;
fn_pointer fnp;
char message[256];
fnp.p = fn_load;
status = sixel_frame_new(&frame, allocator);
if (SIXEL_FAILED(status)) {
goto end;
}
s.img_buffer = s.img_buffer_original = (unsigned char *)buffer;
s.img_buffer_end = (unsigned char *)buffer + size;
memset(&g, 0, sizeof(g));
g.delay = SIXEL_DEFALUT_GIF_DELAY;
status = gif_load_header(&s, &g);
if (status != SIXEL_OK) {
goto end;
}
g.out = (unsigned char *)sixel_allocator_malloc(allocator, (size_t)g.w * (size_t)g.h);
if (g.out == NULL) {
sprintf(message,
"load_gif: sixel_allocator_malloc() failed. size=%zu.",
(size_t)g.max_x * (size_t)g.max_y);
sixel_helper_set_additional_message(message);
status = SIXEL_BAD_ALLOCATION;
goto end;
}
frame->loop_count = 0;
for (;;) { /* per loop */
frame->frame_no = 0;
s.img_buffer = s.img_buffer_original;
status = gif_load_header(&s, &g);
if (status != SIXEL_OK) {
goto end;
}
g.is_terminated = 0;
for (;;) { /* per frame */
status = gif_load_next(&s, &g, bgcolor);
if (status != SIXEL_OK) {
goto end;
}
if (g.is_terminated) {
break;
}
frame->width = g.actual_width;
frame->height = g.actual_height;
status = gif_init_frame(frame, &g, bgcolor, reqcolors, fuse_palette);
if (status != SIXEL_OK) {
goto end;
}
status = fnp.fn(frame, context);
if (status != SIXEL_OK) {
goto end;
}
if (fstatic) {
goto end;
}
++frame->frame_no;
}
++frame->loop_count;
if (g.loop_count < 0) {
break;
}
if (loop_control == SIXEL_LOOP_DISABLE || frame->frame_no == 1) {
break;
}
if (loop_control == SIXEL_LOOP_AUTO) {
if (frame->loop_count == g.loop_count) {
break;
}
}
}
end:
sixel_allocator_free(frame->allocator, g.out);
sixel_frame_unref(frame);
return status;
}
#if HAVE_TESTS
static int
test1(void)
{
int nret = EXIT_FAILURE;
nret = EXIT_SUCCESS;
return nret;
}
SIXELAPI int
sixel_fromgif_tests_main(void)
{
int nret = EXIT_FAILURE;
size_t i;
typedef int (* testcase)(void);
static testcase const testcases[] = {
test1,
};
for (i = 0; i < sizeof(testcases) / sizeof(testcase); ++i) {
nret = testcases[i]();
if (nret != EXIT_SUCCESS) {
goto error;
}
}
nret = EXIT_SUCCESS;
error:
return nret;
}
#endif /* HAVE_TESTS */
/* emacs Local Variables: */
/* emacs mode: c */
/* emacs tab-width: 4 */
/* emacs indent-tabs-mode: nil */
/* emacs c-basic-offset: 4 */
/* emacs End: */
/* vim: set expandtab ts=4 sts=4 sw=4 : */
/* EOF */