/src/libjpeg-turbo.2.1.x/jdmaster.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * jdmaster.c  | 
3  |  |  *  | 
4  |  |  * This file was part of the Independent JPEG Group's software:  | 
5  |  |  * Copyright (C) 1991-1997, Thomas G. Lane.  | 
6  |  |  * Modified 2002-2009 by Guido Vollbeding.  | 
7  |  |  * libjpeg-turbo Modifications:  | 
8  |  |  * Copyright (C) 2009-2011, 2016, 2019, 2022-2023, D. R. Commander.  | 
9  |  |  * Copyright (C) 2013, Linaro Limited.  | 
10  |  |  * Copyright (C) 2015, Google, Inc.  | 
11  |  |  * For conditions of distribution and use, see the accompanying README.ijg  | 
12  |  |  * file.  | 
13  |  |  *  | 
14  |  |  * This file contains master control logic for the JPEG decompressor.  | 
15  |  |  * These routines are concerned with selecting the modules to be executed  | 
16  |  |  * and with determining the number of passes and the work to be done in each  | 
17  |  |  * pass.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #define JPEG_INTERNALS  | 
21  |  | #include "jinclude.h"  | 
22  |  | #include "jpeglib.h"  | 
23  |  | #include "jpegcomp.h"  | 
24  |  | #include "jdmaster.h"  | 
25  |  |  | 
26  |  |  | 
27  |  | /*  | 
28  |  |  * Determine whether merged upsample/color conversion should be used.  | 
29  |  |  * CRUCIAL: this must match the actual capabilities of jdmerge.c!  | 
30  |  |  */  | 
31  |  |  | 
32  |  | LOCAL(boolean)  | 
33  |  | use_merged_upsample(j_decompress_ptr cinfo)  | 
34  | 0  | { | 
35  | 0  | #ifdef UPSAMPLE_MERGING_SUPPORTED  | 
36  |  |   /* Merging is the equivalent of plain box-filter upsampling */  | 
37  | 0  |   if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)  | 
38  | 0  |     return FALSE;  | 
39  |  |   /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */  | 
40  | 0  |   if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||  | 
41  | 0  |       (cinfo->out_color_space != JCS_RGB &&  | 
42  | 0  |        cinfo->out_color_space != JCS_RGB565 &&  | 
43  | 0  |        cinfo->out_color_space != JCS_EXT_RGB &&  | 
44  | 0  |        cinfo->out_color_space != JCS_EXT_RGBX &&  | 
45  | 0  |        cinfo->out_color_space != JCS_EXT_BGR &&  | 
46  | 0  |        cinfo->out_color_space != JCS_EXT_BGRX &&  | 
47  | 0  |        cinfo->out_color_space != JCS_EXT_XBGR &&  | 
48  | 0  |        cinfo->out_color_space != JCS_EXT_XRGB &&  | 
49  | 0  |        cinfo->out_color_space != JCS_EXT_RGBA &&  | 
50  | 0  |        cinfo->out_color_space != JCS_EXT_BGRA &&  | 
51  | 0  |        cinfo->out_color_space != JCS_EXT_ABGR &&  | 
52  | 0  |        cinfo->out_color_space != JCS_EXT_ARGB))  | 
53  | 0  |     return FALSE;  | 
54  | 0  |   if ((cinfo->out_color_space == JCS_RGB565 &&  | 
55  | 0  |        cinfo->out_color_components != 3) ||  | 
56  | 0  |       (cinfo->out_color_space != JCS_RGB565 &&  | 
57  | 0  |        cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))  | 
58  | 0  |     return FALSE;  | 
59  |  |   /* and it only handles 2h1v or 2h2v sampling ratios */  | 
60  | 0  |   if (cinfo->comp_info[0].h_samp_factor != 2 ||  | 
61  | 0  |       cinfo->comp_info[1].h_samp_factor != 1 ||  | 
62  | 0  |       cinfo->comp_info[2].h_samp_factor != 1 ||  | 
63  | 0  |       cinfo->comp_info[0].v_samp_factor >  2 ||  | 
64  | 0  |       cinfo->comp_info[1].v_samp_factor != 1 ||  | 
65  | 0  |       cinfo->comp_info[2].v_samp_factor != 1)  | 
66  | 0  |     return FALSE;  | 
67  |  |   /* furthermore, it doesn't work if we've scaled the IDCTs differently */  | 
68  | 0  |   if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||  | 
69  | 0  |       cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||  | 
70  | 0  |       cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)  | 
71  | 0  |     return FALSE;  | 
72  |  |   /* ??? also need to test for upsample-time rescaling, when & if supported */  | 
73  | 0  |   return TRUE;                  /* by golly, it'll work... */  | 
74  |  | #else  | 
75  |  |   return FALSE;  | 
76  |  | #endif  | 
77  | 0  | }  | 
78  |  |  | 
79  |  |  | 
80  |  | /*  | 
81  |  |  * Compute output image dimensions and related values.  | 
82  |  |  * NOTE: this is exported for possible use by application.  | 
83  |  |  * Hence it mustn't do anything that can't be done twice.  | 
84  |  |  */  | 
85  |  |  | 
86  |  | #if JPEG_LIB_VERSION >= 80  | 
87  |  | GLOBAL(void)  | 
88  |  | #else  | 
89  |  | LOCAL(void)  | 
90  |  | #endif  | 
91  |  | jpeg_core_output_dimensions(j_decompress_ptr cinfo)  | 
92  |  | /* Do computations that are needed before master selection phase.  | 
93  |  |  * This function is used for transcoding and full decompression.  | 
94  |  |  */  | 
95  | 0  | { | 
96  | 0  | #ifdef IDCT_SCALING_SUPPORTED  | 
97  | 0  |   int ci;  | 
98  | 0  |   jpeg_component_info *compptr;  | 
99  |  |  | 
100  |  |   /* Compute actual output image dimensions and DCT scaling choices. */  | 
101  | 0  |   if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) { | 
102  |  |     /* Provide 1/block_size scaling */  | 
103  | 0  |     cinfo->output_width = (JDIMENSION)  | 
104  | 0  |       jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);  | 
105  | 0  |     cinfo->output_height = (JDIMENSION)  | 
106  | 0  |       jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);  | 
107  | 0  |     cinfo->_min_DCT_h_scaled_size = 1;  | 
108  | 0  |     cinfo->_min_DCT_v_scaled_size = 1;  | 
109  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) { | 
110  |  |     /* Provide 2/block_size scaling */  | 
111  | 0  |     cinfo->output_width = (JDIMENSION)  | 
112  | 0  |       jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);  | 
113  | 0  |     cinfo->output_height = (JDIMENSION)  | 
114  | 0  |       jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);  | 
115  | 0  |     cinfo->_min_DCT_h_scaled_size = 2;  | 
116  | 0  |     cinfo->_min_DCT_v_scaled_size = 2;  | 
117  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) { | 
118  |  |     /* Provide 3/block_size scaling */  | 
119  | 0  |     cinfo->output_width = (JDIMENSION)  | 
120  | 0  |       jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);  | 
121  | 0  |     cinfo->output_height = (JDIMENSION)  | 
122  | 0  |       jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);  | 
123  | 0  |     cinfo->_min_DCT_h_scaled_size = 3;  | 
124  | 0  |     cinfo->_min_DCT_v_scaled_size = 3;  | 
125  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) { | 
126  |  |     /* Provide 4/block_size scaling */  | 
127  | 0  |     cinfo->output_width = (JDIMENSION)  | 
128  | 0  |       jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);  | 
129  | 0  |     cinfo->output_height = (JDIMENSION)  | 
130  | 0  |       jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);  | 
131  | 0  |     cinfo->_min_DCT_h_scaled_size = 4;  | 
132  | 0  |     cinfo->_min_DCT_v_scaled_size = 4;  | 
133  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) { | 
134  |  |     /* Provide 5/block_size scaling */  | 
135  | 0  |     cinfo->output_width = (JDIMENSION)  | 
136  | 0  |       jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);  | 
137  | 0  |     cinfo->output_height = (JDIMENSION)  | 
138  | 0  |       jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);  | 
139  | 0  |     cinfo->_min_DCT_h_scaled_size = 5;  | 
140  | 0  |     cinfo->_min_DCT_v_scaled_size = 5;  | 
141  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) { | 
142  |  |     /* Provide 6/block_size scaling */  | 
143  | 0  |     cinfo->output_width = (JDIMENSION)  | 
144  | 0  |       jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);  | 
145  | 0  |     cinfo->output_height = (JDIMENSION)  | 
146  | 0  |       jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);  | 
147  | 0  |     cinfo->_min_DCT_h_scaled_size = 6;  | 
148  | 0  |     cinfo->_min_DCT_v_scaled_size = 6;  | 
149  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) { | 
150  |  |     /* Provide 7/block_size scaling */  | 
151  | 0  |     cinfo->output_width = (JDIMENSION)  | 
152  | 0  |       jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);  | 
153  | 0  |     cinfo->output_height = (JDIMENSION)  | 
154  | 0  |       jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);  | 
155  | 0  |     cinfo->_min_DCT_h_scaled_size = 7;  | 
156  | 0  |     cinfo->_min_DCT_v_scaled_size = 7;  | 
157  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) { | 
158  |  |     /* Provide 8/block_size scaling */  | 
159  | 0  |     cinfo->output_width = (JDIMENSION)  | 
160  | 0  |       jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);  | 
161  | 0  |     cinfo->output_height = (JDIMENSION)  | 
162  | 0  |       jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);  | 
163  | 0  |     cinfo->_min_DCT_h_scaled_size = 8;  | 
164  | 0  |     cinfo->_min_DCT_v_scaled_size = 8;  | 
165  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) { | 
166  |  |     /* Provide 9/block_size scaling */  | 
167  | 0  |     cinfo->output_width = (JDIMENSION)  | 
168  | 0  |       jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);  | 
169  | 0  |     cinfo->output_height = (JDIMENSION)  | 
170  | 0  |       jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);  | 
171  | 0  |     cinfo->_min_DCT_h_scaled_size = 9;  | 
172  | 0  |     cinfo->_min_DCT_v_scaled_size = 9;  | 
173  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) { | 
174  |  |     /* Provide 10/block_size scaling */  | 
175  | 0  |     cinfo->output_width = (JDIMENSION)  | 
176  | 0  |       jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);  | 
177  | 0  |     cinfo->output_height = (JDIMENSION)  | 
178  | 0  |       jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);  | 
179  | 0  |     cinfo->_min_DCT_h_scaled_size = 10;  | 
180  | 0  |     cinfo->_min_DCT_v_scaled_size = 10;  | 
181  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) { | 
182  |  |     /* Provide 11/block_size scaling */  | 
183  | 0  |     cinfo->output_width = (JDIMENSION)  | 
184  | 0  |       jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);  | 
185  | 0  |     cinfo->output_height = (JDIMENSION)  | 
186  | 0  |       jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);  | 
187  | 0  |     cinfo->_min_DCT_h_scaled_size = 11;  | 
188  | 0  |     cinfo->_min_DCT_v_scaled_size = 11;  | 
189  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) { | 
190  |  |     /* Provide 12/block_size scaling */  | 
191  | 0  |     cinfo->output_width = (JDIMENSION)  | 
192  | 0  |       jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);  | 
193  | 0  |     cinfo->output_height = (JDIMENSION)  | 
194  | 0  |       jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);  | 
195  | 0  |     cinfo->_min_DCT_h_scaled_size = 12;  | 
196  | 0  |     cinfo->_min_DCT_v_scaled_size = 12;  | 
197  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) { | 
198  |  |     /* Provide 13/block_size scaling */  | 
199  | 0  |     cinfo->output_width = (JDIMENSION)  | 
200  | 0  |       jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);  | 
201  | 0  |     cinfo->output_height = (JDIMENSION)  | 
202  | 0  |       jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);  | 
203  | 0  |     cinfo->_min_DCT_h_scaled_size = 13;  | 
204  | 0  |     cinfo->_min_DCT_v_scaled_size = 13;  | 
205  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) { | 
206  |  |     /* Provide 14/block_size scaling */  | 
207  | 0  |     cinfo->output_width = (JDIMENSION)  | 
208  | 0  |       jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);  | 
209  | 0  |     cinfo->output_height = (JDIMENSION)  | 
210  | 0  |       jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);  | 
211  | 0  |     cinfo->_min_DCT_h_scaled_size = 14;  | 
212  | 0  |     cinfo->_min_DCT_v_scaled_size = 14;  | 
213  | 0  |   } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) { | 
214  |  |     /* Provide 15/block_size scaling */  | 
215  | 0  |     cinfo->output_width = (JDIMENSION)  | 
216  | 0  |       jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);  | 
217  | 0  |     cinfo->output_height = (JDIMENSION)  | 
218  | 0  |       jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);  | 
219  | 0  |     cinfo->_min_DCT_h_scaled_size = 15;  | 
220  | 0  |     cinfo->_min_DCT_v_scaled_size = 15;  | 
221  | 0  |   } else { | 
222  |  |     /* Provide 16/block_size scaling */  | 
223  | 0  |     cinfo->output_width = (JDIMENSION)  | 
224  | 0  |       jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);  | 
225  | 0  |     cinfo->output_height = (JDIMENSION)  | 
226  | 0  |       jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);  | 
227  | 0  |     cinfo->_min_DCT_h_scaled_size = 16;  | 
228  | 0  |     cinfo->_min_DCT_v_scaled_size = 16;  | 
229  | 0  |   }  | 
230  |  |  | 
231  |  |   /* Recompute dimensions of components */  | 
232  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
233  | 0  |        ci++, compptr++) { | 
234  | 0  |     compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;  | 
235  | 0  |     compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;  | 
236  | 0  |   }  | 
237  |  | 
  | 
238  |  | #else /* !IDCT_SCALING_SUPPORTED */  | 
239  |  |  | 
240  |  |   /* Hardwire it to "no scaling" */  | 
241  |  |   cinfo->output_width = cinfo->image_width;  | 
242  |  |   cinfo->output_height = cinfo->image_height;  | 
243  |  |   /* jdinput.c has already initialized DCT_scaled_size,  | 
244  |  |    * and has computed unscaled downsampled_width and downsampled_height.  | 
245  |  |    */  | 
246  |  |  | 
247  |  | #endif /* IDCT_SCALING_SUPPORTED */  | 
248  | 0  | }  | 
249  |  |  | 
250  |  |  | 
251  |  | /*  | 
252  |  |  * Compute output image dimensions and related values.  | 
253  |  |  * NOTE: this is exported for possible use by application.  | 
254  |  |  * Hence it mustn't do anything that can't be done twice.  | 
255  |  |  * Also note that it may be called before the master module is initialized!  | 
256  |  |  */  | 
257  |  |  | 
258  |  | GLOBAL(void)  | 
259  |  | jpeg_calc_output_dimensions(j_decompress_ptr cinfo)  | 
260  |  | /* Do computations that are needed before master selection phase */  | 
261  | 0  | { | 
262  | 0  | #ifdef IDCT_SCALING_SUPPORTED  | 
263  | 0  |   int ci;  | 
264  | 0  |   jpeg_component_info *compptr;  | 
265  | 0  | #endif  | 
266  |  |  | 
267  |  |   /* Prevent application from calling me at wrong times */  | 
268  | 0  |   if (cinfo->global_state != DSTATE_READY)  | 
269  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
270  |  |  | 
271  |  |   /* Compute core output image dimensions and DCT scaling choices. */  | 
272  | 0  |   jpeg_core_output_dimensions(cinfo);  | 
273  |  | 
  | 
274  | 0  | #ifdef IDCT_SCALING_SUPPORTED  | 
275  |  |  | 
276  |  |   /* In selecting the actual DCT scaling for each component, we try to  | 
277  |  |    * scale up the chroma components via IDCT scaling rather than upsampling.  | 
278  |  |    * This saves time if the upsampler gets to use 1:1 scaling.  | 
279  |  |    * Note this code adapts subsampling ratios which are powers of 2.  | 
280  |  |    */  | 
281  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
282  | 0  |        ci++, compptr++) { | 
283  | 0  |     int ssize = cinfo->_min_DCT_scaled_size;  | 
284  | 0  |     while (ssize < DCTSIZE &&  | 
285  | 0  |            ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %  | 
286  | 0  |             (compptr->h_samp_factor * ssize * 2) == 0) &&  | 
287  | 0  |            ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %  | 
288  | 0  |             (compptr->v_samp_factor * ssize * 2) == 0)) { | 
289  | 0  |       ssize = ssize * 2;  | 
290  | 0  |     }  | 
291  |  | #if JPEG_LIB_VERSION >= 70  | 
292  |  |     compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;  | 
293  |  | #else  | 
294  | 0  |     compptr->DCT_scaled_size = ssize;  | 
295  | 0  | #endif  | 
296  | 0  |   }  | 
297  |  |  | 
298  |  |   /* Recompute downsampled dimensions of components;  | 
299  |  |    * application needs to know these if using raw downsampled data.  | 
300  |  |    */  | 
301  | 0  |   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;  | 
302  | 0  |        ci++, compptr++) { | 
303  |  |     /* Size in samples, after IDCT scaling */  | 
304  | 0  |     compptr->downsampled_width = (JDIMENSION)  | 
305  | 0  |       jdiv_round_up((long)cinfo->image_width *  | 
306  | 0  |                     (long)(compptr->h_samp_factor * compptr->_DCT_scaled_size),  | 
307  | 0  |                     (long)(cinfo->max_h_samp_factor * DCTSIZE));  | 
308  | 0  |     compptr->downsampled_height = (JDIMENSION)  | 
309  | 0  |       jdiv_round_up((long)cinfo->image_height *  | 
310  | 0  |                     (long)(compptr->v_samp_factor * compptr->_DCT_scaled_size),  | 
311  | 0  |                     (long)(cinfo->max_v_samp_factor * DCTSIZE));  | 
312  | 0  |   }  | 
313  |  | 
  | 
314  |  | #else /* !IDCT_SCALING_SUPPORTED */  | 
315  |  |  | 
316  |  |   /* Hardwire it to "no scaling" */  | 
317  |  |   cinfo->output_width = cinfo->image_width;  | 
318  |  |   cinfo->output_height = cinfo->image_height;  | 
319  |  |   /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,  | 
320  |  |    * and has computed unscaled downsampled_width and downsampled_height.  | 
321  |  |    */  | 
322  |  |  | 
323  |  | #endif /* IDCT_SCALING_SUPPORTED */  | 
324  |  |  | 
325  |  |   /* Report number of components in selected colorspace. */  | 
326  |  |   /* Probably this should be in the color conversion module... */  | 
327  | 0  |   switch (cinfo->out_color_space) { | 
328  | 0  |   case JCS_GRAYSCALE:  | 
329  | 0  |     cinfo->out_color_components = 1;  | 
330  | 0  |     break;  | 
331  | 0  |   case JCS_RGB:  | 
332  | 0  |   case JCS_EXT_RGB:  | 
333  | 0  |   case JCS_EXT_RGBX:  | 
334  | 0  |   case JCS_EXT_BGR:  | 
335  | 0  |   case JCS_EXT_BGRX:  | 
336  | 0  |   case JCS_EXT_XBGR:  | 
337  | 0  |   case JCS_EXT_XRGB:  | 
338  | 0  |   case JCS_EXT_RGBA:  | 
339  | 0  |   case JCS_EXT_BGRA:  | 
340  | 0  |   case JCS_EXT_ABGR:  | 
341  | 0  |   case JCS_EXT_ARGB:  | 
342  | 0  |     cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];  | 
343  | 0  |     break;  | 
344  | 0  |   case JCS_YCbCr:  | 
345  | 0  |   case JCS_RGB565:  | 
346  | 0  |     cinfo->out_color_components = 3;  | 
347  | 0  |     break;  | 
348  | 0  |   case JCS_CMYK:  | 
349  | 0  |   case JCS_YCCK:  | 
350  | 0  |     cinfo->out_color_components = 4;  | 
351  | 0  |     break;  | 
352  | 0  |   default:                      /* else must be same colorspace as in file */  | 
353  | 0  |     cinfo->out_color_components = cinfo->num_components;  | 
354  | 0  |     break;  | 
355  | 0  |   }  | 
356  | 0  |   cinfo->output_components = (cinfo->quantize_colors ? 1 :  | 
357  | 0  |                               cinfo->out_color_components);  | 
358  |  |  | 
359  |  |   /* See if upsampler will want to emit more than one row at a time */  | 
360  | 0  |   if (use_merged_upsample(cinfo))  | 
361  | 0  |     cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;  | 
362  | 0  |   else  | 
363  | 0  |     cinfo->rec_outbuf_height = 1;  | 
364  | 0  | }  | 
365  |  |  | 
366  |  |  | 
367  |  | /*  | 
368  |  |  * Several decompression processes need to range-limit values to the range  | 
369  |  |  * 0..MAXJSAMPLE; the input value may fall somewhat outside this range  | 
370  |  |  * due to noise introduced by quantization, roundoff error, etc.  These  | 
371  |  |  * processes are inner loops and need to be as fast as possible.  On most  | 
372  |  |  * machines, particularly CPUs with pipelines or instruction prefetch,  | 
373  |  |  * a (subscript-check-less) C table lookup  | 
374  |  |  *              x = sample_range_limit[x];  | 
375  |  |  * is faster than explicit tests  | 
376  |  |  *              if (x < 0)  x = 0;  | 
377  |  |  *              else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;  | 
378  |  |  * These processes all use a common table prepared by the routine below.  | 
379  |  |  *  | 
380  |  |  * For most steps we can mathematically guarantee that the initial value  | 
381  |  |  * of x is within MAXJSAMPLE+1 of the legal range, so a table running from  | 
382  |  |  * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial  | 
383  |  |  * limiting step (just after the IDCT), a wildly out-of-range value is  | 
384  |  |  * possible if the input data is corrupt.  To avoid any chance of indexing  | 
385  |  |  * off the end of memory and getting a bad-pointer trap, we perform the  | 
386  |  |  * post-IDCT limiting thus:  | 
387  |  |  *              x = range_limit[x & MASK];  | 
388  |  |  * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit  | 
389  |  |  * samples.  Under normal circumstances this is more than enough range and  | 
390  |  |  * a correct output will be generated; with bogus input data the mask will  | 
391  |  |  * cause wraparound, and we will safely generate a bogus-but-in-range output.  | 
392  |  |  * For the post-IDCT step, we want to convert the data from signed to unsigned  | 
393  |  |  * representation by adding CENTERJSAMPLE at the same time that we limit it.  | 
394  |  |  * So the post-IDCT limiting table ends up looking like this:  | 
395  |  |  *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,  | 
396  |  |  *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),  | 
397  |  |  *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),  | 
398  |  |  *   0,1,...,CENTERJSAMPLE-1  | 
399  |  |  * Negative inputs select values from the upper half of the table after  | 
400  |  |  * masking.  | 
401  |  |  *  | 
402  |  |  * We can save some space by overlapping the start of the post-IDCT table  | 
403  |  |  * with the simpler range limiting table.  The post-IDCT table begins at  | 
404  |  |  * sample_range_limit + CENTERJSAMPLE.  | 
405  |  |  */  | 
406  |  |  | 
407  |  | LOCAL(void)  | 
408  |  | prepare_range_limit_table(j_decompress_ptr cinfo)  | 
409  |  | /* Allocate and fill in the sample_range_limit table */  | 
410  | 0  | { | 
411  | 0  |   JSAMPLE *table;  | 
412  | 0  |   int i;  | 
413  |  | 
  | 
414  | 0  |   table = (JSAMPLE *)  | 
415  | 0  |     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,  | 
416  | 0  |                 (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));  | 
417  | 0  |   table += (MAXJSAMPLE + 1);    /* allow negative subscripts of simple table */  | 
418  | 0  |   cinfo->sample_range_limit = table;  | 
419  |  |   /* First segment of "simple" table: limit[x] = 0 for x < 0 */  | 
420  | 0  |   memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE));  | 
421  |  |   /* Main part of "simple" table: limit[x] = x */  | 
422  | 0  |   for (i = 0; i <= MAXJSAMPLE; i++)  | 
423  | 0  |     table[i] = (JSAMPLE)i;  | 
424  | 0  |   table += CENTERJSAMPLE;       /* Point to where post-IDCT table starts */  | 
425  |  |   /* End of simple table, rest of first half of post-IDCT table */  | 
426  | 0  |   for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)  | 
427  | 0  |     table[i] = MAXJSAMPLE;  | 
428  |  |   /* Second half of post-IDCT table */  | 
429  | 0  |   memset(table + (2 * (MAXJSAMPLE + 1)), 0,  | 
430  | 0  |          (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));  | 
431  | 0  |   memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),  | 
432  | 0  |          cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));  | 
433  | 0  | }  | 
434  |  |  | 
435  |  |  | 
436  |  | /*  | 
437  |  |  * Master selection of decompression modules.  | 
438  |  |  * This is done once at jpeg_start_decompress time.  We determine  | 
439  |  |  * which modules will be used and give them appropriate initialization calls.  | 
440  |  |  * We also initialize the decompressor input side to begin consuming data.  | 
441  |  |  *  | 
442  |  |  * Since jpeg_read_header has finished, we know what is in the SOF  | 
443  |  |  * and (first) SOS markers.  We also have all the application parameter  | 
444  |  |  * settings.  | 
445  |  |  */  | 
446  |  |  | 
447  |  | LOCAL(void)  | 
448  |  | master_selection(j_decompress_ptr cinfo)  | 
449  | 0  | { | 
450  | 0  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
451  | 0  |   boolean use_c_buffer;  | 
452  | 0  |   long samplesperrow;  | 
453  | 0  |   JDIMENSION jd_samplesperrow;  | 
454  |  |  | 
455  |  |   /* Initialize dimensions and other stuff */  | 
456  | 0  |   jpeg_calc_output_dimensions(cinfo);  | 
457  | 0  |   prepare_range_limit_table(cinfo);  | 
458  |  |  | 
459  |  |   /* Width of an output scanline must be representable as JDIMENSION. */  | 
460  | 0  |   samplesperrow = (long)cinfo->output_width *  | 
461  | 0  |                   (long)cinfo->out_color_components;  | 
462  | 0  |   jd_samplesperrow = (JDIMENSION)samplesperrow;  | 
463  | 0  |   if ((long)jd_samplesperrow != samplesperrow)  | 
464  | 0  |     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);  | 
465  |  |  | 
466  |  |   /* Initialize my private state */  | 
467  | 0  |   master->pass_number = 0;  | 
468  | 0  |   master->using_merged_upsample = use_merged_upsample(cinfo);  | 
469  |  |  | 
470  |  |   /* Color quantizer selection */  | 
471  | 0  |   master->quantizer_1pass = NULL;  | 
472  | 0  |   master->quantizer_2pass = NULL;  | 
473  |  |   /* No mode changes if not using buffered-image mode. */  | 
474  | 0  |   if (!cinfo->quantize_colors || !cinfo->buffered_image) { | 
475  | 0  |     cinfo->enable_1pass_quant = FALSE;  | 
476  | 0  |     cinfo->enable_external_quant = FALSE;  | 
477  | 0  |     cinfo->enable_2pass_quant = FALSE;  | 
478  | 0  |   }  | 
479  | 0  |   if (cinfo->quantize_colors) { | 
480  | 0  |     if (cinfo->raw_data_out)  | 
481  | 0  |       ERREXIT(cinfo, JERR_NOTIMPL);  | 
482  |  |     /* 2-pass quantizer only works in 3-component color space. */  | 
483  | 0  |     if (cinfo->out_color_components != 3 ||  | 
484  | 0  |         cinfo->out_color_space == JCS_RGB565) { | 
485  | 0  |       cinfo->enable_1pass_quant = TRUE;  | 
486  | 0  |       cinfo->enable_external_quant = FALSE;  | 
487  | 0  |       cinfo->enable_2pass_quant = FALSE;  | 
488  | 0  |       cinfo->colormap = NULL;  | 
489  | 0  |     } else if (cinfo->colormap != NULL) { | 
490  | 0  |       cinfo->enable_external_quant = TRUE;  | 
491  | 0  |     } else if (cinfo->two_pass_quantize) { | 
492  | 0  |       cinfo->enable_2pass_quant = TRUE;  | 
493  | 0  |     } else { | 
494  | 0  |       cinfo->enable_1pass_quant = TRUE;  | 
495  | 0  |     }  | 
496  |  | 
  | 
497  | 0  |     if (cinfo->enable_1pass_quant) { | 
498  | 0  | #ifdef QUANT_1PASS_SUPPORTED  | 
499  | 0  |       jinit_1pass_quantizer(cinfo);  | 
500  | 0  |       master->quantizer_1pass = cinfo->cquantize;  | 
501  |  | #else  | 
502  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
503  |  | #endif  | 
504  | 0  |     }  | 
505  |  |  | 
506  |  |     /* We use the 2-pass code to map to external colormaps. */  | 
507  | 0  |     if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { | 
508  | 0  | #ifdef QUANT_2PASS_SUPPORTED  | 
509  | 0  |       jinit_2pass_quantizer(cinfo);  | 
510  | 0  |       master->quantizer_2pass = cinfo->cquantize;  | 
511  |  | #else  | 
512  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
513  |  | #endif  | 
514  | 0  |     }  | 
515  |  |     /* If both quantizers are initialized, the 2-pass one is left active;  | 
516  |  |      * this is necessary for starting with quantization to an external map.  | 
517  |  |      */  | 
518  | 0  |   }  | 
519  |  |  | 
520  |  |   /* Post-processing: in particular, color conversion first */  | 
521  | 0  |   if (!cinfo->raw_data_out) { | 
522  | 0  |     if (master->using_merged_upsample) { | 
523  | 0  | #ifdef UPSAMPLE_MERGING_SUPPORTED  | 
524  | 0  |       jinit_merged_upsampler(cinfo); /* does color conversion too */  | 
525  |  | #else  | 
526  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
527  |  | #endif  | 
528  | 0  |     } else { | 
529  | 0  |       jinit_color_deconverter(cinfo);  | 
530  | 0  |       jinit_upsampler(cinfo);  | 
531  | 0  |     }  | 
532  | 0  |     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);  | 
533  | 0  |   }  | 
534  |  |   /* Inverse DCT */  | 
535  | 0  |   jinit_inverse_dct(cinfo);  | 
536  |  |   /* Entropy decoding: either Huffman or arithmetic coding. */  | 
537  | 0  |   if (cinfo->arith_code) { | 
538  | 0  | #ifdef D_ARITH_CODING_SUPPORTED  | 
539  | 0  |     jinit_arith_decoder(cinfo);  | 
540  |  | #else  | 
541  |  |     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);  | 
542  |  | #endif  | 
543  | 0  |   } else { | 
544  | 0  |     if (cinfo->progressive_mode) { | 
545  | 0  | #ifdef D_PROGRESSIVE_SUPPORTED  | 
546  | 0  |       jinit_phuff_decoder(cinfo);  | 
547  |  | #else  | 
548  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
549  |  | #endif  | 
550  | 0  |     } else  | 
551  | 0  |       jinit_huff_decoder(cinfo);  | 
552  | 0  |   }  | 
553  |  |  | 
554  |  |   /* Initialize principal buffer controllers. */  | 
555  | 0  |   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;  | 
556  | 0  |   jinit_d_coef_controller(cinfo, use_c_buffer);  | 
557  |  | 
  | 
558  | 0  |   if (!cinfo->raw_data_out)  | 
559  | 0  |     jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);  | 
560  |  |  | 
561  |  |   /* We can now tell the memory manager to allocate virtual arrays. */  | 
562  | 0  |   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);  | 
563  |  |  | 
564  |  |   /* Initialize input side of decompressor to consume first scan. */  | 
565  | 0  |   (*cinfo->inputctl->start_input_pass) (cinfo);  | 
566  |  |  | 
567  |  |   /* Set the first and last iMCU columns to decompress from single-scan images.  | 
568  |  |    * By default, decompress all of the iMCU columns.  | 
569  |  |    */  | 
570  | 0  |   cinfo->master->first_iMCU_col = 0;  | 
571  | 0  |   cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;  | 
572  | 0  |   cinfo->master->last_good_iMCU_row = 0;  | 
573  |  | 
  | 
574  | 0  | #ifdef D_MULTISCAN_FILES_SUPPORTED  | 
575  |  |   /* If jpeg_start_decompress will read the whole file, initialize  | 
576  |  |    * progress monitoring appropriately.  The input step is counted  | 
577  |  |    * as one pass.  | 
578  |  |    */  | 
579  | 0  |   if (cinfo->progress != NULL && !cinfo->buffered_image &&  | 
580  | 0  |       cinfo->inputctl->has_multiple_scans) { | 
581  | 0  |     int nscans;  | 
582  |  |     /* Estimate number of scans to set pass_limit. */  | 
583  | 0  |     if (cinfo->progressive_mode) { | 
584  |  |       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */  | 
585  | 0  |       nscans = 2 + 3 * cinfo->num_components;  | 
586  | 0  |     } else { | 
587  |  |       /* For a nonprogressive multiscan file, estimate 1 scan per component. */  | 
588  | 0  |       nscans = cinfo->num_components;  | 
589  | 0  |     }  | 
590  | 0  |     cinfo->progress->pass_counter = 0L;  | 
591  | 0  |     cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;  | 
592  | 0  |     cinfo->progress->completed_passes = 0;  | 
593  | 0  |     cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);  | 
594  |  |     /* Count the input pass as done */  | 
595  | 0  |     master->pass_number++;  | 
596  | 0  |   }  | 
597  | 0  | #endif /* D_MULTISCAN_FILES_SUPPORTED */  | 
598  | 0  | }  | 
599  |  |  | 
600  |  |  | 
601  |  | /*  | 
602  |  |  * Per-pass setup.  | 
603  |  |  * This is called at the beginning of each output pass.  We determine which  | 
604  |  |  * modules will be active during this pass and give them appropriate  | 
605  |  |  * start_pass calls.  We also set is_dummy_pass to indicate whether this  | 
606  |  |  * is a "real" output pass or a dummy pass for color quantization.  | 
607  |  |  * (In the latter case, jdapistd.c will crank the pass to completion.)  | 
608  |  |  */  | 
609  |  |  | 
610  |  | METHODDEF(void)  | 
611  |  | prepare_for_output_pass(j_decompress_ptr cinfo)  | 
612  | 0  | { | 
613  | 0  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
614  |  | 
  | 
615  | 0  |   if (master->pub.is_dummy_pass) { | 
616  | 0  | #ifdef QUANT_2PASS_SUPPORTED  | 
617  |  |     /* Final pass of 2-pass quantization */  | 
618  | 0  |     master->pub.is_dummy_pass = FALSE;  | 
619  | 0  |     (*cinfo->cquantize->start_pass) (cinfo, FALSE);  | 
620  | 0  |     (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);  | 
621  | 0  |     (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);  | 
622  |  | #else  | 
623  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
624  |  | #endif /* QUANT_2PASS_SUPPORTED */  | 
625  | 0  |   } else { | 
626  | 0  |     if (cinfo->quantize_colors && cinfo->colormap == NULL) { | 
627  |  |       /* Select new quantization method */  | 
628  | 0  |       if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { | 
629  | 0  |         cinfo->cquantize = master->quantizer_2pass;  | 
630  | 0  |         master->pub.is_dummy_pass = TRUE;  | 
631  | 0  |       } else if (cinfo->enable_1pass_quant) { | 
632  | 0  |         cinfo->cquantize = master->quantizer_1pass;  | 
633  | 0  |       } else { | 
634  | 0  |         ERREXIT(cinfo, JERR_MODE_CHANGE);  | 
635  | 0  |       }  | 
636  | 0  |     }  | 
637  | 0  |     (*cinfo->idct->start_pass) (cinfo);  | 
638  | 0  |     (*cinfo->coef->start_output_pass) (cinfo);  | 
639  | 0  |     if (!cinfo->raw_data_out) { | 
640  | 0  |       if (!master->using_merged_upsample)  | 
641  | 0  |         (*cinfo->cconvert->start_pass) (cinfo);  | 
642  | 0  |       (*cinfo->upsample->start_pass) (cinfo);  | 
643  | 0  |       if (cinfo->quantize_colors)  | 
644  | 0  |         (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);  | 
645  | 0  |       (*cinfo->post->start_pass) (cinfo,  | 
646  | 0  |             (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));  | 
647  | 0  |       (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);  | 
648  | 0  |     }  | 
649  | 0  |   }  | 
650  |  |  | 
651  |  |   /* Set up progress monitor's pass info if present */  | 
652  | 0  |   if (cinfo->progress != NULL) { | 
653  | 0  |     cinfo->progress->completed_passes = master->pass_number;  | 
654  | 0  |     cinfo->progress->total_passes = master->pass_number +  | 
655  | 0  |                                     (master->pub.is_dummy_pass ? 2 : 1);  | 
656  |  |     /* In buffered-image mode, we assume one more output pass if EOI not  | 
657  |  |      * yet reached, but no more passes if EOI has been reached.  | 
658  |  |      */  | 
659  | 0  |     if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) { | 
660  | 0  |       cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);  | 
661  | 0  |     }  | 
662  | 0  |   }  | 
663  | 0  | }  | 
664  |  |  | 
665  |  |  | 
666  |  | /*  | 
667  |  |  * Finish up at end of an output pass.  | 
668  |  |  */  | 
669  |  |  | 
670  |  | METHODDEF(void)  | 
671  |  | finish_output_pass(j_decompress_ptr cinfo)  | 
672  | 0  | { | 
673  | 0  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
674  |  | 
  | 
675  | 0  |   if (cinfo->quantize_colors)  | 
676  | 0  |     (*cinfo->cquantize->finish_pass) (cinfo);  | 
677  | 0  |   master->pass_number++;  | 
678  | 0  | }  | 
679  |  |  | 
680  |  |  | 
681  |  | #ifdef D_MULTISCAN_FILES_SUPPORTED  | 
682  |  |  | 
683  |  | /*  | 
684  |  |  * Switch to a new external colormap between output passes.  | 
685  |  |  */  | 
686  |  |  | 
687  |  | GLOBAL(void)  | 
688  |  | jpeg_new_colormap(j_decompress_ptr cinfo)  | 
689  | 0  | { | 
690  | 0  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
691  |  |  | 
692  |  |   /* Prevent application from calling me at wrong times */  | 
693  | 0  |   if (cinfo->global_state != DSTATE_BUFIMAGE)  | 
694  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
695  |  | 
  | 
696  | 0  |   if (cinfo->quantize_colors && cinfo->enable_external_quant &&  | 
697  | 0  |       cinfo->colormap != NULL) { | 
698  |  |     /* Select 2-pass quantizer for external colormap use */  | 
699  | 0  |     cinfo->cquantize = master->quantizer_2pass;  | 
700  |  |     /* Notify quantizer of colormap change */  | 
701  | 0  |     (*cinfo->cquantize->new_color_map) (cinfo);  | 
702  | 0  |     master->pub.is_dummy_pass = FALSE; /* just in case */  | 
703  | 0  |   } else  | 
704  | 0  |     ERREXIT(cinfo, JERR_MODE_CHANGE);  | 
705  | 0  | }  | 
706  |  |  | 
707  |  | #endif /* D_MULTISCAN_FILES_SUPPORTED */  | 
708  |  |  | 
709  |  |  | 
710  |  | /*  | 
711  |  |  * Initialize master decompression control and select active modules.  | 
712  |  |  * This is performed at the start of jpeg_start_decompress.  | 
713  |  |  */  | 
714  |  |  | 
715  |  | GLOBAL(void)  | 
716  |  | jinit_master_decompress(j_decompress_ptr cinfo)  | 
717  | 0  | { | 
718  | 0  |   my_master_ptr master = (my_master_ptr)cinfo->master;  | 
719  |  | 
  | 
720  | 0  |   master->pub.prepare_for_output_pass = prepare_for_output_pass;  | 
721  | 0  |   master->pub.finish_output_pass = finish_output_pass;  | 
722  |  | 
  | 
723  | 0  |   master->pub.is_dummy_pass = FALSE;  | 
724  | 0  |   master->pub.jinit_upsampler_no_alloc = FALSE;  | 
725  |  | 
  | 
726  | 0  |   master_selection(cinfo);  | 
727  | 0  | }  |