-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_gif.c
2083 lines (1744 loc) · 71.8 KB
/
egi_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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This module is a wrapper of GIFLIB routines and functions, based on
giflib-5.2.1.
The GIFLIB distribution is Copyright (c) 1997 Eric S. Raymond
SPDX-License-Identifier: MIT
Note:
1. FB back buffers must be enabled to run EGI_GIF.
Midas Zhou
[email protected](Not in use since 2022_03_01)
------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "egi_gif.h"
#include "egi_common.h"
#define GIF_MAX_CHECKSIZE 32 /* in Mbytes, 1024*1024*GIF_MAX_CHECKSIZE bytes,
* For checking GIF data size,estimated. */
/* static functions */
static void PrintGifError(int ErrorCode);
static void egi_gif_FreeSavedImages(SavedImage **psimg, int ImageCount);
inline static void egi_gif_rasterWriteFB( FBDEV *fbdev, EGI_GIF *egif,
bool DirectFB_ON, int Disposal_Mode,
int xp, int yp, int xw, int yw, int winw, int winh,
ColorMapObject *ColorMap, GifByteType *buffer,
int trans_color, int User_TransColor,
bool ImgTransp_ON );
static void *egi_gif_threadDisplay(void *argv);
/*****************************************************************************
Same as fprintf to stderr but with optional print.
******************************************************************************/
#if 0
static void GifQprintf(char *Format, ...)
{
bool GifNoisyPrint = true;
va_list ArgPtr;
va_start(ArgPtr, Format);
if (GifNoisyPrint) {
char Line[128];
(void)vsnprintf(Line, sizeof(Line), Format, ArgPtr);
(void)fputs(Line, stderr);
}
va_end(ArgPtr);
}
#endif
static void PrintGifError(int ErrorCode)
{
const char *Err = GifErrorString(ErrorCode);
if (Err != NULL)
printf("GIF-LIB error: %s.\n", Err);
else
printf("GIF-LIB undefined error %d.\n", ErrorCode);
}
/*------------------------------------------------------------------------------------------
Read a GIF file and display images one by one, Meanwhile, the corresponding ImageCount
is passed to the caller. If Silent_Mode is TRUE, then only ImageCount is passed out,
Finally, ImageCount gets to the total number of images in the GIF file.
The image is displayed in the center of the screen.
Note:
1. This function is for big size GIF file, it reads and displays frame by frame.
For small size GIF file, just call egi_gif_slurpFile() to load to EGI_GIF first,
then call egi_gif_display(). OR first call egi_gifdata_readFile() to read out
EGI_GIF_DATA.
2. The passed ImageCount starts from 1, and ends with the total number of images.
3. For a GIF with transparent area, you shall initialize FB back groud buffer before calling
the function. One way is to run fb_copy_FBbuffer(&gv_fb_dev, 0, 1) first.
4. Some broken gif file may have invalid offxy and block size, when any of the block is found to
be out of the canvas, the function will return.
TODO:
1. If the GIF file includes invalid block image, whose range is out of canvas, then it fail and return.
If want to display it anyway, call egi_gif_slurpFile() and egi_gif_displayGifCtxt().
@fpath: File path
@Silent_Mode: TRUE: Do NOT display or delay, just read frame by frame and pass
image count to the caller.
FALE: Do display and delay.
@ImgTransp_ON: (User define)
Usually to be set as TRUE.
Suggest to turn OFF only when no transparency setting in the GIF file,
to show SBackGroundColor.
@ImageCount: To pass out number of images that have been displayed/parsed in real time.
If NULL, ignore.
@nloop: loop times for each function call:
<=0 : loop displaying GIF frames forever
>0 : nloop times
@sigstop: Quit if Ture.
If NULL, ignore.
Check every time before get RecordType.
( --- Basic Procedure --- )
1. Open gif file, get canvas size and global OR local color map pointer.
2. Read RecordType by DGifGetRecordType(GifFile, &RecordType), update GifFile->Image.ColorMap if local colormap applys.
3. Switch(RecordType)
|
|_ UNDEFINED
|_ SCREEN_DESC_RECORD
|
|_ IMAGE_DESC_RECORD ----->>> [[[ --- Get image data and display it --- ]]]
|
|_ EXTENSION_RECORD ----->>>|
| |_ CONTINUE_EXT_FUNC
| |_ COMMENT_EXT_FUNC
| |_ GRAPHICS_EXT_FUNC (GIF89) ----->>> Get DisposalMode,TranspColor and DelayTime.
| |_ PLAINTEXT_EXT_FUNC
| |_ APPLICATION_EXT_FUNC (GIF89)
|
|_ TERMINATE_RECORD ----->>> END
4. Go to 2 and Continue
!!! Note: Usually EXTENSION_RECORD type with RAPHICS_EXT_FUNC code comes first, then the IMAGE_DESC_RECORD type.
Return:
0 OK
<0 Fails
>0 Final DGifCloseFile() fails
-------------------------------------------------------------------------------------------*/
int egi_gif_playFile(const char *fpath, bool Silent_Mode, bool ImgTransp_ON, int *ImageCount, int nloop , bool *sigstop)
{
int Error=0, Error2=0;
int i, j, Size;
int k;
int SWidth=0, SHeight=0; /* screen(gif canvas) width and height, defined in gif file */
//int pos=0;
int spos=0;
int offx=0, offy=0; /* gif block image width and height, defined in gif file */
int BWidth=0, BHeight=0; /* image block size */
int Row=0, Col=0; /* init. as image block offset relative to screen/canvvas */
int ExtCode;
int Count; /* Count: for test */
int DelayMs=0; /* delay time in ms */
int DelayFact; /* adjust delay time according to block image size */
EGI_IMGBUF *Simgbuf=NULL;
FBDEV *fbdev=&gv_fb_dev;
int xres=fbdev->pos_xres;
int yres=fbdev->pos_yres;
GifFileType *GifFile=NULL;
GifRecordType RecordType;
GifByteType *Extension=NULL;
GifRowType *ScreenBuffer=NULL; /* typedef unsigned char *GifRowType */
GifRowType GifRow;
int
InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */
int ImageNum = 0;
ColorMapObject *ColorMap=NULL;
GifColorType *ColorMapEntry=NULL;
GraphicsControlBlock gcb;
// bool Is_LocalColorMap=false; /* TRUE If color map is image color map, NOT global screen color map defined in gif file */
bool DirectFB_ON=false; /* With or without FB buffer */
int Disposal_Mode=0; /* Defined in gif file:
0. No disposal specified
1. Leave image in place
2. Set area to background color(image)
3. Restore to previous content
*/
int trans_color=-1; /* Palette index for transparency, -1 if none, or NO_TRANSPARENT_COLOR
* Defined in gif file. */
int User_TransColor=-1; /* -1, User defined palette index for the transparency */
// bool Bkg_Transp=false; /* If true, make backgroud transparent. User define. */
int bkg_color=0; /* Back ground color index, defined in gif file, default as 0 */
EGI_16BIT_COLOR img_bkcolor=0; /* 16bit back color */
/* Open gif file */
if((GifFile = DGifOpenFileName(fpath, &Error)) == NULL) {
PrintGifError(Error);
return -1;
}
if(GifFile->SHeight == 0 || GifFile->SWidth == 0) {
sprintf("%s: Image width or height is 0\n",__func__);
Error=-2;
goto END_FUNC;
}
printf("%s: --- GIF Params ---\n",__func__);
/* Get GIF version*/
printf(" GIF Verion: %s\n", DGifGetGifVersion(GifFile));
/* Get global color map */
if(GifFile->Image.ColorMap) {
printf("Has LocalColorMap!\n");
// Is_LocalColorMap=true;
ColorMap=GifFile->Image.ColorMap;
//printf(" Use local ColorMap, ColorCount=%d\n", GifFile->Image.ColorMap->ColorCount);
}
else if(GifFile->SColorMap) {
printf("Has NO LocalColorMap!\n");
// Is_LocalColorMap=false;
ColorMap=GifFile->SColorMap;
//printf(" Use global ColorMap Colorcount=%d\n", GifFile->SColorMap->ColorCount);
}
else
printf(" GIF has NO ColorMap predefined!\n");
if(ColorMap) {
printf(" GIF ColorMap.BitsPerPixel=%d\n", ColorMap->BitsPerPixel);
/* check that the background color isn't garbage (SF bug #87) */
if (GifFile->SBackGroundColor < 0 || GifFile->SBackGroundColor >= ColorMap->ColorCount) {
sprintf("%s: Background color out of range for colorMap.\n",__func__);
Error=-3;
goto END_FUNC;
}
/* Get back ground color */
bkg_color=GifFile->SBackGroundColor;
ColorMapEntry = &ColorMap->Colors[bkg_color];
img_bkcolor=COLOR_RGB_TO16BITS( ColorMapEntry->Red,
ColorMapEntry->Green,
ColorMapEntry->Blue );
}
else { /* ColorMap==NULL */
img_bkcolor=0;
}
/* get SWidth and SHeight */
SWidth=GifFile->SWidth;
SHeight=GifFile->SHeight;
printf(" GIF Background color index=%d\n", bkg_color);
printf(" GIF SColorResolution = %d\n", (int)GifFile->SColorResolution);
printf(" GIF SWidthxSHeight=%dx%d ---\n", SWidth, SHeight);
//printf("GIF ImageCount=%d\n", GifFile->ImageCount); /* 0 */
/*** --- Allocate screen as vector of column and rows ---
* Note this screen is device independent - it's the screen defined by the
* GIF file parameters.
*/
if ((ScreenBuffer = (GifRowType *)malloc(GifFile->SHeight * sizeof(GifRowType))) == NULL)
{
printf("%s: Failed to allocate memory required, aborted.",__func__);
return -4;
}
Size = GifFile->SWidth * sizeof(GifPixelType); /* Size in bytes one row.*/
if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) { /* First row. */
printf("%s: Failed to allocate memory required, aborted.",__func__);
Error=-5;
goto END_FUNC;
}
for (i = 0; i < GifFile->SWidth; i++) /* Set its color to BackGround. */
ScreenBuffer[0][i] = GifFile->SBackGroundColor;
for (i = 1; i < GifFile->SHeight; i++) {
/* Allocate the other rows, and set their color to background too: */
if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL) {
printf("%s: Failed to allocate memory required, aborted.",__func__);
Error=-6;
goto END_FUNC;
}
memcpy(ScreenBuffer[i], ScreenBuffer[0], Size);
}
/* reset ImageCount */
ImageNum = 0;
if(ImageCount != NULL)
*ImageCount=0;
/* create Simgbuf */
if(!Silent_Mode) {
Simgbuf=egi_imgbuf_create(SHeight, SWidth, ImgTransp_ON==true ? 0:255, img_bkcolor); //height, width, alpha, color
if(Simgbuf==NULL) {
printf("%s: Fail to create egif->Simgbuf!\n", __func__);
Error=-6;
goto END_FUNC;
}
}
k=0; /* start nloop count */
/* Scan the content of the GIF file and load the image(s) in: */
do {
/* check if sigstop */
if(sigstop != NULL && *sigstop==true)
break;
/* read recordtype */
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
PrintGifError(GifFile->Error);
Error=-6;
goto END_FUNC;
}
printf("\n\nDGifGetRecordType() ----> \n");
/*---------- GIFLIB DEFINED RECORD TYPE ---------
UNDEFINED_RECORD_TYPE,
SCREEN_DESC_RECORD_TYPE,
IMAGE_DESC_RECORD_TYPE, Begin with ','
EXTENSION_RECORD_TYPE, Begin with '!'
TERMINATE_RECORD_TYPE Begin with ';'
------------------------------------------------*/
switch (RecordType) {
case SCREEN_DESC_RECORD_TYPE:
printf("RecordType: SCREEN_DESC_RECORD_TYPE\n");
break;
case IMAGE_DESC_RECORD_TYPE:
printf("RecordType: IMAGE_DESC_RECORD_TYPE\n");
if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
PrintGifError(GifFile->Error);
Error=-7;
goto END_FUNC;
}
Row = GifFile->Image.Top; /* Image Position relative to Screen. */
Col = GifFile->Image.Left;
/* Get offx, offy, Original Row AND Col will increment later!!! */
BWidth = GifFile->Image.Width;
BHeight = GifFile->Image.Height;
/* Assign offx/offy as Col/Row */
offx = Col;
offy = Row;
++ImageNum;
#if 0 /* For TEST: */
printf("GIF ImageCount=%d\n", GifFile->ImageCount);
//GifQprintf("GIF Image %d at (%d, %d) [%dx%d]: ",
printf("GIF Image %d at (%d, %d) [%dx%d]\n",
ImageNum, Col, Row, BWidth, BHeight);
#endif
/* Check block image size and position */
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) {
printf("%s: Image %d is not confined to screen dimension, aborted.\n",__func__, ImageNum);
Error=-8;
goto END_FUNC;
}
/* Get color map, colormap may be updated/changed here! */
if(GifFile->Image.ColorMap) {
ColorMap=GifFile->Image.ColorMap;
//printf("GIF Image ColorCount=%d\n", GifFile->Image.ColorMap->ColorCount);
}
else {
ColorMap=GifFile->SColorMap;
//printf("GIF Global Colorcount=%d\n", GifFile->SColorMap->ColorCount);
}
//printf("GIF ColorMap.BitsPerPixel=%d\n", ColorMap->BitsPerPixel);
/* Need to flush out all image data */
if(Silent_Mode) {
GifByteType *Dummy;
/* need to flush out all the rest of image until an empty block (size 0)
* detected. We use GetCodeNext.
*/
do
if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
return GIF_ERROR;
while (Dummy != NULL) ;
}
if(!Silent_Mode) {
/* Get color (index) data */
if (GifFile->Image.Interlace) {
//printf(" Interlaced image data ...\n");
/* Need to perform 4 passes on the images: */
for (Count = i = 0; i < 4; i++)
for (j = Row + InterlacedOffset[i]; j < Row + BHeight;
j += InterlacedJumps[i] )
{
//GifQprintf("\b\b\b\b%-4d", Count++);
//printf("%-4d", Count++);
if ( DGifGetLine(GifFile, &ScreenBuffer[j][Col],
BWidth) == GIF_ERROR )
{
PrintGifError(GifFile->Error);
Error=-9;
goto END_FUNC;
}
}
}
else {
//printf(" Noninterlaced image data ...\n");
for (i = 0; i < BHeight; i++) {
//GifQprintf("\b\b\b\b%-4d", i);
//printf("%-4d", i);
if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],
BWidth) == GIF_ERROR) {
PrintGifError(GifFile->Error);
Error=-10;
goto END_FUNC;
}
}
}
// printf("\n");
#if 1 /* -----------------------> Display <------------------------- */
//if(!Silent_Mode) {
/* Reset Simgbuf and working FB buffer, for the first block image only */
if( GifFile->ImageCount ==0 && fbdev != NULL ) {
egi_imgbuf_resetColorAlpha( Simgbuf, img_bkcolor, ImgTransp_ON ? 0:255 );
memcpy(fbdev->map_bk, fbdev->map_buff+fbdev->screensize, fbdev->screensize);
}
/* get colormap */
if(GifFile->Image.ColorMap) {
// Is_LocalColorMap=true;
ColorMap=GifFile->Image.ColorMap;
printf("Use local ColorMap, ColorCount=%d\n", GifFile->Image.ColorMap->ColorCount);
}
else {
// Is_LocalColorMap=false;
ColorMap=GifFile->SColorMap;
printf("Use global ColorMap Colorcount=%d\n", GifFile->SColorMap->ColorCount);
}
/* update Simgbuf */
for(i=0; i<BHeight; i++)
{
if( offy+i > SHeight-1 )continue; /* Check for some broken gif file! */
GifRow = ScreenBuffer[offy+i];
/* update block of Simgbuf */
for(j=0; j<BWidth; j++ ) {
if( offx+j > SWidth-1 )continue; /* Check for some broken gif file! */
//pos=i*BWidth+j;
spos=(offy+i)*SWidth+(offx+j);
/* Nontransparent color: set color and set alpha to 255 */
if( trans_color < 0 || trans_color != GifRow[offx+j] )
{
ColorMapEntry = &ColorMap->Colors[GifRow[offx+j]];
Simgbuf->imgbuf[spos]=COLOR_RGB_TO16BITS( ColorMapEntry->Red,
ColorMapEntry->Green,
ColorMapEntry->Blue);
/* set alpha */
Simgbuf->alpha[spos]=255;
}
/* Just after above...! ------- Image Transparency ON -----
* Instead of leaving previous color/alpha unchanged, ImgTransp_ON will reset alpha of trans_color
* to 0 while Disposal_Mode==2, so it will be transparent to the back ground image!
* BUT for Disposal_Mode==1, it only keep previous color/alpha unchanged.
*/
#if 1
if(ImgTransp_ON && Disposal_Mode==2) {
//if( GifRow[offx+j] == trans_color || GifRow[offx+j] == User_TransColor) { /* ???? if trans_color == bkg_color */
if( GifRow[offx+j] == User_TransColor) {
Simgbuf->alpha[spos]=0;
}
}
#endif
/* ELSE : Keep old color and alpha value in Simgbuf->imgbuf */
}
}
/* call window display */
egi_imgbuf_windisplay( Simgbuf, fbdev, -1, /* img, fb, subcolor */
SWidth>xres ? (SWidth-xres)/2:0, /* xp */
SHeight>yres ? (SHeight-yres)/2:0, /* yp */
SWidth>xres ? 0:(xres-SWidth)/2, /* xw */
SHeight>yres ? 0:(yres-SHeight)/2, /* yw */
Simgbuf->width, Simgbuf->height /* winw, winh */
);
/* Refresh FB page if NOT DirectFB_ON */
if(!DirectFB_ON && fbdev != NULL )
fb_page_refresh(fbdev,0);
#if 1 //////////////////////////////////////////////////////
/* Delay */
tm_delayms(DelayMs); /* Need to hold on here, even fddev==NULL */
/* ----- Parse Disposal_Mode: Actions after GIF displaying ----- */
switch(Disposal_Mode)
{
case 0: /* Disposal_Mode 0: No disposal specified */
break;
case 1: /* Disposal_Mode 1: Leave image in place */
break;
case 2: /* Disposal_Mode 2: Set block area to background color/image */
if( !DirectFB_ON )
{
/* update Simgbuf Block*, make current block area transparent! */
for(i=0; i<BHeight; i++) {
if( offy+i > SHeight-1 )continue; /* Check for some broken gif file! */
/* update block of Simgbuf */
for(j=0; j<BWidth; j++ ) {
if( offx+j > SWidth-1 )continue; /* Check for some broken gif file! */
spos=(offy+i)*SWidth+(offx+j);
if(ImgTransp_ON ) /* Make curretn block area transparent! */
Simgbuf->alpha[spos]=0;
else { /* bkcolor is NOT applicable for local colorMap*/
Simgbuf->imgbuf[spos]=img_bkcolor; /* bkcolor: WEGI_16BIT_COLOR */
Simgbuf->alpha[spos]=255;
}
}
}
}
/* Transparency set: Set area to FB background color/image */
if(fbdev != NULL) {
memcpy(fbdev->map_bk, fbdev->map_buff+fbdev->screensize, fbdev->screensize);
}
break;
/* TODO: Disposal_Mode 3: Restore to previous image
* "The mode Restore to Previous is intended to be used in small sections of the graphic
* ... this mode should be used sparingly" --- < Graphics Interchange Format Version 89a >
*/
case 3:
printf("%s: !!! Skip Disposal_Mode 3 !!!\n",__func__);
break;
default:
break;
} /* --- End switch() Disposal_Mode --- */
#endif ///////////////////////////////////////////////////////////////
}
//printf(" --- Finish displaying ImageNum=%d (>=1) --- \n", ImageNum );
//getchar();
#endif /* -----------------------> END Display <------------------------- */
break; /* END case IMAGE_DESC_RECORD_TYPE */
case EXTENSION_RECORD_TYPE:
printf("RecordType: EXTENSION_RECORD_TYPE\n");
/* extension blocks in file: */
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
PrintGifError(GifFile->Error);
Error=-11;
goto END_FUNC;
}
/*--------------------------------------------------------------
* ( FUNC CODE defined in GIFLIB )
* CONTINUE_EXT_FUNC_CODE 0x00 continuation subblock
* COMMENT_EXT_FUNC_CODE 0xfe comment
* GRAPHICS_EXT_FUNC_CODE 0xf9 graphics control (GIF89)
* PLAINTEXT_EXT_FUNC_CODE 0x01 plaintext
* APPLICATION_EXT_FUNC_CODE 0xff application block (GIF89)
--------------------------------------------------------------*/
#if 1 /* Just for TEST */
switch(ExtCode) {
case CONTINUE_EXT_FUNC_CODE:
printf(" ExtCode: CONTINUE_EXT_FUNC_CODE\n");
break;
case COMMENT_EXT_FUNC_CODE:
printf(" ExtCode: COMMENT_EXT_FUNC_CODE\n");
break;
case GRAPHICS_EXT_FUNC_CODE:
printf(" ExtCode: GRAPHICS_EXT_FUNC_CODE\n");
break;
case PLAINTEXT_EXT_FUNC_CODE:
printf(" ExtCode: PLAINTEXT_EXT_FUNC_CODE\n");
break;
case APPLICATION_EXT_FUNC_CODE:
printf(" ExtCode: APPLICATION_EXT_FUNC_CODE\n");
break;
default:
break;
}
#endif
/* --- parse extension code --- */
if (ExtCode == GRAPHICS_EXT_FUNC_CODE) {
if (Extension == NULL) {
printf("Invalid extension block\n");
GifFile->Error = D_GIF_ERR_IMAGE_DEFECT;
PrintGifError(GifFile->Error);
Error=-12;
goto END_FUNC;
}
if (DGifExtensionToGCB(Extension[0], Extension+1, &gcb) == GIF_ERROR) {
PrintGifError(GifFile->Error);
Error=-13;
goto END_FUNC;
}
/* ----- for test ---- */
#if 1
printf("Transparency on: %s\n",
gcb.TransparentColor != -1 ? "yes" : "no");
printf("Transparent Index: %d\n", gcb.TransparentColor);
printf("DelayTime: %d\n", gcb.DelayTime);
printf("DisposalMode: %d\n", gcb.DisposalMode);
#endif
/* Get disposal mode */
Disposal_Mode=gcb.DisposalMode;
/* Get trans_color */
trans_color=gcb.TransparentColor;
/* >200x200 no delay, 0 full delay */
if( BWidth*BHeight > 40000)
DelayFact=0;
else
DelayFact= (200*200-BWidth*BHeight);
/* Get delay time in ms, and delay */
DelayMs=gcb.DelayTime*10*DelayFact/40000;
if(DelayMs==0)
DelayMs=20;
} /* if END: GRAPHICS_EXT_FUNC_CODE */
/* Read out next extension and discard, TODO: parse it.. */
while (Extension!=NULL) {
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
PrintGifError(GifFile->Error);
Error=-14;
goto END_FUNC;
}
if (Extension == NULL) {
//printf("Extension is NULL\n");
break;
}
}
break;
case TERMINATE_RECORD_TYPE:
printf("RecordType: TERMINATE_RECORD_TYPE\n");
break;
/* TODO: other record type */
default: /* Should be trapped by DGifGetRecordType. */
break;
}
/* Pass ImageCount */
if(ImageCount != NULL)
*ImageCount=ImageNum;
if(RecordType == TERMINATE_RECORD_TYPE) {
//printf(" --------------- TERMINATE_RECORD_TYPE --------------\n");
k++; /* loop count */
if( k<nloop || nloop<=0 ) {
printf("%s: nloop k=%d, Total ImageCount=%d\n", __func__, k, ImageNum);
/* Close file and reOpen */
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR) {
PrintGifError(Error);
return Error;
}
printf("%s: Reopen GIF file...\n",__func__);
if((GifFile = DGifOpenFileName(fpath, &Error)) == NULL) {
PrintGifError(Error);
return Error;
}
}
}
} while (RecordType != TERMINATE_RECORD_TYPE || GifFile->ImageCount==0);
printf("%s: Total ImageCount=%d\n", __func__, ImageNum);
END_FUNC:
/* Free Simgbuf */
if(Simgbuf != NULL)
egi_imgbuf_free(Simgbuf);
/* Free scree buffers */
if(ScreenBuffer != NULL) {
for (i = 0; i < SHeight; i++)
free(ScreenBuffer[i]);
free(ScreenBuffer);
}
/* Close file */
if (DGifCloseFile(GifFile, &Error2) == GIF_ERROR) {
PrintGifError(Error2);
}
return Error;
}
/*----------------------------------------------------------------
Read a gif file and slurp all image data to an EGI_GIF_DATA struct.
@fpath: Path of an egi file.
Return:
An EGI_GIF_DATA poiner OK
NULL Fails
---------------------------------------------------------------*/
EGI_GIF_DATA* egi_gifdata_readFile(const char *fpath)
{
EGI_GIF_DATA* gif_data=NULL;
GifFileType *GifFile;
int Error;
int check_size;
int ImageTotal=0;
/* Try to get total number of images, for size check */
if( egi_gif_playFile(fpath, true, true, &ImageTotal,1, NULL) !=0 ) /* fpath, Silent, ImgTransp_ON, *ImageCount */
{
printf("%s: Fail to egi_gif_readFile() '%s'\n",__func__,fpath);
return NULL;
}
/* Open gif file */
if ((GifFile = DGifOpenFileName(fpath, &Error)) == NULL) {
PrintGifError(Error);
return NULL;
}
/* Big Size WARNING! */
printf("%s: GIF SHeight*SWidth*ImageTotal=%d*%d*%d \n",__func__,
GifFile->SHeight,GifFile->SWidth, ImageTotal );
check_size=GifFile->SHeight*GifFile->SWidth*ImageTotal;
if( check_size > 1024*1024*GIF_MAX_CHECKSIZE )
{
printf("%s: GIF check_size > 1024*1024*%d, stop slurping! \n", __func__, GIF_MAX_CHECKSIZE );
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR)
PrintGifError(Error);
return NULL;
}
/* calloc EGI_GIF_DATA */
gif_data=calloc(1, sizeof(EGI_GIF_DATA));
if(gif_data==NULL) {
printf("%s: Fail to calloc EGI_GIF_DATA.\n", __func__);
return NULL;
}
/* Slurp reads an entire GIF into core, hanging all its state info off
* the GifFileType pointer, it may take a short of time...
*/
printf("%s: Slurping GIF file and put images to memory...\n", __func__);
if (DGifSlurp(GifFile) == GIF_ERROR) {
PrintGifError(GifFile->Error);
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR)
PrintGifError(Error);
free(gif_data);
return NULL;
}
/* check sanity */
if( GifFile->ImageCount < 1 ) {
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR)
PrintGifError(Error);
free(gif_data);
return NULL;
}
/* Get GIF version*/
if( strcmp( "98",EGifGetGifVersion(GifFile) ) >= 0 )
gif_data->VerGIF89=true; /* GIF89 */
else
gif_data->VerGIF89=false; /* GIF87 */
/* Assign EGI_GIF_DATA members. Ownership to be transfered later...*/
gif_data->SWidth = GifFile->SWidth;
gif_data->SHeight = GifFile->SHeight;
gif_data->SColorResolution = GifFile->SColorResolution;
gif_data->SBackGroundColor = GifFile->SBackGroundColor;
gif_data->AspectByte = GifFile->AspectByte;
gif_data->SColorMap = GifFile->SColorMap;
gif_data->ImageTotal = GifFile->ImageCount; /* after slurp, GifFile->ImageCount is total number */
gif_data->SavedImages= GifFile->SavedImages;
#if 1
printf("%s: --- GIF Params---\n", __func__);
printf(" Version: %s", gif_data->VerGIF89 ? "GIF89":"GIF87");
printf(" SWidth x SHeight: %dx%d\n", gif_data->SWidth, gif_data->SHeight);
printf(" SColorMap: %s\n", gif_data->SColorMap==NULL ? "No":"Yes" );
printf(" SColorResolution: %d\n", gif_data->SColorResolution);
printf(" SBackGroundColor: %d\n", gif_data->SBackGroundColor);
printf(" AspectByte: %d\n", gif_data->AspectByte);
printf(" ImageTotal: %d\n", gif_data->ImageTotal);
#endif
/* Check SColorMap */
if(gif_data->SColorMap==NULL) {
printf("%s: SColorMap is NULL! gif_data NOT accept it!\n", __func__);
free(gif_data);
return NULL;
}
/* Decouple gif file handle, with respect to DGifCloseFile().
* Ownership transfered from GifFile to EGI_GIF!
*/
GifFile->SColorMap =NULL;
GifFile->SavedImages =NULL;
/* Note: Ownership of GifFile->ExtensionBlocks and GifFile->Image NOT transfered!
* it will be freed by DGifCloseFile().
*/
/* Now we can close GIF file, and let EGI_GIF to hold the data. */
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR) {
PrintGifError(Error);
// ...carry on ....
}
return gif_data;
}
/* --------------------------------------------------------------------------------------------------
Create an EGI_GIF struct with refrence to gif_data.
NOTE: SColorMap and SavedImages in the EGI_GIF are reference pointers
and will NOT be freed in egi_gif_free(), instead they will be released
by egi_gifdata_free().
@gif_data: An EGI_GIF_DATA holding uncompressed GIF data.
@ImgTransp_ON: (User define)
Usually set as TRUE, even for a nontransparent GIF.
To make the GIF gcb.TransparentColor transparent to its
background image by resetting its alpha value of EGI_GIF.Simgbuf to 0!
NOTE: GIF Transparency_on dosen't mean the GIF image itself is transparent!
it refers to gcb.TransparentColor for pixles in each sequence block images!
the purpose of gcb.TransparentColor is to keep previous pixel color/alpha unchaged,
and it's NOT necessary to make backgroup of the whole GIF image!
Only if you turn on ImgTransp_ON and all or some of its images' Disposal_Mode is 2!
A transparent GIF is applied only if it is so designed, and its SBackGroundColor
is usually also transparent(BUT not necessary!), so the background of the GIF
image is visible there.
TRUE: Alpha value of transparent pixels in each sequence block images
will be set to 0, and draw_dot() will NOT be called for them,
so it will speed up displaying!
Only when all or some of its images' Disposal_Mode is 2!
FALSE: SBackGroundColor will take effect.
draw_dot() will be called to draw every dot in each block image.
Return:
An EGI_GIF poiner OK
NULL Fails
---------------------------------------------------------------------------------------------------*/
EGI_GIF* egi_gif_create(const EGI_GIF_DATA *gif_data, bool ImgTransp_ON)
{
EGI_GIF* egif=NULL;
GifColorType *ColorMapEntry;
EGI_16BIT_COLOR img_bkcolor;
if(gif_data==NULL || gif_data->SavedImages==NULL ) {
printf("%s: Input gif_data is NULL or its data invalid!\n",__func__);
return NULL;
}
/* Check SColorMap! */
if(gif_data->SColorMap==NULL) {
printf("%s: SColorMap is NULL! EGI_GIF NOT accept it!\n",__func__);
return NULL;
}
/* calloc EGI_GIF */
egif=calloc(1, sizeof(EGI_GIF));
if(egif==NULL) {
printf("%s: Fail to calloc EGI_GIF.\n", __func__);
return NULL;
}
/* Assign EGI_GIF members. Ownership to be transfered later...*/
egif->ImgTransp_ON= ImgTransp_ON;
egif->VerGIF89= gif_data->VerGIF89;
egif->SWidth = gif_data->SWidth;
egif->SHeight = gif_data->SHeight;
egif->SColorResolution = gif_data->SColorResolution;
egif->SBackGroundColor = gif_data->SBackGroundColor;
egif->AspectByte = gif_data->AspectByte;
egif->SColorMap = gif_data->SColorMap; /* refrence, to be freed by egi_gifdata_free() */
egif->ImageCount = 0;
egif->ImageTotal = gif_data->ImageTotal;
egif->SavedImages= gif_data->SavedImages; /* refrence, to be freed by egi_gifdata_free() */
egif->Is_DataOwner= false; /* SColorMap and SavedImages to freed by egi_gifdata_free(), NOT egi_gif_free() */
/* get GIF's back ground color */
ColorMapEntry = &(egif->SColorMap->Colors[egif->SBackGroundColor]);
img_bkcolor=COLOR_RGB_TO16BITS( ColorMapEntry->Red,
ColorMapEntry->Green,
ColorMapEntry->Blue );
egif->bkcolor=img_bkcolor;
/* create egif->Simgbuf */
egif->Simgbuf=egi_imgbuf_create(egif->SHeight, egif->SWidth,
egif->ImgTransp_ON==true ? 0:255, img_bkcolor); //height, width, alpha, color
if(egif->Simgbuf==NULL) {
printf("%s: Fail to create egif->Simgbuf!\n", __func__);
free(egif);
return NULL;
}
return egif;
}
/*------------------------------------------------------------------------------
Slurp a GIF file and load all image data to an EGI_GIF struct.
@fpath: File path
@ImgTransp_ON: (User define)
Usually set as TRUE, even for a nontransparent GIF.
To make the GIF gcb.TransparentColor transparent to its
background image by resetting its alpha value of EGI_GIF.Simgbuf to 0!
NOTE: GIF Transparency_on dosen't mean the GIF image itself is transparent!
it refers to gcb.TransparentColor for pixles in each sequence block images!
the purpose of gcb.TransparentColor is to keep previous pixel color/alpha unchaged,
and it's NOT necessary to make backgroup of the whole GIF image!
Only if you turn on ImgTransp_ON and all or some of its images' Disposal_Mode is 2!
A transparent GIF is applied only if it is so designed, and its SBackGroundColor
is usually also transparent(BUT not necessary!), so the background of the GIF
image is visible there.
TRUE: Alpha value of transparent pixels in each sequence block images
will be set to 0, and draw_dot() will NOT be called for them,
so it will speed up displaying!
Only when all or some of its images' Disposal_Mode is 2!
FALSE: SBackGroundColor will take effect.
draw_dot() will be called to draw every dot in each block image.
Return:
A pointer to EGI_GIF OK
NULL Fail
--------------------------------------------------------------------------------*/
EGI_GIF* egi_gif_slurpFile(const char *fpath, bool ImgTransp_ON)
{
EGI_GIF* egif=NULL;
GifFileType *GifFile;
// GifRecordType RecordType;
int Error;
int check_size;
// GifByteType *ExtData;
// int ExtFunction;
int ImageTotal=0;
GifColorType *ColorMapEntry;
EGI_16BIT_COLOR img_bkcolor;
/* Try to get total number of images, for size check */
if( egi_gif_playFile(fpath, true, true, &ImageTotal, 1, NULL) !=0 ) /* fpath, Silent, ImgTransp_ON, nloop, *ImageCount */
{
printf("%s: Fail to egi_gif_readFile() '%s'\n",__func__,fpath);
return NULL;
}
/* Open gif file */
if ((GifFile = DGifOpenFileName(fpath, &Error)) == NULL) {
PrintGifError(Error);
return NULL;
}
/* Big Size WARNING! */
printf("%s: GIF SHeight*SWidth*ImageCount=%d*%d*%d \n",__func__,
GifFile->SHeight,GifFile->SWidth,ImageTotal );
check_size=GifFile->SHeight*GifFile->SWidth*ImageTotal;
if( check_size > 1024*1024*GIF_MAX_CHECKSIZE )
{
printf("%s: GIF check_size > 1024*1024*%d, stop slurping! \n", __func__, GIF_MAX_CHECKSIZE );
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR)
PrintGifError(Error);
return NULL;
}