blob: 5c9607ed5c890fe5d47a99c185856e1a0f2daa99 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * jdapimin.c
3 *
4 * Copyright (C) 1994-1998, 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.
7 *
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.
12 *
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
16 * case.
17 */
18
19#define JPEG_INTERNALS
20#include "jinclude.h"
21#include "jpeglib.h"
22
23
24/*
25 * Initialization of a JPEG decompression object.
26 * The error manager must already be set up (in case memory manager fails).
27 */
28
29GLOBAL(void)
30jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
31{
32 int i;
33
34 /* Guard against version mismatches between library and caller. */
35 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
36 if (version != JPEG_LIB_VERSION)
37 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
38 if (structsize != SIZEOF(struct jpeg_decompress_struct))
39 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
40 (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
41
42 /* For debugging purposes, we zero the whole master structure.
43 * But the application has already set the err pointer, and may have set
44 * client_data, so we have to save and restore those fields.
45 * Note: if application hasn't set client_data, tools like Purify may
46 * complain here.
47 */
48 {
49 struct jpeg_error_mgr * err = cinfo->err;
50 void * client_data = cinfo->client_data; /* ignore Purify complaint here */
51 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
52 cinfo->err = err;
53 cinfo->client_data = client_data;
54 }
55 cinfo->is_decompressor = TRUE;
56 cinfo->tile_decode = FALSE;
57
58 /* Initialize a memory manager instance for this object */
59 jinit_memory_mgr((j_common_ptr) cinfo);
60
61 /* Zero out pointers to permanent structures. */
62 cinfo->progress = NULL;
63 cinfo->src = NULL;
64
65 for (i = 0; i < NUM_QUANT_TBLS; i++)
66 cinfo->quant_tbl_ptrs[i] = NULL;
67
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;
71 }
72
73 /* Initialize marker processor so application can override methods
74 * for COM, APPn markers before calling jpeg_read_header.
75 */
76 cinfo->marker_list = NULL;
77 jinit_marker_reader(cinfo);
78
79 /* And initialize the overall input controller. */
80 jinit_input_controller(cinfo);
81
82 /* OK, I'm ready */
83 cinfo->global_state = DSTATE_START;
84}
85
86
87/*
88 * Destruction of a JPEG decompression object
89 */
90
91GLOBAL(void)
92jpeg_destroy_decompress (j_decompress_ptr cinfo)
93{
94 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
95}
96
97
98/*
99 * Abort processing of a JPEG decompression operation,
100 * but don't destroy the object itself.
101 */
102
103GLOBAL(void)
104jpeg_abort_decompress (j_decompress_ptr cinfo)
105{
106 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
107}
108
109
110/*
111 * Set default decompression parameters.
112 */
113
114LOCAL(void)
115default_decompress_parms (j_decompress_ptr cinfo)
116{
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) {
121 case 1:
122 cinfo->jpeg_color_space = JCS_GRAYSCALE;
123 cinfo->out_color_space = JCS_GRAYSCALE;
124 break;
125
126 case 3:
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) {
131 case 0:
132 cinfo->jpeg_color_space = JCS_RGB;
133 break;
134 case 1:
135 cinfo->jpeg_color_space = JCS_YCbCr;
136 break;
137 default:
138 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
139 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
140 break;
141 }
142 } else {
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;
147
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' */
152 else {
153 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
154 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
155 }
156 }
157 /* Always guess RGB is proper output colorspace. */
158 cinfo->out_color_space = JCS_RGB;
159 break;
160
161 case 4:
162 if (cinfo->saw_Adobe_marker) {
163 switch (cinfo->Adobe_transform) {
164 case 0:
165 cinfo->jpeg_color_space = JCS_CMYK;
166 break;
167 case 2:
168 cinfo->jpeg_color_space = JCS_YCCK;
169 break;
170 default:
171 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
172 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
173 break;
174 }
175 } else {
176 /* No special markers, assume straight CMYK. */
177 cinfo->jpeg_color_space = JCS_CMYK;
178 }
179 cinfo->out_color_space = JCS_CMYK;
180 break;
181
182 default:
183 cinfo->jpeg_color_space = JCS_UNKNOWN;
184 cinfo->out_color_space = JCS_UNKNOWN;
185 break;
186 }
187
188 /* Set defaults for other decompression parameters. */
189 cinfo->scale_num = 1; /* 1:1 scaling */
190 cinfo->scale_denom = 1;
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;
202#else
203 cinfo->two_pass_quantize = FALSE;
204#endif
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;
211}
212
213
214/*
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.
217 *
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
233 * processing.
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.
236 *
237 * This routine is now just a front end to jpeg_consume_input, with some
238 * extra error checking.
239 */
240
241GLOBAL(int)
242jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
243{
244 int retcode;
245
246 if (cinfo->global_state != DSTATE_START &&
247 cinfo->global_state != DSTATE_INHEADER)
248 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
249
250 retcode = jpeg_consume_input(cinfo);
251
252 switch (retcode) {
253 case JPEG_REACHED_SOS:
254 retcode = JPEG_HEADER_OK;
255 break;
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).
262 */
263 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
264 retcode = JPEG_HEADER_TABLES_ONLY;
265 break;
266 case JPEG_SUSPENDED:
267 /* no work */
268 break;
269 }
270
271 return retcode;
272}
273
274
275/*
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.
279 *
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
284 * method.
285 */
286
287GLOBAL(int)
288jpeg_consume_input (j_decompress_ptr cinfo)
289{
290 int retcode = JPEG_SUSPENDED;
291
292 /* NB: every possible DSTATE value should be listed in this switch */
293 switch (cinfo->global_state) {
294 case DSTATE_START:
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;
300 /*FALLTHROUGH*/
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;
308 }
309 break;
310 case DSTATE_READY:
311 /* Can't advance past first SOS until start_decompress is called */
312 retcode = JPEG_REACHED_SOS;
313 break;
314 case DSTATE_PRELOAD:
315 case DSTATE_PRESCAN:
316 case DSTATE_SCANNING:
317 case DSTATE_RAW_OK:
318 case DSTATE_BUFIMAGE:
319 case DSTATE_BUFPOST:
320 case DSTATE_STOPPING:
321 retcode = (*cinfo->inputctl->consume_input) (cinfo);
322 break;
323 default:
324 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
325 }
326 return retcode;
327}
328
329
330/*
331 * Have we finished reading the input file?
332 */
333
334GLOBAL(boolean)
335jpeg_input_complete (j_decompress_ptr cinfo)
336{
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;
342}
343
344
345/*
346 * Is there more than one scan?
347 */
348
349GLOBAL(boolean)
350jpeg_has_multiple_scans (j_decompress_ptr cinfo)
351{
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;
357}
358
359
360/*
361 * Finish JPEG decompression.
362 *
363 * This will normally just verify the file trailer and release temp storage.
364 *
365 * Returns FALSE if suspended. The return value need be inspected only if
366 * a suspending data source is used.
367 */
368
369GLOBAL(boolean)
370jpeg_finish_decompress (j_decompress_ptr cinfo)
371{
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#ifdef ANDROID_TILE_BASED_DECODE
376 cinfo->output_scanline = cinfo->output_height;
377#endif
378 if (cinfo->output_scanline < cinfo->output_height)
379 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
380 (*cinfo->master->finish_output_pass) (cinfo);
381 cinfo->global_state = DSTATE_STOPPING;
382 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
383 /* Finishing after a buffered-image operation */
384 cinfo->global_state = DSTATE_STOPPING;
385 } else if (cinfo->global_state != DSTATE_STOPPING) {
386 /* STOPPING = repeat call after a suspension, anything else is error */
387 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
388 }
389 /* Read until EOI */
390#ifndef ANDROID_TILE_BASED_DECODE
391 while (! cinfo->inputctl->eoi_reached) {
392 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
393 return FALSE; /* Suspend, come back later */
394 }
395#endif
396 /* Do final cleanup */
397 (*cinfo->src->term_source) (cinfo);
398 /* We can use jpeg_abort to release memory and reset global_state */
399 jpeg_abort((j_common_ptr) cinfo);
400 return TRUE;
401}