Coverage Report

Created: 2026-05-30 06:16

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