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