Coverage Report

Created: 2025-10-10 07:04

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