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