Coverage Report

Created: 2026-04-12 06:05

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