/*1* jdtrans.c2*3* Copyright (C) 1995-1997, Thomas G. Lane.4* Modified 2000-2009 by Guido Vollbeding.5* This file is part of the Independent JPEG Group's software.6* For conditions of distribution and use, see the accompanying README file.7*8* This file contains library routines for transcoding decompression,9* that is, reading raw DCT coefficient arrays from an input JPEG file.10* The routines in jdapimin.c will also be needed by a transcoder.11*/1213#define JPEG_INTERNALS14#include "jinclude.h"15#include "jpeglib.h"161718/* Forward declarations */19LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));202122/*23* Read the coefficient arrays from a JPEG file.24* jpeg_read_header must be completed before calling this.25*26* The entire image is read into a set of virtual coefficient-block arrays,27* one per component. The return value is a pointer to the array of28* virtual-array descriptors. These can be manipulated directly via the29* JPEG memory manager, or handed off to jpeg_write_coefficients().30* To release the memory occupied by the virtual arrays, call31* jpeg_finish_decompress() when done with the data.32*33* An alternative usage is to simply obtain access to the coefficient arrays34* during a buffered-image-mode decompression operation. This is allowed35* after any jpeg_finish_output() call. The arrays can be accessed until36* jpeg_finish_decompress() is called. (Note that any call to the library37* may reposition the arrays, so don't rely on access_virt_barray() results38* to stay valid across library calls.)39*40* Returns NULL if suspended. This case need be checked only if41* a suspending data source is used.42*/4344GLOBAL(jvirt_barray_ptr *)45jpeg_read_coefficients (j_decompress_ptr cinfo)46{47if (cinfo->global_state == DSTATE_READY) {48/* First call: initialize active modules */49transdecode_master_selection(cinfo);50cinfo->global_state = DSTATE_RDCOEFS;51}52if (cinfo->global_state == DSTATE_RDCOEFS) {53/* Absorb whole file into the coef buffer */54for (;;) {55int retcode;56/* Call progress monitor hook if present */57if (cinfo->progress != NULL)58(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);59/* Absorb some more input */60retcode = (*cinfo->inputctl->consume_input) (cinfo);61if (retcode == JPEG_SUSPENDED)62return NULL;63if (retcode == JPEG_REACHED_EOI)64break;65/* Advance progress counter if appropriate */66if (cinfo->progress != NULL &&67(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {68if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {69/* startup underestimated number of scans; ratchet up one scan */70cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;71}72}73}74/* Set state so that jpeg_finish_decompress does the right thing */75cinfo->global_state = DSTATE_STOPPING;76}77/* At this point we should be in state DSTATE_STOPPING if being used78* standalone, or in state DSTATE_BUFIMAGE if being invoked to get access79* to the coefficients during a full buffered-image-mode decompression.80*/81if ((cinfo->global_state == DSTATE_STOPPING ||82cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {83return cinfo->coef->coef_arrays;84}85/* Oops, improper usage */86ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);87return NULL; /* keep compiler happy */88}899091/*92* Master selection of decompression modules for transcoding.93* This substitutes for jdmaster.c's initialization of the full decompressor.94*/9596LOCAL(void)97transdecode_master_selection (j_decompress_ptr cinfo)98{99/* This is effectively a buffered-image operation. */100cinfo->buffered_image = TRUE;101102/* Compute output image dimensions and related values. */103jpeg_core_output_dimensions(cinfo);104105/* Entropy decoding: either Huffman or arithmetic coding. */106if (cinfo->arith_code)107jinit_arith_decoder(cinfo);108else {109jinit_huff_decoder(cinfo);110}111112/* Always get a full-image coefficient buffer. */113jinit_d_coef_controller(cinfo, TRUE);114115/* We can now tell the memory manager to allocate virtual arrays. */116(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);117118/* Initialize input side of decompressor to consume first scan. */119(*cinfo->inputctl->start_input_pass) (cinfo);120121/* Initialize progress monitoring. */122if (cinfo->progress != NULL) {123int nscans;124/* Estimate number of scans to set pass_limit. */125if (cinfo->progressive_mode) {126/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */127nscans = 2 + 3 * cinfo->num_components;128} else if (cinfo->inputctl->has_multiple_scans) {129/* For a nonprogressive multiscan file, estimate 1 scan per component. */130nscans = cinfo->num_components;131} else {132nscans = 1;133}134cinfo->progress->pass_counter = 0L;135cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;136cinfo->progress->completed_passes = 0;137cinfo->progress->total_passes = 1;138}139}140141142