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