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