blob: 8314b67d2660e17c9a2c3d2ea94d20945dc283d5 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdmaster.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
DRC8ece7fe2009-08-06 08:32:00 +00005 * Copyright (C) 2009, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * This file contains master control logic for the JPEG decompressor.
10 * These routines are concerned with selecting the modules to be executed
11 * and with determining the number of passes and the work to be done in each
12 * pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000013 */
14
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000016#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017#include "jpeglib.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000018
19
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000020/* Private state */
21
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022typedef struct {
23 struct jpeg_decomp_master pub; /* public fields */
24
Thomas G. Lanebc79e061995-08-02 00:00:00 +000025 int pass_number; /* # of passes completed */
26
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
28
Thomas G. Lanebc79e061995-08-02 00:00:00 +000029 /* Saved references to initialized quantizer modules,
30 * in case we need to switch modes.
31 */
32 struct jpeg_color_quantizer * quantizer_1pass;
33 struct jpeg_color_quantizer * quantizer_2pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034} my_decomp_master;
35
36typedef my_decomp_master * my_master_ptr;
37
38
39/*
40 * Determine whether merged upsample/color conversion should be used.
41 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
42 */
43
Thomas G. Lane489583f1996-02-07 00:00:00 +000044LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000045use_merged_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000046{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047#ifdef UPSAMPLE_MERGING_SUPPORTED
48 /* Merging is the equivalent of plain box-filter upsampling */
49 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
50 return FALSE;
51 /* jdmerge.c only supports YCC=>RGB color conversion */
52 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
DRC720e1612009-04-05 21:51:25 +000053 (cinfo->out_color_space != JCS_RGB &&
54 cinfo->out_color_space != JCS_EXT_RGB &&
55 cinfo->out_color_space != JCS_EXT_RGBX &&
56 cinfo->out_color_space != JCS_EXT_BGR &&
57 cinfo->out_color_space != JCS_EXT_BGRX &&
58 cinfo->out_color_space != JCS_EXT_XBGR &&
59 cinfo->out_color_space != JCS_EXT_XRGB) ||
60 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space])
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061 return FALSE;
62 /* and it only handles 2h1v or 2h2v sampling ratios */
63 if (cinfo->comp_info[0].h_samp_factor != 2 ||
64 cinfo->comp_info[1].h_samp_factor != 1 ||
65 cinfo->comp_info[2].h_samp_factor != 1 ||
66 cinfo->comp_info[0].v_samp_factor > 2 ||
67 cinfo->comp_info[1].v_samp_factor != 1 ||
68 cinfo->comp_info[2].v_samp_factor != 1)
69 return FALSE;
70 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
71 if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
72 cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
73 cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
74 return FALSE;
75 /* ??? also need to test for upsample-time rescaling, when & if supported */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000076 return TRUE; /* by golly, it'll work... */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077#else
78 return FALSE;
79#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000080}
81
82
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000084 * Compute output image dimensions and related values.
85 * NOTE: this is exported for possible use by application.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000086 * Hence it mustn't do anything that can't be done twice.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000087 * Also note that it may be called before the master module is initialized!
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000088 */
89
Thomas G. Lane489583f1996-02-07 00:00:00 +000090GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
92/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000093{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000094#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000096 jpeg_component_info *compptr;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000097#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000098
Thomas G. Lanebc79e061995-08-02 00:00:00 +000099 /* Prevent application from calling me at wrong times */
100 if (cinfo->global_state != DSTATE_READY)
101 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
102
103#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000104
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105 /* Compute actual output image dimensions and DCT scaling choices. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000106 if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
107 /* Provide 1/8 scaling */
108 cinfo->output_width = (JDIMENSION)
109 jdiv_round_up((long) cinfo->image_width, 8L);
110 cinfo->output_height = (JDIMENSION)
111 jdiv_round_up((long) cinfo->image_height, 8L);
112 cinfo->min_DCT_scaled_size = 1;
113 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
114 /* Provide 1/4 scaling */
115 cinfo->output_width = (JDIMENSION)
116 jdiv_round_up((long) cinfo->image_width, 4L);
117 cinfo->output_height = (JDIMENSION)
118 jdiv_round_up((long) cinfo->image_height, 4L);
119 cinfo->min_DCT_scaled_size = 2;
120 } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
121 /* Provide 1/2 scaling */
122 cinfo->output_width = (JDIMENSION)
123 jdiv_round_up((long) cinfo->image_width, 2L);
124 cinfo->output_height = (JDIMENSION)
125 jdiv_round_up((long) cinfo->image_height, 2L);
126 cinfo->min_DCT_scaled_size = 4;
127 } else {
128 /* Provide 1/1 scaling */
129 cinfo->output_width = cinfo->image_width;
130 cinfo->output_height = cinfo->image_height;
131 cinfo->min_DCT_scaled_size = DCTSIZE;
132 }
133 /* In selecting the actual DCT scaling for each component, we try to
134 * scale up the chroma components via IDCT scaling rather than upsampling.
135 * This saves time if the upsampler gets to use 1:1 scaling.
136 * Note this code assumes that the supported DCT scalings are powers of 2.
137 */
138 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
139 ci++, compptr++) {
140 int ssize = cinfo->min_DCT_scaled_size;
141 while (ssize < DCTSIZE &&
142 (compptr->h_samp_factor * ssize * 2 <=
143 cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
144 (compptr->v_samp_factor * ssize * 2 <=
145 cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
146 ssize = ssize * 2;
147 }
148 compptr->DCT_scaled_size = ssize;
149 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000150
151 /* Recompute downsampled dimensions of components;
152 * application needs to know these if using raw downsampled data.
153 */
154 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
155 ci++, compptr++) {
156 /* Size in samples, after IDCT scaling */
157 compptr->downsampled_width = (JDIMENSION)
158 jdiv_round_up((long) cinfo->image_width *
159 (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
160 (long) (cinfo->max_h_samp_factor * DCTSIZE));
161 compptr->downsampled_height = (JDIMENSION)
162 jdiv_round_up((long) cinfo->image_height *
163 (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
164 (long) (cinfo->max_v_samp_factor * DCTSIZE));
165 }
166
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000167#else /* !IDCT_SCALING_SUPPORTED */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000168
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169 /* Hardwire it to "no scaling" */
170 cinfo->output_width = cinfo->image_width;
171 cinfo->output_height = cinfo->image_height;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000172 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
173 * and has computed unscaled downsampled_width and downsampled_height.
174 */
175
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176#endif /* IDCT_SCALING_SUPPORTED */
177
178 /* Report number of components in selected colorspace. */
179 /* Probably this should be in the color conversion module... */
180 switch (cinfo->out_color_space) {
181 case JCS_GRAYSCALE:
182 cinfo->out_color_components = 1;
183 break;
184 case JCS_RGB:
DRC720e1612009-04-05 21:51:25 +0000185 case JCS_EXT_RGB:
186 case JCS_EXT_RGBX:
187 case JCS_EXT_BGR:
188 case JCS_EXT_BGRX:
189 case JCS_EXT_XBGR:
190 case JCS_EXT_XRGB:
191 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193 case JCS_YCbCr:
194 cinfo->out_color_components = 3;
195 break;
196 case JCS_CMYK:
197 case JCS_YCCK:
198 cinfo->out_color_components = 4;
199 break;
200 default: /* else must be same colorspace as in file */
201 cinfo->out_color_components = cinfo->num_components;
202 break;
203 }
204 cinfo->output_components = (cinfo->quantize_colors ? 1 :
205 cinfo->out_color_components);
206
207 /* See if upsampler will want to emit more than one row at a time */
208 if (use_merged_upsample(cinfo))
209 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
210 else
211 cinfo->rec_outbuf_height = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000212}
213
214
215/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216 * Several decompression processes need to range-limit values to the range
217 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
218 * due to noise introduced by quantization, roundoff error, etc. These
219 * processes are inner loops and need to be as fast as possible. On most
220 * machines, particularly CPUs with pipelines or instruction prefetch,
221 * a (subscript-check-less) C table lookup
222 * x = sample_range_limit[x];
223 * is faster than explicit tests
224 * if (x < 0) x = 0;
225 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
226 * These processes all use a common table prepared by the routine below.
227 *
228 * For most steps we can mathematically guarantee that the initial value
229 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
230 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
231 * limiting step (just after the IDCT), a wildly out-of-range value is
232 * possible if the input data is corrupt. To avoid any chance of indexing
233 * off the end of memory and getting a bad-pointer trap, we perform the
234 * post-IDCT limiting thus:
235 * x = range_limit[x & MASK];
236 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
237 * samples. Under normal circumstances this is more than enough range and
238 * a correct output will be generated; with bogus input data the mask will
239 * cause wraparound, and we will safely generate a bogus-but-in-range output.
240 * For the post-IDCT step, we want to convert the data from signed to unsigned
241 * representation by adding CENTERJSAMPLE at the same time that we limit it.
242 * So the post-IDCT limiting table ends up looking like this:
243 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
244 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
245 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
246 * 0,1,...,CENTERJSAMPLE-1
247 * Negative inputs select values from the upper half of the table after
248 * masking.
249 *
250 * We can save some space by overlapping the start of the post-IDCT table
251 * with the simpler range limiting table. The post-IDCT table begins at
252 * sample_range_limit + CENTERJSAMPLE.
253 *
254 * Note that the table is allocated in near data space on PCs; it's small
255 * enough and used often enough to justify this.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000256 */
257
Thomas G. Lane489583f1996-02-07 00:00:00 +0000258LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000259prepare_range_limit_table (j_decompress_ptr cinfo)
260/* Allocate and fill in the sample_range_limit table */
261{
262 JSAMPLE * table;
263 int i;
264
265 table = (JSAMPLE *)
266 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
267 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
268 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
269 cinfo->sample_range_limit = table;
270 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
271 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
272 /* Main part of "simple" table: limit[x] = x */
273 for (i = 0; i <= MAXJSAMPLE; i++)
274 table[i] = (JSAMPLE) i;
275 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
276 /* End of simple table, rest of first half of post-IDCT table */
277 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
278 table[i] = MAXJSAMPLE;
279 /* Second half of post-IDCT table */
280 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
281 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
282 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
283 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
284}
285
286
287/*
288 * Master selection of decompression modules.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000289 * This is done once at jpeg_start_decompress time. We determine
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 * which modules will be used and give them appropriate initialization calls.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000291 * We also initialize the decompressor input side to begin consuming data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000292 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293 * Since jpeg_read_header has finished, we know what is in the SOF
294 * and (first) SOS markers. We also have all the application parameter
295 * settings.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296 */
297
Thomas G. Lane489583f1996-02-07 00:00:00 +0000298LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299master_selection (j_decompress_ptr cinfo)
300{
301 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000302 boolean use_c_buffer;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 long samplesperrow;
304 JDIMENSION jd_samplesperrow;
305
306 /* Initialize dimensions and other stuff */
307 jpeg_calc_output_dimensions(cinfo);
308 prepare_range_limit_table(cinfo);
309
310 /* Width of an output scanline must be representable as JDIMENSION. */
311 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
312 jd_samplesperrow = (JDIMENSION) samplesperrow;
313 if ((long) jd_samplesperrow != samplesperrow)
314 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
315
316 /* Initialize my private state */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 master->pass_number = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318 master->using_merged_upsample = use_merged_upsample(cinfo);
319
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320 /* Color quantizer selection */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000321 master->quantizer_1pass = NULL;
322 master->quantizer_2pass = NULL;
323 /* No mode changes if not using buffered-image mode. */
324 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
325 cinfo->enable_1pass_quant = FALSE;
326 cinfo->enable_external_quant = FALSE;
327 cinfo->enable_2pass_quant = FALSE;
328 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000329 if (cinfo->quantize_colors) {
330 if (cinfo->raw_data_out)
331 ERREXIT(cinfo, JERR_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000332 /* 2-pass quantizer only works in 3-component color space. */
333 if (cinfo->out_color_components != 3) {
334 cinfo->enable_1pass_quant = TRUE;
335 cinfo->enable_external_quant = FALSE;
336 cinfo->enable_2pass_quant = FALSE;
337 cinfo->colormap = NULL;
338 } else if (cinfo->colormap != NULL) {
339 cinfo->enable_external_quant = TRUE;
340 } else if (cinfo->two_pass_quantize) {
341 cinfo->enable_2pass_quant = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000343 cinfo->enable_1pass_quant = TRUE;
344 }
345
346 if (cinfo->enable_1pass_quant) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347#ifdef QUANT_1PASS_SUPPORTED
348 jinit_1pass_quantizer(cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000349 master->quantizer_1pass = cinfo->cquantize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350#else
351 ERREXIT(cinfo, JERR_NOT_COMPILED);
352#endif
353 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000354
355 /* We use the 2-pass code to map to external colormaps. */
356 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
357#ifdef QUANT_2PASS_SUPPORTED
358 jinit_2pass_quantizer(cinfo);
359 master->quantizer_2pass = cinfo->cquantize;
360#else
361 ERREXIT(cinfo, JERR_NOT_COMPILED);
362#endif
363 }
364 /* If both quantizers are initialized, the 2-pass one is left active;
365 * this is necessary for starting with quantization to an external map.
366 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 }
368
369 /* Post-processing: in particular, color conversion first */
370 if (! cinfo->raw_data_out) {
371 if (master->using_merged_upsample) {
372#ifdef UPSAMPLE_MERGING_SUPPORTED
373 jinit_merged_upsampler(cinfo); /* does color conversion too */
374#else
375 ERREXIT(cinfo, JERR_NOT_COMPILED);
376#endif
377 } else {
378 jinit_color_deconverter(cinfo);
379 jinit_upsampler(cinfo);
380 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000381 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000382 }
383 /* Inverse DCT */
384 jinit_inverse_dct(cinfo);
385 /* Entropy decoding: either Huffman or arithmetic coding. */
386 if (cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000388 } else {
389 if (cinfo->progressive_mode) {
390#ifdef D_PROGRESSIVE_SUPPORTED
391 jinit_phuff_decoder(cinfo);
392#else
393 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000394#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000395 } else
396 jinit_huff_decoder(cinfo);
397 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000398
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000399 /* Initialize principal buffer controllers. */
400 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
401 jinit_d_coef_controller(cinfo, use_c_buffer);
402
403 if (! cinfo->raw_data_out)
404 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000405
406 /* We can now tell the memory manager to allocate virtual arrays. */
407 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000408
409 /* Initialize input side of decompressor to consume first scan. */
410 (*cinfo->inputctl->start_input_pass) (cinfo);
411
412#ifdef D_MULTISCAN_FILES_SUPPORTED
413 /* If jpeg_start_decompress will read the whole file, initialize
414 * progress monitoring appropriately. The input step is counted
415 * as one pass.
416 */
417 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
418 cinfo->inputctl->has_multiple_scans) {
419 int nscans;
420 /* Estimate number of scans to set pass_limit. */
421 if (cinfo->progressive_mode) {
422 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
423 nscans = 2 + 3 * cinfo->num_components;
424 } else {
425 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
426 nscans = cinfo->num_components;
427 }
428 cinfo->progress->pass_counter = 0L;
429 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
430 cinfo->progress->completed_passes = 0;
431 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
432 /* Count the input pass as done */
433 master->pass_number++;
434 }
435#endif /* D_MULTISCAN_FILES_SUPPORTED */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436}
437
438
439/*
440 * Per-pass setup.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000441 * This is called at the beginning of each output pass. We determine which
442 * modules will be active during this pass and give them appropriate
443 * start_pass calls. We also set is_dummy_pass to indicate whether this
444 * is a "real" output pass or a dummy pass for color quantization.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000445 * (In the latter case, jdapistd.c will crank the pass to completion.)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446 */
447
Thomas G. Lane489583f1996-02-07 00:00:00 +0000448METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000449prepare_for_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000450{
451 my_master_ptr master = (my_master_ptr) cinfo->master;
452
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000453 if (master->pub.is_dummy_pass) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454#ifdef QUANT_2PASS_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000455 /* Final pass of 2-pass quantization */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000456 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000457 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
458 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
459 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000460#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000461 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000462#endif /* QUANT_2PASS_SUPPORTED */
463 } else {
464 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
465 /* Select new quantization method */
466 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
467 cinfo->cquantize = master->quantizer_2pass;
468 master->pub.is_dummy_pass = TRUE;
469 } else if (cinfo->enable_1pass_quant) {
470 cinfo->cquantize = master->quantizer_1pass;
471 } else {
472 ERREXIT(cinfo, JERR_MODE_CHANGE);
473 }
474 }
475 (*cinfo->idct->start_pass) (cinfo);
476 (*cinfo->coef->start_output_pass) (cinfo);
477 if (! cinfo->raw_data_out) {
478 if (! master->using_merged_upsample)
479 (*cinfo->cconvert->start_pass) (cinfo);
480 (*cinfo->upsample->start_pass) (cinfo);
481 if (cinfo->quantize_colors)
482 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
483 (*cinfo->post->start_pass) (cinfo,
484 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
485 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
486 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000487 }
488
489 /* Set up progress monitor's pass info if present */
490 if (cinfo->progress != NULL) {
491 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000492 cinfo->progress->total_passes = master->pass_number +
493 (master->pub.is_dummy_pass ? 2 : 1);
494 /* In buffered-image mode, we assume one more output pass if EOI not
495 * yet reached, but no more passes if EOI has been reached.
496 */
497 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
498 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
499 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000500 }
501}
502
503
504/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000505 * Finish up at end of an output pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000506 */
507
Thomas G. Lane489583f1996-02-07 00:00:00 +0000508METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000509finish_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000510{
511 my_master_ptr master = (my_master_ptr) cinfo->master;
512
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000513 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000514 (*cinfo->cquantize->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000515 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000516}
517
518
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000519#ifdef D_MULTISCAN_FILES_SUPPORTED
520
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000522 * Switch to a new external colormap between output passes.
523 */
524
Thomas G. Lane489583f1996-02-07 00:00:00 +0000525GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000526jpeg_new_colormap (j_decompress_ptr cinfo)
527{
528 my_master_ptr master = (my_master_ptr) cinfo->master;
529
530 /* Prevent application from calling me at wrong times */
531 if (cinfo->global_state != DSTATE_BUFIMAGE)
532 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
533
534 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
535 cinfo->colormap != NULL) {
536 /* Select 2-pass quantizer for external colormap use */
537 cinfo->cquantize = master->quantizer_2pass;
538 /* Notify quantizer of colormap change */
539 (*cinfo->cquantize->new_color_map) (cinfo);
540 master->pub.is_dummy_pass = FALSE; /* just in case */
541 } else
542 ERREXIT(cinfo, JERR_MODE_CHANGE);
543}
544
545#endif /* D_MULTISCAN_FILES_SUPPORTED */
546
547
548/*
549 * Initialize master decompression control and select active modules.
550 * This is performed at the start of jpeg_start_decompress.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000551 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000552
Thomas G. Lane489583f1996-02-07 00:00:00 +0000553GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000554jinit_master_decompress (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000555{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000556 my_master_ptr master;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000557
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000558 master = (my_master_ptr)
559 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
560 SIZEOF(my_decomp_master));
561 cinfo->master = (struct jpeg_decomp_master *) master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000562 master->pub.prepare_for_output_pass = prepare_for_output_pass;
563 master->pub.finish_output_pass = finish_output_pass;
564
565 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000566
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000567 master_selection(cinfo);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000568}