4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1998, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2016, 2022, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
11 * This file contains application interface code for the decompression half
12 * of the JPEG library. These are the "minimum" API routines that may be
13 * needed in either the normal full-decompression case or the
14 * transcoding-only case.
16 * Most of the routines intended to be called directly by an application
17 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
18 * shared by compression and decompression, and jdtrans.c for the transcoding
22 #define JPEG_INTERNALS
26 #include "jconfigint.h"
30 * Initialization of a JPEG decompression object.
31 * The error manager must already be set up (in case memory manager fails).
35 jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
39 /* Guard against version mismatches between library and caller. */
40 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
41 if (version != JPEG_LIB_VERSION)
42 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
43 if (structsize != sizeof(struct jpeg_decompress_struct))
44 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
45 (int)sizeof(struct jpeg_decompress_struct), (int)structsize);
47 /* For debugging purposes, we zero the whole master structure.
48 * But the application has already set the err pointer, and may have set
49 * client_data, so we have to save and restore those fields.
50 * Note: if application hasn't set client_data, tools like Purify may
54 struct jpeg_error_mgr *err = cinfo->err;
55 void *client_data = cinfo->client_data; /* ignore Purify complaint here */
56 memset(cinfo, 0, sizeof(struct jpeg_decompress_struct));
58 cinfo->client_data = client_data;
60 cinfo->is_decompressor = TRUE;
62 /* Initialize a memory manager instance for this object */
63 jinit_memory_mgr((j_common_ptr)cinfo);
65 /* Zero out pointers to permanent structures. */
66 cinfo->progress = NULL;
69 for (i = 0; i < NUM_QUANT_TBLS; i++)
70 cinfo->quant_tbl_ptrs[i] = NULL;
72 for (i = 0; i < NUM_HUFF_TBLS; i++) {
73 cinfo->dc_huff_tbl_ptrs[i] = NULL;
74 cinfo->ac_huff_tbl_ptrs[i] = NULL;
77 /* Initialize marker processor so application can override methods
78 * for COM, APPn markers before calling jpeg_read_header.
80 cinfo->marker_list = NULL;
81 jinit_marker_reader(cinfo);
83 /* And initialize the overall input controller. */
84 jinit_input_controller(cinfo);
87 cinfo->global_state = DSTATE_START;
89 /* The master struct is used to store extension parameters, so we allocate it
92 cinfo->master = (struct jpeg_decomp_master *)
93 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT,
94 sizeof(my_decomp_master));
95 memset(cinfo->master, 0, sizeof(my_decomp_master));
100 * Destruction of a JPEG decompression object
104 jpeg_destroy_decompress(j_decompress_ptr cinfo)
106 jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
111 * Abort processing of a JPEG decompression operation,
112 * but don't destroy the object itself.
116 jpeg_abort_decompress(j_decompress_ptr cinfo)
118 jpeg_abort((j_common_ptr)cinfo); /* use common routine */
123 * Set default decompression parameters.
127 default_decompress_parms(j_decompress_ptr cinfo)
129 /* Guess the input colorspace, and set output colorspace accordingly. */
130 /* (Wish JPEG committee had provided a real way to specify this...) */
131 /* Note application may override our guesses. */
132 switch (cinfo->num_components) {
134 cinfo->jpeg_color_space = JCS_GRAYSCALE;
135 cinfo->out_color_space = JCS_GRAYSCALE;
139 if (cinfo->saw_JFIF_marker) {
140 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
141 } else if (cinfo->saw_Adobe_marker) {
142 switch (cinfo->Adobe_transform) {
144 cinfo->jpeg_color_space = JCS_RGB;
147 cinfo->jpeg_color_space = JCS_YCbCr;
150 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
151 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
155 /* Saw no special markers, try to guess from the component IDs */
156 int cid0 = cinfo->comp_info[0].component_id;
157 int cid1 = cinfo->comp_info[1].component_id;
158 int cid2 = cinfo->comp_info[2].component_id;
160 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
161 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
162 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
163 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
165 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
166 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
169 /* Always guess RGB is proper output colorspace. */
170 cinfo->out_color_space = JCS_RGB;
174 if (cinfo->saw_Adobe_marker) {
175 switch (cinfo->Adobe_transform) {
177 cinfo->jpeg_color_space = JCS_CMYK;
180 cinfo->jpeg_color_space = JCS_YCCK;
183 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
184 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
188 /* No special markers, assume straight CMYK. */
189 cinfo->jpeg_color_space = JCS_CMYK;
191 cinfo->out_color_space = JCS_CMYK;
195 cinfo->jpeg_color_space = JCS_UNKNOWN;
196 cinfo->out_color_space = JCS_UNKNOWN;
200 /* Set defaults for other decompression parameters. */
201 cinfo->scale_num = 1; /* 1:1 scaling */
202 cinfo->scale_denom = 1;
203 cinfo->output_gamma = 1.0;
204 cinfo->buffered_image = FALSE;
205 cinfo->raw_data_out = FALSE;
206 cinfo->dct_method = JDCT_DEFAULT;
207 cinfo->do_fancy_upsampling = TRUE;
208 cinfo->do_block_smoothing = TRUE;
209 cinfo->quantize_colors = FALSE;
210 /* We set these in case application only sets quantize_colors. */
211 cinfo->dither_mode = JDITHER_FS;
212 #ifdef QUANT_2PASS_SUPPORTED
213 cinfo->two_pass_quantize = TRUE;
215 cinfo->two_pass_quantize = FALSE;
217 cinfo->desired_number_of_colors = 256;
218 cinfo->colormap = NULL;
219 /* Initialize for no mode change in buffered-image mode. */
220 cinfo->enable_1pass_quant = FALSE;
221 cinfo->enable_external_quant = FALSE;
222 cinfo->enable_2pass_quant = FALSE;
227 * Decompression startup: read start of JPEG datastream to see what's there.
228 * Need only initialize JPEG object and supply a data source before calling.
230 * This routine will read as far as the first SOS marker (ie, actual start of
231 * compressed data), and will save all tables and parameters in the JPEG
232 * object. It will also initialize the decompression parameters to default
233 * values, and finally return JPEG_HEADER_OK. On return, the application may
234 * adjust the decompression parameters and then call jpeg_start_decompress.
235 * (Or, if the application only wanted to determine the image parameters,
236 * the data need not be decompressed. In that case, call jpeg_abort or
237 * jpeg_destroy to release any temporary space.)
238 * If an abbreviated (tables only) datastream is presented, the routine will
239 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
240 * re-use the JPEG object to read the abbreviated image datastream(s).
241 * It is unnecessary (but OK) to call jpeg_abort in this case.
242 * The JPEG_SUSPENDED return code only occurs if the data source module
243 * requests suspension of the decompressor. In this case the application
244 * should load more source data and then re-call jpeg_read_header to resume
246 * If a non-suspending data source is used and require_image is TRUE, then the
247 * return code need not be inspected since only JPEG_HEADER_OK is possible.
249 * This routine is now just a front end to jpeg_consume_input, with some
250 * extra error checking.
254 jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
258 if (cinfo->global_state != DSTATE_START &&
259 cinfo->global_state != DSTATE_INHEADER)
260 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
262 retcode = jpeg_consume_input(cinfo);
265 case JPEG_REACHED_SOS:
266 retcode = JPEG_HEADER_OK;
268 case JPEG_REACHED_EOI:
269 if (require_image) /* Complain if application wanted an image */
270 ERREXIT(cinfo, JERR_NO_IMAGE);
271 /* Reset to start state; it would be safer to require the application to
272 * call jpeg_abort, but we can't change it now for compatibility reasons.
273 * A side effect is to free any temporary memory (there shouldn't be any).
275 jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */
276 retcode = JPEG_HEADER_TABLES_ONLY;
288 * Consume data in advance of what the decompressor requires.
289 * This can be called at any time once the decompressor object has
290 * been created and a data source has been set up.
292 * This routine is essentially a state machine that handles a couple
293 * of critical state-transition actions, namely initial setup and
294 * transition from header scanning to ready-for-start_decompress.
295 * All the actual input is done via the input controller's consume_input
300 jpeg_consume_input(j_decompress_ptr cinfo)
302 int retcode = JPEG_SUSPENDED;
304 /* NB: every possible DSTATE value should be listed in this switch */
305 switch (cinfo->global_state) {
307 /* Start-of-datastream actions: reset appropriate modules */
308 (*cinfo->inputctl->reset_input_controller) (cinfo);
309 /* Initialize application's data source module */
310 (*cinfo->src->init_source) (cinfo);
311 cinfo->global_state = DSTATE_INHEADER;
312 FALLTHROUGH /*FALLTHROUGH*/
313 case DSTATE_INHEADER:
314 retcode = (*cinfo->inputctl->consume_input) (cinfo);
315 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
316 /* Set up default parameters based on header data */
317 default_decompress_parms(cinfo);
318 /* Set global state: ready for start_decompress */
319 cinfo->global_state = DSTATE_READY;
323 /* Can't advance past first SOS until start_decompress is called */
324 retcode = JPEG_REACHED_SOS;
328 case DSTATE_SCANNING:
330 case DSTATE_BUFIMAGE:
332 case DSTATE_STOPPING:
333 retcode = (*cinfo->inputctl->consume_input) (cinfo);
336 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
343 * Have we finished reading the input file?
347 jpeg_input_complete(j_decompress_ptr cinfo)
349 /* Check for valid jpeg object */
350 if (cinfo->global_state < DSTATE_START ||
351 cinfo->global_state > DSTATE_STOPPING)
352 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
353 return cinfo->inputctl->eoi_reached;
358 * Is there more than one scan?
362 jpeg_has_multiple_scans(j_decompress_ptr cinfo)
364 /* Only valid after jpeg_read_header completes */
365 if (cinfo->global_state < DSTATE_READY ||
366 cinfo->global_state > DSTATE_STOPPING)
367 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
368 return cinfo->inputctl->has_multiple_scans;
373 * Finish JPEG decompression.
375 * This will normally just verify the file trailer and release temp storage.
377 * Returns FALSE if suspended. The return value need be inspected only if
378 * a suspending data source is used.
382 jpeg_finish_decompress(j_decompress_ptr cinfo)
384 if ((cinfo->global_state == DSTATE_SCANNING ||
385 cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) {
386 /* Terminate final pass of non-buffered mode */
387 if (cinfo->output_scanline < cinfo->output_height)
388 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
389 (*cinfo->master->finish_output_pass) (cinfo);
390 cinfo->global_state = DSTATE_STOPPING;
391 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
392 /* Finishing after a buffered-image operation */
393 cinfo->global_state = DSTATE_STOPPING;
394 } else if (cinfo->global_state != DSTATE_STOPPING) {
395 /* STOPPING = repeat call after a suspension, anything else is error */
396 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
399 while (!cinfo->inputctl->eoi_reached) {
400 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
401 return FALSE; /* Suspend, come back later */
403 /* Do final cleanup */
404 (*cinfo->src->term_source) (cinfo);
405 /* We can use jpeg_abort to release memory and reset global_state */
406 jpeg_abort((j_common_ptr)cinfo);