2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdmaster.c /main/2 1996/05/09 03:48:58 drk $ */
27 * Copyright (C) 1991-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains master control logic for the JPEG decompressor.
32 * These routines are concerned with selecting the modules to be executed
33 * and with determining the number of passes and the work to be done in each
37 #define JPEG_INTERNALS
45 struct jpeg_decomp_master pub; /* public fields */
47 int pass_number; /* # of passes completed */
49 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
51 /* Saved references to initialized quantizer modules,
52 * in case we need to switch modes.
54 struct jpeg_color_quantizer * quantizer_1pass;
55 struct jpeg_color_quantizer * quantizer_2pass;
58 typedef my_decomp_master * my_master_ptr;
62 * Determine whether merged upsample/color conversion should be used.
63 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
67 use_merged_upsample (j_decompress_ptr cinfo)
69 #ifdef UPSAMPLE_MERGING_SUPPORTED
70 /* Merging is the equivalent of plain box-filter upsampling */
71 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
73 /* jdmerge.c only supports YCC=>RGB color conversion */
74 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
75 cinfo->out_color_space != JCS_RGB ||
76 cinfo->out_color_components != RGB_PIXELSIZE)
78 /* and it only handles 2h1v or 2h2v sampling ratios */
79 if (cinfo->comp_info[0].h_samp_factor != 2 ||
80 cinfo->comp_info[1].h_samp_factor != 1 ||
81 cinfo->comp_info[2].h_samp_factor != 1 ||
82 cinfo->comp_info[0].v_samp_factor > 2 ||
83 cinfo->comp_info[1].v_samp_factor != 1 ||
84 cinfo->comp_info[2].v_samp_factor != 1)
86 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
87 if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
88 cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
89 cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
91 /* ??? also need to test for upsample-time rescaling, when & if supported */
92 return TRUE; /* by golly, it'll work... */
100 * Compute output image dimensions and related values.
101 * NOTE: this is exported for possible use by application.
102 * Hence it mustn't do anything that can't be done twice.
103 * Also note that it may be called before the master module is initialized!
107 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
108 /* Do computations that are needed before master selection phase */
111 jpeg_component_info *compptr;
113 /* Prevent application from calling me at wrong times */
114 if (cinfo->global_state != DSTATE_READY)
115 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
117 #ifdef IDCT_SCALING_SUPPORTED
119 /* Compute actual output image dimensions and DCT scaling choices. */
120 if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
121 /* Provide 1/8 scaling */
122 cinfo->output_width = (JDIMENSION)
123 jdiv_round_up((long) cinfo->image_width, 8L);
124 cinfo->output_height = (JDIMENSION)
125 jdiv_round_up((long) cinfo->image_height, 8L);
126 cinfo->min_DCT_scaled_size = 1;
127 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
128 /* Provide 1/4 scaling */
129 cinfo->output_width = (JDIMENSION)
130 jdiv_round_up((long) cinfo->image_width, 4L);
131 cinfo->output_height = (JDIMENSION)
132 jdiv_round_up((long) cinfo->image_height, 4L);
133 cinfo->min_DCT_scaled_size = 2;
134 } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
135 /* Provide 1/2 scaling */
136 cinfo->output_width = (JDIMENSION)
137 jdiv_round_up((long) cinfo->image_width, 2L);
138 cinfo->output_height = (JDIMENSION)
139 jdiv_round_up((long) cinfo->image_height, 2L);
140 cinfo->min_DCT_scaled_size = 4;
142 /* Provide 1/1 scaling */
143 cinfo->output_width = cinfo->image_width;
144 cinfo->output_height = cinfo->image_height;
145 cinfo->min_DCT_scaled_size = DCTSIZE;
147 /* In selecting the actual DCT scaling for each component, we try to
148 * scale up the chroma components via IDCT scaling rather than upsampling.
149 * This saves time if the upsampler gets to use 1:1 scaling.
150 * Note this code assumes that the supported DCT scalings are powers of 2.
152 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
154 int ssize = cinfo->min_DCT_scaled_size;
155 while (ssize < DCTSIZE &&
156 (compptr->h_samp_factor * ssize * 2 <=
157 cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
158 (compptr->v_samp_factor * ssize * 2 <=
159 cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
162 compptr->DCT_scaled_size = ssize;
165 /* Recompute downsampled dimensions of components;
166 * application needs to know these if using raw downsampled data.
168 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
170 /* Size in samples, after IDCT scaling */
171 compptr->downsampled_width = (JDIMENSION)
172 jdiv_round_up((long) cinfo->image_width *
173 (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
174 (long) (cinfo->max_h_samp_factor * DCTSIZE));
175 compptr->downsampled_height = (JDIMENSION)
176 jdiv_round_up((long) cinfo->image_height *
177 (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
178 (long) (cinfo->max_v_samp_factor * DCTSIZE));
181 #else /* !IDCT_SCALING_SUPPORTED */
183 /* Hardwire it to "no scaling" */
184 cinfo->output_width = cinfo->image_width;
185 cinfo->output_height = cinfo->image_height;
186 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
187 * and has computed unscaled downsampled_width and downsampled_height.
190 #endif /* IDCT_SCALING_SUPPORTED */
192 /* Report number of components in selected colorspace. */
193 /* Probably this should be in the color conversion module... */
194 switch (cinfo->out_color_space) {
196 cinfo->out_color_components = 1;
199 #if RGB_PIXELSIZE != 3
200 cinfo->out_color_components = RGB_PIXELSIZE;
202 #endif /* else share code with YCbCr */
204 cinfo->out_color_components = 3;
208 cinfo->out_color_components = 4;
210 default: /* else must be same colorspace as in file */
211 cinfo->out_color_components = cinfo->num_components;
214 cinfo->output_components = (cinfo->quantize_colors ? 1 :
215 cinfo->out_color_components);
217 /* See if upsampler will want to emit more than one row at a time */
218 if (use_merged_upsample(cinfo))
219 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
221 cinfo->rec_outbuf_height = 1;
226 * Several decompression processes need to range-limit values to the range
227 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
228 * due to noise introduced by quantization, roundoff error, etc. These
229 * processes are inner loops and need to be as fast as possible. On most
230 * machines, particularly CPUs with pipelines or instruction prefetch,
231 * a (subscript-check-less) C table lookup
232 * x = sample_range_limit[x];
233 * is faster than explicit tests
235 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
236 * These processes all use a common table prepared by the routine below.
238 * For most steps we can mathematically guarantee that the initial value
239 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
240 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
241 * limiting step (just after the IDCT), a wildly out-of-range value is
242 * possible if the input data is corrupt. To avoid any chance of indexing
243 * off the end of memory and getting a bad-pointer trap, we perform the
244 * post-IDCT limiting thus:
245 * x = range_limit[x & MASK];
246 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
247 * samples. Under normal circumstances this is more than enough range and
248 * a correct output will be generated; with bogus input data the mask will
249 * cause wraparound, and we will safely generate a bogus-but-in-range output.
250 * For the post-IDCT step, we want to convert the data from signed to unsigned
251 * representation by adding CENTERJSAMPLE at the same time that we limit it.
252 * So the post-IDCT limiting table ends up looking like this:
253 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
254 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
255 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
256 * 0,1,...,CENTERJSAMPLE-1
257 * Negative inputs select values from the upper half of the table after
260 * We can save some space by overlapping the start of the post-IDCT table
261 * with the simpler range limiting table. The post-IDCT table begins at
262 * sample_range_limit + CENTERJSAMPLE.
264 * Note that the table is allocated in near data space on PCs; it's small
265 * enough and used often enough to justify this.
269 prepare_range_limit_table (j_decompress_ptr cinfo)
270 /* Allocate and fill in the sample_range_limit table */
276 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
277 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
278 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
279 cinfo->sample_range_limit = table;
280 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
281 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
282 /* Main part of "simple" table: limit[x] = x */
283 for (i = 0; i <= MAXJSAMPLE; i++)
284 table[i] = (JSAMPLE) i;
285 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
286 /* End of simple table, rest of first half of post-IDCT table */
287 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
288 table[i] = MAXJSAMPLE;
289 /* Second half of post-IDCT table */
290 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
291 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
292 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
293 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
298 * Master selection of decompression modules.
299 * This is done once at jpeg_start_decompress time. We determine
300 * which modules will be used and give them appropriate initialization calls.
301 * We also initialize the decompressor input side to begin consuming data.
303 * Since jpeg_read_header has finished, we know what is in the SOF
304 * and (first) SOS markers. We also have all the application parameter
309 master_selection (j_decompress_ptr cinfo)
311 my_master_ptr master = (my_master_ptr) cinfo->master;
312 boolean use_c_buffer;
314 JDIMENSION jd_samplesperrow;
316 /* Initialize dimensions and other stuff */
317 jpeg_calc_output_dimensions(cinfo);
318 prepare_range_limit_table(cinfo);
320 /* Width of an output scanline must be representable as JDIMENSION. */
321 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
322 jd_samplesperrow = (JDIMENSION) samplesperrow;
323 if ((long) jd_samplesperrow != samplesperrow)
324 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
326 /* Initialize my private state */
327 master->pass_number = 0;
328 master->using_merged_upsample = use_merged_upsample(cinfo);
330 /* Color quantizer selection */
331 master->quantizer_1pass = NULL;
332 master->quantizer_2pass = NULL;
333 /* No mode changes if not using buffered-image mode. */
334 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
335 cinfo->enable_1pass_quant = FALSE;
336 cinfo->enable_external_quant = FALSE;
337 cinfo->enable_2pass_quant = FALSE;
339 if (cinfo->quantize_colors) {
340 if (cinfo->raw_data_out)
341 ERREXIT(cinfo, JERR_NOTIMPL);
342 /* 2-pass quantizer only works in 3-component color space. */
343 if (cinfo->out_color_components != 3) {
344 cinfo->enable_1pass_quant = TRUE;
345 cinfo->enable_external_quant = FALSE;
346 cinfo->enable_2pass_quant = FALSE;
347 cinfo->colormap = NULL;
348 } else if (cinfo->colormap != NULL) {
349 cinfo->enable_external_quant = TRUE;
350 } else if (cinfo->two_pass_quantize) {
351 cinfo->enable_2pass_quant = TRUE;
353 cinfo->enable_1pass_quant = TRUE;
356 if (cinfo->enable_1pass_quant) {
357 #ifdef QUANT_1PASS_SUPPORTED
358 jinit_1pass_quantizer(cinfo);
359 master->quantizer_1pass = cinfo->cquantize;
361 ERREXIT(cinfo, JERR_NOT_COMPILED);
365 /* We use the 2-pass code to map to external colormaps. */
366 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
367 #ifdef QUANT_2PASS_SUPPORTED
368 jinit_2pass_quantizer(cinfo);
369 master->quantizer_2pass = cinfo->cquantize;
371 ERREXIT(cinfo, JERR_NOT_COMPILED);
374 /* If both quantizers are initialized, the 2-pass one is left active;
375 * this is necessary for starting with quantization to an external map.
379 /* Post-processing: in particular, color conversion first */
380 if (! cinfo->raw_data_out) {
381 if (master->using_merged_upsample) {
382 #ifdef UPSAMPLE_MERGING_SUPPORTED
383 jinit_merged_upsampler(cinfo); /* does color conversion too */
385 ERREXIT(cinfo, JERR_NOT_COMPILED);
388 jinit_color_deconverter(cinfo);
389 jinit_upsampler(cinfo);
391 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
394 jinit_inverse_dct(cinfo);
395 /* Entropy decoding: either Huffman or arithmetic coding. */
396 if (cinfo->arith_code) {
397 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
399 if (cinfo->progressive_mode) {
400 #ifdef D_PROGRESSIVE_SUPPORTED
401 jinit_phuff_decoder(cinfo);
403 ERREXIT(cinfo, JERR_NOT_COMPILED);
406 jinit_huff_decoder(cinfo);
409 /* Initialize principal buffer controllers. */
410 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
411 jinit_d_coef_controller(cinfo, use_c_buffer);
413 if (! cinfo->raw_data_out)
414 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
416 /* We can now tell the memory manager to allocate virtual arrays. */
417 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
419 /* Initialize input side of decompressor to consume first scan. */
420 (*cinfo->inputctl->start_input_pass) (cinfo);
422 #ifdef D_MULTISCAN_FILES_SUPPORTED
423 /* If jpeg_start_decompress will read the whole file, initialize
424 * progress monitoring appropriately. The input step is counted
427 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
428 cinfo->inputctl->has_multiple_scans) {
430 /* Estimate number of scans to set pass_limit. */
431 if (cinfo->progressive_mode) {
432 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
433 nscans = 2 + 3 * cinfo->num_components;
435 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
436 nscans = cinfo->num_components;
438 cinfo->progress->pass_counter = 0L;
439 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
440 cinfo->progress->completed_passes = 0;
441 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
442 /* Count the input pass as done */
443 master->pass_number++;
445 #endif /* D_MULTISCAN_FILES_SUPPORTED */
451 * This is called at the beginning of each output pass. We determine which
452 * modules will be active during this pass and give them appropriate
453 * start_pass calls. We also set is_dummy_pass to indicate whether this
454 * is a "real" output pass or a dummy pass for color quantization.
455 * (In the latter case, jdapi.c will crank the pass to completion.)
459 prepare_for_output_pass (j_decompress_ptr cinfo)
461 my_master_ptr master = (my_master_ptr) cinfo->master;
463 if (master->pub.is_dummy_pass) {
464 #ifdef QUANT_2PASS_SUPPORTED
465 /* Final pass of 2-pass quantization */
466 master->pub.is_dummy_pass = FALSE;
467 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
468 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
469 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
471 ERREXIT(cinfo, JERR_NOT_COMPILED);
472 #endif /* QUANT_2PASS_SUPPORTED */
474 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
475 /* Select new quantization method */
476 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
477 cinfo->cquantize = master->quantizer_2pass;
478 master->pub.is_dummy_pass = TRUE;
479 } else if (cinfo->enable_1pass_quant) {
480 cinfo->cquantize = master->quantizer_1pass;
482 ERREXIT(cinfo, JERR_MODE_CHANGE);
485 (*cinfo->idct->start_pass) (cinfo);
486 (*cinfo->coef->start_output_pass) (cinfo);
487 if (! cinfo->raw_data_out) {
488 if (! master->using_merged_upsample)
489 (*cinfo->cconvert->start_pass) (cinfo);
490 (*cinfo->upsample->start_pass) (cinfo);
491 if (cinfo->quantize_colors)
492 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
493 (*cinfo->post->start_pass) (cinfo,
494 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
495 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
499 /* Set up progress monitor's pass info if present */
500 if (cinfo->progress != NULL) {
501 cinfo->progress->completed_passes = master->pass_number;
502 cinfo->progress->total_passes = master->pass_number +
503 (master->pub.is_dummy_pass ? 2 : 1);
504 /* In buffered-image mode, we assume one more output pass if EOI not
505 * yet reached, but no more passes if EOI has been reached.
507 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
508 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
515 * Finish up at end of an output pass.
519 finish_output_pass (j_decompress_ptr cinfo)
521 my_master_ptr master = (my_master_ptr) cinfo->master;
523 if (cinfo->quantize_colors)
524 (*cinfo->cquantize->finish_pass) (cinfo);
525 master->pass_number++;
529 #ifdef D_MULTISCAN_FILES_SUPPORTED
532 * Switch to a new external colormap between output passes.
536 jpeg_new_colormap (j_decompress_ptr cinfo)
538 my_master_ptr master = (my_master_ptr) cinfo->master;
540 /* Prevent application from calling me at wrong times */
541 if (cinfo->global_state != DSTATE_BUFIMAGE)
542 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
544 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
545 cinfo->colormap != NULL) {
546 /* Select 2-pass quantizer for external colormap use */
547 cinfo->cquantize = master->quantizer_2pass;
548 /* Notify quantizer of colormap change */
549 (*cinfo->cquantize->new_color_map) (cinfo);
550 master->pub.is_dummy_pass = FALSE; /* just in case */
552 ERREXIT(cinfo, JERR_MODE_CHANGE);
555 #endif /* D_MULTISCAN_FILES_SUPPORTED */
559 * Initialize master decompression control and select active modules.
560 * This is performed at the start of jpeg_start_decompress.
564 jinit_master_decompress (j_decompress_ptr cinfo)
566 my_master_ptr master;
568 master = (my_master_ptr)
569 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
570 SIZEOF(my_decomp_master));
571 cinfo->master = (struct jpeg_decomp_master *) master;
572 master->pub.prepare_for_output_pass = prepare_for_output_pass;
573 master->pub.finish_output_pass = finish_output_pass;
575 master->pub.is_dummy_pass = FALSE;
577 master_selection(cinfo);