4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1995-1997, Thomas G. Lane.
6 * It was modified by The libjpeg-turbo Project to include only code relevant
8 * For conditions of distribution and use, see the accompanying README file.
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.
15 #define JPEG_INTERNALS
20 /* Forward declarations */
21 LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo);
25 * Read the coefficient arrays from a JPEG file.
26 * jpeg_read_header must be completed before calling this.
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 of
30 * virtual-array descriptors. These can be manipulated directly via the
31 * JPEG memory manager, or handed off to jpeg_write_coefficients().
32 * To release the memory occupied by the virtual arrays, call
33 * jpeg_finish_decompress() when done with the data.
35 * An alternative usage is to simply obtain access to the coefficient arrays
36 * during a buffered-image-mode decompression operation. This is allowed
37 * after any jpeg_finish_output() call. The arrays can be accessed until
38 * jpeg_finish_decompress() is called. (Note that any call to the library
39 * may reposition the arrays, so don't rely on access_virt_barray() results
40 * to stay valid across library calls.)
42 * Returns NULL if suspended. This case need be checked only if
43 * a suspending data source is used.
46 GLOBAL(jvirt_barray_ptr *)
47 jpeg_read_coefficients (j_decompress_ptr cinfo)
49 if (cinfo->global_state == DSTATE_READY) {
50 /* First call: initialize active modules */
51 transdecode_master_selection(cinfo);
52 cinfo->global_state = DSTATE_RDCOEFS;
54 if (cinfo->global_state == DSTATE_RDCOEFS) {
55 /* Absorb whole file into the coef buffer */
58 /* Call progress monitor hook if present */
59 if (cinfo->progress != NULL)
60 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
61 /* Absorb some more input */
62 retcode = (*cinfo->inputctl->consume_input) (cinfo);
63 if (retcode == JPEG_SUSPENDED)
65 if (retcode == JPEG_REACHED_EOI)
67 /* Advance progress counter if appropriate */
68 if (cinfo->progress != NULL &&
69 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
70 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
71 /* startup underestimated number of scans; ratchet up one scan */
72 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
76 /* Set state so that jpeg_finish_decompress does the right thing */
77 cinfo->global_state = DSTATE_STOPPING;
79 /* At this point we should be in state DSTATE_STOPPING if being used
80 * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
81 * to the coefficients during a full buffered-image-mode decompression.
83 if ((cinfo->global_state == DSTATE_STOPPING ||
84 cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
85 return cinfo->coef->coef_arrays;
87 /* Oops, improper usage */
88 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
89 return NULL; /* keep compiler happy */
94 * Master selection of decompression modules for transcoding.
95 * This substitutes for jdmaster.c's initialization of the full decompressor.
99 transdecode_master_selection (j_decompress_ptr cinfo)
101 /* This is effectively a buffered-image operation. */
102 cinfo->buffered_image = TRUE;
104 #if JPEG_LIB_VERSION >= 80
105 /* Compute output image dimensions and related values. */
106 jpeg_core_output_dimensions(cinfo);
109 /* Entropy decoding: either Huffman or arithmetic coding. */
110 if (cinfo->arith_code) {
111 #ifdef D_ARITH_CODING_SUPPORTED
112 jinit_arith_decoder(cinfo);
114 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
117 if (cinfo->progressive_mode) {
118 #ifdef D_PROGRESSIVE_SUPPORTED
119 jinit_phuff_decoder(cinfo);
121 ERREXIT(cinfo, JERR_NOT_COMPILED);
124 jinit_huff_decoder(cinfo);
127 /* Always get a full-image coefficient buffer. */
128 jinit_d_coef_controller(cinfo, TRUE);
130 /* We can now tell the memory manager to allocate virtual arrays. */
131 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
133 /* Initialize input side of decompressor to consume first scan. */
134 (*cinfo->inputctl->start_input_pass) (cinfo);
136 /* Initialize progress monitoring. */
137 if (cinfo->progress != NULL) {
139 /* Estimate number of scans to set pass_limit. */
140 if (cinfo->progressive_mode) {
141 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
142 nscans = 2 + 3 * cinfo->num_components;
143 } else if (cinfo->inputctl->has_multiple_scans) {
144 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
145 nscans = cinfo->num_components;
149 cinfo->progress->pass_counter = 0L;
150 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
151 cinfo->progress->completed_passes = 0;
152 cinfo->progress->total_passes = 1;