2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdcoefct.c /main/2 1996/05/09 03:46:54 drk $ */
27 * Copyright (C) 1994-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains the coefficient buffer controller for decompression.
32 * This controller is the top level of the JPEG decompressor proper.
33 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
35 * In buffered-image mode, this controller is the interface between
36 * input-oriented processing and output-oriented processing.
37 * Also, the input side (only) is used when reading a file for transcoding.
40 #define JPEG_INTERNALS
44 /* Block smoothing is only applicable for progressive JPEG, so: */
45 #ifndef D_PROGRESSIVE_SUPPORTED
46 #undef BLOCK_SMOOTHING_SUPPORTED
49 /* Private buffer controller object */
52 struct jpeg_d_coef_controller pub; /* public fields */
54 /* These variables keep track of the current location of the input side. */
55 /* cinfo->input_iMCU_row is also used for this. */
56 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
57 int MCU_vert_offset; /* counts MCU rows within iMCU row */
58 int MCU_rows_per_iMCU_row; /* number of such rows needed */
60 /* The output side's location is represented by cinfo->output_iMCU_row. */
62 /* In single-pass modes, it's sufficient to buffer just one MCU.
63 * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
64 * and let the entropy decoder write into that workspace each time.
65 * (On 80x86, the workspace is FAR even though it's not really very big;
66 * this is to keep the module interfaces unchanged when a large coefficient
67 * buffer is necessary.)
68 * In multi-pass modes, this array points to the current MCU's blocks
69 * within the virtual arrays; it is used only by the input side.
71 JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];
73 #ifdef D_MULTISCAN_FILES_SUPPORTED
74 /* In multi-pass modes, we need a virtual block array for each component. */
75 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
78 #ifdef BLOCK_SMOOTHING_SUPPORTED
79 /* When doing block smoothing, we latch coefficient Al values here */
80 int * coef_bits_latch;
81 #define SAVED_COEFS 6 /* we save coef_bits[0..5] */
85 typedef my_coef_controller * my_coef_ptr;
87 /* Forward declarations */
88 METHODDEF(int) decompress_onepass
89 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
90 #ifdef D_MULTISCAN_FILES_SUPPORTED
91 METHODDEF(int) decompress_data
92 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
94 #ifdef BLOCK_SMOOTHING_SUPPORTED
95 LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
96 METHODDEF(int) decompress_smooth_data
97 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
102 start_iMCU_row (j_decompress_ptr cinfo)
103 /* Reset within-iMCU-row counters for a new row (input side) */
105 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
107 /* In an interleaved scan, an MCU row is the same as an iMCU row.
108 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
109 * But at the bottom of the image, process only what's left.
111 if (cinfo->comps_in_scan > 1) {
112 coef->MCU_rows_per_iMCU_row = 1;
114 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
115 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
117 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
121 coef->MCU_vert_offset = 0;
126 * Initialize for an input processing pass.
130 start_input_pass (j_decompress_ptr cinfo)
132 cinfo->input_iMCU_row = 0;
133 start_iMCU_row(cinfo);
138 * Initialize for an output processing pass.
142 start_output_pass (j_decompress_ptr cinfo)
144 #ifdef BLOCK_SMOOTHING_SUPPORTED
145 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
147 /* If multipass, check to see whether to use block smoothing on this pass */
148 if (coef->pub.coef_arrays != NULL) {
149 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
150 coef->pub.decompress_data = decompress_smooth_data;
152 coef->pub.decompress_data = decompress_data;
155 cinfo->output_iMCU_row = 0;
160 * Decompress and return some data in the single-pass case.
161 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
162 * Input and output must run in lockstep since we have only a one-MCU buffer.
163 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
165 * NB: output_buf contains a plane for each component in image.
166 * For single pass, this is the same as the components in the scan.
170 decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
172 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
173 JDIMENSION MCU_col_num; /* index of current MCU within row */
174 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
175 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
176 int blkn, ci, xindex, yindex, yoffset, useful_width;
177 JSAMPARRAY output_ptr;
178 JDIMENSION start_col, output_col;
179 jpeg_component_info *compptr;
180 inverse_DCT_method_ptr inverse_DCT;
182 /* Loop to process as much as one whole iMCU row */
183 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
185 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
187 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
188 jzero_far((void FAR *) coef->MCU_buffer[0],
189 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
190 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
191 /* Suspension forced; update state counters and exit */
192 coef->MCU_vert_offset = yoffset;
193 coef->MCU_ctr = MCU_col_num;
194 return JPEG_SUSPENDED;
196 /* Determine where data should go in output_buf and do the IDCT thing.
197 * We skip dummy blocks at the right and bottom edges (but blkn gets
198 * incremented past them!). Note the inner loop relies on having
199 * allocated the MCU_buffer[] blocks sequentially.
201 blkn = 0; /* index of current DCT block within MCU */
202 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
203 compptr = cinfo->cur_comp_info[ci];
204 /* Don't bother to IDCT an uninteresting component. */
205 if (! compptr->component_needed) {
206 blkn += compptr->MCU_blocks;
209 inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
210 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
211 : compptr->last_col_width;
212 output_ptr = output_buf[ci] + yoffset * compptr->DCT_scaled_size;
213 start_col = MCU_col_num * compptr->MCU_sample_width;
214 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
215 if (cinfo->input_iMCU_row < last_iMCU_row ||
216 yoffset+yindex < compptr->last_row_height) {
217 output_col = start_col;
218 for (xindex = 0; xindex < useful_width; xindex++) {
219 (*inverse_DCT) (cinfo, compptr,
220 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
221 output_ptr, output_col);
222 output_col += compptr->DCT_scaled_size;
225 blkn += compptr->MCU_width;
226 output_ptr += compptr->DCT_scaled_size;
230 /* Completed an MCU row, but perhaps not an iMCU row */
233 /* Completed the iMCU row, advance counters for next one */
234 cinfo->output_iMCU_row++;
235 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
236 start_iMCU_row(cinfo);
237 return JPEG_ROW_COMPLETED;
239 /* Completed the scan */
240 (*cinfo->inputctl->finish_input_pass) (cinfo);
241 return JPEG_SCAN_COMPLETED;
246 * Dummy consume-input routine for single-pass operation.
250 dummy_consume_data (j_decompress_ptr cinfo)
252 return JPEG_SUSPENDED; /* Always indicate nothing was done */
256 #ifdef D_MULTISCAN_FILES_SUPPORTED
259 * Consume input data and store it in the full-image coefficient buffer.
260 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
261 * ie, v_samp_factor block rows for each component in the scan.
262 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
266 consume_data (j_decompress_ptr cinfo)
268 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
269 JDIMENSION MCU_col_num; /* index of current MCU within row */
270 int blkn, ci, xindex, yindex, yoffset;
271 JDIMENSION start_col;
272 JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
273 JBLOCKROW buffer_ptr;
274 jpeg_component_info *compptr;
276 /* Align the virtual buffers for the components used in this scan. */
277 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
278 compptr = cinfo->cur_comp_info[ci];
279 buffer[ci] = (*cinfo->mem->access_virt_barray)
280 ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
281 cinfo->input_iMCU_row * compptr->v_samp_factor,
282 (JDIMENSION) compptr->v_samp_factor, TRUE);
283 /* Note: entropy decoder expects buffer to be zeroed,
284 * but this is handled automatically by the memory manager
285 * because we requested a pre-zeroed array.
289 /* Loop to process one whole iMCU row */
290 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
292 for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
294 /* Construct list of pointers to DCT blocks belonging to this MCU */
295 blkn = 0; /* index of current DCT block within MCU */
296 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
297 compptr = cinfo->cur_comp_info[ci];
298 start_col = MCU_col_num * compptr->MCU_width;
299 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
300 buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
301 for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
302 coef->MCU_buffer[blkn++] = buffer_ptr++;
306 /* Try to fetch the MCU. */
307 if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
308 /* Suspension forced; update state counters and exit */
309 coef->MCU_vert_offset = yoffset;
310 coef->MCU_ctr = MCU_col_num;
311 return JPEG_SUSPENDED;
314 /* Completed an MCU row, but perhaps not an iMCU row */
317 /* Completed the iMCU row, advance counters for next one */
318 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
319 start_iMCU_row(cinfo);
320 return JPEG_ROW_COMPLETED;
322 /* Completed the scan */
323 (*cinfo->inputctl->finish_input_pass) (cinfo);
324 return JPEG_SCAN_COMPLETED;
329 * Decompress and return some data in the multi-pass case.
330 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
331 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
333 * NB: output_buf contains a plane for each component in image.
337 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
339 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
340 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
341 JDIMENSION block_num;
342 int ci, block_row, block_rows;
344 JBLOCKROW buffer_ptr;
345 JSAMPARRAY output_ptr;
346 JDIMENSION output_col;
347 jpeg_component_info *compptr;
348 inverse_DCT_method_ptr inverse_DCT;
350 /* Force some input to be done if we are getting ahead of the input. */
351 while (cinfo->input_scan_number < cinfo->output_scan_number ||
352 (cinfo->input_scan_number == cinfo->output_scan_number &&
353 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
354 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
355 return JPEG_SUSPENDED;
358 /* OK, output from the virtual arrays. */
359 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
361 /* Don't bother to IDCT an uninteresting component. */
362 if (! compptr->component_needed)
364 /* Align the virtual buffer for this component. */
365 buffer = (*cinfo->mem->access_virt_barray)
366 ((j_common_ptr) cinfo, coef->whole_image[ci],
367 cinfo->output_iMCU_row * compptr->v_samp_factor,
368 (JDIMENSION) compptr->v_samp_factor, FALSE);
369 /* Count non-dummy DCT block rows in this iMCU row. */
370 if (cinfo->output_iMCU_row < last_iMCU_row)
371 block_rows = compptr->v_samp_factor;
373 /* NB: can't use last_row_height here; it is input-side-dependent! */
374 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
375 if (block_rows == 0) block_rows = compptr->v_samp_factor;
377 inverse_DCT = cinfo->idct->inverse_DCT[ci];
378 output_ptr = output_buf[ci];
379 /* Loop over all DCT blocks to be processed. */
380 for (block_row = 0; block_row < block_rows; block_row++) {
381 buffer_ptr = buffer[block_row];
383 for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
384 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
385 output_ptr, output_col);
387 output_col += compptr->DCT_scaled_size;
389 output_ptr += compptr->DCT_scaled_size;
393 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
394 return JPEG_ROW_COMPLETED;
395 return JPEG_SCAN_COMPLETED;
398 #endif /* D_MULTISCAN_FILES_SUPPORTED */
401 #ifdef BLOCK_SMOOTHING_SUPPORTED
404 * This code applies interblock smoothing as described by section K.8
405 * of the JPEG standard: the first 5 AC coefficients are estimated from
406 * the DC values of a DCT block and its 8 neighboring blocks.
407 * We apply smoothing only for progressive JPEG decoding, and only if
408 * the coefficients it can estimate are not yet known to full precision.
411 /* Natural-order array positions of the first 5 zigzag-order coefficients */
419 * Determine whether block smoothing is applicable and safe.
420 * We also latch the current states of the coef_bits[] entries for the
421 * AC coefficients; otherwise, if the input side of the decompressor
422 * advances into a new scan, we might think the coefficients are known
423 * more accurately than they really are.
427 smoothing_ok (j_decompress_ptr cinfo)
429 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
430 boolean smoothing_useful = FALSE;
432 jpeg_component_info *compptr;
435 int * coef_bits_latch;
437 if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)
440 /* Allocate latch area if not already done */
441 if (coef->coef_bits_latch == NULL)
442 coef->coef_bits_latch = (int *)
443 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
444 cinfo->num_components *
445 (SAVED_COEFS * SIZEOF(int)));
446 coef_bits_latch = coef->coef_bits_latch;
448 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
450 /* All components' quantization values must already be latched. */
451 if ((qtable = compptr->quant_table) == NULL)
453 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
454 if (qtable->quantval[0] == 0 ||
455 qtable->quantval[Q01_POS] == 0 ||
456 qtable->quantval[Q10_POS] == 0 ||
457 qtable->quantval[Q20_POS] == 0 ||
458 qtable->quantval[Q11_POS] == 0 ||
459 qtable->quantval[Q02_POS] == 0)
461 /* DC values must be at least partly known for all components. */
462 coef_bits = cinfo->coef_bits[ci];
463 if (coef_bits[0] < 0)
465 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
466 for (coefi = 1; coefi <= 5; coefi++) {
467 coef_bits_latch[coefi] = coef_bits[coefi];
468 if (coef_bits[coefi] != 0)
469 smoothing_useful = TRUE;
471 coef_bits_latch += SAVED_COEFS;
474 return smoothing_useful;
479 * Variant of decompress_data for use when doing block smoothing.
483 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
485 my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
486 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
487 JDIMENSION block_num, last_block_column;
488 int ci, block_row, block_rows, access_rows;
490 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
491 JSAMPARRAY output_ptr;
492 JDIMENSION output_col;
493 jpeg_component_info *compptr;
494 inverse_DCT_method_ptr inverse_DCT;
495 boolean first_row, last_row;
498 JQUANT_TBL *quanttbl;
499 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
500 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
503 /* Force some input to be done if we are getting ahead of the input. */
504 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
505 ! cinfo->inputctl->eoi_reached) {
506 if (cinfo->input_scan_number == cinfo->output_scan_number) {
507 /* If input is working on current scan, we ordinarily want it to
508 * have completed the current row. But if input scan is DC,
509 * we want it to keep one row ahead so that next block row's DC
510 * values are up to date.
512 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
513 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
516 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
517 return JPEG_SUSPENDED;
520 /* OK, output from the virtual arrays. */
521 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
523 /* Don't bother to IDCT an uninteresting component. */
524 if (! compptr->component_needed)
526 /* Count non-dummy DCT block rows in this iMCU row. */
527 if (cinfo->output_iMCU_row < last_iMCU_row) {
528 block_rows = compptr->v_samp_factor;
529 access_rows = block_rows * 2; /* this and next iMCU row */
532 /* NB: can't use last_row_height here; it is input-side-dependent! */
533 block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
534 if (block_rows == 0) block_rows = compptr->v_samp_factor;
535 access_rows = block_rows; /* this iMCU row only */
538 /* Align the virtual buffer for this component. */
539 if (cinfo->output_iMCU_row > 0) {
540 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
541 buffer = (*cinfo->mem->access_virt_barray)
542 ((j_common_ptr) cinfo, coef->whole_image[ci],
543 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
544 (JDIMENSION) access_rows, FALSE);
545 buffer += compptr->v_samp_factor; /* point to current iMCU row */
548 buffer = (*cinfo->mem->access_virt_barray)
549 ((j_common_ptr) cinfo, coef->whole_image[ci],
550 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
553 /* Fetch component-dependent info */
554 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
555 quanttbl = compptr->quant_table;
556 Q00 = quanttbl->quantval[0];
557 Q01 = quanttbl->quantval[Q01_POS];
558 Q10 = quanttbl->quantval[Q10_POS];
559 Q20 = quanttbl->quantval[Q20_POS];
560 Q11 = quanttbl->quantval[Q11_POS];
561 Q02 = quanttbl->quantval[Q02_POS];
562 inverse_DCT = cinfo->idct->inverse_DCT[ci];
563 output_ptr = output_buf[ci];
564 /* Loop over all DCT blocks to be processed. */
565 for (block_row = 0; block_row < block_rows; block_row++) {
566 buffer_ptr = buffer[block_row];
567 if (first_row && block_row == 0)
568 prev_block_row = buffer_ptr;
570 prev_block_row = buffer[block_row-1];
571 if (last_row && block_row == block_rows-1)
572 next_block_row = buffer_ptr;
574 next_block_row = buffer[block_row+1];
575 /* We fetch the surrounding DC values using a sliding-register approach.
576 * Initialize all nine here so as to do the right thing on narrow pics.
578 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
579 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
580 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
582 last_block_column = compptr->width_in_blocks - 1;
583 for (block_num = 0; block_num <= last_block_column; block_num++) {
584 /* Fetch current DCT block into workspace so we can modify it. */
585 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
586 /* Update DC values */
587 if (block_num < last_block_column) {
588 DC3 = (int) prev_block_row[1][0];
589 DC6 = (int) buffer_ptr[1][0];
590 DC9 = (int) next_block_row[1][0];
592 /* Compute coefficient estimates per K.8.
593 * An estimate is applied only if coefficient is still zero,
594 * and is not known to be fully accurate.
597 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
598 num = 36 * Q00 * (DC4 - DC6);
600 pred = (int) (((Q01<<7) + num) / (Q01<<8));
601 if (Al > 0 && pred >= (1<<Al))
604 pred = (int) (((Q01<<7) - num) / (Q01<<8));
605 if (Al > 0 && pred >= (1<<Al))
609 workspace[1] = (JCOEF) pred;
612 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
613 num = 36 * Q00 * (DC2 - DC8);
615 pred = (int) (((Q10<<7) + num) / (Q10<<8));
616 if (Al > 0 && pred >= (1<<Al))
619 pred = (int) (((Q10<<7) - num) / (Q10<<8));
620 if (Al > 0 && pred >= (1<<Al))
624 workspace[8] = (JCOEF) pred;
627 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
628 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
630 pred = (int) (((Q20<<7) + num) / (Q20<<8));
631 if (Al > 0 && pred >= (1<<Al))
634 pred = (int) (((Q20<<7) - num) / (Q20<<8));
635 if (Al > 0 && pred >= (1<<Al))
639 workspace[16] = (JCOEF) pred;
642 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
643 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
645 pred = (int) (((Q11<<7) + num) / (Q11<<8));
646 if (Al > 0 && pred >= (1<<Al))
649 pred = (int) (((Q11<<7) - num) / (Q11<<8));
650 if (Al > 0 && pred >= (1<<Al))
654 workspace[9] = (JCOEF) pred;
657 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
658 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
660 pred = (int) (((Q02<<7) + num) / (Q02<<8));
661 if (Al > 0 && pred >= (1<<Al))
664 pred = (int) (((Q02<<7) - num) / (Q02<<8));
665 if (Al > 0 && pred >= (1<<Al))
669 workspace[2] = (JCOEF) pred;
671 /* OK, do the IDCT */
672 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
673 output_ptr, output_col);
674 /* Advance for next column */
675 DC1 = DC2; DC2 = DC3;
676 DC4 = DC5; DC5 = DC6;
677 DC7 = DC8; DC8 = DC9;
678 buffer_ptr++, prev_block_row++, next_block_row++;
679 output_col += compptr->DCT_scaled_size;
681 output_ptr += compptr->DCT_scaled_size;
685 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
686 return JPEG_ROW_COMPLETED;
687 return JPEG_SCAN_COMPLETED;
690 #endif /* BLOCK_SMOOTHING_SUPPORTED */
694 * Initialize coefficient buffer controller.
698 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
703 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
704 SIZEOF(my_coef_controller));
705 cinfo->coef = (struct jpeg_d_coef_controller *) coef;
706 coef->pub.start_input_pass = start_input_pass;
707 coef->pub.start_output_pass = start_output_pass;
708 #ifdef BLOCK_SMOOTHING_SUPPORTED
709 coef->coef_bits_latch = NULL;
712 /* Create the coefficient buffer. */
713 if (need_full_buffer) {
714 #ifdef D_MULTISCAN_FILES_SUPPORTED
715 /* Allocate a full-image virtual array for each component, */
716 /* padded to a multiple of samp_factor DCT blocks in each direction. */
717 /* Note we ask for a pre-zeroed array. */
719 jpeg_component_info *compptr;
721 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
723 access_rows = compptr->v_samp_factor;
724 #ifdef BLOCK_SMOOTHING_SUPPORTED
725 /* If block smoothing could be used, need a bigger window */
726 if (cinfo->progressive_mode)
729 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
730 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
731 (JDIMENSION) jround_up((long) compptr->width_in_blocks,
732 (long) compptr->h_samp_factor),
733 (JDIMENSION) jround_up((long) compptr->height_in_blocks,
734 (long) compptr->v_samp_factor),
735 (JDIMENSION) access_rows);
737 coef->pub.consume_data = consume_data;
738 coef->pub.decompress_data = decompress_data;
739 coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */
741 ERREXIT(cinfo, JERR_NOT_COMPILED);
744 /* We only need a single-MCU buffer. */
749 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
750 D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
751 for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {
752 coef->MCU_buffer[i] = buffer + i;
754 coef->pub.consume_data = dummy_consume_data;
755 coef->pub.decompress_data = decompress_onepass;
756 coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */