Debian Codegraph @

src/gif.c

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 static int InterlacedOffset[] = { 0, 4, 2, 1 };
18 static int InterlacedJumps[] = { 8, 8, 4, 2 };
19
20 static int
21 image_gif_read_buf(GifFileType *gif, GifByteType *data, int len)
22 {
23 image *im = (image *)gif->UserData;
24
25 //DEBUG_TRACE("GIF read_buf wants %d bytes, %d in buffer\n", len, buffer_len(im->buf));
26
27 if (im->fh != NULL) {
28 if ( !_check_buf(im->fh, im->buf, len, MAX(len, BUFFER_SIZE)) ) {
29 warn("Image::Scale not enough GIF data (%s)\n", SvPVX(im->path));
30 return 0;
31 }
32 }
33 else {
34 if (len > buffer_len(im->buf)) {
35 // read from SV into buffer
36 int sv_readlen = len - buffer_len(im->buf);
37
38 if (sv_readlen > sv_len(im->sv_data) - im->sv_offset) {
39 warn("Image::Scale not enough GIF data (%s)\n", SvPVX(im->path));
40 return 0;
41 }
42
43 DEBUG_TRACE(" Reading %d bytes of SV data @ %d\n", sv_readlen, im->sv_offset);
44 buffer_append(im->buf, SvPVX(im->sv_data) + im->sv_offset, sv_readlen);
45 im->sv_offset += sv_readlen;
46 }
47 }
48
49 memcpy(data, buffer_ptr(im->buf), len);
50 buffer_consume(im->buf, len);
51
52 return len;
53 }
54
55 int
56 image_gif_read_header(image *im)
57 {
58 #ifdef GIFLIB_API_50
59 im->gif = DGifOpen(im, image_gif_read_buf, NULL);
60 #else
61 im->gif = DGifOpen(im, image_gif_read_buf);
62 #endif
63
64 if (im->gif == NULL) {
65 #ifdef GIFLIB_API_41
66 PrintGifError();
67 #endif
68 warn("Image::Scale unable to open GIF file (%s)\n", SvPVX(im->path));
69 image_gif_finish(im);
70 return 0;
71 }
72
73 im->width = im->gif->SWidth;
74 im->height = im->gif->SHeight;
75
76 return 1;
77 }
78
79 int
80 image_gif_load(image *im)
81 {
82 int x, y, ofs;
83 GifRecordType RecordType;
84 GifPixelType *line = NULL;
85 int ExtFunction = 0;
86 GifByteType *ExtData;
87 SavedImage *sp;
88 SavedImage temp_save;
89 int BackGround = 0;
90 int trans_index = 0; // transparent index if any
91 ColorMapObject *ColorMap;
92 GifColorType *ColorMapEntry;
93
94 temp_save.ExtensionBlocks = NULL;
95 temp_save.ExtensionBlockCount = 0;
96
97 // If reusing the object a second time, start over
98 if (im->used) {
99 DEBUG_TRACE("Recreating giflib objects\n");
100 image_gif_finish(im);
101
102 if (im->fh != NULL) {
103 // reset file to begining of image
104 PerlIO_seek(im->fh, im->image_offset, SEEK_SET);
105 }
106 else {
107 // reset SV read
108 im->sv_offset = im->image_offset;
109 }
110
111 buffer_clear(im->buf);
112
113 image_gif_read_header(im);
114 }
115
116 do {
117 if (DGifGetRecordType(im->gif, &RecordType) == GIF_ERROR) {
118 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
119 image_gif_finish(im);
120 return 0;
121 }
122
123 switch (RecordType) {
124 case IMAGE_DESC_RECORD_TYPE:
125 if (DGifGetImageDesc(im->gif) == GIF_ERROR) {
126 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
127 image_gif_finish(im);
128 return 0;
129 }
130
131 sp = &im->gif->SavedImages[im->gif->ImageCount - 1];
132
133 im->width = sp->ImageDesc.Width;
134 im->height = sp->ImageDesc.Height;
135
136 BackGround = im->gif->SBackGroundColor; // XXX needed?
137 ColorMap = im->gif->Image.ColorMap ? im->gif->Image.ColorMap : im->gif->SColorMap;
138
139 if (ColorMap == NULL) {
140 warn("Image::Scale GIF image has no colormap (%s)\n", SvPVX(im->path));
141 image_gif_finish(im);
142 return 0;
143 }
144
145 // Allocate storage for decompressed image
146 image_alloc(im, im->width, im->height);
147
148 New(0, line, im->width, GifPixelType);
149
150 if (im->gif->Image.Interlace) {
151 int i;
152 for (i = 0; i < 4; i++) {
153 for (x = InterlacedOffset[i]; x < im->height; x += InterlacedJumps[i]) {
154 ofs = x * im->width;
155 if (DGifGetLine(im->gif, line, 0) != GIF_OK) {
156 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
157 image_gif_finish(im);
158 return 0;
159 }
160
161 for (y = 0; y < im->width; y++) {
162 ColorMapEntry = &ColorMap->Colors[line[y]];
163 im->pixbuf[ofs++] = COL_FULL(
164 ColorMapEntry->Red,
165 ColorMapEntry->Green,
166 ColorMapEntry->Blue,
167 trans_index == line[y] ? 0 : 255
168 );
169 }
170 }
171 }
172 }
173 else {
174 ofs = 0;
175 for (x = 0; x < im->height; x++) {
176 if (DGifGetLine(im->gif, line, 0) != GIF_OK) {
177 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
178 image_gif_finish(im);
179 return 0;
180 }
181
182 for (y = 0; y < im->width; y++) {
183 ColorMapEntry = &ColorMap->Colors[line[y]];
184 im->pixbuf[ofs++] = COL_FULL(
185 ColorMapEntry->Red,
186 ColorMapEntry->Green,
187 ColorMapEntry->Blue,
188 trans_index == line[y] ? 0 : 255
189 );
190 }
191 }
192 }
193
194 Safefree(line);
195 break;
196
197 case EXTENSION_RECORD_TYPE:
198 if (DGifGetExtension(im->gif, &ExtFunction, &ExtData) == GIF_ERROR) {
199 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
200 image_gif_finish(im);
201 return 0;
202 }
203
204 if (ExtFunction == 0xF9) { // transparency info
205 if (ExtData[1] & 1)
206 trans_index = ExtData[4];
207 else
208 trans_index = -1;
209 im->has_alpha = 1;
210 DEBUG_TRACE("GIF transparency index: %d\n", trans_index);
211 }
212
213 while (ExtData != NULL) {
214 /* Create an extension block with our data */
215 #ifdef GIFLIB_API_50
216 if (GifAddExtensionBlock(&im->gif->ExtensionBlockCount, &im->gif->ExtensionBlocks, ExtFunction, ExtData[0], &ExtData[1]) == GIF_ERROR) {
217 #else
218 temp_save.Function = ExtFunction;
219 if (AddExtensionBlock(&temp_save, ExtData[0], &ExtData[1]) == GIF_ERROR) {
220 #endif
221 #ifdef GIFLIB_API_41
222 PrintGifError();
223 #endif
224 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
225 image_gif_finish(im);
226 return 0;
227 }
228
229 if (DGifGetExtensionNext(im->gif, &ExtData) == GIF_ERROR) {
230 #ifdef GIFLIB_API_41
231 PrintGifError();
232 #endif
233 warn("Image::Scale unable to read GIF file (%s)\n", SvPVX(im->path));
234 image_gif_finish(im);
235 return 0;
236 }
237
238 ExtFunction = 0; // CONTINUE_EXT_FUNC_CODE
239 }
240 break;
241
242 case TERMINATE_RECORD_TYPE:
243 default:
244 break;
245 }
246 } while (RecordType != TERMINATE_RECORD_TYPE);
247
248 return 1;
249 }
250
251 void
252 image_gif_finish(image *im)
253 {
254 if (im->gif != NULL) {
255 #ifdef GIFLIB_API_51
256 if (DGifCloseFile(im->gif, NULL) != GIF_OK) {
257 #else
258 if (DGifCloseFile(im->gif) != GIF_OK) {
259 #endif
260 #ifdef GIFLIB_API_41
261 PrintGifError();
262 #endif
263 warn("Image::Scale unable to close GIF file (%s)\n", SvPVX(im->path));
264 }
265 im->gif = NULL;
266
267 DEBUG_TRACE("image_gif_finish\n");
268 }
269 }