shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jdapistd.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1994-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2010, 2015-2018, 2020, 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 application interface code for the decompression half
shun-iwasawa 82a8f5
 * of the JPEG library.  These are the "standard" API routines that are
shun-iwasawa 82a8f5
 * used in the normal full-decompression case.  They are not used by a
shun-iwasawa 82a8f5
 * transcoding-only application.  Note that if an application links in
shun-iwasawa 82a8f5
 * jpeg_start_decompress, it will end up linking in the entire decompressor.
shun-iwasawa 82a8f5
 * We thus must separate this file from jdapimin.c to avoid linking the
shun-iwasawa 82a8f5
 * whole decompression library into a transcoder.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jdmainct.h"
shun-iwasawa 82a8f5
#include "jdcoefct.h"
shun-iwasawa 82a8f5
#include "jdmaster.h"
shun-iwasawa 82a8f5
#include "jdmerge.h"
shun-iwasawa 82a8f5
#include "jdsample.h"
shun-iwasawa 82a8f5
#include "jmemsys.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Forward declarations */
shun-iwasawa 82a8f5
LOCAL(boolean) output_pass_setup(j_decompress_ptr cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Decompression initialization.
shun-iwasawa 82a8f5
 * jpeg_read_header must be completed before calling this.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * If a multipass operating mode was selected, this will do all but the
shun-iwasawa 82a8f5
 * last pass, and thus may take a great deal of time.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Returns FALSE if suspended.  The return value need be inspected only if
shun-iwasawa 82a8f5
 * a suspending data source is used.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(boolean)
shun-iwasawa 82a8f5
jpeg_start_decompress(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (cinfo->global_state == DSTATE_READY) {
shun-iwasawa 82a8f5
    /* First call: initialize master control, select active modules */
shun-iwasawa 82a8f5
    jinit_master_decompress(cinfo);
shun-iwasawa 82a8f5
    if (cinfo->buffered_image) {
shun-iwasawa 82a8f5
      /* No more work here; expecting jpeg_start_output next */
shun-iwasawa 82a8f5
      cinfo->global_state = DSTATE_BUFIMAGE;
shun-iwasawa 82a8f5
      return TRUE;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    cinfo->global_state = DSTATE_PRELOAD;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  if (cinfo->global_state == DSTATE_PRELOAD) {
shun-iwasawa 82a8f5
    /* If file has multiple scans, absorb them all into the coef buffer */
shun-iwasawa 82a8f5
    if (cinfo->inputctl->has_multiple_scans) {
shun-iwasawa 82a8f5
#ifdef D_MULTISCAN_FILES_SUPPORTED
shun-iwasawa 82a8f5
      for (;;) {
shun-iwasawa 82a8f5
        int retcode;
shun-iwasawa 82a8f5
        /* Call progress monitor hook if present */
shun-iwasawa 82a8f5
        if (cinfo->progress != NULL)
shun-iwasawa 82a8f5
          (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
        /* Absorb some more input */
shun-iwasawa 82a8f5
        retcode = (*cinfo->inputctl->consume_input) (cinfo);
shun-iwasawa 82a8f5
        if (retcode == JPEG_SUSPENDED)
shun-iwasawa 82a8f5
          return FALSE;
shun-iwasawa 82a8f5
        if (retcode == JPEG_REACHED_EOI)
shun-iwasawa 82a8f5
          break;
shun-iwasawa 82a8f5
        /* Advance progress counter if appropriate */
shun-iwasawa 82a8f5
        if (cinfo->progress != NULL &&
shun-iwasawa 82a8f5
            (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
shun-iwasawa 82a8f5
          if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
shun-iwasawa 82a8f5
            /* jdmaster underestimated number of scans; ratchet up one scan */
shun-iwasawa 82a8f5
            cinfo->progress->pass_limit += (long)cinfo->total_iMCU_rows;
shun-iwasawa 82a8f5
          }
shun-iwasawa 82a8f5
        }
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_NOT_COMPILED);
shun-iwasawa 82a8f5
#endif /* D_MULTISCAN_FILES_SUPPORTED */
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    cinfo->output_scan_number = cinfo->input_scan_number;
shun-iwasawa 82a8f5
  } else if (cinfo->global_state != DSTATE_PRESCAN)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
  /* Perform any dummy output passes, and set up for the final pass */
shun-iwasawa 82a8f5
  return output_pass_setup(cinfo);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Set up for an output pass, and perform any dummy pass(es) needed.
shun-iwasawa 82a8f5
 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
shun-iwasawa 82a8f5
 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
shun-iwasawa 82a8f5
 * Exit: If done, returns TRUE and sets global_state for proper output mode.
shun-iwasawa 82a8f5
 *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(boolean)
shun-iwasawa 82a8f5
output_pass_setup(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (cinfo->global_state != DSTATE_PRESCAN) {
shun-iwasawa 82a8f5
    /* First call: do pass setup */
shun-iwasawa 82a8f5
    (*cinfo->master->prepare_for_output_pass) (cinfo);
shun-iwasawa 82a8f5
    cinfo->output_scanline = 0;
shun-iwasawa 82a8f5
    cinfo->global_state = DSTATE_PRESCAN;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* Loop over any required dummy passes */
shun-iwasawa 82a8f5
  while (cinfo->master->is_dummy_pass) {
shun-iwasawa 82a8f5
#ifdef QUANT_2PASS_SUPPORTED
shun-iwasawa 82a8f5
    /* Crank through the dummy pass */
shun-iwasawa 82a8f5
    while (cinfo->output_scanline < cinfo->output_height) {
shun-iwasawa 82a8f5
      JDIMENSION last_scanline;
shun-iwasawa 82a8f5
      /* Call progress monitor hook if present */
shun-iwasawa 82a8f5
      if (cinfo->progress != NULL) {
shun-iwasawa 82a8f5
        cinfo->progress->pass_counter = (long)cinfo->output_scanline;
shun-iwasawa 82a8f5
        cinfo->progress->pass_limit = (long)cinfo->output_height;
shun-iwasawa 82a8f5
        (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      /* Process some data */
shun-iwasawa 82a8f5
      last_scanline = cinfo->output_scanline;
shun-iwasawa 82a8f5
      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY)NULL,
shun-iwasawa 82a8f5
                                    &cinfo->output_scanline, (JDIMENSION)0);
shun-iwasawa 82a8f5
      if (cinfo->output_scanline == last_scanline)
shun-iwasawa 82a8f5
        return FALSE;           /* No progress made, must suspend */
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    /* Finish up dummy pass, and set up for another one */
shun-iwasawa 82a8f5
    (*cinfo->master->finish_output_pass) (cinfo);
shun-iwasawa 82a8f5
    (*cinfo->master->prepare_for_output_pass) (cinfo);
shun-iwasawa 82a8f5
    cinfo->output_scanline = 0;
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_NOT_COMPILED);
shun-iwasawa 82a8f5
#endif /* QUANT_2PASS_SUPPORTED */
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* Ready for application to drive output pass through
shun-iwasawa 82a8f5
   * jpeg_read_scanlines or jpeg_read_raw_data.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Enable partial scanline decompression
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Must be called after jpeg_start_decompress() and before any calls to
shun-iwasawa 82a8f5
 * jpeg_read_scanlines() or jpeg_skip_scanlines().
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Refer to libjpeg.txt for more information.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(void)
shun-iwasawa 82a8f5
jpeg_crop_scanline(j_decompress_ptr cinfo, JDIMENSION *xoffset,
shun-iwasawa 82a8f5
                   JDIMENSION *width)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int ci, align, orig_downsampled_width;
shun-iwasawa 82a8f5
  JDIMENSION input_xoffset;
shun-iwasawa 82a8f5
  boolean reinit_upsampler = FALSE;
shun-iwasawa 82a8f5
  jpeg_component_info *compptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->global_state != DSTATE_SCANNING || cinfo->output_scanline != 0)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!xoffset || !width)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BAD_CROP_SPEC);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* xoffset and width must fall within the output image dimensions. */
shun-iwasawa 82a8f5
  if (*width == 0 || *xoffset + *width > cinfo->output_width)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* No need to do anything if the caller wants the entire width. */
shun-iwasawa 82a8f5
  if (*width == cinfo->output_width)
shun-iwasawa 82a8f5
    return;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Ensuring the proper alignment of xoffset is tricky.  At minimum, it
shun-iwasawa 82a8f5
   * must align with an MCU boundary, because:
shun-iwasawa 82a8f5
   *
shun-iwasawa 82a8f5
   *   (1) The IDCT is performed in blocks, and it is not feasible to modify
shun-iwasawa 82a8f5
   *       the algorithm so that it can transform partial blocks.
shun-iwasawa 82a8f5
   *   (2) Because of the SIMD extensions, any input buffer passed to the
shun-iwasawa 82a8f5
   *       upsampling and color conversion routines must be aligned to the
shun-iwasawa 82a8f5
   *       SIMD word size (for instance, 128-bit in the case of SSE2.)  The
shun-iwasawa 82a8f5
   *       easiest way to accomplish this without copying data is to ensure
shun-iwasawa 82a8f5
   *       that upsampling and color conversion begin at the start of the
shun-iwasawa 82a8f5
   *       first MCU column that will be inverse transformed.
shun-iwasawa 82a8f5
   *
shun-iwasawa 82a8f5
   * In practice, we actually impose a stricter alignment requirement.  We
shun-iwasawa 82a8f5
   * require that xoffset be a multiple of the maximum MCU column width of all
shun-iwasawa 82a8f5
   * of the components (the "iMCU column width.")  This is to simplify the
shun-iwasawa 82a8f5
   * single-pass decompression case, allowing us to use the same MCU column
shun-iwasawa 82a8f5
   * width for all of the components.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
shun-iwasawa 82a8f5
    align = cinfo->_min_DCT_scaled_size;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Adjust xoffset to the nearest iMCU boundary <= the requested value */
shun-iwasawa 82a8f5
  input_xoffset = *xoffset;
shun-iwasawa 82a8f5
  *xoffset = (input_xoffset / align) * align;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Adjust the width so that the right edge of the output image is as
shun-iwasawa 82a8f5
   * requested (only the left edge is altered.)  It is important that calling
shun-iwasawa 82a8f5
   * programs check this value after this function returns, so that they can
shun-iwasawa 82a8f5
   * allocate an output buffer with the appropriate size.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  *width = *width + input_xoffset - *xoffset;
shun-iwasawa 82a8f5
  cinfo->output_width = *width;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Set the first and last iMCU columns that we must decompress.  These values
shun-iwasawa 82a8f5
   * will be used in single-scan decompressions.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  cinfo->master->first_iMCU_col = (JDIMENSION)(long)(*xoffset) / (long)align;
shun-iwasawa 82a8f5
  cinfo->master->last_iMCU_col =
shun-iwasawa 82a8f5
    (JDIMENSION)jdiv_round_up((long)(*xoffset + cinfo->output_width),
shun-iwasawa 82a8f5
                              (long)align) - 1;
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 hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
shun-iwasawa 82a8f5
              1 : compptr->h_samp_factor;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Set downsampled_width to the new output width. */
shun-iwasawa 82a8f5
    orig_downsampled_width = compptr->downsampled_width;
shun-iwasawa 82a8f5
    compptr->downsampled_width =
shun-iwasawa 82a8f5
      (JDIMENSION)jdiv_round_up((long)(cinfo->output_width *
shun-iwasawa 82a8f5
                                       compptr->h_samp_factor),
shun-iwasawa 82a8f5
                                (long)cinfo->max_h_samp_factor);
shun-iwasawa 82a8f5
    if (compptr->downsampled_width < 2 && orig_downsampled_width >= 2)
shun-iwasawa 82a8f5
      reinit_upsampler = TRUE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Set the first and last iMCU columns that we must decompress.  These
shun-iwasawa 82a8f5
     * values will be used in multi-scan decompressions.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    cinfo->master->first_MCU_col[ci] =
shun-iwasawa 82a8f5
      (JDIMENSION)(long)(*xoffset * hsf) / (long)align;
shun-iwasawa 82a8f5
    cinfo->master->last_MCU_col[ci] =
shun-iwasawa 82a8f5
      (JDIMENSION)jdiv_round_up((long)((*xoffset + cinfo->output_width) * hsf),
shun-iwasawa 82a8f5
                                (long)align) - 1;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (reinit_upsampler) {
shun-iwasawa 82a8f5
    cinfo->master->jinit_upsampler_no_alloc = TRUE;
shun-iwasawa 82a8f5
    jinit_upsampler(cinfo);
shun-iwasawa 82a8f5
    cinfo->master->jinit_upsampler_no_alloc = FALSE;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Read some scanlines of data from the JPEG decompressor.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * The return value will be the number of lines actually read.
shun-iwasawa 82a8f5
 * This may be less than the number requested in several cases,
shun-iwasawa 82a8f5
 * including bottom of image, data source suspension, and operating
shun-iwasawa 82a8f5
 * modes that emit multiple scanlines at a time.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Note: we warn about excess calls to jpeg_read_scanlines() since
shun-iwasawa 82a8f5
 * this likely signals an application programmer error.  However,
shun-iwasawa 82a8f5
 * an oversize buffer (max_lines > scanlines remaining) is not an error.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(JDIMENSION)
shun-iwasawa 82a8f5
jpeg_read_scanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines,
shun-iwasawa 82a8f5
                    JDIMENSION max_lines)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JDIMENSION row_ctr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->global_state != DSTATE_SCANNING)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
  if (cinfo->output_scanline >= cinfo->output_height) {
shun-iwasawa 82a8f5
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Call progress monitor hook if present */
shun-iwasawa 82a8f5
  if (cinfo->progress != NULL) {
shun-iwasawa 82a8f5
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
shun-iwasawa 82a8f5
    cinfo->progress->pass_limit = (long)cinfo->output_height;
shun-iwasawa 82a8f5
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Process some data */
shun-iwasawa 82a8f5
  row_ctr = 0;
shun-iwasawa 82a8f5
  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
shun-iwasawa 82a8f5
  cinfo->output_scanline += row_ctr;
shun-iwasawa 82a8f5
  return row_ctr;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Dummy color convert function used by jpeg_skip_scanlines() */
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
noop_convert(j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
             JDIMENSION input_row, JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Dummy quantize function used by jpeg_skip_scanlines() */
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
noop_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shun-iwasawa 82a8f5
              JSAMPARRAY output_buf, int num_rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * In some cases, it is best to call jpeg_read_scanlines() and discard the
shun-iwasawa 82a8f5
 * output, rather than skipping the scanlines, because this allows us to
shun-iwasawa 82a8f5
 * maintain the internal state of the context-based upsampler.  In these cases,
shun-iwasawa 82a8f5
 * we set up and tear down a dummy color converter in order to avoid valgrind
shun-iwasawa 82a8f5
 * errors and to achieve the best possible performance.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JDIMENSION n;
shun-iwasawa 82a8f5
  my_master_ptr master = (my_master_ptr)cinfo->master;
shun-iwasawa 82a8f5
  JSAMPARRAY scanlines = NULL;
shun-iwasawa 82a8f5
  void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
shun-iwasawa 82a8f5
                         JDIMENSION input_row, JSAMPARRAY output_buf,
shun-iwasawa 82a8f5
                         int num_rows) = NULL;
shun-iwasawa 82a8f5
  void (*color_quantize) (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
shun-iwasawa 82a8f5
                          JSAMPARRAY output_buf, int num_rows) = NULL;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->cconvert && cinfo->cconvert->color_convert) {
shun-iwasawa 82a8f5
    color_convert = cinfo->cconvert->color_convert;
shun-iwasawa 82a8f5
    cinfo->cconvert->color_convert = noop_convert;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->cquantize && cinfo->cquantize->color_quantize) {
shun-iwasawa 82a8f5
    color_quantize = cinfo->cquantize->color_quantize;
shun-iwasawa 82a8f5
    cinfo->cquantize->color_quantize = noop_quantize;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
shun-iwasawa 82a8f5
    my_merged_upsample_ptr upsample = (my_merged_upsample_ptr)cinfo->upsample;
shun-iwasawa 82a8f5
    scanlines = &upsample->spare_row;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (n = 0; n < num_lines; n++)
shun-iwasawa 82a8f5
    jpeg_read_scanlines(cinfo, scanlines, 1);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (color_convert)
shun-iwasawa 82a8f5
    cinfo->cconvert->color_convert = color_convert;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (color_quantize)
shun-iwasawa 82a8f5
    cinfo->cquantize->color_quantize = color_quantize;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Called by jpeg_skip_scanlines().  This partially skips a decompress block by
shun-iwasawa 82a8f5
 * incrementing the rowgroup counter.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
increment_simple_rowgroup_ctr(j_decompress_ptr cinfo, JDIMENSION rows)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JDIMENSION rows_left;
shun-iwasawa 82a8f5
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
shun-iwasawa 82a8f5
  my_master_ptr master = (my_master_ptr)cinfo->master;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (master->using_merged_upsample && cinfo->max_v_samp_factor == 2) {
shun-iwasawa 82a8f5
    read_and_discard_scanlines(cinfo, rows);
shun-iwasawa 82a8f5
    return;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Increment the counter to the next row group after the skipped rows. */
shun-iwasawa 82a8f5
  main_ptr->rowgroup_ctr += rows / cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Partially skipping a row group would involve modifying the internal state
shun-iwasawa 82a8f5
   * of the upsampler, so read the remaining rows into a dummy buffer instead.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  rows_left = rows % cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
  cinfo->output_scanline += rows - rows_left;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  read_and_discard_scanlines(cinfo, rows_left);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Skips some scanlines of data from the JPEG decompressor.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * The return value will be the number of lines actually skipped.  If skipping
shun-iwasawa 82a8f5
 * num_lines would move beyond the end of the image, then the actual number of
shun-iwasawa 82a8f5
 * lines remaining in the image is returned.  Otherwise, the return value will
shun-iwasawa 82a8f5
 * be equal to num_lines.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Refer to libjpeg.txt for more information.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(JDIMENSION)
shun-iwasawa 82a8f5
jpeg_skip_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
shun-iwasawa 82a8f5
  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
shun-iwasawa 82a8f5
  my_master_ptr master = (my_master_ptr)cinfo->master;
shun-iwasawa 82a8f5
  my_upsample_ptr upsample = (my_upsample_ptr)cinfo->upsample;
shun-iwasawa 82a8f5
  JDIMENSION i, x;
shun-iwasawa 82a8f5
  int y;
shun-iwasawa 82a8f5
  JDIMENSION lines_per_iMCU_row, lines_left_in_iMCU_row, lines_after_iMCU_row;
shun-iwasawa 82a8f5
  JDIMENSION lines_to_skip, lines_to_read;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Two-pass color quantization is not supported. */
shun-iwasawa 82a8f5
  if (cinfo->quantize_colors && cinfo->two_pass_quantize)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_NOTIMPL);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->global_state != DSTATE_SCANNING)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Do not skip past the bottom of the image. */
shun-iwasawa 82a8f5
  if (cinfo->output_scanline + num_lines >= cinfo->output_height) {
shun-iwasawa 82a8f5
    num_lines = cinfo->output_height - cinfo->output_scanline;
shun-iwasawa 82a8f5
    cinfo->output_scanline = cinfo->output_height;
shun-iwasawa 82a8f5
    (*cinfo->inputctl->finish_input_pass) (cinfo);
shun-iwasawa 82a8f5
    cinfo->inputctl->eoi_reached = TRUE;
shun-iwasawa 82a8f5
    return num_lines;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (num_lines == 0)
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  lines_per_iMCU_row = cinfo->_min_DCT_scaled_size * cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
  lines_left_in_iMCU_row =
shun-iwasawa 82a8f5
    (lines_per_iMCU_row - (cinfo->output_scanline % lines_per_iMCU_row)) %
shun-iwasawa 82a8f5
    lines_per_iMCU_row;
shun-iwasawa 82a8f5
  lines_after_iMCU_row = num_lines - lines_left_in_iMCU_row;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Skip the lines remaining in the current iMCU row.  When upsampling
shun-iwasawa 82a8f5
   * requires context rows, we need the previous and next rows in order to read
shun-iwasawa 82a8f5
   * the current row.  This adds some complexity.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (cinfo->upsample->need_context_rows) {
shun-iwasawa 82a8f5
    /* If the skipped lines would not move us past the current iMCU row, we
shun-iwasawa 82a8f5
     * read the lines and ignore them.  There might be a faster way of doing
shun-iwasawa 82a8f5
     * this, but we are facing increasing complexity for diminishing returns.
shun-iwasawa 82a8f5
     * The increasing complexity would be a by-product of meddling with the
shun-iwasawa 82a8f5
     * state machine used to skip context rows.  Near the end of an iMCU row,
shun-iwasawa 82a8f5
     * the next iMCU row may have already been entropy-decoded.  In this unique
shun-iwasawa 82a8f5
     * case, we will read the next iMCU row if we cannot skip past it as well.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    if ((num_lines < lines_left_in_iMCU_row + 1) ||
shun-iwasawa 82a8f5
        (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full &&
shun-iwasawa 82a8f5
         lines_after_iMCU_row < lines_per_iMCU_row + 1)) {
shun-iwasawa 82a8f5
      read_and_discard_scanlines(cinfo, num_lines);
shun-iwasawa 82a8f5
      return num_lines;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* If the next iMCU row has already been entropy-decoded, make sure that
shun-iwasawa 82a8f5
     * we do not skip too far.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    if (lines_left_in_iMCU_row <= 1 && main_ptr->buffer_full) {
shun-iwasawa 82a8f5
      cinfo->output_scanline += lines_left_in_iMCU_row + lines_per_iMCU_row;
shun-iwasawa 82a8f5
      lines_after_iMCU_row -= lines_per_iMCU_row;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      cinfo->output_scanline += lines_left_in_iMCU_row;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* If we have just completed the first block, adjust the buffer pointers */
shun-iwasawa 82a8f5
    if (main_ptr->iMCU_row_ctr == 0 ||
shun-iwasawa 82a8f5
        (main_ptr->iMCU_row_ctr == 1 && lines_left_in_iMCU_row > 2))
shun-iwasawa 82a8f5
      set_wraparound_pointers(cinfo);
shun-iwasawa 82a8f5
    main_ptr->buffer_full = FALSE;
shun-iwasawa 82a8f5
    main_ptr->rowgroup_ctr = 0;
shun-iwasawa 82a8f5
    main_ptr->context_state = CTX_PREPARE_FOR_IMCU;
shun-iwasawa 82a8f5
    if (!master->using_merged_upsample) {
shun-iwasawa 82a8f5
      upsample->next_row_out = cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Skipping is much simpler when context rows are not required. */
shun-iwasawa 82a8f5
  else {
shun-iwasawa 82a8f5
    if (num_lines < lines_left_in_iMCU_row) {
shun-iwasawa 82a8f5
      increment_simple_rowgroup_ctr(cinfo, num_lines);
shun-iwasawa 82a8f5
      return num_lines;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      cinfo->output_scanline += lines_left_in_iMCU_row;
shun-iwasawa 82a8f5
      main_ptr->buffer_full = FALSE;
shun-iwasawa 82a8f5
      main_ptr->rowgroup_ctr = 0;
shun-iwasawa 82a8f5
      if (!master->using_merged_upsample) {
shun-iwasawa 82a8f5
        upsample->next_row_out = cinfo->max_v_samp_factor;
shun-iwasawa 82a8f5
        upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Calculate how many full iMCU rows we can skip. */
shun-iwasawa 82a8f5
  if (cinfo->upsample->need_context_rows)
shun-iwasawa 82a8f5
    lines_to_skip = ((lines_after_iMCU_row - 1) / lines_per_iMCU_row) *
shun-iwasawa 82a8f5
                    lines_per_iMCU_row;
shun-iwasawa 82a8f5
  else
shun-iwasawa 82a8f5
    lines_to_skip = (lines_after_iMCU_row / lines_per_iMCU_row) *
shun-iwasawa 82a8f5
                    lines_per_iMCU_row;
shun-iwasawa 82a8f5
  /* Calculate the number of lines that remain to be skipped after skipping all
shun-iwasawa 82a8f5
   * of the full iMCU rows that we can.  We will not read these lines unless we
shun-iwasawa 82a8f5
   * have to.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  lines_to_read = lines_after_iMCU_row - lines_to_skip;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* For images requiring multiple scans (progressive, non-interleaved, etc.),
shun-iwasawa 82a8f5
   * all of the entropy decoding occurs in jpeg_start_decompress(), assuming
shun-iwasawa 82a8f5
   * that the input data source is non-suspending.  This makes skipping easy.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (cinfo->inputctl->has_multiple_scans) {
shun-iwasawa 82a8f5
    if (cinfo->upsample->need_context_rows) {
shun-iwasawa 82a8f5
      cinfo->output_scanline += lines_to_skip;
shun-iwasawa 82a8f5
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
shun-iwasawa 82a8f5
      main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
shun-iwasawa 82a8f5
      /* It is complex to properly move to the middle of a context block, so
shun-iwasawa 82a8f5
       * read the remaining lines instead of skipping them.
shun-iwasawa 82a8f5
       */
shun-iwasawa 82a8f5
      read_and_discard_scanlines(cinfo, lines_to_read);
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      cinfo->output_scanline += lines_to_skip;
shun-iwasawa 82a8f5
      cinfo->output_iMCU_row += lines_to_skip / lines_per_iMCU_row;
shun-iwasawa 82a8f5
      increment_simple_rowgroup_ctr(cinfo, lines_to_read);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (!master->using_merged_upsample)
shun-iwasawa 82a8f5
      upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
shun-iwasawa 82a8f5
    return num_lines;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Skip the iMCU rows that we can safely skip. */
shun-iwasawa 82a8f5
  for (i = 0; i < lines_to_skip; i += lines_per_iMCU_row) {
shun-iwasawa 82a8f5
    for (y = 0; y < coef->MCU_rows_per_iMCU_row; y++) {
shun-iwasawa 82a8f5
      for (x = 0; x < cinfo->MCUs_per_row; x++) {
shun-iwasawa 82a8f5
        /* Calling decode_mcu() with a NULL pointer causes it to discard the
shun-iwasawa 82a8f5
         * decoded coefficients.  This is ~5% faster for large subsets, but
shun-iwasawa 82a8f5
         * it's tough to tell a difference for smaller images.
shun-iwasawa 82a8f5
         */
shun-iwasawa 82a8f5
        (*cinfo->entropy->decode_mcu) (cinfo, NULL);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    cinfo->input_iMCU_row++;
shun-iwasawa 82a8f5
    cinfo->output_iMCU_row++;
shun-iwasawa 82a8f5
    if (cinfo->input_iMCU_row < cinfo->total_iMCU_rows)
shun-iwasawa 82a8f5
      start_iMCU_row(cinfo);
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      (*cinfo->inputctl->finish_input_pass) (cinfo);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  cinfo->output_scanline += lines_to_skip;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->upsample->need_context_rows) {
shun-iwasawa 82a8f5
    /* Context-based upsampling keeps track of iMCU rows. */
shun-iwasawa 82a8f5
    main_ptr->iMCU_row_ctr += lines_to_skip / lines_per_iMCU_row;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* It is complex to properly move to the middle of a context block, so
shun-iwasawa 82a8f5
     * read the remaining lines instead of skipping them.
shun-iwasawa 82a8f5
     */
shun-iwasawa 82a8f5
    read_and_discard_scanlines(cinfo, lines_to_read);
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    increment_simple_rowgroup_ctr(cinfo, lines_to_read);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Since skipping lines involves skipping the upsampling step, the value of
shun-iwasawa 82a8f5
   * "rows_to_go" will become invalid unless we set it here.  NOTE: This is a
shun-iwasawa 82a8f5
   * bit odd, since "rows_to_go" seems to be redundantly keeping track of
shun-iwasawa 82a8f5
   * output_scanline.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (!master->using_merged_upsample)
shun-iwasawa 82a8f5
    upsample->rows_to_go = cinfo->output_height - cinfo->output_scanline;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Always skip the requested number of lines. */
shun-iwasawa 82a8f5
  return num_lines;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Alternate entry point to read raw data.
shun-iwasawa 82a8f5
 * Processes exactly one iMCU row per call, unless suspended.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(JDIMENSION)
shun-iwasawa 82a8f5
jpeg_read_raw_data(j_decompress_ptr cinfo, JSAMPIMAGE data,
shun-iwasawa 82a8f5
                   JDIMENSION max_lines)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JDIMENSION lines_per_iMCU_row;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->global_state != DSTATE_RAW_OK)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
  if (cinfo->output_scanline >= cinfo->output_height) {
shun-iwasawa 82a8f5
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
shun-iwasawa 82a8f5
    return 0;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Call progress monitor hook if present */
shun-iwasawa 82a8f5
  if (cinfo->progress != NULL) {
shun-iwasawa 82a8f5
    cinfo->progress->pass_counter = (long)cinfo->output_scanline;
shun-iwasawa 82a8f5
    cinfo->progress->pass_limit = (long)cinfo->output_height;
shun-iwasawa 82a8f5
    (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Verify that at least one iMCU row can be returned. */
shun-iwasawa 82a8f5
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
shun-iwasawa 82a8f5
  if (max_lines < lines_per_iMCU_row)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Decompress directly into user's buffer. */
shun-iwasawa 82a8f5
  if (!(*cinfo->coef->decompress_data) (cinfo, data))
shun-iwasawa 82a8f5
    return 0;                   /* suspension forced, can do nothing more */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* OK, we processed one iMCU row. */
shun-iwasawa 82a8f5
  cinfo->output_scanline += lines_per_iMCU_row;
shun-iwasawa 82a8f5
  return lines_per_iMCU_row;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Additional entry points for buffered-image mode. */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef D_MULTISCAN_FILES_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Initialize for an output pass in buffered-image mode.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(boolean)
shun-iwasawa 82a8f5
jpeg_start_output(j_decompress_ptr cinfo, int scan_number)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if (cinfo->global_state != DSTATE_BUFIMAGE &&
shun-iwasawa 82a8f5
      cinfo->global_state != DSTATE_PRESCAN)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
  /* Limit scan number to valid range */
shun-iwasawa 82a8f5
  if (scan_number <= 0)
shun-iwasawa 82a8f5
    scan_number = 1;
shun-iwasawa 82a8f5
  if (cinfo->inputctl->eoi_reached && scan_number > cinfo->input_scan_number)
shun-iwasawa 82a8f5
    scan_number = cinfo->input_scan_number;
shun-iwasawa 82a8f5
  cinfo->output_scan_number = scan_number;
shun-iwasawa 82a8f5
  /* Perform any dummy output passes, and set up for the real pass */
shun-iwasawa 82a8f5
  return output_pass_setup(cinfo);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Finish up after an output pass in buffered-image mode.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Returns FALSE if suspended.  The return value need be inspected only if
shun-iwasawa 82a8f5
 * a suspending data source is used.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(boolean)
shun-iwasawa 82a8f5
jpeg_finish_output(j_decompress_ptr cinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  if ((cinfo->global_state == DSTATE_SCANNING ||
shun-iwasawa 82a8f5
       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
shun-iwasawa 82a8f5
    /* Terminate this pass. */
shun-iwasawa 82a8f5
    /* We do not require the whole pass to have been completed. */
shun-iwasawa 82a8f5
    (*cinfo->master->finish_output_pass) (cinfo);
shun-iwasawa 82a8f5
    cinfo->global_state = DSTATE_BUFPOST;
shun-iwasawa 82a8f5
  } else if (cinfo->global_state != DSTATE_BUFPOST) {
shun-iwasawa 82a8f5
    /* BUFPOST = repeat call after a suspension, anything else is error */
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* Read markers looking for SOS or EOI */
shun-iwasawa 82a8f5
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
shun-iwasawa 82a8f5
         !cinfo->inputctl->eoi_reached) {
shun-iwasawa 82a8f5
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
shun-iwasawa 82a8f5
      return FALSE;             /* Suspend, come back later */
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  cinfo->global_state = DSTATE_BUFIMAGE;
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* D_MULTISCAN_FILES_SUPPORTED */