Coverage Report

Created: 2026-07-16 06:47

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