Coverage Report

Created: 2026-09-13 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
67.3k
  (fread(buffer, 1, len, file) == ((size_t)(len)))
43
44
45
4.49k
#define MAXCOLORMAPSIZE  256    /* max # of colors in a GIF colormap */
46
6.59k
#define NUMCOLORS        3      /* # of colors */
47
337M
#define CM_RED           0      /* color component numbers */
48
282M
#define CM_GREEN         1
49
282M
#define CM_BLUE          2
50
51
4.84M
#define MAX_LZW_BITS     12     /* maximum LZW code size */
52
4.83M
#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.2k
  ((unsigned int)(array[offset]) + \
58
15.2k
   (((unsigned int)array[offset + 1]) << 8))
59
60
10.7k
#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
102k
{
138
102k
  register FILE *infile = sinfo->pub.input_file;
139
102k
  register int c;
140
141
102k
  if ((c = getc(infile)) == EOF)
142
1.64k
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
143
102k
  return c;
144
102k
}
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
58.9k
{
152
58.9k
  int count;
153
154
58.9k
  count = ReadByte(sinfo);
155
58.9k
  if (count > 0) {
156
55.1k
    if (!ReadOK(sinfo->pub.input_file, buf, count))
157
305
      ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
158
55.1k
  }
159
58.9k
  return count;
160
58.9k
}
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.99k
{
167
1.99k
  unsigned char buf[256];
168
169
4.09k
  while (GetDataBlock(sinfo, buf) > 0)
170
2.10k
    /* skip */;
171
1.99k
}
172
173
174
LOCAL(void)
175
ReInitLZW(gif_source_ptr sinfo)
176
/* (Re)initialize LZW state; shared code for startup and Clear processing */
177
8.28k
{
178
8.28k
  sinfo->code_size = sinfo->input_code_size + 1;
179
8.28k
  sinfo->limit_code = sinfo->clear_code << 1;   /* 2^code_size */
180
8.28k
  sinfo->max_code = sinfo->clear_code + 2;      /* first unused code value */
181
8.28k
  sinfo->sp = sinfo->symbol_stack;              /* init stack to empty */
182
8.28k
}
183
184
185
LOCAL(void)
186
InitLZWCode(gif_source_ptr sinfo)
187
/* Initialize for a series of LZWReadByte (and hence GetCode) calls */
188
2.43k
{
189
  /* GetCode initialization */
190
2.43k
  sinfo->last_byte = 2;         /* make safe to "recopy last two bytes" */
191
2.43k
  sinfo->code_buf[0] = 0;
192
2.43k
  sinfo->code_buf[1] = 0;
193
2.43k
  sinfo->last_bit = 0;          /* nothing in the buffer */
194
2.43k
  sinfo->cur_bit = 0;           /* force buffer load on first call */
195
2.43k
  sinfo->first_time = TRUE;
196
2.43k
  sinfo->out_of_blocks = FALSE;
197
198
  /* LZWReadByte initialization: */
199
  /* compute special code values (note that these do not change later) */
200
2.43k
  sinfo->clear_code = 1 << sinfo->input_code_size;
201
2.43k
  sinfo->end_code = sinfo->clear_code + 1;
202
2.43k
  ReInitLZW(sinfo);
203
2.43k
}
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
324M
{
211
324M
  register int accum;
212
324M
  int offs, count;
213
214
324M
  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
320M
    if (sinfo->first_time) {
218
2.35k
      sinfo->first_time = FALSE;
219
2.35k
      return sinfo->clear_code;
220
2.35k
    }
221
320M
    if (sinfo->out_of_blocks) {
222
319M
      WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
223
319M
      return sinfo->end_code;   /* fake something useful */
224
319M
    }
225
    /* preserve last two bytes of what we have -- assume code_size <= 16 */
226
54.8k
    sinfo->code_buf[0] = sinfo->code_buf[sinfo->last_byte-2];
227
54.8k
    sinfo->code_buf[1] = sinfo->code_buf[sinfo->last_byte-1];
228
    /* Load more bytes; set flag if we reach the terminator block */
229
54.8k
    if ((count = GetDataBlock(sinfo, &sinfo->code_buf[2])) == 0) {
230
1.13k
      sinfo->out_of_blocks = TRUE;
231
1.13k
      WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
232
1.13k
      return sinfo->end_code;   /* fake something useful */
233
1.13k
    }
234
    /* Reset counters */
235
53.6k
    sinfo->cur_bit = (sinfo->cur_bit - sinfo->last_bit) + 16;
236
53.6k
    sinfo->last_byte = 2 + count;
237
53.6k
    sinfo->last_bit = sinfo->last_byte * 8;
238
53.6k
  }
239
240
  /* Form up next 24 bits in accum */
241
4.83M
  offs = sinfo->cur_bit >> 3;   /* byte containing cur_bit */
242
4.83M
  accum = sinfo->code_buf[offs + 2];
243
4.83M
  accum <<= 8;
244
4.83M
  accum |= sinfo->code_buf[offs + 1];
245
4.83M
  accum <<= 8;
246
4.83M
  accum |= sinfo->code_buf[offs];
247
248
  /* Right-align cur_bit in accum, then mask off desired number of bits */
249
4.83M
  accum >>= (sinfo->cur_bit & 7);
250
4.83M
  sinfo->cur_bit += sinfo->code_size;
251
4.83M
  return accum & ((1 << sinfo->code_size) - 1);
252
324M
}
253
254
255
LOCAL(int)
256
LZWReadByte(gif_source_ptr sinfo)
257
/* Read an LZW-compressed byte */
258
337M
{
259
337M
  register int code;            /* current working code */
260
337M
  int incode;                   /* saves actual input code */
261
262
  /* If any codes are stacked from a previously read symbol, return them */
263
337M
  if (sinfo->sp > sinfo->symbol_stack)
264
12.5M
    return (int)(*(--sinfo->sp));
265
266
  /* Time to read a new symbol */
267
324M
  code = GetCode(sinfo);
268
269
324M
  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
5.85k
    ReInitLZW(sinfo);
273
7.57k
    do {
274
7.57k
      code = GetCode(sinfo);
275
7.57k
    } while (code == sinfo->clear_code);
276
5.85k
    if (code > sinfo->clear_code) { /* make sure it is a raw byte */
277
2.28k
      WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
278
2.28k
      code = 0;                 /* use something valid */
279
2.28k
    }
280
    /* make firstcode, oldcode valid! */
281
5.85k
    sinfo->firstcode = sinfo->oldcode = code;
282
5.85k
    return code;
283
5.85k
  }
284
285
324M
  if (code == sinfo->end_code) {
286
    /* Skip the rest of the image, unless GetCode already read terminator */
287
319M
    if (!sinfo->out_of_blocks) {
288
305
      SkipDataBlocks(sinfo);
289
305
      sinfo->out_of_blocks = TRUE;
290
305
    }
291
    /* Complain that there's not enough data */
292
319M
    WARNMS(sinfo->cinfo, JWRN_GIF_ENDCODE);
293
    /* Pad data with 0's */
294
319M
    return 0;                   /* fake something usable */
295
319M
  }
296
297
  /* Got normal raw byte or LZW symbol */
298
4.81M
  incode = code;                /* save for a moment */
299
300
4.81M
  if (code >= sinfo->max_code) { /* special case for not-yet-defined symbol */
301
    /* code == max_code is OK; anything bigger is bad data */
302
488k
    if (code > sinfo->max_code) {
303
474k
      WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
304
474k
      incode = 0;               /* prevent creation of loops in symbol table */
305
474k
    }
306
    /* this symbol will be defined as oldcode/firstcode */
307
488k
    *(sinfo->sp++) = (UINT8)sinfo->firstcode;
308
488k
    code = sinfo->oldcode;
309
488k
  }
310
311
  /* If it's a symbol, expand it into the stack */
312
16.8M
  while (code >= sinfo->clear_code) {
313
12.0M
    *(sinfo->sp++) = sinfo->symbol_tail[code]; /* tail is a byte value */
314
12.0M
    code = sinfo->symbol_head[code]; /* head is another LZW symbol */
315
12.0M
  }
316
  /* At this point code just represents a raw byte */
317
4.81M
  sinfo->firstcode = code;      /* save for possible future use */
318
319
  /* If there's room in table... */
320
4.81M
  if ((code = sinfo->max_code) < LZW_TABLE_SIZE) {
321
    /* Define a new symbol = prev sym + head of this sym's expansion */
322
2.49M
    sinfo->symbol_head[code] = (UINT16)sinfo->oldcode;
323
2.49M
    sinfo->symbol_tail[code] = (UINT8)sinfo->firstcode;
324
2.49M
    sinfo->max_code++;
325
    /* Is it time to increase code_size? */
326
2.49M
    if (sinfo->max_code >= sinfo->limit_code &&
327
8.58k
        sinfo->code_size < MAX_LZW_BITS) {
328
8.13k
      sinfo->code_size++;
329
8.13k
      sinfo->limit_code <<= 1;  /* keep equal to 2^code_size */
330
8.13k
    }
331
2.49M
  }
332
333
4.81M
  sinfo->oldcode = incode;      /* save last input symbol for future use */
334
4.81M
  return sinfo->firstcode;      /* return first byte of symbol's expansion */
335
324M
}
336
337
338
LOCAL(void)
339
ReadColorMap(gif_source_ptr sinfo, int cmaplen, JSAMPARRAY cmap)
340
/* Read a GIF colormap */
341
1.66k
{
342
1.66k
  int i, gray = 1;
343
344
12.3k
  for (i = 0; i < cmaplen; i++) {
345
10.6k
#if BITS_IN_JSAMPLE == 8
346
31.9k
#define UPSCALE(x)  (x)
347
#else
348
#define UPSCALE(x)  ((x) << (BITS_IN_JSAMPLE - 8))
349
#endif
350
10.6k
    cmap[CM_RED][i]   = (JSAMPLE)UPSCALE(ReadByte(sinfo));
351
10.6k
    cmap[CM_GREEN][i] = (JSAMPLE)UPSCALE(ReadByte(sinfo));
352
10.6k
    cmap[CM_BLUE][i]  = (JSAMPLE)UPSCALE(ReadByte(sinfo));
353
10.6k
    if (cmap[CM_RED][i] != cmap[CM_GREEN][i] ||
354
4.81k
        cmap[CM_GREEN][i] != cmap[CM_BLUE][i])
355
7.11k
      gray = 0;
356
10.6k
  }
357
358
1.66k
  if (sinfo->cinfo->in_color_space == JCS_RGB && gray) {
359
354
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
360
354
    sinfo->cinfo->input_components = 1;
361
354
  }
362
1.66k
}
363
364
365
LOCAL(void)
366
DoExtension(gif_source_ptr sinfo)
367
/* Process an extension block */
368
/* Currently we ignore 'em all */
369
1.69k
{
370
1.69k
  int extlabel;
371
372
  /* Read extension label byte */
373
1.69k
  extlabel = ReadByte(sinfo);
374
1.69k
  TRACEMS1(sinfo->cinfo, 1, JTRC_GIF_EXTENSION, extlabel);
375
  /* Skip the data block(s) associated with the extension */
376
1.69k
  SkipDataBlocks(sinfo);
377
1.69k
}
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.49k
{
387
4.49k
  gif_source_ptr source = (gif_source_ptr)sinfo;
388
4.49k
  unsigned char hdrbuf[10];     /* workspace for reading control blocks */
389
4.49k
  unsigned int width, height;   /* image dimensions */
390
4.49k
  int colormaplen, aspectRatio;
391
4.49k
  int c;
392
393
  /* Read and verify GIF Header */
394
4.49k
  if (!ReadOK(source->pub.input_file, hdrbuf, 6))
395
24
    ERREXIT(cinfo, JERR_GIF_NOT);
396
4.49k
  if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
397
134
    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.49k
  if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
403
4.31k
      (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
404
4.49k
    TRACEMS3(cinfo, 1, JTRC_GIF_BADVERSION, hdrbuf[3], hdrbuf[4], hdrbuf[5]);
405
406
  /* Read and decipher Logical Screen Descriptor */
407
4.49k
  if (!ReadOK(source->pub.input_file, hdrbuf, 7))
408
278
    ERREXIT(cinfo, JERR_INPUT_EOF);
409
4.49k
  width = LM_to_uint(hdrbuf, 0);
410
4.49k
  height = LM_to_uint(hdrbuf, 2);
411
4.49k
  if (width == 0 || height == 0)
412
14
    ERREXIT(cinfo, JERR_GIF_EMPTY);
413
4.49k
  if (width > JPEG_MAX_DIMENSION || height > JPEG_MAX_DIMENSION)
414
18
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
415
4.49k
  if (sinfo->max_pixels &&
416
4.02k
      (unsigned long long)width * height > sinfo->max_pixels)
417
280
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
418
  /* we ignore the color resolution, sort flag, and background color index */
419
4.49k
  aspectRatio = hdrbuf[6];
420
4.49k
  if (aspectRatio != 0 && aspectRatio != 49)
421
2.63k
    TRACEMS(cinfo, 1, JTRC_GIF_NONSQUARE);
422
423
  /* Allocate space to store the colormap */
424
4.49k
  source->colormap = (*cinfo->mem->alloc_sarray)
425
4.49k
    ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)MAXCOLORMAPSIZE,
426
4.49k
     (JDIMENSION)NUMCOLORS);
427
4.49k
  colormaplen = 0;              /* indicate initialization */
428
429
  /* Read global colormap if header indicates it is present */
430
4.49k
  if (BitSet(hdrbuf[4], COLORMAPFLAG)) {
431
1.45k
    colormaplen = 2 << (hdrbuf[4] & 0x07);
432
1.45k
    ReadColorMap(source, colormaplen, source->colormap);
433
1.45k
  }
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
8.05k
  for (;;) {
439
8.05k
    c = ReadByte(source);
440
441
8.05k
    if (c == ';')               /* GIF terminator?? */
442
6
      ERREXIT(cinfo, JERR_GIF_IMAGENOTFOUND);
443
444
8.05k
    if (c == '!') {             /* Extension */
445
1.69k
      DoExtension(source);
446
1.69k
      continue;
447
1.69k
    }
448
449
6.35k
    if (c != ',') {             /* Not an image separator? */
450
3.21k
      WARNMS1(cinfo, JWRN_GIF_CHAR, c);
451
3.21k
      continue;
452
3.21k
    }
453
454
    /* Read and decipher Local Image Descriptor */
455
3.14k
    if (!ReadOK(source->pub.input_file, hdrbuf, 9))
456
22
      ERREXIT(cinfo, JERR_INPUT_EOF);
457
    /* we ignore top/left position info, also sort flag */
458
3.14k
    width = LM_to_uint(hdrbuf, 4);
459
3.14k
    height = LM_to_uint(hdrbuf, 6);
460
3.14k
    if (width == 0 || height == 0)
461
12
      ERREXIT(cinfo, JERR_GIF_EMPTY);
462
3.14k
    if (width > JPEG_MAX_DIMENSION || height > JPEG_MAX_DIMENSION)
463
16
      ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
464
3.14k
    if (sinfo->max_pixels &&
465
2.84k
        (unsigned long long)width * height > sinfo->max_pixels)
466
278
      ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
467
3.14k
    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.14k
    if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
473
208
      colormaplen = 2 << (hdrbuf[8] & 0x07);
474
208
      ReadColorMap(source, colormaplen, source->colormap);
475
208
    }
476
477
3.14k
    source->input_code_size = ReadByte(source); /* get min-code-size byte */
478
3.14k
    if (source->input_code_size < 2 || source->input_code_size > 8)
479
50
      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.14k
    break;
485
6.35k
  }
486
487
  /* Prepare to read selected image: first initialize LZW decompressor */
488
4.49k
  source->symbol_head = (UINT16 *)
489
4.49k
    (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
490
4.49k
                                LZW_TABLE_SIZE * sizeof(UINT16));
491
4.49k
  source->symbol_tail = (UINT8 *)
492
4.49k
    (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
493
4.49k
                                LZW_TABLE_SIZE * sizeof(UINT8));
494
4.49k
  source->symbol_stack = (UINT8 *)
495
4.49k
    (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
496
4.49k
                                LZW_TABLE_SIZE * sizeof(UINT8));
497
4.49k
  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.49k
  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.44k
    source->interlaced_image = (*cinfo->mem->request_virt_sarray)
510
1.44k
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
511
1.44k
       (JDIMENSION)width, (JDIMENSION)height, (JDIMENSION)1);
512
1.44k
    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.44k
    source->pub.get_pixel_rows = load_interlaced_image;
517
3.05k
  } else {
518
3.05k
    source->pub.get_pixel_rows = get_pixel_rows;
519
3.05k
  }
520
521
4.49k
  if (cinfo->in_color_space != JCS_GRAYSCALE) {
522
2.10k
    cinfo->in_color_space = JCS_RGB;
523
2.10k
    cinfo->input_components = NUMCOLORS;
524
2.10k
  }
525
526
  /* Create compressor input buffer. */
527
4.49k
  source->pub.buffer = (*cinfo->mem->alloc_sarray)
528
4.49k
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
529
4.49k
     (JDIMENSION)width * cinfo->input_components, (JDIMENSION)1);
530
4.49k
  source->pub.buffer_height = 1;
531
532
  /* Pad colormap for safety. */
533
137k
  for (c = colormaplen; c < source->clear_code; c++) {
534
132k
    source->colormap[CM_RED][c]   =
535
132k
    source->colormap[CM_GREEN][c] =
536
132k
    source->colormap[CM_BLUE][c]  = CENTERJSAMPLE;
537
132k
  }
538
539
  /* Return info about the image. */
540
4.49k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
541
4.49k
  cinfo->image_width = width;
542
4.49k
  cinfo->image_height = height;
543
544
4.49k
  TRACEMS3(cinfo, 1, JTRC_GIF, width, height, colormaplen);
545
4.49k
}
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
2.34M
{
557
2.34M
  gif_source_ptr source = (gif_source_ptr)sinfo;
558
2.34M
  register int c;
559
2.34M
  register JSAMPROW ptr;
560
2.34M
  register JDIMENSION col;
561
2.34M
  register JSAMPARRAY colormap = source->colormap;
562
563
2.34M
  ptr = source->pub.buffer[0];
564
2.34M
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
565
21.6M
    for (col = cinfo->image_width; col > 0; col--) {
566
21.2M
      c = LZWReadByte(source);
567
21.2M
      *ptr++ = colormap[CM_RED][c];
568
21.2M
    }
569
1.93M
  } else {
570
83.3M
    for (col = cinfo->image_width; col > 0; col--) {
571
81.3M
      c = LZWReadByte(source);
572
81.3M
      *ptr++ = colormap[CM_RED][c];
573
81.3M
      *ptr++ = colormap[CM_GREEN][c];
574
81.3M
      *ptr++ = colormap[CM_BLUE][c];
575
81.3M
    }
576
1.93M
  }
577
2.34M
  return 1;
578
2.34M
}
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.39k
{
590
1.39k
  gif_source_ptr source = (gif_source_ptr)sinfo;
591
1.39k
  register JSAMPROW sptr;
592
1.39k
  register JDIMENSION col;
593
1.39k
  JDIMENSION row;
594
1.39k
  cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
595
596
  /* Read the interlaced image into the virtual array we've created. */
597
30.5M
  for (row = 0; row < cinfo->image_height; row++) {
598
30.5M
    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
30.5M
    sptr = *(*cinfo->mem->access_virt_sarray)
604
30.5M
      ((j_common_ptr)cinfo, source->interlaced_image, row, (JDIMENSION)1,
605
30.5M
       TRUE);
606
265M
    for (col = cinfo->image_width; col > 0; col--) {
607
234M
      *sptr++ = (JSAMPLE)LZWReadByte(source);
608
234M
    }
609
30.5M
  }
610
1.39k
  if (progress != NULL)
611
0
    progress->completed_extra_passes++;
612
613
  /* Replace method pointer so subsequent calls don't come here. */
614
1.39k
  source->pub.get_pixel_rows = get_interlaced_row;
615
  /* Initialize for get_interlaced_row, and perform first call on it. */
616
1.39k
  source->cur_row_number = 0;
617
1.39k
  source->pass2_offset = (cinfo->image_height + 7) / 8;
618
1.39k
  source->pass3_offset = source->pass2_offset + (cinfo->image_height + 3) / 8;
619
1.39k
  source->pass4_offset = source->pass3_offset + (cinfo->image_height + 1) / 4;
620
621
1.39k
  return get_interlaced_row(cinfo, sinfo);
622
1.39k
}
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
30.4M
{
634
30.4M
  gif_source_ptr source = (gif_source_ptr)sinfo;
635
30.4M
  register int c;
636
30.4M
  register JSAMPROW sptr, ptr;
637
30.4M
  register JDIMENSION col;
638
30.4M
  register JSAMPARRAY colormap = source->colormap;
639
30.4M
  JDIMENSION irow;
640
641
  /* Figure out which row of interlaced image is needed, and access it. */
642
30.4M
  switch ((int)(source->cur_row_number & 7)) {
643
3.80M
  case 0:                       /* first-pass row */
644
3.80M
    irow = source->cur_row_number >> 3;
645
3.80M
    break;
646
3.80M
  case 4:                       /* second-pass row */
647
3.80M
    irow = (source->cur_row_number >> 3) + source->pass2_offset;
648
3.80M
    break;
649
3.80M
  case 2:                       /* third-pass row */
650
7.61M
  case 6:
651
7.61M
    irow = (source->cur_row_number >> 2) + source->pass3_offset;
652
7.61M
    break;
653
15.2M
  default:                      /* fourth-pass row */
654
15.2M
    irow = (source->cur_row_number >> 1) + source->pass4_offset;
655
30.4M
  }
656
30.4M
  sptr = *(*cinfo->mem->access_virt_sarray)
657
30.4M
    ((j_common_ptr)cinfo, source->interlaced_image, irow, (JDIMENSION)1,
658
30.4M
     FALSE);
659
  /* Scan the row, expand colormap, and output */
660
30.4M
  ptr = source->pub.buffer[0];
661
30.4M
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
662
34.3M
    for (col = cinfo->image_width; col > 0; col--) {
663
33.2M
      c = *sptr++;
664
33.2M
      *ptr++ = colormap[CM_RED][c];
665
33.2M
    }
666
29.3M
  } else {
667
230M
    for (col = cinfo->image_width; col > 0; col--) {
668
201M
      c = *sptr++;
669
201M
      *ptr++ = colormap[CM_RED][c];
670
201M
      *ptr++ = colormap[CM_GREEN][c];
671
201M
      *ptr++ = colormap[CM_BLUE][c];
672
201M
    }
673
29.3M
  }
674
30.4M
  source->cur_row_number++;     /* for next time */
675
30.4M
  return 1;
676
30.4M
}
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
608
{
683
608
  return FALSE;
684
608
}
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.40k
{
694
  /* no work */
695
2.40k
}
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.49k
{
705
4.49k
  gif_source_ptr source;
706
707
4.49k
  if (cinfo->data_precision != 8)
708
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
709
710
  /* Create module interface object */
711
4.49k
  source = (gif_source_ptr)
712
4.49k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
713
4.49k
                                sizeof(gif_source_struct));
714
4.49k
  source->cinfo = cinfo;        /* make back link for subroutines */
715
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
716
4.49k
  source->pub.start_input = start_input_gif;
717
4.49k
  source->pub.read_icc_profile = read_icc_profile_gif;
718
4.49k
  source->pub.finish_input = finish_input_gif;
719
4.49k
  source->pub.max_pixels = 0;
720
721
4.49k
  return (cjpeg_source_ptr)source;
722
4.49k
}
723
724
#endif /* GIF_SUPPORTED */