4 * Copyright (C) 1994-1995, 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_create_decompress (j_decompress_ptr cinfo)
34 /* For debugging purposes, zero the whole master structure.
35 * But error manager pointer is already there, so save and restore it.
38 struct jpeg_error_mgr * err = cinfo->err;
39 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
42 cinfo->is_decompressor = TRUE;
44 /* Initialize a memory manager instance for this object */
45 jinit_memory_mgr((j_common_ptr) cinfo);
47 /* Zero out pointers to permanent structures. */
48 cinfo->progress = NULL;
51 for (i = 0; i < NUM_QUANT_TBLS; i++)
52 cinfo->quant_tbl_ptrs[i] = NULL;
54 for (i = 0; i < NUM_HUFF_TBLS; i++) {
55 cinfo->dc_huff_tbl_ptrs[i] = NULL;
56 cinfo->ac_huff_tbl_ptrs[i] = NULL;
59 /* Initialize marker processor so application can override methods
60 * for COM, APPn markers before calling jpeg_read_header.
62 jinit_marker_reader(cinfo);
64 /* And initialize the overall input controller. */
65 jinit_input_controller(cinfo);
68 cinfo->global_state = DSTATE_START;
73 * Destruction of a JPEG decompression object
77 jpeg_destroy_decompress (j_decompress_ptr cinfo)
79 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
84 * Abort processing of a JPEG decompression operation,
85 * but don't destroy the object itself.
89 jpeg_abort_decompress (j_decompress_ptr cinfo)
91 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
96 * Install a special processing method for COM or APPn markers.
100 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
101 jpeg_marker_parser_method routine)
103 if (marker_code == JPEG_COM)
104 cinfo->marker->process_COM = routine;
105 else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
106 cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
108 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
113 * Set default decompression parameters.
117 default_decompress_parms (j_decompress_ptr cinfo)
119 /* Guess the input colorspace, and set output colorspace accordingly. */
120 /* (Wish JPEG committee had provided a real way to specify this...) */
121 /* Note application may override our guesses. */
122 switch (cinfo->num_components) {
124 cinfo->jpeg_color_space = JCS_GRAYSCALE;
125 cinfo->out_color_space = JCS_GRAYSCALE;
129 if (cinfo->saw_JFIF_marker) {
130 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
131 } else if (cinfo->saw_Adobe_marker) {
132 switch (cinfo->Adobe_transform) {
134 cinfo->jpeg_color_space = JCS_RGB;
137 cinfo->jpeg_color_space = JCS_YCbCr;
140 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
141 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
145 /* Saw no special markers, try to guess from the component IDs */
146 int cid0 = cinfo->comp_info[0].component_id;
147 int cid1 = cinfo->comp_info[1].component_id;
148 int cid2 = cinfo->comp_info[2].component_id;
150 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
151 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
152 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
153 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
155 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
156 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
159 /* Always guess RGB is proper output colorspace. */
160 cinfo->out_color_space = JCS_RGB;
164 if (cinfo->saw_Adobe_marker) {
165 switch (cinfo->Adobe_transform) {
167 cinfo->jpeg_color_space = JCS_CMYK;
170 cinfo->jpeg_color_space = JCS_YCCK;
173 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
174 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
178 /* No special markers, assume straight CMYK. */
179 cinfo->jpeg_color_space = JCS_CMYK;
181 cinfo->out_color_space = JCS_CMYK;
185 cinfo->jpeg_color_space = JCS_UNKNOWN;
186 cinfo->out_color_space = JCS_UNKNOWN;
190 /* Set defaults for other decompression parameters. */
191 cinfo->scale_num = 1; /* 1:1 scaling */
192 cinfo->scale_denom = 1;
193 cinfo->output_gamma = 1.0;
194 cinfo->buffered_image = FALSE;
195 cinfo->raw_data_out = FALSE;
196 cinfo->dct_method = JDCT_DEFAULT;
197 cinfo->do_fancy_upsampling = TRUE;
198 cinfo->do_block_smoothing = TRUE;
199 cinfo->quantize_colors = FALSE;
200 /* We set these in case application only sets quantize_colors. */
201 cinfo->dither_mode = JDITHER_FS;
202 #ifdef QUANT_2PASS_SUPPORTED
203 cinfo->two_pass_quantize = TRUE;
205 cinfo->two_pass_quantize = FALSE;
207 cinfo->desired_number_of_colors = 256;
208 cinfo->colormap = NULL;
209 /* Initialize for no mode change in buffered-image mode. */
210 cinfo->enable_1pass_quant = FALSE;
211 cinfo->enable_external_quant = FALSE;
212 cinfo->enable_2pass_quant = FALSE;
217 * Decompression startup: read start of JPEG datastream to see what's there.
218 * Need only initialize JPEG object and supply a data source before calling.
220 * This routine will read as far as the first SOS marker (ie, actual start of
221 * compressed data), and will save all tables and parameters in the JPEG
222 * object. It will also initialize the decompression parameters to default
223 * values, and finally return JPEG_HEADER_OK. On return, the application may
224 * adjust the decompression parameters and then call jpeg_start_decompress.
225 * (Or, if the application only wanted to determine the image parameters,
226 * the data need not be decompressed. In that case, call jpeg_abort or
227 * jpeg_destroy to release any temporary space.)
228 * If an abbreviated (tables only) datastream is presented, the routine will
229 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
230 * re-use the JPEG object to read the abbreviated image datastream(s).
231 * It is unnecessary (but OK) to call jpeg_abort in this case.
232 * The JPEG_SUSPENDED return code only occurs if the data source module
233 * requests suspension of the decompressor. In this case the application
234 * should load more source data and then re-call jpeg_read_header to resume
236 * If a non-suspending data source is used and require_image is TRUE, then the
237 * return code need not be inspected since only JPEG_HEADER_OK is possible.
239 * This routine is now just a front end to jpeg_consume_input, with some
240 * extra error checking.
244 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
248 if (cinfo->global_state != DSTATE_START &&
249 cinfo->global_state != DSTATE_INHEADER)
250 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
252 retcode = jpeg_consume_input(cinfo);
255 case JPEG_REACHED_SOS:
256 retcode = JPEG_HEADER_OK;
258 case JPEG_REACHED_EOI:
259 if (require_image) /* Complain if application wanted an image */
260 ERREXIT(cinfo, JERR_NO_IMAGE);
261 /* Reset to start state; it would be safer to require the application to
262 * call jpeg_abort, but we can't change it now for compatibility reasons.
263 * A side effect is to free any temporary memory (there shouldn't be any).
265 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
266 retcode = JPEG_HEADER_TABLES_ONLY;
278 * Consume data in advance of what the decompressor requires.
279 * This can be called at any time once the decompressor object has
280 * been created and a data source has been set up.
282 * This routine is essentially a state machine that handles a couple
283 * of critical state-transition actions, namely initial setup and
284 * transition from header scanning to ready-for-start_decompress.
285 * All the actual input is done via the input controller's consume_input
290 jpeg_consume_input (j_decompress_ptr cinfo)
292 int retcode = JPEG_SUSPENDED;
294 /* NB: every possible DSTATE value should be listed in this switch */
295 switch (cinfo->global_state) {
297 /* Start-of-datastream actions: reset appropriate modules */
298 (*cinfo->inputctl->reset_input_controller) (cinfo);
299 /* Initialize application's data source module */
300 (*cinfo->src->init_source) (cinfo);
301 cinfo->global_state = DSTATE_INHEADER;
303 case DSTATE_INHEADER:
304 retcode = (*cinfo->inputctl->consume_input) (cinfo);
305 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
306 /* Set up default parameters based on header data */
307 default_decompress_parms(cinfo);
308 /* Set global state: ready for start_decompress */
309 cinfo->global_state = DSTATE_READY;
313 /* Can't advance past first SOS until start_decompress is called */
314 retcode = JPEG_REACHED_SOS;
318 case DSTATE_SCANNING:
320 case DSTATE_BUFIMAGE:
322 case DSTATE_STOPPING:
323 retcode = (*cinfo->inputctl->consume_input) (cinfo);
326 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
333 * Have we finished reading the input file?
337 jpeg_input_complete (j_decompress_ptr cinfo)
339 /* Check for valid jpeg object */
340 if (cinfo->global_state < DSTATE_START ||
341 cinfo->global_state > DSTATE_STOPPING)
342 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
343 return cinfo->inputctl->eoi_reached;
348 * Is there more than one scan?
352 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
354 /* Only valid after jpeg_read_header completes */
355 if (cinfo->global_state < DSTATE_READY ||
356 cinfo->global_state > DSTATE_STOPPING)
357 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
358 return cinfo->inputctl->has_multiple_scans;
363 * Finish JPEG decompression.
365 * This will normally just verify the file trailer and release temp storage.
367 * Returns FALSE if suspended. The return value need be inspected only if
368 * a suspending data source is used.
372 jpeg_finish_decompress (j_decompress_ptr cinfo)
374 if ((cinfo->global_state == DSTATE_SCANNING ||
375 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
376 /* Terminate final pass of non-buffered mode */
377 if (cinfo->output_scanline < cinfo->output_height)
378 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
379 (*cinfo->master->finish_output_pass) (cinfo);
380 cinfo->global_state = DSTATE_STOPPING;
381 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
382 /* Finishing after a buffered-image operation */
383 cinfo->global_state = DSTATE_STOPPING;
384 } else if (cinfo->global_state != DSTATE_STOPPING) {
385 /* STOPPING = repeat call after a suspension, anything else is error */
386 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
389 while (! cinfo->inputctl->eoi_reached) {
390 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
391 return FALSE; /* Suspend, come back later */
393 /* Do final cleanup */
394 (*cinfo->src->term_source) (cinfo);
395 /* We can use jpeg_abort to release memory and reset global_state */
396 jpeg_abort((j_common_ptr) cinfo);