nuclear@2: /* nuclear@2: * jdtrans.c nuclear@2: * nuclear@2: * Copyright (C) 1995-1997, Thomas G. Lane. nuclear@2: * This file is part of the Independent JPEG Group's software. nuclear@2: * For conditions of distribution and use, see the accompanying README file. nuclear@2: * nuclear@2: * This file contains library routines for transcoding decompression, nuclear@2: * that is, reading raw DCT coefficient arrays from an input JPEG file. nuclear@2: * The routines in jdapimin.c will also be needed by a transcoder. nuclear@2: */ nuclear@2: nuclear@2: #define JPEG_INTERNALS nuclear@2: #include "jinclude.h" nuclear@2: #include "jpeglib.h" nuclear@2: nuclear@2: nuclear@2: /* Forward declarations */ nuclear@2: LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo)); nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Read the coefficient arrays from a JPEG file. nuclear@2: * jpeg_read_header must be completed before calling this. nuclear@2: * nuclear@2: * The entire image is read into a set of virtual coefficient-block arrays, nuclear@2: * one per component. The return value is a pointer to the array of nuclear@2: * virtual-array descriptors. These can be manipulated directly via the nuclear@2: * JPEG memory manager, or handed off to jpeg_write_coefficients(). nuclear@2: * To release the memory occupied by the virtual arrays, call nuclear@2: * jpeg_finish_decompress() when done with the data. nuclear@2: * nuclear@2: * An alternative usage is to simply obtain access to the coefficient arrays nuclear@2: * during a buffered-image-mode decompression operation. This is allowed nuclear@2: * after any jpeg_finish_output() call. The arrays can be accessed until nuclear@2: * jpeg_finish_decompress() is called. (Note that any call to the library nuclear@2: * may reposition the arrays, so don't rely on access_virt_barray() results nuclear@2: * to stay valid across library calls.) nuclear@2: * nuclear@2: * Returns NULL if suspended. This case need be checked only if nuclear@2: * a suspending data source is used. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(jvirt_barray_ptr *) nuclear@2: jpeg_read_coefficients (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: if (cinfo->global_state == DSTATE_READY) { nuclear@2: /* First call: initialize active modules */ nuclear@2: transdecode_master_selection(cinfo); nuclear@2: cinfo->global_state = DSTATE_RDCOEFS; nuclear@2: } nuclear@2: if (cinfo->global_state == DSTATE_RDCOEFS) { nuclear@2: /* Absorb whole file into the coef buffer */ nuclear@2: for (;;) { nuclear@2: int retcode; nuclear@2: /* Call progress monitor hook if present */ nuclear@2: if (cinfo->progress != NULL) nuclear@2: (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); nuclear@2: /* Absorb some more input */ nuclear@2: retcode = (*cinfo->inputctl->consume_input) (cinfo); nuclear@2: if (retcode == JPEG_SUSPENDED) nuclear@2: return NULL; nuclear@2: if (retcode == JPEG_REACHED_EOI) nuclear@2: break; nuclear@2: /* Advance progress counter if appropriate */ nuclear@2: if (cinfo->progress != NULL && nuclear@2: (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { nuclear@2: if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { nuclear@2: /* startup underestimated number of scans; ratchet up one scan */ nuclear@2: cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: /* Set state so that jpeg_finish_decompress does the right thing */ nuclear@2: cinfo->global_state = DSTATE_STOPPING; nuclear@2: } nuclear@2: /* At this point we should be in state DSTATE_STOPPING if being used nuclear@2: * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access nuclear@2: * to the coefficients during a full buffered-image-mode decompression. nuclear@2: */ nuclear@2: if ((cinfo->global_state == DSTATE_STOPPING || nuclear@2: cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { nuclear@2: return cinfo->coef->coef_arrays; nuclear@2: } nuclear@2: /* Oops, improper usage */ nuclear@2: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); nuclear@2: return NULL; /* keep compiler happy */ nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Master selection of decompression modules for transcoding. nuclear@2: * This substitutes for jdmaster.c's initialization of the full decompressor. nuclear@2: */ nuclear@2: nuclear@2: LOCAL(void) nuclear@2: transdecode_master_selection (j_decompress_ptr cinfo) nuclear@2: { nuclear@2: /* This is effectively a buffered-image operation. */ nuclear@2: cinfo->buffered_image = TRUE; nuclear@2: nuclear@2: /* Entropy decoding: either Huffman or arithmetic coding. */ nuclear@2: if (cinfo->arith_code) { nuclear@2: ERREXIT(cinfo, JERR_ARITH_NOTIMPL); nuclear@2: } else { nuclear@2: if (cinfo->progressive_mode) { nuclear@2: #ifdef D_PROGRESSIVE_SUPPORTED nuclear@2: jinit_phuff_decoder(cinfo); nuclear@2: #else nuclear@2: ERREXIT(cinfo, JERR_NOT_COMPILED); nuclear@2: #endif nuclear@2: } else nuclear@2: jinit_huff_decoder(cinfo); nuclear@2: } nuclear@2: nuclear@2: /* Always get a full-image coefficient buffer. */ nuclear@2: jinit_d_coef_controller(cinfo, TRUE); nuclear@2: nuclear@2: /* We can now tell the memory manager to allocate virtual arrays. */ nuclear@2: (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); nuclear@2: nuclear@2: /* Initialize input side of decompressor to consume first scan. */ nuclear@2: (*cinfo->inputctl->start_input_pass) (cinfo); nuclear@2: nuclear@2: /* Initialize progress monitoring. */ nuclear@2: if (cinfo->progress != NULL) { nuclear@2: int nscans; nuclear@2: /* Estimate number of scans to set pass_limit. */ nuclear@2: if (cinfo->progressive_mode) { nuclear@2: /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ nuclear@2: nscans = 2 + 3 * cinfo->num_components; nuclear@2: } else if (cinfo->inputctl->has_multiple_scans) { nuclear@2: /* For a nonprogressive multiscan file, estimate 1 scan per component. */ nuclear@2: nscans = cinfo->num_components; nuclear@2: } else { nuclear@2: nscans = 1; nuclear@2: } nuclear@2: cinfo->progress->pass_counter = 0L; nuclear@2: cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; nuclear@2: cinfo->progress->completed_passes = 0; nuclear@2: cinfo->progress->total_passes = 1; nuclear@2: } nuclear@2: }