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