blob: 25ad416c523bab54a70a6a5906c42bf4b2521b22 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jdmaster.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRCa73e8702012-12-31 02:52:30 +00006 * Modifications:
DRC67ce3b22011-12-19 02:21:03 +00007 * Copyright (C) 2009-2011, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000010 * This file contains master control logic for the JPEG decompressor.
11 * These routines are concerned with selecting the modules to be executed
12 * and with determining the number of passes and the work to be done in each
13 * pass.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000014 */
15
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000017#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000018#include "jpeglib.h"
DRC36a6eec2010-10-08 08:05:44 +000019#include "jpegcomp.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000020
21
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022/* Private state */
23
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024typedef struct {
25 struct jpeg_decomp_master pub; /* public fields */
26
Thomas G. Lanebc79e061995-08-02 00:00:00 +000027 int pass_number; /* # of passes completed */
28
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
30
Thomas G. Lanebc79e061995-08-02 00:00:00 +000031 /* Saved references to initialized quantizer modules,
32 * in case we need to switch modes.
33 */
34 struct jpeg_color_quantizer * quantizer_1pass;
35 struct jpeg_color_quantizer * quantizer_2pass;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000036} my_decomp_master;
37
38typedef my_decomp_master * my_master_ptr;
39
40
41/*
42 * Determine whether merged upsample/color conversion should be used.
43 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
44 */
45
Thomas G. Lane489583f1996-02-07 00:00:00 +000046LOCAL(boolean)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047use_merged_upsample (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000048{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049#ifdef UPSAMPLE_MERGING_SUPPORTED
50 /* Merging is the equivalent of plain box-filter upsampling */
51 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
52 return FALSE;
53 /* jdmerge.c only supports YCC=>RGB color conversion */
54 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
DRC720e1612009-04-05 21:51:25 +000055 (cinfo->out_color_space != JCS_RGB &&
56 cinfo->out_color_space != JCS_EXT_RGB &&
57 cinfo->out_color_space != JCS_EXT_RGBX &&
58 cinfo->out_color_space != JCS_EXT_BGR &&
59 cinfo->out_color_space != JCS_EXT_BGRX &&
60 cinfo->out_color_space != JCS_EXT_XBGR &&
DRC67ce3b22011-12-19 02:21:03 +000061 cinfo->out_color_space != JCS_EXT_XRGB &&
62 cinfo->out_color_space != JCS_EXT_RGBA &&
63 cinfo->out_color_space != JCS_EXT_BGRA &&
64 cinfo->out_color_space != JCS_EXT_ABGR &&
65 cinfo->out_color_space != JCS_EXT_ARGB) ||
DRC720e1612009-04-05 21:51:25 +000066 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space])
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067 return FALSE;
68 /* and it only handles 2h1v or 2h2v sampling ratios */
69 if (cinfo->comp_info[0].h_samp_factor != 2 ||
70 cinfo->comp_info[1].h_samp_factor != 1 ||
71 cinfo->comp_info[2].h_samp_factor != 1 ||
72 cinfo->comp_info[0].v_samp_factor > 2 ||
73 cinfo->comp_info[1].v_samp_factor != 1 ||
74 cinfo->comp_info[2].v_samp_factor != 1)
75 return FALSE;
76 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
DRC49967cd2010-10-09 19:57:51 +000077 if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
78 cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
79 cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080 return FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081 /* ??? also need to test for upsample-time rescaling, when & if supported */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000082 return TRUE; /* by golly, it'll work... */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083#else
84 return FALSE;
85#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000086}
87
88
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +000090 * Compute output image dimensions and related values.
91 * NOTE: this is exported for possible use by application.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092 * Hence it mustn't do anything that can't be done twice.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000093 * Also note that it may be called before the master module is initialized!
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094 */
95
Thomas G. Lane489583f1996-02-07 00:00:00 +000096GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
98/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000099{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000100#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000101 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000102 jpeg_component_info *compptr;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000103#endif
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000104
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000105 /* Prevent application from calling me at wrong times */
106 if (cinfo->global_state != DSTATE_READY)
107 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
108
109#ifdef IDCT_SCALING_SUPPORTED
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000110
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111 /* Compute actual output image dimensions and DCT scaling choices. */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000112 if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
113 /* Provide 1/8 scaling */
114 cinfo->output_width = (JDIMENSION)
115 jdiv_round_up((long) cinfo->image_width, 8L);
116 cinfo->output_height = (JDIMENSION)
117 jdiv_round_up((long) cinfo->image_height, 8L);
DRC36a6eec2010-10-08 08:05:44 +0000118#if JPEG_LIB_VERSION >= 70
119 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 1;
120#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000121 cinfo->min_DCT_scaled_size = 1;
DRC36a6eec2010-10-08 08:05:44 +0000122#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000123 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
124 /* Provide 1/4 scaling */
125 cinfo->output_width = (JDIMENSION)
126 jdiv_round_up((long) cinfo->image_width, 4L);
127 cinfo->output_height = (JDIMENSION)
128 jdiv_round_up((long) cinfo->image_height, 4L);
DRC36a6eec2010-10-08 08:05:44 +0000129#if JPEG_LIB_VERSION >= 70
130 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 2;
131#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132 cinfo->min_DCT_scaled_size = 2;
DRC36a6eec2010-10-08 08:05:44 +0000133#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000134 } 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);
DRC36a6eec2010-10-08 08:05:44 +0000140#if JPEG_LIB_VERSION >= 70
141 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = 4;
142#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000143 cinfo->min_DCT_scaled_size = 4;
DRC36a6eec2010-10-08 08:05:44 +0000144#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145 } else {
146 /* Provide 1/1 scaling */
147 cinfo->output_width = cinfo->image_width;
148 cinfo->output_height = cinfo->image_height;
DRC36a6eec2010-10-08 08:05:44 +0000149#if JPEG_LIB_VERSION >= 70
150 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;
151#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152 cinfo->min_DCT_scaled_size = DCTSIZE;
DRC36a6eec2010-10-08 08:05:44 +0000153#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154 }
155 /* In selecting the actual DCT scaling for each component, we try to
156 * scale up the chroma components via IDCT scaling rather than upsampling.
157 * This saves time if the upsampler gets to use 1:1 scaling.
158 * Note this code assumes that the supported DCT scalings are powers of 2.
159 */
160 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
161 ci++, compptr++) {
DRC49967cd2010-10-09 19:57:51 +0000162 int ssize = cinfo->_min_DCT_scaled_size;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000163 while (ssize < DCTSIZE &&
164 (compptr->h_samp_factor * ssize * 2 <=
DRC49967cd2010-10-09 19:57:51 +0000165 cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) &&
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000166 (compptr->v_samp_factor * ssize * 2 <=
DRC49967cd2010-10-09 19:57:51 +0000167 cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000168 ssize = ssize * 2;
169 }
DRC36a6eec2010-10-08 08:05:44 +0000170#if JPEG_LIB_VERSION >= 70
171 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
172#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173 compptr->DCT_scaled_size = ssize;
DRC36a6eec2010-10-08 08:05:44 +0000174#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000176
177 /* Recompute downsampled dimensions of components;
178 * application needs to know these if using raw downsampled data.
179 */
180 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
181 ci++, compptr++) {
182 /* Size in samples, after IDCT scaling */
183 compptr->downsampled_width = (JDIMENSION)
184 jdiv_round_up((long) cinfo->image_width *
DRC49967cd2010-10-09 19:57:51 +0000185 (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000186 (long) (cinfo->max_h_samp_factor * DCTSIZE));
187 compptr->downsampled_height = (JDIMENSION)
188 jdiv_round_up((long) cinfo->image_height *
DRC49967cd2010-10-09 19:57:51 +0000189 (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000190 (long) (cinfo->max_v_samp_factor * DCTSIZE));
191 }
192
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193#else /* !IDCT_SCALING_SUPPORTED */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000194
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000195 /* Hardwire it to "no scaling" */
196 cinfo->output_width = cinfo->image_width;
197 cinfo->output_height = cinfo->image_height;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000198 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
199 * and has computed unscaled downsampled_width and downsampled_height.
200 */
201
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202#endif /* IDCT_SCALING_SUPPORTED */
203
204 /* Report number of components in selected colorspace. */
205 /* Probably this should be in the color conversion module... */
206 switch (cinfo->out_color_space) {
207 case JCS_GRAYSCALE:
208 cinfo->out_color_components = 1;
209 break;
210 case JCS_RGB:
DRC720e1612009-04-05 21:51:25 +0000211 case JCS_EXT_RGB:
212 case JCS_EXT_RGBX:
213 case JCS_EXT_BGR:
214 case JCS_EXT_BGRX:
215 case JCS_EXT_XBGR:
216 case JCS_EXT_XRGB:
DRC67ce3b22011-12-19 02:21:03 +0000217 case JCS_EXT_RGBA:
218 case JCS_EXT_BGRA:
219 case JCS_EXT_ABGR:
220 case JCS_EXT_ARGB:
DRC720e1612009-04-05 21:51:25 +0000221 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223 case JCS_YCbCr:
224 cinfo->out_color_components = 3;
225 break;
226 case JCS_CMYK:
227 case JCS_YCCK:
228 cinfo->out_color_components = 4;
229 break;
230 default: /* else must be same colorspace as in file */
231 cinfo->out_color_components = cinfo->num_components;
232 break;
233 }
234 cinfo->output_components = (cinfo->quantize_colors ? 1 :
235 cinfo->out_color_components);
236
237 /* See if upsampler will want to emit more than one row at a time */
238 if (use_merged_upsample(cinfo))
239 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
240 else
241 cinfo->rec_outbuf_height = 1;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000242}
243
244
245/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 * Several decompression processes need to range-limit values to the range
247 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
248 * due to noise introduced by quantization, roundoff error, etc. These
249 * processes are inner loops and need to be as fast as possible. On most
250 * machines, particularly CPUs with pipelines or instruction prefetch,
251 * a (subscript-check-less) C table lookup
252 * x = sample_range_limit[x];
253 * is faster than explicit tests
254 * if (x < 0) x = 0;
255 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
256 * These processes all use a common table prepared by the routine below.
257 *
258 * For most steps we can mathematically guarantee that the initial value
259 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
260 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
261 * limiting step (just after the IDCT), a wildly out-of-range value is
262 * possible if the input data is corrupt. To avoid any chance of indexing
263 * off the end of memory and getting a bad-pointer trap, we perform the
264 * post-IDCT limiting thus:
265 * x = range_limit[x & MASK];
266 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
267 * samples. Under normal circumstances this is more than enough range and
268 * a correct output will be generated; with bogus input data the mask will
269 * cause wraparound, and we will safely generate a bogus-but-in-range output.
270 * For the post-IDCT step, we want to convert the data from signed to unsigned
271 * representation by adding CENTERJSAMPLE at the same time that we limit it.
272 * So the post-IDCT limiting table ends up looking like this:
273 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
274 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
275 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
276 * 0,1,...,CENTERJSAMPLE-1
277 * Negative inputs select values from the upper half of the table after
278 * masking.
279 *
280 * We can save some space by overlapping the start of the post-IDCT table
281 * with the simpler range limiting table. The post-IDCT table begins at
282 * sample_range_limit + CENTERJSAMPLE.
283 *
284 * Note that the table is allocated in near data space on PCs; it's small
285 * enough and used often enough to justify this.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000286 */
287
Thomas G. Lane489583f1996-02-07 00:00:00 +0000288LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289prepare_range_limit_table (j_decompress_ptr cinfo)
290/* Allocate and fill in the sample_range_limit table */
291{
292 JSAMPLE * table;
293 int i;
294
295 table = (JSAMPLE *)
296 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
297 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
298 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
299 cinfo->sample_range_limit = table;
300 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
301 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
302 /* Main part of "simple" table: limit[x] = x */
303 for (i = 0; i <= MAXJSAMPLE; i++)
304 table[i] = (JSAMPLE) i;
305 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
306 /* End of simple table, rest of first half of post-IDCT table */
307 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
308 table[i] = MAXJSAMPLE;
309 /* Second half of post-IDCT table */
310 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
311 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
312 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
313 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
314}
315
316
317/*
318 * Master selection of decompression modules.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000319 * This is done once at jpeg_start_decompress time. We determine
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000320 * which modules will be used and give them appropriate initialization calls.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000321 * We also initialize the decompressor input side to begin consuming data.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000322 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000323 * Since jpeg_read_header has finished, we know what is in the SOF
324 * and (first) SOS markers. We also have all the application parameter
325 * settings.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326 */
327
Thomas G. Lane489583f1996-02-07 00:00:00 +0000328LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000329master_selection (j_decompress_ptr cinfo)
330{
331 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000332 boolean use_c_buffer;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333 long samplesperrow;
334 JDIMENSION jd_samplesperrow;
335
336 /* Initialize dimensions and other stuff */
337 jpeg_calc_output_dimensions(cinfo);
338 prepare_range_limit_table(cinfo);
339
340 /* Width of an output scanline must be representable as JDIMENSION. */
341 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
342 jd_samplesperrow = (JDIMENSION) samplesperrow;
343 if ((long) jd_samplesperrow != samplesperrow)
344 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
345
346 /* Initialize my private state */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347 master->pass_number = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348 master->using_merged_upsample = use_merged_upsample(cinfo);
349
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350 /* Color quantizer selection */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000351 master->quantizer_1pass = NULL;
352 master->quantizer_2pass = NULL;
353 /* No mode changes if not using buffered-image mode. */
354 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
355 cinfo->enable_1pass_quant = FALSE;
356 cinfo->enable_external_quant = FALSE;
357 cinfo->enable_2pass_quant = FALSE;
358 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 if (cinfo->quantize_colors) {
360 if (cinfo->raw_data_out)
361 ERREXIT(cinfo, JERR_NOTIMPL);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000362 /* 2-pass quantizer only works in 3-component color space. */
363 if (cinfo->out_color_components != 3) {
364 cinfo->enable_1pass_quant = TRUE;
365 cinfo->enable_external_quant = FALSE;
366 cinfo->enable_2pass_quant = FALSE;
367 cinfo->colormap = NULL;
368 } else if (cinfo->colormap != NULL) {
369 cinfo->enable_external_quant = TRUE;
370 } else if (cinfo->two_pass_quantize) {
371 cinfo->enable_2pass_quant = TRUE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000372 } else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000373 cinfo->enable_1pass_quant = TRUE;
374 }
375
376 if (cinfo->enable_1pass_quant) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000377#ifdef QUANT_1PASS_SUPPORTED
378 jinit_1pass_quantizer(cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000379 master->quantizer_1pass = cinfo->cquantize;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000380#else
381 ERREXIT(cinfo, JERR_NOT_COMPILED);
382#endif
383 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000384
385 /* We use the 2-pass code to map to external colormaps. */
386 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
387#ifdef QUANT_2PASS_SUPPORTED
388 jinit_2pass_quantizer(cinfo);
389 master->quantizer_2pass = cinfo->cquantize;
390#else
391 ERREXIT(cinfo, JERR_NOT_COMPILED);
392#endif
393 }
394 /* If both quantizers are initialized, the 2-pass one is left active;
395 * this is necessary for starting with quantization to an external map.
396 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397 }
398
399 /* Post-processing: in particular, color conversion first */
400 if (! cinfo->raw_data_out) {
401 if (master->using_merged_upsample) {
402#ifdef UPSAMPLE_MERGING_SUPPORTED
403 jinit_merged_upsampler(cinfo); /* does color conversion too */
404#else
405 ERREXIT(cinfo, JERR_NOT_COMPILED);
406#endif
407 } else {
408 jinit_color_deconverter(cinfo);
409 jinit_upsampler(cinfo);
410 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000411 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000412 }
413 /* Inverse DCT */
414 jinit_inverse_dct(cinfo);
415 /* Entropy decoding: either Huffman or arithmetic coding. */
416 if (cinfo->arith_code) {
DRC66f97e62010-11-23 05:49:54 +0000417#ifdef D_ARITH_CODING_SUPPORTED
Guido Vollbeding1e247ac1998-03-28 00:00:00 +0000418 jinit_arith_decoder(cinfo);
DRC66f97e62010-11-23 05:49:54 +0000419#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000420 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
DRC66f97e62010-11-23 05:49:54 +0000421#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000422 } else {
423 if (cinfo->progressive_mode) {
424#ifdef D_PROGRESSIVE_SUPPORTED
425 jinit_phuff_decoder(cinfo);
426#else
427 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000428#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000429 } else
430 jinit_huff_decoder(cinfo);
431 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000433 /* Initialize principal buffer controllers. */
434 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
435 jinit_d_coef_controller(cinfo, use_c_buffer);
436
437 if (! cinfo->raw_data_out)
438 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000439
440 /* We can now tell the memory manager to allocate virtual arrays. */
441 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000442
443 /* Initialize input side of decompressor to consume first scan. */
444 (*cinfo->inputctl->start_input_pass) (cinfo);
445
446#ifdef D_MULTISCAN_FILES_SUPPORTED
447 /* If jpeg_start_decompress will read the whole file, initialize
448 * progress monitoring appropriately. The input step is counted
449 * as one pass.
450 */
451 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
452 cinfo->inputctl->has_multiple_scans) {
453 int nscans;
454 /* Estimate number of scans to set pass_limit. */
455 if (cinfo->progressive_mode) {
456 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
457 nscans = 2 + 3 * cinfo->num_components;
458 } else {
459 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
460 nscans = cinfo->num_components;
461 }
462 cinfo->progress->pass_counter = 0L;
463 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
464 cinfo->progress->completed_passes = 0;
465 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
466 /* Count the input pass as done */
467 master->pass_number++;
468 }
469#endif /* D_MULTISCAN_FILES_SUPPORTED */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000470}
471
472
473/*
474 * Per-pass setup.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000475 * This is called at the beginning of each output pass. We determine which
476 * modules will be active during this pass and give them appropriate
477 * start_pass calls. We also set is_dummy_pass to indicate whether this
478 * is a "real" output pass or a dummy pass for color quantization.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000479 * (In the latter case, jdapistd.c will crank the pass to completion.)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480 */
481
Thomas G. Lane489583f1996-02-07 00:00:00 +0000482METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000483prepare_for_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000484{
485 my_master_ptr master = (my_master_ptr) cinfo->master;
486
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000487 if (master->pub.is_dummy_pass) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000488#ifdef QUANT_2PASS_SUPPORTED
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000489 /* Final pass of 2-pass quantization */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000490 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000491 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
492 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
493 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000494#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000495 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000496#endif /* QUANT_2PASS_SUPPORTED */
497 } else {
498 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
499 /* Select new quantization method */
500 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
501 cinfo->cquantize = master->quantizer_2pass;
502 master->pub.is_dummy_pass = TRUE;
503 } else if (cinfo->enable_1pass_quant) {
504 cinfo->cquantize = master->quantizer_1pass;
505 } else {
506 ERREXIT(cinfo, JERR_MODE_CHANGE);
507 }
508 }
509 (*cinfo->idct->start_pass) (cinfo);
510 (*cinfo->coef->start_output_pass) (cinfo);
511 if (! cinfo->raw_data_out) {
512 if (! master->using_merged_upsample)
513 (*cinfo->cconvert->start_pass) (cinfo);
514 (*cinfo->upsample->start_pass) (cinfo);
515 if (cinfo->quantize_colors)
516 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
517 (*cinfo->post->start_pass) (cinfo,
518 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
519 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
520 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 }
522
523 /* Set up progress monitor's pass info if present */
524 if (cinfo->progress != NULL) {
525 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000526 cinfo->progress->total_passes = master->pass_number +
527 (master->pub.is_dummy_pass ? 2 : 1);
528 /* In buffered-image mode, we assume one more output pass if EOI not
529 * yet reached, but no more passes if EOI has been reached.
530 */
531 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
532 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
533 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000534 }
535}
536
537
538/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000539 * Finish up at end of an output pass.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000540 */
541
Thomas G. Lane489583f1996-02-07 00:00:00 +0000542METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000543finish_output_pass (j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000544{
545 my_master_ptr master = (my_master_ptr) cinfo->master;
546
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000547 if (cinfo->quantize_colors)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000548 (*cinfo->cquantize->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000549 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000550}
551
552
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000553#ifdef D_MULTISCAN_FILES_SUPPORTED
554
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000555/*
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000556 * Switch to a new external colormap between output passes.
557 */
558
Thomas G. Lane489583f1996-02-07 00:00:00 +0000559GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000560jpeg_new_colormap (j_decompress_ptr cinfo)
561{
562 my_master_ptr master = (my_master_ptr) cinfo->master;
563
564 /* Prevent application from calling me at wrong times */
565 if (cinfo->global_state != DSTATE_BUFIMAGE)
566 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
567
568 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
569 cinfo->colormap != NULL) {
570 /* Select 2-pass quantizer for external colormap use */
571 cinfo->cquantize = master->quantizer_2pass;
572 /* Notify quantizer of colormap change */
573 (*cinfo->cquantize->new_color_map) (cinfo);
574 master->pub.is_dummy_pass = FALSE; /* just in case */
575 } else
576 ERREXIT(cinfo, JERR_MODE_CHANGE);
577}
578
579#endif /* D_MULTISCAN_FILES_SUPPORTED */
580
581
582/*
583 * Initialize master decompression control and select active modules.
584 * This is performed at the start of jpeg_start_decompress.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000585 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000586
Thomas G. Lane489583f1996-02-07 00:00:00 +0000587GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000588jinit_master_decompress (j_decompress_ptr cinfo)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000589{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000590 my_master_ptr master;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000591
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000592 master = (my_master_ptr)
593 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
594 SIZEOF(my_decomp_master));
595 cinfo->master = (struct jpeg_decomp_master *) master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000596 master->pub.prepare_for_output_pass = prepare_for_output_pass;
597 master->pub.finish_output_pass = finish_output_pass;
598
599 master->pub.is_dummy_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000600
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000601 master_selection(cinfo);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000602}