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