winex11.drv: Release window data before calling sync_window_cursor().
[wine.git] / libs / jpeg / jdmaster.c
blob3070b7bb41f59a2242931e6b5557870d8d6ae0ec
1 /*
2 * jdmaster.c
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2002-2020 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * 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.
15 #define JPEG_INTERNALS
16 #include "jinclude.h"
17 #include "jpeglib.h"
20 /* Private state */
22 typedef struct {
23 struct jpeg_decomp_master pub; /* public fields */
25 int pass_number; /* # of passes completed */
27 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
29 /* Saved references to initialized quantizer modules,
30 * in case we need to switch modes.
32 struct jpeg_color_quantizer * quantizer_1pass;
33 struct jpeg_color_quantizer * quantizer_2pass;
34 } my_decomp_master;
36 typedef my_decomp_master * my_master_ptr;
40 * Determine whether merged upsample/color conversion should be used.
41 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
44 LOCAL(boolean)
45 use_merged_upsample (j_decompress_ptr cinfo)
47 #ifdef UPSAMPLE_MERGING_SUPPORTED
48 /* Merging is the equivalent of plain box-filter upsampling. */
49 /* The following condition is only needed if fancy shall select
50 * a different upsampling method. In our current implementation
51 * fancy only affects the DCT scaling, thus we can use fancy
52 * upsampling and merged upsample simultaneously, in particular
53 * with scaled DCT sizes larger than the default DCTSIZE.
55 #if 0
56 if (cinfo->do_fancy_upsampling)
57 return FALSE;
58 #endif
59 if (cinfo->CCIR601_sampling)
60 return FALSE;
61 /* jdmerge.c only supports YCC=>RGB color conversion */
62 if ((cinfo->jpeg_color_space != JCS_YCbCr &&
63 cinfo->jpeg_color_space != JCS_BG_YCC) ||
64 cinfo->num_components != 3 ||
65 cinfo->out_color_space != JCS_RGB ||
66 cinfo->out_color_components != RGB_PIXELSIZE ||
67 cinfo->color_transform)
68 return FALSE;
69 /* and it only handles 2h1v or 2h2v sampling ratios */
70 if (cinfo->comp_info[0].h_samp_factor != 2 ||
71 cinfo->comp_info[1].h_samp_factor != 1 ||
72 cinfo->comp_info[2].h_samp_factor != 1 ||
73 cinfo->comp_info[0].v_samp_factor > 2 ||
74 cinfo->comp_info[1].v_samp_factor != 1 ||
75 cinfo->comp_info[2].v_samp_factor != 1)
76 return FALSE;
77 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
78 if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
79 cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
80 cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
81 cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
82 cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
83 cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)
84 return FALSE;
85 /* ??? also need to test for upsample-time rescaling, when & if supported */
86 return TRUE; /* by golly, it'll work... */
87 #else
88 return FALSE;
89 #endif
94 * Compute output image dimensions and related values.
95 * NOTE: this is exported for possible use by application.
96 * Hence it mustn't do anything that can't be done twice.
97 * Also note that it may be called before the master module is initialized!
100 GLOBAL(void)
101 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
102 /* Do computations that are needed before master selection phase.
103 * This function is used for full decompression.
106 int ci, i;
107 jpeg_component_info *compptr;
109 /* Prevent application from calling me at wrong times */
110 if (cinfo->global_state != DSTATE_READY)
111 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
113 /* Compute core output image dimensions and DCT scaling choices. */
114 jpeg_core_output_dimensions(cinfo);
116 #ifdef IDCT_SCALING_SUPPORTED
118 /* In selecting the actual DCT scaling for each component, we try to
119 * scale up the chroma components via IDCT scaling rather than upsampling.
120 * This saves time if the upsampler gets to use 1:1 scaling.
121 * Note this code adapts subsampling ratios which are powers of 2.
123 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
124 ci++, compptr++) {
125 int ssize = 1;
126 if (! cinfo->raw_data_out)
127 while (cinfo->min_DCT_h_scaled_size * ssize <=
128 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
129 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) ==
130 0) {
131 ssize = ssize * 2;
133 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
134 ssize = 1;
135 if (! cinfo->raw_data_out)
136 while (cinfo->min_DCT_v_scaled_size * ssize <=
137 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
138 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) ==
139 0) {
140 ssize = ssize * 2;
142 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
144 /* We don't support IDCT ratios larger than 2. */
145 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
146 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
147 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
148 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
150 /* Recompute downsampled dimensions of components;
151 * application needs to know these if using raw downsampled data.
153 /* Size in samples, after IDCT scaling */
154 compptr->downsampled_width = (JDIMENSION)
155 jdiv_round_up((long) cinfo->image_width *
156 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
157 (long) (cinfo->max_h_samp_factor * cinfo->block_size));
158 compptr->downsampled_height = (JDIMENSION)
159 jdiv_round_up((long) cinfo->image_height *
160 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
161 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
164 #endif /* IDCT_SCALING_SUPPORTED */
166 /* Report number of components in selected colorspace. */
167 /* This should correspond to the actual code in the color conversion module. */
168 switch (cinfo->out_color_space) {
169 case JCS_GRAYSCALE:
170 cinfo->out_color_components = 1;
171 break;
172 case JCS_RGB:
173 case JCS_BG_RGB:
174 cinfo->out_color_components = RGB_PIXELSIZE;
175 break;
176 default: /* YCCK <=> CMYK conversion or same colorspace as in file */
177 i = 0;
178 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
179 ci++, compptr++)
180 if (compptr->component_needed)
181 i++; /* count output color components */
182 cinfo->out_color_components = i;
184 cinfo->output_components = (cinfo->quantize_colors ? 1 :
185 cinfo->out_color_components);
187 /* See if upsampler will want to emit more than one row at a time */
188 if (use_merged_upsample(cinfo))
189 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
190 else
191 cinfo->rec_outbuf_height = 1;
196 * Several decompression processes need to range-limit values to the range
197 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
198 * due to noise introduced by quantization, roundoff error, etc. These
199 * processes are inner loops and need to be as fast as possible. On most
200 * machines, particularly CPUs with pipelines or instruction prefetch,
201 * a (subscript-check-less) C table lookup
202 * x = sample_range_limit[x];
203 * is faster than explicit tests
204 * if (x < 0) x = 0;
205 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
206 * These processes all use a common table prepared by the routine below.
208 * For most steps we can mathematically guarantee that the initial value
209 * of x is within 2*(MAXJSAMPLE+1) of the legal range, so a table running
210 * from -2*(MAXJSAMPLE+1) to 3*MAXJSAMPLE+2 is sufficient. But for the
211 * initial limiting step (just after the IDCT), a wildly out-of-range value
212 * is possible if the input data is corrupt. To avoid any chance of indexing
213 * off the end of memory and getting a bad-pointer trap, we perform the
214 * post-IDCT limiting thus:
215 * x = (sample_range_limit - SUBSET)[(x + CENTER) & MASK];
216 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
217 * samples. Under normal circumstances this is more than enough range and
218 * a correct output will be generated; with bogus input data the mask will
219 * cause wraparound, and we will safely generate a bogus-but-in-range output.
220 * For the post-IDCT step, we want to convert the data from signed to unsigned
221 * representation by adding CENTERJSAMPLE at the same time that we limit it.
222 * This is accomplished with SUBSET = CENTER - CENTERJSAMPLE.
224 * Note that the table is allocated in near data space on PCs; it's small
225 * enough and used often enough to justify this.
228 LOCAL(void)
229 prepare_range_limit_table (j_decompress_ptr cinfo)
230 /* Allocate and fill in the sample_range_limit table */
232 JSAMPLE * table;
233 int i;
235 table = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
236 JPOOL_IMAGE, (RANGE_CENTER * 2 + MAXJSAMPLE + 1) * SIZEOF(JSAMPLE));
237 /* First segment of range limit table: limit[x] = 0 for x < 0 */
238 MEMZERO(table, RANGE_CENTER * SIZEOF(JSAMPLE));
239 table += RANGE_CENTER; /* allow negative subscripts of table */
240 cinfo->sample_range_limit = table;
241 /* Main part of range limit table: limit[x] = x */
242 for (i = 0; i <= MAXJSAMPLE; i++)
243 table[i] = (JSAMPLE) i;
244 /* End of range limit table: limit[x] = MAXJSAMPLE for x > MAXJSAMPLE */
245 for (; i <= MAXJSAMPLE + RANGE_CENTER; i++)
246 table[i] = MAXJSAMPLE;
251 * Master selection of decompression modules.
252 * This is done once at jpeg_start_decompress time. We determine
253 * which modules will be used and give them appropriate initialization calls.
254 * We also initialize the decompressor input side to begin consuming data.
256 * Since jpeg_read_header has finished, we know what is in the SOF
257 * and (first) SOS markers. We also have all the application parameter
258 * settings.
261 LOCAL(void)
262 master_selection (j_decompress_ptr cinfo)
264 my_master_ptr master = (my_master_ptr) cinfo->master;
265 boolean use_c_buffer;
266 long samplesperrow;
267 JDIMENSION jd_samplesperrow;
269 /* For now, precision must match compiled-in value... */
270 if (cinfo->data_precision != BITS_IN_JSAMPLE)
271 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
273 /* Initialize dimensions and other stuff */
274 jpeg_calc_output_dimensions(cinfo);
275 prepare_range_limit_table(cinfo);
277 /* Sanity check on image dimensions */
278 if (cinfo->output_height <= 0 || cinfo->output_width <= 0 ||
279 cinfo->out_color_components <= 0)
280 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
282 /* Width of an output scanline must be representable as JDIMENSION. */
283 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
284 jd_samplesperrow = (JDIMENSION) samplesperrow;
285 if ((long) jd_samplesperrow != samplesperrow)
286 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
288 /* Initialize my private state */
289 master->pass_number = 0;
290 master->using_merged_upsample = use_merged_upsample(cinfo);
292 /* Color quantizer selection */
293 master->quantizer_1pass = NULL;
294 master->quantizer_2pass = NULL;
295 /* No mode changes if not using buffered-image mode. */
296 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
297 cinfo->enable_1pass_quant = FALSE;
298 cinfo->enable_external_quant = FALSE;
299 cinfo->enable_2pass_quant = FALSE;
301 if (cinfo->quantize_colors) {
302 if (cinfo->raw_data_out)
303 ERREXIT(cinfo, JERR_NOTIMPL);
304 /* 2-pass quantizer only works in 3-component color space. */
305 if (cinfo->out_color_components != 3) {
306 cinfo->enable_1pass_quant = TRUE;
307 cinfo->enable_external_quant = FALSE;
308 cinfo->enable_2pass_quant = FALSE;
309 cinfo->colormap = NULL;
310 } else if (cinfo->colormap != NULL) {
311 cinfo->enable_external_quant = TRUE;
312 } else if (cinfo->two_pass_quantize) {
313 cinfo->enable_2pass_quant = TRUE;
314 } else {
315 cinfo->enable_1pass_quant = TRUE;
318 if (cinfo->enable_1pass_quant) {
319 #ifdef QUANT_1PASS_SUPPORTED
320 jinit_1pass_quantizer(cinfo);
321 master->quantizer_1pass = cinfo->cquantize;
322 #else
323 ERREXIT(cinfo, JERR_NOT_COMPILED);
324 #endif
327 /* We use the 2-pass code to map to external colormaps. */
328 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
329 #ifdef QUANT_2PASS_SUPPORTED
330 jinit_2pass_quantizer(cinfo);
331 master->quantizer_2pass = cinfo->cquantize;
332 #else
333 ERREXIT(cinfo, JERR_NOT_COMPILED);
334 #endif
336 /* If both quantizers are initialized, the 2-pass one is left active;
337 * this is necessary for starting with quantization to an external map.
341 /* Post-processing: in particular, color conversion first */
342 if (! cinfo->raw_data_out) {
343 if (master->using_merged_upsample) {
344 #ifdef UPSAMPLE_MERGING_SUPPORTED
345 jinit_merged_upsampler(cinfo); /* does color conversion too */
346 #else
347 ERREXIT(cinfo, JERR_NOT_COMPILED);
348 #endif
349 } else {
350 jinit_color_deconverter(cinfo);
351 jinit_upsampler(cinfo);
353 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
355 /* Inverse DCT */
356 jinit_inverse_dct(cinfo);
357 /* Entropy decoding: either Huffman or arithmetic coding. */
358 if (cinfo->arith_code)
359 jinit_arith_decoder(cinfo);
360 else {
361 jinit_huff_decoder(cinfo);
364 /* Initialize principal buffer controllers. */
365 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
366 jinit_d_coef_controller(cinfo, use_c_buffer);
368 if (! cinfo->raw_data_out)
369 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
371 /* We can now tell the memory manager to allocate virtual arrays. */
372 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
374 /* Initialize input side of decompressor to consume first scan. */
375 (*cinfo->inputctl->start_input_pass) (cinfo);
377 #ifdef D_MULTISCAN_FILES_SUPPORTED
378 /* If jpeg_start_decompress will read the whole file, initialize
379 * progress monitoring appropriately. The input step is counted
380 * as one pass.
382 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
383 cinfo->inputctl->has_multiple_scans) {
384 int nscans;
385 /* Estimate number of scans to set pass_limit. */
386 if (cinfo->progressive_mode) {
387 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
388 nscans = 2 + 3 * cinfo->num_components;
389 } else {
390 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
391 nscans = cinfo->num_components;
393 cinfo->progress->pass_counter = 0L;
394 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
395 cinfo->progress->completed_passes = 0;
396 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
397 /* Count the input pass as done */
398 master->pass_number++;
400 #endif /* D_MULTISCAN_FILES_SUPPORTED */
405 * Per-pass setup.
406 * This is called at the beginning of each output pass. We determine which
407 * modules will be active during this pass and give them appropriate
408 * start_pass calls. We also set is_dummy_pass to indicate whether this
409 * is a "real" output pass or a dummy pass for color quantization.
410 * (In the latter case, jdapistd.c will crank the pass to completion.)
413 METHODDEF(void)
414 prepare_for_output_pass (j_decompress_ptr cinfo)
416 my_master_ptr master = (my_master_ptr) cinfo->master;
418 if (master->pub.is_dummy_pass) {
419 #ifdef QUANT_2PASS_SUPPORTED
420 /* Final pass of 2-pass quantization */
421 master->pub.is_dummy_pass = FALSE;
422 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
423 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
424 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
425 #else
426 ERREXIT(cinfo, JERR_NOT_COMPILED);
427 #endif /* QUANT_2PASS_SUPPORTED */
428 } else {
429 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
430 /* Select new quantization method */
431 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
432 cinfo->cquantize = master->quantizer_2pass;
433 master->pub.is_dummy_pass = TRUE;
434 } else if (cinfo->enable_1pass_quant) {
435 cinfo->cquantize = master->quantizer_1pass;
436 } else {
437 ERREXIT(cinfo, JERR_MODE_CHANGE);
440 (*cinfo->idct->start_pass) (cinfo);
441 (*cinfo->coef->start_output_pass) (cinfo);
442 if (! cinfo->raw_data_out) {
443 if (! master->using_merged_upsample)
444 (*cinfo->cconvert->start_pass) (cinfo);
445 (*cinfo->upsample->start_pass) (cinfo);
446 if (cinfo->quantize_colors)
447 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
448 (*cinfo->post->start_pass) (cinfo,
449 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
450 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
454 /* Set up progress monitor's pass info if present */
455 if (cinfo->progress != NULL) {
456 cinfo->progress->completed_passes = master->pass_number;
457 cinfo->progress->total_passes = master->pass_number +
458 (master->pub.is_dummy_pass ? 2 : 1);
459 /* In buffered-image mode, we assume one more output pass if EOI not
460 * yet reached, but no more passes if EOI has been reached.
462 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
463 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
470 * Finish up at end of an output pass.
473 METHODDEF(void)
474 finish_output_pass (j_decompress_ptr cinfo)
476 my_master_ptr master = (my_master_ptr) cinfo->master;
478 if (cinfo->quantize_colors)
479 (*cinfo->cquantize->finish_pass) (cinfo);
480 master->pass_number++;
484 #ifdef D_MULTISCAN_FILES_SUPPORTED
487 * Switch to a new external colormap between output passes.
490 GLOBAL(void)
491 jpeg_new_colormap (j_decompress_ptr cinfo)
493 my_master_ptr master = (my_master_ptr) cinfo->master;
495 /* Prevent application from calling me at wrong times */
496 if (cinfo->global_state != DSTATE_BUFIMAGE)
497 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
499 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
500 cinfo->colormap != NULL) {
501 /* Select 2-pass quantizer for external colormap use */
502 cinfo->cquantize = master->quantizer_2pass;
503 /* Notify quantizer of colormap change */
504 (*cinfo->cquantize->new_color_map) (cinfo);
505 master->pub.is_dummy_pass = FALSE; /* just in case */
506 } else
507 ERREXIT(cinfo, JERR_MODE_CHANGE);
510 #endif /* D_MULTISCAN_FILES_SUPPORTED */
514 * Initialize master decompression control and select active modules.
515 * This is performed at the start of jpeg_start_decompress.
518 GLOBAL(void)
519 jinit_master_decompress (j_decompress_ptr cinfo)
521 my_master_ptr master;
523 master = (my_master_ptr) (*cinfo->mem->alloc_small)
524 ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_decomp_master));
525 cinfo->master = &master->pub;
526 master->pub.prepare_for_output_pass = prepare_for_output_pass;
527 master->pub.finish_output_pass = finish_output_pass;
529 master->pub.is_dummy_pass = FALSE;
531 master_selection(cinfo);