7 * Copyright (C) 1995, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
11 * For conditions of distribution and use, see the accompanying README file.
15 * This file contains library routines for transcoding decompression,
17 * that is, reading raw DCT coefficient arrays from an input JPEG file.
19 * The routines in jdapimin.c will also be needed by a transcoder.
25 #define JPEG_INTERNALS
29 #include "radiant_jpeglib.h"
35 /* Forward declarations */
37 LOCAL void transdecode_master_selection JPP((j_decompress_ptr cinfo));
45 * Read the coefficient arrays from a JPEG file.
47 * jpeg_read_header must be completed before calling this.
51 * The entire image is read into a set of virtual coefficient-block arrays,
53 * one per component. The return value is a pointer to the array of
55 * virtual-array descriptors. These can be manipulated directly via the
57 * JPEG memory manager, or handed off to jpeg_write_coefficients().
59 * To release the memory occupied by the virtual arrays, call
61 * jpeg_finish_decompress() when done with the data.
65 * Returns NULL if suspended. This case need be checked only if
67 * a suspending data source is used.
73 GLOBAL jvirt_barray_ptr *
75 jpeg_read_coefficients (j_decompress_ptr cinfo)
79 if (cinfo->global_state == DSTATE_READY) {
81 /* First call: initialize active modules */
83 transdecode_master_selection(cinfo);
85 cinfo->global_state = DSTATE_RDCOEFS;
87 } else if (cinfo->global_state != DSTATE_RDCOEFS)
89 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
91 /* Absorb whole file into the coef buffer */
97 /* Call progress monitor hook if present */
99 if (cinfo->progress != NULL)
101 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
103 /* Absorb some more input */
105 retcode = (*cinfo->inputctl->consume_input) (cinfo);
107 if (retcode == JPEG_SUSPENDED)
111 if (retcode == JPEG_REACHED_EOI)
115 /* Advance progress counter if appropriate */
117 if (cinfo->progress != NULL &&
119 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
121 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
123 /* startup underestimated number of scans; ratchet up one scan */
125 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
133 /* Set state so that jpeg_finish_decompress does the right thing */
135 cinfo->global_state = DSTATE_STOPPING;
137 return cinfo->coef->coef_arrays;
147 * Master selection of decompression modules for transcoding.
149 * This substitutes for jdmaster.c's initialization of the full decompressor.
157 transdecode_master_selection (j_decompress_ptr cinfo)
161 /* Entropy decoding: either Huffman or arithmetic coding. */
163 if (cinfo->arith_code) {
165 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
169 if (cinfo->progressive_mode) {
171 #ifdef D_PROGRESSIVE_SUPPORTED
173 jinit_phuff_decoder(cinfo);
177 ERREXIT(cinfo, JERR_NOT_COMPILED);
183 jinit_huff_decoder(cinfo);
189 /* Always get a full-image coefficient buffer. */
191 jinit_d_coef_controller(cinfo, TRUE);
195 /* We can now tell the memory manager to allocate virtual arrays. */
197 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
201 /* Initialize input side of decompressor to consume first scan. */
203 (*cinfo->inputctl->start_input_pass) (cinfo);
207 /* Initialize progress monitoring. */
209 if (cinfo->progress != NULL) {
213 /* Estimate number of scans to set pass_limit. */
215 if (cinfo->progressive_mode) {
217 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
219 nscans = 2 + 3 * cinfo->num_components;
221 } else if (cinfo->inputctl->has_multiple_scans) {
223 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
225 nscans = cinfo->num_components;
233 cinfo->progress->pass_counter = 0L;
235 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
237 cinfo->progress->completed_passes = 0;
239 cinfo->progress->total_passes = 1;