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, D. R. Commander.
9 * Copyright (C) 2013, Linaro Limited.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains master control logic for the JPEG decompressor.
13 * These routines are concerned with selecting the modules to be executed
14 * and with determining the number of passes and the work to be done in each
18 #define JPEG_INTERNALS
27 struct jpeg_decomp_master pub; /* public fields */
29 int pass_number; /* # of passes completed */
31 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
33 /* Saved references to initialized quantizer modules,
34 * in case we need to switch modes.
36 struct jpeg_color_quantizer * quantizer_1pass;
37 struct jpeg_color_quantizer * quantizer_2pass;
40 typedef my_decomp_master * my_master_ptr;
44 * Determine whether merged upsample/color conversion should be used.
45 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
49 use_merged_upsample (j_decompress_ptr cinfo)
51 #ifdef UPSAMPLE_MERGING_SUPPORTED
52 /* Merging is the equivalent of plain box-filter upsampling */
53 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
55 /* jdmerge.c only supports YCC=>RGB and YCC=>RGB565 color conversion */
56 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
57 (cinfo->out_color_space != JCS_RGB &&
58 cinfo->out_color_space != JCS_RGB565 &&
59 cinfo->out_color_space != JCS_EXT_RGB &&
60 cinfo->out_color_space != JCS_EXT_RGBX &&
61 cinfo->out_color_space != JCS_EXT_BGR &&
62 cinfo->out_color_space != JCS_EXT_BGRX &&
63 cinfo->out_color_space != JCS_EXT_XBGR &&
64 cinfo->out_color_space != JCS_EXT_XRGB &&
65 cinfo->out_color_space != JCS_EXT_RGBA &&
66 cinfo->out_color_space != JCS_EXT_BGRA &&
67 cinfo->out_color_space != JCS_EXT_ABGR &&
68 cinfo->out_color_space != JCS_EXT_ARGB))
70 if ((cinfo->out_color_space == JCS_RGB565 &&
71 cinfo->out_color_components != 3) ||
72 (cinfo->out_color_space != JCS_RGB565 &&
73 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space]))
75 /* and it only handles 2h1v or 2h2v sampling ratios */
76 if (cinfo->comp_info[0].h_samp_factor != 2 ||
77 cinfo->comp_info[1].h_samp_factor != 1 ||
78 cinfo->comp_info[2].h_samp_factor != 1 ||
79 cinfo->comp_info[0].v_samp_factor > 2 ||
80 cinfo->comp_info[1].v_samp_factor != 1 ||
81 cinfo->comp_info[2].v_samp_factor != 1)
83 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
84 if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
85 cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
86 cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
88 /* ??? also need to test for upsample-time rescaling, when & if supported */
89 return TRUE; /* by golly, it'll work... */
97 * Compute output image dimensions and related values.
98 * NOTE: this is exported for possible use by application.
99 * Hence it mustn't do anything that can't be done twice.
102 #if JPEG_LIB_VERSION >= 80
107 jpeg_core_output_dimensions (j_decompress_ptr cinfo)
108 /* Do computations that are needed before master selection phase.
109 * This function is used for transcoding and full decompression.
112 #ifdef IDCT_SCALING_SUPPORTED
114 jpeg_component_info *compptr;
116 /* Compute actual output image dimensions and DCT scaling choices. */
117 if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
118 /* Provide 1/block_size scaling */
119 cinfo->output_width = (JDIMENSION)
120 jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);
121 cinfo->output_height = (JDIMENSION)
122 jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);
123 cinfo->_min_DCT_h_scaled_size = 1;
124 cinfo->_min_DCT_v_scaled_size = 1;
125 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
126 /* Provide 2/block_size scaling */
127 cinfo->output_width = (JDIMENSION)
128 jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);
129 cinfo->output_height = (JDIMENSION)
130 jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);
131 cinfo->_min_DCT_h_scaled_size = 2;
132 cinfo->_min_DCT_v_scaled_size = 2;
133 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
134 /* Provide 3/block_size scaling */
135 cinfo->output_width = (JDIMENSION)
136 jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);
137 cinfo->output_height = (JDIMENSION)
138 jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);
139 cinfo->_min_DCT_h_scaled_size = 3;
140 cinfo->_min_DCT_v_scaled_size = 3;
141 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
142 /* Provide 4/block_size scaling */
143 cinfo->output_width = (JDIMENSION)
144 jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);
145 cinfo->output_height = (JDIMENSION)
146 jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);
147 cinfo->_min_DCT_h_scaled_size = 4;
148 cinfo->_min_DCT_v_scaled_size = 4;
149 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
150 /* Provide 5/block_size scaling */
151 cinfo->output_width = (JDIMENSION)
152 jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);
153 cinfo->output_height = (JDIMENSION)
154 jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);
155 cinfo->_min_DCT_h_scaled_size = 5;
156 cinfo->_min_DCT_v_scaled_size = 5;
157 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
158 /* Provide 6/block_size scaling */
159 cinfo->output_width = (JDIMENSION)
160 jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);
161 cinfo->output_height = (JDIMENSION)
162 jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);
163 cinfo->_min_DCT_h_scaled_size = 6;
164 cinfo->_min_DCT_v_scaled_size = 6;
165 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
166 /* Provide 7/block_size scaling */
167 cinfo->output_width = (JDIMENSION)
168 jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);
169 cinfo->output_height = (JDIMENSION)
170 jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);
171 cinfo->_min_DCT_h_scaled_size = 7;
172 cinfo->_min_DCT_v_scaled_size = 7;
173 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
174 /* Provide 8/block_size scaling */
175 cinfo->output_width = (JDIMENSION)
176 jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);
177 cinfo->output_height = (JDIMENSION)
178 jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);
179 cinfo->_min_DCT_h_scaled_size = 8;
180 cinfo->_min_DCT_v_scaled_size = 8;
181 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
182 /* Provide 9/block_size scaling */
183 cinfo->output_width = (JDIMENSION)
184 jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);
185 cinfo->output_height = (JDIMENSION)
186 jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);
187 cinfo->_min_DCT_h_scaled_size = 9;
188 cinfo->_min_DCT_v_scaled_size = 9;
189 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
190 /* Provide 10/block_size scaling */
191 cinfo->output_width = (JDIMENSION)
192 jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);
193 cinfo->output_height = (JDIMENSION)
194 jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);
195 cinfo->_min_DCT_h_scaled_size = 10;
196 cinfo->_min_DCT_v_scaled_size = 10;
197 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
198 /* Provide 11/block_size scaling */
199 cinfo->output_width = (JDIMENSION)
200 jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);
201 cinfo->output_height = (JDIMENSION)
202 jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);
203 cinfo->_min_DCT_h_scaled_size = 11;
204 cinfo->_min_DCT_v_scaled_size = 11;
205 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
206 /* Provide 12/block_size scaling */
207 cinfo->output_width = (JDIMENSION)
208 jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);
209 cinfo->output_height = (JDIMENSION)
210 jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);
211 cinfo->_min_DCT_h_scaled_size = 12;
212 cinfo->_min_DCT_v_scaled_size = 12;
213 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
214 /* Provide 13/block_size scaling */
215 cinfo->output_width = (JDIMENSION)
216 jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);
217 cinfo->output_height = (JDIMENSION)
218 jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);
219 cinfo->_min_DCT_h_scaled_size = 13;
220 cinfo->_min_DCT_v_scaled_size = 13;
221 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
222 /* Provide 14/block_size scaling */
223 cinfo->output_width = (JDIMENSION)
224 jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);
225 cinfo->output_height = (JDIMENSION)
226 jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);
227 cinfo->_min_DCT_h_scaled_size = 14;
228 cinfo->_min_DCT_v_scaled_size = 14;
229 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
230 /* Provide 15/block_size scaling */
231 cinfo->output_width = (JDIMENSION)
232 jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);
233 cinfo->output_height = (JDIMENSION)
234 jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);
235 cinfo->_min_DCT_h_scaled_size = 15;
236 cinfo->_min_DCT_v_scaled_size = 15;
238 /* Provide 16/block_size scaling */
239 cinfo->output_width = (JDIMENSION)
240 jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);
241 cinfo->output_height = (JDIMENSION)
242 jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);
243 cinfo->_min_DCT_h_scaled_size = 16;
244 cinfo->_min_DCT_v_scaled_size = 16;
247 /* Recompute dimensions of components */
248 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
250 compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
251 compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
254 #else /* !IDCT_SCALING_SUPPORTED */
256 /* Hardwire it to "no scaling" */
257 cinfo->output_width = cinfo->image_width;
258 cinfo->output_height = cinfo->image_height;
259 /* jdinput.c has already initialized DCT_scaled_size,
260 * and has computed unscaled downsampled_width and downsampled_height.
263 #endif /* IDCT_SCALING_SUPPORTED */
268 * Compute output image dimensions and related values.
269 * NOTE: this is exported for possible use by application.
270 * Hence it mustn't do anything that can't be done twice.
271 * Also note that it may be called before the master module is initialized!
275 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
276 /* Do computations that are needed before master selection phase */
278 #ifdef IDCT_SCALING_SUPPORTED
280 jpeg_component_info *compptr;
283 /* Prevent application from calling me at wrong times */
284 if (cinfo->global_state != DSTATE_READY)
285 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
287 /* Compute core output image dimensions and DCT scaling choices. */
288 jpeg_core_output_dimensions(cinfo);
290 #ifdef IDCT_SCALING_SUPPORTED
292 /* In selecting the actual DCT scaling for each component, we try to
293 * scale up the chroma components via IDCT scaling rather than upsampling.
294 * This saves time if the upsampler gets to use 1:1 scaling.
295 * Note this code adapts subsampling ratios which are powers of 2.
297 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
299 int ssize = cinfo->_min_DCT_scaled_size;
300 while (ssize < DCTSIZE &&
301 ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
302 (compptr->h_samp_factor * ssize * 2) == 0) &&
303 ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
304 (compptr->v_samp_factor * ssize * 2) == 0)) {
307 #if JPEG_LIB_VERSION >= 70
308 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
310 compptr->DCT_scaled_size = ssize;
314 /* Recompute downsampled dimensions of components;
315 * application needs to know these if using raw downsampled data.
317 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
319 /* Size in samples, after IDCT scaling */
320 compptr->downsampled_width = (JDIMENSION)
321 jdiv_round_up((long) cinfo->image_width *
322 (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
323 (long) (cinfo->max_h_samp_factor * DCTSIZE));
324 compptr->downsampled_height = (JDIMENSION)
325 jdiv_round_up((long) cinfo->image_height *
326 (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
327 (long) (cinfo->max_v_samp_factor * DCTSIZE));
330 #else /* !IDCT_SCALING_SUPPORTED */
332 /* Hardwire it to "no scaling" */
333 cinfo->output_width = cinfo->image_width;
334 cinfo->output_height = cinfo->image_height;
335 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
336 * and has computed unscaled downsampled_width and downsampled_height.
339 #endif /* IDCT_SCALING_SUPPORTED */
341 /* Report number of components in selected colorspace. */
342 /* Probably this should be in the color conversion module... */
343 switch (cinfo->out_color_space) {
345 cinfo->out_color_components = 1;
358 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
362 cinfo->out_color_components = 3;
366 cinfo->out_color_components = 4;
368 default: /* else must be same colorspace as in file */
369 cinfo->out_color_components = cinfo->num_components;
372 cinfo->output_components = (cinfo->quantize_colors ? 1 :
373 cinfo->out_color_components);
375 /* See if upsampler will want to emit more than one row at a time */
376 if (use_merged_upsample(cinfo))
377 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
379 cinfo->rec_outbuf_height = 1;
384 * Several decompression processes need to range-limit values to the range
385 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
386 * due to noise introduced by quantization, roundoff error, etc. These
387 * processes are inner loops and need to be as fast as possible. On most
388 * machines, particularly CPUs with pipelines or instruction prefetch,
389 * a (subscript-check-less) C table lookup
390 * x = sample_range_limit[x];
391 * is faster than explicit tests
393 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
394 * These processes all use a common table prepared by the routine below.
396 * For most steps we can mathematically guarantee that the initial value
397 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
398 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
399 * limiting step (just after the IDCT), a wildly out-of-range value is
400 * possible if the input data is corrupt. To avoid any chance of indexing
401 * off the end of memory and getting a bad-pointer trap, we perform the
402 * post-IDCT limiting thus:
403 * x = range_limit[x & MASK];
404 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
405 * samples. Under normal circumstances this is more than enough range and
406 * a correct output will be generated; with bogus input data the mask will
407 * cause wraparound, and we will safely generate a bogus-but-in-range output.
408 * For the post-IDCT step, we want to convert the data from signed to unsigned
409 * representation by adding CENTERJSAMPLE at the same time that we limit it.
410 * So the post-IDCT limiting table ends up looking like this:
411 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
412 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
413 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
414 * 0,1,...,CENTERJSAMPLE-1
415 * Negative inputs select values from the upper half of the table after
418 * We can save some space by overlapping the start of the post-IDCT table
419 * with the simpler range limiting table. The post-IDCT table begins at
420 * sample_range_limit + CENTERJSAMPLE.
424 prepare_range_limit_table (j_decompress_ptr cinfo)
425 /* Allocate and fill in the sample_range_limit table */
431 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
432 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * sizeof(JSAMPLE));
433 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
434 cinfo->sample_range_limit = table;
435 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
436 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * sizeof(JSAMPLE));
437 /* Main part of "simple" table: limit[x] = x */
438 for (i = 0; i <= MAXJSAMPLE; i++)
439 table[i] = (JSAMPLE) i;
440 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
441 /* End of simple table, rest of first half of post-IDCT table */
442 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
443 table[i] = MAXJSAMPLE;
444 /* Second half of post-IDCT table */
445 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
446 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * sizeof(JSAMPLE));
447 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
448 cinfo->sample_range_limit, CENTERJSAMPLE * sizeof(JSAMPLE));
453 * Master selection of decompression modules.
454 * This is done once at jpeg_start_decompress time. We determine
455 * which modules will be used and give them appropriate initialization calls.
456 * We also initialize the decompressor input side to begin consuming data.
458 * Since jpeg_read_header has finished, we know what is in the SOF
459 * and (first) SOS markers. We also have all the application parameter
464 master_selection (j_decompress_ptr cinfo)
466 my_master_ptr master = (my_master_ptr) cinfo->master;
467 boolean use_c_buffer;
469 JDIMENSION jd_samplesperrow;
471 /* Initialize dimensions and other stuff */
472 jpeg_calc_output_dimensions(cinfo);
473 prepare_range_limit_table(cinfo);
475 /* Width of an output scanline must be representable as JDIMENSION. */
476 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
477 jd_samplesperrow = (JDIMENSION) samplesperrow;
478 if ((long) jd_samplesperrow != samplesperrow)
479 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
481 /* Initialize my private state */
482 master->pass_number = 0;
483 master->using_merged_upsample = use_merged_upsample(cinfo);
485 /* Color quantizer selection */
486 master->quantizer_1pass = NULL;
487 master->quantizer_2pass = NULL;
488 /* No mode changes if not using buffered-image mode. */
489 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
490 cinfo->enable_1pass_quant = FALSE;
491 cinfo->enable_external_quant = FALSE;
492 cinfo->enable_2pass_quant = FALSE;
494 if (cinfo->quantize_colors) {
495 if (cinfo->raw_data_out)
496 ERREXIT(cinfo, JERR_NOTIMPL);
497 /* 2-pass quantizer only works in 3-component color space. */
498 if (cinfo->out_color_components != 3) {
499 cinfo->enable_1pass_quant = TRUE;
500 cinfo->enable_external_quant = FALSE;
501 cinfo->enable_2pass_quant = FALSE;
502 cinfo->colormap = NULL;
503 } else if (cinfo->colormap != NULL) {
504 cinfo->enable_external_quant = TRUE;
505 } else if (cinfo->two_pass_quantize) {
506 cinfo->enable_2pass_quant = TRUE;
508 cinfo->enable_1pass_quant = TRUE;
511 if (cinfo->enable_1pass_quant) {
512 #ifdef QUANT_1PASS_SUPPORTED
513 jinit_1pass_quantizer(cinfo);
514 master->quantizer_1pass = cinfo->cquantize;
516 ERREXIT(cinfo, JERR_NOT_COMPILED);
520 /* We use the 2-pass code to map to external colormaps. */
521 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
522 #ifdef QUANT_2PASS_SUPPORTED
523 jinit_2pass_quantizer(cinfo);
524 master->quantizer_2pass = cinfo->cquantize;
526 ERREXIT(cinfo, JERR_NOT_COMPILED);
529 /* If both quantizers are initialized, the 2-pass one is left active;
530 * this is necessary for starting with quantization to an external map.
534 /* Post-processing: in particular, color conversion first */
535 if (! cinfo->raw_data_out) {
536 if (master->using_merged_upsample) {
537 #ifdef UPSAMPLE_MERGING_SUPPORTED
538 jinit_merged_upsampler(cinfo); /* does color conversion too */
540 ERREXIT(cinfo, JERR_NOT_COMPILED);
543 jinit_color_deconverter(cinfo);
544 jinit_upsampler(cinfo);
546 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
549 jinit_inverse_dct(cinfo);
550 /* Entropy decoding: either Huffman or arithmetic coding. */
551 if (cinfo->arith_code) {
552 #ifdef D_ARITH_CODING_SUPPORTED
553 jinit_arith_decoder(cinfo);
555 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
558 if (cinfo->progressive_mode) {
559 #ifdef D_PROGRESSIVE_SUPPORTED
560 jinit_phuff_decoder(cinfo);
562 ERREXIT(cinfo, JERR_NOT_COMPILED);
565 jinit_huff_decoder(cinfo);
568 /* Initialize principal buffer controllers. */
569 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
570 jinit_d_coef_controller(cinfo, use_c_buffer);
572 if (! cinfo->raw_data_out)
573 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
575 /* We can now tell the memory manager to allocate virtual arrays. */
576 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
578 /* Initialize input side of decompressor to consume first scan. */
579 (*cinfo->inputctl->start_input_pass) (cinfo);
581 #ifdef D_MULTISCAN_FILES_SUPPORTED
582 /* If jpeg_start_decompress will read the whole file, initialize
583 * progress monitoring appropriately. The input step is counted
586 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
587 cinfo->inputctl->has_multiple_scans) {
589 /* Estimate number of scans to set pass_limit. */
590 if (cinfo->progressive_mode) {
591 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
592 nscans = 2 + 3 * cinfo->num_components;
594 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
595 nscans = cinfo->num_components;
597 cinfo->progress->pass_counter = 0L;
598 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
599 cinfo->progress->completed_passes = 0;
600 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
601 /* Count the input pass as done */
602 master->pass_number++;
604 #endif /* D_MULTISCAN_FILES_SUPPORTED */
610 * This is called at the beginning of each output pass. We determine which
611 * modules will be active during this pass and give them appropriate
612 * start_pass calls. We also set is_dummy_pass to indicate whether this
613 * is a "real" output pass or a dummy pass for color quantization.
614 * (In the latter case, jdapistd.c will crank the pass to completion.)
618 prepare_for_output_pass (j_decompress_ptr cinfo)
620 my_master_ptr master = (my_master_ptr) cinfo->master;
622 if (master->pub.is_dummy_pass) {
623 #ifdef QUANT_2PASS_SUPPORTED
624 /* Final pass of 2-pass quantization */
625 master->pub.is_dummy_pass = FALSE;
626 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
627 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
628 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
630 ERREXIT(cinfo, JERR_NOT_COMPILED);
631 #endif /* QUANT_2PASS_SUPPORTED */
633 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
634 /* Select new quantization method */
635 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
636 cinfo->cquantize = master->quantizer_2pass;
637 master->pub.is_dummy_pass = TRUE;
638 } else if (cinfo->enable_1pass_quant) {
639 cinfo->cquantize = master->quantizer_1pass;
641 ERREXIT(cinfo, JERR_MODE_CHANGE);
644 (*cinfo->idct->start_pass) (cinfo);
645 (*cinfo->coef->start_output_pass) (cinfo);
646 if (! cinfo->raw_data_out) {
647 if (! master->using_merged_upsample)
648 (*cinfo->cconvert->start_pass) (cinfo);
649 (*cinfo->upsample->start_pass) (cinfo);
650 if (cinfo->quantize_colors)
651 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
652 (*cinfo->post->start_pass) (cinfo,
653 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
654 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
658 /* Set up progress monitor's pass info if present */
659 if (cinfo->progress != NULL) {
660 cinfo->progress->completed_passes = master->pass_number;
661 cinfo->progress->total_passes = master->pass_number +
662 (master->pub.is_dummy_pass ? 2 : 1);
663 /* In buffered-image mode, we assume one more output pass if EOI not
664 * yet reached, but no more passes if EOI has been reached.
666 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
667 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
674 * Finish up at end of an output pass.
678 finish_output_pass (j_decompress_ptr cinfo)
680 my_master_ptr master = (my_master_ptr) cinfo->master;
682 if (cinfo->quantize_colors)
683 (*cinfo->cquantize->finish_pass) (cinfo);
684 master->pass_number++;
688 #ifdef D_MULTISCAN_FILES_SUPPORTED
691 * Switch to a new external colormap between output passes.
695 jpeg_new_colormap (j_decompress_ptr cinfo)
697 my_master_ptr master = (my_master_ptr) cinfo->master;
699 /* Prevent application from calling me at wrong times */
700 if (cinfo->global_state != DSTATE_BUFIMAGE)
701 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
703 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
704 cinfo->colormap != NULL) {
705 /* Select 2-pass quantizer for external colormap use */
706 cinfo->cquantize = master->quantizer_2pass;
707 /* Notify quantizer of colormap change */
708 (*cinfo->cquantize->new_color_map) (cinfo);
709 master->pub.is_dummy_pass = FALSE; /* just in case */
711 ERREXIT(cinfo, JERR_MODE_CHANGE);
714 #endif /* D_MULTISCAN_FILES_SUPPORTED */
718 * Initialize master decompression control and select active modules.
719 * This is performed at the start of jpeg_start_decompress.
723 jinit_master_decompress (j_decompress_ptr cinfo)
725 my_master_ptr master;
727 master = (my_master_ptr)
728 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
729 sizeof(my_decomp_master));
730 cinfo->master = (struct jpeg_decomp_master *) master;
731 master->pub.prepare_for_output_pass = prepare_for_output_pass;
732 master->pub.finish_output_pass = finish_output_pass;
734 master->pub.is_dummy_pass = FALSE;
736 master_selection(cinfo);