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
21 #include "radiant_jpeglib.h"
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 i = sizeof(struct jpeg_decompress_struct);
40 i = SIZEOF(struct jpeg_decompress_struct);
41 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
44 cinfo->is_decompressor = TRUE;
46 /* Initialize a memory manager instance for this object */
47 jinit_memory_mgr((j_common_ptr) cinfo);
49 /* Zero out pointers to permanent structures. */
50 cinfo->progress = NULL;
53 for (i = 0; i < NUM_QUANT_TBLS; i++)
54 cinfo->quant_tbl_ptrs[i] = NULL;
56 for (i = 0; i < NUM_HUFF_TBLS; i++) {
57 cinfo->dc_huff_tbl_ptrs[i] = NULL;
58 cinfo->ac_huff_tbl_ptrs[i] = NULL;
61 /* Initialize marker processor so application can override methods
62 * for COM, APPn markers before calling jpeg_read_header.
64 jinit_marker_reader(cinfo);
66 /* And initialize the overall input controller. */
67 jinit_input_controller(cinfo);
70 cinfo->global_state = DSTATE_START;
75 * Destruction of a JPEG decompression object
79 jpeg_destroy_decompress (j_decompress_ptr cinfo)
81 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
86 * Abort processing of a JPEG decompression operation,
87 * but don't destroy the object itself.
91 jpeg_abort_decompress (j_decompress_ptr cinfo)
93 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
98 * Install a special processing method for COM or APPn markers.
102 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
103 jpeg_marker_parser_method routine)
105 if (marker_code == JPEG_COM)
106 cinfo->marker->process_COM = routine;
107 else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
108 cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
110 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
115 * Set default decompression parameters.
119 default_decompress_parms (j_decompress_ptr cinfo)
121 /* Guess the input colorspace, and set output colorspace accordingly. */
122 /* (Wish JPEG committee had provided a real way to specify this...) */
123 /* Note application may override our guesses. */
124 switch (cinfo->num_components) {
126 cinfo->jpeg_color_space = JCS_GRAYSCALE;
127 cinfo->out_color_space = JCS_GRAYSCALE;
131 if (cinfo->saw_JFIF_marker) {
132 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
133 } else if (cinfo->saw_Adobe_marker) {
134 switch (cinfo->Adobe_transform) {
136 cinfo->jpeg_color_space = JCS_RGB;
139 cinfo->jpeg_color_space = JCS_YCbCr;
142 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
143 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
147 /* Saw no special markers, try to guess from the component IDs */
148 int cid0 = cinfo->comp_info[0].component_id;
149 int cid1 = cinfo->comp_info[1].component_id;
150 int cid2 = cinfo->comp_info[2].component_id;
152 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
153 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
154 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
155 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
157 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
158 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
161 /* Always guess RGB is proper output colorspace. */
162 cinfo->out_color_space = JCS_RGB;
166 if (cinfo->saw_Adobe_marker) {
167 switch (cinfo->Adobe_transform) {
169 cinfo->jpeg_color_space = JCS_CMYK;
172 cinfo->jpeg_color_space = JCS_YCCK;
175 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
176 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
180 /* No special markers, assume straight CMYK. */
181 cinfo->jpeg_color_space = JCS_CMYK;
183 cinfo->out_color_space = JCS_CMYK;
187 cinfo->jpeg_color_space = JCS_UNKNOWN;
188 cinfo->out_color_space = JCS_UNKNOWN;
192 /* Set defaults for other decompression parameters. */
193 cinfo->scale_num = 1; /* 1:1 scaling */
194 cinfo->scale_denom = 1;
195 cinfo->output_gamma = 1.0;
196 cinfo->buffered_image = FALSE;
197 cinfo->raw_data_out = FALSE;
198 cinfo->dct_method = JDCT_DEFAULT;
199 cinfo->do_fancy_upsampling = TRUE;
200 cinfo->do_block_smoothing = TRUE;
201 cinfo->quantize_colors = FALSE;
202 /* We set these in case application only sets quantize_colors. */
203 cinfo->dither_mode = JDITHER_FS;
204 #ifdef QUANT_2PASS_SUPPORTED
205 cinfo->two_pass_quantize = TRUE;
207 cinfo->two_pass_quantize = FALSE;
209 cinfo->desired_number_of_colors = 256;
210 cinfo->colormap = NULL;
211 /* Initialize for no mode change in buffered-image mode. */
212 cinfo->enable_1pass_quant = FALSE;
213 cinfo->enable_external_quant = FALSE;
214 cinfo->enable_2pass_quant = FALSE;
219 * Decompression startup: read start of JPEG datastream to see what's there.
220 * Need only initialize JPEG object and supply a data source before calling.
222 * This routine will read as far as the first SOS marker (ie, actual start of
223 * compressed data), and will save all tables and parameters in the JPEG
224 * object. It will also initialize the decompression parameters to default
225 * values, and finally return JPEG_HEADER_OK. On return, the application may
226 * adjust the decompression parameters and then call jpeg_start_decompress.
227 * (Or, if the application only wanted to determine the image parameters,
228 * the data need not be decompressed. In that case, call jpeg_abort or
229 * jpeg_destroy to release any temporary space.)
230 * If an abbreviated (tables only) datastream is presented, the routine will
231 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
232 * re-use the JPEG object to read the abbreviated image datastream(s).
233 * It is unnecessary (but OK) to call jpeg_abort in this case.
234 * The JPEG_SUSPENDED return code only occurs if the data source module
235 * requests suspension of the decompressor. In this case the application
236 * should load more source data and then re-call jpeg_read_header to resume
238 * If a non-suspending data source is used and require_image is TRUE, then the
239 * return code need not be inspected since only JPEG_HEADER_OK is possible.
241 * This routine is now just a front end to jpeg_consume_input, with some
242 * extra error checking.
246 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
250 if (cinfo->global_state != DSTATE_START &&
251 cinfo->global_state != DSTATE_INHEADER)
252 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
254 retcode = jpeg_consume_input(cinfo);
257 case JPEG_REACHED_SOS:
258 retcode = JPEG_HEADER_OK;
260 case JPEG_REACHED_EOI:
261 if (require_image) /* Complain if application wanted an image */
262 ERREXIT(cinfo, JERR_NO_IMAGE);
263 /* Reset to start state; it would be safer to require the application to
264 * call jpeg_abort, but we can't change it now for compatibility reasons.
265 * A side effect is to free any temporary memory (there shouldn't be any).
267 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
268 retcode = JPEG_HEADER_TABLES_ONLY;
280 * Consume data in advance of what the decompressor requires.
281 * This can be called at any time once the decompressor object has
282 * been created and a data source has been set up.
284 * This routine is essentially a state machine that handles a couple
285 * of critical state-transition actions, namely initial setup and
286 * transition from header scanning to ready-for-start_decompress.
287 * All the actual input is done via the input controller's consume_input
292 jpeg_consume_input (j_decompress_ptr cinfo)
294 int retcode = JPEG_SUSPENDED;
296 /* NB: every possible DSTATE value should be listed in this switch */
297 switch (cinfo->global_state) {
299 /* Start-of-datastream actions: reset appropriate modules */
300 (*cinfo->inputctl->reset_input_controller) (cinfo);
301 /* Initialize application's data source module */
302 (*cinfo->src->init_source) (cinfo);
303 cinfo->global_state = DSTATE_INHEADER;
305 case DSTATE_INHEADER:
306 retcode = (*cinfo->inputctl->consume_input) (cinfo);
307 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
308 /* Set up default parameters based on header data */
309 default_decompress_parms(cinfo);
310 /* Set global state: ready for start_decompress */
311 cinfo->global_state = DSTATE_READY;
315 /* Can't advance past first SOS until start_decompress is called */
316 retcode = JPEG_REACHED_SOS;
320 case DSTATE_SCANNING:
322 case DSTATE_BUFIMAGE:
324 case DSTATE_STOPPING:
325 retcode = (*cinfo->inputctl->consume_input) (cinfo);
328 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
335 * Have we finished reading the input file?
339 jpeg_input_complete (j_decompress_ptr cinfo)
341 /* Check for valid jpeg object */
342 if (cinfo->global_state < DSTATE_START ||
343 cinfo->global_state > DSTATE_STOPPING)
344 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
345 return cinfo->inputctl->eoi_reached;
350 * Is there more than one scan?
354 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
356 /* Only valid after jpeg_read_header completes */
357 if (cinfo->global_state < DSTATE_READY ||
358 cinfo->global_state > DSTATE_STOPPING)
359 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
360 return cinfo->inputctl->has_multiple_scans;
365 * Finish JPEG decompression.
367 * This will normally just verify the file trailer and release temp storage.
369 * Returns FALSE if suspended. The return value need be inspected only if
370 * a suspending data source is used.
374 jpeg_finish_decompress (j_decompress_ptr cinfo)
376 if ((cinfo->global_state == DSTATE_SCANNING ||
377 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
378 /* Terminate final pass of non-buffered mode */
379 if (cinfo->output_scanline < cinfo->output_height)
380 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
381 (*cinfo->master->finish_output_pass) (cinfo);
382 cinfo->global_state = DSTATE_STOPPING;
383 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
384 /* Finishing after a buffered-image operation */
385 cinfo->global_state = DSTATE_STOPPING;
386 } else if (cinfo->global_state != DSTATE_STOPPING) {
387 /* STOPPING = repeat call after a suspension, anything else is error */
388 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
391 while (! cinfo->inputctl->eoi_reached) {
392 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
393 return FALSE; /* Suspend, come back later */
395 /* Do final cleanup */
396 (*cinfo->src->term_source) (cinfo);
397 /* We can use jpeg_abort to release memory and reset global_state */
398 jpeg_abort((j_common_ptr) cinfo);