4 * Copyright (C) 1994-1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains application interface code for the decompression half
9 * of the JPEG library. These are the "minimum" API routines that may be
10 * needed in either the normal full-decompression case or the
11 * transcoding-only case.
13 * Most of the routines intended to be called directly by an application
14 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
15 * shared by compression and decompression, and jdtrans.c for the transcoding
19 #define JPEG_INTERNALS
25 * Initialization of a JPEG decompression object.
26 * The error manager must already be set up (in case memory manager fails).
30 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
34 /* Guard against version mismatches between library and caller. */
35 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
36 if (version != JPEG_LIB_VERSION)
37 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
38 if (structsize != SIZEOF(struct jpeg_decompress_struct))
39 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
40 (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
42 /* For debugging purposes, we zero the whole master structure.
43 * But the application has already set the err pointer, and may have set
44 * client_data, so we have to save and restore those fields.
45 * Note: if application hasn't set client_data, tools like Purify may
49 struct jpeg_error_mgr * err = cinfo->err;
50 void * client_data = cinfo->client_data; /* ignore Purify complaint here */
51 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
53 cinfo->client_data = client_data;
55 cinfo->is_decompressor = TRUE;
57 /* Initialize a memory manager instance for this object */
58 jinit_memory_mgr((j_common_ptr) cinfo);
60 /* Zero out pointers to permanent structures. */
61 cinfo->progress = NULL;
64 for (i = 0; i < NUM_QUANT_TBLS; i++)
65 cinfo->quant_tbl_ptrs[i] = NULL;
67 for (i = 0; i < NUM_HUFF_TBLS; i++) {
68 cinfo->dc_huff_tbl_ptrs[i] = NULL;
69 cinfo->ac_huff_tbl_ptrs[i] = NULL;
72 /* Initialize marker processor so application can override methods
73 * for COM, APPn markers before calling jpeg_read_header.
75 cinfo->marker_list = NULL;
76 jinit_marker_reader(cinfo);
78 /* And initialize the overall input controller. */
79 jinit_input_controller(cinfo);
82 cinfo->global_state = DSTATE_START;
87 * Destruction of a JPEG decompression object
91 jpeg_destroy_decompress (j_decompress_ptr cinfo)
93 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
98 * Abort processing of a JPEG decompression operation,
99 * but don't destroy the object itself.
103 jpeg_abort_decompress (j_decompress_ptr cinfo)
105 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
110 * Set default decompression parameters.
114 default_decompress_parms (j_decompress_ptr cinfo)
116 /* Guess the input colorspace, and set output colorspace accordingly. */
117 /* (Wish JPEG committee had provided a real way to specify this...) */
118 /* Note application may override our guesses. */
119 switch (cinfo->num_components) {
121 cinfo->jpeg_color_space = JCS_GRAYSCALE;
122 cinfo->out_color_space = JCS_GRAYSCALE;
126 if (cinfo->saw_JFIF_marker) {
127 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
128 } else if (cinfo->saw_Adobe_marker) {
129 switch (cinfo->Adobe_transform) {
131 cinfo->jpeg_color_space = JCS_RGB;
134 cinfo->jpeg_color_space = JCS_YCbCr;
137 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
138 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
142 /* Saw no special markers, try to guess from the component IDs */
143 int cid0 = cinfo->comp_info[0].component_id;
144 int cid1 = cinfo->comp_info[1].component_id;
145 int cid2 = cinfo->comp_info[2].component_id;
147 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
148 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
149 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
150 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
152 if (cinfo->process == JPROC_LOSSLESS) {
153 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_LOSSLESS_IDS, cid0, cid1, cid2);
154 cinfo->jpeg_color_space = JCS_RGB; /* assume it's RGB */
156 else { /* Lossy processes */
157 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_LOSSY_IDS, cid0, cid1, cid2);
158 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
162 /* Always guess RGB is proper output colorspace. */
163 cinfo->out_color_space = JCS_RGB;
167 if (cinfo->saw_Adobe_marker) {
168 switch (cinfo->Adobe_transform) {
170 cinfo->jpeg_color_space = JCS_CMYK;
173 cinfo->jpeg_color_space = JCS_YCCK;
176 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
177 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
181 /* No special markers, assume straight CMYK. */
182 cinfo->jpeg_color_space = JCS_CMYK;
184 cinfo->out_color_space = JCS_CMYK;
188 cinfo->jpeg_color_space = JCS_UNKNOWN;
189 cinfo->out_color_space = JCS_UNKNOWN;
193 /* Set defaults for other decompression parameters. */
194 cinfo->scale_num = 1; /* 1:1 scaling */
195 cinfo->scale_denom = 1;
196 cinfo->output_gamma = 1.0;
197 cinfo->buffered_image = FALSE;
198 cinfo->raw_data_out = FALSE;
199 cinfo->dct_method = JDCT_DEFAULT;
200 cinfo->do_fancy_upsampling = TRUE;
201 cinfo->do_block_smoothing = TRUE;
202 cinfo->quantize_colors = FALSE;
203 /* We set these in case application only sets quantize_colors. */
204 cinfo->dither_mode = JDITHER_FS;
205 #ifdef QUANT_2PASS_SUPPORTED
206 cinfo->two_pass_quantize = TRUE;
208 cinfo->two_pass_quantize = FALSE;
210 cinfo->desired_number_of_colors = 256;
211 cinfo->colormap = NULL;
212 /* Initialize for no mode change in buffered-image mode. */
213 cinfo->enable_1pass_quant = FALSE;
214 cinfo->enable_external_quant = FALSE;
215 cinfo->enable_2pass_quant = FALSE;
220 * Decompression startup: read start of JPEG datastream to see what's there.
221 * Need only initialize JPEG object and supply a data source before calling.
223 * This routine will read as far as the first SOS marker (ie, actual start of
224 * compressed data), and will save all tables and parameters in the JPEG
225 * object. It will also initialize the decompression parameters to default
226 * values, and finally return JPEG_HEADER_OK. On return, the application may
227 * adjust the decompression parameters and then call jpeg_start_decompress.
228 * (Or, if the application only wanted to determine the image parameters,
229 * the data need not be decompressed. In that case, call jpeg_abort or
230 * jpeg_destroy to release any temporary space.)
231 * If an abbreviated (tables only) datastream is presented, the routine will
232 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
233 * re-use the JPEG object to read the abbreviated image datastream(s).
234 * It is unnecessary (but OK) to call jpeg_abort in this case.
235 * The JPEG_SUSPENDED return code only occurs if the data source module
236 * requests suspension of the decompressor. In this case the application
237 * should load more source data and then re-call jpeg_read_header to resume
239 * If a non-suspending data source is used and require_image is TRUE, then the
240 * return code need not be inspected since only JPEG_HEADER_OK is possible.
242 * This routine is now just a front end to jpeg_consume_input, with some
243 * extra error checking.
247 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
251 if (cinfo->global_state != DSTATE_START &&
252 cinfo->global_state != DSTATE_INHEADER)
253 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
255 retcode = jpeg_consume_input(cinfo);
258 case JPEG_REACHED_SOS:
259 retcode = JPEG_HEADER_OK;
261 case JPEG_REACHED_EOI:
262 if (require_image) /* Complain if application wanted an image */
263 ERREXIT(cinfo, JERR_NO_IMAGE);
264 /* Reset to start state; it would be safer to require the application to
265 * call jpeg_abort, but we can't change it now for compatibility reasons.
266 * A side effect is to free any temporary memory (there shouldn't be any).
268 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
269 retcode = JPEG_HEADER_TABLES_ONLY;
281 * Consume data in advance of what the decompressor requires.
282 * This can be called at any time once the decompressor object has
283 * been created and a data source has been set up.
285 * This routine is essentially a state machine that handles a couple
286 * of critical state-transition actions, namely initial setup and
287 * transition from header scanning to ready-for-start_decompress.
288 * All the actual input is done via the input controller's consume_input
293 jpeg_consume_input (j_decompress_ptr cinfo)
295 int retcode = JPEG_SUSPENDED;
297 /* NB: every possible DSTATE value should be listed in this switch */
298 switch (cinfo->global_state) {
300 /* Start-of-datastream actions: reset appropriate modules */
301 (*cinfo->inputctl->reset_input_controller) (cinfo);
302 /* Initialize application's data source module */
303 (*cinfo->src->init_source) (cinfo);
304 cinfo->global_state = DSTATE_INHEADER;
306 case DSTATE_INHEADER:
307 retcode = (*cinfo->inputctl->consume_input) (cinfo);
308 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
309 /* Set up default parameters based on header data */
310 default_decompress_parms(cinfo);
311 /* Set global state: ready for start_decompress */
312 cinfo->global_state = DSTATE_READY;
316 /* Can't advance past first SOS until start_decompress is called */
317 retcode = JPEG_REACHED_SOS;
321 case DSTATE_SCANNING:
323 case DSTATE_BUFIMAGE:
325 case DSTATE_STOPPING:
326 retcode = (*cinfo->inputctl->consume_input) (cinfo);
329 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
336 * Have we finished reading the input file?
340 jpeg_input_complete (j_decompress_ptr cinfo)
342 /* Check for valid jpeg object */
343 if (cinfo->global_state < DSTATE_START ||
344 cinfo->global_state > DSTATE_STOPPING)
345 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
346 return cinfo->inputctl->eoi_reached;
351 * Is there more than one scan?
355 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
357 /* Only valid after jpeg_read_header completes */
358 if (cinfo->global_state < DSTATE_READY ||
359 cinfo->global_state > DSTATE_STOPPING)
360 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
361 return cinfo->inputctl->has_multiple_scans;
366 * Finish JPEG decompression.
368 * This will normally just verify the file trailer and release temp storage.
370 * Returns FALSE if suspended. The return value need be inspected only if
371 * a suspending data source is used.
375 jpeg_finish_decompress (j_decompress_ptr cinfo)
377 if ((cinfo->global_state == DSTATE_SCANNING ||
378 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
379 /* Terminate final pass of non-buffered mode */
380 if (cinfo->output_scanline < cinfo->output_height)
381 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
382 (*cinfo->master->finish_output_pass) (cinfo);
383 cinfo->global_state = DSTATE_STOPPING;
384 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
385 /* Finishing after a buffered-image operation */
386 cinfo->global_state = DSTATE_STOPPING;
387 } else if (cinfo->global_state != DSTATE_STOPPING) {
388 /* STOPPING = repeat call after a suspension, anything else is error */
389 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
392 while (! cinfo->inputctl->eoi_reached) {
393 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
394 return FALSE; /* Suspend, come back later */
396 /* Do final cleanup */
397 (*cinfo->src->term_source) (cinfo);
398 /* We can use jpeg_abort to release memory and reset global_state */
399 jpeg_abort((j_common_ptr) cinfo);