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