Path: blob/master/3rdparty/libjpeg-turbo/src/jdtrans.c
16337 views
/*1* jdtrans.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1995-1997, Thomas G. Lane.5* It was modified by The libjpeg-turbo Project to include only code relevant6* to libjpeg-turbo.7* For conditions of distribution and use, see the accompanying README.ijg8* file.9*10* This file contains library routines for transcoding decompression,11* that is, reading raw DCT coefficient arrays from an input JPEG file.12* The routines in jdapimin.c will also be needed by a transcoder.13*/1415#define JPEG_INTERNALS16#include "jinclude.h"17#include "jpeglib.h"181920/* Forward declarations */21LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo);222324/*25* Read the coefficient arrays from a JPEG file.26* jpeg_read_header must be completed before calling this.27*28* The entire image is read into a set of virtual coefficient-block arrays,29* one per component. The return value is a pointer to the array of30* virtual-array descriptors. These can be manipulated directly via the31* JPEG memory manager, or handed off to jpeg_write_coefficients().32* To release the memory occupied by the virtual arrays, call33* jpeg_finish_decompress() when done with the data.34*35* An alternative usage is to simply obtain access to the coefficient arrays36* during a buffered-image-mode decompression operation. This is allowed37* after any jpeg_finish_output() call. The arrays can be accessed until38* jpeg_finish_decompress() is called. (Note that any call to the library39* may reposition the arrays, so don't rely on access_virt_barray() results40* to stay valid across library calls.)41*42* Returns NULL if suspended. This case need be checked only if43* a suspending data source is used.44*/4546GLOBAL(jvirt_barray_ptr *)47jpeg_read_coefficients (j_decompress_ptr cinfo)48{49if (cinfo->global_state == DSTATE_READY) {50/* First call: initialize active modules */51transdecode_master_selection(cinfo);52cinfo->global_state = DSTATE_RDCOEFS;53}54if (cinfo->global_state == DSTATE_RDCOEFS) {55/* Absorb whole file into the coef buffer */56for (;;) {57int retcode;58/* Call progress monitor hook if present */59if (cinfo->progress != NULL)60(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);61/* Absorb some more input */62retcode = (*cinfo->inputctl->consume_input) (cinfo);63if (retcode == JPEG_SUSPENDED)64return NULL;65if (retcode == JPEG_REACHED_EOI)66break;67/* Advance progress counter if appropriate */68if (cinfo->progress != NULL &&69(retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {70if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {71/* startup underestimated number of scans; ratchet up one scan */72cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;73}74}75}76/* Set state so that jpeg_finish_decompress does the right thing */77cinfo->global_state = DSTATE_STOPPING;78}79/* At this point we should be in state DSTATE_STOPPING if being used80* standalone, or in state DSTATE_BUFIMAGE if being invoked to get access81* to the coefficients during a full buffered-image-mode decompression.82*/83if ((cinfo->global_state == DSTATE_STOPPING ||84cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {85return cinfo->coef->coef_arrays;86}87/* Oops, improper usage */88ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);89return NULL; /* keep compiler happy */90}919293/*94* Master selection of decompression modules for transcoding.95* This substitutes for jdmaster.c's initialization of the full decompressor.96*/9798LOCAL(void)99transdecode_master_selection (j_decompress_ptr cinfo)100{101/* This is effectively a buffered-image operation. */102cinfo->buffered_image = TRUE;103104#if JPEG_LIB_VERSION >= 80105/* Compute output image dimensions and related values. */106jpeg_core_output_dimensions(cinfo);107#endif108109/* Entropy decoding: either Huffman or arithmetic coding. */110if (cinfo->arith_code) {111#ifdef D_ARITH_CODING_SUPPORTED112jinit_arith_decoder(cinfo);113#else114ERREXIT(cinfo, JERR_ARITH_NOTIMPL);115#endif116} else {117if (cinfo->progressive_mode) {118#ifdef D_PROGRESSIVE_SUPPORTED119jinit_phuff_decoder(cinfo);120#else121ERREXIT(cinfo, JERR_NOT_COMPILED);122#endif123} else124jinit_huff_decoder(cinfo);125}126127/* Always get a full-image coefficient buffer. */128jinit_d_coef_controller(cinfo, TRUE);129130/* We can now tell the memory manager to allocate virtual arrays. */131(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);132133/* Initialize input side of decompressor to consume first scan. */134(*cinfo->inputctl->start_input_pass) (cinfo);135136/* Initialize progress monitoring. */137if (cinfo->progress != NULL) {138int nscans;139/* Estimate number of scans to set pass_limit. */140if (cinfo->progressive_mode) {141/* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */142nscans = 2 + 3 * cinfo->num_components;143} else if (cinfo->inputctl->has_multiple_scans) {144/* For a nonprogressive multiscan file, estimate 1 scan per component. */145nscans = cinfo->num_components;146} else {147nscans = 1;148}149cinfo->progress->pass_counter = 0L;150cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;151cinfo->progress->completed_passes = 0;152cinfo->progress->total_passes = 1;153}154}155156157