/src/libjpeg-turbo.main/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, 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 | 613k | { |
35 | 613k | #ifdef UPSAMPLE_MERGING_SUPPORTED |
36 | | /* Merging is the equivalent of plain box-filter upsampling */ |
37 | 613k | if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling) |
38 | 460k | return FALSE; |
39 | | /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */ |
40 | 153k | if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 || |
41 | 153k | (cinfo->out_color_space != JCS_RGB && |
42 | 139k | cinfo->out_color_space != JCS_RGB565 && |
43 | 139k | cinfo->out_color_space != JCS_EXT_RGB && |
44 | 139k | cinfo->out_color_space != JCS_EXT_RGBX && |
45 | 139k | cinfo->out_color_space != JCS_EXT_BGR && |
46 | 139k | cinfo->out_color_space != JCS_EXT_BGRX && |
47 | 139k | cinfo->out_color_space != JCS_EXT_XBGR && |
48 | 139k | cinfo->out_color_space != JCS_EXT_XRGB && |
49 | 139k | cinfo->out_color_space != JCS_EXT_RGBA && |
50 | 139k | cinfo->out_color_space != JCS_EXT_BGRA && |
51 | 139k | cinfo->out_color_space != JCS_EXT_ABGR && |
52 | 139k | cinfo->out_color_space != JCS_EXT_ARGB)) |
53 | 14.0k | return FALSE; |
54 | 139k | if ((cinfo->out_color_space == JCS_RGB565 && |
55 | 139k | cinfo->out_color_components != 3) || |
56 | 139k | (cinfo->out_color_space != JCS_RGB565 && |
57 | 139k | 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 | 139k | if (cinfo->comp_info[0].h_samp_factor != 2 || |
61 | 139k | cinfo->comp_info[1].h_samp_factor != 1 || |
62 | 139k | cinfo->comp_info[2].h_samp_factor != 1 || |
63 | 139k | cinfo->comp_info[0].v_samp_factor > 2 || |
64 | 139k | cinfo->comp_info[1].v_samp_factor != 1 || |
65 | 139k | cinfo->comp_info[2].v_samp_factor != 1) |
66 | 103k | return FALSE; |
67 | | /* furthermore, it doesn't work if we've scaled the IDCTs differently */ |
68 | 35.7k | if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size || |
69 | 35.7k | cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size || |
70 | 35.7k | 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 | 35.7k | return TRUE; /* by golly, it'll work... */ |
74 | | #else |
75 | | return FALSE; |
76 | | #endif |
77 | 35.7k | } |
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 | 306k | { |
96 | 306k | #ifdef IDCT_SCALING_SUPPORTED |
97 | 306k | int ci; |
98 | 306k | jpeg_component_info *compptr; |
99 | | |
100 | | /* Compute actual output image dimensions and DCT scaling choices. */ |
101 | 306k | 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 | 306k | } 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 | 306k | } 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 | 306k | } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) { |
126 | | /* Provide 4/block_size scaling */ |
127 | 76.3k | cinfo->output_width = (JDIMENSION) |
128 | 76.3k | jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE); |
129 | 76.3k | cinfo->output_height = (JDIMENSION) |
130 | 76.3k | jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE); |
131 | 76.3k | cinfo->_min_DCT_h_scaled_size = 4; |
132 | 76.3k | cinfo->_min_DCT_v_scaled_size = 4; |
133 | 230k | } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) { |
134 | | /* Provide 5/block_size scaling */ |
135 | 306 | cinfo->output_width = (JDIMENSION) |
136 | 306 | jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE); |
137 | 306 | cinfo->output_height = (JDIMENSION) |
138 | 306 | jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE); |
139 | 306 | cinfo->_min_DCT_h_scaled_size = 5; |
140 | 306 | cinfo->_min_DCT_v_scaled_size = 5; |
141 | 230k | } 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 | 230k | } 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 | 230k | } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) { |
158 | | /* Provide 8/block_size scaling */ |
159 | 230k | cinfo->output_width = (JDIMENSION) |
160 | 230k | jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE); |
161 | 230k | cinfo->output_height = (JDIMENSION) |
162 | 230k | jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE); |
163 | 230k | cinfo->_min_DCT_h_scaled_size = 8; |
164 | 230k | cinfo->_min_DCT_v_scaled_size = 8; |
165 | 230k | } 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 | 1.25M | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
233 | 952k | ci++, compptr++) { |
234 | 952k | compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size; |
235 | 952k | compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size; |
236 | 952k | } |
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 | 306k | } |
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 | 306k | { |
262 | 306k | #ifdef IDCT_SCALING_SUPPORTED |
263 | 306k | int ci; |
264 | 306k | jpeg_component_info *compptr; |
265 | 306k | #endif |
266 | | |
267 | | /* Prevent application from calling me at wrong times */ |
268 | 306k | 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 | 306k | jpeg_core_output_dimensions(cinfo); |
273 | | |
274 | 306k | #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 | 1.25M | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
282 | 952k | ci++, compptr++) { |
283 | 952k | int ssize = cinfo->_min_DCT_scaled_size; |
284 | 1.02M | while (ssize < DCTSIZE && |
285 | 1.02M | ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) % |
286 | 237k | (compptr->h_samp_factor * ssize * 2) == 0) && |
287 | 1.02M | ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) % |
288 | 123k | (compptr->v_samp_factor * ssize * 2) == 0)) { |
289 | 74.2k | ssize = ssize * 2; |
290 | 74.2k | } |
291 | | #if JPEG_LIB_VERSION >= 70 |
292 | | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize; |
293 | | #else |
294 | 952k | compptr->DCT_scaled_size = ssize; |
295 | 952k | #endif |
296 | 952k | } |
297 | | |
298 | | /* Recompute downsampled dimensions of components; |
299 | | * application needs to know these if using raw downsampled data. |
300 | | */ |
301 | 1.25M | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
302 | 952k | ci++, compptr++) { |
303 | | /* Size in samples, after IDCT scaling */ |
304 | 952k | compptr->downsampled_width = (JDIMENSION) |
305 | 952k | jdiv_round_up((long)cinfo->image_width * |
306 | 952k | (long)(compptr->h_samp_factor * compptr->_DCT_scaled_size), |
307 | 952k | (long)(cinfo->max_h_samp_factor * DCTSIZE)); |
308 | 952k | compptr->downsampled_height = (JDIMENSION) |
309 | 952k | jdiv_round_up((long)cinfo->image_height * |
310 | 952k | (long)(compptr->v_samp_factor * compptr->_DCT_scaled_size), |
311 | 952k | (long)(cinfo->max_v_samp_factor * DCTSIZE)); |
312 | 952k | } |
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 | 306k | switch (cinfo->out_color_space) { |
328 | 76.7k | case JCS_GRAYSCALE: |
329 | 76.7k | cinfo->out_color_components = 1; |
330 | 76.7k | break; |
331 | 0 | case JCS_RGB: |
332 | 76.7k | case JCS_EXT_RGB: |
333 | 76.7k | case JCS_EXT_RGBX: |
334 | 76.7k | case JCS_EXT_BGR: |
335 | 153k | case JCS_EXT_BGRX: |
336 | 153k | case JCS_EXT_XBGR: |
337 | 153k | case JCS_EXT_XRGB: |
338 | 153k | case JCS_EXT_RGBA: |
339 | 153k | case JCS_EXT_BGRA: |
340 | 153k | case JCS_EXT_ABGR: |
341 | 153k | case JCS_EXT_ARGB: |
342 | 153k | cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space]; |
343 | 153k | break; |
344 | 0 | case JCS_YCbCr: |
345 | 0 | case JCS_RGB565: |
346 | 0 | cinfo->out_color_components = 3; |
347 | 0 | break; |
348 | 76.7k | case JCS_CMYK: |
349 | 76.7k | case JCS_YCCK: |
350 | 76.7k | cinfo->out_color_components = 4; |
351 | 76.7k | 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 | 306k | } |
356 | 306k | cinfo->output_components = (cinfo->quantize_colors ? 1 : |
357 | 306k | cinfo->out_color_components); |
358 | | |
359 | | /* See if upsampler will want to emit more than one row at a time */ |
360 | 306k | if (use_merged_upsample(cinfo)) |
361 | 17.8k | cinfo->rec_outbuf_height = cinfo->max_v_samp_factor; |
362 | 289k | else |
363 | 289k | cinfo->rec_outbuf_height = 1; |
364 | 306k | } |
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 | 306k | { |
411 | 306k | JSAMPLE *table; |
412 | 306k | int i; |
413 | | |
414 | 306k | table = (JSAMPLE *) |
415 | 306k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
416 | 306k | (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE)); |
417 | 306k | table += (MAXJSAMPLE + 1); /* allow negative subscripts of simple table */ |
418 | 306k | cinfo->sample_range_limit = table; |
419 | | /* First segment of "simple" table: limit[x] = 0 for x < 0 */ |
420 | 306k | memset(table - (MAXJSAMPLE + 1), 0, (MAXJSAMPLE + 1) * sizeof(JSAMPLE)); |
421 | | /* Main part of "simple" table: limit[x] = x */ |
422 | 78.8M | for (i = 0; i <= MAXJSAMPLE; i++) |
423 | 78.5M | table[i] = (JSAMPLE)i; |
424 | 306k | table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */ |
425 | | /* End of simple table, rest of first half of post-IDCT table */ |
426 | 118M | for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++) |
427 | 117M | table[i] = MAXJSAMPLE; |
428 | | /* Second half of post-IDCT table */ |
429 | 306k | memset(table + (2 * (MAXJSAMPLE + 1)), 0, |
430 | 306k | (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE)); |
431 | 306k | memcpy(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE), |
432 | 306k | cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE)); |
433 | 306k | } |
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 | 306k | { |
450 | 306k | my_master_ptr master = (my_master_ptr)cinfo->master; |
451 | 306k | boolean use_c_buffer; |
452 | 306k | long samplesperrow; |
453 | 306k | JDIMENSION jd_samplesperrow; |
454 | | |
455 | | /* Initialize dimensions and other stuff */ |
456 | 306k | jpeg_calc_output_dimensions(cinfo); |
457 | 306k | prepare_range_limit_table(cinfo); |
458 | | |
459 | | /* Width of an output scanline must be representable as JDIMENSION. */ |
460 | 306k | samplesperrow = (long)cinfo->output_width * |
461 | 306k | (long)cinfo->out_color_components; |
462 | 306k | jd_samplesperrow = (JDIMENSION)samplesperrow; |
463 | 306k | if ((long)jd_samplesperrow != samplesperrow) |
464 | 0 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
465 | | |
466 | | /* Initialize my private state */ |
467 | 306k | master->pass_number = 0; |
468 | 306k | master->using_merged_upsample = use_merged_upsample(cinfo); |
469 | | |
470 | | /* Color quantizer selection */ |
471 | 306k | master->quantizer_1pass = NULL; |
472 | 306k | master->quantizer_2pass = NULL; |
473 | | /* No mode changes if not using buffered-image mode. */ |
474 | 306k | if (!cinfo->quantize_colors || !cinfo->buffered_image) { |
475 | 306k | cinfo->enable_1pass_quant = FALSE; |
476 | 306k | cinfo->enable_external_quant = FALSE; |
477 | 306k | cinfo->enable_2pass_quant = FALSE; |
478 | 306k | } |
479 | 306k | 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->enable_1pass_quant = TRUE; |
485 | 0 | cinfo->enable_external_quant = FALSE; |
486 | 0 | cinfo->enable_2pass_quant = FALSE; |
487 | 0 | cinfo->colormap = NULL; |
488 | 0 | } else if (cinfo->colormap != NULL) { |
489 | 0 | cinfo->enable_external_quant = TRUE; |
490 | 0 | } else if (cinfo->two_pass_quantize) { |
491 | 0 | cinfo->enable_2pass_quant = TRUE; |
492 | 0 | } else { |
493 | 0 | cinfo->enable_1pass_quant = TRUE; |
494 | 0 | } |
495 | |
|
496 | 0 | if (cinfo->enable_1pass_quant) { |
497 | 0 | #ifdef QUANT_1PASS_SUPPORTED |
498 | 0 | jinit_1pass_quantizer(cinfo); |
499 | 0 | master->quantizer_1pass = cinfo->cquantize; |
500 | | #else |
501 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
502 | | #endif |
503 | 0 | } |
504 | | |
505 | | /* We use the 2-pass code to map to external colormaps. */ |
506 | 0 | if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) { |
507 | 0 | #ifdef QUANT_2PASS_SUPPORTED |
508 | 0 | jinit_2pass_quantizer(cinfo); |
509 | 0 | master->quantizer_2pass = cinfo->cquantize; |
510 | | #else |
511 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
512 | | #endif |
513 | 0 | } |
514 | | /* If both quantizers are initialized, the 2-pass one is left active; |
515 | | * this is necessary for starting with quantization to an external map. |
516 | | */ |
517 | 0 | } |
518 | | |
519 | | /* Post-processing: in particular, color conversion first */ |
520 | 306k | if (!cinfo->raw_data_out) { |
521 | 306k | if (master->using_merged_upsample) { |
522 | 17.8k | #ifdef UPSAMPLE_MERGING_SUPPORTED |
523 | 17.8k | jinit_merged_upsampler(cinfo); /* does color conversion too */ |
524 | | #else |
525 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
526 | | #endif |
527 | 289k | } else { |
528 | 289k | jinit_color_deconverter(cinfo); |
529 | 289k | jinit_upsampler(cinfo); |
530 | 289k | } |
531 | 306k | jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant); |
532 | 306k | } |
533 | | /* Inverse DCT */ |
534 | 306k | jinit_inverse_dct(cinfo); |
535 | | /* Entropy decoding: either Huffman or arithmetic coding. */ |
536 | 306k | if (cinfo->arith_code) { |
537 | 73.0k | #ifdef D_ARITH_CODING_SUPPORTED |
538 | 73.0k | jinit_arith_decoder(cinfo); |
539 | | #else |
540 | | ERREXIT(cinfo, JERR_ARITH_NOTIMPL); |
541 | | #endif |
542 | 233k | } else { |
543 | 233k | if (cinfo->progressive_mode) { |
544 | 63.0k | #ifdef D_PROGRESSIVE_SUPPORTED |
545 | 63.0k | jinit_phuff_decoder(cinfo); |
546 | | #else |
547 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
548 | | #endif |
549 | 63.0k | } else |
550 | 170k | jinit_huff_decoder(cinfo); |
551 | 233k | } |
552 | | |
553 | | /* Initialize principal buffer controllers. */ |
554 | 306k | use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image; |
555 | 306k | jinit_d_coef_controller(cinfo, use_c_buffer); |
556 | | |
557 | 306k | if (!cinfo->raw_data_out) |
558 | 221k | jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */); |
559 | | |
560 | | /* We can now tell the memory manager to allocate virtual arrays. */ |
561 | 306k | (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo); |
562 | | |
563 | | /* Initialize input side of decompressor to consume first scan. */ |
564 | 306k | (*cinfo->inputctl->start_input_pass) (cinfo); |
565 | | |
566 | | /* Set the first and last iMCU columns to decompress from single-scan images. |
567 | | * By default, decompress all of the iMCU columns. |
568 | | */ |
569 | 306k | cinfo->master->first_iMCU_col = 0; |
570 | 306k | cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1; |
571 | 306k | cinfo->master->last_good_iMCU_row = 0; |
572 | | |
573 | 306k | #ifdef D_MULTISCAN_FILES_SUPPORTED |
574 | | /* If jpeg_start_decompress will read the whole file, initialize |
575 | | * progress monitoring appropriately. The input step is counted |
576 | | * as one pass. |
577 | | */ |
578 | 306k | if (cinfo->progress != NULL && !cinfo->buffered_image && |
579 | 306k | cinfo->inputctl->has_multiple_scans) { |
580 | 157k | int nscans; |
581 | | /* Estimate number of scans to set pass_limit. */ |
582 | 157k | if (cinfo->progressive_mode) { |
583 | | /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ |
584 | 105k | nscans = 2 + 3 * cinfo->num_components; |
585 | 105k | } else { |
586 | | /* For a nonprogressive multiscan file, estimate 1 scan per component. */ |
587 | 52.0k | nscans = cinfo->num_components; |
588 | 52.0k | } |
589 | 157k | cinfo->progress->pass_counter = 0L; |
590 | 157k | cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans; |
591 | 157k | cinfo->progress->completed_passes = 0; |
592 | 157k | cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2); |
593 | | /* Count the input pass as done */ |
594 | 157k | master->pass_number++; |
595 | 157k | } |
596 | 306k | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
597 | 306k | } |
598 | | |
599 | | |
600 | | /* |
601 | | * Per-pass setup. |
602 | | * This is called at the beginning of each output pass. We determine which |
603 | | * modules will be active during this pass and give them appropriate |
604 | | * start_pass calls. We also set is_dummy_pass to indicate whether this |
605 | | * is a "real" output pass or a dummy pass for color quantization. |
606 | | * (In the latter case, jdapistd.c will crank the pass to completion.) |
607 | | */ |
608 | | |
609 | | METHODDEF(void) |
610 | | prepare_for_output_pass(j_decompress_ptr cinfo) |
611 | 120k | { |
612 | 120k | my_master_ptr master = (my_master_ptr)cinfo->master; |
613 | | |
614 | 120k | if (master->pub.is_dummy_pass) { |
615 | 0 | #ifdef QUANT_2PASS_SUPPORTED |
616 | | /* Final pass of 2-pass quantization */ |
617 | 0 | master->pub.is_dummy_pass = FALSE; |
618 | 0 | (*cinfo->cquantize->start_pass) (cinfo, FALSE); |
619 | 0 | (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST); |
620 | 0 | (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST); |
621 | | #else |
622 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
623 | | #endif /* QUANT_2PASS_SUPPORTED */ |
624 | 120k | } else { |
625 | 120k | if (cinfo->quantize_colors && cinfo->colormap == NULL) { |
626 | | /* Select new quantization method */ |
627 | 0 | if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) { |
628 | 0 | cinfo->cquantize = master->quantizer_2pass; |
629 | 0 | master->pub.is_dummy_pass = TRUE; |
630 | 0 | } else if (cinfo->enable_1pass_quant) { |
631 | 0 | cinfo->cquantize = master->quantizer_1pass; |
632 | 0 | } else { |
633 | 0 | ERREXIT(cinfo, JERR_MODE_CHANGE); |
634 | 0 | } |
635 | 0 | } |
636 | 120k | (*cinfo->idct->start_pass) (cinfo); |
637 | 120k | (*cinfo->coef->start_output_pass) (cinfo); |
638 | 120k | if (!cinfo->raw_data_out) { |
639 | 120k | if (!master->using_merged_upsample) |
640 | 107k | (*cinfo->cconvert->start_pass) (cinfo); |
641 | 120k | (*cinfo->upsample->start_pass) (cinfo); |
642 | 120k | if (cinfo->quantize_colors) |
643 | 0 | (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass); |
644 | 120k | (*cinfo->post->start_pass) (cinfo, |
645 | 120k | (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
646 | 120k | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
647 | 120k | } |
648 | 120k | } |
649 | | |
650 | | /* Set up progress monitor's pass info if present */ |
651 | 120k | if (cinfo->progress != NULL) { |
652 | 120k | cinfo->progress->completed_passes = master->pass_number; |
653 | 120k | cinfo->progress->total_passes = master->pass_number + |
654 | 120k | (master->pub.is_dummy_pass ? 2 : 1); |
655 | | /* In buffered-image mode, we assume one more output pass if EOI not |
656 | | * yet reached, but no more passes if EOI has been reached. |
657 | | */ |
658 | 120k | if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) { |
659 | 0 | cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1); |
660 | 0 | } |
661 | 120k | } |
662 | 120k | } |
663 | | |
664 | | |
665 | | /* |
666 | | * Finish up at end of an output pass. |
667 | | */ |
668 | | |
669 | | METHODDEF(void) |
670 | | finish_output_pass(j_decompress_ptr cinfo) |
671 | 120k | { |
672 | 120k | my_master_ptr master = (my_master_ptr)cinfo->master; |
673 | | |
674 | 120k | if (cinfo->quantize_colors) |
675 | 0 | (*cinfo->cquantize->finish_pass) (cinfo); |
676 | 120k | master->pass_number++; |
677 | 120k | } |
678 | | |
679 | | |
680 | | #ifdef D_MULTISCAN_FILES_SUPPORTED |
681 | | |
682 | | /* |
683 | | * Switch to a new external colormap between output passes. |
684 | | */ |
685 | | |
686 | | GLOBAL(void) |
687 | | jpeg_new_colormap(j_decompress_ptr cinfo) |
688 | 0 | { |
689 | 0 | my_master_ptr master = (my_master_ptr)cinfo->master; |
690 | | |
691 | | /* Prevent application from calling me at wrong times */ |
692 | 0 | if (cinfo->global_state != DSTATE_BUFIMAGE) |
693 | 0 | ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); |
694 | |
|
695 | 0 | if (cinfo->quantize_colors && cinfo->enable_external_quant && |
696 | 0 | cinfo->colormap != NULL) { |
697 | | /* Select 2-pass quantizer for external colormap use */ |
698 | 0 | cinfo->cquantize = master->quantizer_2pass; |
699 | | /* Notify quantizer of colormap change */ |
700 | 0 | (*cinfo->cquantize->new_color_map) (cinfo); |
701 | 0 | master->pub.is_dummy_pass = FALSE; /* just in case */ |
702 | 0 | } else |
703 | 0 | ERREXIT(cinfo, JERR_MODE_CHANGE); |
704 | 0 | } |
705 | | |
706 | | #endif /* D_MULTISCAN_FILES_SUPPORTED */ |
707 | | |
708 | | |
709 | | /* |
710 | | * Initialize master decompression control and select active modules. |
711 | | * This is performed at the start of jpeg_start_decompress. |
712 | | */ |
713 | | |
714 | | GLOBAL(void) |
715 | | jinit_master_decompress(j_decompress_ptr cinfo) |
716 | 306k | { |
717 | 306k | my_master_ptr master = (my_master_ptr)cinfo->master; |
718 | | |
719 | 306k | master->pub.prepare_for_output_pass = prepare_for_output_pass; |
720 | 306k | master->pub.finish_output_pass = finish_output_pass; |
721 | | |
722 | 306k | master->pub.is_dummy_pass = FALSE; |
723 | 306k | master->pub.jinit_upsampler_no_alloc = FALSE; |
724 | | |
725 | 306k | master_selection(cinfo); |
726 | 306k | } |