Line data Source code
1 : /*
2 : * jdapimin.c
3 : *
4 : * This file was part of the Independent JPEG Group's software:
5 : * Copyright (C) 1994-1998, Thomas G. Lane.
6 : * libjpeg-turbo Modifications:
7 : * Copyright (C) 2016, D. R. Commander.
8 : * For conditions of distribution and use, see the accompanying README.ijg
9 : * file.
10 : *
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 : #include "jdmaster.h"
26 :
27 :
28 : /*
29 : * Initialization of a JPEG decompression object.
30 : * The error manager must already be set up (in case memory manager fails).
31 : */
32 :
33 : GLOBAL(void)
34 0 : jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
35 : {
36 : int i;
37 :
38 : /* Guard against version mismatches between library and caller. */
39 0 : cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
40 0 : if (version != JPEG_LIB_VERSION)
41 0 : ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
42 0 : if (structsize != sizeof(struct jpeg_decompress_struct))
43 0 : ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
44 : (int) sizeof(struct jpeg_decompress_struct), (int) structsize);
45 :
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
50 : * complain here.
51 : */
52 : {
53 0 : struct jpeg_error_mgr * err = cinfo->err;
54 0 : void * client_data = cinfo->client_data; /* ignore Purify complaint here */
55 0 : MEMZERO(cinfo, sizeof(struct jpeg_decompress_struct));
56 0 : cinfo->err = err;
57 0 : cinfo->client_data = client_data;
58 : }
59 0 : cinfo->is_decompressor = TRUE;
60 :
61 : /* Initialize a memory manager instance for this object */
62 0 : jinit_memory_mgr((j_common_ptr) cinfo);
63 :
64 : /* Zero out pointers to permanent structures. */
65 0 : cinfo->progress = NULL;
66 0 : cinfo->src = NULL;
67 :
68 0 : for (i = 0; i < NUM_QUANT_TBLS; i++)
69 0 : cinfo->quant_tbl_ptrs[i] = NULL;
70 :
71 0 : for (i = 0; i < NUM_HUFF_TBLS; i++) {
72 0 : cinfo->dc_huff_tbl_ptrs[i] = NULL;
73 0 : cinfo->ac_huff_tbl_ptrs[i] = NULL;
74 : }
75 :
76 : /* Initialize marker processor so application can override methods
77 : * for COM, APPn markers before calling jpeg_read_header.
78 : */
79 0 : cinfo->marker_list = NULL;
80 0 : jinit_marker_reader(cinfo);
81 :
82 : /* And initialize the overall input controller. */
83 0 : jinit_input_controller(cinfo);
84 :
85 : /* OK, I'm ready */
86 0 : cinfo->global_state = DSTATE_START;
87 :
88 : /* The master struct is used to store extension parameters, so we allocate it
89 : * here.
90 : */
91 0 : cinfo->master = (struct jpeg_decomp_master *)
92 0 : (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
93 : sizeof(my_decomp_master));
94 0 : MEMZERO(cinfo->master, sizeof(my_decomp_master));
95 0 : }
96 :
97 :
98 : /*
99 : * Destruction of a JPEG decompression object
100 : */
101 :
102 : GLOBAL(void)
103 0 : jpeg_destroy_decompress (j_decompress_ptr cinfo)
104 : {
105 0 : jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
106 0 : }
107 :
108 :
109 : /*
110 : * Abort processing of a JPEG decompression operation,
111 : * but don't destroy the object itself.
112 : */
113 :
114 : GLOBAL(void)
115 0 : jpeg_abort_decompress (j_decompress_ptr cinfo)
116 : {
117 0 : jpeg_abort((j_common_ptr) cinfo); /* use common routine */
118 0 : }
119 :
120 :
121 : /*
122 : * Set default decompression parameters.
123 : */
124 :
125 : LOCAL(void)
126 0 : default_decompress_parms (j_decompress_ptr cinfo)
127 : {
128 : /* Guess the input colorspace, and set output colorspace accordingly. */
129 : /* (Wish JPEG committee had provided a real way to specify this...) */
130 : /* Note application may override our guesses. */
131 0 : switch (cinfo->num_components) {
132 : case 1:
133 0 : cinfo->jpeg_color_space = JCS_GRAYSCALE;
134 0 : cinfo->out_color_space = JCS_GRAYSCALE;
135 0 : break;
136 :
137 : case 3:
138 0 : if (cinfo->saw_JFIF_marker) {
139 0 : cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
140 0 : } else if (cinfo->saw_Adobe_marker) {
141 0 : switch (cinfo->Adobe_transform) {
142 : case 0:
143 0 : cinfo->jpeg_color_space = JCS_RGB;
144 0 : break;
145 : case 1:
146 0 : cinfo->jpeg_color_space = JCS_YCbCr;
147 0 : break;
148 : default:
149 0 : WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
150 0 : cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
151 0 : break;
152 : }
153 : } else {
154 : /* Saw no special markers, try to guess from the component IDs */
155 0 : int cid0 = cinfo->comp_info[0].component_id;
156 0 : int cid1 = cinfo->comp_info[1].component_id;
157 0 : int cid2 = cinfo->comp_info[2].component_id;
158 :
159 0 : if (cid0 == 1 && cid1 == 2 && cid2 == 3)
160 0 : cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
161 0 : else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
162 0 : cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
163 : else {
164 0 : TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
165 0 : cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
166 : }
167 : }
168 : /* Always guess RGB is proper output colorspace. */
169 0 : cinfo->out_color_space = JCS_RGB;
170 0 : break;
171 :
172 : case 4:
173 0 : if (cinfo->saw_Adobe_marker) {
174 0 : switch (cinfo->Adobe_transform) {
175 : case 0:
176 0 : cinfo->jpeg_color_space = JCS_CMYK;
177 0 : break;
178 : case 2:
179 0 : cinfo->jpeg_color_space = JCS_YCCK;
180 0 : break;
181 : default:
182 0 : WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
183 0 : cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
184 0 : break;
185 : }
186 : } else {
187 : /* No special markers, assume straight CMYK. */
188 0 : cinfo->jpeg_color_space = JCS_CMYK;
189 : }
190 0 : cinfo->out_color_space = JCS_CMYK;
191 0 : break;
192 :
193 : default:
194 0 : cinfo->jpeg_color_space = JCS_UNKNOWN;
195 0 : cinfo->out_color_space = JCS_UNKNOWN;
196 0 : break;
197 : }
198 :
199 : /* Set defaults for other decompression parameters. */
200 0 : cinfo->scale_num = 1; /* 1:1 scaling */
201 0 : cinfo->scale_denom = 1;
202 0 : cinfo->output_gamma = 1.0;
203 0 : cinfo->buffered_image = FALSE;
204 0 : cinfo->raw_data_out = FALSE;
205 0 : cinfo->dct_method = JDCT_DEFAULT;
206 0 : cinfo->do_fancy_upsampling = TRUE;
207 0 : cinfo->do_block_smoothing = TRUE;
208 0 : cinfo->quantize_colors = FALSE;
209 : /* We set these in case application only sets quantize_colors. */
210 0 : cinfo->dither_mode = JDITHER_FS;
211 : #ifdef QUANT_2PASS_SUPPORTED
212 0 : cinfo->two_pass_quantize = TRUE;
213 : #else
214 : cinfo->two_pass_quantize = FALSE;
215 : #endif
216 0 : cinfo->desired_number_of_colors = 256;
217 0 : cinfo->colormap = NULL;
218 : /* Initialize for no mode change in buffered-image mode. */
219 0 : cinfo->enable_1pass_quant = FALSE;
220 0 : cinfo->enable_external_quant = FALSE;
221 0 : cinfo->enable_2pass_quant = FALSE;
222 0 : }
223 :
224 :
225 : /*
226 : * Decompression startup: read start of JPEG datastream to see what's there.
227 : * Need only initialize JPEG object and supply a data source before calling.
228 : *
229 : * This routine will read as far as the first SOS marker (ie, actual start of
230 : * compressed data), and will save all tables and parameters in the JPEG
231 : * object. It will also initialize the decompression parameters to default
232 : * values, and finally return JPEG_HEADER_OK. On return, the application may
233 : * adjust the decompression parameters and then call jpeg_start_decompress.
234 : * (Or, if the application only wanted to determine the image parameters,
235 : * the data need not be decompressed. In that case, call jpeg_abort or
236 : * jpeg_destroy to release any temporary space.)
237 : * If an abbreviated (tables only) datastream is presented, the routine will
238 : * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
239 : * re-use the JPEG object to read the abbreviated image datastream(s).
240 : * It is unnecessary (but OK) to call jpeg_abort in this case.
241 : * The JPEG_SUSPENDED return code only occurs if the data source module
242 : * requests suspension of the decompressor. In this case the application
243 : * should load more source data and then re-call jpeg_read_header to resume
244 : * processing.
245 : * If a non-suspending data source is used and require_image is TRUE, then the
246 : * return code need not be inspected since only JPEG_HEADER_OK is possible.
247 : *
248 : * This routine is now just a front end to jpeg_consume_input, with some
249 : * extra error checking.
250 : */
251 :
252 : GLOBAL(int)
253 0 : jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
254 : {
255 : int retcode;
256 :
257 0 : if (cinfo->global_state != DSTATE_START &&
258 0 : cinfo->global_state != DSTATE_INHEADER)
259 0 : ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
260 :
261 0 : retcode = jpeg_consume_input(cinfo);
262 :
263 0 : switch (retcode) {
264 : case JPEG_REACHED_SOS:
265 0 : retcode = JPEG_HEADER_OK;
266 0 : break;
267 : case JPEG_REACHED_EOI:
268 0 : if (require_image) /* Complain if application wanted an image */
269 0 : ERREXIT(cinfo, JERR_NO_IMAGE);
270 : /* Reset to start state; it would be safer to require the application to
271 : * call jpeg_abort, but we can't change it now for compatibility reasons.
272 : * A side effect is to free any temporary memory (there shouldn't be any).
273 : */
274 0 : jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
275 0 : retcode = JPEG_HEADER_TABLES_ONLY;
276 0 : break;
277 : case JPEG_SUSPENDED:
278 : /* no work */
279 0 : break;
280 : }
281 :
282 0 : return retcode;
283 : }
284 :
285 :
286 : /*
287 : * Consume data in advance of what the decompressor requires.
288 : * This can be called at any time once the decompressor object has
289 : * been created and a data source has been set up.
290 : *
291 : * This routine is essentially a state machine that handles a couple
292 : * of critical state-transition actions, namely initial setup and
293 : * transition from header scanning to ready-for-start_decompress.
294 : * All the actual input is done via the input controller's consume_input
295 : * method.
296 : */
297 :
298 : GLOBAL(int)
299 0 : jpeg_consume_input (j_decompress_ptr cinfo)
300 : {
301 0 : int retcode = JPEG_SUSPENDED;
302 :
303 : /* NB: every possible DSTATE value should be listed in this switch */
304 0 : switch (cinfo->global_state) {
305 : case DSTATE_START:
306 : /* Start-of-datastream actions: reset appropriate modules */
307 0 : (*cinfo->inputctl->reset_input_controller) (cinfo);
308 : /* Initialize application's data source module */
309 0 : (*cinfo->src->init_source) (cinfo);
310 0 : cinfo->global_state = DSTATE_INHEADER;
311 : /*FALLTHROUGH*/
312 : case DSTATE_INHEADER:
313 0 : retcode = (*cinfo->inputctl->consume_input) (cinfo);
314 0 : if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
315 : /* Set up default parameters based on header data */
316 0 : default_decompress_parms(cinfo);
317 : /* Set global state: ready for start_decompress */
318 0 : cinfo->global_state = DSTATE_READY;
319 : }
320 0 : break;
321 : case DSTATE_READY:
322 : /* Can't advance past first SOS until start_decompress is called */
323 0 : retcode = JPEG_REACHED_SOS;
324 0 : break;
325 : case DSTATE_PRELOAD:
326 : case DSTATE_PRESCAN:
327 : case DSTATE_SCANNING:
328 : case DSTATE_RAW_OK:
329 : case DSTATE_BUFIMAGE:
330 : case DSTATE_BUFPOST:
331 : case DSTATE_STOPPING:
332 0 : retcode = (*cinfo->inputctl->consume_input) (cinfo);
333 0 : break;
334 : default:
335 0 : ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
336 : }
337 0 : return retcode;
338 : }
339 :
340 :
341 : /*
342 : * Have we finished reading the input file?
343 : */
344 :
345 : GLOBAL(boolean)
346 0 : jpeg_input_complete (j_decompress_ptr cinfo)
347 : {
348 : /* Check for valid jpeg object */
349 0 : if (cinfo->global_state < DSTATE_START ||
350 0 : cinfo->global_state > DSTATE_STOPPING)
351 0 : ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
352 0 : return cinfo->inputctl->eoi_reached;
353 : }
354 :
355 :
356 : /*
357 : * Is there more than one scan?
358 : */
359 :
360 : GLOBAL(boolean)
361 0 : jpeg_has_multiple_scans (j_decompress_ptr cinfo)
362 : {
363 : /* Only valid after jpeg_read_header completes */
364 0 : if (cinfo->global_state < DSTATE_READY ||
365 0 : cinfo->global_state > DSTATE_STOPPING)
366 0 : ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
367 0 : return cinfo->inputctl->has_multiple_scans;
368 : }
369 :
370 :
371 : /*
372 : * Finish JPEG decompression.
373 : *
374 : * This will normally just verify the file trailer and release temp storage.
375 : *
376 : * Returns FALSE if suspended. The return value need be inspected only if
377 : * a suspending data source is used.
378 : */
379 :
380 : GLOBAL(boolean)
381 0 : jpeg_finish_decompress (j_decompress_ptr cinfo)
382 : {
383 0 : if ((cinfo->global_state == DSTATE_SCANNING ||
384 0 : cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
385 : /* Terminate final pass of non-buffered mode */
386 0 : if (cinfo->output_scanline < cinfo->output_height)
387 0 : ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
388 0 : (*cinfo->master->finish_output_pass) (cinfo);
389 0 : cinfo->global_state = DSTATE_STOPPING;
390 0 : } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
391 : /* Finishing after a buffered-image operation */
392 0 : cinfo->global_state = DSTATE_STOPPING;
393 0 : } else if (cinfo->global_state != DSTATE_STOPPING) {
394 : /* STOPPING = repeat call after a suspension, anything else is error */
395 0 : ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
396 : }
397 : /* Read until EOI */
398 0 : while (! cinfo->inputctl->eoi_reached) {
399 0 : if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
400 0 : return FALSE; /* Suspend, come back later */
401 : }
402 : /* Do final cleanup */
403 0 : (*cinfo->src->term_source) (cinfo);
404 : /* We can use jpeg_abort to release memory and reset global_state */
405 0 : jpeg_abort((j_common_ptr) cinfo);
406 0 : return TRUE;
407 : }
|