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