1991-10-06 20:00:00 -04:00
|
|
|
/*
|
|
|
|
* jdmaster.c
|
|
|
|
*
|
1998-03-26 19:00:00 -05:00
|
|
|
* Copyright (C) 1991-1997, Thomas G. Lane.
|
2020-04-03 18:24:42 -04:00
|
|
|
* Modified 2002-2019 by Guido Vollbeding.
|
1991-10-06 20:00:00 -04:00
|
|
|
* This file is part of the Independent JPEG Group's software.
|
|
|
|
* For conditions of distribution and use, see the accompanying README file.
|
|
|
|
*
|
1994-09-23 20:00:00 -04:00
|
|
|
* This file contains master control logic for the JPEG decompressor.
|
|
|
|
* These routines are concerned with selecting the modules to be executed
|
|
|
|
* and with determining the number of passes and the work to be done in each
|
|
|
|
* pass.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
#define JPEG_INTERNALS
|
1991-10-06 20:00:00 -04:00
|
|
|
#include "jinclude.h"
|
1994-09-23 20:00:00 -04:00
|
|
|
#include "jpeglib.h"
|
1991-10-06 20:00:00 -04:00
|
|
|
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Private state */
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
typedef struct {
|
|
|
|
struct jpeg_decomp_master pub; /* public fields */
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
int pass_number; /* # of passes completed */
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
|
|
|
|
|
|
|
|
/* Saved references to initialized quantizer modules,
|
|
|
|
* in case we need to switch modes.
|
|
|
|
*/
|
|
|
|
struct jpeg_color_quantizer * quantizer_1pass;
|
|
|
|
struct jpeg_color_quantizer * quantizer_2pass;
|
1994-09-23 20:00:00 -04:00
|
|
|
} my_decomp_master;
|
1993-02-17 19:00:00 -05:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
typedef my_decomp_master * my_master_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine whether merged upsample/color conversion should be used.
|
|
|
|
* CRUCIAL: this must match the actual capabilities of jdmerge.c!
|
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
LOCAL(boolean)
|
1994-09-23 20:00:00 -04:00
|
|
|
use_merged_upsample (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
#ifdef UPSAMPLE_MERGING_SUPPORTED
|
2016-01-16 19:00:00 -05:00
|
|
|
/* Merging is the equivalent of plain box-filter upsampling. */
|
|
|
|
/* The following condition is only needed if fancy shall select
|
|
|
|
* a different upsampling method. In our current implementation
|
|
|
|
* fancy only affects the DCT scaling, thus we can use fancy
|
|
|
|
* upsampling and merged upsample simultaneously, in particular
|
|
|
|
* with scaled DCT sizes larger than the default DCTSIZE.
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
if (cinfo->do_fancy_upsampling)
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
if (cinfo->CCIR601_sampling)
|
1994-09-23 20:00:00 -04:00
|
|
|
return FALSE;
|
|
|
|
/* jdmerge.c only supports YCC=>RGB color conversion */
|
2016-01-16 19:00:00 -05:00
|
|
|
if ((cinfo->jpeg_color_space != JCS_YCbCr &&
|
|
|
|
cinfo->jpeg_color_space != JCS_BG_YCC) ||
|
|
|
|
cinfo->num_components != 3 ||
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->out_color_space != JCS_RGB ||
|
2014-01-18 19:00:00 -05:00
|
|
|
cinfo->out_color_components != RGB_PIXELSIZE ||
|
|
|
|
cinfo->color_transform)
|
1994-09-23 20:00:00 -04:00
|
|
|
return FALSE;
|
|
|
|
/* and it only handles 2h1v or 2h2v sampling ratios */
|
|
|
|
if (cinfo->comp_info[0].h_samp_factor != 2 ||
|
|
|
|
cinfo->comp_info[1].h_samp_factor != 1 ||
|
|
|
|
cinfo->comp_info[2].h_samp_factor != 1 ||
|
|
|
|
cinfo->comp_info[0].v_samp_factor > 2 ||
|
|
|
|
cinfo->comp_info[1].v_samp_factor != 1 ||
|
|
|
|
cinfo->comp_info[2].v_samp_factor != 1)
|
|
|
|
return FALSE;
|
|
|
|
/* furthermore, it doesn't work if we've scaled the IDCTs differently */
|
2009-06-26 20:00:00 -04:00
|
|
|
if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
|
|
|
|
cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
|
|
|
|
cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
|
|
|
|
cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
|
|
|
|
cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
|
|
|
|
cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)
|
1994-09-23 20:00:00 -04:00
|
|
|
return FALSE;
|
|
|
|
/* ??? also need to test for upsample-time rescaling, when & if supported */
|
1995-08-01 20:00:00 -04:00
|
|
|
return TRUE; /* by golly, it'll work... */
|
1993-02-17 19:00:00 -05:00
|
|
|
#else
|
1994-09-23 20:00:00 -04:00
|
|
|
return FALSE;
|
1993-02-17 19:00:00 -05:00
|
|
|
#endif
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/*
|
1995-08-01 20:00:00 -04:00
|
|
|
* Compute output image dimensions and related values.
|
|
|
|
* NOTE: this is exported for possible use by application.
|
1994-09-23 20:00:00 -04:00
|
|
|
* Hence it mustn't do anything that can't be done twice.
|
1995-08-01 20:00:00 -04:00
|
|
|
* Also note that it may be called before the master module is initialized!
|
1994-09-23 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
GLOBAL(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
|
2010-01-09 19:00:00 -05:00
|
|
|
/* Do computations that are needed before master selection phase.
|
|
|
|
* This function is used for full decompression.
|
|
|
|
*/
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1998-03-26 19:00:00 -05:00
|
|
|
#ifdef IDCT_SCALING_SUPPORTED
|
2020-04-03 18:24:42 -04:00
|
|
|
int ci, ssize;
|
1991-10-06 20:00:00 -04:00
|
|
|
jpeg_component_info *compptr;
|
1998-03-26 19:00:00 -05:00
|
|
|
#endif
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
/* Prevent application from calling me at wrong times */
|
|
|
|
if (cinfo->global_state != DSTATE_READY)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
1994-09-23 20:00:00 -04:00
|
|
|
|
2010-01-09 19:00:00 -05:00
|
|
|
/* Compute core output image dimensions and DCT scaling choices. */
|
|
|
|
jpeg_core_output_dimensions(cinfo);
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
#ifdef IDCT_SCALING_SUPPORTED
|
1995-08-01 20:00:00 -04:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* In selecting the actual DCT scaling for each component, we try to
|
|
|
|
* scale up the chroma components via IDCT scaling rather than upsampling.
|
|
|
|
* This saves time if the upsampler gets to use 1:1 scaling.
|
2009-06-26 20:00:00 -04:00
|
|
|
* Note this code adapts subsampling ratios which are powers of 2.
|
1994-09-23 20:00:00 -04:00
|
|
|
*/
|
|
|
|
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
|
|
|
ci++, compptr++) {
|
2020-04-03 18:24:42 -04:00
|
|
|
ssize = 1;
|
|
|
|
if (! cinfo->raw_data_out)
|
|
|
|
while (cinfo->min_DCT_h_scaled_size * ssize <=
|
|
|
|
(cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
|
|
|
|
(cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) ==
|
|
|
|
0) {
|
|
|
|
ssize = ssize * 2;
|
|
|
|
}
|
2009-06-26 20:00:00 -04:00
|
|
|
compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
|
|
|
|
ssize = 1;
|
2020-04-03 18:24:42 -04:00
|
|
|
if (! cinfo->raw_data_out)
|
|
|
|
while (cinfo->min_DCT_v_scaled_size * ssize <=
|
|
|
|
(cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
|
|
|
|
(cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) ==
|
|
|
|
0) {
|
|
|
|
ssize = ssize * 2;
|
|
|
|
}
|
2009-06-26 20:00:00 -04:00
|
|
|
compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
|
|
|
|
|
|
|
|
/* We don't support IDCT ratios larger than 2. */
|
|
|
|
if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
|
|
|
|
compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
|
|
|
|
else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
|
|
|
|
compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
|
1995-08-01 20:00:00 -04:00
|
|
|
|
2020-04-03 18:24:42 -04:00
|
|
|
/* Recompute downsampled dimensions of components;
|
|
|
|
* application needs to know these if using raw downsampled data.
|
|
|
|
*/
|
1995-08-01 20:00:00 -04:00
|
|
|
/* Size in samples, after IDCT scaling */
|
|
|
|
compptr->downsampled_width = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_width *
|
2009-06-26 20:00:00 -04:00
|
|
|
(long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
|
2010-01-09 19:00:00 -05:00
|
|
|
(long) (cinfo->max_h_samp_factor * cinfo->block_size));
|
1995-08-01 20:00:00 -04:00
|
|
|
compptr->downsampled_height = (JDIMENSION)
|
|
|
|
jdiv_round_up((long) cinfo->image_height *
|
2009-06-26 20:00:00 -04:00
|
|
|
(long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
|
2010-01-09 19:00:00 -05:00
|
|
|
(long) (cinfo->max_v_samp_factor * cinfo->block_size));
|
1995-08-01 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
#endif /* IDCT_SCALING_SUPPORTED */
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Report number of components in selected colorspace. */
|
|
|
|
/* Probably this should be in the color conversion module... */
|
|
|
|
switch (cinfo->out_color_space) {
|
|
|
|
case JCS_GRAYSCALE:
|
|
|
|
cinfo->out_color_components = 1;
|
|
|
|
break;
|
|
|
|
case JCS_RGB:
|
2014-01-18 19:00:00 -05:00
|
|
|
case JCS_BG_RGB:
|
2020-04-03 18:24:42 -04:00
|
|
|
#if RGB_PIXELSIZE != 3
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->out_color_components = RGB_PIXELSIZE;
|
|
|
|
break;
|
2020-04-03 18:24:42 -04:00
|
|
|
#endif /* else share code with YCbCr */
|
1994-09-23 20:00:00 -04:00
|
|
|
case JCS_YCbCr:
|
2014-01-18 19:00:00 -05:00
|
|
|
case JCS_BG_YCC:
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->out_color_components = 3;
|
|
|
|
break;
|
|
|
|
case JCS_CMYK:
|
|
|
|
case JCS_YCCK:
|
|
|
|
cinfo->out_color_components = 4;
|
|
|
|
break;
|
|
|
|
default: /* else must be same colorspace as in file */
|
|
|
|
cinfo->out_color_components = cinfo->num_components;
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->output_components = (cinfo->quantize_colors ? 1 :
|
|
|
|
cinfo->out_color_components);
|
|
|
|
|
|
|
|
/* See if upsampler will want to emit more than one row at a time */
|
|
|
|
if (use_merged_upsample(cinfo))
|
|
|
|
cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
|
|
|
|
else
|
|
|
|
cinfo->rec_outbuf_height = 1;
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1994-09-23 20:00:00 -04:00
|
|
|
* Several decompression processes need to range-limit values to the range
|
|
|
|
* 0..MAXJSAMPLE; the input value may fall somewhat outside this range
|
|
|
|
* due to noise introduced by quantization, roundoff error, etc. These
|
|
|
|
* processes are inner loops and need to be as fast as possible. On most
|
|
|
|
* machines, particularly CPUs with pipelines or instruction prefetch,
|
|
|
|
* a (subscript-check-less) C table lookup
|
|
|
|
* x = sample_range_limit[x];
|
|
|
|
* is faster than explicit tests
|
|
|
|
* if (x < 0) x = 0;
|
|
|
|
* else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
|
|
|
|
* These processes all use a common table prepared by the routine below.
|
|
|
|
*
|
|
|
|
* For most steps we can mathematically guarantee that the initial value
|
2016-01-16 19:00:00 -05:00
|
|
|
* of x is within 2*(MAXJSAMPLE+1) of the legal range, so a table running
|
|
|
|
* from -2*(MAXJSAMPLE+1) to 3*MAXJSAMPLE+2 is sufficient. But for the
|
|
|
|
* initial limiting step (just after the IDCT), a wildly out-of-range value
|
|
|
|
* is possible if the input data is corrupt. To avoid any chance of indexing
|
1994-09-23 20:00:00 -04:00
|
|
|
* off the end of memory and getting a bad-pointer trap, we perform the
|
|
|
|
* post-IDCT limiting thus:
|
2016-01-16 19:00:00 -05:00
|
|
|
* x = (sample_range_limit - SUBSET)[(x + CENTER) & MASK];
|
1994-09-23 20:00:00 -04:00
|
|
|
* where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
|
|
|
|
* samples. Under normal circumstances this is more than enough range and
|
|
|
|
* a correct output will be generated; with bogus input data the mask will
|
|
|
|
* cause wraparound, and we will safely generate a bogus-but-in-range output.
|
|
|
|
* For the post-IDCT step, we want to convert the data from signed to unsigned
|
|
|
|
* representation by adding CENTERJSAMPLE at the same time that we limit it.
|
2016-01-16 19:00:00 -05:00
|
|
|
* This is accomplished with SUBSET = CENTER - CENTERJSAMPLE.
|
1994-09-23 20:00:00 -04:00
|
|
|
*
|
|
|
|
* Note that the table is allocated in near data space on PCs; it's small
|
|
|
|
* enough and used often enough to justify this.
|
1991-10-06 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
LOCAL(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
prepare_range_limit_table (j_decompress_ptr cinfo)
|
|
|
|
/* Allocate and fill in the sample_range_limit table */
|
|
|
|
{
|
|
|
|
JSAMPLE * table;
|
|
|
|
int i;
|
1991-10-06 20:00:00 -04:00
|
|
|
|
2020-04-03 18:23:44 -04:00
|
|
|
table = (JSAMPLE *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
|
|
|
|
JPOOL_IMAGE, (RANGE_CENTER * 2 + MAXJSAMPLE + 1) * SIZEOF(JSAMPLE));
|
2016-01-16 19:00:00 -05:00
|
|
|
/* First segment of range limit table: limit[x] = 0 for x < 0 */
|
2020-04-03 18:23:44 -04:00
|
|
|
MEMZERO(table, RANGE_CENTER * SIZEOF(JSAMPLE));
|
|
|
|
table += RANGE_CENTER; /* allow negative subscripts of table */
|
1994-09-23 20:00:00 -04:00
|
|
|
cinfo->sample_range_limit = table;
|
2016-01-16 19:00:00 -05:00
|
|
|
/* Main part of range limit table: limit[x] = x */
|
1994-09-23 20:00:00 -04:00
|
|
|
for (i = 0; i <= MAXJSAMPLE; i++)
|
|
|
|
table[i] = (JSAMPLE) i;
|
2016-01-16 19:00:00 -05:00
|
|
|
/* End of range limit table: limit[x] = MAXJSAMPLE for x > MAXJSAMPLE */
|
2020-04-03 18:23:44 -04:00
|
|
|
for (; i <= MAXJSAMPLE + RANGE_CENTER; i++)
|
1994-09-23 20:00:00 -04:00
|
|
|
table[i] = MAXJSAMPLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Master selection of decompression modules.
|
1995-08-01 20:00:00 -04:00
|
|
|
* This is done once at jpeg_start_decompress time. We determine
|
1994-09-23 20:00:00 -04:00
|
|
|
* which modules will be used and give them appropriate initialization calls.
|
1995-08-01 20:00:00 -04:00
|
|
|
* We also initialize the decompressor input side to begin consuming data.
|
1994-09-23 20:00:00 -04:00
|
|
|
*
|
1995-08-01 20:00:00 -04:00
|
|
|
* Since jpeg_read_header has finished, we know what is in the SOF
|
|
|
|
* and (first) SOS markers. We also have all the application parameter
|
|
|
|
* settings.
|
1994-09-23 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
LOCAL(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
master_selection (j_decompress_ptr cinfo)
|
1991-10-06 20:00:00 -04:00
|
|
|
{
|
1994-09-23 20:00:00 -04:00
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
1995-08-01 20:00:00 -04:00
|
|
|
boolean use_c_buffer;
|
1994-09-23 20:00:00 -04:00
|
|
|
long samplesperrow;
|
|
|
|
JDIMENSION jd_samplesperrow;
|
|
|
|
|
2014-01-18 19:00:00 -05:00
|
|
|
/* For now, precision must match compiled-in value... */
|
|
|
|
if (cinfo->data_precision != BITS_IN_JSAMPLE)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Initialize dimensions and other stuff */
|
|
|
|
jpeg_calc_output_dimensions(cinfo);
|
|
|
|
prepare_range_limit_table(cinfo);
|
|
|
|
|
2014-01-18 19:00:00 -05:00
|
|
|
/* Sanity check on image dimensions */
|
|
|
|
if (cinfo->output_height <= 0 || cinfo->output_width <= 0 ||
|
|
|
|
cinfo->out_color_components <= 0)
|
|
|
|
ERREXIT(cinfo, JERR_EMPTY_IMAGE);
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Width of an output scanline must be representable as JDIMENSION. */
|
|
|
|
samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
|
|
|
|
jd_samplesperrow = (JDIMENSION) samplesperrow;
|
|
|
|
if ((long) jd_samplesperrow != samplesperrow)
|
|
|
|
ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
|
|
|
|
|
|
|
|
/* Initialize my private state */
|
|
|
|
master->pass_number = 0;
|
|
|
|
master->using_merged_upsample = use_merged_upsample(cinfo);
|
|
|
|
|
|
|
|
/* Color quantizer selection */
|
1995-08-01 20:00:00 -04:00
|
|
|
master->quantizer_1pass = NULL;
|
|
|
|
master->quantizer_2pass = NULL;
|
|
|
|
/* No mode changes if not using buffered-image mode. */
|
|
|
|
if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
|
|
|
|
cinfo->enable_1pass_quant = FALSE;
|
|
|
|
cinfo->enable_external_quant = FALSE;
|
|
|
|
cinfo->enable_2pass_quant = FALSE;
|
|
|
|
}
|
1994-09-23 20:00:00 -04:00
|
|
|
if (cinfo->quantize_colors) {
|
|
|
|
if (cinfo->raw_data_out)
|
|
|
|
ERREXIT(cinfo, JERR_NOTIMPL);
|
1995-08-01 20:00:00 -04:00
|
|
|
/* 2-pass quantizer only works in 3-component color space. */
|
|
|
|
if (cinfo->out_color_components != 3) {
|
|
|
|
cinfo->enable_1pass_quant = TRUE;
|
|
|
|
cinfo->enable_external_quant = FALSE;
|
|
|
|
cinfo->enable_2pass_quant = FALSE;
|
|
|
|
cinfo->colormap = NULL;
|
|
|
|
} else if (cinfo->colormap != NULL) {
|
|
|
|
cinfo->enable_external_quant = TRUE;
|
|
|
|
} else if (cinfo->two_pass_quantize) {
|
|
|
|
cinfo->enable_2pass_quant = TRUE;
|
|
|
|
} else {
|
|
|
|
cinfo->enable_1pass_quant = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cinfo->enable_1pass_quant) {
|
|
|
|
#ifdef QUANT_1PASS_SUPPORTED
|
|
|
|
jinit_1pass_quantizer(cinfo);
|
|
|
|
master->quantizer_1pass = cinfo->cquantize;
|
1994-09-23 20:00:00 -04:00
|
|
|
#else
|
1995-08-01 20:00:00 -04:00
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
1994-09-23 20:00:00 -04:00
|
|
|
#endif
|
1995-08-01 20:00:00 -04:00
|
|
|
}
|
1994-09-23 20:00:00 -04:00
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
/* We use the 2-pass code to map to external colormaps. */
|
|
|
|
if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
|
1994-09-23 20:00:00 -04:00
|
|
|
#ifdef QUANT_2PASS_SUPPORTED
|
|
|
|
jinit_2pass_quantizer(cinfo);
|
1995-08-01 20:00:00 -04:00
|
|
|
master->quantizer_2pass = cinfo->cquantize;
|
1994-09-23 20:00:00 -04:00
|
|
|
#else
|
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
|
|
#endif
|
|
|
|
}
|
1995-08-01 20:00:00 -04:00
|
|
|
/* If both quantizers are initialized, the 2-pass one is left active;
|
|
|
|
* this is necessary for starting with quantization to an external map.
|
|
|
|
*/
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Post-processing: in particular, color conversion first */
|
|
|
|
if (! cinfo->raw_data_out) {
|
|
|
|
if (master->using_merged_upsample) {
|
|
|
|
#ifdef UPSAMPLE_MERGING_SUPPORTED
|
|
|
|
jinit_merged_upsampler(cinfo); /* does color conversion too */
|
|
|
|
#else
|
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
jinit_color_deconverter(cinfo);
|
|
|
|
jinit_upsampler(cinfo);
|
|
|
|
}
|
1995-08-01 20:00:00 -04:00
|
|
|
jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
/* Inverse DCT */
|
|
|
|
jinit_inverse_dct(cinfo);
|
|
|
|
/* Entropy decoding: either Huffman or arithmetic coding. */
|
2010-01-09 19:00:00 -05:00
|
|
|
if (cinfo->arith_code)
|
1998-03-27 19:00:00 -05:00
|
|
|
jinit_arith_decoder(cinfo);
|
2010-01-09 19:00:00 -05:00
|
|
|
else {
|
2009-06-26 20:00:00 -04:00
|
|
|
jinit_huff_decoder(cinfo);
|
1995-08-01 20:00:00 -04:00
|
|
|
}
|
1994-09-23 20:00:00 -04:00
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
/* Initialize principal buffer controllers. */
|
|
|
|
use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
|
|
|
|
jinit_d_coef_controller(cinfo, use_c_buffer);
|
|
|
|
|
|
|
|
if (! cinfo->raw_data_out)
|
|
|
|
jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
|
1994-09-23 20:00:00 -04:00
|
|
|
|
|
|
|
/* We can now tell the memory manager to allocate virtual arrays. */
|
|
|
|
(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
|
1995-08-01 20:00:00 -04:00
|
|
|
|
|
|
|
/* Initialize input side of decompressor to consume first scan. */
|
|
|
|
(*cinfo->inputctl->start_input_pass) (cinfo);
|
|
|
|
|
|
|
|
#ifdef D_MULTISCAN_FILES_SUPPORTED
|
|
|
|
/* If jpeg_start_decompress will read the whole file, initialize
|
|
|
|
* progress monitoring appropriately. The input step is counted
|
|
|
|
* as one pass.
|
|
|
|
*/
|
|
|
|
if (cinfo->progress != NULL && ! cinfo->buffered_image &&
|
|
|
|
cinfo->inputctl->has_multiple_scans) {
|
|
|
|
int nscans;
|
|
|
|
/* Estimate number of scans to set pass_limit. */
|
|
|
|
if (cinfo->progressive_mode) {
|
|
|
|
/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
|
|
|
|
nscans = 2 + 3 * cinfo->num_components;
|
|
|
|
} else {
|
|
|
|
/* For a nonprogressive multiscan file, estimate 1 scan per component. */
|
|
|
|
nscans = cinfo->num_components;
|
|
|
|
}
|
|
|
|
cinfo->progress->pass_counter = 0L;
|
|
|
|
cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
|
|
|
|
cinfo->progress->completed_passes = 0;
|
|
|
|
cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
|
|
|
|
/* Count the input pass as done */
|
|
|
|
master->pass_number++;
|
|
|
|
}
|
|
|
|
#endif /* D_MULTISCAN_FILES_SUPPORTED */
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Per-pass setup.
|
1995-08-01 20:00:00 -04:00
|
|
|
* This is called at the beginning of each output pass. We determine which
|
|
|
|
* modules will be active during this pass and give them appropriate
|
|
|
|
* start_pass calls. We also set is_dummy_pass to indicate whether this
|
|
|
|
* is a "real" output pass or a dummy pass for color quantization.
|
1998-03-26 19:00:00 -05:00
|
|
|
* (In the latter case, jdapistd.c will crank the pass to completion.)
|
1994-09-23 20:00:00 -04:00
|
|
|
*/
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1995-08-01 20:00:00 -04:00
|
|
|
prepare_for_output_pass (j_decompress_ptr cinfo)
|
1994-09-23 20:00:00 -04:00
|
|
|
{
|
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
if (master->pub.is_dummy_pass) {
|
|
|
|
#ifdef QUANT_2PASS_SUPPORTED
|
|
|
|
/* Final pass of 2-pass quantization */
|
|
|
|
master->pub.is_dummy_pass = FALSE;
|
|
|
|
(*cinfo->cquantize->start_pass) (cinfo, FALSE);
|
|
|
|
(*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
|
|
|
|
(*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
|
|
|
|
#else
|
|
|
|
ERREXIT(cinfo, JERR_NOT_COMPILED);
|
|
|
|
#endif /* QUANT_2PASS_SUPPORTED */
|
|
|
|
} else {
|
|
|
|
if (cinfo->quantize_colors && cinfo->colormap == NULL) {
|
|
|
|
/* Select new quantization method */
|
|
|
|
if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
|
|
|
|
cinfo->cquantize = master->quantizer_2pass;
|
|
|
|
master->pub.is_dummy_pass = TRUE;
|
|
|
|
} else if (cinfo->enable_1pass_quant) {
|
|
|
|
cinfo->cquantize = master->quantizer_1pass;
|
|
|
|
} else {
|
|
|
|
ERREXIT(cinfo, JERR_MODE_CHANGE);
|
|
|
|
}
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
1995-08-01 20:00:00 -04:00
|
|
|
(*cinfo->idct->start_pass) (cinfo);
|
|
|
|
(*cinfo->coef->start_output_pass) (cinfo);
|
1994-09-23 20:00:00 -04:00
|
|
|
if (! cinfo->raw_data_out) {
|
|
|
|
if (! master->using_merged_upsample)
|
|
|
|
(*cinfo->cconvert->start_pass) (cinfo);
|
|
|
|
(*cinfo->upsample->start_pass) (cinfo);
|
|
|
|
if (cinfo->quantize_colors)
|
1995-08-01 20:00:00 -04:00
|
|
|
(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
|
1994-09-23 20:00:00 -04:00
|
|
|
(*cinfo->post->start_pass) (cinfo,
|
1995-08-01 20:00:00 -04:00
|
|
|
(master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
|
|
|
|
(*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
}
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/* Set up progress monitor's pass info if present */
|
|
|
|
if (cinfo->progress != NULL) {
|
|
|
|
cinfo->progress->completed_passes = master->pass_number;
|
1995-08-01 20:00:00 -04:00
|
|
|
cinfo->progress->total_passes = master->pass_number +
|
|
|
|
(master->pub.is_dummy_pass ? 2 : 1);
|
|
|
|
/* In buffered-image mode, we assume one more output pass if EOI not
|
|
|
|
* yet reached, but no more passes if EOI has been reached.
|
|
|
|
*/
|
|
|
|
if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
|
|
|
|
cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
|
|
|
|
}
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
}
|
1991-10-06 20:00:00 -04:00
|
|
|
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
/*
|
1995-08-01 20:00:00 -04:00
|
|
|
* Finish up at end of an output pass.
|
1994-09-23 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
METHODDEF(void)
|
1995-08-01 20:00:00 -04:00
|
|
|
finish_output_pass (j_decompress_ptr cinfo)
|
1994-09-23 20:00:00 -04:00
|
|
|
{
|
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
if (cinfo->quantize_colors)
|
1994-09-23 20:00:00 -04:00
|
|
|
(*cinfo->cquantize->finish_pass) (cinfo);
|
1995-08-01 20:00:00 -04:00
|
|
|
master->pass_number++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef D_MULTISCAN_FILES_SUPPORTED
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch to a new external colormap between output passes.
|
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
GLOBAL(void)
|
1995-08-01 20:00:00 -04:00
|
|
|
jpeg_new_colormap (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
my_master_ptr master = (my_master_ptr) cinfo->master;
|
|
|
|
|
|
|
|
/* Prevent application from calling me at wrong times */
|
|
|
|
if (cinfo->global_state != DSTATE_BUFIMAGE)
|
|
|
|
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|
|
|
|
|
|
|
if (cinfo->quantize_colors && cinfo->enable_external_quant &&
|
|
|
|
cinfo->colormap != NULL) {
|
|
|
|
/* Select 2-pass quantizer for external colormap use */
|
|
|
|
cinfo->cquantize = master->quantizer_2pass;
|
|
|
|
/* Notify quantizer of colormap change */
|
|
|
|
(*cinfo->cquantize->new_color_map) (cinfo);
|
|
|
|
master->pub.is_dummy_pass = FALSE; /* just in case */
|
|
|
|
} else
|
|
|
|
ERREXIT(cinfo, JERR_MODE_CHANGE);
|
1994-09-23 20:00:00 -04:00
|
|
|
}
|
|
|
|
|
1995-08-01 20:00:00 -04:00
|
|
|
#endif /* D_MULTISCAN_FILES_SUPPORTED */
|
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
|
|
|
|
/*
|
1995-08-01 20:00:00 -04:00
|
|
|
* Initialize master decompression control and select active modules.
|
|
|
|
* This is performed at the start of jpeg_start_decompress.
|
1994-09-23 20:00:00 -04:00
|
|
|
*/
|
|
|
|
|
1996-02-06 19:00:00 -05:00
|
|
|
GLOBAL(void)
|
1994-09-23 20:00:00 -04:00
|
|
|
jinit_master_decompress (j_decompress_ptr cinfo)
|
|
|
|
{
|
|
|
|
my_master_ptr master;
|
1991-10-06 20:00:00 -04:00
|
|
|
|
2020-04-03 18:24:42 -04:00
|
|
|
master = (my_master_ptr) (*cinfo->mem->alloc_small)
|
|
|
|
((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_decomp_master));
|
2014-01-18 19:00:00 -05:00
|
|
|
cinfo->master = &master->pub;
|
1995-08-01 20:00:00 -04:00
|
|
|
master->pub.prepare_for_output_pass = prepare_for_output_pass;
|
|
|
|
master->pub.finish_output_pass = finish_output_pass;
|
|
|
|
|
|
|
|
master->pub.is_dummy_pass = FALSE;
|
1991-10-06 20:00:00 -04:00
|
|
|
|
1994-09-23 20:00:00 -04:00
|
|
|
master_selection(cinfo);
|
1991-10-06 20:00:00 -04:00
|
|
|
}
|