Coverage Report

Created: 2026-03-12 07:03

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