/src/libjpeg-turbo.main/jdapimin.c
Line | Count | Source (jump to first uncovered line) |
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, 2022, 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 | | jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize) |
35 | 10.3k | { |
36 | 10.3k | int i; |
37 | | |
38 | | /* Guard against version mismatches between library and caller. */ |
39 | 10.3k | cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */ |
40 | 10.3k | if (version != JPEG_LIB_VERSION) |
41 | 0 | ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version); |
42 | 10.3k | if (structsize != sizeof(struct jpeg_decompress_struct)) |
43 | 0 | ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, |
44 | 10.3k | (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 | 10.3k | { |
53 | 10.3k | struct jpeg_error_mgr *err = cinfo->err; |
54 | 10.3k | void *client_data = cinfo->client_data; /* ignore Purify complaint here */ |
55 | 10.3k | memset(cinfo, 0, sizeof(struct jpeg_decompress_struct)); |
56 | 10.3k | cinfo->err = err; |
57 | 10.3k | cinfo->client_data = client_data; |
58 | 10.3k | } |
59 | 10.3k | cinfo->is_decompressor = TRUE; |
60 | | |
61 | | /* Initialize a memory manager instance for this object */ |
62 | 10.3k | jinit_memory_mgr((j_common_ptr)cinfo); |
63 | | |
64 | | /* Zero out pointers to permanent structures. */ |
65 | 10.3k | cinfo->progress = NULL; |
66 | 10.3k | cinfo->src = NULL; |
67 | | |
68 | 51.9k | for (i = 0; i < NUM_QUANT_TBLS; i++) |
69 | 41.5k | cinfo->quant_tbl_ptrs[i] = NULL; |
70 | | |
71 | 51.9k | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
72 | 41.5k | cinfo->dc_huff_tbl_ptrs[i] = NULL; |
73 | 41.5k | cinfo->ac_huff_tbl_ptrs[i] = NULL; |
74 | 41.5k | } |
75 | | |
76 | | /* Initialize marker processor so application can override methods |
77 | | * for COM, APPn markers before calling jpeg_read_header. |
78 | | */ |
79 | 10.3k | cinfo->marker_list = NULL; |
80 | 10.3k | jinit_marker_reader(cinfo); |
81 | | |
82 | | /* And initialize the overall input controller. */ |
83 | 10.3k | jinit_input_controller(cinfo); |
84 | | |
85 | | /* OK, I'm ready */ |
86 | 10.3k | cinfo->global_state = DSTATE_START; |
87 | | |
88 | | /* The master struct is used to store extension parameters, so we allocate it |
89 | | * here. |
90 | | */ |
91 | 10.3k | cinfo->master = (struct jpeg_decomp_master *) |
92 | 10.3k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_PERMANENT, |
93 | 10.3k | sizeof(my_decomp_master)); |
94 | 10.3k | memset(cinfo->master, 0, sizeof(my_decomp_master)); |
95 | 10.3k | } |
96 | | |
97 | | |
98 | | /* |
99 | | * Destruction of a JPEG decompression object |
100 | | */ |
101 | | |
102 | | GLOBAL(void) |
103 | | jpeg_destroy_decompress(j_decompress_ptr cinfo) |
104 | 10.3k | { |
105 | 10.3k | jpeg_destroy((j_common_ptr)cinfo); /* use common routine */ |
106 | 10.3k | } |
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 | | jpeg_abort_decompress(j_decompress_ptr cinfo) |
116 | 26.2k | { |
117 | 26.2k | jpeg_abort((j_common_ptr)cinfo); /* use common routine */ |
118 | 26.2k | } |
119 | | |
120 | | |
121 | | /* |
122 | | * Set default decompression parameters. |
123 | | */ |
124 | | |
125 | | LOCAL(void) |
126 | | default_decompress_parms(j_decompress_ptr cinfo) |
127 | 33.9k | { |
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 | 33.9k | switch (cinfo->num_components) { |
132 | 757 | case 1: |
133 | 757 | cinfo->jpeg_color_space = JCS_GRAYSCALE; |
134 | 757 | cinfo->out_color_space = JCS_GRAYSCALE; |
135 | 757 | break; |
136 | | |
137 | 30.7k | case 3: |
138 | 30.7k | if (cinfo->saw_JFIF_marker) { |
139 | 23.7k | cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */ |
140 | 23.7k | } else if (cinfo->saw_Adobe_marker) { |
141 | 1.37k | switch (cinfo->Adobe_transform) { |
142 | 1.11k | case 0: |
143 | 1.11k | cinfo->jpeg_color_space = JCS_RGB; |
144 | 1.11k | break; |
145 | 18 | case 1: |
146 | 18 | cinfo->jpeg_color_space = JCS_YCbCr; |
147 | 18 | break; |
148 | 242 | default: |
149 | 242 | WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); |
150 | 242 | cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ |
151 | 242 | break; |
152 | 1.37k | } |
153 | 5.57k | } else { |
154 | | /* Saw no special markers, try to guess from the component IDs */ |
155 | 5.57k | int cid0 = cinfo->comp_info[0].component_id; |
156 | 5.57k | int cid1 = cinfo->comp_info[1].component_id; |
157 | 5.57k | int cid2 = cinfo->comp_info[2].component_id; |
158 | | |
159 | 5.57k | if (cid0 == 1 && cid1 == 2 && cid2 == 3) |
160 | 5.26k | cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */ |
161 | 315 | else if (cid0 == 82 && cid1 == 71 && cid2 == 66) |
162 | 0 | cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */ |
163 | 315 | else { |
164 | 315 | TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2); |
165 | 315 | cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */ |
166 | 315 | } |
167 | 5.57k | } |
168 | | /* Always guess RGB is proper output colorspace. */ |
169 | 30.7k | cinfo->out_color_space = JCS_RGB; |
170 | 30.7k | break; |
171 | | |
172 | 1.64k | case 4: |
173 | 1.64k | if (cinfo->saw_Adobe_marker) { |
174 | 696 | switch (cinfo->Adobe_transform) { |
175 | 12 | case 0: |
176 | 12 | cinfo->jpeg_color_space = JCS_CMYK; |
177 | 12 | break; |
178 | 12 | case 2: |
179 | 12 | cinfo->jpeg_color_space = JCS_YCCK; |
180 | 12 | break; |
181 | 672 | default: |
182 | 672 | WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform); |
183 | 672 | cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */ |
184 | 672 | break; |
185 | 696 | } |
186 | 949 | } else { |
187 | | /* No special markers, assume straight CMYK. */ |
188 | 949 | cinfo->jpeg_color_space = JCS_CMYK; |
189 | 949 | } |
190 | 1.64k | cinfo->out_color_space = JCS_CMYK; |
191 | 1.64k | break; |
192 | | |
193 | 818 | default: |
194 | 818 | cinfo->jpeg_color_space = JCS_UNKNOWN; |
195 | 818 | cinfo->out_color_space = JCS_UNKNOWN; |
196 | 818 | break; |
197 | 33.9k | } |
198 | | |
199 | | /* Set defaults for other decompression parameters. */ |
200 | 33.9k | cinfo->scale_num = 1; /* 1:1 scaling */ |
201 | 33.9k | cinfo->scale_denom = 1; |
202 | 33.9k | cinfo->output_gamma = 1.0; |
203 | 33.9k | cinfo->buffered_image = FALSE; |
204 | 33.9k | cinfo->raw_data_out = FALSE; |
205 | 33.9k | cinfo->dct_method = JDCT_DEFAULT; |
206 | 33.9k | cinfo->do_fancy_upsampling = TRUE; |
207 | 33.9k | cinfo->do_block_smoothing = TRUE; |
208 | 33.9k | cinfo->quantize_colors = FALSE; |
209 | | /* We set these in case application only sets quantize_colors. */ |
210 | 33.9k | cinfo->dither_mode = JDITHER_FS; |
211 | 33.9k | #ifdef QUANT_2PASS_SUPPORTED |
212 | 33.9k | cinfo->two_pass_quantize = TRUE; |
213 | | #else |
214 | | cinfo->two_pass_quantize = FALSE; |
215 | | #endif |
216 | 33.9k | cinfo->desired_number_of_colors = 256; |
217 | 33.9k | cinfo->colormap = NULL; |
218 | | /* Initialize for no mode change in buffered-image mode. */ |
219 | 33.9k | cinfo->enable_1pass_quant = FALSE; |
220 | 33.9k | cinfo->enable_external_quant = FALSE; |
221 | 33.9k | cinfo->enable_2pass_quant = FALSE; |
222 | 33.9k | } |
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 | | jpeg_read_header(j_decompress_ptr cinfo, boolean require_image) |
254 | 37.0k | { |
255 | 37.0k | int retcode; |
256 | | |
257 | 37.0k | if (cinfo->global_state != DSTATE_START && |
258 | 37.0k | cinfo->global_state != DSTATE_INHEADER) |
259 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
260 | | |
261 | 37.0k | retcode = jpeg_consume_input(cinfo); |
262 | | |
263 | 37.0k | switch (retcode) { |
264 | 33.9k | case JPEG_REACHED_SOS: |
265 | 33.9k | retcode = JPEG_HEADER_OK; |
266 | 33.9k | break; |
267 | 467 | case JPEG_REACHED_EOI: |
268 | 467 | 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 | 467 | jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */ |
275 | 467 | retcode = JPEG_HEADER_TABLES_ONLY; |
276 | 467 | break; |
277 | 0 | case JPEG_SUSPENDED: |
278 | | /* no work */ |
279 | 0 | break; |
280 | 37.0k | } |
281 | | |
282 | 34.4k | return retcode; |
283 | 37.0k | } |
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 | | jpeg_consume_input(j_decompress_ptr cinfo) |
300 | 37.0k | { |
301 | 37.0k | int retcode = JPEG_SUSPENDED; |
302 | | |
303 | | /* NB: every possible DSTATE value should be listed in this switch */ |
304 | 37.0k | switch (cinfo->global_state) { |
305 | 37.0k | case DSTATE_START: |
306 | | /* Start-of-datastream actions: reset appropriate modules */ |
307 | 37.0k | (*cinfo->inputctl->reset_input_controller) (cinfo); |
308 | | /* Initialize application's data source module */ |
309 | 37.0k | (*cinfo->src->init_source) (cinfo); |
310 | 37.0k | cinfo->global_state = DSTATE_INHEADER; |
311 | | FALLTHROUGH /*FALLTHROUGH*/ |
312 | 37.0k | case DSTATE_INHEADER: |
313 | 37.0k | retcode = (*cinfo->inputctl->consume_input) (cinfo); |
314 | 37.0k | if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */ |
315 | | /* Set up default parameters based on header data */ |
316 | 33.9k | default_decompress_parms(cinfo); |
317 | | /* Set global state: ready for start_decompress */ |
318 | 33.9k | cinfo->global_state = DSTATE_READY; |
319 | 33.9k | } |
320 | 37.0k | break; |
321 | 0 | 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 | 0 | case DSTATE_PRELOAD: |
326 | 0 | case DSTATE_PRESCAN: |
327 | 0 | case DSTATE_SCANNING: |
328 | 0 | case DSTATE_RAW_OK: |
329 | 0 | case DSTATE_BUFIMAGE: |
330 | 0 | case DSTATE_BUFPOST: |
331 | 0 | case DSTATE_STOPPING: |
332 | 0 | retcode = (*cinfo->inputctl->consume_input) (cinfo); |
333 | 0 | break; |
334 | 0 | default: |
335 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
336 | 37.0k | } |
337 | 34.4k | return retcode; |
338 | 37.0k | } |
339 | | |
340 | | |
341 | | /* |
342 | | * Have we finished reading the input file? |
343 | | */ |
344 | | |
345 | | GLOBAL(boolean) |
346 | | jpeg_input_complete(j_decompress_ptr cinfo) |
347 | 0 | { |
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 | 0 | } |
354 | | |
355 | | |
356 | | /* |
357 | | * Is there more than one scan? |
358 | | */ |
359 | | |
360 | | GLOBAL(boolean) |
361 | | jpeg_has_multiple_scans(j_decompress_ptr cinfo) |
362 | 0 | { |
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 | 0 | } |
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 | | jpeg_finish_decompress(j_decompress_ptr cinfo) |
382 | 8.71k | { |
383 | 8.71k | if ((cinfo->global_state == DSTATE_SCANNING || |
384 | 8.71k | cinfo->global_state == DSTATE_RAW_OK) && !cinfo->buffered_image) { |
385 | | /* Terminate final pass of non-buffered mode */ |
386 | 8.71k | if (cinfo->output_scanline < cinfo->output_height) |
387 | 0 | ERREXIT(cinfo, JERR_TOO_LITTLE_DATA); |
388 | 8.71k | (*cinfo->master->finish_output_pass) (cinfo); |
389 | 8.71k | cinfo->global_state = DSTATE_STOPPING; |
390 | 8.71k | } 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 | 0 | } |
397 | | /* Read until EOI */ |
398 | 12.5k | while (!cinfo->inputctl->eoi_reached) { |
399 | 3.78k | if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED) |
400 | 0 | return FALSE; /* Suspend, come back later */ |
401 | 3.78k | } |
402 | | /* Do final cleanup */ |
403 | 8.71k | (*cinfo->src->term_source) (cinfo); |
404 | | /* We can use jpeg_abort to release memory and reset global_state */ |
405 | 8.71k | jpeg_abort((j_common_ptr)cinfo); |
406 | 8.71k | return TRUE; |
407 | 8.71k | } |