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
76.7k
  (fread(buffer, 1, len, file) == ((size_t)(len)))
45
46
47
1.81k
#define MAXCOLORMAPSIZE  256    /* max # of colors in a GIF colormap */
48
2.79k
#define NUMCOLORS        3      /* # of colors */
49
241M
#define CM_RED           0      /* color component numbers */
50
220M
#define CM_GREEN         1
51
220M
#define CM_BLUE          2
52
53
5.99M
#define MAX_LZW_BITS     12     /* maximum LZW code size */
54
5.99M
#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
6.45k
  ((unsigned int)(array[offset]) + \
60
6.45k
   (((unsigned int)array[offset + 1]) << 8))
61
62
4.64k
#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
95.3k
{
140
95.3k
  register FILE *infile = sinfo->pub.input_file;
141
95.3k
  register int c;
142
143
95.3k
  if ((c = getc(infile)) == EOF)
144
385
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
145
95.3k
  return c;
146
95.3k
}
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
73.2k
{
154
73.2k
  int count;
155
156
73.2k
  count = ReadByte(sinfo);
157
73.2k
  if (count > 0) {
158
71.7k
    if (!ReadOK(sinfo->pub.input_file, buf, count))
159
158
      ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
160
71.7k
  }
161
73.2k
  return count;
162
73.2k
}
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
857
{
169
857
  unsigned char buf[256];
170
171
2.46k
  while (GetDataBlock(sinfo, buf) > 0)
172
1.60k
    /* skip */;
173
857
}
174
175
176
LOCAL(void)
177
ReInitLZW(gif_source_ptr sinfo)
178
/* (Re)initialize LZW state; shared code for startup and Clear processing */
179
4.39k
{
180
4.39k
  sinfo->code_size = sinfo->input_code_size + 1;
181
4.39k
  sinfo->limit_code = sinfo->clear_code << 1;   /* 2^code_size */
182
4.39k
  sinfo->max_code = sinfo->clear_code + 2;      /* first unused code value */
183
4.39k
  sinfo->sp = sinfo->symbol_stack;              /* init stack to empty */
184
4.39k
}
185
186
187
LOCAL(void)
188
InitLZWCode(gif_source_ptr sinfo)
189
/* Initialize for a series of LZWReadByte (and hence GetCode) calls */
190
1.15k
{
191
  /* GetCode initialization */
192
1.15k
  sinfo->last_byte = 2;         /* make safe to "recopy last two bytes" */
193
1.15k
  sinfo->code_buf[0] = 0;
194
1.15k
  sinfo->code_buf[1] = 0;
195
1.15k
  sinfo->last_bit = 0;          /* nothing in the buffer */
196
1.15k
  sinfo->cur_bit = 0;           /* force buffer load on first call */
197
1.15k
  sinfo->first_time = TRUE;
198
1.15k
  sinfo->out_of_blocks = FALSE;
199
200
  /* LZWReadByte initialization: */
201
  /* compute special code values (note that these do not change later) */
202
1.15k
  sinfo->clear_code = 1 << sinfo->input_code_size;
203
1.15k
  sinfo->end_code = sinfo->clear_code + 1;
204
1.15k
  ReInitLZW(sinfo);
205
1.15k
}
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
229M
{
213
229M
  register int accum;
214
229M
  int offs, count;
215
216
229M
  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
223M
    if (sinfo->first_time) {
220
1.07k
      sinfo->first_time = FALSE;
221
1.07k
      return sinfo->clear_code;
222
1.07k
    }
223
223M
    if (sinfo->out_of_blocks) {
224
223M
      WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
225
223M
      return sinfo->end_code;   /* fake something useful */
226
223M
    }
227
    /* preserve last two bytes of what we have -- assume code_size <= 16 */
228
70.7k
    sinfo->code_buf[0] = sinfo->code_buf[sinfo->last_byte-2];
229
70.7k
    sinfo->code_buf[1] = sinfo->code_buf[sinfo->last_byte-1];
230
    /* Load more bytes; set flag if we reach the terminator block */
231
70.7k
    if ((count = GetDataBlock(sinfo, &sinfo->code_buf[2])) == 0) {
232
596
      sinfo->out_of_blocks = TRUE;
233
596
      WARNMS(sinfo->cinfo, JWRN_GIF_NOMOREDATA);
234
596
      return sinfo->end_code;   /* fake something useful */
235
596
    }
236
    /* Reset counters */
237
70.1k
    sinfo->cur_bit = (sinfo->cur_bit - sinfo->last_bit) + 16;
238
70.1k
    sinfo->last_byte = 2 + count;
239
70.1k
    sinfo->last_bit = sinfo->last_byte * 8;
240
70.1k
  }
241
242
  /* Form up next 24 bits in accum */
243
5.99M
  offs = sinfo->cur_bit >> 3;   /* byte containing cur_bit */
244
5.99M
  accum = sinfo->code_buf[offs + 2];
245
5.99M
  accum <<= 8;
246
5.99M
  accum |= sinfo->code_buf[offs + 1];
247
5.99M
  accum <<= 8;
248
5.99M
  accum |= sinfo->code_buf[offs];
249
250
  /* Right-align cur_bit in accum, then mask off desired number of bits */
251
5.99M
  accum >>= (sinfo->cur_bit & 7);
252
5.99M
  sinfo->cur_bit += sinfo->code_size;
253
5.99M
  return accum & ((1 << sinfo->code_size) - 1);
254
229M
}
255
256
257
LOCAL(int)
258
LZWReadByte(gif_source_ptr sinfo)
259
/* Read an LZW-compressed byte */
260
241M
{
261
241M
  register int code;            /* current working code */
262
241M
  int incode;                   /* saves actual input code */
263
264
  /* If any codes are stacked from a previously read symbol, return them */
265
241M
  if (sinfo->sp > sinfo->symbol_stack)
266
12.2M
    return (int)(*(--sinfo->sp));
267
268
  /* Time to read a new symbol */
269
229M
  code = GetCode(sinfo);
270
271
229M
  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
3.24k
    ReInitLZW(sinfo);
275
3.87k
    do {
276
3.87k
      code = GetCode(sinfo);
277
3.87k
    } while (code == sinfo->clear_code);
278
3.24k
    if (code > sinfo->clear_code) { /* make sure it is a raw byte */
279
1.02k
      WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
280
1.02k
      code = 0;                 /* use something valid */
281
1.02k
    }
282
    /* make firstcode, oldcode valid! */
283
3.24k
    sinfo->firstcode = sinfo->oldcode = code;
284
3.24k
    return code;
285
3.24k
  }
286
287
229M
  if (code == sinfo->end_code) {
288
    /* Skip the rest of the image, unless GetCode already read terminator */
289
223M
    if (!sinfo->out_of_blocks) {
290
251
      SkipDataBlocks(sinfo);
291
251
      sinfo->out_of_blocks = TRUE;
292
251
    }
293
    /* Complain that there's not enough data */
294
223M
    WARNMS(sinfo->cinfo, JWRN_GIF_ENDCODE);
295
    /* Pad data with 0's */
296
223M
    return 0;                   /* fake something usable */
297
223M
  }
298
299
  /* Got normal raw byte or LZW symbol */
300
5.98M
  incode = code;                /* save for a moment */
301
302
5.98M
  if (code >= sinfo->max_code) { /* special case for not-yet-defined symbol */
303
    /* code == max_code is OK; anything bigger is bad data */
304
493k
    if (code > sinfo->max_code) {
305
489k
      WARNMS(sinfo->cinfo, JWRN_GIF_BADDATA);
306
489k
      incode = 0;               /* prevent creation of loops in symbol table */
307
489k
    }
308
    /* this symbol will be defined as oldcode/firstcode */
309
493k
    *(sinfo->sp++) = (UINT8)sinfo->firstcode;
310
493k
    code = sinfo->oldcode;
311
493k
  }
312
313
  /* If it's a symbol, expand it into the stack */
314
17.7M
  while (code >= sinfo->clear_code) {
315
11.7M
    *(sinfo->sp++) = sinfo->symbol_tail[code]; /* tail is a byte value */
316
11.7M
    code = sinfo->symbol_head[code]; /* head is another LZW symbol */
317
11.7M
  }
318
  /* At this point code just represents a raw byte */
319
5.98M
  sinfo->firstcode = code;      /* save for possible future use */
320
321
  /* If there's room in table... */
322
5.98M
  if ((code = sinfo->max_code) < LZW_TABLE_SIZE) {
323
    /* Define a new symbol = prev sym + head of this sym's expansion */
324
2.00M
    sinfo->symbol_head[code] = (UINT16)sinfo->oldcode;
325
2.00M
    sinfo->symbol_tail[code] = (UINT8)sinfo->firstcode;
326
2.00M
    sinfo->max_code++;
327
    /* Is it time to increase code_size? */
328
2.00M
    if (sinfo->max_code >= sinfo->limit_code &&
329
5.50k
        sinfo->code_size < MAX_LZW_BITS) {
330
5.19k
      sinfo->code_size++;
331
5.19k
      sinfo->limit_code <<= 1;  /* keep equal to 2^code_size */
332
5.19k
    }
333
2.00M
  }
334
335
5.98M
  sinfo->oldcode = incode;      /* save last input symbol for future use */
336
5.98M
  return sinfo->firstcode;      /* return first byte of symbol's expansion */
337
229M
}
338
339
340
LOCAL(void)
341
ReadColorMap(gif_source_ptr sinfo, int cmaplen, _JSAMPARRAY cmap)
342
/* Read a GIF colormap */
343
982
{
344
982
  int i, gray = 1;
345
346
5.63k
  for (i = 0; i < cmaplen; i++) {
347
4.65k
#if BITS_IN_JSAMPLE == 8
348
13.9k
#define UPSCALE(x)  (x)
349
#else
350
#define UPSCALE(x)  ((x) << (BITS_IN_JSAMPLE - 8))
351
#endif
352
4.65k
    cmap[CM_RED][i]   = (_JSAMPLE)UPSCALE(ReadByte(sinfo));
353
4.65k
    cmap[CM_GREEN][i] = (_JSAMPLE)UPSCALE(ReadByte(sinfo));
354
4.65k
    cmap[CM_BLUE][i]  = (_JSAMPLE)UPSCALE(ReadByte(sinfo));
355
4.65k
    if (cmap[CM_RED][i] != cmap[CM_GREEN][i] ||
356
2.50k
        cmap[CM_GREEN][i] != cmap[CM_BLUE][i])
357
2.95k
      gray = 0;
358
4.65k
  }
359
360
982
  if (sinfo->cinfo->in_color_space == JCS_RGB && gray) {
361
178
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
362
178
    sinfo->cinfo->input_components = 1;
363
178
  }
364
982
}
365
366
367
LOCAL(void)
368
DoExtension(gif_source_ptr sinfo)
369
/* Process an extension block */
370
/* Currently we ignore 'em all */
371
610
{
372
610
  int extlabel;
373
374
  /* Read extension label byte */
375
610
  extlabel = ReadByte(sinfo);
376
610
  TRACEMS1(sinfo->cinfo, 1, JTRC_GIF_EXTENSION, extlabel);
377
  /* Skip the data block(s) associated with the extension */
378
610
  SkipDataBlocks(sinfo);
379
610
}
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
1.81k
{
389
1.81k
  gif_source_ptr source = (gif_source_ptr)sinfo;
390
1.81k
  unsigned char hdrbuf[10];     /* workspace for reading control blocks */
391
1.81k
  unsigned int width, height;   /* image dimensions */
392
1.81k
  int colormaplen, aspectRatio;
393
1.81k
  int c;
394
395
  /* Read and verify GIF Header */
396
1.81k
  if (!ReadOK(source->pub.input_file, hdrbuf, 6))
397
8
    ERREXIT(cinfo, JERR_GIF_NOT);
398
1.81k
  if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
399
46
    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
1.81k
  if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
405
1.75k
      (hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
406
1.81k
    TRACEMS3(cinfo, 1, JTRC_GIF_BADVERSION, hdrbuf[3], hdrbuf[4], hdrbuf[5]);
407
408
  /* Read and decipher Logical Screen Descriptor */
409
1.81k
  if (!ReadOK(source->pub.input_file, hdrbuf, 7))
410
82
    ERREXIT(cinfo, JERR_INPUT_EOF);
411
1.81k
  width = LM_to_uint(hdrbuf, 0);
412
1.81k
  height = LM_to_uint(hdrbuf, 2);
413
1.81k
  if (width == 0 || height == 0)
414
4
    ERREXIT(cinfo, JERR_GIF_EMPTY);
415
1.81k
  if (width > JPEG_MAX_DIMENSION || height > JPEG_MAX_DIMENSION)
416
6
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
417
1.81k
  if (sinfo->max_pixels &&
418
1.66k
      (unsigned long long)width * height > sinfo->max_pixels)
419
90
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
420
  /* we ignore the color resolution, sort flag, and background color index */
421
1.81k
  aspectRatio = hdrbuf[6];
422
1.81k
  if (aspectRatio != 0 && aspectRatio != 49)
423
1.01k
    TRACEMS(cinfo, 1, JTRC_GIF_NONSQUARE);
424
425
  /* Allocate space to store the colormap */
426
1.81k
  source->colormap = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
427
1.81k
    ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)MAXCOLORMAPSIZE,
428
1.81k
     (JDIMENSION)NUMCOLORS);
429
1.81k
  colormaplen = 0;              /* indicate initialization */
430
431
  /* Read global colormap if header indicates it is present */
432
1.81k
  if (BitSet(hdrbuf[4], COLORMAPFLAG)) {
433
786
    colormaplen = 2 << (hdrbuf[4] & 0x07);
434
786
    ReadColorMap(source, colormaplen, source->colormap);
435
786
  }
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
6.48k
  for (;;) {
441
6.48k
    c = ReadByte(source);
442
443
6.48k
    if (c == ';')               /* GIF terminator?? */
444
2
      ERREXIT(cinfo, JERR_GIF_IMAGENOTFOUND);
445
446
6.48k
    if (c == '!') {             /* Extension */
447
610
      DoExtension(source);
448
610
      continue;
449
610
    }
450
451
5.87k
    if (c != ',') {             /* Not an image separator? */
452
4.45k
      WARNMS1(cinfo, JWRN_GIF_CHAR, c);
453
4.45k
      continue;
454
4.45k
    }
455
456
    /* Read and decipher Local Image Descriptor */
457
1.41k
    if (!ReadOK(source->pub.input_file, hdrbuf, 9))
458
8
      ERREXIT(cinfo, JERR_INPUT_EOF);
459
    /* we ignore top/left position info, also sort flag */
460
1.41k
    width = LM_to_uint(hdrbuf, 4);
461
1.41k
    height = LM_to_uint(hdrbuf, 6);
462
1.41k
    if (width == 0 || height == 0)
463
4
      ERREXIT(cinfo, JERR_GIF_EMPTY);
464
1.41k
    if (width > JPEG_MAX_DIMENSION || height > JPEG_MAX_DIMENSION)
465
10
      ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
466
1.41k
    if (sinfo->max_pixels &&
467
1.29k
        (unsigned long long)width * height > sinfo->max_pixels)
468
84
      ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
469
1.41k
    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
1.41k
    if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
475
196
      colormaplen = 2 << (hdrbuf[8] & 0x07);
476
196
      ReadColorMap(source, colormaplen, source->colormap);
477
196
    }
478
479
1.41k
    source->input_code_size = ReadByte(source); /* get min-code-size byte */
480
1.41k
    if (source->input_code_size < 2 || source->input_code_size > 8)
481
18
      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
1.41k
    break;
487
5.87k
  }
488
489
  /* Prepare to read selected image: first initialize LZW decompressor */
490
1.81k
  source->symbol_head = (UINT16 *)
491
1.81k
    (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
492
1.81k
                                LZW_TABLE_SIZE * sizeof(UINT16));
493
1.81k
  source->symbol_tail = (UINT8 *)
494
1.81k
    (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
495
1.81k
                                LZW_TABLE_SIZE * sizeof(UINT8));
496
1.81k
  source->symbol_stack = (UINT8 *)
497
1.81k
    (*cinfo->mem->alloc_large) ((j_common_ptr)cinfo, JPOOL_IMAGE,
498
1.81k
                                LZW_TABLE_SIZE * sizeof(UINT8));
499
1.81k
  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
1.81k
  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
812
    source->interlaced_image = (*cinfo->mem->request_virt_sarray)
512
812
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
513
812
       (JDIMENSION)width, (JDIMENSION)height, (JDIMENSION)1);
514
812
    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
812
    source->pub.get_pixel_rows = load_interlaced_image;
519
1.00k
  } else {
520
1.00k
    source->pub.get_pixel_rows = get_pixel_rows;
521
1.00k
  }
522
523
1.81k
  if (cinfo->in_color_space != JCS_GRAYSCALE) {
524
986
    cinfo->in_color_space = JCS_RGB;
525
986
    cinfo->input_components = NUMCOLORS;
526
986
  }
527
528
  /* Create compressor input buffer. */
529
1.81k
  source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
530
1.81k
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
531
1.81k
     (JDIMENSION)width * cinfo->input_components, (JDIMENSION)1);
532
1.81k
  source->pub.buffer_height = 1;
533
534
  /* Pad colormap for safety. */
535
87.3k
  for (c = colormaplen; c < source->clear_code; c++) {
536
85.5k
    source->colormap[CM_RED][c]   =
537
85.5k
    source->colormap[CM_GREEN][c] =
538
85.5k
    source->colormap[CM_BLUE][c]  = _CENTERJSAMPLE;
539
85.5k
  }
540
541
  /* Return info about the image. */
542
1.81k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
543
1.81k
  cinfo->image_width = width;
544
1.81k
  cinfo->image_height = height;
545
546
1.81k
  TRACEMS3(cinfo, 1, JTRC_GIF, width, height, colormaplen);
547
1.81k
}
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
1.73M
{
559
1.73M
  gif_source_ptr source = (gif_source_ptr)sinfo;
560
1.73M
  register int c;
561
1.73M
  register _JSAMPROW ptr;
562
1.73M
  register JDIMENSION col;
563
1.73M
  register _JSAMPARRAY colormap = source->colormap;
564
565
1.73M
  ptr = source->pub._buffer[0];
566
1.73M
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
567
3.22M
    for (col = cinfo->image_width; col > 0; col--) {
568
3.15M
      c = LZWReadByte(source);
569
3.15M
      *ptr++ = colormap[CM_RED][c];
570
3.15M
    }
571
1.65M
  } else {
572
55.1M
    for (col = cinfo->image_width; col > 0; col--) {
573
53.4M
      c = LZWReadByte(source);
574
53.4M
      *ptr++ = colormap[CM_RED][c];
575
53.4M
      *ptr++ = colormap[CM_GREEN][c];
576
53.4M
      *ptr++ = colormap[CM_BLUE][c];
577
53.4M
    }
578
1.65M
  }
579
1.73M
  return 1;
580
1.73M
}
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
748
{
592
748
  gif_source_ptr source = (gif_source_ptr)sinfo;
593
748
  register _JSAMPROW sptr;
594
748
  register JDIMENSION col;
595
748
  JDIMENSION row;
596
748
  cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
597
598
  /* Read the interlaced image into the virtual array we've created. */
599
21.4M
  for (row = 0; row < cinfo->image_height; row++) {
600
21.4M
    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
21.4M
    sptr = *(_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
606
21.4M
      ((j_common_ptr)cinfo, source->interlaced_image, row, (JDIMENSION)1,
607
21.4M
       TRUE);
608
206M
    for (col = cinfo->image_width; col > 0; col--) {
609
184M
      *sptr++ = (_JSAMPLE)LZWReadByte(source);
610
184M
    }
611
21.4M
  }
612
748
  if (progress != NULL)
613
0
    progress->completed_extra_passes++;
614
615
  /* Replace method pointer so subsequent calls don't come here. */
616
748
  source->pub.get_pixel_rows = get_interlaced_row;
617
  /* Initialize for get_interlaced_row, and perform first call on it. */
618
748
  source->cur_row_number = 0;
619
748
  source->pass2_offset = (cinfo->image_height + 7) / 8;
620
748
  source->pass3_offset = source->pass2_offset + (cinfo->image_height + 3) / 8;
621
748
  source->pass4_offset = source->pass3_offset + (cinfo->image_height + 1) / 4;
622
623
748
  return get_interlaced_row(cinfo, sinfo);
624
748
}
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
21.3M
{
636
21.3M
  gif_source_ptr source = (gif_source_ptr)sinfo;
637
21.3M
  register int c;
638
21.3M
  register _JSAMPROW sptr, ptr;
639
21.3M
  register JDIMENSION col;
640
21.3M
  register _JSAMPARRAY colormap = source->colormap;
641
21.3M
  JDIMENSION irow;
642
643
  /* Figure out which row of interlaced image is needed, and access it. */
644
21.3M
  switch ((int)(source->cur_row_number & 7)) {
645
2.67M
  case 0:                       /* first-pass row */
646
2.67M
    irow = source->cur_row_number >> 3;
647
2.67M
    break;
648
2.67M
  case 4:                       /* second-pass row */
649
2.67M
    irow = (source->cur_row_number >> 3) + source->pass2_offset;
650
2.67M
    break;
651
2.67M
  case 2:                       /* third-pass row */
652
5.34M
  case 6:
653
5.34M
    irow = (source->cur_row_number >> 2) + source->pass3_offset;
654
5.34M
    break;
655
10.6M
  default:                      /* fourth-pass row */
656
10.6M
    irow = (source->cur_row_number >> 1) + source->pass4_offset;
657
21.3M
  }
658
21.3M
  sptr = *(_JSAMPARRAY)(*cinfo->mem->access_virt_sarray)
659
21.3M
    ((j_common_ptr)cinfo, source->interlaced_image, irow, (JDIMENSION)1,
660
21.3M
     FALSE);
661
  /* Scan the row, expand colormap, and output */
662
21.3M
  ptr = source->pub._buffer[0];
663
21.3M
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
664
18.1M
    for (col = cinfo->image_width; col > 0; col--) {
665
17.2M
      c = *sptr++;
666
17.2M
      *ptr++ = colormap[CM_RED][c];
667
17.2M
    }
668
20.4M
  } else {
669
187M
    for (col = cinfo->image_width; col > 0; col--) {
670
167M
      c = *sptr++;
671
167M
      *ptr++ = colormap[CM_RED][c];
672
167M
      *ptr++ = colormap[CM_GREEN][c];
673
167M
      *ptr++ = colormap[CM_BLUE][c];
674
167M
    }
675
20.4M
  }
676
21.3M
  source->cur_row_number++;     /* for next time */
677
21.3M
  return 1;
678
21.3M
}
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
823
{
688
  /* no work */
689
823
}
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
1.81k
{
699
1.81k
  gif_source_ptr source;
700
701
1.81k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
702
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
703
704
  /* Create module interface object */
705
1.81k
  source = (gif_source_ptr)
706
1.81k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
707
1.81k
                                sizeof(gif_source_struct));
708
1.81k
  source->cinfo = cinfo;        /* make back link for subroutines */
709
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
710
1.81k
  source->pub.start_input = start_input_gif;
711
1.81k
  source->pub.finish_input = finish_input_gif;
712
1.81k
  source->pub.max_pixels = 0;
713
714
1.81k
  return (cjpeg_source_ptr)source;
715
1.81k
}
jinit_read_gif
Line
Count
Source
698
1.81k
{
699
1.81k
  gif_source_ptr source;
700
701
1.81k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
702
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
703
704
  /* Create module interface object */
705
1.81k
  source = (gif_source_ptr)
706
1.81k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
707
1.81k
                                sizeof(gif_source_struct));
708
1.81k
  source->cinfo = cinfo;        /* make back link for subroutines */
709
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
710
1.81k
  source->pub.start_input = start_input_gif;
711
1.81k
  source->pub.finish_input = finish_input_gif;
712
1.81k
  source->pub.max_pixels = 0;
713
714
1.81k
  return (cjpeg_source_ptr)source;
715
1.81k
}
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)) */