1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * jdcoefct.c
|
---|
11 | *
|
---|
12 | * Copyright (C) 1994-1996, Thomas G. Lane.
|
---|
13 | * This file is part of the Independent JPEG Group's software.
|
---|
14 | * For conditions of distribution and use, see the accompanying README file.
|
---|
15 | *
|
---|
16 | * This file contains the coefficient buffer controller for decompression.
|
---|
17 | * This controller is the top level of the JPEG decompressor proper.
|
---|
18 | * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
|
---|
19 | *
|
---|
20 | * In buffered-image mode, this controller is the interface between
|
---|
21 | * input-oriented processing and output-oriented processing.
|
---|
22 | * Also, the input side (only) is used when reading a file for transcoding.
|
---|
23 | */
|
---|
24 |
|
---|
25 | #define JPEG_INTERNALS
|
---|
26 | #include "loaders/jpg/jinclude.h"
|
---|
27 | #include "loaders/jpg/jpeglib.h"
|
---|
28 |
|
---|
29 | /* Block smoothing is only applicable for progressive JPEG, so: */
|
---|
30 | #ifndef D_PROGRESSIVE_SUPPORTED
|
---|
31 | #undef BLOCK_SMOOTHING_SUPPORTED
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /* Private buffer controller object */
|
---|
35 |
|
---|
36 | typedef struct {
|
---|
37 | struct jpeg_d_coef_controller pub; /* public fields */
|
---|
38 |
|
---|
39 | /* These variables keep track of the current location of the input side. */
|
---|
40 | /* cinfo->input_iMCU_row is also used for this. */
|
---|
41 | JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
|
---|
42 | int MCU_vert_offset; /* counts MCU rows within iMCU row */
|
---|
43 | int MCU_rows_per_iMCU_row; /* number of such rows needed */
|
---|
44 |
|
---|
45 | /* The output side's location is represented by cinfo->output_iMCU_row. */
|
---|
46 |
|
---|
47 | /* In single-pass modes, it's sufficient to buffer just one MCU.
|
---|
48 | * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
|
---|
49 | * and let the entropy decoder write into that workspace each time.
|
---|
50 | * (On 80x86, the workspace is FAR even though it's not really very big;
|
---|
51 | * this is to keep the module interfaces unchanged when a large coefficient
|
---|
52 | * buffer is necessary.)
|
---|
53 | * In multi-pass modes, this array points to the current MCU's blocks
|
---|
54 | * within the virtual arrays; it is used only by the input side.
|
---|
55 | */
|
---|
56 | JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
|
---|
57 |
|
---|
58 | #ifdef D_MULTISCAN_FILES_SUPPORTED
|
---|
59 | /* In multi-pass modes, we need a virtual block array for each component. */
|
---|
60 | jvirt_barray_ptr whole_image[MAX_COMPONENTS];
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #ifdef BLOCK_SMOOTHING_SUPPORTED
|
---|
64 | /* When doing block smoothing, we latch coefficient Al values here */
|
---|
65 | int * coef_bits_latch;
|
---|
66 | #define SAVED_COEFS 6 /* we save coef_bits[0..5] */
|
---|
67 | #endif
|
---|
68 | } my_coef_controller;
|
---|
69 |
|
---|
70 | typedef my_coef_controller * my_coef_ptr;
|
---|
71 |
|
---|
72 | /* Forward declarations */
|
---|
73 | METHODDEF(int) decompress_onepass
|
---|
74 | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
|
---|
75 | #ifdef D_MULTISCAN_FILES_SUPPORTED
|
---|
76 | METHODDEF(int) decompress_data
|
---|
77 | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
|
---|
78 | #endif
|
---|
79 | #ifdef BLOCK_SMOOTHING_SUPPORTED
|
---|
80 | LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
|
---|
81 | METHODDEF(int) decompress_smooth_data
|
---|
82 | JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
|
---|
83 | #endif
|
---|
84 |
|
---|
85 |
|
---|
86 | LOCAL(void)
|
---|
87 | start_iMCU_row (j_decompress_ptr cinfo)
|
---|
88 | /* Reset within-iMCU-row counters for a new row (input side) */
|
---|
89 | {
|
---|
90 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
91 |
|
---|
92 | /* In an interleaved scan, an MCU row is the same as an iMCU row.
|
---|
93 | * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
|
---|
94 | * But at the bottom of the image, process only what's left.
|
---|
95 | */
|
---|
96 | if (cinfo->comps_in_scan > 1) {
|
---|
97 | coef->MCU_rows_per_iMCU_row = 1;
|
---|
98 | } else {
|
---|
99 | if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
|
---|
100 | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
|
---|
101 | else
|
---|
102 | coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
|
---|
103 | }
|
---|
104 |
|
---|
105 | coef->MCU_ctr = 0;
|
---|
106 | coef->MCU_vert_offset = 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Initialize for an input processing pass.
|
---|
112 | */
|
---|
113 |
|
---|
114 | METHODDEF(void)
|
---|
115 | start_input_pass (j_decompress_ptr cinfo)
|
---|
116 | {
|
---|
117 | cinfo->input_iMCU_row = 0;
|
---|
118 | start_iMCU_row(cinfo);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Initialize for an output processing pass.
|
---|
124 | */
|
---|
125 |
|
---|
126 | METHODDEF(void)
|
---|
127 | start_output_pass (j_decompress_ptr cinfo)
|
---|
128 | {
|
---|
129 | #ifdef BLOCK_SMOOTHING_SUPPORTED
|
---|
130 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
131 |
|
---|
132 | /* If multipass, check to see whether to use block smoothing on this pass */
|
---|
133 | if (coef->pub.coef_arrays != NULL) {
|
---|
134 | if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
|
---|
135 | coef->pub.decompress_data = decompress_smooth_data;
|
---|
136 | else
|
---|
137 | coef->pub.decompress_data = decompress_data;
|
---|
138 | }
|
---|
139 | #endif
|
---|
140 | cinfo->output_iMCU_row = 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * Decompress and return some data in the single-pass case.
|
---|
146 | * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
|
---|
147 | * Input and output must run in lockstep since we have only a one-MCU buffer.
|
---|
148 | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
|
---|
149 | *
|
---|
150 | * NB: output_buf contains a plane for each component in image.
|
---|
151 | * For single pass, this is the same as the components in the scan.
|
---|
152 | */
|
---|
153 |
|
---|
154 | METHODDEF(int)
|
---|
155 | decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
|
---|
156 | {
|
---|
157 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
158 | JDIMENSION MCU_col_num; /* index of current MCU within row */
|
---|
159 | JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
|
---|
160 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
|
---|
161 | int blkn, ci, xindex, yindex, yoffset, useful_width;
|
---|
162 | JSAMPARRAY output_ptr;
|
---|
163 | JDIMENSION start_col, output_col;
|
---|
164 | jpeg_component_info *compptr;
|
---|
165 | inverse_DCT_method_ptr inverse_DCT;
|
---|
166 |
|
---|
167 | /* Loop to process as much as one whole iMCU row */
|
---|
168 | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
|
---|
169 | yoffset++) {
|
---|
170 | for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
|
---|
171 | MCU_col_num++) {
|
---|
172 | /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
|
---|
173 | jzero_far((void FAR *) coef->MCU_buffer[0],
|
---|
174 | (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
|
---|
175 | if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
|
---|
176 | /* Suspension forced; update state counters and exit */
|
---|
177 | coef->MCU_vert_offset = yoffset;
|
---|
178 | coef->MCU_ctr = MCU_col_num;
|
---|
179 | return JPEG_SUSPENDED;
|
---|
180 | }
|
---|
181 | /* Determine where data should go in output_buf and do the IDCT thing.
|
---|
182 | * We skip dummy blocks at the right and bottom edges (but blkn gets
|
---|
183 | * incremented past them!). Note the inner loop relies on having
|
---|
184 | * allocated the MCU_buffer[] blocks sequentially.
|
---|
185 | */
|
---|
186 | blkn = 0; /* index of current DCT block within MCU */
|
---|
187 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
---|
188 | compptr = cinfo->cur_comp_info[ci];
|
---|
189 | /* Don't bother to IDCT an uninteresting component. */
|
---|
190 | if (! compptr->component_needed) {
|
---|
191 | blkn += compptr->MCU_blocks;
|
---|
192 | continue;
|
---|
193 | }
|
---|
194 | inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
|
---|
195 | useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
|
---|
196 | : compptr->last_col_width;
|
---|
197 | output_ptr = output_buf[ci] + yoffset * compptr->DCT_scaled_size;
|
---|
198 | start_col = MCU_col_num * compptr->MCU_sample_width;
|
---|
199 | for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
|
---|
200 | if (cinfo->input_iMCU_row < last_iMCU_row ||
|
---|
201 | yoffset+yindex < compptr->last_row_height) {
|
---|
202 | output_col = start_col;
|
---|
203 | for (xindex = 0; xindex < useful_width; xindex++) {
|
---|
204 | (*inverse_DCT) (cinfo, compptr,
|
---|
205 | (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
|
---|
206 | output_ptr, output_col);
|
---|
207 | output_col += compptr->DCT_scaled_size;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | blkn += compptr->MCU_width;
|
---|
211 | output_ptr += compptr->DCT_scaled_size;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | /* Completed an MCU row, but perhaps not an iMCU row */
|
---|
216 | coef->MCU_ctr = 0;
|
---|
217 | }
|
---|
218 | /* Completed the iMCU row, advance counters for next one */
|
---|
219 | cinfo->output_iMCU_row++;
|
---|
220 | if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
|
---|
221 | start_iMCU_row(cinfo);
|
---|
222 | return JPEG_ROW_COMPLETED;
|
---|
223 | }
|
---|
224 | /* Completed the scan */
|
---|
225 | (*cinfo->inputctl->finish_input_pass) (cinfo);
|
---|
226 | return JPEG_SCAN_COMPLETED;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Dummy consume-input routine for single-pass operation.
|
---|
232 | */
|
---|
233 |
|
---|
234 | METHODDEF(int)
|
---|
235 | dummy_consume_data (j_decompress_ptr cinfo)
|
---|
236 | {
|
---|
237 | return JPEG_SUSPENDED; /* Always indicate nothing was done */
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | #ifdef D_MULTISCAN_FILES_SUPPORTED
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Consume input data and store it in the full-image coefficient buffer.
|
---|
245 | * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
|
---|
246 | * ie, v_samp_factor block rows for each component in the scan.
|
---|
247 | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
|
---|
248 | */
|
---|
249 |
|
---|
250 | METHODDEF(int)
|
---|
251 | consume_data (j_decompress_ptr cinfo)
|
---|
252 | {
|
---|
253 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
254 | JDIMENSION MCU_col_num; /* index of current MCU within row */
|
---|
255 | int blkn, ci, xindex, yindex, yoffset;
|
---|
256 | JDIMENSION start_col;
|
---|
257 | JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
|
---|
258 | JBLOCKROW buffer_ptr;
|
---|
259 | jpeg_component_info *compptr;
|
---|
260 |
|
---|
261 | /* Align the virtual buffers for the components used in this scan. */
|
---|
262 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
---|
263 | compptr = cinfo->cur_comp_info[ci];
|
---|
264 | buffer[ci] = (*cinfo->mem->access_virt_barray)
|
---|
265 | ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
|
---|
266 | cinfo->input_iMCU_row * compptr->v_samp_factor,
|
---|
267 | (JDIMENSION) compptr->v_samp_factor, TRUE);
|
---|
268 | /* Note: entropy decoder expects buffer to be zeroed,
|
---|
269 | * but this is handled automatically by the memory manager
|
---|
270 | * because we requested a pre-zeroed array.
|
---|
271 | */
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* Loop to process one whole iMCU row */
|
---|
275 | for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
|
---|
276 | yoffset++) {
|
---|
277 | for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
|
---|
278 | MCU_col_num++) {
|
---|
279 | /* Construct list of pointers to DCT blocks belonging to this MCU */
|
---|
280 | blkn = 0; /* index of current DCT block within MCU */
|
---|
281 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
---|
282 | compptr = cinfo->cur_comp_info[ci];
|
---|
283 | start_col = MCU_col_num * compptr->MCU_width;
|
---|
284 | for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
|
---|
285 | buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
|
---|
286 | for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
|
---|
287 | coef->MCU_buffer[blkn++] = buffer_ptr++;
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | /* Try to fetch the MCU. */
|
---|
292 | if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
|
---|
293 | /* Suspension forced; update state counters and exit */
|
---|
294 | coef->MCU_vert_offset = yoffset;
|
---|
295 | coef->MCU_ctr = MCU_col_num;
|
---|
296 | return JPEG_SUSPENDED;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | /* Completed an MCU row, but perhaps not an iMCU row */
|
---|
300 | coef->MCU_ctr = 0;
|
---|
301 | }
|
---|
302 | /* Completed the iMCU row, advance counters for next one */
|
---|
303 | if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
|
---|
304 | start_iMCU_row(cinfo);
|
---|
305 | return JPEG_ROW_COMPLETED;
|
---|
306 | }
|
---|
307 | /* Completed the scan */
|
---|
308 | (*cinfo->inputctl->finish_input_pass) (cinfo);
|
---|
309 | return JPEG_SCAN_COMPLETED;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * Decompress and return some data in the multi-pass case.
|
---|
315 | * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
|
---|
316 | * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
|
---|
317 | *
|
---|
318 | * NB: output_buf contains a plane for each component in image.
|
---|
319 | */
|
---|
320 |
|
---|
321 | METHODDEF(int)
|
---|
322 | decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
|
---|
323 | {
|
---|
324 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
325 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
|
---|
326 | JDIMENSION block_num;
|
---|
327 | int ci, block_row, block_rows;
|
---|
328 | JBLOCKARRAY buffer;
|
---|
329 | JBLOCKROW buffer_ptr;
|
---|
330 | JSAMPARRAY output_ptr;
|
---|
331 | JDIMENSION output_col;
|
---|
332 | jpeg_component_info *compptr;
|
---|
333 | inverse_DCT_method_ptr inverse_DCT;
|
---|
334 |
|
---|
335 | /* Force some input to be done if we are getting ahead of the input. */
|
---|
336 | while (cinfo->input_scan_number < cinfo->output_scan_number ||
|
---|
337 | (cinfo->input_scan_number == cinfo->output_scan_number &&
|
---|
338 | cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
|
---|
339 | if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
|
---|
340 | return JPEG_SUSPENDED;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /* OK, output from the virtual arrays. */
|
---|
344 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
---|
345 | ci++, compptr++) {
|
---|
346 | /* Don't bother to IDCT an uninteresting component. */
|
---|
347 | if (! compptr->component_needed)
|
---|
348 | continue;
|
---|
349 | /* Align the virtual buffer for this component. */
|
---|
350 | buffer = (*cinfo->mem->access_virt_barray)
|
---|
351 | ((j_common_ptr) cinfo, coef->whole_image[ci],
|
---|
352 | cinfo->output_iMCU_row * compptr->v_samp_factor,
|
---|
353 | (JDIMENSION) compptr->v_samp_factor, FALSE);
|
---|
354 | /* Count non-dummy DCT block rows in this iMCU row. */
|
---|
355 | if (cinfo->output_iMCU_row < last_iMCU_row)
|
---|
356 | block_rows = compptr->v_samp_factor;
|
---|
357 | else {
|
---|
358 | /* NB: can't use last_row_height here; it is input-side-dependent! */
|
---|
359 | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
|
---|
360 | if (block_rows == 0) block_rows = compptr->v_samp_factor;
|
---|
361 | }
|
---|
362 | inverse_DCT = cinfo->idct->inverse_DCT[ci];
|
---|
363 | output_ptr = output_buf[ci];
|
---|
364 | /* Loop over all DCT blocks to be processed. */
|
---|
365 | for (block_row = 0; block_row < block_rows; block_row++) {
|
---|
366 | buffer_ptr = buffer[block_row];
|
---|
367 | output_col = 0;
|
---|
368 | for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
|
---|
369 | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
|
---|
370 | output_ptr, output_col);
|
---|
371 | buffer_ptr++;
|
---|
372 | output_col += compptr->DCT_scaled_size;
|
---|
373 | }
|
---|
374 | output_ptr += compptr->DCT_scaled_size;
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
|
---|
379 | return JPEG_ROW_COMPLETED;
|
---|
380 | return JPEG_SCAN_COMPLETED;
|
---|
381 | }
|
---|
382 |
|
---|
383 | #endif /* D_MULTISCAN_FILES_SUPPORTED */
|
---|
384 |
|
---|
385 |
|
---|
386 | #ifdef BLOCK_SMOOTHING_SUPPORTED
|
---|
387 |
|
---|
388 | /*
|
---|
389 | * This code applies interblock smoothing as described by section K.8
|
---|
390 | * of the JPEG standard: the first 5 AC coefficients are estimated from
|
---|
391 | * the DC values of a DCT block and its 8 neighboring blocks.
|
---|
392 | * We apply smoothing only for progressive JPEG decoding, and only if
|
---|
393 | * the coefficients it can estimate are not yet known to full precision.
|
---|
394 | */
|
---|
395 |
|
---|
396 | /* Natural-order array positions of the first 5 zigzag-order coefficients */
|
---|
397 | #define Q01_POS 1
|
---|
398 | #define Q10_POS 8
|
---|
399 | #define Q20_POS 16
|
---|
400 | #define Q11_POS 9
|
---|
401 | #define Q02_POS 2
|
---|
402 |
|
---|
403 | /*
|
---|
404 | * Determine whether block smoothing is applicable and safe.
|
---|
405 | * We also latch the current states of the coef_bits[] entries for the
|
---|
406 | * AC coefficients; otherwise, if the input side of the decompressor
|
---|
407 | * advances into a new scan, we might think the coefficients are known
|
---|
408 | * more accurately than they really are.
|
---|
409 | */
|
---|
410 |
|
---|
411 | LOCAL(boolean)
|
---|
412 | smoothing_ok (j_decompress_ptr cinfo)
|
---|
413 | {
|
---|
414 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
415 | boolean smoothing_useful = FALSE;
|
---|
416 | int ci, coefi;
|
---|
417 | jpeg_component_info *compptr;
|
---|
418 | JQUANT_TBL * qtable;
|
---|
419 | int * coef_bits;
|
---|
420 | int * coef_bits_latch;
|
---|
421 |
|
---|
422 | if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
|
---|
423 | return FALSE;
|
---|
424 |
|
---|
425 | /* Allocate latch area if not already done */
|
---|
426 | if (coef->coef_bits_latch == NULL)
|
---|
427 | coef->coef_bits_latch = (int *)
|
---|
428 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
---|
429 | cinfo->num_components *
|
---|
430 | (SAVED_COEFS * SIZEOF(int)));
|
---|
431 | coef_bits_latch = coef->coef_bits_latch;
|
---|
432 |
|
---|
433 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
---|
434 | ci++, compptr++) {
|
---|
435 | /* All components' quantization values must already be latched. */
|
---|
436 | if ((qtable = compptr->quant_table) == NULL)
|
---|
437 | return FALSE;
|
---|
438 | /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
|
---|
439 | if (qtable->quantval[0] == 0 ||
|
---|
440 | qtable->quantval[Q01_POS] == 0 ||
|
---|
441 | qtable->quantval[Q10_POS] == 0 ||
|
---|
442 | qtable->quantval[Q20_POS] == 0 ||
|
---|
443 | qtable->quantval[Q11_POS] == 0 ||
|
---|
444 | qtable->quantval[Q02_POS] == 0)
|
---|
445 | return FALSE;
|
---|
446 | /* DC values must be at least partly known for all components. */
|
---|
447 | coef_bits = cinfo->coef_bits[ci];
|
---|
448 | if (coef_bits[0] < 0)
|
---|
449 | return FALSE;
|
---|
450 | /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
|
---|
451 | for (coefi = 1; coefi <= 5; coefi++) {
|
---|
452 | coef_bits_latch[coefi] = coef_bits[coefi];
|
---|
453 | if (coef_bits[coefi] != 0)
|
---|
454 | smoothing_useful = TRUE;
|
---|
455 | }
|
---|
456 | coef_bits_latch += SAVED_COEFS;
|
---|
457 | }
|
---|
458 |
|
---|
459 | return smoothing_useful;
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * Variant of decompress_data for use when doing block smoothing.
|
---|
465 | */
|
---|
466 |
|
---|
467 | METHODDEF(int)
|
---|
468 | decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
|
---|
469 | {
|
---|
470 | my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
|
---|
471 | JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
|
---|
472 | JDIMENSION block_num, last_block_column;
|
---|
473 | int ci, block_row, block_rows, access_rows;
|
---|
474 | JBLOCKARRAY buffer;
|
---|
475 | JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
|
---|
476 | JSAMPARRAY output_ptr;
|
---|
477 | JDIMENSION output_col;
|
---|
478 | jpeg_component_info *compptr;
|
---|
479 | inverse_DCT_method_ptr inverse_DCT;
|
---|
480 | boolean first_row, last_row;
|
---|
481 | JBLOCK workspace;
|
---|
482 | int *coef_bits;
|
---|
483 | JQUANT_TBL *quanttbl;
|
---|
484 | INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
|
---|
485 | int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
|
---|
486 | int Al, pred;
|
---|
487 |
|
---|
488 | /* Force some input to be done if we are getting ahead of the input. */
|
---|
489 | while (cinfo->input_scan_number <= cinfo->output_scan_number &&
|
---|
490 | ! cinfo->inputctl->eoi_reached) {
|
---|
491 | if (cinfo->input_scan_number == cinfo->output_scan_number) {
|
---|
492 | /* If input is working on current scan, we ordinarily want it to
|
---|
493 | * have completed the current row. But if input scan is DC,
|
---|
494 | * we want it to keep one row ahead so that next block row's DC
|
---|
495 | * values are up to date.
|
---|
496 | */
|
---|
497 | JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
|
---|
498 | if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
|
---|
499 | break;
|
---|
500 | }
|
---|
501 | if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
|
---|
502 | return JPEG_SUSPENDED;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /* OK, output from the virtual arrays. */
|
---|
506 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
---|
507 | ci++, compptr++) {
|
---|
508 | /* Don't bother to IDCT an uninteresting component. */
|
---|
509 | if (! compptr->component_needed)
|
---|
510 | continue;
|
---|
511 | /* Count non-dummy DCT block rows in this iMCU row. */
|
---|
512 | if (cinfo->output_iMCU_row < last_iMCU_row) {
|
---|
513 | block_rows = compptr->v_samp_factor;
|
---|
514 | access_rows = block_rows * 2; /* this and next iMCU row */
|
---|
515 | last_row = FALSE;
|
---|
516 | } else {
|
---|
517 | /* NB: can't use last_row_height here; it is input-side-dependent! */
|
---|
518 | block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
|
---|
519 | if (block_rows == 0) block_rows = compptr->v_samp_factor;
|
---|
520 | access_rows = block_rows; /* this iMCU row only */
|
---|
521 | last_row = TRUE;
|
---|
522 | }
|
---|
523 | /* Align the virtual buffer for this component. */
|
---|
524 | if (cinfo->output_iMCU_row > 0) {
|
---|
525 | access_rows += compptr->v_samp_factor; /* prior iMCU row too */
|
---|
526 | buffer = (*cinfo->mem->access_virt_barray)
|
---|
527 | ((j_common_ptr) cinfo, coef->whole_image[ci],
|
---|
528 | (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
|
---|
529 | (JDIMENSION) access_rows, FALSE);
|
---|
530 | buffer += compptr->v_samp_factor; /* point to current iMCU row */
|
---|
531 | first_row = FALSE;
|
---|
532 | } else {
|
---|
533 | buffer = (*cinfo->mem->access_virt_barray)
|
---|
534 | ((j_common_ptr) cinfo, coef->whole_image[ci],
|
---|
535 | (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
|
---|
536 | first_row = TRUE;
|
---|
537 | }
|
---|
538 | /* Fetch component-dependent info */
|
---|
539 | coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
|
---|
540 | quanttbl = compptr->quant_table;
|
---|
541 | Q00 = quanttbl->quantval[0];
|
---|
542 | Q01 = quanttbl->quantval[Q01_POS];
|
---|
543 | Q10 = quanttbl->quantval[Q10_POS];
|
---|
544 | Q20 = quanttbl->quantval[Q20_POS];
|
---|
545 | Q11 = quanttbl->quantval[Q11_POS];
|
---|
546 | Q02 = quanttbl->quantval[Q02_POS];
|
---|
547 | inverse_DCT = cinfo->idct->inverse_DCT[ci];
|
---|
548 | output_ptr = output_buf[ci];
|
---|
549 | /* Loop over all DCT blocks to be processed. */
|
---|
550 | for (block_row = 0; block_row < block_rows; block_row++) {
|
---|
551 | buffer_ptr = buffer[block_row];
|
---|
552 | if (first_row && block_row == 0)
|
---|
553 | prev_block_row = buffer_ptr;
|
---|
554 | else
|
---|
555 | prev_block_row = buffer[block_row-1];
|
---|
556 | if (last_row && block_row == block_rows-1)
|
---|
557 | next_block_row = buffer_ptr;
|
---|
558 | else
|
---|
559 | next_block_row = buffer[block_row+1];
|
---|
560 | /* We fetch the surrounding DC values using a sliding-register approach.
|
---|
561 | * Initialize all nine here so as to do the right thing on narrow pics.
|
---|
562 | */
|
---|
563 | DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
|
---|
564 | DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
|
---|
565 | DC7 = DC8 = DC9 = (int) next_block_row[0][0];
|
---|
566 | output_col = 0;
|
---|
567 | last_block_column = compptr->width_in_blocks - 1;
|
---|
568 | for (block_num = 0; block_num <= last_block_column; block_num++) {
|
---|
569 | /* Fetch current DCT block into workspace so we can modify it. */
|
---|
570 | jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
|
---|
571 | /* Update DC values */
|
---|
572 | if (block_num < last_block_column) {
|
---|
573 | DC3 = (int) prev_block_row[1][0];
|
---|
574 | DC6 = (int) buffer_ptr[1][0];
|
---|
575 | DC9 = (int) next_block_row[1][0];
|
---|
576 | }
|
---|
577 | /* Compute coefficient estimates per K.8.
|
---|
578 | * An estimate is applied only if coefficient is still zero,
|
---|
579 | * and is not known to be fully accurate.
|
---|
580 | */
|
---|
581 | /* AC01 */
|
---|
582 | if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
|
---|
583 | num = 36 * Q00 * (DC4 - DC6);
|
---|
584 | if (num >= 0) {
|
---|
585 | pred = (int) (((Q01<<7) + num) / (Q01<<8));
|
---|
586 | if (Al > 0 && pred >= (1<<Al))
|
---|
587 | pred = (1<<Al)-1;
|
---|
588 | } else {
|
---|
589 | pred = (int) (((Q01<<7) - num) / (Q01<<8));
|
---|
590 | if (Al > 0 && pred >= (1<<Al))
|
---|
591 | pred = (1<<Al)-1;
|
---|
592 | pred = -pred;
|
---|
593 | }
|
---|
594 | workspace[1] = (JCOEF) pred;
|
---|
595 | }
|
---|
596 | /* AC10 */
|
---|
597 | if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
|
---|
598 | num = 36 * Q00 * (DC2 - DC8);
|
---|
599 | if (num >= 0) {
|
---|
600 | pred = (int) (((Q10<<7) + num) / (Q10<<8));
|
---|
601 | if (Al > 0 && pred >= (1<<Al))
|
---|
602 | pred = (1<<Al)-1;
|
---|
603 | } else {
|
---|
604 | pred = (int) (((Q10<<7) - num) / (Q10<<8));
|
---|
605 | if (Al > 0 && pred >= (1<<Al))
|
---|
606 | pred = (1<<Al)-1;
|
---|
607 | pred = -pred;
|
---|
608 | }
|
---|
609 | workspace[8] = (JCOEF) pred;
|
---|
610 | }
|
---|
611 | /* AC20 */
|
---|
612 | if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
|
---|
613 | num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
|
---|
614 | if (num >= 0) {
|
---|
615 | pred = (int) (((Q20<<7) + num) / (Q20<<8));
|
---|
616 | if (Al > 0 && pred >= (1<<Al))
|
---|
617 | pred = (1<<Al)-1;
|
---|
618 | } else {
|
---|
619 | pred = (int) (((Q20<<7) - num) / (Q20<<8));
|
---|
620 | if (Al > 0 && pred >= (1<<Al))
|
---|
621 | pred = (1<<Al)-1;
|
---|
622 | pred = -pred;
|
---|
623 | }
|
---|
624 | workspace[16] = (JCOEF) pred;
|
---|
625 | }
|
---|
626 | /* AC11 */
|
---|
627 | if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
|
---|
628 | num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
|
---|
629 | if (num >= 0) {
|
---|
630 | pred = (int) (((Q11<<7) + num) / (Q11<<8));
|
---|
631 | if (Al > 0 && pred >= (1<<Al))
|
---|
632 | pred = (1<<Al)-1;
|
---|
633 | } else {
|
---|
634 | pred = (int) (((Q11<<7) - num) / (Q11<<8));
|
---|
635 | if (Al > 0 && pred >= (1<<Al))
|
---|
636 | pred = (1<<Al)-1;
|
---|
637 | pred = -pred;
|
---|
638 | }
|
---|
639 | workspace[9] = (JCOEF) pred;
|
---|
640 | }
|
---|
641 | /* AC02 */
|
---|
642 | if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
|
---|
643 | num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
|
---|
644 | if (num >= 0) {
|
---|
645 | pred = (int) (((Q02<<7) + num) / (Q02<<8));
|
---|
646 | if (Al > 0 && pred >= (1<<Al))
|
---|
647 | pred = (1<<Al)-1;
|
---|
648 | } else {
|
---|
649 | pred = (int) (((Q02<<7) - num) / (Q02<<8));
|
---|
650 | if (Al > 0 && pred >= (1<<Al))
|
---|
651 | pred = (1<<Al)-1;
|
---|
652 | pred = -pred;
|
---|
653 | }
|
---|
654 | workspace[2] = (JCOEF) pred;
|
---|
655 | }
|
---|
656 | /* OK, do the IDCT */
|
---|
657 | (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
|
---|
658 | output_ptr, output_col);
|
---|
659 | /* Advance for next column */
|
---|
660 | DC1 = DC2; DC2 = DC3;
|
---|
661 | DC4 = DC5; DC5 = DC6;
|
---|
662 | DC7 = DC8; DC8 = DC9;
|
---|
663 | buffer_ptr++, prev_block_row++, next_block_row++;
|
---|
664 | output_col += compptr->DCT_scaled_size;
|
---|
665 | }
|
---|
666 | output_ptr += compptr->DCT_scaled_size;
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
|
---|
671 | return JPEG_ROW_COMPLETED;
|
---|
672 | return JPEG_SCAN_COMPLETED;
|
---|
673 | }
|
---|
674 |
|
---|
675 | #endif /* BLOCK_SMOOTHING_SUPPORTED */
|
---|
676 |
|
---|
677 |
|
---|
678 | /*
|
---|
679 | * Initialize coefficient buffer controller.
|
---|
680 | */
|
---|
681 |
|
---|
682 | GLOBAL(void)
|
---|
683 | jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
|
---|
684 | {
|
---|
685 | my_coef_ptr coef;
|
---|
686 |
|
---|
687 | coef = (my_coef_ptr)
|
---|
688 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
---|
689 | SIZEOF(my_coef_controller));
|
---|
690 | cinfo->coef = (struct jpeg_d_coef_controller *) coef;
|
---|
691 | coef->pub.start_input_pass = start_input_pass;
|
---|
692 | coef->pub.start_output_pass = start_output_pass;
|
---|
693 | #ifdef BLOCK_SMOOTHING_SUPPORTED
|
---|
694 | coef->coef_bits_latch = NULL;
|
---|
695 | #endif
|
---|
696 |
|
---|
697 | /* Create the coefficient buffer. */
|
---|
698 | if (need_full_buffer) {
|
---|
699 | #ifdef D_MULTISCAN_FILES_SUPPORTED
|
---|
700 | /* Allocate a full-image virtual array for each component, */
|
---|
701 | /* padded to a multiple of samp_factor DCT blocks in each direction. */
|
---|
702 | /* Note we ask for a pre-zeroed array. */
|
---|
703 | int ci, access_rows;
|
---|
704 | jpeg_component_info *compptr;
|
---|
705 |
|
---|
706 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
---|
707 | ci++, compptr++) {
|
---|
708 | access_rows = compptr->v_samp_factor;
|
---|
709 | #ifdef BLOCK_SMOOTHING_SUPPORTED
|
---|
710 | /* If block smoothing could be used, need a bigger window */
|
---|
711 | if (cinfo->progressive_mode)
|
---|
712 | access_rows *= 3;
|
---|
713 | #endif
|
---|
714 | coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
|
---|
715 | ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
|
---|
716 | (JDIMENSION) jround_up((long) compptr->width_in_blocks,
|
---|
717 | (long) compptr->h_samp_factor),
|
---|
718 | (JDIMENSION) jround_up((long) compptr->height_in_blocks,
|
---|
719 | (long) compptr->v_samp_factor),
|
---|
720 | (JDIMENSION) access_rows);
|
---|
721 | }
|
---|
722 | coef->pub.consume_data = consume_data;
|
---|
723 | coef->pub.decompress_data = decompress_data;
|
---|
724 | coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
|
---|
725 | #else
|
---|
726 | ERREXIT(cinfo, JERR_NOT_COMPILED);
|
---|
727 | #endif
|
---|
728 | } else {
|
---|
729 | /* We only need a single-MCU buffer. */
|
---|
730 | JBLOCKROW buffer;
|
---|
731 | int i;
|
---|
732 |
|
---|
733 | buffer = (JBLOCKROW)
|
---|
734 | (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
---|
735 | D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
|
---|
736 | for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
|
---|
737 | coef->MCU_buffer[i] = buffer + i;
|
---|
738 | }
|
---|
739 | coef->pub.consume_data = dummy_consume_data;
|
---|
740 | coef->pub.decompress_data = decompress_onepass;
|
---|
741 | coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */
|
---|
742 | }
|
---|
743 | }
|
---|