4 * Copyright (C) 1994-1998, Thomas G. Lane.
5 * Modified 2009 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains application interface code for the decompression half
10 * of the JPEG library. These are the "minimum" API routines that may be
11 * needed in either the normal full-decompression case or the
12 * transcoding-only case.
14 * Most of the routines intended to be called directly by an application
15 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
16 * shared by compression and decompression, and jdtrans.c for the transcoding
20 #define JPEG_INTERNALS
26 * Initialization of a JPEG decompression object.
27 * The error manager must already be set up (in case memory manager fails).
31 jpeg_CreateDecompress (j_decompress_ptr cinfo
, int version
, size_t structsize
)
35 /* Guard against version mismatches between library and caller. */
36 cinfo
->mem
= NULL
; /* so jpeg_destroy knows mem mgr not called */
37 if (version
!= JPEG_LIB_VERSION
)
38 ERREXIT2(cinfo
, JERR_BAD_LIB_VERSION
, JPEG_LIB_VERSION
, version
);
39 if (structsize
!= SIZEOF(struct jpeg_decompress_struct
))
40 ERREXIT2(cinfo
, JERR_BAD_STRUCT_SIZE
,
41 (int) SIZEOF(struct jpeg_decompress_struct
), (int) structsize
);
43 /* For debugging purposes, we zero the whole master structure.
44 * But the application has already set the err pointer, and may have set
45 * client_data, so we have to save and restore those fields.
46 * Note: if application hasn't set client_data, tools like Purify may
50 struct jpeg_error_mgr
* err
= cinfo
->err
;
51 void * client_data
= cinfo
->client_data
; /* ignore Purify complaint here */
52 MEMZERO(cinfo
, SIZEOF(struct jpeg_decompress_struct
));
54 cinfo
->client_data
= client_data
;
56 cinfo
->is_decompressor
= TRUE
;
58 /* Initialize a memory manager instance for this object */
59 jinit_memory_mgr((j_common_ptr
) cinfo
);
61 /* Zero out pointers to permanent structures. */
62 cinfo
->progress
= NULL
;
65 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++)
66 cinfo
->quant_tbl_ptrs
[i
] = NULL
;
68 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
69 cinfo
->dc_huff_tbl_ptrs
[i
] = NULL
;
70 cinfo
->ac_huff_tbl_ptrs
[i
] = NULL
;
73 /* Initialize marker processor so application can override methods
74 * for COM, APPn markers before calling jpeg_read_header.
76 cinfo
->marker_list
= NULL
;
77 jinit_marker_reader(cinfo
);
79 /* And initialize the overall input controller. */
80 jinit_input_controller(cinfo
);
83 cinfo
->global_state
= DSTATE_START
;
88 * Destruction of a JPEG decompression object
92 jpeg_destroy_decompress (j_decompress_ptr cinfo
)
94 jpeg_destroy((j_common_ptr
) cinfo
); /* use common routine */
99 * Abort processing of a JPEG decompression operation,
100 * but don't destroy the object itself.
104 jpeg_abort_decompress (j_decompress_ptr cinfo
)
106 jpeg_abort((j_common_ptr
) cinfo
); /* use common routine */
111 * Set default decompression parameters.
115 default_decompress_parms (j_decompress_ptr cinfo
)
117 /* Guess the input colorspace, and set output colorspace accordingly. */
118 /* (Wish JPEG committee had provided a real way to specify this...) */
119 /* Note application may override our guesses. */
120 switch (cinfo
->num_components
) {
122 cinfo
->jpeg_color_space
= JCS_GRAYSCALE
;
123 cinfo
->out_color_space
= JCS_GRAYSCALE
;
127 if (cinfo
->saw_JFIF_marker
) {
128 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* JFIF implies YCbCr */
129 } else if (cinfo
->saw_Adobe_marker
) {
130 switch (cinfo
->Adobe_transform
) {
132 cinfo
->jpeg_color_space
= JCS_RGB
;
135 cinfo
->jpeg_color_space
= JCS_YCbCr
;
138 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
139 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
143 /* Saw no special markers, try to guess from the component IDs */
144 int cid0
= cinfo
->comp_info
[0].component_id
;
145 int cid1
= cinfo
->comp_info
[1].component_id
;
146 int cid2
= cinfo
->comp_info
[2].component_id
;
148 if (cid0
== 1 && cid1
== 2 && cid2
== 3)
149 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume JFIF w/out marker */
150 else if (cid0
== 82 && cid1
== 71 && cid2
== 66)
151 cinfo
->jpeg_color_space
= JCS_RGB
; /* ASCII 'R', 'G', 'B' */
153 TRACEMS3(cinfo
, 1, JTRC_UNKNOWN_IDS
, cid0
, cid1
, cid2
);
154 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
157 /* Always guess RGB is proper output colorspace. */
158 cinfo
->out_color_space
= JCS_RGB
;
162 if (cinfo
->saw_Adobe_marker
) {
163 switch (cinfo
->Adobe_transform
) {
165 cinfo
->jpeg_color_space
= JCS_CMYK
;
168 cinfo
->jpeg_color_space
= JCS_YCCK
;
171 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
172 cinfo
->jpeg_color_space
= JCS_YCCK
; /* assume it's YCCK */
176 /* No special markers, assume straight CMYK. */
177 cinfo
->jpeg_color_space
= JCS_CMYK
;
179 cinfo
->out_color_space
= JCS_CMYK
;
183 cinfo
->jpeg_color_space
= JCS_UNKNOWN
;
184 cinfo
->out_color_space
= JCS_UNKNOWN
;
188 /* Set defaults for other decompression parameters. */
189 cinfo
->scale_num
= cinfo
->block_size
; /* 1:1 scaling */
190 cinfo
->scale_denom
= cinfo
->block_size
;
191 cinfo
->output_gamma
= 1.0;
192 cinfo
->buffered_image
= FALSE
;
193 cinfo
->raw_data_out
= FALSE
;
194 cinfo
->dct_method
= JDCT_DEFAULT
;
195 cinfo
->do_fancy_upsampling
= TRUE
;
196 cinfo
->do_block_smoothing
= TRUE
;
197 cinfo
->quantize_colors
= FALSE
;
198 /* We set these in case application only sets quantize_colors. */
199 cinfo
->dither_mode
= JDITHER_FS
;
200 #ifdef QUANT_2PASS_SUPPORTED
201 cinfo
->two_pass_quantize
= TRUE
;
203 cinfo
->two_pass_quantize
= FALSE
;
205 cinfo
->desired_number_of_colors
= 256;
206 cinfo
->colormap
= NULL
;
207 /* Initialize for no mode change in buffered-image mode. */
208 cinfo
->enable_1pass_quant
= FALSE
;
209 cinfo
->enable_external_quant
= FALSE
;
210 cinfo
->enable_2pass_quant
= FALSE
;
215 * Decompression startup: read start of JPEG datastream to see what's there.
216 * Need only initialize JPEG object and supply a data source before calling.
218 * This routine will read as far as the first SOS marker (ie, actual start of
219 * compressed data), and will save all tables and parameters in the JPEG
220 * object. It will also initialize the decompression parameters to default
221 * values, and finally return JPEG_HEADER_OK. On return, the application may
222 * adjust the decompression parameters and then call jpeg_start_decompress.
223 * (Or, if the application only wanted to determine the image parameters,
224 * the data need not be decompressed. In that case, call jpeg_abort or
225 * jpeg_destroy to release any temporary space.)
226 * If an abbreviated (tables only) datastream is presented, the routine will
227 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
228 * re-use the JPEG object to read the abbreviated image datastream(s).
229 * It is unnecessary (but OK) to call jpeg_abort in this case.
230 * The JPEG_SUSPENDED return code only occurs if the data source module
231 * requests suspension of the decompressor. In this case the application
232 * should load more source data and then re-call jpeg_read_header to resume
234 * If a non-suspending data source is used and require_image is TRUE, then the
235 * return code need not be inspected since only JPEG_HEADER_OK is possible.
237 * This routine is now just a front end to jpeg_consume_input, with some
238 * extra error checking.
242 jpeg_read_header (j_decompress_ptr cinfo
, boolean require_image
)
246 if (cinfo
->global_state
!= DSTATE_START
&&
247 cinfo
->global_state
!= DSTATE_INHEADER
)
248 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
250 retcode
= jpeg_consume_input(cinfo
);
253 case JPEG_REACHED_SOS
:
254 retcode
= JPEG_HEADER_OK
;
256 case JPEG_REACHED_EOI
:
257 if (require_image
) /* Complain if application wanted an image */
258 ERREXIT(cinfo
, JERR_NO_IMAGE
);
259 /* Reset to start state; it would be safer to require the application to
260 * call jpeg_abort, but we can't change it now for compatibility reasons.
261 * A side effect is to free any temporary memory (there shouldn't be any).
263 jpeg_abort((j_common_ptr
) cinfo
); /* sets state = DSTATE_START */
264 retcode
= JPEG_HEADER_TABLES_ONLY
;
276 * Consume data in advance of what the decompressor requires.
277 * This can be called at any time once the decompressor object has
278 * been created and a data source has been set up.
280 * This routine is essentially a state machine that handles a couple
281 * of critical state-transition actions, namely initial setup and
282 * transition from header scanning to ready-for-start_decompress.
283 * All the actual input is done via the input controller's consume_input
288 jpeg_consume_input (j_decompress_ptr cinfo
)
290 int retcode
= JPEG_SUSPENDED
;
292 /* NB: every possible DSTATE value should be listed in this switch */
293 switch (cinfo
->global_state
) {
295 /* Start-of-datastream actions: reset appropriate modules */
296 (*cinfo
->inputctl
->reset_input_controller
) (cinfo
);
297 /* Initialize application's data source module */
298 (*cinfo
->src
->init_source
) (cinfo
);
299 cinfo
->global_state
= DSTATE_INHEADER
;
301 case DSTATE_INHEADER
:
302 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
303 if (retcode
== JPEG_REACHED_SOS
) { /* Found SOS, prepare to decompress */
304 /* Set up default parameters based on header data */
305 default_decompress_parms(cinfo
);
306 /* Set global state: ready for start_decompress */
307 cinfo
->global_state
= DSTATE_READY
;
311 /* Can't advance past first SOS until start_decompress is called */
312 retcode
= JPEG_REACHED_SOS
;
316 case DSTATE_SCANNING
:
318 case DSTATE_BUFIMAGE
:
320 case DSTATE_STOPPING
:
321 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
324 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
331 * Have we finished reading the input file?
335 jpeg_input_complete (j_decompress_ptr cinfo
)
337 /* Check for valid jpeg object */
338 if (cinfo
->global_state
< DSTATE_START
||
339 cinfo
->global_state
> DSTATE_STOPPING
)
340 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
341 return cinfo
->inputctl
->eoi_reached
;
346 * Is there more than one scan?
350 jpeg_has_multiple_scans (j_decompress_ptr cinfo
)
352 /* Only valid after jpeg_read_header completes */
353 if (cinfo
->global_state
< DSTATE_READY
||
354 cinfo
->global_state
> DSTATE_STOPPING
)
355 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
356 return cinfo
->inputctl
->has_multiple_scans
;
361 * Finish JPEG decompression.
363 * This will normally just verify the file trailer and release temp storage.
365 * Returns FALSE if suspended. The return value need be inspected only if
366 * a suspending data source is used.
370 jpeg_finish_decompress (j_decompress_ptr cinfo
)
372 if ((cinfo
->global_state
== DSTATE_SCANNING
||
373 cinfo
->global_state
== DSTATE_RAW_OK
) && ! cinfo
->buffered_image
) {
374 /* Terminate final pass of non-buffered mode */
375 if (cinfo
->output_scanline
< cinfo
->output_height
)
376 ERREXIT(cinfo
, JERR_TOO_LITTLE_DATA
);
377 (*cinfo
->master
->finish_output_pass
) (cinfo
);
378 cinfo
->global_state
= DSTATE_STOPPING
;
379 } else if (cinfo
->global_state
== DSTATE_BUFIMAGE
) {
380 /* Finishing after a buffered-image operation */
381 cinfo
->global_state
= DSTATE_STOPPING
;
382 } else if (cinfo
->global_state
!= DSTATE_STOPPING
) {
383 /* STOPPING = repeat call after a suspension, anything else is error */
384 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
387 while (! cinfo
->inputctl
->eoi_reached
) {
388 if ((*cinfo
->inputctl
->consume_input
) (cinfo
) == JPEG_SUSPENDED
)
389 return FALSE
; /* Suspend, come back later */
391 /* Do final cleanup */
392 (*cinfo
->src
->term_source
) (cinfo
);
393 /* We can use jpeg_abort to release memory and reset global_state */
394 jpeg_abort((j_common_ptr
) cinfo
);