-
Notifications
You must be signed in to change notification settings - Fork 1
/
gif.c
executable file
·804 lines (689 loc) · 24.4 KB
/
gif.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#ifndef _TEST_
/* 包含头文件 */
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "lzw.h"
#include "gif.h"
/* ++ gif fio context & driver ++ */
typedef struct {
BYTE bitbuf;
int bitflag;
void *basefp;
FIO *basefio;
int counter;
} GIFFIOCTXT;
static int _gif_fio_getc(void *fp)
{
GIFFIOCTXT *context = (GIFFIOCTXT*)fp;
if (context->counter == 0) {
context->counter = (context->basefio->getc)(context->basefp);
if (context->counter == EOF || context->counter == 0) return EOF;
}
context->counter--;
return (context->basefio->getc)(context->basefp);
}
static int _gif_fio_putc(int c, void *fp)
{
GIFFIOCTXT *context = (GIFFIOCTXT*)fp;
if (context->counter == 0) {
if (EOF == (context->basefio->putc)(255, context->basefp)) return EOF;
}
if (++context->counter == 255) context->counter = 0;
return (context->basefio->putc)(c, context->basefp);
}
static int _gif_fio_flush(void *fp)
{
GIFFIOCTXT *context = (GIFFIOCTXT*)fp;
if (context->counter != 0) {
if (EOF == (context->basefio->seek)(context->basefp, -context->counter-1, SEEK_CUR)) return EOF;
if (EOF == (context->basefio->putc)(context->counter, context->basefp)) return EOF;
if (EOF == (context->basefio->seek)(context->basefp, context->counter, SEEK_CUR)) return EOF;
}
if (EOF == (context->basefio->putc)(0, context->basefp)) return EOF;
return (context->basefio->flush)(context->basefp);
}
static void gif_fio_init(GIFFIOCTXT *ctxt, FIO *fio, void *basefp, FIO *basefio) {
ctxt->bitbuf = 0;
ctxt->bitflag = 0;
ctxt->basefp = basefp;
ctxt->basefio = basefio;
ctxt->counter = 0;
memcpy(fio, basefio, sizeof(FIO));
fio->getc = _gif_fio_getc;
fio->putc = _gif_fio_putc;
fio->flush = _gif_fio_flush;
}
static void gif_lzw_decode(LZWCODEC *lzw, int codesize, BYTE *buf, unsigned bufsize, void *fp, FIO *fio)
{
void *memfp;
GIFFIOCTXT giffp;
FIO gifio;
memfp = MEM_FIO.open((const char*)buf, (const char*)bufsize);
gif_fio_init(&giffp, &gifio, fp, fio);
lzw->LZW_ROOT_SIZE = codesize;
lzw->LZW_CODE_SIZE_OUT = 8;
lzw->LZW_CODE_SIZE_MIN = codesize + 1;
lzw->LZW_ROOT_NUM = 1 << codesize;
lzw->LZW_CLEAR_CODE = 1 << codesize;
lzw->LZW_END_CODE = (1 << codesize) + 1;
lzwdecode(lzw, memfp, &MEM_FIO, &giffp, &gifio);
MEM_FIO.close(memfp);
}
static void gif_lzw_encode(LZWCODEC *lzw, int codesize, void *fp, FIO *fio, BYTE *buf, unsigned bufsize)
{
void *memfp;
GIFFIOCTXT giffp;
FIO gifio;
memfp = MEM_FIO.open((const char*)buf, (const char*)bufsize);
gif_fio_init(&giffp, &gifio, fp, fio);
lzw->LZW_ROOT_SIZE = codesize;
lzw->LZW_CODE_SIZE_OUT = 8;
lzw->LZW_CODE_SIZE_MIN = codesize + 1;
lzw->LZW_ROOT_NUM = 1 << codesize;
lzw->LZW_CLEAR_CODE = 1 << codesize;
lzw->LZW_END_CODE = (1 << codesize) + 1;
lzwencode(lzw, &giffp, &gifio, memfp, &MEM_FIO);
gifio.flush(&giffp);
MEM_FIO.close(memfp);
}
/* -- gif fio context & driver -- */
/* 常量定义 */
#define GIF_IMG_SPRT ','
#define GIF_EXT_INTR '!'
#define GIF_TEXT_EXT 0x01
#define GIF_CTRL_EXT 0xf9
#define GIF_APP_EXT 0xff
#define GIF_CMT_EXT 0xfe
/* 类型定义 */
/* + GIF file format structs */
/* GIF sub-block */
#pragma pack(1)
typedef struct {
BYTE block_size;
BYTE *pblock_data;
} SUBBLOCK;
/* GIF file header */
typedef struct {
BYTE signature[3];
BYTE version[3];
} GIFHEADER;
/* GIF file logic screen descriptor */
typedef struct {
WORD logic_screen_width;
WORD logic_screen_height;
BYTE global_color_table_size : 3;
BYTE sort_flag : 1;
BYTE color_resolution : 3;
BYTE global_color_table_flag : 1;
BYTE background_color;
BYTE pixel_aspect_ratio;
} LOGICSCREENDESC;
/* GIF file image descriptor */
typedef struct {
WORD image_left_position;
WORD image_top_position;
WORD image_width;
WORD image_height;
BYTE local_color_table_size : 3;
BYTE reserved : 2;
BYTE sort_flag : 1;
BYTE interlace_flag : 1;
BYTE local_color_table_flag : 1;
} IMAGEDESCRIPTOR;
/* GIF file image data */
typedef struct {
BYTE lzw_min_code_size;
SUBBLOCK *pimage_data;
} GIFIMAGEDATA;
/* GIF file graphic control extension */
typedef struct {
BYTE transparent_color_flag : 1;
BYTE user_input_flag : 1;
BYTE disposal_method : 3;
BYTE reserved : 3;
WORD delay_time;
BYTE transparent_color;
} GRAPHCTRLEXT;
/* GIF file text extension */
typedef struct {
WORD text_grid_left_position;
WORD text_grid_top_position;
WORD text_grid_width;
WORD text_grid_height;
BYTE char_cell_width;
BYTE char_cell_height;
BYTE text_foreground_color;
BYTE text_background_color;
SUBBLOCK *plain_text_data;
} PLAINTEXTEXT;
/* GIF file comment extenstion */
typedef struct {
SUBBLOCK *pcomment_data;
} COMMENTEXT;
/* GIF file app extension */
typedef struct {
BYTE application_identifier[8];
BYTE authentication_code[3];
SUBBLOCK *papplication_data;
} APPLICATIONEXT;
/* GIF file format */
typedef struct {
GIFHEADER gif_header;
LOGICSCREENDESC logic_screen_desc;
BYTE *pglobal_color_table;
BYTE far *pdata;
BYTE trailer;
} GIFFILE;
#pragma pack()
/* - GIF file format structs */
/* GIF codec context */
typedef struct {
int width; /* GIF 动画的宽度 */
int height; /* GIF 动画的高度 */
BYTE bkcolor; /* GIF 动画的背景色 */
BYTE gpaldata[1024]; /* GIF 动画的全局调色板 */
int gpalsize; /* GIF 动画的全局调色板 */
int locpalflag; /* 当前帧是否使用局部调色板 */
void *fp; /* fp */
FIO *fio; /* fio */
LZWCODEC lzwcodec; /* LZW 编解码器 */
long dataoffset; /* GIF 动画数据偏移地址 */
} GIFCONTEXT;
/* 内部函数实现 */
/* 反隔行扫描处理 */
static void gifdeinterlace(BMP *pb)
{
int i;
int srcaddr = 0;
BMP bmp = {0};
bmp.width = pb->width;
bmp.height = pb->height;
bmp.cdepth = pb->cdepth;
if (!createbmp(&bmp)) return;
bmp.stride = pb->stride;
for (i=0; i<bmp.height; i+=8) {
memcpy(bmp.pdata + i * bmp.stride, pb->pdata + srcaddr, pb->stride);
srcaddr += pb->stride;
}
for (i=4; i<bmp.height; i+=8) {
memcpy(bmp.pdata + i * bmp.stride, pb->pdata + srcaddr, pb->stride);
srcaddr += pb->stride;
}
for (i=2; i<bmp.height; i+=4) {
memcpy(bmp.pdata + i * bmp.stride, pb->pdata + srcaddr, pb->stride);
srcaddr += pb->stride;
}
for (i=1; i<bmp.height; i+=2) {
memcpy(bmp.pdata + i * bmp.stride, pb->pdata + srcaddr, pb->stride);
srcaddr += pb->stride;
}
memcpy(pb->pdata, bmp.pdata, bmp.height * bmp.stride);
destroybmp(&bmp);
}
/* decode */
void* gifdecodeinit(void *fp, FIO *fio)
{
GIFCONTEXT *context = NULL;
BOOL failed = FALSE;
GIFHEADER gifheader;
LOGICSCREENDESC logscrdesc;
BYTE palbuf[256*3];
int i;
context = calloc(1, sizeof(GIFCONTEXT));
if (!context) {
failed = TRUE;
goto done;
}
/* init fp and fio */
context->fp = fp;
context->fio = fio;
/* init lzw codec */
context->lzwcodec.LZW_CODE_SIZE_MAX = 12;
if (!initlzwcodec(&(context->lzwcodec))) {
failed = TRUE;
goto done;
}
/* read gif header */
fio->read(&gifheader, sizeof(gifheader), 1, context->fp);
/* check if it is valid gif file */
if ( gifheader.signature[0] != 'G'
|| gifheader.signature[1] != 'I'
|| gifheader.signature[2] != 'F')
{
/* it is not gif file */
log_printf("check gif signature failed !\r\n");
return FALSE;
}
/* read gif logic screen descriptor */
fio->read(&logscrdesc, sizeof(logscrdesc), 1, context->fp);
/* init gif object */
context->width = logscrdesc.logic_screen_width;
context->height = logscrdesc.logic_screen_height;
context->bkcolor = logscrdesc.background_color;
/* load global color table */
if (logscrdesc.global_color_table_flag == 1) {
context->gpalsize = 1 << (logscrdesc.global_color_table_size + 1);
fio->read(palbuf, context->gpalsize * 3, 1, context->fp);
for (i=0; i<context->gpalsize; i++) {
context->gpaldata[i * 4 + 2] = palbuf[i * 3 + 0];
context->gpaldata[i * 4 + 1] = palbuf[i * 3 + 1];
context->gpaldata[i * 4 + 0] = palbuf[i * 3 + 2];
}
}
/* save the image data offset */
context->dataoffset = fio->tell(context->fp);
done:
if (failed) {
log_printf("failed to init gif decoder !\r\n");
gifdecodefree(context);
return NULL;
}
return context;
}
void gifdecodefree(void *ctxt)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
if (context) {
/* close lzw codec */
closelzwcodec(&(context->lzwcodec));
/* free fp fo context */
context->fio->close(context->fp);
free(context);
}
}
BOOL gifdecodeframe(void *ctxt, int *type, BMP *pb, int *xpos, int *ypos, GIF_CTRL *ctrl, GIF_TEXT *text)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
FIO *fio = context->fio;
BYTE curbyte;
int palsize;
BYTE codesize;
IMAGEDESCRIPTOR imagedesc = {0};
GRAPHCTRLEXT gctrlext = {0};
PLAINTEXTEXT gtextext = {0};
BYTE pallocal [256*4];
BYTE palbuffer[256*3];
int i;
*type = GIF_FRAME_TYPE_UNKNOWN;
curbyte = (fio->getc)(context->fp);
switch (curbyte) {
case GIF_IMG_SPRT:
/* set type */
*type = GIF_FRAME_TYPE_IMAGE;
/* read image descriptor */
fio->read(&imagedesc, sizeof(IMAGEDESCRIPTOR), 1, context->fp);
/* get bmp's display position */
*xpos = imagedesc.image_left_position;
*ypos = imagedesc.image_top_position;
/* create a bmp to load image data */
if ( pb->width != imagedesc.image_width
|| pb->height != imagedesc.image_height)
{
destroybmp(pb);
pb->width = imagedesc.image_width;
pb->height = imagedesc.image_height;
pb->cdepth = 8;
if (!createbmp(pb)) return FALSE;
else pb->stride = pb->width; /* not align to 4-bytes */
}
/* load image's palette data */
context->locpalflag = imagedesc.local_color_table_flag;
if (imagedesc.local_color_table_flag) {
palsize = 1 << (imagedesc.local_color_table_size + 1);
fio->read(palbuffer, palsize * 3, 1, context->fp);
for (i=0; i<palsize; i++) {
pallocal[i * 4 + 2] = palbuffer[i * 3 + 0];
pallocal[i * 4 + 1] = palbuffer[i * 3 + 1];
pallocal[i * 4 + 0] = palbuffer[i * 3 + 2];
}
setbmppal(pb, 0, palsize, pallocal);
} else {
setbmppal(pb, 0, context->gpalsize, context->gpaldata);
}
/* get image's lzw min code size */
codesize = (fio->getc)(context->fp);
gif_lzw_decode(&context->lzwcodec, codesize, pb->pdata, pb->stride * pb->height, context->fp, context->fio);
/* deinterlace gif image */
if (imagedesc.interlace_flag) gifdeinterlace(pb);
break;
case GIF_EXT_INTR:
curbyte = (fio->getc)(context->fp);
switch (curbyte) {
case GIF_CTRL_EXT:
/* load gif control extension */
curbyte = (fio->getc)(context->fp);
if (curbyte != sizeof(GRAPHCTRLEXT)) break;
fio->read(&gctrlext, sizeof(GRAPHCTRLEXT), 1, context->fp);
/* fill ctrl block */
*type = GIF_FRAME_TYPE_CTRL;
ctrl->disposal = gctrlext.disposal_method;
ctrl->inputflag = gctrlext.user_input_flag;
ctrl->transflag = gctrlext.transparent_color_flag;
ctrl->transcolor = gctrlext.transparent_color;
ctrl->delay = gctrlext.delay_time * 10;
curbyte = (fio->getc)(context->fp);
if (curbyte != 0) {
log_printf("parser GIF_CTRL_EXT failed, can't get block terminator !\r\n");
break;
}
break;
case GIF_TEXT_EXT:
/* load gif text extension */
curbyte = (fio->getc)(context->fp);
if (curbyte != sizeof(PLAINTEXTEXT)) break;
fio->read(>extext, sizeof(PLAINTEXTEXT), 1, context->fp);
curbyte = (fio->getc)(context->fp);
/* fill text block */
*type = GIF_FRAME_TYPE_TEXT;
*xpos = gtextext.text_grid_left_position;
*ypos = gtextext.text_grid_top_position;
text->width = gtextext.text_grid_width;
text->height = gtextext.text_grid_height;
text->charw = gtextext.char_cell_width;
text->charh = gtextext.char_cell_height;
text->forecolor = gtextext.text_foreground_color;
text->backcolor = gtextext.text_background_color;
/* ++ we only support 256 bytes text length ++ */
curbyte = (fio->getc)(context->fp);
if (curbyte == 0) break;
fio->read(text->text, curbyte, 1, context->fp);
for (;;) {
curbyte = (fio->getc)(context->fp);
if (curbyte == 0) break;
fio->seek(context->fp, curbyte, SEEK_CUR);
}
/* -- we only support 256 bytes text length -- */
break;
/* ++ not support now ++ */
case GIF_CMT_EXT:
case GIF_APP_EXT:
/* log_printf("unsupported gif ext_intr !\r\n"); */
for (;;) {
curbyte = (fio->getc)(context->fp);
if (curbyte == 0) break;
fio->seek(context->fp, curbyte, SEEK_CUR);
}
break;
}
break;
/* -- not support now -- */
case ';':
fio->seek(context->fp, context->dataoffset, SEEK_SET);
*type = GIF_FRAME_TYPE_END; /* type */
break;
}
return TRUE;
}
/* encode */
void* gifencodeinit(void *fp, FIO *fio, int width, int height, BYTE bkcolor, BYTE *pal, int size)
{
GIFCONTEXT *context = NULL;
BOOL failed = FALSE;
GIFHEADER gifheader;
LOGICSCREENDESC logscrdesc;
BYTE palbuf[256*3];
int i;
context = calloc(1, sizeof(GIFCONTEXT));
if (!context) {
failed = TRUE;
goto done;
}
/* init fp and fio */
context->fp = fp;
context->fio = fio;
/* init lzw codec */
context->lzwcodec.LZW_CODE_SIZE_MAX = 12;
if (!initlzwcodec(&(context->lzwcodec))) {
failed = TRUE;
goto done;
}
/* set gif global palette */
if (pal) {
memcpy(context->gpaldata, pal, size * 4);
}
context->gpalsize = size;
/* width & height & bkcolor */
context->width = width;
context->height = height;
context->bkcolor = bkcolor;
/* fill gif file header */
gifheader.signature[0] = 'G';
gifheader.signature[1] = 'I';
gifheader.signature[2] = 'F';
gifheader.version[0] = '8';
gifheader.version[1] = '9';
gifheader.version[2] = 'a';
/* write gif header */
fio->write(&gifheader, sizeof(gifheader), 1, context->fp);
/* fill gif logic screen desc */
logscrdesc.logic_screen_width = context->width;
logscrdesc.logic_screen_height = context->height;
logscrdesc.background_color = context->bkcolor;
logscrdesc.color_resolution = 7;
logscrdesc.pixel_aspect_ratio = 0;
logscrdesc.sort_flag = 0;
if (context->gpalsize > 0) {
logscrdesc.global_color_table_flag = 1;
logscrdesc.global_color_table_size = 7;
}
/* write gif logic screen descriptor */
fio->write(&logscrdesc, sizeof(logscrdesc), 1, context->fp);
/* if there is global palette write it to file */
if (context->gpalsize > 0) {
for (i=0; i<context->gpalsize; i++) {
palbuf[i * 3 + 0] = context->gpaldata[i * 4 + 2];
palbuf[i * 3 + 1] = context->gpaldata[i * 4 + 1];
palbuf[i * 3 + 2] = context->gpaldata[i * 4 + 0];
}
fio->write(palbuf, 256 * 3, 1, context->fp);
}
done:
if (failed) {
log_printf("failed to init gif decoder !\r\n");
gifdecodefree(context);
return NULL;
}
return context;
}
void gifencodefree(void *ctxt)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
if (context) {
/* close lzw codec */
closelzwcodec(&(context->lzwcodec));
/* free fp fo context */
context->fio->close(context->fp);
free(context);
}
}
BOOL gifencodeframe(void *ctxt, int type, BMP *pb, int xpos, int ypos, GIF_CTRL *ctrl, GIF_TEXT *text)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
FIO *fio = context->fio;
GRAPHCTRLEXT gifctrlext = {0};
IMAGEDESCRIPTOR imagedesc = {0};
BYTE palbuf[256*3];
int codesize = 8;
int i;
switch (type) {
case GIF_FRAME_TYPE_IMAGE:
/* first write Image Separator to file */
(fio->putc)(GIF_IMG_SPRT, context->fp);
/* fill imagedesc and write it to file */
imagedesc.image_left_position = xpos;
imagedesc.image_top_position = ypos;
imagedesc.image_width = pb->width;
imagedesc.image_height = pb->height;
if (context->locpalflag) {
imagedesc.local_color_table_flag = 1;
imagedesc.local_color_table_size = 7;
}
fio->write(&imagedesc, sizeof(imagedesc), 1, context->fp);
/* if there is local palette write it to file */
if (context->locpalflag) {
for (i=0; i<context->gpalsize; i++) {
palbuf[i * 3 + 0] = pb->ppal[i * 4 + 2];
palbuf[i * 3 + 1] = pb->ppal[i * 4 + 1];
palbuf[i * 3 + 2] = pb->ppal[i * 4 + 0];
}
fio->write(palbuf, 256 * 3, 1, context->fp);
}
/* write lzw minimum code size to file */
(fio->putc)(8, context->fp);
gif_lzw_encode(&context->lzwcodec, codesize, context->fp, context->fio, pb->pdata, pb->stride * pb->height);
break;
case GIF_FRAME_TYPE_CTRL:
/* write the Extension Introducer and Graphic Control Label to file */
(fio->putc)(GIF_EXT_INTR, context->fp);
(fio->putc)(GIF_CTRL_EXT, context->fp);
/* write block size to file */
(fio->putc)(sizeof(gifctrlext), context->fp);
/* fill gifctrlext and write it to file */
gifctrlext.disposal_method = ctrl->disposal;
gifctrlext.user_input_flag = ctrl->inputflag;
gifctrlext.transparent_color_flag = ctrl->transflag;
gifctrlext.transparent_color = ctrl->transcolor;
gifctrlext.delay_time = ctrl->delay / 10;
fio->write(&gifctrlext, sizeof(gifctrlext), 1, context->fp);
/* write block terminator to file */
(fio->putc)(0, context->fp);
break;
case GIF_FRAME_TYPE_END:
/* write the gif file Trailer */
(fio->putc)(';', context->fp);
break;
case GIF_FRAME_TYPE_TEXT:
/* todo... */
DO_USE_VAR(text);
break;
}
return TRUE;
}
void gifgetimageinfo(void *ctxt, int *width, int *height, BYTE *bkcolor)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
*width = context->width;
*height = context->height;
*bkcolor = context->bkcolor;
}
void gifgetglobalpal(void *ctxt, BYTE *pal, int *size)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
*size = context->gpalsize;
memcpy(pal, context->gpaldata, *size * 4);
}
void gifgetlocalpal(void *ctxt, int *flag)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
*flag = context->locpalflag;
}
void gifsetlocalpal(void *ctxt, int flag)
{
GIFCONTEXT *context = (GIFCONTEXT*)ctxt;
context->locpalflag = flag;
}
#else
#include "screen.h"
#include "gif.h"
#include "bitblt.h"
#include "log.h"
static void *g_gdctxt = NULL;
static void *g_gectxt = NULL;
static BMP g_mybmp = {0};
static GIF_CTRL g_ctrl = {0};
static GIF_TEXT g_text = {0};
LRESULT CALLBACK MyWndProc(
HWND hwnd, /* handle to window */
UINT uMsg, /* message identifier */
WPARAM wParam, /* first message parameter */
LPARAM lParam /* second message parameter */
)
{
int type;
int xpos;
int ypos;
switch (uMsg) {
case WM_TIMER:
while (1) {
gifdecodeframe(g_gdctxt, &type , &g_mybmp, &xpos, &ypos, &g_ctrl, &g_text);
switch (type) {
case GIF_FRAME_TYPE_IMAGE:
/*
log_printf("GIF_FRAME_TYPE_IMAGE %d, %d\r\n", xpos, ypos);
log_printf("\r\n");
*/
putbmp(&SCREEN, xpos, ypos, &g_mybmp, FS_AUTO_LOCK, 0, 0, NULL);
break;
case GIF_FRAME_TYPE_CTRL:
/*
log_printf("GIF_FRAME_TYPE_CTRL\r\n");
log_printf("g_ctrl.disposal = %d\r\n", g_ctrl.disposal );
log_printf("g_ctrl.inputflag = %d\r\n", g_ctrl.inputflag );
log_printf("g_ctrl.transflag = %d\r\n", g_ctrl.transflag );
log_printf("g_ctrl.transcolor = %d\r\n", g_ctrl.transcolor);
log_printf("g_ctrl.delay = %d\r\n", g_ctrl.delay );
log_printf("\r\n");
*/
SetTimer(hwnd, 1, g_ctrl.delay, NULL);
break;
case GIF_FRAME_TYPE_TEXT:
/*
log_printf("GIF_FRAME_TYPE_TEXT %d, %d\r\n", xpos, ypos);
log_printf("\r\n");
*/
break;
}
if (type == GIF_FRAME_TYPE_CTRL) break;
}
return 0;
default:
return DEF_SCREEN_WNDPROC(hwnd, uMsg, wParam, lParam);
}
}
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow)
{
void *fpdec = NULL;
void *fpenc = NULL;
int width, height;
BYTE bkcolor;
BYTE globalpaldata[256*4];
int globalpalsize = 0;
int localpalflag = 0;
int xpos, ypos;
int type;
log_init("gif.log");
if (*lpszCmdLine == 0) {
lpszCmdLine = "res/cs.gif";
}
fpdec = DEF_FIO.open(lpszCmdLine , "rb");
fpenc = DEF_FIO.open("output.gif", "wb");
g_gdctxt = gifdecodeinit(fpdec , &DEF_FIO);
gifgetimageinfo(g_gdctxt, &width, &height, &bkcolor);
gifgetglobalpal(g_gdctxt, globalpaldata, &globalpalsize);
g_gectxt = gifencodeinit(fpenc, &DEF_FIO, width, height, bkcolor, globalpaldata, globalpalsize);
while (1) {
gifdecodeframe(g_gdctxt, &type, &g_mybmp, &xpos, &ypos, &g_ctrl, &g_text);
gifgetlocalpal(g_gdctxt, &localpalflag);
gifsetlocalpal(g_gectxt, localpalflag);
gifencodeframe(g_gectxt, type, &g_mybmp, xpos, ypos, &g_ctrl, &g_text);
if (type == GIF_FRAME_TYPE_END) break;
}
RGE_WIN_INIT(hInst);
SCREEN.width = 640;
SCREEN.height = 480;
SCREEN.cdepth = 32;
createbmp(&SCREEN);
SetWindowLong(GET_SCREEN_HWND(), GWL_WNDPROC, (long)MyWndProc);
SetTimer(GET_SCREEN_HWND(), 1, 100, NULL);
RGE_MSG_LOOP();
KillTimer(GET_SCREEN_HWND(), 1);
destroybmp(&SCREEN);
gifdecodefree(g_gdctxt);
gifdecodefree(g_gectxt);
DEF_FIO.close(fpdec );
DEF_FIO.close(fpenc);
log_done();
return 0;
}
#endif