1bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 2bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * jdtrans.c 3bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * 45033f3e19a31e8ad40c1a79700365aefe5664494DRC * This file was part of the Independent JPEG Group's software: 55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * Copyright (C) 1995-1997, Thomas G. Lane. 65033f3e19a31e8ad40c1a79700365aefe5664494DRC * It was modified by The libjpeg-turbo Project to include only code relevant 75033f3e19a31e8ad40c1a79700365aefe5664494DRC * to libjpeg-turbo. 86eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * For conditions of distribution and use, see the accompanying README.ijg 96eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * file. 10bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * 11bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This file contains library routines for transcoding decompression, 12bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * that is, reading raw DCT coefficient arrays from an input JPEG file. 13bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * The routines in jdapimin.c will also be needed by a transcoder. 14bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 15bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 16bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#define JPEG_INTERNALS 17bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "jinclude.h" 18bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "jpeglib.h" 19bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 20bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 21bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* Forward declarations */ 22bc56b754e1a6a1db9ccadf64d6dda8a74140e1a3DRCLOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo); 23bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 24bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 25bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 26bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Read the coefficient arrays from a JPEG file. 27bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * jpeg_read_header must be completed before calling this. 28bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * 29bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * The entire image is read into a set of virtual coefficient-block arrays, 30bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * one per component. The return value is a pointer to the array of 31bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * virtual-array descriptors. These can be manipulated directly via the 32bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * JPEG memory manager, or handed off to jpeg_write_coefficients(). 33bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * To release the memory occupied by the virtual arrays, call 34bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * jpeg_finish_decompress() when done with the data. 35bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * 365ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * An alternative usage is to simply obtain access to the coefficient arrays 375ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * during a buffered-image-mode decompression operation. This is allowed 385ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * after any jpeg_finish_output() call. The arrays can be accessed until 395ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * jpeg_finish_decompress() is called. (Note that any call to the library 405ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * may reposition the arrays, so don't rely on access_virt_barray() results 415ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * to stay valid across library calls.) 425ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * 43bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Returns NULL if suspended. This case need be checked only if 44bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * a suspending data source is used. 45bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 46bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 47489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(jvirt_barray_ptr *) 48bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_read_coefficients (j_decompress_ptr cinfo) 49bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 50bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->global_state == DSTATE_READY) { 51bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* First call: initialize active modules */ 52bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane transdecode_master_selection(cinfo); 53bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->global_state = DSTATE_RDCOEFS; 545ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane } 555ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane if (cinfo->global_state == DSTATE_RDCOEFS) { 565ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* Absorb whole file into the coef buffer */ 575ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane for (;;) { 585ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane int retcode; 595ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* Call progress monitor hook if present */ 605ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane if (cinfo->progress != NULL) 61e5eaf37440b8e337ab150c017df7c03faf846c51DRC (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo); 625ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* Absorb some more input */ 635ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane retcode = (*cinfo->inputctl->consume_input) (cinfo); 645ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane if (retcode == JPEG_SUSPENDED) 65e5eaf37440b8e337ab150c017df7c03faf846c51DRC return NULL; 665ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane if (retcode == JPEG_REACHED_EOI) 67e5eaf37440b8e337ab150c017df7c03faf846c51DRC break; 685ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* Advance progress counter if appropriate */ 695ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane if (cinfo->progress != NULL && 70e5eaf37440b8e337ab150c017df7c03faf846c51DRC (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { 71e5eaf37440b8e337ab150c017df7c03faf846c51DRC if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { 72e5eaf37440b8e337ab150c017df7c03faf846c51DRC /* startup underestimated number of scans; ratchet up one scan */ 73e5eaf37440b8e337ab150c017df7c03faf846c51DRC cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows; 74e5eaf37440b8e337ab150c017df7c03faf846c51DRC } 75bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 76bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 775ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* Set state so that jpeg_finish_decompress does the right thing */ 785ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane cinfo->global_state = DSTATE_STOPPING; 79bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 805ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* At this point we should be in state DSTATE_STOPPING if being used 815ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access 825ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * to the coefficients during a full buffered-image-mode decompression. 835ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane */ 845ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane if ((cinfo->global_state == DSTATE_STOPPING || 855ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) { 865ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane return cinfo->coef->coef_arrays; 875ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane } 885ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* Oops, improper usage */ 895ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); 90e5eaf37440b8e337ab150c017df7c03faf846c51DRC return NULL; /* keep compiler happy */ 91bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 92bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 93bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 94bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/* 95bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Master selection of decompression modules for transcoding. 96bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This substitutes for jdmaster.c's initialization of the full decompressor. 97bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */ 98bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 99489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneLOCAL(void) 100bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanetransdecode_master_selection (j_decompress_ptr cinfo) 101bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{ 1025ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane /* This is effectively a buffered-image operation. */ 1035ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane cinfo->buffered_image = TRUE; 1045ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane 105df48945d2d965346c974d4e0a34a9711bdff18d4DRC#if JPEG_LIB_VERSION >= 80 106989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding /* Compute output image dimensions and related values. */ 107989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding jpeg_core_output_dimensions(cinfo); 108df48945d2d965346c974d4e0a34a9711bdff18d4DRC#endif 109989630f70cf1af69ebfefca8910d1647bf189712Guido Vollbeding 110bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Entropy decoding: either Huffman or arithmetic coding. */ 111bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->arith_code) { 11266f97e6820e2cc9ef7429ea36285c80ffda87c8fDRC#ifdef D_ARITH_CODING_SUPPORTED 1131e247ac854f8e33682bcfea475f6bccc42377208Guido Vollbeding jinit_arith_decoder(cinfo); 11466f97e6820e2cc9ef7429ea36285c80ffda87c8fDRC#else 115bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ERREXIT(cinfo, JERR_ARITH_NOTIMPL); 11666f97e6820e2cc9ef7429ea36285c80ffda87c8fDRC#endif 117bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } else { 118bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->progressive_mode) { 119bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifdef D_PROGRESSIVE_SUPPORTED 120bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane jinit_phuff_decoder(cinfo); 121bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#else 122bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane ERREXIT(cinfo, JERR_NOT_COMPILED); 123bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#endif 124bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } else 125bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane jinit_huff_decoder(cinfo); 126bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 127bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 128bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Always get a full-image coefficient buffer. */ 129bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane jinit_d_coef_controller(cinfo, TRUE); 130bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 131bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* We can now tell the memory manager to allocate virtual arrays. */ 132bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); 133bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 134bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Initialize input side of decompressor to consume first scan. */ 135bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane (*cinfo->inputctl->start_input_pass) (cinfo); 136bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane 137bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Initialize progress monitoring. */ 138bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->progress != NULL) { 139bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane int nscans; 140bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Estimate number of scans to set pass_limit. */ 141bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane if (cinfo->progressive_mode) { 142bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */ 143bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane nscans = 2 + 3 * cinfo->num_components; 144bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } else if (cinfo->inputctl->has_multiple_scans) { 145bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane /* For a nonprogressive multiscan file, estimate 1 scan per component. */ 146bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane nscans = cinfo->num_components; 147bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } else { 148bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane nscans = 1; 149bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 150bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->progress->pass_counter = 0L; 151bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans; 152bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->progress->completed_passes = 0; 153bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane cinfo->progress->total_passes = 1; 154bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane } 155bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane} 156