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