0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdtrans.c
0N/A *
0N/A * Copyright (C) 1995-1997, 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 library routines for transcoding decompression,
0N/A * that is, reading raw DCT coefficient arrays from an input JPEG file.
0N/A * The routines in jdapimin.c will also be needed by 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(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
0N/A
0N/A
0N/A/*
0N/A * Read the coefficient arrays from a JPEG file.
0N/A * jpeg_read_header must be completed before calling this.
0N/A *
0N/A * The entire image is read into a set of virtual coefficient-block arrays,
0N/A * one per component. The return value is a pointer to the array of
0N/A * virtual-array descriptors. These can be manipulated directly via the
0N/A * JPEG memory manager, or handed off to jpeg_write_coefficients().
0N/A * To release the memory occupied by the virtual arrays, call
0N/A * jpeg_finish_decompress() when done with the data.
0N/A *
0N/A * An alternative usage is to simply obtain access to the coefficient arrays
0N/A * during a buffered-image-mode decompression operation. This is allowed
0N/A * after any jpeg_finish_output() call. The arrays can be accessed until
0N/A * jpeg_finish_decompress() is called. (Note that any call to the library
0N/A * may reposition the arrays, so don't rely on access_virt_barray() results
0N/A * to stay valid across library calls.)
0N/A *
0N/A * Returns NULL if suspended. This case need be checked only if
0N/A * a suspending data source is used.
0N/A */
0N/A
0N/AGLOBAL(jvirt_barray_ptr *)
0N/Ajpeg_read_coefficients (j_decompress_ptr cinfo)
0N/A{
0N/A if (cinfo->global_state == DSTATE_READY) {
0N/A /* First call: initialize active modules */
0N/A transdecode_master_selection(cinfo);
0N/A cinfo->global_state = DSTATE_RDCOEFS;
0N/A }
0N/A if (cinfo->global_state == DSTATE_RDCOEFS) {
0N/A /* Absorb whole file into the coef buffer */
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 NULL;
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 /* startup 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 /* Set state so that jpeg_finish_decompress does the right thing */
0N/A cinfo->global_state = DSTATE_STOPPING;
0N/A }
0N/A /* At this point we should be in state DSTATE_STOPPING if being used
0N/A * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
0N/A * to the coefficients during a full buffered-image-mode decompression.
0N/A */
0N/A if ((cinfo->global_state == DSTATE_STOPPING ||
0N/A cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
0N/A return cinfo->coef->coef_arrays;
0N/A }
0N/A /* Oops, improper usage */
0N/A ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
0N/A return NULL; /* keep compiler happy */
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Master selection of decompression modules for transcoding.
0N/A * This substitutes for jdmaster.c's initialization of the full decompressor.
0N/A */
0N/A
0N/ALOCAL(void)
0N/Atransdecode_master_selection (j_decompress_ptr cinfo)
0N/A{
0N/A /* This is effectively a buffered-image operation. */
0N/A cinfo->buffered_image = TRUE;
0N/A
0N/A /* Entropy decoding: either Huffman or arithmetic coding. */
0N/A if (cinfo->arith_code) {
0N/A ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
0N/A } else {
0N/A if (cinfo->progressive_mode) {
0N/A#ifdef D_PROGRESSIVE_SUPPORTED
0N/A jinit_phuff_decoder(cinfo);
0N/A#else
0N/A ERREXIT(cinfo, JERR_NOT_COMPILED);
0N/A#endif
0N/A } else
0N/A jinit_huff_decoder(cinfo);
0N/A }
0N/A
0N/A /* Always get a full-image coefficient buffer. */
0N/A jinit_d_coef_controller(cinfo, TRUE);
0N/A
0N/A /* We can now tell the memory manager to allocate virtual arrays. */
0N/A (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
0N/A
0N/A /* Initialize input side of decompressor to consume first scan. */
0N/A (*cinfo->inputctl->start_input_pass) (cinfo);
0N/A
0N/A /* Initialize progress monitoring. */
0N/A if (cinfo->progress != NULL) {
0N/A int nscans;
0N/A /* Estimate number of scans to set pass_limit. */
0N/A if (cinfo->progressive_mode) {
0N/A /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
0N/A nscans = 2 + 3 * cinfo->num_components;
0N/A } else if (cinfo->inputctl->has_multiple_scans) {
0N/A /* For a nonprogressive multiscan file, estimate 1 scan per component. */
0N/A nscans = cinfo->num_components;
0N/A } else {
0N/A nscans = 1;
0N/A }
0N/A cinfo->progress->pass_counter = 0L;
0N/A cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
0N/A cinfo->progress->completed_passes = 0;
0N/A cinfo->progress->total_passes = 1;
0N/A }
0N/A}