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.ijg
11 * This file contains library routines for transcoding decompression,
12 * that is, reading raw DCT coefficient arrays from an input JPEG file.
13 * The routines in jdapimin.c will also be needed by a transcoder.
16 #define JPEG_INTERNALS
21 /* Forward declarations */
22 LOCAL(void) transdecode_master_selection(j_decompress_ptr cinfo
);
26 * Read the coefficient arrays from a JPEG file.
27 * jpeg_read_header must be completed before calling this.
29 * The entire image is read into a set of virtual coefficient-block arrays,
30 * one per component. The return value is a pointer to the array of
31 * virtual-array descriptors. These can be manipulated directly via the
32 * JPEG memory manager, or handed off to jpeg_write_coefficients().
33 * To release the memory occupied by the virtual arrays, call
34 * jpeg_finish_decompress() when done with the data.
36 * An alternative usage is to simply obtain access to the coefficient arrays
37 * during a buffered-image-mode decompression operation. This is allowed
38 * after any jpeg_finish_output() call. The arrays can be accessed until
39 * jpeg_finish_decompress() is called. (Note that any call to the library
40 * may reposition the arrays, so don't rely on access_virt_barray() results
41 * to stay valid across library calls.)
43 * Returns NULL if suspended. This case need be checked only if
44 * a suspending data source is used.
47 GLOBAL(jvirt_barray_ptr
*)
48 jpeg_read_coefficients(j_decompress_ptr cinfo
)
50 if (cinfo
->global_state
== DSTATE_READY
) {
51 /* First call: initialize active modules */
52 transdecode_master_selection(cinfo
);
53 cinfo
->global_state
= DSTATE_RDCOEFS
;
55 if (cinfo
->global_state
== DSTATE_RDCOEFS
) {
56 /* Absorb whole file into the coef buffer */
59 /* Call progress monitor hook if present */
60 if (cinfo
->progress
!= NULL
)
61 (*cinfo
->progress
->progress_monitor
) ((j_common_ptr
)cinfo
);
62 /* Absorb some more input */
63 retcode
= (*cinfo
->inputctl
->consume_input
) (cinfo
);
64 if (retcode
== JPEG_SUSPENDED
)
66 if (retcode
== JPEG_REACHED_EOI
)
68 /* Advance progress counter if appropriate */
69 if (cinfo
->progress
!= NULL
&&
70 (retcode
== JPEG_ROW_COMPLETED
|| retcode
== JPEG_REACHED_SOS
)) {
71 if (++cinfo
->progress
->pass_counter
>= cinfo
->progress
->pass_limit
) {
72 /* startup underestimated number of scans; ratchet up one scan */
73 cinfo
->progress
->pass_limit
+= (long)cinfo
->total_iMCU_rows
;
77 /* Set state so that jpeg_finish_decompress does the right thing */
78 cinfo
->global_state
= DSTATE_STOPPING
;
80 /* At this point we should be in state DSTATE_STOPPING if being used
81 * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
82 * to the coefficients during a full buffered-image-mode decompression.
84 if ((cinfo
->global_state
== DSTATE_STOPPING
||
85 cinfo
->global_state
== DSTATE_BUFIMAGE
) && cinfo
->buffered_image
) {
86 return cinfo
->coef
->coef_arrays
;
88 /* Oops, improper usage */
89 ERREXIT1(cinfo
, JERR_BAD_STATE
, cinfo
->global_state
);
90 return NULL
; /* keep compiler happy */
95 * Master selection of decompression modules for transcoding.
96 * This substitutes for jdmaster.c's initialization of the full decompressor.
100 transdecode_master_selection(j_decompress_ptr cinfo
)
102 /* This is effectively a buffered-image operation. */
103 cinfo
->buffered_image
= TRUE
;
105 #if JPEG_LIB_VERSION >= 80
106 /* Compute output image dimensions and related values. */
107 jpeg_core_output_dimensions(cinfo
);
110 /* Entropy decoding: either Huffman or arithmetic coding. */
111 if (cinfo
->arith_code
) {
112 #ifdef D_ARITH_CODING_SUPPORTED
113 jinit_arith_decoder(cinfo
);
115 ERREXIT(cinfo
, JERR_ARITH_NOTIMPL
);
118 if (cinfo
->progressive_mode
) {
119 #ifdef D_PROGRESSIVE_SUPPORTED
120 jinit_phuff_decoder(cinfo
);
122 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
125 jinit_huff_decoder(cinfo
);
128 /* Always get a full-image coefficient buffer. */
129 jinit_d_coef_controller(cinfo
, TRUE
);
131 /* We can now tell the memory manager to allocate virtual arrays. */
132 (*cinfo
->mem
->realize_virt_arrays
) ((j_common_ptr
)cinfo
);
134 /* Initialize input side of decompressor to consume first scan. */
135 (*cinfo
->inputctl
->start_input_pass
) (cinfo
);
137 /* Initialize progress monitoring. */
138 if (cinfo
->progress
!= NULL
) {
140 /* Estimate number of scans to set pass_limit. */
141 if (cinfo
->progressive_mode
) {
142 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
143 nscans
= 2 + 3 * cinfo
->num_components
;
144 } else if (cinfo
->inputctl
->has_multiple_scans
) {
145 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
146 nscans
= cinfo
->num_components
;
150 cinfo
->progress
->pass_counter
= 0L;
151 cinfo
->progress
->pass_limit
= (long)cinfo
->total_iMCU_rows
* nscans
;
152 cinfo
->progress
->completed_passes
= 0;
153 cinfo
->progress
->total_passes
= 1;