Coverage Report

Created: 2026-04-30 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo/src/jdapimin.c
Line
Count
Source
1
/*
2
 * jdapimin.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1998, Thomas G. Lane.
6
 * Lossless JPEG Modifications:
7
 * Copyright (C) 1999, Ken Murchison.
8
 * libjpeg-turbo Modifications:
9
 * Copyright (C) 2016, 2022, 2024-2025, D. R. Commander.
10
 * For conditions of distribution and use, see the accompanying README.ijg
11
 * file.
12
 *
13
 * This file contains application interface code for the decompression half
14
 * of the JPEG library.  These are the "minimum" API routines that may be
15
 * needed in either the normal full-decompression case or the
16
 * transcoding-only case.
17
 *
18
 * Most of the routines intended to be called directly by an application
19
 * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
20
 * shared by compression and decompression, and jdtrans.c for the transcoding
21
 * case.
22
 */
23
24
#define JPEG_INTERNALS
25
#include "jinclude.h"
26
#include "jpeglib.h"
27
#include "jdmaster.h"
28
#ifdef WITH_SIMD
29
#include "../simd/jsimdconst.h"
30
#endif
31
32
33
/*
34
 * Initialization of a JPEG decompression object.
35
 * The error manager must already be set up (in case memory manager fails).
36
 */
37
38
GLOBAL(void)
39
jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
40
69.3k
{
41
69.3k
  int i;
42
43
  /* Guard against version mismatches between library and caller. */
44
69.3k
  cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */
45
69.3k
  if (version != JPEG_LIB_VERSION)
46
0
    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
47
69.3k
  if (structsize != sizeof(struct jpeg_decompress_struct))
48
0
    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
49
69.3k
             (int)sizeof(struct jpeg_decompress_struct), (int)structsize);
50
51
  /* For debugging purposes, we zero the whole master structure.
52
   * But the application has already set the err pointer, and may have set
53
   * client_data, so we have to save and restore those fields.
54
   * Note: if application hasn't set client_data, tools like Purify may
55
   * complain here.
56
   */
57
69.3k
  {
58
69.3k
    struct jpeg_error_mgr *err = cinfo->err;
59
69.3k
    void *client_data = cinfo->client_data; /* ignore Purify complaint here */
60
69.3k
    memset(cinfo, 0, sizeof(struct jpeg_decompress_struct));
61
69.3k
    cinfo->err = err;
62
69.3k
    cinfo->client_data = client_data;
63
69.3k
  }
64
69.3k
  cinfo->is_decompressor = TRUE;
65
66
  /* Initialize a memory manager instance for this object */
67
69.3k
  jinit_memory_mgr((j_common_ptr)cinfo);
68
69
  /* Zero out pointers to permanent structures. */
70
69.3k
  cinfo->progress = NULL;
71
69.3k
  cinfo->src = NULL;
72
73
346k
  for (i = 0; i < NUM_QUANT_TBLS; i++)
74
277k
    cinfo->quant_tbl_ptrs[i] = NULL;
75
76
346k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
77
277k
    cinfo->dc_huff_tbl_ptrs[i] = NULL;
78
277k
    cinfo->ac_huff_tbl_ptrs[i] = NULL;
79
277k
  }
80
81
  /* Initialize marker processor so application can override methods
82
   * for COM, APPn markers before calling jpeg_read_header.
83
   */
84
69.3k
  cinfo->marker_list = NULL;
85
69.3k
  jinit_marker_reader(cinfo);
86
87
  /* And initialize the overall input controller. */
88
69.3k
  jinit_input_controller(cinfo);
89
90
69.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
91
92
  /* OK, I'm ready */
93
69.3k
  cinfo->global_state = DSTATE_START;
94
95
  /* The master struct is used to store extension parameters, so we allocate it
96
   * here.
97
   */
98
69.3k
  cinfo->master = (struct jpeg_decomp_master *)
99
69.3k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
100
69.3k
                                sizeof(my_decomp_master));
101
69.3k
  memset(cinfo->master, 0, sizeof(my_decomp_master));
102
69.3k
#ifdef WITH_SIMD
103
69.3k
  cinfo->master->simd_support = JSIMD_UNDEFINED;
104
69.3k
  cinfo->master->simd_huffman = 1;
105
69.3k
#endif
106
69.3k
}
107
108
109
/*
110
 * Destruction of a JPEG decompression object
111
 */
112
113
GLOBAL(void)
114
jpeg_destroy_decompress(j_decompress_ptr cinfo)
115
69.3k
{
116
69.3k
  jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
117
69.3k
}
118
119
120
/*
121
 * Abort processing of a JPEG decompression operation,
122
 * but don't destroy the object itself.
123
 */
124
125
GLOBAL(void)
126
jpeg_abort_decompress(j_decompress_ptr cinfo)
127
0
{
128
0
  jpeg_abort((j_common_ptr)cinfo); /* use common routine */
129
0
}
130
131
132
/*
133
 * Set default decompression parameters.
134
 */
135
136
LOCAL(void)
137
default_decompress_parms(j_decompress_ptr cinfo)
138
54.6k
{
139
  /* Guess the input colorspace, and set output colorspace accordingly. */
140
  /* (Wish JPEG committee had provided a real way to specify this...) */
141
  /* Note application may override our guesses. */
142
54.6k
  switch (cinfo->num_components) {
143
14.2k
  case 1:
144
14.2k
    cinfo->jpeg_color_space = JCS_GRAYSCALE;
145
14.2k
    cinfo->out_color_space = JCS_GRAYSCALE;
146
14.2k
    break;
147
148
30.4k
  case 3:
149
30.4k
    if (cinfo->saw_JFIF_marker) {
150
11.1k
      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
151
19.2k
    } else if (cinfo->saw_Adobe_marker) {
152
583
      switch (cinfo->Adobe_transform) {
153
243
      case 0:
154
243
        cinfo->jpeg_color_space = JCS_RGB;
155
243
        break;
156
301
      case 1:
157
301
        cinfo->jpeg_color_space = JCS_YCbCr;
158
301
        break;
159
39
      default:
160
39
        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
161
39
        cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
162
39
        break;
163
583
      }
164
18.6k
    } else {
165
      /* Saw no special markers, try to guess from the component IDs */
166
18.6k
      int cid0 = cinfo->comp_info[0].component_id;
167
18.6k
      int cid1 = cinfo->comp_info[1].component_id;
168
18.6k
      int cid2 = cinfo->comp_info[2].component_id;
169
170
18.6k
      if (cid0 == 1 && cid1 == 2 && cid2 == 3) {
171
9.00k
#ifdef D_LOSSLESS_SUPPORTED
172
9.00k
        if (cinfo->master->lossless)
173
3.50k
          cinfo->jpeg_color_space = JCS_RGB; /* assume RGB w/out marker */
174
5.50k
        else
175
5.50k
#endif
176
5.50k
          cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
177
9.64k
      } else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
178
1.68k
        cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
179
7.95k
      else {
180
7.95k
        TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
181
7.95k
#ifdef D_LOSSLESS_SUPPORTED
182
7.95k
        if (cinfo->master->lossless)
183
7.37k
          cinfo->jpeg_color_space = JCS_RGB; /* assume it's RGB */
184
580
        else
185
580
#endif
186
580
          cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
187
7.95k
      }
188
18.6k
    }
189
    /* Always guess RGB is proper output colorspace. */
190
30.3k
    cinfo->out_color_space = JCS_RGB;
191
30.3k
    break;
192
193
6.74k
  case 4:
194
6.74k
    if (cinfo->saw_Adobe_marker) {
195
1.65k
      switch (cinfo->Adobe_transform) {
196
968
      case 0:
197
968
        cinfo->jpeg_color_space = JCS_CMYK;
198
968
        break;
199
636
      case 2:
200
636
        cinfo->jpeg_color_space = JCS_YCCK;
201
636
        break;
202
49
      default:
203
49
        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
204
49
        cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
205
49
        break;
206
1.65k
      }
207
5.09k
    } else {
208
      /* No special markers, assume straight CMYK. */
209
5.09k
      cinfo->jpeg_color_space = JCS_CMYK;
210
5.09k
    }
211
6.69k
    cinfo->out_color_space = JCS_CMYK;
212
6.69k
    break;
213
214
3.31k
  default:
215
3.31k
    cinfo->jpeg_color_space = JCS_UNKNOWN;
216
3.31k
    cinfo->out_color_space = JCS_UNKNOWN;
217
3.31k
    break;
218
54.6k
  }
219
220
  /* Set defaults for other decompression parameters. */
221
54.6k
  cinfo->scale_num = 1;         /* 1:1 scaling */
222
54.6k
  cinfo->scale_denom = 1;
223
54.6k
  cinfo->output_gamma = 1.0;
224
54.6k
  cinfo->buffered_image = FALSE;
225
54.6k
  cinfo->raw_data_out = FALSE;
226
54.6k
  cinfo->dct_method = JDCT_DEFAULT;
227
54.6k
  cinfo->do_fancy_upsampling = TRUE;
228
54.6k
  cinfo->do_block_smoothing = TRUE;
229
54.6k
  cinfo->quantize_colors = FALSE;
230
  /* We set these in case application only sets quantize_colors. */
231
54.6k
  cinfo->dither_mode = JDITHER_FS;
232
54.6k
#ifdef QUANT_2PASS_SUPPORTED
233
54.6k
  cinfo->two_pass_quantize = TRUE;
234
#else
235
  cinfo->two_pass_quantize = FALSE;
236
#endif
237
54.6k
  cinfo->desired_number_of_colors = 256;
238
54.6k
  cinfo->colormap = NULL;
239
  /* Initialize for no mode change in buffered-image mode. */
240
54.6k
  cinfo->enable_1pass_quant = FALSE;
241
54.6k
  cinfo->enable_external_quant = FALSE;
242
54.6k
  cinfo->enable_2pass_quant = FALSE;
243
54.6k
}
244
245
246
/*
247
 * Decompression startup: read start of JPEG datastream to see what's there.
248
 * Need only initialize JPEG object and supply a data source before calling.
249
 *
250
 * This routine will read as far as the first SOS marker (ie, actual start of
251
 * compressed data), and will save all tables and parameters in the JPEG
252
 * object.  It will also initialize the decompression parameters to default
253
 * values, and finally return JPEG_HEADER_OK.  On return, the application may
254
 * adjust the decompression parameters and then call jpeg_start_decompress.
255
 * (Or, if the application only wanted to determine the image parameters,
256
 * the data need not be decompressed.  In that case, call jpeg_abort or
257
 * jpeg_destroy to release any temporary space.)
258
 * If an abbreviated (tables only) datastream is presented, the routine will
259
 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
260
 * re-use the JPEG object to read the abbreviated image datastream(s).
261
 * It is unnecessary (but OK) to call jpeg_abort in this case.
262
 * The JPEG_SUSPENDED return code only occurs if the data source module
263
 * requests suspension of the decompressor.  In this case the application
264
 * should load more source data and then re-call jpeg_read_header to resume
265
 * processing.
266
 * If a non-suspending data source is used and require_image is TRUE, then the
267
 * return code need not be inspected since only JPEG_HEADER_OK is possible.
268
 *
269
 * This routine is now just a front end to jpeg_consume_input, with some
270
 * extra error checking.
271
 */
272
273
GLOBAL(int)
274
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
275
69.3k
{
276
69.3k
  int retcode;
277
278
69.3k
  if (cinfo->global_state != DSTATE_START &&
279
0
      cinfo->global_state != DSTATE_INHEADER)
280
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
281
282
69.3k
  retcode = jpeg_consume_input(cinfo);
283
284
69.3k
  switch (retcode) {
285
54.6k
  case JPEG_REACHED_SOS:
286
54.6k
    retcode = JPEG_HEADER_OK;
287
54.6k
    break;
288
77
  case JPEG_REACHED_EOI:
289
77
    if (require_image)          /* Complain if application wanted an image */
290
77
      ERREXIT(cinfo, JERR_NO_IMAGE);
291
    /* Reset to start state; it would be safer to require the application to
292
     * call jpeg_abort, but we can't change it now for compatibility reasons.
293
     * A side effect is to free any temporary memory (there shouldn't be any).
294
     */
295
77
    jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */
296
77
    retcode = JPEG_HEADER_TABLES_ONLY;
297
77
    break;
298
0
  case JPEG_SUSPENDED:
299
    /* no work */
300
0
    break;
301
69.3k
  }
302
303
54.6k
  return retcode;
304
69.3k
}
305
306
307
/*
308
 * Consume data in advance of what the decompressor requires.
309
 * This can be called at any time once the decompressor object has
310
 * been created and a data source has been set up.
311
 *
312
 * This routine is essentially a state machine that handles a couple
313
 * of critical state-transition actions, namely initial setup and
314
 * transition from header scanning to ready-for-start_decompress.
315
 * All the actual input is done via the input controller's consume_input
316
 * method.
317
 */
318
319
GLOBAL(int)
320
jpeg_consume_input(j_decompress_ptr cinfo)
321
69.3k
{
322
69.3k
  int retcode = JPEG_SUSPENDED;
323
324
  /* NB: every possible DSTATE value should be listed in this switch */
325
69.3k
  switch (cinfo->global_state) {
326
69.3k
  case DSTATE_START:
327
    /* Start-of-datastream actions: reset appropriate modules */
328
69.3k
    (*cinfo->inputctl->reset_input_controller) (cinfo);
329
    /* Initialize application's data source module */
330
69.3k
    (*cinfo->src->init_source) (cinfo);
331
69.3k
    cinfo->global_state = DSTATE_INHEADER;
332
69.3k
    FALLTHROUGH                 /*FALLTHROUGH*/
333
69.3k
  case DSTATE_INHEADER:
334
69.3k
    retcode = (*cinfo->inputctl->consume_input) (cinfo);
335
69.3k
    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
336
      /* Set up default parameters based on header data */
337
54.6k
      default_decompress_parms(cinfo);
338
      /* Set global state: ready for start_decompress */
339
54.6k
      cinfo->global_state = DSTATE_READY;
340
54.6k
    }
341
69.3k
    break;
342
0
  case DSTATE_READY:
343
    /* Can't advance past first SOS until start_decompress is called */
344
0
    retcode = JPEG_REACHED_SOS;
345
0
    break;
346
0
  case DSTATE_PRELOAD:
347
0
  case DSTATE_PRESCAN:
348
0
  case DSTATE_SCANNING:
349
0
  case DSTATE_RAW_OK:
350
0
  case DSTATE_BUFIMAGE:
351
0
  case DSTATE_BUFPOST:
352
0
  case DSTATE_STOPPING:
353
0
    retcode = (*cinfo->inputctl->consume_input) (cinfo);
354
0
    break;
355
0
  default:
356
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
357
69.3k
  }
358
54.6k
  return retcode;
359
69.3k
}
360
361
362
/*
363
 * Have we finished reading the input file?
364
 */
365
366
GLOBAL(boolean)
367
jpeg_input_complete(j_decompress_ptr cinfo)
368
0
{
369
  /* Check for valid jpeg object */
370
0
  if (cinfo->global_state < DSTATE_START ||
371
0
      cinfo->global_state > DSTATE_STOPPING)
372
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
373
0
  return cinfo->inputctl->eoi_reached;
374
0
}
375
376
377
/*
378
 * Is there more than one scan?
379
 */
380
381
GLOBAL(boolean)
382
jpeg_has_multiple_scans(j_decompress_ptr cinfo)
383
0
{
384
  /* Only valid after jpeg_read_header completes */
385
0
  if (cinfo->global_state < DSTATE_READY ||
386
0
      cinfo->global_state > DSTATE_STOPPING)
387
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
388
0
  return cinfo->inputctl->has_multiple_scans;
389
0
}
390
391
392
/*
393
 * Finish JPEG decompression.
394
 *
395
 * This will normally just verify the file trailer and release temp storage.
396
 *
397
 * Returns FALSE if suspended.  The return value need be inspected only if
398
 * a suspending data source is used.
399
 */
400
401
GLOBAL(boolean)
402
jpeg_finish_decompress(j_decompress_ptr cinfo)
403
15.8k
{
404
15.8k
  if ((cinfo->global_state == DSTATE_SCANNING ||
405
15.8k
       cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) {
406
    /* Terminate final pass of non-buffered mode */
407
15.8k
    if (cinfo->output_scanline < cinfo->output_height)
408
0
      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
409
15.8k
    (*cinfo->master->finish_output_pass) (cinfo);
410
15.8k
    cinfo->global_state = DSTATE_STOPPING;
411
15.8k
  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
412
    /* Finishing after a buffered-image operation */
413
0
    cinfo->global_state = DSTATE_STOPPING;
414
0
  } else if (cinfo->global_state != DSTATE_STOPPING) {
415
    /* STOPPING = repeat call after a suspension, anything else is error */
416
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
417
0
  }
418
  /* Read until EOI */
419
20.7k
  while (!cinfo->inputctl->eoi_reached) {
420
4.87k
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
421
0
      return FALSE;             /* Suspend, come back later */
422
4.87k
  }
423
  /* Do final cleanup */
424
15.8k
  (*cinfo->src->term_source) (cinfo);
425
  /* We can use jpeg_abort to release memory and reset global_state */
426
15.8k
  jpeg_abort((j_common_ptr)cinfo);
427
15.8k
  return TRUE;
428
15.8k
}