kusano 7d535a
/*
kusano 7d535a
 * jdapistd.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
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 application interface code for the decompression half
kusano 7d535a
 * of the JPEG library.  These are the "standard" API routines that are
kusano 7d535a
 * used in the normal full-decompression case.  They are not used by a
kusano 7d535a
 * transcoding-only application.  Note that if an application links in
kusano 7d535a
 * jpeg_start_decompress, it will end up linking in the entire decompressor.
kusano 7d535a
 * We thus must separate this file from jdapimin.c to avoid linking the
kusano 7d535a
 * whole decompression library into a transcoder.
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
/* Forward declarations */
kusano 7d535a
LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Decompression initialization.
kusano 7d535a
 * jpeg_read_header must be completed before calling this.
kusano 7d535a
 *
kusano 7d535a
 * If a multipass operating mode was selected, this will do all but the
kusano 7d535a
 * last pass, and thus may take a great deal of time.
kusano 7d535a
 *
kusano 7d535a
 * Returns FALSE if suspended.  The return value need be inspected only if
kusano 7d535a
 * a suspending data source is used.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jpeg_start_decompress (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  if (cinfo->global_state == DSTATE_READY) {
kusano 7d535a
    /* First call: initialize master control, select active modules */
kusano 7d535a
    jinit_master_decompress(cinfo);
kusano 7d535a
    if (cinfo->buffered_image) {
kusano 7d535a
      /* No more work here; expecting jpeg_start_output next */
kusano 7d535a
      cinfo->global_state = DSTATE_BUFIMAGE;
kusano 7d535a
      return TRUE;
kusano 7d535a
    }
kusano 7d535a
    cinfo->global_state = DSTATE_PRELOAD;
kusano 7d535a
  }
kusano 7d535a
  if (cinfo->global_state == DSTATE_PRELOAD) {
kusano 7d535a
    /* If file has multiple scans, absorb them all into the coef buffer */
kusano 7d535a
    if (cinfo->inputctl->has_multiple_scans) {
kusano 7d535a
#ifdef D_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
      for (;;) {
kusano 7d535a
	int retcode;
kusano 7d535a
	/* Call progress monitor hook if present */
kusano 7d535a
	if (cinfo->progress != NULL)
kusano 7d535a
	  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
	/* Absorb some more input */
kusano 7d535a
	retcode = (*cinfo->inputctl->consume_input) (cinfo);
kusano 7d535a
	if (retcode == JPEG_SUSPENDED)
kusano 7d535a
	  return FALSE;
kusano 7d535a
	if (retcode == JPEG_REACHED_EOI)
kusano 7d535a
	  break;
kusano 7d535a
	/* Advance progress counter if appropriate */
kusano 7d535a
	if (cinfo->progress != NULL &&
kusano 7d535a
	    (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
kusano 7d535a
	  if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
kusano 7d535a
	    /* jdmaster underestimated number of scans; ratchet up one scan */
kusano 7d535a
	    cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
kusano 7d535a
	  }
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
#else
kusano 7d535a
      ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
#endif /* D_MULTISCAN_FILES_SUPPORTED */
kusano 7d535a
    }
kusano 7d535a
    cinfo->output_scan_number = cinfo->input_scan_number;
kusano 7d535a
  } else if (cinfo->global_state != DSTATE_PRESCAN)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  /* Perform any dummy output passes, and set up for the final pass */
kusano 7d535a
  return output_pass_setup(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Set up for an output pass, and perform any dummy pass(es) needed.
kusano 7d535a
 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
kusano 7d535a
 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
kusano 7d535a
 * Exit: If done, returns TRUE and sets global_state for proper output mode.
kusano 7d535a
 *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(boolean)
kusano 7d535a
output_pass_setup (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  if (cinfo->global_state != DSTATE_PRESCAN) {
kusano 7d535a
    /* First call: do pass setup */
kusano 7d535a
    (*cinfo->master->prepare_for_output_pass) (cinfo);
kusano 7d535a
    cinfo->output_scanline = 0;
kusano 7d535a
    cinfo->global_state = DSTATE_PRESCAN;
kusano 7d535a
  }
kusano 7d535a
  /* Loop over any required dummy passes */
kusano 7d535a
  while (cinfo->master->is_dummy_pass) {
kusano 7d535a
#ifdef QUANT_2PASS_SUPPORTED
kusano 7d535a
    /* Crank through the dummy pass */
kusano 7d535a
    while (cinfo->output_scanline < cinfo->output_height) {
kusano 7d535a
      JDIMENSION last_scanline;
kusano 7d535a
      /* Call progress monitor hook if present */
kusano 7d535a
      if (cinfo->progress != NULL) {
kusano 7d535a
	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
kusano 7d535a
	cinfo->progress->pass_limit = (long) cinfo->output_height;
kusano 7d535a
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
      }
kusano 7d535a
      /* Process some data */
kusano 7d535a
      last_scanline = cinfo->output_scanline;
kusano 7d535a
      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
kusano 7d535a
				    &cinfo->output_scanline, (JDIMENSION) 0);
kusano 7d535a
      if (cinfo->output_scanline == last_scanline)
kusano 7d535a
	return FALSE;		/* No progress made, must suspend */
kusano 7d535a
    }
kusano 7d535a
    /* Finish up dummy pass, and set up for another one */
kusano 7d535a
    (*cinfo->master->finish_output_pass) (cinfo);
kusano 7d535a
    (*cinfo->master->prepare_for_output_pass) (cinfo);
kusano 7d535a
    cinfo->output_scanline = 0;
kusano 7d535a
#else
kusano 7d535a
    ERREXIT(cinfo, JERR_NOT_COMPILED);
kusano 7d535a
#endif /* QUANT_2PASS_SUPPORTED */
kusano 7d535a
  }
kusano 7d535a
  /* Ready for application to drive output pass through
kusano 7d535a
   * jpeg_read_scanlines or jpeg_read_raw_data.
kusano 7d535a
   */
kusano 7d535a
  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read some scanlines of data from the JPEG decompressor.
kusano 7d535a
 *
kusano 7d535a
 * The return value will be the number of lines actually read.
kusano 7d535a
 * This may be less than the number requested in several cases,
kusano 7d535a
 * including bottom of image, data source suspension, and operating
kusano 7d535a
 * modes that emit multiple scanlines at a time.
kusano 7d535a
 *
kusano 7d535a
 * Note: we warn about excess calls to jpeg_read_scanlines() since
kusano 7d535a
 * this likely signals an application programmer error.  However,
kusano 7d535a
 * an oversize buffer (max_lines > scanlines remaining) is not an error.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(JDIMENSION)
kusano 7d535a
jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
kusano 7d535a
		     JDIMENSION max_lines)
kusano 7d535a
{
kusano 7d535a
  JDIMENSION row_ctr;
kusano 7d535a
kusano 7d535a
  if (cinfo->global_state != DSTATE_SCANNING)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  if (cinfo->output_scanline >= cinfo->output_height) {
kusano 7d535a
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
kusano 7d535a
    return 0;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Call progress monitor hook if present */
kusano 7d535a
  if (cinfo->progress != NULL) {
kusano 7d535a
    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
kusano 7d535a
    cinfo->progress->pass_limit = (long) cinfo->output_height;
kusano 7d535a
    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Process some data */
kusano 7d535a
  row_ctr = 0;
kusano 7d535a
  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
kusano 7d535a
  cinfo->output_scanline += row_ctr;
kusano 7d535a
  return row_ctr;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Alternate entry point to read raw data.
kusano 7d535a
 * Processes exactly one iMCU row per call, unless suspended.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(JDIMENSION)
kusano 7d535a
jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
kusano 7d535a
		    JDIMENSION max_lines)
kusano 7d535a
{
kusano 7d535a
  JDIMENSION lines_per_iMCU_row;
kusano 7d535a
kusano 7d535a
  if (cinfo->global_state != DSTATE_RAW_OK)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  if (cinfo->output_scanline >= cinfo->output_height) {
kusano 7d535a
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
kusano 7d535a
    return 0;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Call progress monitor hook if present */
kusano 7d535a
  if (cinfo->progress != NULL) {
kusano 7d535a
    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
kusano 7d535a
    cinfo->progress->pass_limit = (long) cinfo->output_height;
kusano 7d535a
    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Verify that at least one iMCU row can be returned. */
kusano 7d535a
  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size;
kusano 7d535a
  if (max_lines < lines_per_iMCU_row)
kusano 7d535a
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
kusano 7d535a
kusano 7d535a
  /* Decompress directly into user's buffer. */
kusano 7d535a
  if (! (*cinfo->coef->decompress_data) (cinfo, data))
kusano 7d535a
    return 0;			/* suspension forced, can do nothing more */
kusano 7d535a
kusano 7d535a
  /* OK, we processed one iMCU row. */
kusano 7d535a
  cinfo->output_scanline += lines_per_iMCU_row;
kusano 7d535a
  return lines_per_iMCU_row;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Additional entry points for buffered-image mode. */
kusano 7d535a
kusano 7d535a
#ifdef D_MULTISCAN_FILES_SUPPORTED
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initialize for an output pass in buffered-image mode.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
kusano 7d535a
{
kusano 7d535a
  if (cinfo->global_state != DSTATE_BUFIMAGE &&
kusano 7d535a
      cinfo->global_state != DSTATE_PRESCAN)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  /* Limit scan number to valid range */
kusano 7d535a
  if (scan_number <= 0)
kusano 7d535a
    scan_number = 1;
kusano 7d535a
  if (cinfo->inputctl->eoi_reached &&
kusano 7d535a
      scan_number > cinfo->input_scan_number)
kusano 7d535a
    scan_number = cinfo->input_scan_number;
kusano 7d535a
  cinfo->output_scan_number = scan_number;
kusano 7d535a
  /* Perform any dummy output passes, and set up for the real pass */
kusano 7d535a
  return output_pass_setup(cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up after an output pass in buffered-image mode.
kusano 7d535a
 *
kusano 7d535a
 * Returns FALSE if suspended.  The return value need be inspected only if
kusano 7d535a
 * a suspending data source is used.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(boolean)
kusano 7d535a
jpeg_finish_output (j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  if ((cinfo->global_state == DSTATE_SCANNING ||
kusano 7d535a
       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
kusano 7d535a
    /* Terminate this pass. */
kusano 7d535a
    /* We do not require the whole pass to have been completed. */
kusano 7d535a
    (*cinfo->master->finish_output_pass) (cinfo);
kusano 7d535a
    cinfo->global_state = DSTATE_BUFPOST;
kusano 7d535a
  } else if (cinfo->global_state != DSTATE_BUFPOST) {
kusano 7d535a
    /* BUFPOST = repeat call after a suspension, anything else is error */
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  }
kusano 7d535a
  /* Read markers looking for SOS or EOI */
kusano 7d535a
  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
kusano 7d535a
	 ! cinfo->inputctl->eoi_reached) {
kusano 7d535a
    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
kusano 7d535a
      return FALSE;		/* Suspend, come back later */
kusano 7d535a
  }
kusano 7d535a
  cinfo->global_state = DSTATE_BUFIMAGE;
kusano 7d535a
  return TRUE;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* D_MULTISCAN_FILES_SUPPORTED */