2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdapimin.c /main/2 1996/05/09 03:46:12 drk $ */
27 * Copyright (C) 1994-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains application interface code for the decompression half
32 * of the JPEG library. These are the "minimum" API routines that may be
33 * needed in either the normal full-decompression case or the
34 * transcoding-only case.
36 * Most of the routines intended to be called directly by an application
37 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
38 * shared by compression and decompression, and jdtrans.c for the transcoding
42 #define JPEG_INTERNALS
48 * Initialization of a JPEG decompression object.
49 * The error manager must already be set up (in case memory manager fails).
53 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
57 /* Guard against version mismatches between library and caller. */
58 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
59 if (version != JPEG_LIB_VERSION)
60 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
61 if (structsize != SIZEOF(struct jpeg_decompress_struct))
62 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
63 (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
65 /* For debugging purposes, zero the whole master structure.
66 * But error manager pointer is already there, so save and restore it.
69 struct jpeg_error_mgr * err = cinfo->err;
70 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
73 cinfo->is_decompressor = TRUE;
75 /* Initialize a memory manager instance for this object */
76 jinit_memory_mgr((j_common_ptr) cinfo);
78 /* Zero out pointers to permanent structures. */
79 cinfo->progress = NULL;
82 for (i = 0; i < NUM_QUANT_TBLS; i++)
83 cinfo->quant_tbl_ptrs[i] = NULL;
85 for (i = 0; i < NUM_HUFF_TBLS; i++) {
86 cinfo->dc_huff_tbl_ptrs[i] = NULL;
87 cinfo->ac_huff_tbl_ptrs[i] = NULL;
90 /* Initialize marker processor so application can override methods
91 * for COM, APPn markers before calling jpeg_read_header.
93 jinit_marker_reader(cinfo);
95 /* And initialize the overall input controller. */
96 jinit_input_controller(cinfo);
99 cinfo->global_state = DSTATE_START;
104 * Destruction of a JPEG decompression object
108 jpeg_destroy_decompress (j_decompress_ptr cinfo)
110 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
115 * Abort processing of a JPEG decompression operation,
116 * but don't destroy the object itself.
120 jpeg_abort_decompress (j_decompress_ptr cinfo)
122 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
127 * Install a special processing method for COM or APPn markers.
131 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
132 jpeg_marker_parser_method routine)
134 if (marker_code == JPEG_COM)
135 cinfo->marker->process_COM = routine;
136 else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
137 cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
139 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
144 * Set default decompression parameters.
148 default_decompress_parms (j_decompress_ptr cinfo)
150 /* Guess the input colorspace, and set output colorspace accordingly. */
151 /* (Wish JPEG committee had provided a real way to specify this...) */
152 /* Note application may override our guesses. */
153 switch (cinfo->num_components) {
155 cinfo->jpeg_color_space = JCS_GRAYSCALE;
156 cinfo->out_color_space = JCS_GRAYSCALE;
160 if (cinfo->saw_JFIF_marker) {
161 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
162 } else if (cinfo->saw_Adobe_marker) {
163 switch (cinfo->Adobe_transform) {
165 cinfo->jpeg_color_space = JCS_RGB;
168 cinfo->jpeg_color_space = JCS_YCbCr;
171 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
172 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
176 /* Saw no special markers, try to guess from the component IDs */
177 int cid0 = cinfo->comp_info[0].component_id;
178 int cid1 = cinfo->comp_info[1].component_id;
179 int cid2 = cinfo->comp_info[2].component_id;
181 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
182 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
183 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
184 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
186 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
187 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
190 /* Always guess RGB is proper output colorspace. */
191 cinfo->out_color_space = JCS_RGB;
195 if (cinfo->saw_Adobe_marker) {
196 switch (cinfo->Adobe_transform) {
198 cinfo->jpeg_color_space = JCS_CMYK;
201 cinfo->jpeg_color_space = JCS_YCCK;
204 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
205 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
209 /* No special markers, assume straight CMYK. */
210 cinfo->jpeg_color_space = JCS_CMYK;
212 cinfo->out_color_space = JCS_CMYK;
216 cinfo->jpeg_color_space = JCS_UNKNOWN;
217 cinfo->out_color_space = JCS_UNKNOWN;
221 /* Set defaults for other decompression parameters. */
222 cinfo->scale_num = 1; /* 1:1 scaling */
223 cinfo->scale_denom = 1;
224 cinfo->output_gamma = 1.0;
225 cinfo->buffered_image = FALSE;
226 cinfo->raw_data_out = FALSE;
227 cinfo->dct_method = JDCT_DEFAULT;
228 cinfo->do_fancy_upsampling = TRUE;
229 cinfo->do_block_smoothing = TRUE;
230 cinfo->quantize_colors = FALSE;
231 /* We set these in case application only sets quantize_colors. */
232 cinfo->dither_mode = JDITHER_FS;
233 #ifdef QUANT_2PASS_SUPPORTED
234 cinfo->two_pass_quantize = TRUE;
236 cinfo->two_pass_quantize = FALSE;
238 cinfo->desired_number_of_colors = 256;
239 cinfo->colormap = NULL;
240 /* Initialize for no mode change in buffered-image mode. */
241 cinfo->enable_1pass_quant = FALSE;
242 cinfo->enable_external_quant = FALSE;
243 cinfo->enable_2pass_quant = FALSE;
248 * Decompression startup: read start of JPEG datastream to see what's there.
249 * Need only initialize JPEG object and supply a data source before calling.
251 * This routine will read as far as the first SOS marker (ie, actual start of
252 * compressed data), and will save all tables and parameters in the JPEG
253 * object. It will also initialize the decompression parameters to default
254 * values, and finally return JPEG_HEADER_OK. On return, the application may
255 * adjust the decompression parameters and then call jpeg_start_decompress.
256 * (Or, if the application only wanted to determine the image parameters,
257 * the data need not be decompressed. In that case, call jpeg_abort or
258 * jpeg_destroy to release any temporary space.)
259 * If an abbreviated (tables only) datastream is presented, the routine will
260 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
261 * re-use the JPEG object to read the abbreviated image datastream(s).
262 * It is unnecessary (but OK) to call jpeg_abort in this case.
263 * The JPEG_SUSPENDED return code only occurs if the data source module
264 * requests suspension of the decompressor. In this case the application
265 * should load more source data and then re-call jpeg_read_header to resume
267 * If a non-suspending data source is used and require_image is TRUE, then the
268 * return code need not be inspected since only JPEG_HEADER_OK is possible.
270 * This routine is now just a front end to jpeg_consume_input, with some
271 * extra error checking.
275 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
279 if (cinfo->global_state != DSTATE_START &&
280 cinfo->global_state != DSTATE_INHEADER)
281 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
283 retcode = jpeg_consume_input(cinfo);
286 case JPEG_REACHED_SOS:
287 retcode = JPEG_HEADER_OK;
289 case JPEG_REACHED_EOI:
290 if (require_image) /* Complain if application wanted an image */
291 ERREXIT(cinfo, JERR_NO_IMAGE);
292 /* Reset to start state; it would be safer to require the application to
293 * call jpeg_abort, but we can't change it now for compatibility reasons.
294 * A side effect is to free any temporary memory (there shouldn't be any).
296 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
297 retcode = JPEG_HEADER_TABLES_ONLY;
309 * Consume data in advance of what the decompressor requires.
310 * This can be called at any time once the decompressor object has
311 * been created and a data source has been set up.
313 * This routine is essentially a state machine that handles a couple
314 * of critical state-transition actions, namely initial setup and
315 * transition from header scanning to ready-for-start_decompress.
316 * All the actual input is done via the input controller's consume_input
321 jpeg_consume_input (j_decompress_ptr cinfo)
323 int retcode = JPEG_SUSPENDED;
325 /* NB: every possible DSTATE value should be listed in this switch */
326 switch (cinfo->global_state) {
328 /* Start-of-datastream actions: reset appropriate modules */
329 (*cinfo->inputctl->reset_input_controller) (cinfo);
330 /* Initialize application's data source module */
331 (*cinfo->src->init_source) (cinfo);
332 cinfo->global_state = DSTATE_INHEADER;
334 case DSTATE_INHEADER:
335 retcode = (*cinfo->inputctl->consume_input) (cinfo);
336 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
337 /* Set up default parameters based on header data */
338 default_decompress_parms(cinfo);
339 /* Set global state: ready for start_decompress */
340 cinfo->global_state = DSTATE_READY;
344 /* Can't advance past first SOS until start_decompress is called */
345 retcode = JPEG_REACHED_SOS;
349 case DSTATE_SCANNING:
351 case DSTATE_BUFIMAGE:
353 case DSTATE_STOPPING:
354 retcode = (*cinfo->inputctl->consume_input) (cinfo);
357 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
364 * Have we finished reading the input file?
368 jpeg_input_complete (j_decompress_ptr cinfo)
370 /* Check for valid jpeg object */
371 if (cinfo->global_state < DSTATE_START ||
372 cinfo->global_state > DSTATE_STOPPING)
373 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
374 return cinfo->inputctl->eoi_reached;
379 * Is there more than one scan?
383 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
385 /* Only valid after jpeg_read_header completes */
386 if (cinfo->global_state < DSTATE_READY ||
387 cinfo->global_state > DSTATE_STOPPING)
388 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
389 return cinfo->inputctl->has_multiple_scans;
394 * Finish JPEG decompression.
396 * This will normally just verify the file trailer and release temp storage.
398 * Returns FALSE if suspended. The return value need be inspected only if
399 * a suspending data source is used.
403 jpeg_finish_decompress (j_decompress_ptr cinfo)
405 if ((cinfo->global_state == DSTATE_SCANNING ||
406 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
407 /* Terminate final pass of non-buffered mode */
408 if (cinfo->output_scanline < cinfo->output_height)
409 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
410 (*cinfo->master->finish_output_pass) (cinfo);
411 cinfo->global_state = DSTATE_STOPPING;
412 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
413 /* Finishing after a buffered-image operation */
414 cinfo->global_state = DSTATE_STOPPING;
415 } else if (cinfo->global_state != DSTATE_STOPPING) {
416 /* STOPPING = repeat call after a suspension, anything else is error */
417 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
420 while (! cinfo->inputctl->eoi_reached) {
421 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
422 return FALSE; /* Suspend, come back later */
424 /* Do final cleanup */
425 (*cinfo->src->term_source) (cinfo);
426 /* We can use jpeg_abort to release memory and reset global_state */
427 jpeg_abort((j_common_ptr) cinfo);