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