Overhauled GRUB menus to reduce number of entries, mainly by making use
[cake.git] / compiler / libjpeg / main / jdcoefct.c
blob2fdec722a5909b9e7676ea10d5451ff2d3561e71
1 /*
2 $Id$
3 */
5 /*
6 * jdcoefct.c
8 * Copyright (C) 1994-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains the coefficient buffer controller for decompression.
13 * This controller is the top level of the lossy JPEG decompressor proper.
14 * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
16 * In buffered-image mode, this controller is the interface between
17 * input-oriented processing and output-oriented processing.
18 * Also, the input side (only) is used when reading a file for transcoding.
21 #define JPEG_INTERNALS
22 #include "jinclude.h"
23 #include "jpeglib.h"
24 #include "jlossy.h"
26 /* Block smoothing is only applicable for progressive JPEG, so: */
27 #ifndef D_PROGRESSIVE_SUPPORTED
28 #undef BLOCK_SMOOTHING_SUPPORTED
29 #endif
31 /* Private buffer controller object */
33 typedef struct {
34 /* These variables keep track of the current location of the input side. */
35 /* cinfo->input_iMCU_row is also used for this. */
36 JDIMENSION MCU_ctr; /* counts MCUs processed in current row */
37 int MCU_vert_offset; /* counts MCU rows within iMCU row */
38 int MCU_rows_per_iMCU_row; /* number of such rows needed */
40 /* The output side's location is represented by cinfo->output_iMCU_row. */
42 /* In single-pass modes, it's sufficient to buffer just one MCU.
43 * We allocate a workspace of D_MAX_DATA_UNITS_IN_MCU coefficient blocks,
44 * and let the entropy decoder write into that workspace each time.
45 * (On 80x86, the workspace is FAR even though it's not really very big;
46 * this is to keep the module interfaces unchanged when a large coefficient
47 * buffer is necessary.)
48 * In multi-pass modes, this array points to the current MCU's blocks
49 * within the virtual arrays; it is used only by the input side.
51 JBLOCKROW MCU_buffer[D_MAX_DATA_UNITS_IN_MCU];
53 #ifdef D_MULTISCAN_FILES_SUPPORTED
54 /* In multi-pass modes, we need a virtual block array for each component. */
55 jvirt_barray_ptr whole_image[MAX_COMPONENTS];
56 #endif
58 #ifdef BLOCK_SMOOTHING_SUPPORTED
59 /* When doing block smoothing, we latch coefficient Al values here */
60 int * coef_bits_latch;
61 #define SAVED_COEFS 6 /* we save coef_bits[0..5] */
62 #endif
63 } d_coef_controller;
65 typedef d_coef_controller * d_coef_ptr;
67 /* Forward declarations */
68 METHODDEF(int) decompress_onepass
69 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
70 #ifdef D_MULTISCAN_FILES_SUPPORTED
71 METHODDEF(int) decompress_data
72 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
73 #endif
74 #ifdef BLOCK_SMOOTHING_SUPPORTED
75 LOCAL(boolean) smoothing_ok JPP((j_decompress_ptr cinfo));
76 METHODDEF(int) decompress_smooth_data
77 JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
78 #endif
81 LOCAL(void)
82 start_iMCU_row (j_decompress_ptr cinfo)
83 /* Reset within-iMCU-row counters for a new row (input side) */
85 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
86 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
88 /* In an interleaved scan, an MCU row is the same as an iMCU row.
89 * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
90 * But at the bottom of the image, process only what's left.
92 if (cinfo->comps_in_scan > 1) {
93 coef->MCU_rows_per_iMCU_row = 1;
94 } else {
95 if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))
96 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
97 else
98 coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
101 coef->MCU_ctr = 0;
102 coef->MCU_vert_offset = 0;
107 * Initialize for an input processing pass.
110 METHODDEF(void)
111 start_input_pass (j_decompress_ptr cinfo)
113 cinfo->input_iMCU_row = 0;
114 start_iMCU_row(cinfo);
119 * Initialize for an output processing pass.
122 METHODDEF(void)
123 start_output_pass (j_decompress_ptr cinfo)
125 #ifdef BLOCK_SMOOTHING_SUPPORTED
126 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
127 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
129 /* If multipass, check to see whether to use block smoothing on this pass */
130 if (lossyd->coef_arrays != NULL) {
131 if (cinfo->do_block_smoothing && smoothing_ok(cinfo))
132 lossyd->pub.decompress_data = decompress_smooth_data;
133 else
134 lossyd->pub.decompress_data = decompress_data;
136 #endif
137 cinfo->output_iMCU_row = 0;
142 * Decompress and return some data in the single-pass case.
143 * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
144 * Input and output must run in lockstep since we have only a one-MCU buffer.
145 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
147 * NB: output_buf contains a plane for each component in image,
148 * which we index according to the component's SOF position.
151 METHODDEF(int)
152 decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
154 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
155 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
156 JDIMENSION MCU_col_num; /* index of current MCU within row */
157 JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
158 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
159 int blkn, ci, xindex, yindex, yoffset, useful_width;
160 JSAMPARRAY output_ptr;
161 JDIMENSION start_col, output_col;
162 jpeg_component_info *compptr;
163 inverse_DCT_method_ptr inverse_DCT;
165 /* Loop to process as much as one whole iMCU row */
166 for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
167 yoffset++) {
168 for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;
169 MCU_col_num++) {
170 /* Try to fetch an MCU. Entropy decoder expects buffer to be zeroed. */
171 jzero_far((void FAR *) coef->MCU_buffer[0],
172 (size_t) (cinfo->data_units_in_MCU * SIZEOF(JBLOCK)));
173 if (! (*lossyd->entropy_decode_mcu) (cinfo, coef->MCU_buffer)) {
174 /* Suspension forced; update state counters and exit */
175 coef->MCU_vert_offset = yoffset;
176 coef->MCU_ctr = MCU_col_num;
177 return JPEG_SUSPENDED;
179 /* Determine where data should go in output_buf and do the IDCT thing.
180 * We skip dummy blocks at the right and bottom edges (but blkn gets
181 * incremented past them!). Note the inner loop relies on having
182 * allocated the MCU_buffer[] blocks sequentially.
184 blkn = 0; /* index of current DCT block within MCU */
185 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
186 compptr = cinfo->cur_comp_info[ci];
187 /* Don't bother to IDCT an uninteresting component. */
188 if (! compptr->component_needed) {
189 blkn += compptr->MCU_data_units;
190 continue;
192 inverse_DCT = lossyd->inverse_DCT[compptr->component_index];
193 useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
194 : compptr->last_col_width;
195 output_ptr = output_buf[compptr->component_index] +
196 yoffset * compptr->codec_data_unit;
197 start_col = MCU_col_num * compptr->MCU_sample_width;
198 for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
199 if (cinfo->input_iMCU_row < last_iMCU_row ||
200 yoffset+yindex < compptr->last_row_height) {
201 output_col = start_col;
202 for (xindex = 0; xindex < useful_width; xindex++) {
203 (*inverse_DCT) (cinfo, compptr,
204 (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
205 output_ptr, output_col);
206 output_col += compptr->codec_data_unit;
209 blkn += compptr->MCU_width;
210 output_ptr += compptr->codec_data_unit;
214 /* Completed an MCU row, but perhaps not an iMCU row */
215 coef->MCU_ctr = 0;
217 /* Completed the iMCU row, advance counters for next one */
218 cinfo->output_iMCU_row++;
219 if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
220 start_iMCU_row(cinfo);
221 return JPEG_ROW_COMPLETED;
223 /* Completed the scan */
224 (*cinfo->inputctl->finish_input_pass) (cinfo);
225 return JPEG_SCAN_COMPLETED;
230 * Dummy consume-input routine for single-pass operation.
233 METHODDEF(int)
234 dummy_consume_data (j_decompress_ptr cinfo)
236 return JPEG_SUSPENDED; /* Always indicate nothing was done */
240 #ifdef D_MULTISCAN_FILES_SUPPORTED
243 * Consume input data and store it in the full-image coefficient buffer.
244 * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
245 * ie, v_samp_factor block rows for each component in the scan.
246 * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
249 METHODDEF(int)
250 consume_data (j_decompress_ptr cinfo)
252 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
253 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
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;
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.
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++;
291 /* Try to fetch the MCU. */
292 if (! (*lossyd->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;
299 /* Completed an MCU row, but perhaps not an iMCU row */
300 coef->MCU_ctr = 0;
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;
307 /* Completed the scan */
308 (*cinfo->inputctl->finish_input_pass) (cinfo);
309 return JPEG_SCAN_COMPLETED;
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.
318 * NB: output_buf contains a plane for each component in image.
321 METHODDEF(int)
322 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
324 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
325 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
326 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
327 JDIMENSION block_num;
328 int ci, block_row, block_rows;
329 JBLOCKARRAY buffer;
330 JBLOCKROW buffer_ptr;
331 JSAMPARRAY output_ptr;
332 JDIMENSION output_col;
333 jpeg_component_info *compptr;
334 inverse_DCT_method_ptr inverse_DCT;
336 /* Force some input to be done if we are getting ahead of the input. */
337 while (cinfo->input_scan_number < cinfo->output_scan_number ||
338 (cinfo->input_scan_number == cinfo->output_scan_number &&
339 cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
340 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
341 return JPEG_SUSPENDED;
344 /* OK, output from the virtual arrays. */
345 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
346 ci++, compptr++) {
347 /* Don't bother to IDCT an uninteresting component. */
348 if (! compptr->component_needed)
349 continue;
350 /* Align the virtual buffer for this component. */
351 buffer = (*cinfo->mem->access_virt_barray)
352 ((j_common_ptr) cinfo, coef->whole_image[ci],
353 cinfo->output_iMCU_row * compptr->v_samp_factor,
354 (JDIMENSION) compptr->v_samp_factor, FALSE);
355 /* Count non-dummy DCT block rows in this iMCU row. */
356 if (cinfo->output_iMCU_row < last_iMCU_row)
357 block_rows = compptr->v_samp_factor;
358 else {
359 /* NB: can't use last_row_height here; it is input-side-dependent! */
360 block_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
361 if (block_rows == 0) block_rows = compptr->v_samp_factor;
363 inverse_DCT = lossyd->inverse_DCT[ci];
364 output_ptr = output_buf[ci];
365 /* Loop over all DCT blocks to be processed. */
366 for (block_row = 0; block_row < block_rows; block_row++) {
367 buffer_ptr = buffer[block_row];
368 output_col = 0;
369 for (block_num = 0; block_num < compptr->width_in_data_units; block_num++) {
370 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
371 output_ptr, output_col);
372 buffer_ptr++;
373 output_col += compptr->codec_data_unit;
375 output_ptr += compptr->codec_data_unit;
379 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
380 return JPEG_ROW_COMPLETED;
381 return JPEG_SCAN_COMPLETED;
384 #endif /* D_MULTISCAN_FILES_SUPPORTED */
387 #ifdef BLOCK_SMOOTHING_SUPPORTED
390 * This code applies interblock smoothing as described by section K.8
391 * of the JPEG standard: the first 5 AC coefficients are estimated from
392 * the DC values of a DCT block and its 8 neighboring blocks.
393 * We apply smoothing only for progressive JPEG decoding, and only if
394 * the coefficients it can estimate are not yet known to full precision.
397 /* Natural-order array positions of the first 5 zigzag-order coefficients */
398 #define Q01_POS 1
399 #define Q10_POS 8
400 #define Q20_POS 16
401 #define Q11_POS 9
402 #define Q02_POS 2
405 * Determine whether block smoothing is applicable and safe.
406 * We also latch the current states of the coef_bits[] entries for the
407 * AC coefficients; otherwise, if the input side of the decompressor
408 * advances into a new scan, we might think the coefficients are known
409 * more accurately than they really are.
412 LOCAL(boolean)
413 smoothing_ok (j_decompress_ptr cinfo)
415 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
416 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
417 boolean smoothing_useful = FALSE;
418 int ci, coefi;
419 jpeg_component_info *compptr;
420 JQUANT_TBL * qtable;
421 int * coef_bits;
422 int * coef_bits_latch;
424 if (! cinfo->process == JPROC_PROGRESSIVE || cinfo->coef_bits == NULL)
425 return FALSE;
427 /* Allocate latch area if not already done */
428 if (coef->coef_bits_latch == NULL)
429 coef->coef_bits_latch = (int *)
430 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
431 cinfo->num_components *
432 (SAVED_COEFS * SIZEOF(int)));
433 coef_bits_latch = coef->coef_bits_latch;
435 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
436 ci++, compptr++) {
437 /* All components' quantization values must already be latched. */
438 if ((qtable = compptr->quant_table) == NULL)
439 return FALSE;
440 /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
441 if (qtable->quantval[0] == 0 ||
442 qtable->quantval[Q01_POS] == 0 ||
443 qtable->quantval[Q10_POS] == 0 ||
444 qtable->quantval[Q20_POS] == 0 ||
445 qtable->quantval[Q11_POS] == 0 ||
446 qtable->quantval[Q02_POS] == 0)
447 return FALSE;
448 /* DC values must be at least partly known for all components. */
449 coef_bits = cinfo->coef_bits[ci];
450 if (coef_bits[0] < 0)
451 return FALSE;
452 /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
453 for (coefi = 1; coefi <= 5; coefi++) {
454 coef_bits_latch[coefi] = coef_bits[coefi];
455 if (coef_bits[coefi] != 0)
456 smoothing_useful = TRUE;
458 coef_bits_latch += SAVED_COEFS;
461 return smoothing_useful;
466 * Variant of decompress_data for use when doing block smoothing.
469 METHODDEF(int)
470 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
472 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
473 d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
474 JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
475 JDIMENSION block_num, last_block_column;
476 int ci, block_row, block_rows, access_rows;
477 JBLOCKARRAY buffer;
478 JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
479 JSAMPARRAY output_ptr;
480 JDIMENSION output_col;
481 jpeg_component_info *compptr;
482 inverse_DCT_method_ptr inverse_DCT;
483 boolean first_row, last_row;
484 JBLOCK workspace;
485 int *coef_bits;
486 JQUANT_TBL *quanttbl;
487 INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
488 int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
489 int Al, pred;
491 /* Force some input to be done if we are getting ahead of the input. */
492 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
493 ! cinfo->inputctl->eoi_reached) {
494 if (cinfo->input_scan_number == cinfo->output_scan_number) {
495 /* If input is working on current scan, we ordinarily want it to
496 * have completed the current row. But if input scan is DC,
497 * we want it to keep one row ahead so that next block row's DC
498 * values are up to date.
500 JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
501 if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
502 break;
504 if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
505 return JPEG_SUSPENDED;
508 /* OK, output from the virtual arrays. */
509 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
510 ci++, compptr++) {
511 /* Don't bother to IDCT an uninteresting component. */
512 if (! compptr->component_needed)
513 continue;
514 /* Count non-dummy DCT block rows in this iMCU row. */
515 if (cinfo->output_iMCU_row < last_iMCU_row) {
516 block_rows = compptr->v_samp_factor;
517 access_rows = block_rows * 2; /* this and next iMCU row */
518 last_row = FALSE;
519 } else {
520 /* NB: can't use last_row_height here; it is input-side-dependent! */
521 block_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
522 if (block_rows == 0) block_rows = compptr->v_samp_factor;
523 access_rows = block_rows; /* this iMCU row only */
524 last_row = TRUE;
526 /* Align the virtual buffer for this component. */
527 if (cinfo->output_iMCU_row > 0) {
528 access_rows += compptr->v_samp_factor; /* prior iMCU row too */
529 buffer = (*cinfo->mem->access_virt_barray)
530 ((j_common_ptr) cinfo, coef->whole_image[ci],
531 (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
532 (JDIMENSION) access_rows, FALSE);
533 buffer += compptr->v_samp_factor; /* point to current iMCU row */
534 first_row = FALSE;
535 } else {
536 buffer = (*cinfo->mem->access_virt_barray)
537 ((j_common_ptr) cinfo, coef->whole_image[ci],
538 (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
539 first_row = TRUE;
541 /* Fetch component-dependent info */
542 coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
543 quanttbl = compptr->quant_table;
544 Q00 = quanttbl->quantval[0];
545 Q01 = quanttbl->quantval[Q01_POS];
546 Q10 = quanttbl->quantval[Q10_POS];
547 Q20 = quanttbl->quantval[Q20_POS];
548 Q11 = quanttbl->quantval[Q11_POS];
549 Q02 = quanttbl->quantval[Q02_POS];
550 inverse_DCT = lossyd->inverse_DCT[ci];
551 output_ptr = output_buf[ci];
552 /* Loop over all DCT blocks to be processed. */
553 for (block_row = 0; block_row < block_rows; block_row++) {
554 buffer_ptr = buffer[block_row];
555 if (first_row && block_row == 0)
556 prev_block_row = buffer_ptr;
557 else
558 prev_block_row = buffer[block_row-1];
559 if (last_row && block_row == block_rows-1)
560 next_block_row = buffer_ptr;
561 else
562 next_block_row = buffer[block_row+1];
563 /* We fetch the surrounding DC values using a sliding-register approach.
564 * Initialize all nine here so as to do the right thing on narrow pics.
566 DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
567 DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
568 DC7 = DC8 = DC9 = (int) next_block_row[0][0];
569 output_col = 0;
570 last_block_column = compptr->width_in_data_units - 1;
571 for (block_num = 0; block_num <= last_block_column; block_num++) {
572 /* Fetch current DCT block into workspace so we can modify it. */
573 jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
574 /* Update DC values */
575 if (block_num < last_block_column) {
576 DC3 = (int) prev_block_row[1][0];
577 DC6 = (int) buffer_ptr[1][0];
578 DC9 = (int) next_block_row[1][0];
580 /* Compute coefficient estimates per K.8.
581 * An estimate is applied only if coefficient is still zero,
582 * and is not known to be fully accurate.
584 /* AC01 */
585 if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
586 num = 36 * Q00 * (DC4 - DC6);
587 if (num >= 0) {
588 pred = (int) (((Q01<<7) + num) / (Q01<<8));
589 if (Al > 0 && pred >= (1<<Al))
590 pred = (1<<Al)-1;
591 } else {
592 pred = (int) (((Q01<<7) - num) / (Q01<<8));
593 if (Al > 0 && pred >= (1<<Al))
594 pred = (1<<Al)-1;
595 pred = -pred;
597 workspace[1] = (JCOEF) pred;
599 /* AC10 */
600 if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
601 num = 36 * Q00 * (DC2 - DC8);
602 if (num >= 0) {
603 pred = (int) (((Q10<<7) + num) / (Q10<<8));
604 if (Al > 0 && pred >= (1<<Al))
605 pred = (1<<Al)-1;
606 } else {
607 pred = (int) (((Q10<<7) - num) / (Q10<<8));
608 if (Al > 0 && pred >= (1<<Al))
609 pred = (1<<Al)-1;
610 pred = -pred;
612 workspace[8] = (JCOEF) pred;
614 /* AC20 */
615 if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
616 num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
617 if (num >= 0) {
618 pred = (int) (((Q20<<7) + num) / (Q20<<8));
619 if (Al > 0 && pred >= (1<<Al))
620 pred = (1<<Al)-1;
621 } else {
622 pred = (int) (((Q20<<7) - num) / (Q20<<8));
623 if (Al > 0 && pred >= (1<<Al))
624 pred = (1<<Al)-1;
625 pred = -pred;
627 workspace[16] = (JCOEF) pred;
629 /* AC11 */
630 if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
631 num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
632 if (num >= 0) {
633 pred = (int) (((Q11<<7) + num) / (Q11<<8));
634 if (Al > 0 && pred >= (1<<Al))
635 pred = (1<<Al)-1;
636 } else {
637 pred = (int) (((Q11<<7) - num) / (Q11<<8));
638 if (Al > 0 && pred >= (1<<Al))
639 pred = (1<<Al)-1;
640 pred = -pred;
642 workspace[9] = (JCOEF) pred;
644 /* AC02 */
645 if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
646 num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
647 if (num >= 0) {
648 pred = (int) (((Q02<<7) + num) / (Q02<<8));
649 if (Al > 0 && pred >= (1<<Al))
650 pred = (1<<Al)-1;
651 } else {
652 pred = (int) (((Q02<<7) - num) / (Q02<<8));
653 if (Al > 0 && pred >= (1<<Al))
654 pred = (1<<Al)-1;
655 pred = -pred;
657 workspace[2] = (JCOEF) pred;
659 /* OK, do the IDCT */
660 (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
661 output_ptr, output_col);
662 /* Advance for next column */
663 DC1 = DC2; DC2 = DC3;
664 DC4 = DC5; DC5 = DC6;
665 DC7 = DC8; DC8 = DC9;
666 buffer_ptr++, prev_block_row++, next_block_row++;
667 output_col += compptr->codec_data_unit;
669 output_ptr += compptr->codec_data_unit;
673 if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
674 return JPEG_ROW_COMPLETED;
675 return JPEG_SCAN_COMPLETED;
678 #endif /* BLOCK_SMOOTHING_SUPPORTED */
682 * Initialize coefficient buffer controller.
685 JGLOBAL(void)
686 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
688 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
689 d_coef_ptr coef;
691 coef = (d_coef_ptr)
692 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
693 SIZEOF(d_coef_controller));
694 lossyd->coef_private = (void *) coef;
695 lossyd->coef_start_input_pass = start_input_pass;
696 lossyd->coef_start_output_pass = start_output_pass;
697 #ifdef BLOCK_SMOOTHING_SUPPORTED
698 coef->coef_bits_latch = NULL;
699 #endif
701 /* Create the coefficient buffer. */
702 if (need_full_buffer) {
703 #ifdef D_MULTISCAN_FILES_SUPPORTED
704 /* Allocate a full-image virtual array for each component, */
705 /* padded to a multiple of samp_factor DCT blocks in each direction. */
706 /* Note we ask for a pre-zeroed array. */
707 int ci, access_rows;
708 jpeg_component_info *compptr;
710 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
711 ci++, compptr++) {
712 access_rows = compptr->v_samp_factor;
713 #ifdef BLOCK_SMOOTHING_SUPPORTED
714 /* If block smoothing could be used, need a bigger window */
715 if (cinfo->process == JPROC_PROGRESSIVE)
716 access_rows *= 3;
717 #endif
718 coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
719 ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
720 (JDIMENSION) jround_up((long) compptr->width_in_data_units,
721 (long) compptr->h_samp_factor),
722 (JDIMENSION) jround_up((long) compptr->height_in_data_units,
723 (long) compptr->v_samp_factor),
724 (JDIMENSION) access_rows);
726 lossyd->pub.consume_data = consume_data;
727 lossyd->pub.decompress_data = decompress_data;
728 lossyd->coef_arrays = coef->whole_image; /* link to virtual arrays */
729 #else
730 ERREXIT(cinfo, JERR_NOT_COMPILED);
731 #endif
732 } else {
733 /* We only need a single-MCU buffer. */
734 JBLOCKROW buffer;
735 int i;
737 buffer = (JBLOCKROW)
738 (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
739 D_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
740 for (i = 0; i < D_MAX_DATA_UNITS_IN_MCU; i++) {
741 coef->MCU_buffer[i] = buffer + i;
743 lossyd->pub.consume_data = dummy_consume_data;
744 lossyd->pub.decompress_data = decompress_onepass;
745 lossyd->coef_arrays = NULL; /* flag for no virtual arrays */