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