blob: 38b7dd0cb77093142af61467a3ac7c53aadc29b1 [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.
DRC36a6eec2010-10-08 08:05:44 +00005 * Copyright (C) 2009-2010, 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"
DRC36a6eec2010-10-08 08:05:44 +000018#include "jpegcomp.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000019
20
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000021/* Private state */
22
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023typedef struct {
24 struct jpeg_decomp_master pub; /* public fields */
25
Thomas G. Lanebc79e061995-08-02 00:00:00 +000026 int pass_number; /* # of passes completed */
27
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000028 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
29
Thomas G. Lanebc79e061995-08-02 00:00:00 +000030 /* Saved references to initialized quantizer modules,
31 * in case we need to switch modes.
32 */
33 struct jpeg_color_quantizer * quantizer_1pass;
34 struct jpeg_color_quantizer * quantizer_2pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000035} my_decomp_master;
36
37typedef my_decomp_master * my_master_ptr;
38
39
40/*
41 * Determine whether merged upsample/color conversion should be used.
42 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
43 */
44
Thomas G. Lane489583f1996-02-07 00:00:00 +000045LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000046use_merged_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000047{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000048#ifdef UPSAMPLE_MERGING_SUPPORTED
49 /* Merging is the equivalent of plain box-filter upsampling */
50 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
51 return FALSE;
52 /* jdmerge.c only supports YCC=>RGB color conversion */
53 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
DRC720e1612009-04-05 21:51:25 +000054 (cinfo->out_color_space != JCS_RGB &&
55 cinfo->out_color_space != JCS_EXT_RGB &&
56 cinfo->out_color_space != JCS_EXT_RGBX &&
57 cinfo->out_color_space != JCS_EXT_BGR &&
58 cinfo->out_color_space != JCS_EXT_BGRX &&
59 cinfo->out_color_space != JCS_EXT_XBGR &&
60 cinfo->out_color_space != JCS_EXT_XRGB) ||
61 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space])
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062 return FALSE;
63 /* and it only handles 2h1v or 2h2v sampling ratios */
64 if (cinfo->comp_info[0].h_samp_factor != 2 ||
65 cinfo->comp_info[1].h_samp_factor != 1 ||
66 cinfo->comp_info[2].h_samp_factor != 1 ||
67 cinfo->comp_info[0].v_samp_factor > 2 ||
68 cinfo->comp_info[1].v_samp_factor != 1 ||
69 cinfo->comp_info[2].v_samp_factor != 1)
70 return FALSE;
71 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
DRC49967cd2010-10-09 19:57:51 +000072 if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
73 cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
74 cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075 return FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000076 /* ??? also need to test for upsample-time rescaling, when & if supported */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000077 return TRUE; /* by golly, it'll work... */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078#else
79 return FALSE;
80#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000081}
82
83
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000085 * Compute output image dimensions and related values.
86 * NOTE: this is exported for possible use by application.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 * Hence it mustn't do anything that can't be done twice.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000088 * Also note that it may be called before the master module is initialized!
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089 */
90
Thomas G. Lane489583f1996-02-07 00:00:00 +000091GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
93/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000094{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000095#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000097 jpeg_component_info *compptr;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000098#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000099
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000100 /* Prevent application from calling me at wrong times */
101 if (cinfo->global_state != DSTATE_READY)
102 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
103
104#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000105
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000106 /* Compute actual output image dimensions and DCT scaling choices. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107 if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
108 /* Provide 1/8 scaling */
109 cinfo->output_width = (JDIMENSION)
110 jdiv_round_up((long) cinfo->image_width, 8L);
111 cinfo->output_height = (JDIMENSION)
112 jdiv_round_up((long) cinfo->image_height, 8L);
DRC36a6eec2010-10-08 08:05:44 +0000113#if JPEG_LIB_VERSION >= 70
114 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 1;
115#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116 cinfo->min_DCT_scaled_size = 1;
DRC36a6eec2010-10-08 08:05:44 +0000117#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
119 /* Provide 1/4 scaling */
120 cinfo->output_width = (JDIMENSION)
121 jdiv_round_up((long) cinfo->image_width, 4L);
122 cinfo->output_height = (JDIMENSION)
123 jdiv_round_up((long) cinfo->image_height, 4L);
DRC36a6eec2010-10-08 08:05:44 +0000124#if JPEG_LIB_VERSION >= 70
125 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 2;
126#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000127 cinfo->min_DCT_scaled_size = 2;
DRC36a6eec2010-10-08 08:05:44 +0000128#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129 } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
130 /* Provide 1/2 scaling */
131 cinfo->output_width = (JDIMENSION)
132 jdiv_round_up((long) cinfo->image_width, 2L);
133 cinfo->output_height = (JDIMENSION)
134 jdiv_round_up((long) cinfo->image_height, 2L);
DRC36a6eec2010-10-08 08:05:44 +0000135#if JPEG_LIB_VERSION >= 70
136 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 4;
137#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138 cinfo->min_DCT_scaled_size = 4;
DRC36a6eec2010-10-08 08:05:44 +0000139#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000140 } else {
141 /* Provide 1/1 scaling */
142 cinfo->output_width = cinfo->image_width;
143 cinfo->output_height = cinfo->image_height;
DRC36a6eec2010-10-08 08:05:44 +0000144#if JPEG_LIB_VERSION >= 70
145 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;
146#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147 cinfo->min_DCT_scaled_size = DCTSIZE;
DRC36a6eec2010-10-08 08:05:44 +0000148#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149 }
150 /* In selecting the actual DCT scaling for each component, we try to
151 * scale up the chroma components via IDCT scaling rather than upsampling.
152 * This saves time if the upsampler gets to use 1:1 scaling.
153 * Note this code assumes that the supported DCT scalings are powers of 2.
154 */
155 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
156 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000157 int ssize = cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 while (ssize < DCTSIZE &&
159 (compptr->h_samp_factor * ssize * 2 <=
DRC49967cd2010-10-09 19:57:51 +0000160 cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) &&
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000161 (compptr->v_samp_factor * ssize * 2 <=
DRC49967cd2010-10-09 19:57:51 +0000162 cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000163 ssize = ssize * 2;
164 }
DRC36a6eec2010-10-08 08:05:44 +0000165#if JPEG_LIB_VERSION >= 70
166 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
167#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000168 compptr->DCT_scaled_size = ssize;
DRC36a6eec2010-10-08 08:05:44 +0000169#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000170 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000171
172 /* Recompute downsampled dimensions of components;
173 * application needs to know these if using raw downsampled data.
174 */
175 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
176 ci++, compptr++) {
177 /* Size in samples, after IDCT scaling */
178 compptr->downsampled_width = (JDIMENSION)
179 jdiv_round_up((long) cinfo->image_width *
DRC49967cd2010-10-09 19:57:51 +0000180 (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000181 (long) (cinfo->max_h_samp_factor * DCTSIZE));
182 compptr->downsampled_height = (JDIMENSION)
183 jdiv_round_up((long) cinfo->image_height *
DRC49967cd2010-10-09 19:57:51 +0000184 (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000185 (long) (cinfo->max_v_samp_factor * DCTSIZE));
186 }
187
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188#else /* !IDCT_SCALING_SUPPORTED */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000189
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190 /* Hardwire it to "no scaling" */
191 cinfo->output_width = cinfo->image_width;
192 cinfo->output_height = cinfo->image_height;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000193 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
194 * and has computed unscaled downsampled_width and downsampled_height.
195 */
196
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000197#endif /* IDCT_SCALING_SUPPORTED */
198
199 /* Report number of components in selected colorspace. */
200 /* Probably this should be in the color conversion module... */
201 switch (cinfo->out_color_space) {
202 case JCS_GRAYSCALE:
203 cinfo->out_color_components = 1;
204 break;
205 case JCS_RGB:
DRC720e1612009-04-05 21:51:25 +0000206 case JCS_EXT_RGB:
207 case JCS_EXT_RGBX:
208 case JCS_EXT_BGR:
209 case JCS_EXT_BGRX:
210 case JCS_EXT_XBGR:
211 case JCS_EXT_XRGB:
212 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000214 case JCS_YCbCr:
215 cinfo->out_color_components = 3;
216 break;
217 case JCS_CMYK:
218 case JCS_YCCK:
219 cinfo->out_color_components = 4;
220 break;
221 default: /* else must be same colorspace as in file */
222 cinfo->out_color_components = cinfo->num_components;
223 break;
224 }
225 cinfo->output_components = (cinfo->quantize_colors ? 1 :
226 cinfo->out_color_components);
227
228 /* See if upsampler will want to emit more than one row at a time */
229 if (use_merged_upsample(cinfo))
230 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
231 else
232 cinfo->rec_outbuf_height = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000233}
234
235
236/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237 * Several decompression processes need to range-limit values to the range
238 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
239 * due to noise introduced by quantization, roundoff error, etc. These
240 * processes are inner loops and need to be as fast as possible. On most
241 * machines, particularly CPUs with pipelines or instruction prefetch,
242 * a (subscript-check-less) C table lookup
243 * x = sample_range_limit[x];
244 * is faster than explicit tests
245 * if (x < 0) x = 0;
246 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
247 * These processes all use a common table prepared by the routine below.
248 *
249 * For most steps we can mathematically guarantee that the initial value
250 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
251 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
252 * limiting step (just after the IDCT), a wildly out-of-range value is
253 * possible if the input data is corrupt. To avoid any chance of indexing
254 * off the end of memory and getting a bad-pointer trap, we perform the
255 * post-IDCT limiting thus:
256 * x = range_limit[x & MASK];
257 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
258 * samples. Under normal circumstances this is more than enough range and
259 * a correct output will be generated; with bogus input data the mask will
260 * cause wraparound, and we will safely generate a bogus-but-in-range output.
261 * For the post-IDCT step, we want to convert the data from signed to unsigned
262 * representation by adding CENTERJSAMPLE at the same time that we limit it.
263 * So the post-IDCT limiting table ends up looking like this:
264 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
265 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
266 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
267 * 0,1,...,CENTERJSAMPLE-1
268 * Negative inputs select values from the upper half of the table after
269 * masking.
270 *
271 * We can save some space by overlapping the start of the post-IDCT table
272 * with the simpler range limiting table. The post-IDCT table begins at
273 * sample_range_limit + CENTERJSAMPLE.
274 *
275 * Note that the table is allocated in near data space on PCs; it's small
276 * enough and used often enough to justify this.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000277 */
278
Thomas G. Lane489583f1996-02-07 00:00:00 +0000279LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000280prepare_range_limit_table (j_decompress_ptr cinfo)
281/* Allocate and fill in the sample_range_limit table */
282{
283 JSAMPLE * table;
284 int i;
285
286 table = (JSAMPLE *)
287 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
288 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
289 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
290 cinfo->sample_range_limit = table;
291 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
292 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
293 /* Main part of "simple" table: limit[x] = x */
294 for (i = 0; i <= MAXJSAMPLE; i++)
295 table[i] = (JSAMPLE) i;
296 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
297 /* End of simple table, rest of first half of post-IDCT table */
298 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
299 table[i] = MAXJSAMPLE;
300 /* Second half of post-IDCT table */
301 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
302 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
303 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
304 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
305}
306
307
308/*
309 * Master selection of decompression modules.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000310 * This is done once at jpeg_start_decompress time. We determine
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311 * which modules will be used and give them appropriate initialization calls.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000312 * We also initialize the decompressor input side to begin consuming data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000313 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000314 * Since jpeg_read_header has finished, we know what is in the SOF
315 * and (first) SOS markers. We also have all the application parameter
316 * settings.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 */
318
Thomas G. Lane489583f1996-02-07 00:00:00 +0000319LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320master_selection (j_decompress_ptr cinfo)
321{
322 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000323 boolean use_c_buffer;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000324 long samplesperrow;
325 JDIMENSION jd_samplesperrow;
326
327 /* Initialize dimensions and other stuff */
328 jpeg_calc_output_dimensions(cinfo);
329 prepare_range_limit_table(cinfo);
330
331 /* Width of an output scanline must be representable as JDIMENSION. */
332 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
333 jd_samplesperrow = (JDIMENSION) samplesperrow;
334 if ((long) jd_samplesperrow != samplesperrow)
335 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
336
337 /* Initialize my private state */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000338 master->pass_number = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000339 master->using_merged_upsample = use_merged_upsample(cinfo);
340
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000341 /* Color quantizer selection */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000342 master->quantizer_1pass = NULL;
343 master->quantizer_2pass = NULL;
344 /* No mode changes if not using buffered-image mode. */
345 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
346 cinfo->enable_1pass_quant = FALSE;
347 cinfo->enable_external_quant = FALSE;
348 cinfo->enable_2pass_quant = FALSE;
349 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350 if (cinfo->quantize_colors) {
351 if (cinfo->raw_data_out)
352 ERREXIT(cinfo, JERR_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000353 /* 2-pass quantizer only works in 3-component color space. */
354 if (cinfo->out_color_components != 3) {
355 cinfo->enable_1pass_quant = TRUE;
356 cinfo->enable_external_quant = FALSE;
357 cinfo->enable_2pass_quant = FALSE;
358 cinfo->colormap = NULL;
359 } else if (cinfo->colormap != NULL) {
360 cinfo->enable_external_quant = TRUE;
361 } else if (cinfo->two_pass_quantize) {
362 cinfo->enable_2pass_quant = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000364 cinfo->enable_1pass_quant = TRUE;
365 }
366
367 if (cinfo->enable_1pass_quant) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368#ifdef QUANT_1PASS_SUPPORTED
369 jinit_1pass_quantizer(cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000370 master->quantizer_1pass = cinfo->cquantize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371#else
372 ERREXIT(cinfo, JERR_NOT_COMPILED);
373#endif
374 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000375
376 /* We use the 2-pass code to map to external colormaps. */
377 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
378#ifdef QUANT_2PASS_SUPPORTED
379 jinit_2pass_quantizer(cinfo);
380 master->quantizer_2pass = cinfo->cquantize;
381#else
382 ERREXIT(cinfo, JERR_NOT_COMPILED);
383#endif
384 }
385 /* If both quantizers are initialized, the 2-pass one is left active;
386 * this is necessary for starting with quantization to an external map.
387 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 }
389
390 /* Post-processing: in particular, color conversion first */
391 if (! cinfo->raw_data_out) {
392 if (master->using_merged_upsample) {
393#ifdef UPSAMPLE_MERGING_SUPPORTED
394 jinit_merged_upsampler(cinfo); /* does color conversion too */
395#else
396 ERREXIT(cinfo, JERR_NOT_COMPILED);
397#endif
398 } else {
399 jinit_color_deconverter(cinfo);
400 jinit_upsampler(cinfo);
401 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000402 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 }
404 /* Inverse DCT */
405 jinit_inverse_dct(cinfo);
406 /* Entropy decoding: either Huffman or arithmetic coding. */
407 if (cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000408 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000409 } else {
410 if (cinfo->progressive_mode) {
411#ifdef D_PROGRESSIVE_SUPPORTED
412 jinit_phuff_decoder(cinfo);
413#else
414 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000415#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000416 } else
417 jinit_huff_decoder(cinfo);
418 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000419
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000420 /* Initialize principal buffer controllers. */
421 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
422 jinit_d_coef_controller(cinfo, use_c_buffer);
423
424 if (! cinfo->raw_data_out)
425 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000426
427 /* We can now tell the memory manager to allocate virtual arrays. */
428 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000429
430 /* Initialize input side of decompressor to consume first scan. */
431 (*cinfo->inputctl->start_input_pass) (cinfo);
432
433#ifdef D_MULTISCAN_FILES_SUPPORTED
434 /* If jpeg_start_decompress will read the whole file, initialize
435 * progress monitoring appropriately. The input step is counted
436 * as one pass.
437 */
438 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
439 cinfo->inputctl->has_multiple_scans) {
440 int nscans;
441 /* Estimate number of scans to set pass_limit. */
442 if (cinfo->progressive_mode) {
443 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
444 nscans = 2 + 3 * cinfo->num_components;
445 } else {
446 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
447 nscans = cinfo->num_components;
448 }
449 cinfo->progress->pass_counter = 0L;
450 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
451 cinfo->progress->completed_passes = 0;
452 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
453 /* Count the input pass as done */
454 master->pass_number++;
455 }
456#endif /* D_MULTISCAN_FILES_SUPPORTED */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000457}
458
459
460/*
461 * Per-pass setup.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000462 * This is called at the beginning of each output pass. We determine which
463 * modules will be active during this pass and give them appropriate
464 * start_pass calls. We also set is_dummy_pass to indicate whether this
465 * is a "real" output pass or a dummy pass for color quantization.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000466 * (In the latter case, jdapistd.c will crank the pass to completion.)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 */
468
Thomas G. Lane489583f1996-02-07 00:00:00 +0000469METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000470prepare_for_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000471{
472 my_master_ptr master = (my_master_ptr) cinfo->master;
473
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000474 if (master->pub.is_dummy_pass) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475#ifdef QUANT_2PASS_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000476 /* Final pass of 2-pass quantization */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000477 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
479 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
480 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000481#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000482 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000483#endif /* QUANT_2PASS_SUPPORTED */
484 } else {
485 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
486 /* Select new quantization method */
487 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
488 cinfo->cquantize = master->quantizer_2pass;
489 master->pub.is_dummy_pass = TRUE;
490 } else if (cinfo->enable_1pass_quant) {
491 cinfo->cquantize = master->quantizer_1pass;
492 } else {
493 ERREXIT(cinfo, JERR_MODE_CHANGE);
494 }
495 }
496 (*cinfo->idct->start_pass) (cinfo);
497 (*cinfo->coef->start_output_pass) (cinfo);
498 if (! cinfo->raw_data_out) {
499 if (! master->using_merged_upsample)
500 (*cinfo->cconvert->start_pass) (cinfo);
501 (*cinfo->upsample->start_pass) (cinfo);
502 if (cinfo->quantize_colors)
503 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
504 (*cinfo->post->start_pass) (cinfo,
505 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
506 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
507 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000508 }
509
510 /* Set up progress monitor's pass info if present */
511 if (cinfo->progress != NULL) {
512 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000513 cinfo->progress->total_passes = master->pass_number +
514 (master->pub.is_dummy_pass ? 2 : 1);
515 /* In buffered-image mode, we assume one more output pass if EOI not
516 * yet reached, but no more passes if EOI has been reached.
517 */
518 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
519 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
520 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 }
522}
523
524
525/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000526 * Finish up at end of an output pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000527 */
528
Thomas G. Lane489583f1996-02-07 00:00:00 +0000529METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000530finish_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000531{
532 my_master_ptr master = (my_master_ptr) cinfo->master;
533
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000534 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000535 (*cinfo->cquantize->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000536 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000537}
538
539
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000540#ifdef D_MULTISCAN_FILES_SUPPORTED
541
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000542/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000543 * Switch to a new external colormap between output passes.
544 */
545
Thomas G. Lane489583f1996-02-07 00:00:00 +0000546GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000547jpeg_new_colormap (j_decompress_ptr cinfo)
548{
549 my_master_ptr master = (my_master_ptr) cinfo->master;
550
551 /* Prevent application from calling me at wrong times */
552 if (cinfo->global_state != DSTATE_BUFIMAGE)
553 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
554
555 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
556 cinfo->colormap != NULL) {
557 /* Select 2-pass quantizer for external colormap use */
558 cinfo->cquantize = master->quantizer_2pass;
559 /* Notify quantizer of colormap change */
560 (*cinfo->cquantize->new_color_map) (cinfo);
561 master->pub.is_dummy_pass = FALSE; /* just in case */
562 } else
563 ERREXIT(cinfo, JERR_MODE_CHANGE);
564}
565
566#endif /* D_MULTISCAN_FILES_SUPPORTED */
567
568
569/*
570 * Initialize master decompression control and select active modules.
571 * This is performed at the start of jpeg_start_decompress.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000572 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000573
Thomas G. Lane489583f1996-02-07 00:00:00 +0000574GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000575jinit_master_decompress (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000576{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000577 my_master_ptr master;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000578
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000579 master = (my_master_ptr)
580 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
581 SIZEOF(my_decomp_master));
582 cinfo->master = (struct jpeg_decomp_master *) master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000583 master->pub.prepare_for_output_pass = prepare_for_output_pass;
584 master->pub.finish_output_pass = finish_output_pass;
585
586 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000587
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000588 master_selection(cinfo);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000589}