2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdtrans.c /main/2 1996/05/09 03:50:06 drk $ */
27 * Copyright (C) 1995-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains library routines for transcoding decompression,
32 * that is, reading raw DCT coefficient arrays from an input JPEG file.
33 * The routines in jdapimin.c will also be needed by a transcoder.
36 #define JPEG_INTERNALS
41 /* Forward declarations */
42 LOCAL(void) transdecode_master_selection JPP((j_decompress_ptr cinfo));
46 * Read the coefficient arrays from a JPEG file.
47 * jpeg_read_header must be completed before calling this.
49 * The entire image is read into a set of virtual coefficient-block arrays,
50 * one per component. The return value is a pointer to the array of
51 * virtual-array descriptors. These can be manipulated directly via the
52 * JPEG memory manager, or handed off to jpeg_write_coefficients().
53 * To release the memory occupied by the virtual arrays, call
54 * jpeg_finish_decompress() when done with the data.
56 * Returns NULL if suspended. This case need be checked only if
57 * a suspending data source is used.
60 GLOBAL(jvirt_barray_ptr *)
61 jpeg_read_coefficients (j_decompress_ptr cinfo)
63 if (cinfo->global_state == DSTATE_READY) {
64 /* First call: initialize active modules */
65 transdecode_master_selection(cinfo);
66 cinfo->global_state = DSTATE_RDCOEFS;
67 } else if (cinfo->global_state != DSTATE_RDCOEFS)
68 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
69 /* Absorb whole file into the coef buffer */
72 /* Call progress monitor hook if present */
73 if (cinfo->progress != NULL)
74 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
75 /* Absorb some more input */
76 retcode = (*cinfo->inputctl->consume_input) (cinfo);
77 if (retcode == JPEG_SUSPENDED)
79 if (retcode == JPEG_REACHED_EOI)
81 /* Advance progress counter if appropriate */
82 if (cinfo->progress != NULL &&
83 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
84 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
85 /* startup underestimated number of scans; ratchet up one scan */
86 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
90 /* Set state so that jpeg_finish_decompress does the right thing */
91 cinfo->global_state = DSTATE_STOPPING;
92 return cinfo->coef->coef_arrays;
97 * Master selection of decompression modules for transcoding.
98 * This substitutes for jdmaster.c's initialization of the full decompressor.
102 transdecode_master_selection (j_decompress_ptr cinfo)
104 /* Entropy decoding: either Huffman or arithmetic coding. */
105 if (cinfo->arith_code) {
106 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
108 if (cinfo->progressive_mode) {
109 #ifdef D_PROGRESSIVE_SUPPORTED
110 jinit_phuff_decoder(cinfo);
112 ERREXIT(cinfo, JERR_NOT_COMPILED);
115 jinit_huff_decoder(cinfo);
118 /* Always get a full-image coefficient buffer. */
119 jinit_d_coef_controller(cinfo, TRUE);
121 /* We can now tell the memory manager to allocate virtual arrays. */
122 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
124 /* Initialize input side of decompressor to consume first scan. */
125 (*cinfo->inputctl->start_input_pass) (cinfo);
127 /* Initialize progress monitoring. */
128 if (cinfo->progress != NULL) {
130 /* Estimate number of scans to set pass_limit. */
131 if (cinfo->progressive_mode) {
132 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
133 nscans = 2 + 3 * cinfo->num_components;
134 } else if (cinfo->inputctl->has_multiple_scans) {
135 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
136 nscans = cinfo->num_components;
140 cinfo->progress->pass_counter = 0L;
141 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
142 cinfo->progress->completed_passes = 0;
143 cinfo->progress->total_passes = 1;