-
Notifications
You must be signed in to change notification settings - Fork 3
/
img_gif.c
148 lines (134 loc) · 3.59 KB
/
img_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
#include <gif_lib.h>
#undef TRUE
#undef FALSE
static BOOL decGif_start (const char * file, IMGINFO info);
static BOOL decGif_read (IMGINFO, CHAR * buffer);
static void decGif_quit (IMGINFO);
static DECODER _decoder_gif = {
DECODER_CHAIN,
{ MIME_IMG_GIF, 0 },
decGif_start
};
#undef DECODER_CHAIN
#define DECODER_CHAIN &_decoder_gif
#if defined(GIFLIB_MAJOR)
static void _PrintGifError(GifFileType *gif)
{
const char *err = GifErrorString(gif->Error);
if (err != NULL)
fprintf(stderr, "\nGIF-LIB error: %s.\n", err);
else
fprintf(stderr, "\nGIF-LIB undefined error %d.\n", gif->Error);
}
#define PrintGifError() _PrintGifError(gif)
#endif
/*----------------------------------------------------------------------------*/
static BOOL
decGif_start (const char * file, IMGINFO info)
{
short interlace = 0;
short transpar = -1;
short img_w = 0;
short img_h = 0;
ColorMapObject * map = NULL;
#if defined(GIFLIB_MAJOR)
int gif_error;
GifFileType * gif = DGifOpenFileName (file, &gif_error);
#else
GifFileType * gif = DGifOpenFileName (file);
#endif
if (!gif) {
return FALSE;
} else {
GifRecordType rec;
do {
if (DGifGetRecordType (gif, &rec) == GIF_ERROR) {
fprintf (stderr, "DGifGetRecordType() ");
PrintGifError();
break;
} else if (rec == IMAGE_DESC_RECORD_TYPE) {
if (DGifGetImageDesc(gif) == GIF_ERROR) {
fprintf (stderr, "DGifGetImageDesc() ");
PrintGifError();
break;
} else {
GifImageDesc * dsc = &gif->Image;
map = (dsc->ColorMap ? dsc->ColorMap : gif->SColorMap);
img_w = dsc->Width;
img_h = dsc->Height;
if (dsc->Interlace) {
interlace = 8;
}
}
break;
} else if (rec == EXTENSION_RECORD_TYPE) {
int code;
GifByteType * block;
if (DGifGetExtension (gif, &code, &block) == GIF_ERROR) {
fprintf (stderr, "DGifGetExtension() ");
PrintGifError();
break;
} else while (block != NULL) {
if (code == 0xF9 && (block[1] & 1)) {
transpar = (short)block[4];
}
if (DGifGetExtensionNext (gif, &block) == GIF_ERROR) {
fprintf (stderr, "DGifGetExtensionNext() ");
PrintGifError();
break;
}
}
} else {
fprintf (stderr, "other: %i \n", rec);
break;
}
} while (rec != TERMINATE_RECORD_TYPE);
}
if (!map || img_w <= 0 || img_w >= 4096 || img_h <= 0 || img_h >= 4096) {
#if defined(GIFLIB_MAJOR)
DGifCloseFile (gif, NULL);
#else
DGifCloseFile (gif);
#endif
return TRUE;
}
info->_priv_data = gif;
/* info->_priv_more = NULL; */
/* info->_priv_file = NULL; */
info->read = decGif_read;
info->quit = decGif_quit;
info->ImgWidth = img_w;
info->ImgHeight = img_h;
info->NumComps = 1;
info->BitDepth = map->BitsPerPixel;
info->NumColors = map->ColorCount;
if ((info->Palette = &map->Colors->Red) != NULL) {
GifColorType * rgb = NULL;
info->PalRpos = (unsigned)&rgb->Red;
info->PalGpos = (unsigned)&rgb->Green;
info->PalBpos = (unsigned)&rgb->Blue;
info->PalStep = (unsigned)&rgb[1];
}
info->Transp = transpar;
info->Interlace = interlace;
return TRUE;
}
/*----------------------------------------------------------------------------*/
static BOOL
decGif_read (IMGINFO info, CHAR * buffer)
{
return (DGifGetLine (info->_priv_data, (void *)buffer, info->ImgWidth) == GIF_OK);
}
/*----------------------------------------------------------------------------*/
static void
decGif_quit (IMGINFO info)
{
if (info->_priv_data) {
#if defined(GIFLIB_MAJOR)
DGifCloseFile (info->_priv_data, NULL);
#else
DGifCloseFile (info->_priv_data);
#endif
info->_priv_data = NULL;
}
}