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