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