]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jdtrans.cpp
...
[xonotic/netradiant.git] / libs / jpeg6 / jdtrans.cpp
1 /*
2
3  * jdtrans.c
4
5  *
6
7  * Copyright (C) 1995, Thomas G. Lane.
8
9  * This file is part of the Independent JPEG Group's software.
10
11  * For conditions of distribution and use, see the accompanying README file.
12
13  *
14
15  * This file contains library routines for transcoding decompression,
16
17  * that is, reading raw DCT coefficient arrays from an input JPEG file.
18
19  * The routines in jdapimin.c will also be needed by a transcoder.
20
21  */
22
23
24
25 #define JPEG_INTERNALS
26
27 #include "jinclude.h"
28
29 #include "radiant_jpeglib.h"
30
31
32
33
34
35 /* Forward declarations */
36
37 LOCAL void transdecode_master_selection JPP((j_decompress_ptr cinfo));
38
39
40
41
42
43 /*
44
45  * Read the coefficient arrays from a JPEG file.
46
47  * jpeg_read_header must be completed before calling this.
48
49  *
50
51  * The entire image is read into a set of virtual coefficient-block arrays,
52
53  * one per component.  The return value is a pointer to the array of
54
55  * virtual-array descriptors.  These can be manipulated directly via the
56
57  * JPEG memory manager, or handed off to jpeg_write_coefficients().
58
59  * To release the memory occupied by the virtual arrays, call
60
61  * jpeg_finish_decompress() when done with the data.
62
63  *
64
65  * Returns NULL if suspended.  This case need be checked only if
66
67  * a suspending data source is used.
68
69  */
70
71
72
73 GLOBAL jvirt_barray_ptr *
74
75 jpeg_read_coefficients (j_decompress_ptr cinfo)
76
77 {
78
79   if (cinfo->global_state == DSTATE_READY) {
80
81     /* First call: initialize active modules */
82
83     transdecode_master_selection(cinfo);
84
85     cinfo->global_state = DSTATE_RDCOEFS;
86
87   } else if (cinfo->global_state != DSTATE_RDCOEFS)
88
89     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
90
91   /* Absorb whole file into the coef buffer */
92
93   for (;;) {
94
95     int retcode;
96
97     /* Call progress monitor hook if present */
98
99     if (cinfo->progress != NULL)
100
101       (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
102
103     /* Absorb some more input */
104
105     retcode = (*cinfo->inputctl->consume_input) (cinfo);
106
107     if (retcode == JPEG_SUSPENDED)
108
109       return NULL;
110
111     if (retcode == JPEG_REACHED_EOI)
112
113       break;
114
115     /* Advance progress counter if appropriate */
116
117     if (cinfo->progress != NULL &&
118
119         (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
120
121       if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
122
123         /* startup underestimated number of scans; ratchet up one scan */
124
125         cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
126
127       }
128
129     }
130
131   }
132
133   /* Set state so that jpeg_finish_decompress does the right thing */
134
135   cinfo->global_state = DSTATE_STOPPING;
136
137   return cinfo->coef->coef_arrays;
138
139 }
140
141
142
143
144
145 /*
146
147  * Master selection of decompression modules for transcoding.
148
149  * This substitutes for jdmaster.c's initialization of the full decompressor.
150
151  */
152
153
154
155 LOCAL void
156
157 transdecode_master_selection (j_decompress_ptr cinfo)
158
159 {
160
161   /* Entropy decoding: either Huffman or arithmetic coding. */
162
163   if (cinfo->arith_code) {
164
165     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
166
167   } else {
168
169     if (cinfo->progressive_mode) {
170
171 #ifdef D_PROGRESSIVE_SUPPORTED
172
173       jinit_phuff_decoder(cinfo);
174
175 #else
176
177       ERREXIT(cinfo, JERR_NOT_COMPILED);
178
179 #endif
180
181     } else
182
183       jinit_huff_decoder(cinfo);
184
185   }
186
187
188
189   /* Always get a full-image coefficient buffer. */
190
191   jinit_d_coef_controller(cinfo, TRUE);
192
193
194
195   /* We can now tell the memory manager to allocate virtual arrays. */
196
197   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
198
199
200
201   /* Initialize input side of decompressor to consume first scan. */
202
203   (*cinfo->inputctl->start_input_pass) (cinfo);
204
205
206
207   /* Initialize progress monitoring. */
208
209   if (cinfo->progress != NULL) {
210
211     int nscans;
212
213     /* Estimate number of scans to set pass_limit. */
214
215     if (cinfo->progressive_mode) {
216
217       /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
218
219       nscans = 2 + 3 * cinfo->num_components;
220
221     } else if (cinfo->inputctl->has_multiple_scans) {
222
223       /* For a nonprogressive multiscan file, estimate 1 scan per component. */
224
225       nscans = cinfo->num_components;
226
227     } else {
228
229       nscans = 1;
230
231     }
232
233     cinfo->progress->pass_counter = 0L;
234
235     cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
236
237     cinfo->progress->completed_passes = 0;
238
239     cinfo->progress->total_passes = 1;
240
241   }
242
243 }
244