kusano 7d535a
/*
kusano 7d535a
 * jdinput.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1991-1997, Thomas G. Lane.
kusano 7d535a
 * Modified 2002-2009 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 input control logic for the JPEG decompressor.
kusano 7d535a
 * These routines are concerned with controlling the decompressor's input
kusano 7d535a
 * processing (marker reading and coefficient decoding).  The actual input
kusano 7d535a
 * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
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_input_controller pub; /* public fields */
kusano 7d535a
kusano 7d535a
  int inheaders;		/* Nonzero until first SOS is reached */
kusano 7d535a
} my_input_controller;
kusano 7d535a
kusano 7d535a
typedef my_input_controller * my_inputctl_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Forward declarations */
kusano 7d535a
METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Routines to calculate various quantities related to the size of the image.
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
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_core_output_dimensions (j_decompress_ptr cinfo)
kusano 7d535a
/* Do computations that are needed before master selection phase.
kusano 7d535a
 * This function is used for transcoding and full decompression.
kusano 7d535a
 */
kusano 7d535a
{
kusano 7d535a
#ifdef IDCT_SCALING_SUPPORTED
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Compute actual output image dimensions and DCT scaling choices. */
kusano 7d535a
  if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
kusano 7d535a
    /* Provide 1/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 1;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 1;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
kusano 7d535a
    /* Provide 2/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 2;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 2;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 3) {
kusano 7d535a
    /* Provide 3/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 3L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 3L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 3;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 3;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
kusano 7d535a
    /* Provide 4/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 4;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 4;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 5) {
kusano 7d535a
    /* Provide 5/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 5L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 5L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 5;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 5;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 6) {
kusano 7d535a
    /* Provide 6/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 6L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 6L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 6;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 6;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 7) {
kusano 7d535a
    /* Provide 7/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 7L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 7L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 7;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 7;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
kusano 7d535a
    /* Provide 8/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 8;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 8;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 9) {
kusano 7d535a
    /* Provide 9/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 9L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 9L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 9;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 9;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 10) {
kusano 7d535a
    /* Provide 10/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 10L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 10L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 10;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 10;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 11) {
kusano 7d535a
    /* Provide 11/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 11L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 11L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 11;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 11;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 12) {
kusano 7d535a
    /* Provide 12/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 12L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 12L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 12;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 12;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 13) {
kusano 7d535a
    /* Provide 13/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 13L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 13L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 13;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 13;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 14) {
kusano 7d535a
    /* Provide 14/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 14L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 14L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 14;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 14;
kusano 7d535a
  } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 15) {
kusano 7d535a
    /* Provide 15/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 15L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 15L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 15;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 15;
kusano 7d535a
  } else {
kusano 7d535a
    /* Provide 16/block_size scaling */
kusano 7d535a
    cinfo->output_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * 16L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->output_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * 16L, (long) cinfo->block_size);
kusano 7d535a
    cinfo->min_DCT_h_scaled_size = 16;
kusano 7d535a
    cinfo->min_DCT_v_scaled_size = 16;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Recompute dimensions of components */
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
kusano 7d535a
    compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
#else /* !IDCT_SCALING_SUPPORTED */
kusano 7d535a
kusano 7d535a
  /* Hardwire it to "no scaling" */
kusano 7d535a
  cinfo->output_width = cinfo->image_width;
kusano 7d535a
  cinfo->output_height = cinfo->image_height;
kusano 7d535a
  /* jdinput.c has already initialized DCT_scaled_size,
kusano 7d535a
   * and has computed unscaled downsampled_width and downsampled_height.
kusano 7d535a
   */
kusano 7d535a
kusano 7d535a
#endif /* IDCT_SCALING_SUPPORTED */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
initial_setup (j_decompress_ptr cinfo)
kusano 7d535a
/* Called once, when first SOS marker is reached */
kusano 7d535a
{
kusano 7d535a
  int ci;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
kusano 7d535a
  /* Make sure image isn't bigger than I can handle */
kusano 7d535a
  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
kusano 7d535a
      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
kusano 7d535a
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
kusano 7d535a
kusano 7d535a
  /* For now, precision must match compiled-in value... */
kusano 7d535a
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
kusano 7d535a
kusano 7d535a
  /* Check that number of components won't exceed internal array sizes */
kusano 7d535a
  if (cinfo->num_components > MAX_COMPONENTS)
kusano 7d535a
    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
kusano 7d535a
	     MAX_COMPONENTS);
kusano 7d535a
kusano 7d535a
  /* Compute maximum sampling factors; check factor validity */
kusano 7d535a
  cinfo->max_h_samp_factor = 1;
kusano 7d535a
  cinfo->max_v_samp_factor = 1;
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
kusano 7d535a
	compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
kusano 7d535a
      ERREXIT(cinfo, JERR_BAD_SAMPLING);
kusano 7d535a
    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
kusano 7d535a
				   compptr->h_samp_factor);
kusano 7d535a
    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
kusano 7d535a
				   compptr->v_samp_factor);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Derive block_size, natural_order, and lim_Se */
kusano 7d535a
  if (cinfo->is_baseline || (cinfo->progressive_mode &&
kusano 7d535a
      cinfo->comps_in_scan)) { /* no pseudo SOS marker */
kusano 7d535a
    cinfo->block_size = DCTSIZE;
kusano 7d535a
    cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
    cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
  } else
kusano 7d535a
    switch (cinfo->Se) {
kusano 7d535a
    case (1*1-1):
kusano 7d535a
      cinfo->block_size = 1;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order; /* not needed */
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (2*2-1):
kusano 7d535a
      cinfo->block_size = 2;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order2;
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (3*3-1):
kusano 7d535a
      cinfo->block_size = 3;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order3;
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (4*4-1):
kusano 7d535a
      cinfo->block_size = 4;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order4;
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (5*5-1):
kusano 7d535a
      cinfo->block_size = 5;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order5;
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (6*6-1):
kusano 7d535a
      cinfo->block_size = 6;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order6;
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (7*7-1):
kusano 7d535a
      cinfo->block_size = 7;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order7;
kusano 7d535a
      cinfo->lim_Se = cinfo->Se;
kusano 7d535a
      break;
kusano 7d535a
    case (8*8-1):
kusano 7d535a
      cinfo->block_size = 8;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (9*9-1):
kusano 7d535a
      cinfo->block_size = 9;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (10*10-1):
kusano 7d535a
      cinfo->block_size = 10;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (11*11-1):
kusano 7d535a
      cinfo->block_size = 11;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (12*12-1):
kusano 7d535a
      cinfo->block_size = 12;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (13*13-1):
kusano 7d535a
      cinfo->block_size = 13;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (14*14-1):
kusano 7d535a
      cinfo->block_size = 14;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (15*15-1):
kusano 7d535a
      cinfo->block_size = 15;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    case (16*16-1):
kusano 7d535a
      cinfo->block_size = 16;
kusano 7d535a
      cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
      cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
      break;
kusano 7d535a
    default:
kusano 7d535a
      ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
kusano 7d535a
	       cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
kusano 7d535a
      break;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
  /* We initialize DCT_scaled_size and min_DCT_scaled_size to block_size.
kusano 7d535a
   * In the full decompressor,
kusano 7d535a
   * this will be overridden by jpeg_calc_output_dimensions in jdmaster.c;
kusano 7d535a
   * but in the transcoder,
kusano 7d535a
   * jpeg_calc_output_dimensions is not used, so we must do it here.
kusano 7d535a
   */
kusano 7d535a
  cinfo->min_DCT_h_scaled_size = cinfo->block_size;
kusano 7d535a
  cinfo->min_DCT_v_scaled_size = cinfo->block_size;
kusano 7d535a
kusano 7d535a
  /* Compute dimensions of components */
kusano 7d535a
  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
kusano 7d535a
       ci++, compptr++) {
kusano 7d535a
    compptr->DCT_h_scaled_size = cinfo->block_size;
kusano 7d535a
    compptr->DCT_v_scaled_size = cinfo->block_size;
kusano 7d535a
    /* Size in DCT blocks */
kusano 7d535a
    compptr->width_in_blocks = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
kusano 7d535a
		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
kusano 7d535a
    compptr->height_in_blocks = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
kusano 7d535a
		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
kusano 7d535a
    /* downsampled_width and downsampled_height will also be overridden by
kusano 7d535a
     * jdmaster.c if we are doing full decompression.  The transcoder library
kusano 7d535a
     * doesn't use these values, but the calling application might.
kusano 7d535a
     */
kusano 7d535a
    /* Size in samples */
kusano 7d535a
    compptr->downsampled_width = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
kusano 7d535a
		    (long) cinfo->max_h_samp_factor);
kusano 7d535a
    compptr->downsampled_height = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
kusano 7d535a
		    (long) cinfo->max_v_samp_factor);
kusano 7d535a
    /* Mark component needed, until color conversion says otherwise */
kusano 7d535a
    compptr->component_needed = TRUE;
kusano 7d535a
    /* Mark no quantization table yet saved for component */
kusano 7d535a
    compptr->quant_table = NULL;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Compute number of fully interleaved MCU rows. */
kusano 7d535a
  cinfo->total_iMCU_rows = (JDIMENSION)
kusano 7d535a
    jdiv_round_up((long) cinfo->image_height,
kusano 7d535a
	          (long) (cinfo->max_v_samp_factor * cinfo->block_size));
kusano 7d535a
kusano 7d535a
  /* Decide whether file contains multiple scans */
kusano 7d535a
  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
kusano 7d535a
    cinfo->inputctl->has_multiple_scans = TRUE;
kusano 7d535a
  else
kusano 7d535a
    cinfo->inputctl->has_multiple_scans = FALSE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
per_scan_setup (j_decompress_ptr cinfo)
kusano 7d535a
/* Do computations that are needed before processing a JPEG scan */
kusano 7d535a
/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
kusano 7d535a
{
kusano 7d535a
  int ci, mcublks, tmp;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  
kusano 7d535a
  if (cinfo->comps_in_scan == 1) {
kusano 7d535a
    
kusano 7d535a
    /* Noninterleaved (single-component) scan */
kusano 7d535a
    compptr = cinfo->cur_comp_info[0];
kusano 7d535a
    
kusano 7d535a
    /* Overall image size in MCUs */
kusano 7d535a
    cinfo->MCUs_per_row = compptr->width_in_blocks;
kusano 7d535a
    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
kusano 7d535a
    
kusano 7d535a
    /* For noninterleaved scan, always one block per MCU */
kusano 7d535a
    compptr->MCU_width = 1;
kusano 7d535a
    compptr->MCU_height = 1;
kusano 7d535a
    compptr->MCU_blocks = 1;
kusano 7d535a
    compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
kusano 7d535a
    compptr->last_col_width = 1;
kusano 7d535a
    /* For noninterleaved scans, it is convenient to define last_row_height
kusano 7d535a
     * as the number of block rows present in the last iMCU row.
kusano 7d535a
     */
kusano 7d535a
    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
kusano 7d535a
    if (tmp == 0) tmp = compptr->v_samp_factor;
kusano 7d535a
    compptr->last_row_height = tmp;
kusano 7d535a
    
kusano 7d535a
    /* Prepare array describing MCU composition */
kusano 7d535a
    cinfo->blocks_in_MCU = 1;
kusano 7d535a
    cinfo->MCU_membership[0] = 0;
kusano 7d535a
    
kusano 7d535a
  } else {
kusano 7d535a
    
kusano 7d535a
    /* Interleaved (multi-component) scan */
kusano 7d535a
    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
kusano 7d535a
      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
kusano 7d535a
	       MAX_COMPS_IN_SCAN);
kusano 7d535a
    
kusano 7d535a
    /* Overall image size in MCUs */
kusano 7d535a
    cinfo->MCUs_per_row = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_width,
kusano 7d535a
		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
kusano 7d535a
    cinfo->MCU_rows_in_scan = (JDIMENSION)
kusano 7d535a
      jdiv_round_up((long) cinfo->image_height,
kusano 7d535a
		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
kusano 7d535a
    
kusano 7d535a
    cinfo->blocks_in_MCU = 0;
kusano 7d535a
    
kusano 7d535a
    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
      compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
      /* Sampling factors give # of blocks of component in each MCU */
kusano 7d535a
      compptr->MCU_width = compptr->h_samp_factor;
kusano 7d535a
      compptr->MCU_height = compptr->v_samp_factor;
kusano 7d535a
      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
kusano 7d535a
      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
kusano 7d535a
      /* Figure number of non-dummy blocks in last MCU column & row */
kusano 7d535a
      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
kusano 7d535a
      if (tmp == 0) tmp = compptr->MCU_width;
kusano 7d535a
      compptr->last_col_width = tmp;
kusano 7d535a
      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
kusano 7d535a
      if (tmp == 0) tmp = compptr->MCU_height;
kusano 7d535a
      compptr->last_row_height = tmp;
kusano 7d535a
      /* Prepare array describing MCU composition */
kusano 7d535a
      mcublks = compptr->MCU_blocks;
kusano 7d535a
      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
kusano 7d535a
	ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
kusano 7d535a
      while (mcublks-- > 0) {
kusano 7d535a
	cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
    
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Save away a copy of the Q-table referenced by each component present
kusano 7d535a
 * in the current scan, unless already saved during a prior scan.
kusano 7d535a
 *
kusano 7d535a
 * In a multiple-scan JPEG file, the encoder could assign different components
kusano 7d535a
 * the same Q-table slot number, but change table definitions between scans
kusano 7d535a
 * so that each component uses a different Q-table.  (The IJG encoder is not
kusano 7d535a
 * currently capable of doing this, but other encoders might.)  Since we want
kusano 7d535a
 * to be able to dequantize all the components at the end of the file, this
kusano 7d535a
 * means that we have to save away the table actually used for each component.
kusano 7d535a
 * We do this by copying the table at the start of the first scan containing
kusano 7d535a
 * the component.
kusano 7d535a
 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
kusano 7d535a
 * slot between scans of a component using that slot.  If the encoder does so
kusano 7d535a
 * anyway, this decoder will simply use the Q-table values that were current
kusano 7d535a
 * at the start of the first scan for the component.
kusano 7d535a
 *
kusano 7d535a
 * The decompressor output side looks only at the saved quant tables,
kusano 7d535a
 * not at the current Q-table slots.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
latch_quant_tables (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  int ci, qtblno;
kusano 7d535a
  jpeg_component_info *compptr;
kusano 7d535a
  JQUANT_TBL * qtbl;
kusano 7d535a
kusano 7d535a
  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
kusano 7d535a
    compptr = cinfo->cur_comp_info[ci];
kusano 7d535a
    /* No work if we already saved Q-table for this component */
kusano 7d535a
    if (compptr->quant_table != NULL)
kusano 7d535a
      continue;
kusano 7d535a
    /* Make sure specified quantization table is present */
kusano 7d535a
    qtblno = compptr->quant_tbl_no;
kusano 7d535a
    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
kusano 7d535a
	cinfo->quant_tbl_ptrs[qtblno] == NULL)
kusano 7d535a
      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
kusano 7d535a
    /* OK, save away the quantization table */
kusano 7d535a
    qtbl = (JQUANT_TBL *)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(JQUANT_TBL));
kusano 7d535a
    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
kusano 7d535a
    compptr->quant_table = qtbl;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize the input modules to read a scan of compressed data.
kusano 7d535a
 * The first call to this is done by jdmaster.c after initializing
kusano 7d535a
 * the entire decompressor (during jpeg_start_decompress).
kusano 7d535a
 * Subsequent calls come from consume_markers, below.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_input_pass (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  per_scan_setup(cinfo);
kusano 7d535a
  latch_quant_tables(cinfo);
kusano 7d535a
  (*cinfo->entropy->start_pass) (cinfo);
kusano 7d535a
  (*cinfo->coef->start_input_pass) (cinfo);
kusano 7d535a
  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up after inputting a compressed-data scan.
kusano 7d535a
 * This is called by the coefficient controller after it's read all
kusano 7d535a
 * the expected data of the scan.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
finish_input_pass (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  cinfo->inputctl->consume_input = consume_markers;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read JPEG markers before, between, or after compressed-data scans.
kusano 7d535a
 * Change state as necessary when a new scan is reached.
kusano 7d535a
 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
kusano 7d535a
 *
kusano 7d535a
 * The consume_input method pointer points either here or to the
kusano 7d535a
 * coefficient controller's consume_data routine, depending on whether
kusano 7d535a
 * we are reading a compressed data segment or inter-segment markers.
kusano 7d535a
 *
kusano 7d535a
 * Note: This function should NOT return a pseudo SOS marker (with zero
kusano 7d535a
 * component number) to the caller.  A pseudo marker received by
kusano 7d535a
 * read_markers is processed and then skipped for other markers.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(int)
kusano 7d535a
consume_markers (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
kusano 7d535a
  int val;
kusano 7d535a
kusano 7d535a
  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
kusano 7d535a
    return JPEG_REACHED_EOI;
kusano 7d535a
kusano 7d535a
  for (;;) {			/* Loop to pass pseudo SOS marker */
kusano 7d535a
    val = (*cinfo->marker->read_markers) (cinfo);
kusano 7d535a
kusano 7d535a
    switch (val) {
kusano 7d535a
    case JPEG_REACHED_SOS:	/* Found SOS */
kusano 7d535a
      if (inputctl->inheaders) { /* 1st SOS */
kusano 7d535a
	if (inputctl->inheaders == 1)
kusano 7d535a
	  initial_setup(cinfo);
kusano 7d535a
	if (cinfo->comps_in_scan == 0) { /* pseudo SOS marker */
kusano 7d535a
	  inputctl->inheaders = 2;
kusano 7d535a
	  break;
kusano 7d535a
	}
kusano 7d535a
	inputctl->inheaders = 0;
kusano 7d535a
	/* Note: start_input_pass must be called by jdmaster.c
kusano 7d535a
	 * before any more input can be consumed.  jdapimin.c is
kusano 7d535a
	 * responsible for enforcing this sequencing.
kusano 7d535a
	 */
kusano 7d535a
      } else {			/* 2nd or later SOS marker */
kusano 7d535a
	if (! inputctl->pub.has_multiple_scans)
kusano 7d535a
	  ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
kusano 7d535a
	if (cinfo->comps_in_scan == 0) /* unexpected pseudo SOS marker */
kusano 7d535a
	  break;
kusano 7d535a
	start_input_pass(cinfo);
kusano 7d535a
      }
kusano 7d535a
      return val;
kusano 7d535a
    case JPEG_REACHED_EOI:	/* Found EOI */
kusano 7d535a
      inputctl->pub.eoi_reached = TRUE;
kusano 7d535a
      if (inputctl->inheaders) { /* Tables-only datastream, apparently */
kusano 7d535a
	if (cinfo->marker->saw_SOF)
kusano 7d535a
	  ERREXIT(cinfo, JERR_SOF_NO_SOS);
kusano 7d535a
      } else {
kusano 7d535a
	/* Prevent infinite loop in coef ctlr's decompress_data routine
kusano 7d535a
	 * if user set output_scan_number larger than number of scans.
kusano 7d535a
	 */
kusano 7d535a
	if (cinfo->output_scan_number > cinfo->input_scan_number)
kusano 7d535a
	  cinfo->output_scan_number = cinfo->input_scan_number;
kusano 7d535a
      }
kusano 7d535a
      return val;
kusano 7d535a
    case JPEG_SUSPENDED:
kusano 7d535a
      return val;
kusano 7d535a
    default:
kusano 7d535a
      return val;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Reset state to begin a fresh datastream.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
reset_input_controller (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
kusano 7d535a
kusano 7d535a
  inputctl->pub.consume_input = consume_markers;
kusano 7d535a
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
kusano 7d535a
  inputctl->pub.eoi_reached = FALSE;
kusano 7d535a
  inputctl->inheaders = 1;
kusano 7d535a
  /* Reset other modules */
kusano 7d535a
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
kusano 7d535a
  (*cinfo->marker->reset_marker_reader) (cinfo);
kusano 7d535a
  /* Reset progression state -- would be cleaner if entropy decoder did this */
kusano 7d535a
  cinfo->coef_bits = NULL;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize the input controller module.
kusano 7d535a
 * This is called only once, when the decompression object is created.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jinit_input_controller (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  my_inputctl_ptr inputctl;
kusano 7d535a
kusano 7d535a
  /* Create subobject in permanent pool */
kusano 7d535a
  inputctl = (my_inputctl_ptr)
kusano 7d535a
    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
kusano 7d535a
				SIZEOF(my_input_controller));
kusano 7d535a
  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
kusano 7d535a
  /* Initialize method pointers */
kusano 7d535a
  inputctl->pub.consume_input = consume_markers;
kusano 7d535a
  inputctl->pub.reset_input_controller = reset_input_controller;
kusano 7d535a
  inputctl->pub.start_input_pass = start_input_pass;
kusano 7d535a
  inputctl->pub.finish_input_pass = finish_input_pass;
kusano 7d535a
  /* Initialize state: can't use reset_input_controller since we don't
kusano 7d535a
   * want to try to reset other modules yet.
kusano 7d535a
   */
kusano 7d535a
  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
kusano 7d535a
  inputctl->pub.eoi_reached = FALSE;
kusano 7d535a
  inputctl->inheaders = 1;
kusano 7d535a
}