Coverage Report

Created: 2025-06-24 07:01

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