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