8 * Copyright (C) 1994-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains application interface code for the decompression half
13 * of the JPEG library. These are the "minimum" API routines that may be
14 * needed in either the normal full-decompression case or the
15 * transcoding-only case.
17 * Most of the routines intended to be called directly by an application
18 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
19 * shared by compression and decompression, and jdtrans.c for the transcoding
23 #define JPEG_INTERNALS
29 * Initialization of a JPEG decompression object.
30 * The error manager must already be set up (in case memory manager fails).
34 jpeg_CreateDecompress (j_decompress_ptr cinfo
, int version
, size_t structsize
)
38 /* Guard against version mismatches between library and caller. */
39 cinfo
->mem
= NULL
; /* so jpeg_destroy knows mem mgr not called */
40 if (version
!= JPEG_LIB_VERSION
)
41 ERREXIT2(cinfo
, JERR_BAD_LIB_VERSION
, JPEG_LIB_VERSION
, version
);
42 if (structsize
!= SIZEOF(struct jpeg_decompress_struct
))
43 ERREXIT2(cinfo
, JERR_BAD_STRUCT_SIZE
,
44 (int) SIZEOF(struct jpeg_decompress_struct
), (int) structsize
);
46 /* For debugging purposes, we zero the whole master structure.
47 * But the application has already set the err pointer, and may have set
48 * client_data, so we have to save and restore those fields.
49 * Note: if application hasn't set client_data, tools like Purify may
53 struct jpeg_error_mgr
* err
= cinfo
->err
;
54 void * client_data
= cinfo
->client_data
; /* ignore Purify complaint here */
55 MEMZERO(cinfo
, SIZEOF(struct jpeg_decompress_struct
));
57 cinfo
->client_data
= client_data
;
59 cinfo
->is_decompressor
= TRUE
;
61 /* Initialize a memory manager instance for this object */
62 jinit_memory_mgr((j_common_ptr
) cinfo
);
64 /* Zero out pointers to permanent structures. */
65 cinfo
->progress
= NULL
;
68 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++)
69 cinfo
->quant_tbl_ptrs
[i
] = NULL
;
71 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
72 cinfo
->dc_huff_tbl_ptrs
[i
] = NULL
;
73 cinfo
->ac_huff_tbl_ptrs
[i
] = NULL
;
76 /* Initialize marker processor so application can override methods
77 * for COM, APPn markers before calling jpeg_read_header.
79 cinfo
->marker_list
= NULL
;
80 jinit_marker_reader(cinfo
);
82 /* And initialize the overall input controller. */
83 jinit_input_controller(cinfo
);
86 cinfo
->global_state
= DSTATE_START
;
91 * Destruction of a JPEG decompression object
95 jpeg_destroy_decompress (j_decompress_ptr cinfo
)
97 jpeg_destroy((j_common_ptr
) cinfo
); /* use common routine */
102 * Abort processing of a JPEG decompression operation,
103 * but don't destroy the object itself.
107 jpeg_abort_decompress (j_decompress_ptr cinfo
)
109 jpeg_abort((j_common_ptr
) cinfo
); /* use common routine */
114 * Set default decompression parameters.
118 default_decompress_parms (j_decompress_ptr cinfo
)
120 /* Guess the input colorspace, and set output colorspace accordingly. */
121 /* (Wish JPEG committee had provided a real way to specify this...) */
122 /* Note application may override our guesses. */
123 switch (cinfo
->num_components
) {
125 cinfo
->jpeg_color_space
= JCS_GRAYSCALE
;
126 cinfo
->out_color_space
= JCS_GRAYSCALE
;
130 if (cinfo
->saw_JFIF_marker
) {
131 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* JFIF implies YCbCr */
132 } else if (cinfo
->saw_Adobe_marker
) {
133 switch (cinfo
->Adobe_transform
) {
135 cinfo
->jpeg_color_space
= JCS_RGB
;
138 cinfo
->jpeg_color_space
= JCS_YCbCr
;
141 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
142 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
146 /* Saw no special markers, try to guess from the component IDs */
147 int cid0
= cinfo
->comp_info
[0].component_id
;
148 int cid1
= cinfo
->comp_info
[1].component_id
;
149 int cid2
= cinfo
->comp_info
[2].component_id
;
151 if (cid0
== 1 && cid1
== 2 && cid2
== 3)
152 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume JFIF w/out marker */
153 else if (cid0
== 82 && cid1
== 71 && cid2
== 66)
154 cinfo
->jpeg_color_space
= JCS_RGB
; /* ASCII 'R', 'G', 'B' */
156 if (cinfo
->process
== JPROC_LOSSLESS
) {
157 TRACEMS3(cinfo
, 1, JTRC_UNKNOWN_LOSSLESS_IDS
, cid0
, cid1
, cid2
);
158 cinfo
->jpeg_color_space
= JCS_RGB
; /* assume it's RGB */
160 else { /* Lossy processes */
161 TRACEMS3(cinfo
, 1, JTRC_UNKNOWN_LOSSY_IDS
, cid0
, cid1
, cid2
);
162 cinfo
->jpeg_color_space
= JCS_YCbCr
; /* assume it's YCbCr */
166 /* Always guess RGB is proper output colorspace. */
167 cinfo
->out_color_space
= JCS_RGB
;
171 if (cinfo
->saw_Adobe_marker
) {
172 switch (cinfo
->Adobe_transform
) {
174 cinfo
->jpeg_color_space
= JCS_CMYK
;
177 cinfo
->jpeg_color_space
= JCS_YCCK
;
180 WARNMS1(cinfo
, JWRN_ADOBE_XFORM
, cinfo
->Adobe_transform
);
181 cinfo
->jpeg_color_space
= JCS_YCCK
; /* assume it's YCCK */
185 /* No special markers, assume straight CMYK. */
186 cinfo
->jpeg_color_space
= JCS_CMYK
;
188 cinfo
->out_color_space
= JCS_CMYK
;
192 cinfo
->jpeg_color_space
= JCS_UNKNOWN
;
193 cinfo
->out_color_space
= JCS_UNKNOWN
;
197 /* Set defaults for other decompression parameters. */
198 cinfo
->scale_num
= 1; /* 1:1 scaling */
199 cinfo
->scale_denom
= 1;
200 cinfo
->output_gamma
= 1.0;
201 cinfo
->buffered_image
= FALSE
;
202 cinfo
->raw_data_out
= FALSE
;
203 cinfo
->dct_method
= JDCT_DEFAULT
;
204 cinfo
->do_fancy_upsampling
= TRUE
;
205 cinfo
->do_block_smoothing
= TRUE
;
206 cinfo
->quantize_colors
= FALSE
;
207 /* We set these in case application only sets quantize_colors. */
208 cinfo
->dither_mode
= JDITHER_FS
;
209 #ifdef QUANT_2PASS_SUPPORTED
210 cinfo
->two_pass_quantize
= TRUE
;
212 cinfo
->two_pass_quantize
= FALSE
;
214 cinfo
->desired_number_of_colors
= 256;
215 cinfo
->colormap
= NULL
;
216 /* Initialize for no mode change in buffered-image mode. */
217 cinfo
->enable_1pass_quant
= FALSE
;
218 cinfo
->enable_external_quant
= FALSE
;
219 cinfo
->enable_2pass_quant
= FALSE
;
224 * Decompression startup: read start of JPEG datastream to see what's there.
225 * Need only initialize JPEG object and supply a data source before calling.
227 * This routine will read as far as the first SOS marker (ie, actual start of
228 * compressed data), and will save all tables and parameters in the JPEG
229 * object. It will also initialize the decompression parameters to default
230 * values, and finally return JPEG_HEADER_OK. On return, the application may
231 * adjust the decompression parameters and then call jpeg_start_decompress.
232 * (Or, if the application only wanted to determine the image parameters,
233 * the data need not be decompressed. In that case, call jpeg_abort or
234 * jpeg_destroy to release any temporary space.)
235 * If an abbreviated (tables only) datastream is presented, the routine will
236 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
237 * re-use the JPEG object to read the abbreviated image datastream(s).
238 * It is unnecessary (but OK) to call jpeg_abort in this case.
239 * The JPEG_SUSPENDED return code only occurs if the data source module
240 * requests suspension of the decompressor. In this case the application
241 * should load more source data and then re-call jpeg_read_header to resume
243 * If a non-suspending data source is used and require_image is TRUE, then the
244 * return code need not be inspected since only JPEG_HEADER_OK is possible.
246 * This routine is now just a front end to jpeg_consume_input, with some
247 * extra error checking.
251 jpeg_read_header (j_decompress_ptr cinfo
, boolean require_image
)
255 if (cinfo
->global_state
!= DSTATE_START
&&
256 cinfo
->global_state
!= DSTATE_INHEADER
)
257 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
259 retcode
= jpeg_consume_input(cinfo
);
262 case JPEG_REACHED_SOS
:
263 retcode
= JPEG_HEADER_OK
;
265 case JPEG_REACHED_EOI
:
266 if (require_image
) /* Complain if application wanted an image */
267 ERREXIT(cinfo
, JERR_NO_IMAGE
);
268 /* Reset to start state; it would be safer to require the application to
269 * call jpeg_abort, but we can't change it now for compatibility reasons.
270 * A side effect is to free any temporary memory (there shouldn't be any).
272 jpeg_abort((j_common_ptr
) cinfo
); /* sets state = DSTATE_START */
273 retcode
= JPEG_HEADER_TABLES_ONLY
;
285 * Consume data in advance of what the decompressor requires.
286 * This can be called at any time once the decompressor object has
287 * been created and a data source has been set up.
289 * This routine is essentially a state machine that handles a couple
290 * of critical state-transition actions, namely initial setup and
291 * transition from header scanning to ready-for-start_decompress.
292 * All the actual input is done via the input controller's consume_input
297 jpeg_consume_input (j_decompress_ptr cinfo
)
299 int retcode
= JPEG_SUSPENDED
;
301 /* NB: every possible DSTATE value should be listed in this switch */
302 switch (cinfo
->global_state
) {
304 /* Start-of-datastream actions: reset appropriate modules */
305 (*cinfo
->inputctl
->reset_input_controller
) (cinfo
);
306 /* Initialize application's data source module */
307 (*cinfo
->src
->init_source
) (cinfo
);
308 cinfo
->global_state
= DSTATE_INHEADER
;
310 case DSTATE_INHEADER
:
311 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
312 if (retcode
== JPEG_REACHED_SOS
) { /* Found SOS, prepare to decompress */
313 /* Set up default parameters based on header data */
314 default_decompress_parms(cinfo
);
315 /* Set global state: ready for start_decompress */
316 cinfo
->global_state
= DSTATE_READY
;
320 /* Can't advance past first SOS until start_decompress is called */
321 retcode
= JPEG_REACHED_SOS
;
325 case DSTATE_SCANNING
:
327 case DSTATE_BUFIMAGE
:
329 case DSTATE_STOPPING
:
330 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
333 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
340 * Have we finished reading the input file?
344 jpeg_input_complete (j_decompress_ptr cinfo
)
346 /* Check for valid jpeg object */
347 if (cinfo
->global_state
< DSTATE_START
||
348 cinfo
->global_state
> DSTATE_STOPPING
)
349 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
350 return cinfo
->inputctl
->eoi_reached
;
355 * Is there more than one scan?
359 jpeg_has_multiple_scans (j_decompress_ptr cinfo
)
361 /* Only valid after jpeg_read_header completes */
362 if (cinfo
->global_state
< DSTATE_READY
||
363 cinfo
->global_state
> DSTATE_STOPPING
)
364 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
365 return cinfo
->inputctl
->has_multiple_scans
;
370 * Finish JPEG decompression.
372 * This will normally just verify the file trailer and release temp storage.
374 * Returns FALSE if suspended. The return value need be inspected only if
375 * a suspending data source is used.
379 jpeg_finish_decompress (j_decompress_ptr cinfo
)
381 if ((cinfo
->global_state
== DSTATE_SCANNING
||
382 cinfo
->global_state
== DSTATE_RAW_OK
) && ! cinfo
->buffered_image
) {
383 /* Terminate final pass of non-buffered mode */
384 if (cinfo
->output_scanline
< cinfo
->output_height
)
385 ERREXIT(cinfo
, JERR_TOO_LITTLE_DATA
);
386 (*cinfo
->master
->finish_output_pass
) (cinfo
);
387 cinfo
->global_state
= DSTATE_STOPPING
;
388 } else if (cinfo
->global_state
== DSTATE_BUFIMAGE
) {
389 /* Finishing after a buffered-image operation */
390 cinfo
->global_state
= DSTATE_STOPPING
;
391 } else if (cinfo
->global_state
!= DSTATE_STOPPING
) {
392 /* STOPPING = repeat call after a suspension, anything else is error */
393 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
396 while (! cinfo
->inputctl
->eoi_reached
) {
397 if ((*cinfo
->inputctl
->consume_input
) (cinfo
) == JPEG_SUSPENDED
)
398 return FALSE
; /* Suspend, come back later */
400 /* Do final cleanup */
401 (*cinfo
->src
->term_source
) (cinfo
);
402 /* We can use jpeg_abort to release memory and reset global_state */
403 jpeg_abort((j_common_ptr
) cinfo
);