rev |
line source |
nuclear@14
|
1 /*
|
nuclear@14
|
2 * jdmaster.c
|
nuclear@14
|
3 *
|
nuclear@14
|
4 * Copyright (C) 1991-1997, Thomas G. Lane.
|
nuclear@14
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@14
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@14
|
7 *
|
nuclear@14
|
8 * This file contains master control logic for the JPEG decompressor.
|
nuclear@14
|
9 * These routines are concerned with selecting the modules to be executed
|
nuclear@14
|
10 * and with determining the number of passes and the work to be done in each
|
nuclear@14
|
11 * pass.
|
nuclear@14
|
12 */
|
nuclear@14
|
13
|
nuclear@14
|
14 #define JPEG_INTERNALS
|
nuclear@14
|
15 #include "jinclude.h"
|
nuclear@14
|
16 #include "jpeglib.h"
|
nuclear@14
|
17
|
nuclear@14
|
18
|
nuclear@14
|
19 /* Private state */
|
nuclear@14
|
20
|
nuclear@14
|
21 typedef struct {
|
nuclear@14
|
22 struct jpeg_decomp_master pub; /* public fields */
|
nuclear@14
|
23
|
nuclear@14
|
24 int pass_number; /* # of passes completed */
|
nuclear@14
|
25
|
nuclear@14
|
26 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
|
nuclear@14
|
27
|
nuclear@14
|
28 /* Saved references to initialized quantizer modules,
|
nuclear@14
|
29 * in case we need to switch modes.
|
nuclear@14
|
30 */
|
nuclear@14
|
31 struct jpeg_color_quantizer * quantizer_1pass;
|
nuclear@14
|
32 struct jpeg_color_quantizer * quantizer_2pass;
|
nuclear@14
|
33 } my_decomp_master;
|
nuclear@14
|
34
|
nuclear@14
|
35 typedef my_decomp_master * my_master_ptr;
|
nuclear@14
|
36
|
nuclear@14
|
37
|
nuclear@14
|
38 /*
|
nuclear@14
|
39 * Determine whether merged upsample/color conversion should be used.
|
nuclear@14
|
40 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
|
nuclear@14
|
41 */
|
nuclear@14
|
42
|
nuclear@14
|
43 LOCAL(boolean)
|
nuclear@14
|
44 use_merged_upsample (j_decompress_ptr cinfo)
|
nuclear@14
|
45 {
|
nuclear@14
|
46 #ifdef UPSAMPLE_MERGING_SUPPORTED
|
nuclear@14
|
47 /* Merging is the equivalent of plain box-filter upsampling */
|
nuclear@14
|
48 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
|
nuclear@14
|
49 return FALSE;
|
nuclear@14
|
50 /* jdmerge.c only supports YCC=>RGB color conversion */
|
nuclear@14
|
51 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
|
nuclear@14
|
52 cinfo->out_color_space != JCS_RGB ||
|
nuclear@14
|
53 cinfo->out_color_components != RGB_PIXELSIZE)
|
nuclear@14
|
54 return FALSE;
|
nuclear@14
|
55 /* and it only handles 2h1v or 2h2v sampling ratios */
|
nuclear@14
|
56 if (cinfo->comp_info[0].h_samp_factor != 2 ||
|
nuclear@14
|
57 cinfo->comp_info[1].h_samp_factor != 1 ||
|
nuclear@14
|
58 cinfo->comp_info[2].h_samp_factor != 1 ||
|
nuclear@14
|
59 cinfo->comp_info[0].v_samp_factor > 2 ||
|
nuclear@14
|
60 cinfo->comp_info[1].v_samp_factor != 1 ||
|
nuclear@14
|
61 cinfo->comp_info[2].v_samp_factor != 1)
|
nuclear@14
|
62 return FALSE;
|
nuclear@14
|
63 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
|
nuclear@14
|
64 if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
|
nuclear@14
|
65 cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
|
nuclear@14
|
66 cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
|
nuclear@14
|
67 return FALSE;
|
nuclear@14
|
68 /* ??? also need to test for upsample-time rescaling, when & if supported */
|
nuclear@14
|
69 return TRUE; /* by golly, it'll work... */
|
nuclear@14
|
70 #else
|
nuclear@14
|
71 return FALSE;
|
nuclear@14
|
72 #endif
|
nuclear@14
|
73 }
|
nuclear@14
|
74
|
nuclear@14
|
75
|
nuclear@14
|
76 /*
|
nuclear@14
|
77 * Compute output image dimensions and related values.
|
nuclear@14
|
78 * NOTE: this is exported for possible use by application.
|
nuclear@14
|
79 * Hence it mustn't do anything that can't be done twice.
|
nuclear@14
|
80 * Also note that it may be called before the master module is initialized!
|
nuclear@14
|
81 */
|
nuclear@14
|
82
|
nuclear@14
|
83 GLOBAL(void)
|
nuclear@14
|
84 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
|
nuclear@14
|
85 /* Do computations that are needed before master selection phase */
|
nuclear@14
|
86 {
|
nuclear@14
|
87 #ifdef IDCT_SCALING_SUPPORTED
|
nuclear@14
|
88 int ci;
|
nuclear@14
|
89 jpeg_component_info *compptr;
|
nuclear@14
|
90 #endif
|
nuclear@14
|
91
|
nuclear@14
|
92 /* Prevent application from calling me at wrong times */
|
nuclear@14
|
93 if (cinfo->global_state != DSTATE_READY)
|
nuclear@14
|
94 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
nuclear@14
|
95
|
nuclear@14
|
96 #ifdef IDCT_SCALING_SUPPORTED
|
nuclear@14
|
97
|
nuclear@14
|
98 /* Compute actual output image dimensions and DCT scaling choices. */
|
nuclear@14
|
99 if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
|
nuclear@14
|
100 /* Provide 1/8 scaling */
|
nuclear@14
|
101 cinfo->output_width = (JDIMENSION)
|
nuclear@14
|
102 jdiv_round_up((long) cinfo->image_width, 8L);
|
nuclear@14
|
103 cinfo->output_height = (JDIMENSION)
|
nuclear@14
|
104 jdiv_round_up((long) cinfo->image_height, 8L);
|
nuclear@14
|
105 cinfo->min_DCT_scaled_size = 1;
|
nuclear@14
|
106 } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
|
nuclear@14
|
107 /* Provide 1/4 scaling */
|
nuclear@14
|
108 cinfo->output_width = (JDIMENSION)
|
nuclear@14
|
109 jdiv_round_up((long) cinfo->image_width, 4L);
|
nuclear@14
|
110 cinfo->output_height = (JDIMENSION)
|
nuclear@14
|
111 jdiv_round_up((long) cinfo->image_height, 4L);
|
nuclear@14
|
112 cinfo->min_DCT_scaled_size = 2;
|
nuclear@14
|
113 } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
|
nuclear@14
|
114 /* Provide 1/2 scaling */
|
nuclear@14
|
115 cinfo->output_width = (JDIMENSION)
|
nuclear@14
|
116 jdiv_round_up((long) cinfo->image_width, 2L);
|
nuclear@14
|
117 cinfo->output_height = (JDIMENSION)
|
nuclear@14
|
118 jdiv_round_up((long) cinfo->image_height, 2L);
|
nuclear@14
|
119 cinfo->min_DCT_scaled_size = 4;
|
nuclear@14
|
120 } else {
|
nuclear@14
|
121 /* Provide 1/1 scaling */
|
nuclear@14
|
122 cinfo->output_width = cinfo->image_width;
|
nuclear@14
|
123 cinfo->output_height = cinfo->image_height;
|
nuclear@14
|
124 cinfo->min_DCT_scaled_size = DCTSIZE;
|
nuclear@14
|
125 }
|
nuclear@14
|
126 /* In selecting the actual DCT scaling for each component, we try to
|
nuclear@14
|
127 * scale up the chroma components via IDCT scaling rather than upsampling.
|
nuclear@14
|
128 * This saves time if the upsampler gets to use 1:1 scaling.
|
nuclear@14
|
129 * Note this code assumes that the supported DCT scalings are powers of 2.
|
nuclear@14
|
130 */
|
nuclear@14
|
131 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@14
|
132 ci++, compptr++) {
|
nuclear@14
|
133 int ssize = cinfo->min_DCT_scaled_size;
|
nuclear@14
|
134 while (ssize < DCTSIZE &&
|
nuclear@14
|
135 (compptr->h_samp_factor * ssize * 2 <=
|
nuclear@14
|
136 cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
|
nuclear@14
|
137 (compptr->v_samp_factor * ssize * 2 <=
|
nuclear@14
|
138 cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
|
nuclear@14
|
139 ssize = ssize * 2;
|
nuclear@14
|
140 }
|
nuclear@14
|
141 compptr->DCT_scaled_size = ssize;
|
nuclear@14
|
142 }
|
nuclear@14
|
143
|
nuclear@14
|
144 /* Recompute downsampled dimensions of components;
|
nuclear@14
|
145 * application needs to know these if using raw downsampled data.
|
nuclear@14
|
146 */
|
nuclear@14
|
147 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@14
|
148 ci++, compptr++) {
|
nuclear@14
|
149 /* Size in samples, after IDCT scaling */
|
nuclear@14
|
150 compptr->downsampled_width = (JDIMENSION)
|
nuclear@14
|
151 jdiv_round_up((long) cinfo->image_width *
|
nuclear@14
|
152 (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
|
nuclear@14
|
153 (long) (cinfo->max_h_samp_factor * DCTSIZE));
|
nuclear@14
|
154 compptr->downsampled_height = (JDIMENSION)
|
nuclear@14
|
155 jdiv_round_up((long) cinfo->image_height *
|
nuclear@14
|
156 (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
|
nuclear@14
|
157 (long) (cinfo->max_v_samp_factor * DCTSIZE));
|
nuclear@14
|
158 }
|
nuclear@14
|
159
|
nuclear@14
|
160 #else /* !IDCT_SCALING_SUPPORTED */
|
nuclear@14
|
161
|
nuclear@14
|
162 /* Hardwire it to "no scaling" */
|
nuclear@14
|
163 cinfo->output_width = cinfo->image_width;
|
nuclear@14
|
164 cinfo->output_height = cinfo->image_height;
|
nuclear@14
|
165 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
|
nuclear@14
|
166 * and has computed unscaled downsampled_width and downsampled_height.
|
nuclear@14
|
167 */
|
nuclear@14
|
168
|
nuclear@14
|
169 #endif /* IDCT_SCALING_SUPPORTED */
|
nuclear@14
|
170
|
nuclear@14
|
171 /* Report number of components in selected colorspace. */
|
nuclear@14
|
172 /* Probably this should be in the color conversion module... */
|
nuclear@14
|
173 switch (cinfo->out_color_space) {
|
nuclear@14
|
174 case JCS_GRAYSCALE:
|
nuclear@14
|
175 cinfo->out_color_components = 1;
|
nuclear@14
|
176 break;
|
nuclear@14
|
177 case JCS_RGB:
|
nuclear@14
|
178 #if RGB_PIXELSIZE != 3
|
nuclear@14
|
179 cinfo->out_color_components = RGB_PIXELSIZE;
|
nuclear@14
|
180 break;
|
nuclear@14
|
181 #endif /* else share code with YCbCr */
|
nuclear@14
|
182 case JCS_YCbCr:
|
nuclear@14
|
183 cinfo->out_color_components = 3;
|
nuclear@14
|
184 break;
|
nuclear@14
|
185 case JCS_CMYK:
|
nuclear@14
|
186 case JCS_YCCK:
|
nuclear@14
|
187 cinfo->out_color_components = 4;
|
nuclear@14
|
188 break;
|
nuclear@14
|
189 default: /* else must be same colorspace as in file */
|
nuclear@14
|
190 cinfo->out_color_components = cinfo->num_components;
|
nuclear@14
|
191 break;
|
nuclear@14
|
192 }
|
nuclear@14
|
193 cinfo->output_components = (cinfo->quantize_colors ? 1 :
|
nuclear@14
|
194 cinfo->out_color_components);
|
nuclear@14
|
195
|
nuclear@14
|
196 /* See if upsampler will want to emit more than one row at a time */
|
nuclear@14
|
197 if (use_merged_upsample(cinfo))
|
nuclear@14
|
198 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
|
nuclear@14
|
199 else
|
nuclear@14
|
200 cinfo->rec_outbuf_height = 1;
|
nuclear@14
|
201 }
|
nuclear@14
|
202
|
nuclear@14
|
203
|
nuclear@14
|
204 /*
|
nuclear@14
|
205 * Several decompression processes need to range-limit values to the range
|
nuclear@14
|
206 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
|
nuclear@14
|
207 * due to noise introduced by quantization, roundoff error, etc. These
|
nuclear@14
|
208 * processes are inner loops and need to be as fast as possible. On most
|
nuclear@14
|
209 * machines, particularly CPUs with pipelines or instruction prefetch,
|
nuclear@14
|
210 * a (subscript-check-less) C table lookup
|
nuclear@14
|
211 * x = sample_range_limit[x];
|
nuclear@14
|
212 * is faster than explicit tests
|
nuclear@14
|
213 * if (x < 0) x = 0;
|
nuclear@14
|
214 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
|
nuclear@14
|
215 * These processes all use a common table prepared by the routine below.
|
nuclear@14
|
216 *
|
nuclear@14
|
217 * For most steps we can mathematically guarantee that the initial value
|
nuclear@14
|
218 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
|
nuclear@14
|
219 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
|
nuclear@14
|
220 * limiting step (just after the IDCT), a wildly out-of-range value is
|
nuclear@14
|
221 * possible if the input data is corrupt. To avoid any chance of indexing
|
nuclear@14
|
222 * off the end of memory and getting a bad-pointer trap, we perform the
|
nuclear@14
|
223 * post-IDCT limiting thus:
|
nuclear@14
|
224 * x = range_limit[x & MASK];
|
nuclear@14
|
225 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
|
nuclear@14
|
226 * samples. Under normal circumstances this is more than enough range and
|
nuclear@14
|
227 * a correct output will be generated; with bogus input data the mask will
|
nuclear@14
|
228 * cause wraparound, and we will safely generate a bogus-but-in-range output.
|
nuclear@14
|
229 * For the post-IDCT step, we want to convert the data from signed to unsigned
|
nuclear@14
|
230 * representation by adding CENTERJSAMPLE at the same time that we limit it.
|
nuclear@14
|
231 * So the post-IDCT limiting table ends up looking like this:
|
nuclear@14
|
232 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
|
nuclear@14
|
233 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
|
nuclear@14
|
234 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
|
nuclear@14
|
235 * 0,1,...,CENTERJSAMPLE-1
|
nuclear@14
|
236 * Negative inputs select values from the upper half of the table after
|
nuclear@14
|
237 * masking.
|
nuclear@14
|
238 *
|
nuclear@14
|
239 * We can save some space by overlapping the start of the post-IDCT table
|
nuclear@14
|
240 * with the simpler range limiting table. The post-IDCT table begins at
|
nuclear@14
|
241 * sample_range_limit + CENTERJSAMPLE.
|
nuclear@14
|
242 *
|
nuclear@14
|
243 * Note that the table is allocated in near data space on PCs; it's small
|
nuclear@14
|
244 * enough and used often enough to justify this.
|
nuclear@14
|
245 */
|
nuclear@14
|
246
|
nuclear@14
|
247 LOCAL(void)
|
nuclear@14
|
248 prepare_range_limit_table (j_decompress_ptr cinfo)
|
nuclear@14
|
249 /* Allocate and fill in the sample_range_limit table */
|
nuclear@14
|
250 {
|
nuclear@14
|
251 JSAMPLE * table;
|
nuclear@14
|
252 int i;
|
nuclear@14
|
253
|
nuclear@14
|
254 table = (JSAMPLE *)
|
nuclear@14
|
255 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@14
|
256 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
|
nuclear@14
|
257 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
|
nuclear@14
|
258 cinfo->sample_range_limit = table;
|
nuclear@14
|
259 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
|
nuclear@14
|
260 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
|
nuclear@14
|
261 /* Main part of "simple" table: limit[x] = x */
|
nuclear@14
|
262 for (i = 0; i <= MAXJSAMPLE; i++)
|
nuclear@14
|
263 table[i] = (JSAMPLE) i;
|
nuclear@14
|
264 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
|
nuclear@14
|
265 /* End of simple table, rest of first half of post-IDCT table */
|
nuclear@14
|
266 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
|
nuclear@14
|
267 table[i] = MAXJSAMPLE;
|
nuclear@14
|
268 /* Second half of post-IDCT table */
|
nuclear@14
|
269 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
|
nuclear@14
|
270 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
|
nuclear@14
|
271 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
|
nuclear@14
|
272 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
|
nuclear@14
|
273 }
|
nuclear@14
|
274
|
nuclear@14
|
275
|
nuclear@14
|
276 /*
|
nuclear@14
|
277 * Master selection of decompression modules.
|
nuclear@14
|
278 * This is done once at jpeg_start_decompress time. We determine
|
nuclear@14
|
279 * which modules will be used and give them appropriate initialization calls.
|
nuclear@14
|
280 * We also initialize the decompressor input side to begin consuming data.
|
nuclear@14
|
281 *
|
nuclear@14
|
282 * Since jpeg_read_header has finished, we know what is in the SOF
|
nuclear@14
|
283 * and (first) SOS markers. We also have all the application parameter
|
nuclear@14
|
284 * settings.
|
nuclear@14
|
285 */
|
nuclear@14
|
286
|
nuclear@14
|
287 LOCAL(void)
|
nuclear@14
|
288 master_selection (j_decompress_ptr cinfo)
|
nuclear@14
|
289 {
|
nuclear@14
|
290 my_master_ptr master = (my_master_ptr) cinfo->master;
|
nuclear@14
|
291 boolean use_c_buffer;
|
nuclear@14
|
292 long samplesperrow;
|
nuclear@14
|
293 JDIMENSION jd_samplesperrow;
|
nuclear@14
|
294
|
nuclear@14
|
295 /* Initialize dimensions and other stuff */
|
nuclear@14
|
296 jpeg_calc_output_dimensions(cinfo);
|
nuclear@14
|
297 prepare_range_limit_table(cinfo);
|
nuclear@14
|
298
|
nuclear@14
|
299 /* Width of an output scanline must be representable as JDIMENSION. */
|
nuclear@14
|
300 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
|
nuclear@14
|
301 jd_samplesperrow = (JDIMENSION) samplesperrow;
|
nuclear@14
|
302 if ((long) jd_samplesperrow != samplesperrow)
|
nuclear@14
|
303 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
|
nuclear@14
|
304
|
nuclear@14
|
305 /* Initialize my private state */
|
nuclear@14
|
306 master->pass_number = 0;
|
nuclear@14
|
307 master->using_merged_upsample = use_merged_upsample(cinfo);
|
nuclear@14
|
308
|
nuclear@14
|
309 /* Color quantizer selection */
|
nuclear@14
|
310 master->quantizer_1pass = NULL;
|
nuclear@14
|
311 master->quantizer_2pass = NULL;
|
nuclear@14
|
312 /* No mode changes if not using buffered-image mode. */
|
nuclear@14
|
313 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
|
nuclear@14
|
314 cinfo->enable_1pass_quant = FALSE;
|
nuclear@14
|
315 cinfo->enable_external_quant = FALSE;
|
nuclear@14
|
316 cinfo->enable_2pass_quant = FALSE;
|
nuclear@14
|
317 }
|
nuclear@14
|
318 if (cinfo->quantize_colors) {
|
nuclear@14
|
319 if (cinfo->raw_data_out)
|
nuclear@14
|
320 ERREXIT(cinfo, JERR_NOTIMPL);
|
nuclear@14
|
321 /* 2-pass quantizer only works in 3-component color space. */
|
nuclear@14
|
322 if (cinfo->out_color_components != 3) {
|
nuclear@14
|
323 cinfo->enable_1pass_quant = TRUE;
|
nuclear@14
|
324 cinfo->enable_external_quant = FALSE;
|
nuclear@14
|
325 cinfo->enable_2pass_quant = FALSE;
|
nuclear@14
|
326 cinfo->colormap = NULL;
|
nuclear@14
|
327 } else if (cinfo->colormap != NULL) {
|
nuclear@14
|
328 cinfo->enable_external_quant = TRUE;
|
nuclear@14
|
329 } else if (cinfo->two_pass_quantize) {
|
nuclear@14
|
330 cinfo->enable_2pass_quant = TRUE;
|
nuclear@14
|
331 } else {
|
nuclear@14
|
332 cinfo->enable_1pass_quant = TRUE;
|
nuclear@14
|
333 }
|
nuclear@14
|
334
|
nuclear@14
|
335 if (cinfo->enable_1pass_quant) {
|
nuclear@14
|
336 #ifdef QUANT_1PASS_SUPPORTED
|
nuclear@14
|
337 jinit_1pass_quantizer(cinfo);
|
nuclear@14
|
338 master->quantizer_1pass = cinfo->cquantize;
|
nuclear@14
|
339 #else
|
nuclear@14
|
340 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@14
|
341 #endif
|
nuclear@14
|
342 }
|
nuclear@14
|
343
|
nuclear@14
|
344 /* We use the 2-pass code to map to external colormaps. */
|
nuclear@14
|
345 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
|
nuclear@14
|
346 #ifdef QUANT_2PASS_SUPPORTED
|
nuclear@14
|
347 jinit_2pass_quantizer(cinfo);
|
nuclear@14
|
348 master->quantizer_2pass = cinfo->cquantize;
|
nuclear@14
|
349 #else
|
nuclear@14
|
350 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@14
|
351 #endif
|
nuclear@14
|
352 }
|
nuclear@14
|
353 /* If both quantizers are initialized, the 2-pass one is left active;
|
nuclear@14
|
354 * this is necessary for starting with quantization to an external map.
|
nuclear@14
|
355 */
|
nuclear@14
|
356 }
|
nuclear@14
|
357
|
nuclear@14
|
358 /* Post-processing: in particular, color conversion first */
|
nuclear@14
|
359 if (! cinfo->raw_data_out) {
|
nuclear@14
|
360 if (master->using_merged_upsample) {
|
nuclear@14
|
361 #ifdef UPSAMPLE_MERGING_SUPPORTED
|
nuclear@14
|
362 jinit_merged_upsampler(cinfo); /* does color conversion too */
|
nuclear@14
|
363 #else
|
nuclear@14
|
364 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@14
|
365 #endif
|
nuclear@14
|
366 } else {
|
nuclear@14
|
367 jinit_color_deconverter(cinfo);
|
nuclear@14
|
368 jinit_upsampler(cinfo);
|
nuclear@14
|
369 }
|
nuclear@14
|
370 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
|
nuclear@14
|
371 }
|
nuclear@14
|
372 /* Inverse DCT */
|
nuclear@14
|
373 jinit_inverse_dct(cinfo);
|
nuclear@14
|
374 /* Entropy decoding: either Huffman or arithmetic coding. */
|
nuclear@14
|
375 if (cinfo->arith_code) {
|
nuclear@14
|
376 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
|
nuclear@14
|
377 } else {
|
nuclear@14
|
378 if (cinfo->progressive_mode) {
|
nuclear@14
|
379 #ifdef D_PROGRESSIVE_SUPPORTED
|
nuclear@14
|
380 jinit_phuff_decoder(cinfo);
|
nuclear@14
|
381 #else
|
nuclear@14
|
382 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@14
|
383 #endif
|
nuclear@14
|
384 } else
|
nuclear@14
|
385 jinit_huff_decoder(cinfo);
|
nuclear@14
|
386 }
|
nuclear@14
|
387
|
nuclear@14
|
388 /* Initialize principal buffer controllers. */
|
nuclear@14
|
389 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
|
nuclear@14
|
390 jinit_d_coef_controller(cinfo, use_c_buffer);
|
nuclear@14
|
391
|
nuclear@14
|
392 if (! cinfo->raw_data_out)
|
nuclear@14
|
393 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
|
nuclear@14
|
394
|
nuclear@14
|
395 /* We can now tell the memory manager to allocate virtual arrays. */
|
nuclear@14
|
396 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
|
nuclear@14
|
397
|
nuclear@14
|
398 /* Initialize input side of decompressor to consume first scan. */
|
nuclear@14
|
399 (*cinfo->inputctl->start_input_pass) (cinfo);
|
nuclear@14
|
400
|
nuclear@14
|
401 #ifdef D_MULTISCAN_FILES_SUPPORTED
|
nuclear@14
|
402 /* If jpeg_start_decompress will read the whole file, initialize
|
nuclear@14
|
403 * progress monitoring appropriately. The input step is counted
|
nuclear@14
|
404 * as one pass.
|
nuclear@14
|
405 */
|
nuclear@14
|
406 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
|
nuclear@14
|
407 cinfo->inputctl->has_multiple_scans) {
|
nuclear@14
|
408 int nscans;
|
nuclear@14
|
409 /* Estimate number of scans to set pass_limit. */
|
nuclear@14
|
410 if (cinfo->progressive_mode) {
|
nuclear@14
|
411 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
|
nuclear@14
|
412 nscans = 2 + 3 * cinfo->num_components;
|
nuclear@14
|
413 } else {
|
nuclear@14
|
414 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
|
nuclear@14
|
415 nscans = cinfo->num_components;
|
nuclear@14
|
416 }
|
nuclear@14
|
417 cinfo->progress->pass_counter = 0L;
|
nuclear@14
|
418 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
|
nuclear@14
|
419 cinfo->progress->completed_passes = 0;
|
nuclear@14
|
420 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
|
nuclear@14
|
421 /* Count the input pass as done */
|
nuclear@14
|
422 master->pass_number++;
|
nuclear@14
|
423 }
|
nuclear@14
|
424 #endif /* D_MULTISCAN_FILES_SUPPORTED */
|
nuclear@14
|
425 }
|
nuclear@14
|
426
|
nuclear@14
|
427
|
nuclear@14
|
428 /*
|
nuclear@14
|
429 * Per-pass setup.
|
nuclear@14
|
430 * This is called at the beginning of each output pass. We determine which
|
nuclear@14
|
431 * modules will be active during this pass and give them appropriate
|
nuclear@14
|
432 * start_pass calls. We also set is_dummy_pass to indicate whether this
|
nuclear@14
|
433 * is a "real" output pass or a dummy pass for color quantization.
|
nuclear@14
|
434 * (In the latter case, jdapistd.c will crank the pass to completion.)
|
nuclear@14
|
435 */
|
nuclear@14
|
436
|
nuclear@14
|
437 METHODDEF(void)
|
nuclear@14
|
438 prepare_for_output_pass (j_decompress_ptr cinfo)
|
nuclear@14
|
439 {
|
nuclear@14
|
440 my_master_ptr master = (my_master_ptr) cinfo->master;
|
nuclear@14
|
441
|
nuclear@14
|
442 if (master->pub.is_dummy_pass) {
|
nuclear@14
|
443 #ifdef QUANT_2PASS_SUPPORTED
|
nuclear@14
|
444 /* Final pass of 2-pass quantization */
|
nuclear@14
|
445 master->pub.is_dummy_pass = FALSE;
|
nuclear@14
|
446 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
|
nuclear@14
|
447 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
|
nuclear@14
|
448 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
|
nuclear@14
|
449 #else
|
nuclear@14
|
450 ERREXIT(cinfo, JERR_NOT_COMPILED);
|
nuclear@14
|
451 #endif /* QUANT_2PASS_SUPPORTED */
|
nuclear@14
|
452 } else {
|
nuclear@14
|
453 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
|
nuclear@14
|
454 /* Select new quantization method */
|
nuclear@14
|
455 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
|
nuclear@14
|
456 cinfo->cquantize = master->quantizer_2pass;
|
nuclear@14
|
457 master->pub.is_dummy_pass = TRUE;
|
nuclear@14
|
458 } else if (cinfo->enable_1pass_quant) {
|
nuclear@14
|
459 cinfo->cquantize = master->quantizer_1pass;
|
nuclear@14
|
460 } else {
|
nuclear@14
|
461 ERREXIT(cinfo, JERR_MODE_CHANGE);
|
nuclear@14
|
462 }
|
nuclear@14
|
463 }
|
nuclear@14
|
464 (*cinfo->idct->start_pass) (cinfo);
|
nuclear@14
|
465 (*cinfo->coef->start_output_pass) (cinfo);
|
nuclear@14
|
466 if (! cinfo->raw_data_out) {
|
nuclear@14
|
467 if (! master->using_merged_upsample)
|
nuclear@14
|
468 (*cinfo->cconvert->start_pass) (cinfo);
|
nuclear@14
|
469 (*cinfo->upsample->start_pass) (cinfo);
|
nuclear@14
|
470 if (cinfo->quantize_colors)
|
nuclear@14
|
471 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
|
nuclear@14
|
472 (*cinfo->post->start_pass) (cinfo,
|
nuclear@14
|
473 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
|
nuclear@14
|
474 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
|
nuclear@14
|
475 }
|
nuclear@14
|
476 }
|
nuclear@14
|
477
|
nuclear@14
|
478 /* Set up progress monitor's pass info if present */
|
nuclear@14
|
479 if (cinfo->progress != NULL) {
|
nuclear@14
|
480 cinfo->progress->completed_passes = master->pass_number;
|
nuclear@14
|
481 cinfo->progress->total_passes = master->pass_number +
|
nuclear@14
|
482 (master->pub.is_dummy_pass ? 2 : 1);
|
nuclear@14
|
483 /* In buffered-image mode, we assume one more output pass if EOI not
|
nuclear@14
|
484 * yet reached, but no more passes if EOI has been reached.
|
nuclear@14
|
485 */
|
nuclear@14
|
486 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
|
nuclear@14
|
487 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
|
nuclear@14
|
488 }
|
nuclear@14
|
489 }
|
nuclear@14
|
490 }
|
nuclear@14
|
491
|
nuclear@14
|
492
|
nuclear@14
|
493 /*
|
nuclear@14
|
494 * Finish up at end of an output pass.
|
nuclear@14
|
495 */
|
nuclear@14
|
496
|
nuclear@14
|
497 METHODDEF(void)
|
nuclear@14
|
498 finish_output_pass (j_decompress_ptr cinfo)
|
nuclear@14
|
499 {
|
nuclear@14
|
500 my_master_ptr master = (my_master_ptr) cinfo->master;
|
nuclear@14
|
501
|
nuclear@14
|
502 if (cinfo->quantize_colors)
|
nuclear@14
|
503 (*cinfo->cquantize->finish_pass) (cinfo);
|
nuclear@14
|
504 master->pass_number++;
|
nuclear@14
|
505 }
|
nuclear@14
|
506
|
nuclear@14
|
507
|
nuclear@14
|
508 #ifdef D_MULTISCAN_FILES_SUPPORTED
|
nuclear@14
|
509
|
nuclear@14
|
510 /*
|
nuclear@14
|
511 * Switch to a new external colormap between output passes.
|
nuclear@14
|
512 */
|
nuclear@14
|
513
|
nuclear@14
|
514 GLOBAL(void)
|
nuclear@14
|
515 jpeg_new_colormap (j_decompress_ptr cinfo)
|
nuclear@14
|
516 {
|
nuclear@14
|
517 my_master_ptr master = (my_master_ptr) cinfo->master;
|
nuclear@14
|
518
|
nuclear@14
|
519 /* Prevent application from calling me at wrong times */
|
nuclear@14
|
520 if (cinfo->global_state != DSTATE_BUFIMAGE)
|
nuclear@14
|
521 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
nuclear@14
|
522
|
nuclear@14
|
523 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
|
nuclear@14
|
524 cinfo->colormap != NULL) {
|
nuclear@14
|
525 /* Select 2-pass quantizer for external colormap use */
|
nuclear@14
|
526 cinfo->cquantize = master->quantizer_2pass;
|
nuclear@14
|
527 /* Notify quantizer of colormap change */
|
nuclear@14
|
528 (*cinfo->cquantize->new_color_map) (cinfo);
|
nuclear@14
|
529 master->pub.is_dummy_pass = FALSE; /* just in case */
|
nuclear@14
|
530 } else
|
nuclear@14
|
531 ERREXIT(cinfo, JERR_MODE_CHANGE);
|
nuclear@14
|
532 }
|
nuclear@14
|
533
|
nuclear@14
|
534 #endif /* D_MULTISCAN_FILES_SUPPORTED */
|
nuclear@14
|
535
|
nuclear@14
|
536
|
nuclear@14
|
537 /*
|
nuclear@14
|
538 * Initialize master decompression control and select active modules.
|
nuclear@14
|
539 * This is performed at the start of jpeg_start_decompress.
|
nuclear@14
|
540 */
|
nuclear@14
|
541
|
nuclear@14
|
542 GLOBAL(void)
|
nuclear@14
|
543 jinit_master_decompress (j_decompress_ptr cinfo)
|
nuclear@14
|
544 {
|
nuclear@14
|
545 my_master_ptr master;
|
nuclear@14
|
546
|
nuclear@14
|
547 master = (my_master_ptr)
|
nuclear@14
|
548 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@14
|
549 SIZEOF(my_decomp_master));
|
nuclear@14
|
550 cinfo->master = (struct jpeg_decomp_master *) master;
|
nuclear@14
|
551 master->pub.prepare_for_output_pass = prepare_for_output_pass;
|
nuclear@14
|
552 master->pub.finish_output_pass = finish_output_pass;
|
nuclear@14
|
553
|
nuclear@14
|
554 master->pub.is_dummy_pass = FALSE;
|
nuclear@14
|
555
|
nuclear@14
|
556 master_selection(cinfo);
|
nuclear@14
|
557 }
|