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, 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)
use_merged_upsample(j_decompress_ptr cinfo)33 use_merged_upsample(j_decompress_ptr cinfo)
34 {
35 #ifdef UPSAMPLE_MERGING_SUPPORTED
36 /* Merging is the equivalent of plain box-filter upsampling */
37 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
38 return FALSE;
39 /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
40 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
41 (cinfo->out_color_space != JCS_RGB &&
42 cinfo->out_color_space != JCS_RGB565 &&
43 cinfo->out_color_space != JCS_EXT_RGB &&
44 cinfo->out_color_space != JCS_EXT_RGBX &&
45 cinfo->out_color_space != JCS_EXT_BGR &&
46 cinfo->out_color_space != JCS_EXT_BGRX &&
47 cinfo->out_color_space != JCS_EXT_XBGR &&
48 cinfo->out_color_space != JCS_EXT_XRGB &&
49 cinfo->out_color_space != JCS_EXT_RGBA &&
50 cinfo->out_color_space != JCS_EXT_BGRA &&
51 cinfo->out_color_space != JCS_EXT_ABGR &&
52 cinfo->out_color_space != JCS_EXT_ARGB))
53 return FALSE;
54 if ((cinfo->out_color_space == JCS_RGB565 &&
55 cinfo->out_color_components != 3) ||
56 (cinfo->out_color_space != JCS_RGB565 &&
57 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
58 return FALSE;
59 /* and it only handles 2h1v or 2h2v sampling ratios */
60 if (cinfo->comp_info[0].h_samp_factor != 2 ||
61 cinfo->comp_info[1].h_samp_factor != 1 ||
62 cinfo->comp_info[2].h_samp_factor != 1 ||
63 cinfo->comp_info[0].v_samp_factor > 2 ||
64 cinfo->comp_info[1].v_samp_factor != 1 ||
65 cinfo->comp_info[2].v_samp_factor != 1)
66 return FALSE;
67 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
68 if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
69 cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
70 cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
71 return FALSE;
72 /* ??? also need to test for upsample-time rescaling, when & if supported */
73 return TRUE; /* by golly, it'll work... */
74 #else
75 return FALSE;
76 #endif
77 }
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
jpeg_core_output_dimensions(j_decompress_ptr cinfo)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 {
96 #ifdef IDCT_SCALING_SUPPORTED
97 int ci;
98 jpeg_component_info *compptr;
99
100 /* Compute actual output image dimensions and DCT scaling choices. */
101 if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
102 /* Provide 1/block_size scaling */
103 cinfo->output_width = (JDIMENSION)
104 jdiv_round_up((long)cinfo->image_width, (long)DCTSIZE);
105 cinfo->output_height = (JDIMENSION)
106 jdiv_round_up((long)cinfo->image_height, (long)DCTSIZE);
107 cinfo->_min_DCT_h_scaled_size = 1;
108 cinfo->_min_DCT_v_scaled_size = 1;
109 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
110 /* Provide 2/block_size scaling */
111 cinfo->output_width = (JDIMENSION)
112 jdiv_round_up((long)cinfo->image_width * 2L, (long)DCTSIZE);
113 cinfo->output_height = (JDIMENSION)
114 jdiv_round_up((long)cinfo->image_height * 2L, (long)DCTSIZE);
115 cinfo->_min_DCT_h_scaled_size = 2;
116 cinfo->_min_DCT_v_scaled_size = 2;
117 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
118 /* Provide 3/block_size scaling */
119 cinfo->output_width = (JDIMENSION)
120 jdiv_round_up((long)cinfo->image_width * 3L, (long)DCTSIZE);
121 cinfo->output_height = (JDIMENSION)
122 jdiv_round_up((long)cinfo->image_height * 3L, (long)DCTSIZE);
123 cinfo->_min_DCT_h_scaled_size = 3;
124 cinfo->_min_DCT_v_scaled_size = 3;
125 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
126 /* Provide 4/block_size scaling */
127 cinfo->output_width = (JDIMENSION)
128 jdiv_round_up((long)cinfo->image_width * 4L, (long)DCTSIZE);
129 cinfo->output_height = (JDIMENSION)
130 jdiv_round_up((long)cinfo->image_height * 4L, (long)DCTSIZE);
131 cinfo->_min_DCT_h_scaled_size = 4;
132 cinfo->_min_DCT_v_scaled_size = 4;
133 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
134 /* Provide 5/block_size scaling */
135 cinfo->output_width = (JDIMENSION)
136 jdiv_round_up((long)cinfo->image_width * 5L, (long)DCTSIZE);
137 cinfo->output_height = (JDIMENSION)
138 jdiv_round_up((long)cinfo->image_height * 5L, (long)DCTSIZE);
139 cinfo->_min_DCT_h_scaled_size = 5;
140 cinfo->_min_DCT_v_scaled_size = 5;
141 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
142 /* Provide 6/block_size scaling */
143 cinfo->output_width = (JDIMENSION)
144 jdiv_round_up((long)cinfo->image_width * 6L, (long)DCTSIZE);
145 cinfo->output_height = (JDIMENSION)
146 jdiv_round_up((long)cinfo->image_height * 6L, (long)DCTSIZE);
147 cinfo->_min_DCT_h_scaled_size = 6;
148 cinfo->_min_DCT_v_scaled_size = 6;
149 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
150 /* Provide 7/block_size scaling */
151 cinfo->output_width = (JDIMENSION)
152 jdiv_round_up((long)cinfo->image_width * 7L, (long)DCTSIZE);
153 cinfo->output_height = (JDIMENSION)
154 jdiv_round_up((long)cinfo->image_height * 7L, (long)DCTSIZE);
155 cinfo->_min_DCT_h_scaled_size = 7;
156 cinfo->_min_DCT_v_scaled_size = 7;
157 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
158 /* Provide 8/block_size scaling */
159 cinfo->output_width = (JDIMENSION)
160 jdiv_round_up((long)cinfo->image_width * 8L, (long)DCTSIZE);
161 cinfo->output_height = (JDIMENSION)
162 jdiv_round_up((long)cinfo->image_height * 8L, (long)DCTSIZE);
163 cinfo->_min_DCT_h_scaled_size = 8;
164 cinfo->_min_DCT_v_scaled_size = 8;
165 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
166 /* Provide 9/block_size scaling */
167 cinfo->output_width = (JDIMENSION)
168 jdiv_round_up((long)cinfo->image_width * 9L, (long)DCTSIZE);
169 cinfo->output_height = (JDIMENSION)
170 jdiv_round_up((long)cinfo->image_height * 9L, (long)DCTSIZE);
171 cinfo->_min_DCT_h_scaled_size = 9;
172 cinfo->_min_DCT_v_scaled_size = 9;
173 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
174 /* Provide 10/block_size scaling */
175 cinfo->output_width = (JDIMENSION)
176 jdiv_round_up((long)cinfo->image_width * 10L, (long)DCTSIZE);
177 cinfo->output_height = (JDIMENSION)
178 jdiv_round_up((long)cinfo->image_height * 10L, (long)DCTSIZE);
179 cinfo->_min_DCT_h_scaled_size = 10;
180 cinfo->_min_DCT_v_scaled_size = 10;
181 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
182 /* Provide 11/block_size scaling */
183 cinfo->output_width = (JDIMENSION)
184 jdiv_round_up((long)cinfo->image_width * 11L, (long)DCTSIZE);
185 cinfo->output_height = (JDIMENSION)
186 jdiv_round_up((long)cinfo->image_height * 11L, (long)DCTSIZE);
187 cinfo->_min_DCT_h_scaled_size = 11;
188 cinfo->_min_DCT_v_scaled_size = 11;
189 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
190 /* Provide 12/block_size scaling */
191 cinfo->output_width = (JDIMENSION)
192 jdiv_round_up((long)cinfo->image_width * 12L, (long)DCTSIZE);
193 cinfo->output_height = (JDIMENSION)
194 jdiv_round_up((long)cinfo->image_height * 12L, (long)DCTSIZE);
195 cinfo->_min_DCT_h_scaled_size = 12;
196 cinfo->_min_DCT_v_scaled_size = 12;
197 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
198 /* Provide 13/block_size scaling */
199 cinfo->output_width = (JDIMENSION)
200 jdiv_round_up((long)cinfo->image_width * 13L, (long)DCTSIZE);
201 cinfo->output_height = (JDIMENSION)
202 jdiv_round_up((long)cinfo->image_height * 13L, (long)DCTSIZE);
203 cinfo->_min_DCT_h_scaled_size = 13;
204 cinfo->_min_DCT_v_scaled_size = 13;
205 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
206 /* Provide 14/block_size scaling */
207 cinfo->output_width = (JDIMENSION)
208 jdiv_round_up((long)cinfo->image_width * 14L, (long)DCTSIZE);
209 cinfo->output_height = (JDIMENSION)
210 jdiv_round_up((long)cinfo->image_height * 14L, (long)DCTSIZE);
211 cinfo->_min_DCT_h_scaled_size = 14;
212 cinfo->_min_DCT_v_scaled_size = 14;
213 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
214 /* Provide 15/block_size scaling */
215 cinfo->output_width = (JDIMENSION)
216 jdiv_round_up((long)cinfo->image_width * 15L, (long)DCTSIZE);
217 cinfo->output_height = (JDIMENSION)
218 jdiv_round_up((long)cinfo->image_height * 15L, (long)DCTSIZE);
219 cinfo->_min_DCT_h_scaled_size = 15;
220 cinfo->_min_DCT_v_scaled_size = 15;
221 } else {
222 /* Provide 16/block_size scaling */
223 cinfo->output_width = (JDIMENSION)
224 jdiv_round_up((long)cinfo->image_width * 16L, (long)DCTSIZE);
225 cinfo->output_height = (JDIMENSION)
226 jdiv_round_up((long)cinfo->image_height * 16L, (long)DCTSIZE);
227 cinfo->_min_DCT_h_scaled_size = 16;
228 cinfo->_min_DCT_v_scaled_size = 16;
229 }
230
231 /* Recompute dimensions of components */
232 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
233 ci++, compptr++) {
234 compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
235 compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
236 }
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 }
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)
jpeg_calc_output_dimensions(j_decompress_ptr cinfo)259 jpeg_calc_output_dimensions(j_decompress_ptr cinfo)
260 /* Do computations that are needed before master selection phase */
261 {
262 #ifdef IDCT_SCALING_SUPPORTED
263 int ci;
264 jpeg_component_info *compptr;
265 #endif
266
267 /* Prevent application from calling me at wrong times */
268 if (cinfo->global_state != DSTATE_READY)
269 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
270
271 /* Compute core output image dimensions and DCT scaling choices. */
272 jpeg_core_output_dimensions(cinfo);
273
274 #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 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
282 ci++, compptr++) {
283 int ssize = cinfo->_min_DCT_scaled_size;
284 while (ssize < DCTSIZE &&
285 ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
286 (compptr->h_samp_factor * ssize * 2) == 0) &&
287 ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
288 (compptr->v_samp_factor * ssize * 2) == 0)) {
289 ssize = ssize * 2;
290 }
291 #if JPEG_LIB_VERSION >= 70
292 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
293 #else
294 compptr->DCT_scaled_size = ssize;
295 #endif
296 }
297
298 /* Recompute downsampled dimensions of components;
299 * application needs to know these if using raw downsampled data.
300 */
301 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
302 ci++, compptr++) {
303 /* Size in samples, after IDCT scaling */
304 compptr->downsampled_width = (JDIMENSION)
305 jdiv_round_up((long)cinfo->image_width *
306 (long)(compptr->h_samp_factor * compptr->_DCT_scaled_size),
307 (long)(cinfo->max_h_samp_factor * DCTSIZE));
308 compptr->downsampled_height = (JDIMENSION)
309 jdiv_round_up((long)cinfo->image_height *
310 (long)(compptr->v_samp_factor * compptr->_DCT_scaled_size),
311 (long)(cinfo->max_v_samp_factor * DCTSIZE));
312 }
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 switch (cinfo->out_color_space) {
328 case JCS_GRAYSCALE:
329 cinfo->out_color_components = 1;
330 break;
331 case JCS_RGB:
332 case JCS_EXT_RGB:
333 case JCS_EXT_RGBX:
334 case JCS_EXT_BGR:
335 case JCS_EXT_BGRX:
336 case JCS_EXT_XBGR:
337 case JCS_EXT_XRGB:
338 case JCS_EXT_RGBA:
339 case JCS_EXT_BGRA:
340 case JCS_EXT_ABGR:
341 case JCS_EXT_ARGB:
342 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
343 break;
344 case JCS_YCbCr:
345 case JCS_RGB565:
346 cinfo->out_color_components = 3;
347 break;
348 case JCS_CMYK:
349 case JCS_YCCK:
350 cinfo->out_color_components = 4;
351 break;
352 default: /* else must be same colorspace as in file */
353 cinfo->out_color_components = cinfo->num_components;
354 break;
355 }
356 cinfo->output_components = (cinfo->quantize_colors ? 1 :
357 cinfo->out_color_components);
358
359 /* See if upsampler will want to emit more than one row at a time */
360 if (use_merged_upsample(cinfo))
361 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
362 else
363 cinfo->rec_outbuf_height = 1;
364 }
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)
prepare_range_limit_table(j_decompress_ptr cinfo)408 prepare_range_limit_table(j_decompress_ptr cinfo)
409 /* Allocate and fill in the sample_range_limit table */
410 {
411 JSAMPLE *table;
412 int i;
413
414 table = (JSAMPLE *)
415 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
416 (5 * (MAXJSAMPLE + 1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
417 table += (MAXJSAMPLE + 1); /* allow negative subscripts of simple table */
418 cinfo->sample_range_limit = table;
419 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
420 MEMZERO(table - (MAXJSAMPLE + 1), (MAXJSAMPLE + 1) * sizeof(JSAMPLE));
421 /* Main part of "simple" table: limit[x] = x */
422 for (i = 0; i <= MAXJSAMPLE; i++)
423 table[i] = (JSAMPLE)i;
424 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
425 /* End of simple table, rest of first half of post-IDCT table */
426 for (i = CENTERJSAMPLE; i < 2 * (MAXJSAMPLE + 1); i++)
427 table[i] = MAXJSAMPLE;
428 /* Second half of post-IDCT table */
429 MEMZERO(table + (2 * (MAXJSAMPLE + 1)),
430 (2 * (MAXJSAMPLE + 1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
431 MEMCOPY(table + (4 * (MAXJSAMPLE + 1) - CENTERJSAMPLE),
432 cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
433 }
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)
master_selection(j_decompress_ptr cinfo)448 master_selection(j_decompress_ptr cinfo)
449 {
450 my_master_ptr master = (my_master_ptr)cinfo->master;
451 boolean use_c_buffer;
452 long samplesperrow;
453 JDIMENSION jd_samplesperrow;
454
455 /* Initialize dimensions and other stuff */
456 jpeg_calc_output_dimensions(cinfo);
457 prepare_range_limit_table(cinfo);
458
459 /* Width of an output scanline must be representable as JDIMENSION. */
460 samplesperrow = (long)cinfo->output_width *
461 (long)cinfo->out_color_components;
462 jd_samplesperrow = (JDIMENSION)samplesperrow;
463 if ((long)jd_samplesperrow != samplesperrow)
464 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
465
466 /* Initialize my private state */
467 master->pass_number = 0;
468 master->using_merged_upsample = use_merged_upsample(cinfo);
469
470 /* Color quantizer selection */
471 master->quantizer_1pass = NULL;
472 master->quantizer_2pass = NULL;
473 /* No mode changes if not using buffered-image mode. */
474 if (!cinfo->quantize_colors || !cinfo->buffered_image) {
475 cinfo->enable_1pass_quant = FALSE;
476 cinfo->enable_external_quant = FALSE;
477 cinfo->enable_2pass_quant = FALSE;
478 }
479 if (cinfo->quantize_colors) {
480 if (cinfo->raw_data_out)
481 ERREXIT(cinfo, JERR_NOTIMPL);
482 /* 2-pass quantizer only works in 3-component color space. */
483 if (cinfo->out_color_components != 3) {
484 cinfo->enable_1pass_quant = TRUE;
485 cinfo->enable_external_quant = FALSE;
486 cinfo->enable_2pass_quant = FALSE;
487 cinfo->colormap = NULL;
488 } else if (cinfo->colormap != NULL) {
489 cinfo->enable_external_quant = TRUE;
490 } else if (cinfo->two_pass_quantize) {
491 cinfo->enable_2pass_quant = TRUE;
492 } else {
493 cinfo->enable_1pass_quant = TRUE;
494 }
495
496 if (cinfo->enable_1pass_quant) {
497 #ifdef QUANT_1PASS_SUPPORTED
498 jinit_1pass_quantizer(cinfo);
499 master->quantizer_1pass = cinfo->cquantize;
500 #else
501 ERREXIT(cinfo, JERR_NOT_COMPILED);
502 #endif
503 }
504
505 /* We use the 2-pass code to map to external colormaps. */
506 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
507 #ifdef QUANT_2PASS_SUPPORTED
508 jinit_2pass_quantizer(cinfo);
509 master->quantizer_2pass = cinfo->cquantize;
510 #else
511 ERREXIT(cinfo, JERR_NOT_COMPILED);
512 #endif
513 }
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 }
518
519 /* Post-processing: in particular, color conversion first */
520 if (!cinfo->raw_data_out) {
521 if (master->using_merged_upsample) {
522 #ifdef UPSAMPLE_MERGING_SUPPORTED
523 jinit_merged_upsampler(cinfo); /* does color conversion too */
524 #else
525 ERREXIT(cinfo, JERR_NOT_COMPILED);
526 #endif
527 } else {
528 jinit_color_deconverter(cinfo);
529 jinit_upsampler(cinfo);
530 }
531 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
532 }
533 /* Inverse DCT */
534 jinit_inverse_dct(cinfo);
535 /* Entropy decoding: either Huffman or arithmetic coding. */
536 if (cinfo->arith_code) {
537 #ifdef D_ARITH_CODING_SUPPORTED
538 jinit_arith_decoder(cinfo);
539 #else
540 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
541 #endif
542 } else {
543 if (cinfo->progressive_mode) {
544 #ifdef D_PROGRESSIVE_SUPPORTED
545 jinit_phuff_decoder(cinfo);
546 #else
547 ERREXIT(cinfo, JERR_NOT_COMPILED);
548 #endif
549 } else
550 jinit_huff_decoder(cinfo);
551 }
552
553 /* Initialize principal buffer controllers. */
554 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
555 jinit_d_coef_controller(cinfo, use_c_buffer);
556
557 if (!cinfo->raw_data_out)
558 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 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
562
563 /* Initialize input side of decompressor to consume first scan. */
564 (*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 cinfo->master->first_iMCU_col = 0;
570 cinfo->master->last_iMCU_col = cinfo->MCUs_per_row - 1;
571 cinfo->master->last_good_iMCU_row = 0;
572
573 #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 if (cinfo->progress != NULL && !cinfo->buffered_image &&
579 cinfo->inputctl->has_multiple_scans) {
580 int nscans;
581 /* Estimate number of scans to set pass_limit. */
582 if (cinfo->progressive_mode) {
583 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
584 nscans = 2 + 3 * cinfo->num_components;
585 } else {
586 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
587 nscans = cinfo->num_components;
588 }
589 cinfo->progress->pass_counter = 0L;
590 cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows * nscans;
591 cinfo->progress->completed_passes = 0;
592 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
593 /* Count the input pass as done */
594 master->pass_number++;
595 }
596 #endif /* D_MULTISCAN_FILES_SUPPORTED */
597 }
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)
prepare_for_output_pass(j_decompress_ptr cinfo)610 prepare_for_output_pass(j_decompress_ptr cinfo)
611 {
612 my_master_ptr master = (my_master_ptr)cinfo->master;
613
614 if (master->pub.is_dummy_pass) {
615 #ifdef QUANT_2PASS_SUPPORTED
616 /* Final pass of 2-pass quantization */
617 master->pub.is_dummy_pass = FALSE;
618 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
619 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
620 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
621 #else
622 ERREXIT(cinfo, JERR_NOT_COMPILED);
623 #endif /* QUANT_2PASS_SUPPORTED */
624 } else {
625 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
626 /* Select new quantization method */
627 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
628 cinfo->cquantize = master->quantizer_2pass;
629 master->pub.is_dummy_pass = TRUE;
630 } else if (cinfo->enable_1pass_quant) {
631 cinfo->cquantize = master->quantizer_1pass;
632 } else {
633 ERREXIT(cinfo, JERR_MODE_CHANGE);
634 }
635 }
636 (*cinfo->idct->start_pass) (cinfo);
637 (*cinfo->coef->start_output_pass) (cinfo);
638 if (!cinfo->raw_data_out) {
639 if (!master->using_merged_upsample)
640 (*cinfo->cconvert->start_pass) (cinfo);
641 (*cinfo->upsample->start_pass) (cinfo);
642 if (cinfo->quantize_colors)
643 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
644 (*cinfo->post->start_pass) (cinfo,
645 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
646 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
647 }
648 }
649
650 /* Set up progress monitor's pass info if present */
651 if (cinfo->progress != NULL) {
652 cinfo->progress->completed_passes = master->pass_number;
653 cinfo->progress->total_passes = master->pass_number +
654 (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 if (cinfo->buffered_image && !cinfo->inputctl->eoi_reached) {
659 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
660 }
661 }
662 }
663
664
665 /*
666 * Finish up at end of an output pass.
667 */
668
669 METHODDEF(void)
finish_output_pass(j_decompress_ptr cinfo)670 finish_output_pass(j_decompress_ptr cinfo)
671 {
672 my_master_ptr master = (my_master_ptr)cinfo->master;
673
674 if (cinfo->quantize_colors)
675 (*cinfo->cquantize->finish_pass) (cinfo);
676 master->pass_number++;
677 }
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)
jpeg_new_colormap(j_decompress_ptr cinfo)687 jpeg_new_colormap(j_decompress_ptr cinfo)
688 {
689 my_master_ptr master = (my_master_ptr)cinfo->master;
690
691 /* Prevent application from calling me at wrong times */
692 if (cinfo->global_state != DSTATE_BUFIMAGE)
693 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
694
695 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
696 cinfo->colormap != NULL) {
697 /* Select 2-pass quantizer for external colormap use */
698 cinfo->cquantize = master->quantizer_2pass;
699 /* Notify quantizer of colormap change */
700 (*cinfo->cquantize->new_color_map) (cinfo);
701 master->pub.is_dummy_pass = FALSE; /* just in case */
702 } else
703 ERREXIT(cinfo, JERR_MODE_CHANGE);
704 }
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)
jinit_master_decompress(j_decompress_ptr cinfo)715 jinit_master_decompress(j_decompress_ptr cinfo)
716 {
717 my_master_ptr master = (my_master_ptr)cinfo->master;
718
719 master->pub.prepare_for_output_pass = prepare_for_output_pass;
720 master->pub.finish_output_pass = finish_output_pass;
721
722 master->pub.is_dummy_pass = FALSE;
723 master->pub.jinit_upsampler_no_alloc = FALSE;
724
725 master_selection(cinfo);
726 }
727