Coverage Report

Created: 2026-05-11 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/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
6.64k
{
41
6.64k
  int i;
42
43
  /* Guard against version mismatches between library and caller. */
44
6.64k
  cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */
45
6.64k
  if (version != JPEG_LIB_VERSION)
46
0
    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
47
6.64k
  if (structsize != sizeof(struct jpeg_decompress_struct))
48
0
    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
49
6.64k
             (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
6.64k
  {
58
6.64k
    struct jpeg_error_mgr *err = cinfo->err;
59
6.64k
    void *client_data = cinfo->client_data; /* ignore Purify complaint here */
60
6.64k
    memset(cinfo, 0, sizeof(struct jpeg_decompress_struct));
61
6.64k
    cinfo->err = err;
62
6.64k
    cinfo->client_data = client_data;
63
6.64k
  }
64
6.64k
  cinfo->is_decompressor = TRUE;
65
66
  /* Initialize a memory manager instance for this object */
67
6.64k
  jinit_memory_mgr((j_common_ptr)cinfo);
68
69
  /* Zero out pointers to permanent structures. */
70
6.64k
  cinfo->progress = NULL;
71
6.64k
  cinfo->src = NULL;
72
73
33.2k
  for (i = 0; i < NUM_QUANT_TBLS; i++)
74
26.5k
    cinfo->quant_tbl_ptrs[i] = NULL;
75
76
33.2k
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
77
26.5k
    cinfo->dc_huff_tbl_ptrs[i] = NULL;
78
26.5k
    cinfo->ac_huff_tbl_ptrs[i] = NULL;
79
26.5k
  }
80
81
  /* Initialize marker processor so application can override methods
82
   * for COM, APPn markers before calling jpeg_read_header.
83
   */
84
6.64k
  cinfo->marker_list = NULL;
85
6.64k
  jinit_marker_reader(cinfo);
86
87
  /* And initialize the overall input controller. */
88
6.64k
  jinit_input_controller(cinfo);
89
90
6.64k
  cinfo->data_precision = BITS_IN_JSAMPLE;
91
92
  /* OK, I'm ready */
93
6.64k
  cinfo->global_state = DSTATE_START;
94
95
  /* The master struct is used to store extension parameters, so we allocate it
96
   * here.
97
   */
98
6.64k
  cinfo->master = (struct jpeg_decomp_master *)
99
6.64k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
100
6.64k
                                sizeof(my_decomp_master));
101
6.64k
  memset(cinfo->master, 0, sizeof(my_decomp_master));
102
6.64k
#ifdef WITH_SIMD
103
6.64k
  cinfo->master->simd_support = JSIMD_UNDEFINED;
104
6.64k
  cinfo->master->simd_huffman = 1;
105
6.64k
#endif
106
6.64k
}
107
108
109
/*
110
 * Destruction of a JPEG decompression object
111
 */
112
113
GLOBAL(void)
114
jpeg_destroy_decompress(j_decompress_ptr cinfo)
115
6.64k
{
116
6.64k
  jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
117
6.64k
}
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
3.66k
{
128
3.66k
  jpeg_abort((j_common_ptr)cinfo); /* use common routine */
129
3.66k
}
130
131
132
/*
133
 * Set default decompression parameters.
134
 */
135
136
LOCAL(void)
137
default_decompress_parms(j_decompress_ptr cinfo)
138
18.5k
{
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
18.5k
  switch (cinfo->num_components) {
143
6.56k
  case 1:
144
6.56k
    cinfo->jpeg_color_space = JCS_GRAYSCALE;
145
6.56k
    cinfo->out_color_space = JCS_GRAYSCALE;
146
6.56k
    break;
147
148
10.6k
  case 3:
149
10.6k
    if (cinfo->saw_JFIF_marker) {
150
827
      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
151
9.79k
    } else if (cinfo->saw_Adobe_marker) {
152
501
      switch (cinfo->Adobe_transform) {
153
442
      case 0:
154
442
        cinfo->jpeg_color_space = JCS_RGB;
155
442
        break;
156
16
      case 1:
157
16
        cinfo->jpeg_color_space = JCS_YCbCr;
158
16
        break;
159
43
      default:
160
43
        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
161
43
        cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
162
43
        break;
163
501
      }
164
9.29k
    } else {
165
      /* Saw no special markers, try to guess from the component IDs */
166
9.29k
      int cid0 = cinfo->comp_info[0].component_id;
167
9.29k
      int cid1 = cinfo->comp_info[1].component_id;
168
9.29k
      int cid2 = cinfo->comp_info[2].component_id;
169
170
9.29k
      if (cid0 == 1 && cid1 == 2 && cid2 == 3) {
171
3.30k
#ifdef D_LOSSLESS_SUPPORTED
172
3.30k
        if (cinfo->master->lossless)
173
1
          cinfo->jpeg_color_space = JCS_RGB; /* assume RGB w/out marker */
174
3.30k
        else
175
3.30k
#endif
176
3.30k
          cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
177
5.98k
      } else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
178
41
        cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
179
5.94k
      else {
180
5.94k
        TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
181
5.94k
#ifdef D_LOSSLESS_SUPPORTED
182
5.94k
        if (cinfo->master->lossless)
183
14
          cinfo->jpeg_color_space = JCS_RGB; /* assume it's RGB */
184
5.93k
        else
185
5.93k
#endif
186
5.93k
          cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
187
5.94k
      }
188
9.29k
    }
189
    /* Always guess RGB is proper output colorspace. */
190
10.6k
    cinfo->out_color_space = JCS_RGB;
191
10.6k
    break;
192
193
114
  case 4:
194
114
    if (cinfo->saw_Adobe_marker) {
195
22
      switch (cinfo->Adobe_transform) {
196
1
      case 0:
197
1
        cinfo->jpeg_color_space = JCS_CMYK;
198
1
        break;
199
2
      case 2:
200
2
        cinfo->jpeg_color_space = JCS_YCCK;
201
2
        break;
202
19
      default:
203
19
        WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
204
19
        cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
205
19
        break;
206
22
      }
207
92
    } else {
208
      /* No special markers, assume straight CMYK. */
209
92
      cinfo->jpeg_color_space = JCS_CMYK;
210
92
    }
211
114
    cinfo->out_color_space = JCS_CMYK;
212
114
    break;
213
214
1.20k
  default:
215
1.20k
    cinfo->jpeg_color_space = JCS_UNKNOWN;
216
1.20k
    cinfo->out_color_space = JCS_UNKNOWN;
217
1.20k
    break;
218
18.5k
  }
219
220
  /* Set defaults for other decompression parameters. */
221
18.5k
  cinfo->scale_num = 1;         /* 1:1 scaling */
222
18.5k
  cinfo->scale_denom = 1;
223
18.5k
  cinfo->output_gamma = 1.0;
224
18.5k
  cinfo->buffered_image = FALSE;
225
18.5k
  cinfo->raw_data_out = FALSE;
226
18.5k
  cinfo->dct_method = JDCT_DEFAULT;
227
18.5k
  cinfo->do_fancy_upsampling = TRUE;
228
18.5k
  cinfo->do_block_smoothing = TRUE;
229
18.5k
  cinfo->quantize_colors = FALSE;
230
  /* We set these in case application only sets quantize_colors. */
231
18.5k
  cinfo->dither_mode = JDITHER_FS;
232
18.5k
#ifdef QUANT_2PASS_SUPPORTED
233
18.5k
  cinfo->two_pass_quantize = TRUE;
234
#else
235
  cinfo->two_pass_quantize = FALSE;
236
#endif
237
18.5k
  cinfo->desired_number_of_colors = 256;
238
18.5k
  cinfo->colormap = NULL;
239
  /* Initialize for no mode change in buffered-image mode. */
240
18.5k
  cinfo->enable_1pass_quant = FALSE;
241
18.5k
  cinfo->enable_external_quant = FALSE;
242
18.5k
  cinfo->enable_2pass_quant = FALSE;
243
18.5k
}
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
20.1k
{
276
20.1k
  int retcode;
277
278
20.1k
  if (cinfo->global_state != DSTATE_START &&
279
328
      cinfo->global_state != DSTATE_INHEADER)
280
328
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
281
282
20.1k
  retcode = jpeg_consume_input(cinfo);
283
284
20.1k
  switch (retcode) {
285
18.5k
  case JPEG_REACHED_SOS:
286
18.5k
    retcode = JPEG_HEADER_OK;
287
18.5k
    break;
288
636
  case JPEG_REACHED_EOI:
289
636
    if (require_image)          /* Complain if application wanted an image */
290
636
      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
636
    jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */
296
636
    retcode = JPEG_HEADER_TABLES_ONLY;
297
636
    break;
298
10
  case JPEG_SUSPENDED:
299
    /* no work */
300
10
    break;
301
20.1k
  }
302
303
18.5k
  return retcode;
304
20.1k
}
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
10.7M
{
322
10.7M
  int retcode = JPEG_SUSPENDED;
323
324
  /* NB: every possible DSTATE value should be listed in this switch */
325
10.7M
  switch (cinfo->global_state) {
326
19.7k
  case DSTATE_START:
327
    /* Start-of-datastream actions: reset appropriate modules */
328
19.7k
    (*cinfo->inputctl->reset_input_controller) (cinfo);
329
    /* Initialize application's data source module */
330
19.7k
    (*cinfo->src->init_source) (cinfo);
331
19.7k
    cinfo->global_state = DSTATE_INHEADER;
332
19.7k
    FALLTHROUGH                 /*FALLTHROUGH*/
333
19.7k
  case DSTATE_INHEADER:
334
19.7k
    retcode = (*cinfo->inputctl->consume_input) (cinfo);
335
19.7k
    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
336
      /* Set up default parameters based on header data */
337
18.5k
      default_decompress_parms(cinfo);
338
      /* Set global state: ready for start_decompress */
339
18.5k
      cinfo->global_state = DSTATE_READY;
340
18.5k
    }
341
19.7k
    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
10.7M
  case DSTATE_BUFIMAGE:
351
10.7M
  case DSTATE_BUFPOST:
352
10.7M
  case DSTATE_STOPPING:
353
10.7M
    retcode = (*cinfo->inputctl->consume_input) (cinfo);
354
10.7M
    break;
355
0
  default:
356
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
357
10.7M
  }
358
10.7M
  return retcode;
359
10.7M
}
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
39.4k
{
369
  /* Check for valid jpeg object */
370
39.4k
  if (cinfo->global_state < DSTATE_START ||
371
39.4k
      cinfo->global_state > DSTATE_STOPPING)
372
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
373
39.4k
  return cinfo->inputctl->eoi_reached;
374
39.4k
}
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
18.4k
{
384
  /* Only valid after jpeg_read_header completes */
385
18.4k
  if (cinfo->global_state < DSTATE_READY ||
386
18.4k
      cinfo->global_state > DSTATE_STOPPING)
387
2
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
388
18.4k
  return cinfo->inputctl->has_multiple_scans;
389
18.4k
}
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
16.8k
{
404
16.8k
  if ((cinfo->global_state == DSTATE_SCANNING ||
405
11.4k
       cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) {
406
    /* Terminate final pass of non-buffered mode */
407
5.35k
    if (cinfo->output_scanline < cinfo->output_height)
408
0
      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
409
5.35k
    (*cinfo->master->finish_output_pass) (cinfo);
410
5.35k
    cinfo->global_state = DSTATE_STOPPING;
411
11.4k
  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
412
    /* Finishing after a buffered-image operation */
413
11.4k
    cinfo->global_state = DSTATE_STOPPING;
414
11.4k
  } else if (cinfo->global_state != DSTATE_STOPPING) {
415
    /* STOPPING = repeat call after a suspension, anything else is error */
416
60
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
417
60
  }
418
  /* Read until EOI */
419
84.3k
  while (!cinfo->inputctl->eoi_reached) {
420
67.9k
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
421
435
      return FALSE;             /* Suspend, come back later */
422
67.9k
  }
423
  /* Do final cleanup */
424
16.3k
  (*cinfo->src->term_source) (cinfo);
425
  /* We can use jpeg_abort to release memory and reset global_state */
426
16.3k
  jpeg_abort((j_common_ptr)cinfo);
427
16.3k
  return TRUE;
428
16.8k
}