blob: 7cb3949d019fe8877e44dcd4fbd658947e6d25e5 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdapimin.c
3 *
DRC5de454b2014-05-18 19:04:03 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1994-1998, Thomas G. Lane.
DRC5de454b2014-05-18 19:04:03 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
DRC7e3acc02015-10-10 10:25:46 -05008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000010 *
11 * This file contains application interface code for the decompression half
12 * of the JPEG library. These are the "minimum" API routines that may be
13 * needed in either the normal full-decompression case or the
14 * transcoding-only case.
15 *
16 * Most of the routines intended to be called directly by an application
17 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
18 * shared by compression and decompression, and jdtrans.c for the transcoding
19 * case.
20 */
21
22#define JPEG_INTERNALS
23#include "jinclude.h"
24#include "jpeglib.h"
25
26
27/*
28 * Initialization of a JPEG decompression object.
29 * The error manager must already be set up (in case memory manager fails).
30 */
31
Thomas G. Lane489583f1996-02-07 00:00:00 +000032GLOBAL(void)
33jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000034{
35 int i;
36
Thomas G. Lane489583f1996-02-07 00:00:00 +000037 /* Guard against version mismatches between library and caller. */
DRCe5eaf372014-05-09 18:00:32 +000038 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
Thomas G. Lane489583f1996-02-07 00:00:00 +000039 if (version != JPEG_LIB_VERSION)
40 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
DRC5de454b2014-05-18 19:04:03 +000041 if (structsize != sizeof(struct jpeg_decompress_struct))
DRCe5eaf372014-05-09 18:00:32 +000042 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
DRC5de454b2014-05-18 19:04:03 +000043 (int) sizeof(struct jpeg_decompress_struct), (int) structsize);
Thomas G. Lane489583f1996-02-07 00:00:00 +000044
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000045 /* For debugging purposes, we zero the whole master structure.
46 * But the application has already set the err pointer, and may have set
47 * client_data, so we have to save and restore those fields.
48 * Note: if application hasn't set client_data, tools like Purify may
49 * complain here.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000050 */
51 {
52 struct jpeg_error_mgr * err = cinfo->err;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000053 void * client_data = cinfo->client_data; /* ignore Purify complaint here */
DRC5de454b2014-05-18 19:04:03 +000054 MEMZERO(cinfo, sizeof(struct jpeg_decompress_struct));
Thomas G. Lanebc79e061995-08-02 00:00:00 +000055 cinfo->err = err;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000056 cinfo->client_data = client_data;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000057 }
58 cinfo->is_decompressor = TRUE;
59
60 /* Initialize a memory manager instance for this object */
61 jinit_memory_mgr((j_common_ptr) cinfo);
62
63 /* Zero out pointers to permanent structures. */
64 cinfo->progress = NULL;
65 cinfo->src = NULL;
66
67 for (i = 0; i < NUM_QUANT_TBLS; i++)
68 cinfo->quant_tbl_ptrs[i] = NULL;
69
70 for (i = 0; i < NUM_HUFF_TBLS; i++) {
71 cinfo->dc_huff_tbl_ptrs[i] = NULL;
72 cinfo->ac_huff_tbl_ptrs[i] = NULL;
73 }
74
75 /* Initialize marker processor so application can override methods
76 * for COM, APPn markers before calling jpeg_read_header.
77 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000078 cinfo->marker_list = NULL;
Thomas G. Lanebc79e061995-08-02 00:00:00 +000079 jinit_marker_reader(cinfo);
80
81 /* And initialize the overall input controller. */
82 jinit_input_controller(cinfo);
83
84 /* OK, I'm ready */
85 cinfo->global_state = DSTATE_START;
86}
87
88
89/*
90 * Destruction of a JPEG decompression object
91 */
92
Thomas G. Lane489583f1996-02-07 00:00:00 +000093GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000094jpeg_destroy_decompress (j_decompress_ptr cinfo)
95{
96 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
97}
98
99
100/*
101 * Abort processing of a JPEG decompression operation,
102 * but don't destroy the object itself.
103 */
104
Thomas G. Lane489583f1996-02-07 00:00:00 +0000105GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000106jpeg_abort_decompress (j_decompress_ptr cinfo)
107{
108 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
109}
110
111
112/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000113 * Set default decompression parameters.
114 */
115
Thomas G. Lane489583f1996-02-07 00:00:00 +0000116LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000117default_decompress_parms (j_decompress_ptr cinfo)
118{
119 /* Guess the input colorspace, and set output colorspace accordingly. */
120 /* (Wish JPEG committee had provided a real way to specify this...) */
121 /* Note application may override our guesses. */
122 switch (cinfo->num_components) {
123 case 1:
124 cinfo->jpeg_color_space = JCS_GRAYSCALE;
125 cinfo->out_color_space = JCS_GRAYSCALE;
126 break;
DRCe5eaf372014-05-09 18:00:32 +0000127
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000128 case 3:
129 if (cinfo->saw_JFIF_marker) {
130 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
131 } else if (cinfo->saw_Adobe_marker) {
132 switch (cinfo->Adobe_transform) {
133 case 0:
DRCe5eaf372014-05-09 18:00:32 +0000134 cinfo->jpeg_color_space = JCS_RGB;
135 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000136 case 1:
DRCe5eaf372014-05-09 18:00:32 +0000137 cinfo->jpeg_color_space = JCS_YCbCr;
138 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000139 default:
DRCe5eaf372014-05-09 18:00:32 +0000140 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
141 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
142 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000143 }
144 } else {
145 /* Saw no special markers, try to guess from the component IDs */
146 int cid0 = cinfo->comp_info[0].component_id;
147 int cid1 = cinfo->comp_info[1].component_id;
148 int cid2 = cinfo->comp_info[2].component_id;
149
150 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
DRCe5eaf372014-05-09 18:00:32 +0000151 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000152 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
DRCe5eaf372014-05-09 18:00:32 +0000153 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000154 else {
DRCe5eaf372014-05-09 18:00:32 +0000155 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
156 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000157 }
158 }
159 /* Always guess RGB is proper output colorspace. */
160 cinfo->out_color_space = JCS_RGB;
161 break;
DRCe5eaf372014-05-09 18:00:32 +0000162
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163 case 4:
164 if (cinfo->saw_Adobe_marker) {
165 switch (cinfo->Adobe_transform) {
166 case 0:
DRCe5eaf372014-05-09 18:00:32 +0000167 cinfo->jpeg_color_space = JCS_CMYK;
168 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000169 case 2:
DRCe5eaf372014-05-09 18:00:32 +0000170 cinfo->jpeg_color_space = JCS_YCCK;
171 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000172 default:
DRCe5eaf372014-05-09 18:00:32 +0000173 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
174 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
175 break;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000176 }
177 } else {
178 /* No special markers, assume straight CMYK. */
179 cinfo->jpeg_color_space = JCS_CMYK;
180 }
181 cinfo->out_color_space = JCS_CMYK;
182 break;
DRCe5eaf372014-05-09 18:00:32 +0000183
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000184 default:
185 cinfo->jpeg_color_space = JCS_UNKNOWN;
186 cinfo->out_color_space = JCS_UNKNOWN;
187 break;
188 }
189
190 /* Set defaults for other decompression parameters. */
DRCe5eaf372014-05-09 18:00:32 +0000191 cinfo->scale_num = 1; /* 1:1 scaling */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000192 cinfo->scale_denom = 1;
193 cinfo->output_gamma = 1.0;
194 cinfo->buffered_image = FALSE;
195 cinfo->raw_data_out = FALSE;
196 cinfo->dct_method = JDCT_DEFAULT;
197 cinfo->do_fancy_upsampling = TRUE;
198 cinfo->do_block_smoothing = TRUE;
199 cinfo->quantize_colors = FALSE;
200 /* We set these in case application only sets quantize_colors. */
201 cinfo->dither_mode = JDITHER_FS;
202#ifdef QUANT_2PASS_SUPPORTED
203 cinfo->two_pass_quantize = TRUE;
204#else
205 cinfo->two_pass_quantize = FALSE;
206#endif
207 cinfo->desired_number_of_colors = 256;
208 cinfo->colormap = NULL;
209 /* Initialize for no mode change in buffered-image mode. */
210 cinfo->enable_1pass_quant = FALSE;
211 cinfo->enable_external_quant = FALSE;
212 cinfo->enable_2pass_quant = FALSE;
213}
214
215
216/*
217 * Decompression startup: read start of JPEG datastream to see what's there.
218 * Need only initialize JPEG object and supply a data source before calling.
219 *
220 * This routine will read as far as the first SOS marker (ie, actual start of
221 * compressed data), and will save all tables and parameters in the JPEG
222 * object. It will also initialize the decompression parameters to default
223 * values, and finally return JPEG_HEADER_OK. On return, the application may
224 * adjust the decompression parameters and then call jpeg_start_decompress.
225 * (Or, if the application only wanted to determine the image parameters,
226 * the data need not be decompressed. In that case, call jpeg_abort or
227 * jpeg_destroy to release any temporary space.)
228 * If an abbreviated (tables only) datastream is presented, the routine will
229 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
230 * re-use the JPEG object to read the abbreviated image datastream(s).
231 * It is unnecessary (but OK) to call jpeg_abort in this case.
232 * The JPEG_SUSPENDED return code only occurs if the data source module
233 * requests suspension of the decompressor. In this case the application
234 * should load more source data and then re-call jpeg_read_header to resume
235 * processing.
236 * If a non-suspending data source is used and require_image is TRUE, then the
237 * return code need not be inspected since only JPEG_HEADER_OK is possible.
238 *
239 * This routine is now just a front end to jpeg_consume_input, with some
240 * extra error checking.
241 */
242
Thomas G. Lane489583f1996-02-07 00:00:00 +0000243GLOBAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000244jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
245{
246 int retcode;
247
248 if (cinfo->global_state != DSTATE_START &&
249 cinfo->global_state != DSTATE_INHEADER)
250 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
251
252 retcode = jpeg_consume_input(cinfo);
253
254 switch (retcode) {
255 case JPEG_REACHED_SOS:
256 retcode = JPEG_HEADER_OK;
257 break;
258 case JPEG_REACHED_EOI:
DRCe5eaf372014-05-09 18:00:32 +0000259 if (require_image) /* Complain if application wanted an image */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000260 ERREXIT(cinfo, JERR_NO_IMAGE);
261 /* Reset to start state; it would be safer to require the application to
262 * call jpeg_abort, but we can't change it now for compatibility reasons.
263 * A side effect is to free any temporary memory (there shouldn't be any).
264 */
265 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
266 retcode = JPEG_HEADER_TABLES_ONLY;
267 break;
268 case JPEG_SUSPENDED:
269 /* no work */
270 break;
271 }
272
273 return retcode;
274}
275
276
277/*
278 * Consume data in advance of what the decompressor requires.
279 * This can be called at any time once the decompressor object has
280 * been created and a data source has been set up.
281 *
282 * This routine is essentially a state machine that handles a couple
283 * of critical state-transition actions, namely initial setup and
284 * transition from header scanning to ready-for-start_decompress.
285 * All the actual input is done via the input controller's consume_input
286 * method.
287 */
288
Thomas G. Lane489583f1996-02-07 00:00:00 +0000289GLOBAL(int)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000290jpeg_consume_input (j_decompress_ptr cinfo)
291{
292 int retcode = JPEG_SUSPENDED;
293
294 /* NB: every possible DSTATE value should be listed in this switch */
295 switch (cinfo->global_state) {
296 case DSTATE_START:
297 /* Start-of-datastream actions: reset appropriate modules */
298 (*cinfo->inputctl->reset_input_controller) (cinfo);
299 /* Initialize application's data source module */
300 (*cinfo->src->init_source) (cinfo);
301 cinfo->global_state = DSTATE_INHEADER;
302 /*FALLTHROUGH*/
303 case DSTATE_INHEADER:
304 retcode = (*cinfo->inputctl->consume_input) (cinfo);
305 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
306 /* Set up default parameters based on header data */
307 default_decompress_parms(cinfo);
308 /* Set global state: ready for start_decompress */
309 cinfo->global_state = DSTATE_READY;
310 }
311 break;
312 case DSTATE_READY:
313 /* Can't advance past first SOS until start_decompress is called */
314 retcode = JPEG_REACHED_SOS;
315 break;
316 case DSTATE_PRELOAD:
317 case DSTATE_PRESCAN:
318 case DSTATE_SCANNING:
319 case DSTATE_RAW_OK:
320 case DSTATE_BUFIMAGE:
321 case DSTATE_BUFPOST:
322 case DSTATE_STOPPING:
323 retcode = (*cinfo->inputctl->consume_input) (cinfo);
324 break;
325 default:
326 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
327 }
328 return retcode;
329}
330
331
332/*
333 * Have we finished reading the input file?
334 */
335
Thomas G. Lane489583f1996-02-07 00:00:00 +0000336GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000337jpeg_input_complete (j_decompress_ptr cinfo)
338{
339 /* Check for valid jpeg object */
340 if (cinfo->global_state < DSTATE_START ||
341 cinfo->global_state > DSTATE_STOPPING)
342 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
343 return cinfo->inputctl->eoi_reached;
344}
345
346
347/*
348 * Is there more than one scan?
349 */
350
Thomas G. Lane489583f1996-02-07 00:00:00 +0000351GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000352jpeg_has_multiple_scans (j_decompress_ptr cinfo)
353{
354 /* Only valid after jpeg_read_header completes */
355 if (cinfo->global_state < DSTATE_READY ||
356 cinfo->global_state > DSTATE_STOPPING)
357 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
358 return cinfo->inputctl->has_multiple_scans;
359}
360
361
362/*
363 * Finish JPEG decompression.
364 *
365 * This will normally just verify the file trailer and release temp storage.
366 *
367 * Returns FALSE if suspended. The return value need be inspected only if
368 * a suspending data source is used.
369 */
370
Thomas G. Lane489583f1996-02-07 00:00:00 +0000371GLOBAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000372jpeg_finish_decompress (j_decompress_ptr cinfo)
373{
374 if ((cinfo->global_state == DSTATE_SCANNING ||
375 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
376 /* Terminate final pass of non-buffered mode */
377 if (cinfo->output_scanline < cinfo->output_height)
378 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
379 (*cinfo->master->finish_output_pass) (cinfo);
380 cinfo->global_state = DSTATE_STOPPING;
381 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
382 /* Finishing after a buffered-image operation */
383 cinfo->global_state = DSTATE_STOPPING;
384 } else if (cinfo->global_state != DSTATE_STOPPING) {
385 /* STOPPING = repeat call after a suspension, anything else is error */
386 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
387 }
388 /* Read until EOI */
389 while (! cinfo->inputctl->eoi_reached) {
390 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
DRCe5eaf372014-05-09 18:00:32 +0000391 return FALSE; /* Suspend, come back later */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000392 }
393 /* Do final cleanup */
394 (*cinfo->src->term_source) (cinfo);
395 /* We can use jpeg_abort to release memory and reset global_state */
396 jpeg_abort((j_common_ptr) cinfo);
397 return TRUE;
398}