Line data Source code
1 : /*
2 : * jdtrans.c
3 : *
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
7 : * to libjpeg-turbo.
8 : * For conditions of distribution and use, see the accompanying README.ijg
9 : * file.
10 : *
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.
14 : */
15 :
16 : #define JPEG_INTERNALS
17 : #include "jinclude.h"
18 : #include "jpeglib.h"
19 :
20 :
21 : /* Forward declarations */
22 : LOCAL(void) transdecode_master_selection (j_decompress_ptr cinfo);
23 :
24 :
25 : /*
26 : * Read the coefficient arrays from a JPEG file.
27 : * jpeg_read_header must be completed before calling this.
28 : *
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.
35 : *
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.)
42 : *
43 : * Returns NULL if suspended. This case need be checked only if
44 : * a suspending data source is used.
45 : */
46 :
47 : GLOBAL(jvirt_barray_ptr *)
48 0 : jpeg_read_coefficients (j_decompress_ptr cinfo)
49 : {
50 0 : if (cinfo->global_state == DSTATE_READY) {
51 : /* First call: initialize active modules */
52 0 : transdecode_master_selection(cinfo);
53 0 : cinfo->global_state = DSTATE_RDCOEFS;
54 : }
55 0 : if (cinfo->global_state == DSTATE_RDCOEFS) {
56 : /* Absorb whole file into the coef buffer */
57 0 : for (;;) {
58 : int retcode;
59 : /* Call progress monitor hook if present */
60 0 : if (cinfo->progress != NULL)
61 0 : (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
62 : /* Absorb some more input */
63 0 : retcode = (*cinfo->inputctl->consume_input) (cinfo);
64 0 : if (retcode == JPEG_SUSPENDED)
65 0 : return NULL;
66 0 : if (retcode == JPEG_REACHED_EOI)
67 0 : break;
68 : /* Advance progress counter if appropriate */
69 0 : if (cinfo->progress != NULL &&
70 0 : (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
71 0 : if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
72 : /* startup underestimated number of scans; ratchet up one scan */
73 0 : cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
74 : }
75 : }
76 : }
77 : /* Set state so that jpeg_finish_decompress does the right thing */
78 0 : cinfo->global_state = DSTATE_STOPPING;
79 : }
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.
83 : */
84 0 : if ((cinfo->global_state == DSTATE_STOPPING ||
85 0 : cinfo->global_state == DSTATE_BUFIMAGE) && cinfo->buffered_image) {
86 0 : return cinfo->coef->coef_arrays;
87 : }
88 : /* Oops, improper usage */
89 0 : ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
90 0 : return NULL; /* keep compiler happy */
91 : }
92 :
93 :
94 : /*
95 : * Master selection of decompression modules for transcoding.
96 : * This substitutes for jdmaster.c's initialization of the full decompressor.
97 : */
98 :
99 : LOCAL(void)
100 0 : transdecode_master_selection (j_decompress_ptr cinfo)
101 : {
102 : /* This is effectively a buffered-image operation. */
103 0 : cinfo->buffered_image = TRUE;
104 :
105 : #if JPEG_LIB_VERSION >= 80
106 : /* Compute output image dimensions and related values. */
107 : jpeg_core_output_dimensions(cinfo);
108 : #endif
109 :
110 : /* Entropy decoding: either Huffman or arithmetic coding. */
111 0 : if (cinfo->arith_code) {
112 : #ifdef D_ARITH_CODING_SUPPORTED
113 : jinit_arith_decoder(cinfo);
114 : #else
115 0 : ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
116 : #endif
117 : } else {
118 0 : if (cinfo->progressive_mode) {
119 : #ifdef D_PROGRESSIVE_SUPPORTED
120 0 : jinit_phuff_decoder(cinfo);
121 : #else
122 : ERREXIT(cinfo, JERR_NOT_COMPILED);
123 : #endif
124 : } else
125 0 : jinit_huff_decoder(cinfo);
126 : }
127 :
128 : /* Always get a full-image coefficient buffer. */
129 0 : jinit_d_coef_controller(cinfo, TRUE);
130 :
131 : /* We can now tell the memory manager to allocate virtual arrays. */
132 0 : (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
133 :
134 : /* Initialize input side of decompressor to consume first scan. */
135 0 : (*cinfo->inputctl->start_input_pass) (cinfo);
136 :
137 : /* Initialize progress monitoring. */
138 0 : if (cinfo->progress != NULL) {
139 : int nscans;
140 : /* Estimate number of scans to set pass_limit. */
141 0 : if (cinfo->progressive_mode) {
142 : /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
143 0 : nscans = 2 + 3 * cinfo->num_components;
144 0 : } else if (cinfo->inputctl->has_multiple_scans) {
145 : /* For a nonprogressive multiscan file, estimate 1 scan per component. */
146 0 : nscans = cinfo->num_components;
147 : } else {
148 0 : nscans = 1;
149 : }
150 0 : cinfo->progress->pass_counter = 0L;
151 0 : cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
152 0 : cinfo->progress->completed_passes = 0;
153 0 : cinfo->progress->total_passes = 1;
154 : }
155 0 : }
|