/src/libjpeg-turbo.main/src/rdgif.c
Line | Count | Source |
1 | | /* |
2 | | * rdgif.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1997, Thomas G. Lane. |
6 | | * Modified 2019 by Guido Vollbeding. |
7 | | * libjpeg-turbo Modifications: |
8 | | * Copyright (C) 2021-2023, 2026, D. R. Commander. |
9 | | * For conditions of distribution and use, see the accompanying README.ijg |
10 | | * file. |
11 | | * |
12 | | * This file contains routines to read input images in GIF format. |
13 | | * |
14 | | * These routines may need modification for non-Unix environments or |
15 | | * specialized applications. As they stand, they assume input from |
16 | | * an ordinary stdio stream. They further assume that reading begins |
17 | | * at the start of the file; start_input may need work if the |
18 | | * user interface has already read some data (e.g., to determine that |
19 | | * the file is indeed GIF format). |
20 | | */ |
21 | | |
22 | | /* |
23 | | * This code is loosely based on giftoppm from the PBMPLUS distribution |
24 | | * of Feb. 1991. That file contains the following copyright notice: |
25 | | * +-------------------------------------------------------------------+ |
26 | | * | Copyright 1990, David Koblas. | |
27 | | * | Permission to use, copy, modify, and distribute this software | |
28 | | * | and its documentation for any purpose and without fee is hereby | |
29 | | * | granted, provided that the above copyright notice appear in all | |
30 | | * | copies and that both that copyright notice and this permission | |
31 | | * | notice appear in supporting documentation. This software is | |
32 | | * | provided "as is" without express or implied warranty. | |
33 | | * +-------------------------------------------------------------------+ |
34 | | */ |
35 | | |
36 | | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
37 | | |
38 | | #ifdef GIF_SUPPORTED |
39 | | |
40 | | |
41 | | #define ReadOK(file, buffer, len) \ |
42 | 100k | (fread(buffer, 1, len, file) == ((size_t)(len))) |
43 | | |
44 | | |
45 | 4.68k | #define MAXCOLORMAPSIZE 256 /* max # of colors in a GIF colormap */ |
46 | 6.83k | #define NUMCOLORS 3 /* # of colors */ |
47 | 400M | #define CM_RED 0 /* color component numbers */ |
48 | 349M | #define CM_GREEN 1 |
49 | 349M | #define CM_BLUE 2 |
50 | | |
51 | 7.07M | #define MAX_LZW_BITS 12 /* maximum LZW code size */ |
52 | 7.06M | #define LZW_TABLE_SIZE (1 << MAX_LZW_BITS) /* # of possible LZW symbols */ |
53 | | |
54 | | /* Macros for extracting header data --- note we assume chars may be signed */ |
55 | | |
56 | | #define LM_to_uint(array, offset) \ |
57 | 15.9k | ((unsigned int)(array[offset]) + \ |
58 | 15.9k | (((unsigned int)array[offset + 1]) << 8)) |
59 | | |
60 | 11.2k | #define BitSet(byte, bit) ((byte) & (bit)) |
61 | | #define INTERLACE 0x40 /* mask for bit signifying interlaced image */ |
62 | | #define COLORMAPFLAG 0x80 /* mask for bit signifying colormap presence */ |
63 | | |
64 | | |
65 | | /* |
66 | | * LZW decompression tables look like this: |
67 | | * symbol_head[K] = prefix symbol of any LZW symbol K (0..LZW_TABLE_SIZE-1) |
68 | | * symbol_tail[K] = suffix byte of any LZW symbol K (0..LZW_TABLE_SIZE-1) |
69 | | * Note that entries 0..end_code of the above tables are not used, |
70 | | * since those symbols represent raw bytes or special codes. |
71 | | * |
72 | | * The stack represents the not-yet-used expansion of the last LZW symbol. |
73 | | * In the worst case, a symbol could expand to as many bytes as there are |
74 | | * LZW symbols, so we allocate LZW_TABLE_SIZE bytes for the stack. |
75 | | * (This is conservative since that number includes the raw-byte symbols.) |
76 | | */ |
77 | | |
78 | | |
79 | | /* Private version of data source object */ |
80 | | |
81 | | typedef struct { |
82 | | struct cjpeg_source_struct pub; /* public fields */ |
83 | | |
84 | | j_compress_ptr cinfo; /* back link saves passing separate parm */ |
85 | | |
86 | | JSAMPARRAY colormap; /* GIF colormap (converted to my format) */ |
87 | | |
88 | | /* State for GetCode and LZWReadByte */ |
89 | | unsigned char code_buf[256 + 4]; /* current input data block */ |
90 | | int last_byte; /* # of bytes in code_buf */ |
91 | | int last_bit; /* # of bits in code_buf */ |
92 | | int cur_bit; /* next bit index to read */ |
93 | | boolean first_time; /* flags first call to GetCode */ |
94 | | boolean out_of_blocks; /* TRUE if hit terminator data block */ |
95 | | |
96 | | int input_code_size; /* codesize given in GIF file */ |
97 | | int clear_code, end_code; /* values for Clear and End codes */ |
98 | | |
99 | | int code_size; /* current actual code size */ |
100 | | int limit_code; /* 2^code_size */ |
101 | | int max_code; /* first unused code value */ |
102 | | |
103 | | /* Private state for LZWReadByte */ |
104 | | int oldcode; /* previous LZW symbol */ |
105 | | int firstcode; /* first byte of oldcode's expansion */ |
106 | | |
107 | | /* LZW symbol table and expansion stack */ |
108 | | UINT16 *symbol_head; /* => table of prefix symbols */ |
109 | | UINT8 *symbol_tail; /* => table of suffix bytes */ |
110 | | UINT8 *symbol_stack; /* => stack for symbol expansions */ |
111 | | UINT8 *sp; /* stack pointer */ |
112 | | |
113 | | /* State for interlaced image processing */ |
114 | | boolean is_interlaced; /* TRUE if have interlaced image */ |
115 | | jvirt_sarray_ptr interlaced_image; /* full image in interlaced order */ |
116 | | JDIMENSION cur_row_number; /* need to know actual row number */ |
117 | | JDIMENSION pass2_offset; /* # of pixel rows in pass 1 */ |
118 | | JDIMENSION pass3_offset; /* # of pixel rows in passes 1&2 */ |
119 | | JDIMENSION pass4_offset; /* # of pixel rows in passes 1,2,3 */ |
120 | | } gif_source_struct; |
121 | | |
122 | | typedef gif_source_struct *gif_source_ptr; |
123 | | |
124 | | |
125 | | /* Forward declarations */ |
126 | | METHODDEF(JDIMENSION) get_pixel_rows(j_compress_ptr cinfo, |
127 | | cjpeg_source_ptr sinfo); |
128 | | METHODDEF(JDIMENSION) load_interlaced_image(j_compress_ptr cinfo, |
129 | | cjpeg_source_ptr sinfo); |
130 | | METHODDEF(JDIMENSION) get_interlaced_row(j_compress_ptr cinfo, |
131 | | cjpeg_source_ptr sinfo); |
132 | | |
133 | | |
134 | | LOCAL(int) |
135 | | ReadByte(gif_source_ptr sinfo) |
136 | | /* Read next byte from GIF file */ |
137 | 141k | { |
138 | 141k | register FILE *infile = sinfo->pub.input_file; |
139 | 141k | register int c; |
140 | | |
141 | 141k | if ((c = getc(infile)) == EOF) |
142 | 1.53k | ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); |
143 | 141k | return c; |
144 | 141k | } |
145 | | |
146 | | |
147 | | LOCAL(int) |
148 | | GetDataBlock(gif_source_ptr sinfo, unsigned char *buf) |
149 | | /* Read a GIF data block, which has a leading count byte */ |
150 | | /* A zero-length block marks the end of a data block sequence */ |
151 | 91.8k | { |
152 | 91.8k | int count; |
153 | | |
154 | 91.8k | count = ReadByte(sinfo); |
155 | 91.8k | if (count > 0) { |
156 | 88.2k | if (!ReadOK(sinfo->pub.input_file, buf, count)) |
157 | 396 | ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); |
158 | 88.2k | } |
159 | 91.8k | return count; |
160 | 91.8k | } |
161 | | |
162 | | |
163 | | LOCAL(void) |
164 | | SkipDataBlocks(gif_source_ptr sinfo) |
165 | | /* Skip a series of data blocks, until a block terminator is found */ |
166 | 1.98k | { |
167 | 1.98k | unsigned char buf[256]; |
168 | | |
169 | 4.42k | while (GetDataBlock(sinfo, buf) > 0) |
170 | 2.44k | /* skip */; |
171 | 1.98k | } |
172 | | |
173 | | |
174 | | LOCAL(void) |
175 | | ReInitLZW(gif_source_ptr sinfo) |
176 | | /* (Re)initialize LZW state; shared code for startup and Clear processing */ |
177 | 9.20k | { |
178 | 9.20k | sinfo->code_size = sinfo->input_code_size + 1; |
179 | 9.20k | sinfo->limit_code = sinfo->clear_code << 1; /* 2^code_size */ |
180 | 9.20k | sinfo->max_code = sinfo->clear_code + 2; /* first unused code value */ |
181 | 9.20k | sinfo->sp = sinfo->symbol_stack; /* init stack to empty */ |
182 | 9.20k | } |
183 | | |
184 | | |
185 | | LOCAL(void) |
186 | | InitLZWCode(gif_source_ptr sinfo) |
187 | | /* Initialize for a series of LZWReadByte (and hence GetCode) calls */ |
188 | 2.51k | { |
189 | | /* GetCode initialization */ |
190 | 2.51k | sinfo->last_byte = 2; /* make safe to "recopy last two bytes" */ |
191 | 2.51k | sinfo->code_buf[0] = 0; |
192 | 2.51k | sinfo->code_buf[1] = 0; |
193 | 2.51k | sinfo->last_bit = 0; /* nothing in the buffer */ |
194 | 2.51k | sinfo->cur_bit = 0; /* force buffer load on first call */ |
195 | 2.51k | sinfo->first_time = TRUE; |
196 | 2.51k | sinfo->out_of_blocks = FALSE; |
197 | | |
198 | | /* LZWReadByte initialization: */ |
199 | | /* compute special code values (note that these do not change later) */ |
200 | 2.51k | sinfo->clear_code = 1 << sinfo->input_code_size; |
201 | 2.51k | sinfo->end_code = sinfo->clear_code + 1; |
202 | 2.51k | ReInitLZW(sinfo); |
203 | 2.51k | } |
204 | | |
205 | | |
206 | | LOCAL(int) |
207 | | GetCode(gif_source_ptr sinfo) |
208 | | /* Fetch the next code_size bits from the GIF data */ |
209 | | /* We assume code_size is less than 16 */ |
210 | 384M | { |
211 | 384M | register int accum; |
212 | 384M | int offs, count; |
213 | | |
214 | 384M | while (sinfo->cur_bit + sinfo->code_size > sinfo->last_bit) { |
215 | | /* Time to reload the buffer */ |
216 | | /* First time, share code with Clear case */ |
217 | 377M | if (sinfo->first_time) { |
218 | 2.41k | sinfo->first_time = FALSE; |
219 | 2.41k | return sinfo->clear_code; |
220 | 2.41k | } |
221 | 377M | if (sinfo->out_of_blocks) { |
222 | 377M | WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA); |
223 | 377M | return sinfo->end_code; /* fake something useful */ |
224 | 377M | } |
225 | | /* preserve last two bytes of what we have -- assume code_size <= 16 */ |
226 | 87.3k | sinfo->code_buf[0] = sinfo->code_buf[sinfo->last_byte-2]; |
227 | 87.3k | sinfo->code_buf[1] = sinfo->code_buf[sinfo->last_byte-1]; |
228 | | /* Load more bytes; set flag if we reach the terminator block */ |
229 | 87.3k | if ((count = GetDataBlock(sinfo, &sinfo->code_buf[2])) == 0) { |
230 | 1.17k | sinfo->out_of_blocks = TRUE; |
231 | 1.17k | WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA); |
232 | 1.17k | return sinfo->end_code; /* fake something useful */ |
233 | 1.17k | } |
234 | | /* Reset counters */ |
235 | 86.2k | sinfo->cur_bit = (sinfo->cur_bit - sinfo->last_bit) + 16; |
236 | 86.2k | sinfo->last_byte = 2 + count; |
237 | 86.2k | sinfo->last_bit = sinfo->last_byte * 8; |
238 | 86.2k | } |
239 | | |
240 | | /* Form up next 24 bits in accum */ |
241 | 7.06M | offs = sinfo->cur_bit >> 3; /* byte containing cur_bit */ |
242 | 7.06M | accum = sinfo->code_buf[offs + 2]; |
243 | 7.06M | accum <<= 8; |
244 | 7.06M | accum |= sinfo->code_buf[offs + 1]; |
245 | 7.06M | accum <<= 8; |
246 | 7.06M | accum |= sinfo->code_buf[offs]; |
247 | | |
248 | | /* Right-align cur_bit in accum, then mask off desired number of bits */ |
249 | 7.06M | accum >>= (sinfo->cur_bit & 7); |
250 | 7.06M | sinfo->cur_bit += sinfo->code_size; |
251 | 7.06M | return accum & ((1 << sinfo->code_size) - 1); |
252 | 384M | } |
253 | | |
254 | | |
255 | | LOCAL(int) |
256 | | LZWReadByte(gif_source_ptr sinfo) |
257 | | /* Read an LZW-compressed byte */ |
258 | 400M | { |
259 | 400M | register int code; /* current working code */ |
260 | 400M | int incode; /* saves actual input code */ |
261 | | |
262 | | /* If any codes are stacked from a previously read symbol, return them */ |
263 | 400M | if (sinfo->sp > sinfo->symbol_stack) |
264 | 16.5M | return (int)(*(--sinfo->sp)); |
265 | | |
266 | | /* Time to read a new symbol */ |
267 | 384M | code = GetCode(sinfo); |
268 | | |
269 | 384M | if (code == sinfo->clear_code) { |
270 | | /* Reinit state, swallow any extra Clear codes, and */ |
271 | | /* return next code, which is expected to be a raw byte. */ |
272 | 6.69k | ReInitLZW(sinfo); |
273 | 8.47k | do { |
274 | 8.47k | code = GetCode(sinfo); |
275 | 8.47k | } while (code == sinfo->clear_code); |
276 | 6.69k | if (code > sinfo->clear_code) { /* make sure it is a raw byte */ |
277 | 2.39k | WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA); |
278 | 2.39k | code = 0; /* use something valid */ |
279 | 2.39k | } |
280 | | /* make firstcode, oldcode valid! */ |
281 | 6.69k | sinfo->firstcode = sinfo->oldcode = code; |
282 | 6.69k | return code; |
283 | 6.69k | } |
284 | | |
285 | 384M | if (code == sinfo->end_code) { |
286 | | /* Skip the rest of the image, unless GetCode already read terminator */ |
287 | 377M | if (!sinfo->out_of_blocks) { |
288 | 398 | SkipDataBlocks(sinfo); |
289 | 398 | sinfo->out_of_blocks = TRUE; |
290 | 398 | } |
291 | | /* Complain that there's not enough data */ |
292 | 377M | WARNMS(sinfo->cinfo, JWRN_GIF_ENDCODE); |
293 | | /* Pad data with 0's */ |
294 | 377M | return 0; /* fake something usable */ |
295 | 377M | } |
296 | | |
297 | | /* Got normal raw byte or LZW symbol */ |
298 | 7.05M | incode = code; /* save for a moment */ |
299 | | |
300 | 7.05M | if (code >= sinfo->max_code) { /* special case for not-yet-defined symbol */ |
301 | | /* code == max_code is OK; anything bigger is bad data */ |
302 | 603k | if (code > sinfo->max_code) { |
303 | 589k | WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA); |
304 | 589k | incode = 0; /* prevent creation of loops in symbol table */ |
305 | 589k | } |
306 | | /* this symbol will be defined as oldcode/firstcode */ |
307 | 603k | *(sinfo->sp++) = (UINT8)sinfo->firstcode; |
308 | 603k | code = sinfo->oldcode; |
309 | 603k | } |
310 | | |
311 | | /* If it's a symbol, expand it into the stack */ |
312 | 23.0M | while (code >= sinfo->clear_code) { |
313 | 15.9M | *(sinfo->sp++) = sinfo->symbol_tail[code]; /* tail is a byte value */ |
314 | 15.9M | code = sinfo->symbol_head[code]; /* head is another LZW symbol */ |
315 | 15.9M | } |
316 | | /* At this point code just represents a raw byte */ |
317 | 7.05M | sinfo->firstcode = code; /* save for possible future use */ |
318 | | |
319 | | /* If there's room in table... */ |
320 | 7.05M | if ((code = sinfo->max_code) < LZW_TABLE_SIZE) { |
321 | | /* Define a new symbol = prev sym + head of this sym's expansion */ |
322 | 3.03M | sinfo->symbol_head[code] = (UINT16)sinfo->oldcode; |
323 | 3.03M | sinfo->symbol_tail[code] = (UINT8)sinfo->firstcode; |
324 | 3.03M | sinfo->max_code++; |
325 | | /* Is it time to increase code_size? */ |
326 | 3.03M | if (sinfo->max_code >= sinfo->limit_code && |
327 | 9.44k | sinfo->code_size < MAX_LZW_BITS) { |
328 | 8.89k | sinfo->code_size++; |
329 | 8.89k | sinfo->limit_code <<= 1; /* keep equal to 2^code_size */ |
330 | 8.89k | } |
331 | 3.03M | } |
332 | | |
333 | 7.05M | sinfo->oldcode = incode; /* save last input symbol for future use */ |
334 | 7.05M | return sinfo->firstcode; /* return first byte of symbol's expansion */ |
335 | 384M | } |
336 | | |
337 | | |
338 | | LOCAL(void) |
339 | | ReadColorMap(gif_source_ptr sinfo, int cmaplen, JSAMPARRAY cmap) |
340 | | /* Read a GIF colormap */ |
341 | 1.79k | { |
342 | 1.79k | int i, gray = 1; |
343 | | |
344 | 13.8k | for (i = 0; i < cmaplen; i++) { |
345 | 12.0k | #if BITS_IN_JSAMPLE == 8 |
346 | 36.0k | #define UPSCALE(x) (x) |
347 | | #else |
348 | | #define UPSCALE(x) ((x) << (BITS_IN_JSAMPLE - 8)) |
349 | | #endif |
350 | 12.0k | cmap[CM_RED][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo)); |
351 | 12.0k | cmap[CM_GREEN][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo)); |
352 | 12.0k | cmap[CM_BLUE][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo)); |
353 | 12.0k | if (cmap[CM_RED][i] != cmap[CM_GREEN][i] || |
354 | 5.85k | cmap[CM_GREEN][i] != cmap[CM_BLUE][i]) |
355 | 7.63k | gray = 0; |
356 | 12.0k | } |
357 | | |
358 | 1.79k | if (sinfo->cinfo->in_color_space == JCS_RGB && gray) { |
359 | 382 | sinfo->cinfo->in_color_space = JCS_GRAYSCALE; |
360 | 382 | sinfo->cinfo->input_components = 1; |
361 | 382 | } |
362 | 1.79k | } |
363 | | |
364 | | |
365 | | LOCAL(void) |
366 | | DoExtension(gif_source_ptr sinfo) |
367 | | /* Process an extension block */ |
368 | | /* Currently we ignore 'em all */ |
369 | 1.59k | { |
370 | 1.59k | int extlabel; |
371 | | |
372 | | /* Read extension label byte */ |
373 | 1.59k | extlabel = ReadByte(sinfo); |
374 | 1.59k | TRACEMS1(sinfo->cinfo, 1, JTRC_GIF_EXTENSION, extlabel); |
375 | | /* Skip the data block(s) associated with the extension */ |
376 | 1.59k | SkipDataBlocks(sinfo); |
377 | 1.59k | } |
378 | | |
379 | | |
380 | | /* |
381 | | * Read the file header; return image size and component count. |
382 | | */ |
383 | | |
384 | | METHODDEF(void) |
385 | | start_input_gif(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
386 | 4.68k | { |
387 | 4.68k | gif_source_ptr source = (gif_source_ptr)sinfo; |
388 | 4.68k | unsigned char hdrbuf[10]; /* workspace for reading control blocks */ |
389 | 4.68k | unsigned int width, height; /* image dimensions */ |
390 | 4.68k | int colormaplen, aspectRatio; |
391 | 4.68k | int c; |
392 | | |
393 | | /* Read and verify GIF Header */ |
394 | 4.68k | if (!ReadOK(source->pub.input_file, hdrbuf, 6)) |
395 | 24 | ERREXIT(cinfo, JERR_GIF_NOT); |
396 | 4.68k | if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F') |
397 | 142 | ERREXIT(cinfo, JERR_GIF_NOT); |
398 | | /* Check for expected version numbers. |
399 | | * If unknown version, give warning and try to process anyway; |
400 | | * this is per recommendation in GIF89a standard. |
401 | | */ |
402 | 4.68k | if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') && |
403 | 4.50k | (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a')) |
404 | 4.68k | TRACEMS3(cinfo, 1, JTRC_GIF_BADVERSION, hdrbuf[3], hdrbuf[4], hdrbuf[5]); |
405 | | |
406 | | /* Read and decipher Logical Screen Descriptor */ |
407 | 4.68k | if (!ReadOK(source->pub.input_file, hdrbuf, 7)) |
408 | 296 | ERREXIT(cinfo, JERR_INPUT_EOF); |
409 | 4.68k | width = LM_to_uint(hdrbuf, 0); |
410 | 4.68k | height = LM_to_uint(hdrbuf, 2); |
411 | 4.68k | if (width == 0 || height == 0) |
412 | 12 | ERREXIT(cinfo, JERR_GIF_EMPTY); |
413 | 4.68k | if (width > JPEG_MAX_DIMENSION || height > JPEG_MAX_DIMENSION) |
414 | 20 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION); |
415 | 4.68k | if (sinfo->max_pixels && |
416 | 4.19k | (unsigned long long)width * height > sinfo->max_pixels) |
417 | 296 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels); |
418 | | /* we ignore the color resolution, sort flag, and background color index */ |
419 | 4.68k | aspectRatio = hdrbuf[6]; |
420 | 4.68k | if (aspectRatio != 0 && aspectRatio != 49) |
421 | 2.57k | TRACEMS(cinfo, 1, JTRC_GIF_NONSQUARE); |
422 | | |
423 | | /* Allocate space to store the colormap */ |
424 | 4.68k | source->colormap = (*cinfo->mem->alloc_sarray) |
425 | 4.68k | ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)MAXCOLORMAPSIZE, |
426 | 4.68k | (JDIMENSION)NUMCOLORS); |
427 | 4.68k | colormaplen = 0; /* indicate initialization */ |
428 | | |
429 | | /* Read global colormap if header indicates it is present */ |
430 | 4.68k | if (BitSet(hdrbuf[4], COLORMAPFLAG)) { |
431 | 1.53k | colormaplen = 2 << (hdrbuf[4] & 0x07); |
432 | 1.53k | ReadColorMap(source, colormaplen, source->colormap); |
433 | 1.53k | } |
434 | | |
435 | | /* Scan until we reach start of desired image. |
436 | | * We don't currently support skipping images, but could add it easily. |
437 | | */ |
438 | 9.77k | for (;;) { |
439 | 9.77k | c = ReadByte(source); |
440 | | |
441 | 9.77k | if (c == ';') /* GIF terminator?? */ |
442 | 8 | ERREXIT(cinfo, JERR_GIF_IMAGENOTFOUND); |
443 | | |
444 | 9.77k | if (c == '!') { /* Extension */ |
445 | 1.59k | DoExtension(source); |
446 | 1.59k | continue; |
447 | 1.59k | } |
448 | | |
449 | 8.17k | if (c != ',') { /* Not an image separator? */ |
450 | 4.88k | WARNMS1(cinfo, JWRN_GIF_CHAR, c); |
451 | 4.88k | continue; |
452 | 4.88k | } |
453 | | |
454 | | /* Read and decipher Local Image Descriptor */ |
455 | 3.29k | if (!ReadOK(source->pub.input_file, hdrbuf, 9)) |
456 | 20 | ERREXIT(cinfo, JERR_INPUT_EOF); |
457 | | /* we ignore top/left position info, also sort flag */ |
458 | 3.29k | width = LM_to_uint(hdrbuf, 4); |
459 | 3.29k | height = LM_to_uint(hdrbuf, 6); |
460 | 3.29k | if (width == 0 || height == 0) |
461 | 12 | ERREXIT(cinfo, JERR_GIF_EMPTY); |
462 | 3.29k | if (width > JPEG_MAX_DIMENSION || height > JPEG_MAX_DIMENSION) |
463 | 28 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION); |
464 | 3.29k | if (sinfo->max_pixels && |
465 | 2.95k | (unsigned long long)width * height > sinfo->max_pixels) |
466 | 284 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels); |
467 | 3.29k | source->is_interlaced = (BitSet(hdrbuf[8], INTERLACE) != 0); |
468 | | |
469 | | /* Read local colormap if header indicates it is present */ |
470 | | /* Note: if we wanted to support skipping images, */ |
471 | | /* we'd need to skip rather than read colormap for ignored images */ |
472 | 3.29k | if (BitSet(hdrbuf[8], COLORMAPFLAG)) { |
473 | 254 | colormaplen = 2 << (hdrbuf[8] & 0x07); |
474 | 254 | ReadColorMap(source, colormaplen, source->colormap); |
475 | 254 | } |
476 | | |
477 | 3.29k | source->input_code_size = ReadByte(source); /* get min-code-size byte */ |
478 | 3.29k | if (source->input_code_size < 2 || source->input_code_size > 8) |
479 | 54 | ERREXIT1(cinfo, JERR_GIF_CODESIZE, source->input_code_size); |
480 | | |
481 | | /* Reached desired image, so break out of loop */ |
482 | | /* If we wanted to skip this image, */ |
483 | | /* we'd call SkipDataBlocks and then continue the loop */ |
484 | 3.29k | break; |
485 | 8.17k | } |
486 | | |
487 | | /* Prepare to read selected image: first initialize LZW decompressor */ |
488 | 4.68k | source->symbol_head = (UINT16 *) |
489 | 4.68k | (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
490 | 4.68k | LZW_TABLE_SIZE * sizeof(UINT16)); |
491 | 4.68k | source->symbol_tail = (UINT8 *) |
492 | 4.68k | (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
493 | 4.68k | LZW_TABLE_SIZE * sizeof(UINT8)); |
494 | 4.68k | source->symbol_stack = (UINT8 *) |
495 | 4.68k | (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
496 | 4.68k | LZW_TABLE_SIZE * sizeof(UINT8)); |
497 | 4.68k | InitLZWCode(source); |
498 | | |
499 | | /* |
500 | | * If image is interlaced, we read it into a full-size sample array, |
501 | | * decompressing as we go; then get_interlaced_row selects rows from the |
502 | | * sample array in the proper order. |
503 | | */ |
504 | 4.68k | if (source->is_interlaced) { |
505 | | /* We request the virtual array now, but can't access it until virtual |
506 | | * arrays have been allocated. Hence, the actual work of reading the |
507 | | * image is postponed until the first call to get_pixel_rows. |
508 | | */ |
509 | 1.46k | source->interlaced_image = (*cinfo->mem->request_virt_sarray) |
510 | 1.46k | ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE, |
511 | 1.46k | (JDIMENSION)width, (JDIMENSION)height, (JDIMENSION)1); |
512 | 1.46k | if (cinfo->progress != NULL) { |
513 | 0 | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
514 | 0 | progress->total_extra_passes++; /* count file input as separate pass */ |
515 | 0 | } |
516 | 1.46k | source->pub.get_pixel_rows = load_interlaced_image; |
517 | 3.22k | } else { |
518 | 3.22k | source->pub.get_pixel_rows = get_pixel_rows; |
519 | 3.22k | } |
520 | | |
521 | 4.68k | if (cinfo->in_color_space != JCS_GRAYSCALE) { |
522 | 2.15k | cinfo->in_color_space = JCS_RGB; |
523 | 2.15k | cinfo->input_components = NUMCOLORS; |
524 | 2.15k | } |
525 | | |
526 | | /* Create compressor input buffer. */ |
527 | 4.68k | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
528 | 4.68k | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
529 | 4.68k | (JDIMENSION)width * cinfo->input_components, (JDIMENSION)1); |
530 | 4.68k | source->pub.buffer_height = 1; |
531 | | |
532 | | /* Pad colormap for safety. */ |
533 | 154k | for (c = colormaplen; c < source->clear_code; c++) { |
534 | 150k | source->colormap[CM_RED][c] = |
535 | 150k | source->colormap[CM_GREEN][c] = |
536 | 150k | source->colormap[CM_BLUE][c] = CENTERJSAMPLE; |
537 | 150k | } |
538 | | |
539 | | /* Return info about the image. */ |
540 | 4.68k | cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ |
541 | 4.68k | cinfo->image_width = width; |
542 | 4.68k | cinfo->image_height = height; |
543 | | |
544 | 4.68k | TRACEMS3(cinfo, 1, JTRC_GIF, width, height, colormaplen); |
545 | 4.68k | } |
546 | | |
547 | | |
548 | | /* |
549 | | * Read one row of pixels. |
550 | | * This version is used for noninterlaced GIF images: |
551 | | * we read directly from the GIF file. |
552 | | */ |
553 | | |
554 | | METHODDEF(JDIMENSION) |
555 | | get_pixel_rows(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
556 | 4.42M | { |
557 | 4.42M | gif_source_ptr source = (gif_source_ptr)sinfo; |
558 | 4.42M | register int c; |
559 | 4.42M | register JSAMPROW ptr; |
560 | 4.42M | register JDIMENSION col; |
561 | 4.42M | register JSAMPARRAY colormap = source->colormap; |
562 | | |
563 | 4.42M | ptr = source->pub.buffer[0]; |
564 | 4.42M | if (cinfo->in_color_space == JCS_GRAYSCALE) { |
565 | 16.7M | for (col = cinfo->image_width; col > 0; col--) { |
566 | 16.3M | c = LZWReadByte(source); |
567 | 16.3M | *ptr++ = colormap[CM_RED][c]; |
568 | 16.3M | } |
569 | 4.04M | } else { |
570 | 87.4M | for (col = cinfo->image_width; col > 0; col--) { |
571 | 83.3M | c = LZWReadByte(source); |
572 | 83.3M | *ptr++ = colormap[CM_RED][c]; |
573 | 83.3M | *ptr++ = colormap[CM_GREEN][c]; |
574 | 83.3M | *ptr++ = colormap[CM_BLUE][c]; |
575 | 83.3M | } |
576 | 4.04M | } |
577 | 4.42M | return 1; |
578 | 4.42M | } |
579 | | |
580 | | |
581 | | /* |
582 | | * Read one row of pixels. |
583 | | * This version is used for the first call on get_pixel_rows when |
584 | | * reading an interlaced GIF file: we read the whole image into memory. |
585 | | */ |
586 | | |
587 | | METHODDEF(JDIMENSION) |
588 | | load_interlaced_image(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
589 | 1.38k | { |
590 | 1.38k | gif_source_ptr source = (gif_source_ptr)sinfo; |
591 | 1.38k | register JSAMPROW sptr; |
592 | 1.38k | register JDIMENSION col; |
593 | 1.38k | JDIMENSION row; |
594 | 1.38k | cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress; |
595 | | |
596 | | /* Read the interlaced image into the virtual array we've created. */ |
597 | 27.0M | for (row = 0; row < cinfo->image_height; row++) { |
598 | 27.0M | if (progress != NULL) { |
599 | 0 | progress->pub.pass_counter = (long)row; |
600 | 0 | progress->pub.pass_limit = (long)cinfo->image_height; |
601 | 0 | (*progress->pub.progress_monitor) ((j_common_ptr)cinfo); |
602 | 0 | } |
603 | 27.0M | sptr = *(*cinfo->mem->access_virt_sarray) |
604 | 27.0M | ((j_common_ptr)cinfo, source->interlaced_image, row, (JDIMENSION)1, |
605 | 27.0M | TRUE); |
606 | 328M | for (col = cinfo->image_width; col > 0; col--) { |
607 | 301M | *sptr++ = (JSAMPLE)LZWReadByte(source); |
608 | 301M | } |
609 | 27.0M | } |
610 | 1.38k | if (progress != NULL) |
611 | 0 | progress->completed_extra_passes++; |
612 | | |
613 | | /* Replace method pointer so subsequent calls don't come here. */ |
614 | 1.38k | source->pub.get_pixel_rows = get_interlaced_row; |
615 | | /* Initialize for get_interlaced_row, and perform first call on it. */ |
616 | 1.38k | source->cur_row_number = 0; |
617 | 1.38k | source->pass2_offset = (cinfo->image_height + 7) / 8; |
618 | 1.38k | source->pass3_offset = source->pass2_offset + (cinfo->image_height + 3) / 8; |
619 | 1.38k | source->pass4_offset = source->pass3_offset + (cinfo->image_height + 1) / 4; |
620 | | |
621 | 1.38k | return get_interlaced_row(cinfo, sinfo); |
622 | 1.38k | } |
623 | | |
624 | | |
625 | | /* |
626 | | * Read one row of pixels. |
627 | | * This version is used for interlaced GIF images: |
628 | | * we read from the virtual array. |
629 | | */ |
630 | | |
631 | | METHODDEF(JDIMENSION) |
632 | | get_interlaced_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
633 | 26.9M | { |
634 | 26.9M | gif_source_ptr source = (gif_source_ptr)sinfo; |
635 | 26.9M | register int c; |
636 | 26.9M | register JSAMPROW sptr, ptr; |
637 | 26.9M | register JDIMENSION col; |
638 | 26.9M | register JSAMPARRAY colormap = source->colormap; |
639 | 26.9M | JDIMENSION irow; |
640 | | |
641 | | /* Figure out which row of interlaced image is needed, and access it. */ |
642 | 26.9M | switch ((int)(source->cur_row_number & 7)) { |
643 | 3.37M | case 0: /* first-pass row */ |
644 | 3.37M | irow = source->cur_row_number >> 3; |
645 | 3.37M | break; |
646 | 3.37M | case 4: /* second-pass row */ |
647 | 3.37M | irow = (source->cur_row_number >> 3) + source->pass2_offset; |
648 | 3.37M | break; |
649 | 3.37M | case 2: /* third-pass row */ |
650 | 6.74M | case 6: |
651 | 6.74M | irow = (source->cur_row_number >> 2) + source->pass3_offset; |
652 | 6.74M | break; |
653 | 13.4M | default: /* fourth-pass row */ |
654 | 13.4M | irow = (source->cur_row_number >> 1) + source->pass4_offset; |
655 | 26.9M | } |
656 | 26.9M | sptr = *(*cinfo->mem->access_virt_sarray) |
657 | 26.9M | ((j_common_ptr)cinfo, source->interlaced_image, irow, (JDIMENSION)1, |
658 | 26.9M | FALSE); |
659 | | /* Scan the row, expand colormap, and output */ |
660 | 26.9M | ptr = source->pub.buffer[0]; |
661 | 26.9M | if (cinfo->in_color_space == JCS_GRAYSCALE) { |
662 | 36.7M | for (col = cinfo->image_width; col > 0; col--) { |
663 | 35.3M | c = *sptr++; |
664 | 35.3M | *ptr++ = colormap[CM_RED][c]; |
665 | 35.3M | } |
666 | 25.6M | } else { |
667 | 291M | for (col = cinfo->image_width; col > 0; col--) { |
668 | 265M | c = *sptr++; |
669 | 265M | *ptr++ = colormap[CM_RED][c]; |
670 | 265M | *ptr++ = colormap[CM_GREEN][c]; |
671 | 265M | *ptr++ = colormap[CM_BLUE][c]; |
672 | 265M | } |
673 | 25.6M | } |
674 | 26.9M | source->cur_row_number++; /* for next time */ |
675 | 26.9M | return 1; |
676 | 26.9M | } |
677 | | |
678 | | |
679 | | METHODDEF(boolean) |
680 | | read_icc_profile_gif(j_compress_ptr cinfo, cjpeg_source_ptr sinfo, |
681 | | JOCTET **icc_data_ptr, unsigned int *icc_data_len) |
682 | 612 | { |
683 | 612 | return FALSE; |
684 | 612 | } |
685 | | |
686 | | |
687 | | /* |
688 | | * Finish up at the end of the file. |
689 | | */ |
690 | | |
691 | | METHODDEF(void) |
692 | | finish_input_gif(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
693 | 2.54k | { |
694 | | /* no work */ |
695 | 2.54k | } |
696 | | |
697 | | |
698 | | /* |
699 | | * The module selection routine for GIF format input. |
700 | | */ |
701 | | |
702 | | GLOBAL(cjpeg_source_ptr) |
703 | | jinit_read_gif(j_compress_ptr cinfo) |
704 | 4.68k | { |
705 | 4.68k | gif_source_ptr source; |
706 | | |
707 | 4.68k | if (cinfo->data_precision != 8) |
708 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
709 | | |
710 | | /* Create module interface object */ |
711 | 4.68k | source = (gif_source_ptr) |
712 | 4.68k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
713 | 4.68k | sizeof(gif_source_struct)); |
714 | 4.68k | source->cinfo = cinfo; /* make back link for subroutines */ |
715 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
716 | 4.68k | source->pub.start_input = start_input_gif; |
717 | 4.68k | source->pub.read_icc_profile = read_icc_profile_gif; |
718 | 4.68k | source->pub.finish_input = finish_input_gif; |
719 | 4.68k | source->pub.max_pixels = 0; |
720 | | |
721 | 4.68k | return (cjpeg_source_ptr)source; |
722 | 4.68k | } |
723 | | |
724 | | #endif /* GIF_SUPPORTED */ |