Path: blob/master/src/java.desktop/share/native/libjavajpeg/jdtrans.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jdtrans.c6*7* Copyright (C) 1995-1997, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains library routines for transcoding decompression,12* that is, reading raw DCT coefficient arrays from an input JPEG file.13* The routines in jdapimin.c will also be needed by a transcoder.14*/1516#define JPEG_INTERNALS17#include "jinclude.h"18#include "jpeglib.h"192021/* Forward declarations */22LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));232425/*26* Read the coefficient arrays from a JPEG file.27* jpeg_read_header must be completed before calling this.28*29* The entire image is read into a set of virtual coefficient-block arrays,30* one per component. The return value is a pointer to the array of31* virtual-array descriptors. These can be manipulated directly via the32* JPEG memory manager, or handed off to jpeg_write_coefficients().33* To release the memory occupied by the virtual arrays, call34* jpeg_finish_decompress() when done with the data.35*36* An alternative usage is to simply obtain access to the coefficient arrays37* during a buffered-image-mode decompression operation. This is allowed38* after any jpeg_finish_output() call. The arrays can be accessed until39* jpeg_finish_decompress() is called. (Note that any call to the library40* may reposition the arrays, so don't rely on access_virt_barray() results41* to stay valid across library calls.)42*43* Returns NULL if suspended. This case need be checked only if44* a suspending data source is used.45*/4647GLOBAL(jvirt_barray_ptr *)48jpeg_read_coefficients (j_decompress_ptr cinfo)49{50if (cinfo->global_state == DSTATE_READY) {51/* First call: initialize active modules */52transdecode_master_selection(cinfo);53cinfo->global_state = DSTATE_RDCOEFS;54}55if (cinfo->global_state == DSTATE_RDCOEFS) {56/* Absorb whole file into the coef buffer */57for (;;) {58int retcode;59/* Call progress monitor hook if present */60if (cinfo->progress != NULL)61(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);62/* Absorb some more input */63retcode = (*cinfo->inputctl->consume_input) (cinfo);64if (retcode == JPEG_SUSPENDED)65return NULL;66if (retcode == JPEG_REACHED_EOI)67break;68/* Advance progress counter if appropriate */69if (cinfo->progress != NULL &&70(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {71if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {72/* startup underestimated number of scans; ratchet up one scan */73cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;74}75}76}77/* Set state so that jpeg_finish_decompress does the right thing */78cinfo->global_state = DSTATE_STOPPING;79}80/* At this point we should be in state DSTATE_STOPPING if being used81* standalone, or in state DSTATE_BUFIMAGE if being invoked to get access82* to the coefficients during a full buffered-image-mode decompression.83*/84if ((cinfo->global_state == DSTATE_STOPPING ||85cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {86return cinfo->coef->coef_arrays;87}88/* Oops, improper usage */89ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);90return NULL; /* keep compiler happy */91}929394/*95* Master selection of decompression modules for transcoding.96* This substitutes for jdmaster.c's initialization of the full decompressor.97*/9899LOCAL(void)100transdecode_master_selection (j_decompress_ptr cinfo)101{102/* This is effectively a buffered-image operation. */103cinfo->buffered_image = TRUE;104105/* Entropy decoding: either Huffman or arithmetic coding. */106if (cinfo->arith_code) {107ERREXIT(cinfo, JERR_ARITH_NOTIMPL);108} else {109if (cinfo->progressive_mode) {110#ifdef D_PROGRESSIVE_SUPPORTED111jinit_phuff_decoder(cinfo);112#else113ERREXIT(cinfo, JERR_NOT_COMPILED);114#endif115} else116jinit_huff_decoder(cinfo);117}118119/* Always get a full-image coefficient buffer. */120jinit_d_coef_controller(cinfo, TRUE);121122/* We can now tell the memory manager to allocate virtual arrays. */123(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);124125/* Initialize input side of decompressor to consume first scan. */126(*cinfo->inputctl->start_input_pass) (cinfo);127128/* Initialize progress monitoring. */129if (cinfo->progress != NULL) {130int nscans;131/* Estimate number of scans to set pass_limit. */132if (cinfo->progressive_mode) {133/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */134nscans = 2 + 3 * cinfo->num_components;135} else if (cinfo->inputctl->has_multiple_scans) {136/* For a nonprogressive multiscan file, estimate 1 scan per component. */137nscans = cinfo->num_components;138} else {139nscans = 1;140}141cinfo->progress->pass_counter = 0L;142cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;143cinfo->progress->completed_passes = 0;144cinfo->progress->total_passes = 1;145}146}147148149