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