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