]> Creatis software - gdcm.git/blob - src/gdcmjpeg/jdcoefct.c
Fix mistypings
[gdcm.git] / src / gdcmjpeg / 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   (void)cinfo;
233   return JPEG_SUSPENDED;  /* Always indicate nothing was done */
234 }
235
236
237 #ifdef D_MULTISCAN_FILES_SUPPORTED
238
239 /*
240  * Consume input data and store it in the full-image coefficient buffer.
241  * We read as much as one fully interleaved MCU row ("iMCU" row) per call,
242  * ie, v_samp_factor block rows for each component in the scan.
243  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
244  */
245
246 METHODDEF(int)
247 consume_data (j_decompress_ptr cinfo)
248 {
249   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
250   d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
251   JDIMENSION MCU_col_num;  /* index of current MCU within row */
252   int blkn, ci, xindex, yindex, yoffset;
253   JDIMENSION start_col;
254   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
255   JBLOCKROW buffer_ptr;
256   jpeg_component_info *compptr;
257
258   /* Align the virtual buffers for the components used in this scan. */
259   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
260     compptr = cinfo->cur_comp_info[ci];
261     buffer[ci] = (*cinfo->mem->access_virt_barray)
262       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
263        cinfo->input_iMCU_row * compptr->v_samp_factor,
264        (JDIMENSION) compptr->v_samp_factor, TRUE);
265     /* Note: entropy decoder expects buffer to be zeroed,
266      * but this is handled automatically by the memory manager
267      * because we requested a pre-zeroed array.
268      */
269   }
270
271   /* Loop to process one whole iMCU row */
272   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
273        yoffset++) {
274     for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;
275    MCU_col_num++) {
276       /* Construct list of pointers to DCT blocks belonging to this MCU */
277       blkn = 0;      /* index of current DCT block within MCU */
278       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
279   compptr = cinfo->cur_comp_info[ci];
280   start_col = MCU_col_num * compptr->MCU_width;
281   for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
282     buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
283     for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
284       coef->MCU_buffer[blkn++] = buffer_ptr++;
285     }
286   }
287       }
288       /* Try to fetch the MCU. */
289       if (! (*lossyd->entropy_decode_mcu) (cinfo, coef->MCU_buffer)) {
290   /* Suspension forced; update state counters and exit */
291   coef->MCU_vert_offset = yoffset;
292   coef->MCU_ctr = MCU_col_num;
293   return JPEG_SUSPENDED;
294       }
295     }
296     /* Completed an MCU row, but perhaps not an iMCU row */
297     coef->MCU_ctr = 0;
298   }
299   /* Completed the iMCU row, advance counters for next one */
300   if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {
301     start_iMCU_row(cinfo);
302     return JPEG_ROW_COMPLETED;
303   }
304   /* Completed the scan */
305   (*cinfo->inputctl->finish_input_pass) (cinfo);
306   return JPEG_SCAN_COMPLETED;
307 }
308
309
310 /*
311  * Decompress and return some data in the multi-pass case.
312  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
313  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.
314  *
315  * NB: output_buf contains a plane for each component in image.
316  */
317
318 METHODDEF(int)
319 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
320 {
321   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
322   d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
323   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
324   JDIMENSION block_num;
325   int ci, block_row, block_rows;
326   JBLOCKARRAY buffer;
327   JBLOCKROW buffer_ptr;
328   JSAMPARRAY output_ptr;
329   JDIMENSION output_col;
330   jpeg_component_info *compptr;
331   inverse_DCT_method_ptr inverse_DCT;
332
333   /* Force some input to be done if we are getting ahead of the input. */
334   while (cinfo->input_scan_number < cinfo->output_scan_number ||
335    (cinfo->input_scan_number == cinfo->output_scan_number &&
336     cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {
337     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
338       return JPEG_SUSPENDED;
339   }
340
341   /* OK, output from the virtual arrays. */
342   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
343        ci++, compptr++) {
344     /* Don't bother to IDCT an uninteresting component. */
345     if (! compptr->component_needed)
346       continue;
347     /* Align the virtual buffer for this component. */
348     buffer = (*cinfo->mem->access_virt_barray)
349       ((j_common_ptr) cinfo, coef->whole_image[ci],
350        cinfo->output_iMCU_row * compptr->v_samp_factor,
351        (JDIMENSION) compptr->v_samp_factor, FALSE);
352     /* Count non-dummy DCT block rows in this iMCU row. */
353     if (cinfo->output_iMCU_row < last_iMCU_row)
354       block_rows = compptr->v_samp_factor;
355     else {
356       /* NB: can't use last_row_height here; it is input-side-dependent! */
357       block_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
358       if (block_rows == 0) block_rows = compptr->v_samp_factor;
359     }
360     inverse_DCT = lossyd->inverse_DCT[ci];
361     output_ptr = output_buf[ci];
362     /* Loop over all DCT blocks to be processed. */
363     for (block_row = 0; block_row < block_rows; block_row++) {
364       buffer_ptr = buffer[block_row];
365       output_col = 0;
366       for (block_num = 0; block_num < compptr->width_in_data_units; block_num++) {
367   (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
368       output_ptr, output_col);
369   buffer_ptr++;
370   output_col += compptr->codec_data_unit;
371       }
372       output_ptr += compptr->codec_data_unit;
373     }
374   }
375
376   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
377     return JPEG_ROW_COMPLETED;
378   return JPEG_SCAN_COMPLETED;
379 }
380
381 #endif /* D_MULTISCAN_FILES_SUPPORTED */
382
383
384 #ifdef BLOCK_SMOOTHING_SUPPORTED
385
386 /*
387  * This code applies interblock smoothing as described by section K.8
388  * of the JPEG standard: the first 5 AC coefficients are estimated from
389  * the DC values of a DCT block and its 8 neighboring blocks.
390  * We apply smoothing only for progressive JPEG decoding, and only if
391  * the coefficients it can estimate are not yet known to full precision.
392  */
393
394 /* Natural-order array positions of the first 5 zigzag-order coefficients */
395 #define Q01_POS  1
396 #define Q10_POS  8
397 #define Q20_POS  16
398 #define Q11_POS  9
399 #define Q02_POS  2
400
401 /*
402  * Determine whether block smoothing is applicable and safe.
403  * We also latch the current states of the coef_bits[] entries for the
404  * AC coefficients; otherwise, if the input side of the decompressor
405  * advances into a new scan, we might think the coefficients are known
406  * more accurately than they really are.
407  */
408
409 LOCAL(boolean)
410 smoothing_ok (j_decompress_ptr cinfo)
411 {
412   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
413   d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
414   boolean smoothing_useful = FALSE;
415   int ci, coefi;
416   jpeg_component_info *compptr;
417   JQUANT_TBL * qtable;
418   int * coef_bits;
419   int * coef_bits_latch;
420
421   if (! cinfo->process == JPROC_PROGRESSIVE || cinfo->coef_bits == NULL)
422     return FALSE;
423
424   /* Allocate latch area if not already done */
425   if (coef->coef_bits_latch == NULL)
426     coef->coef_bits_latch = (int *)
427       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
428           cinfo->num_components *
429           (SAVED_COEFS * SIZEOF(int)));
430   coef_bits_latch = coef->coef_bits_latch;
431
432   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
433        ci++, compptr++) {
434     /* All components' quantization values must already be latched. */
435     if ((qtable = compptr->quant_table) == NULL)
436       return FALSE;
437     /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */
438     if (qtable->quantval[0] == 0 ||
439   qtable->quantval[Q01_POS] == 0 ||
440   qtable->quantval[Q10_POS] == 0 ||
441   qtable->quantval[Q20_POS] == 0 ||
442   qtable->quantval[Q11_POS] == 0 ||
443   qtable->quantval[Q02_POS] == 0)
444       return FALSE;
445     /* DC values must be at least partly known for all components. */
446     coef_bits = cinfo->coef_bits[ci];
447     if (coef_bits[0] < 0)
448       return FALSE;
449     /* Block smoothing is helpful if some AC coefficients remain inaccurate. */
450     for (coefi = 1; coefi <= 5; coefi++) {
451       coef_bits_latch[coefi] = coef_bits[coefi];
452       if (coef_bits[coefi] != 0)
453   smoothing_useful = TRUE;
454     }
455     coef_bits_latch += SAVED_COEFS;
456   }
457
458   return smoothing_useful;
459 }
460
461
462 /*
463  * Variant of decompress_data for use when doing block smoothing.
464  */
465
466 METHODDEF(int)
467 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
468 {
469   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
470   d_coef_ptr coef = (d_coef_ptr) lossyd->coef_private;
471   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
472   JDIMENSION block_num, last_block_column;
473   int ci, block_row, block_rows, access_rows;
474   JBLOCKARRAY buffer;
475   JBLOCKROW buffer_ptr, prev_block_row, next_block_row;
476   JSAMPARRAY output_ptr;
477   JDIMENSION output_col;
478   jpeg_component_info *compptr;
479   inverse_DCT_method_ptr inverse_DCT;
480   boolean first_row, last_row;
481   JBLOCK workspace;
482   int *coef_bits;
483   JQUANT_TBL *quanttbl;
484   INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;
485   int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;
486   int Al, pred;
487
488   /* Force some input to be done if we are getting ahead of the input. */
489   while (cinfo->input_scan_number <= cinfo->output_scan_number &&
490    ! cinfo->inputctl->eoi_reached) {
491     if (cinfo->input_scan_number == cinfo->output_scan_number) {
492       /* If input is working on current scan, we ordinarily want it to
493        * have completed the current row.  But if input scan is DC,
494        * we want it to keep one row ahead so that next block row's DC
495        * values are up to date.
496        */
497       JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;
498       if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)
499   break;
500     }
501     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
502       return JPEG_SUSPENDED;
503   }
504
505   /* OK, output from the virtual arrays. */
506   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
507        ci++, compptr++) {
508     /* Don't bother to IDCT an uninteresting component. */
509     if (! compptr->component_needed)
510       continue;
511     /* Count non-dummy DCT block rows in this iMCU row. */
512     if (cinfo->output_iMCU_row < last_iMCU_row) {
513       block_rows = compptr->v_samp_factor;
514       access_rows = block_rows * 2; /* this and next iMCU row */
515       last_row = FALSE;
516     } else {
517       /* NB: can't use last_row_height here; it is input-side-dependent! */
518       block_rows = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
519       if (block_rows == 0) block_rows = compptr->v_samp_factor;
520       access_rows = block_rows; /* this iMCU row only */
521       last_row = TRUE;
522     }
523     /* Align the virtual buffer for this component. */
524     if (cinfo->output_iMCU_row > 0) {
525       access_rows += compptr->v_samp_factor; /* prior iMCU row too */
526       buffer = (*cinfo->mem->access_virt_barray)
527   ((j_common_ptr) cinfo, coef->whole_image[ci],
528    (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,
529    (JDIMENSION) access_rows, FALSE);
530       buffer += compptr->v_samp_factor;  /* point to current iMCU row */
531       first_row = FALSE;
532     } else {
533       buffer = (*cinfo->mem->access_virt_barray)
534   ((j_common_ptr) cinfo, coef->whole_image[ci],
535    (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);
536       first_row = TRUE;
537     }
538     /* Fetch component-dependent info */
539     coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);
540     quanttbl = compptr->quant_table;
541     Q00 = quanttbl->quantval[0];
542     Q01 = quanttbl->quantval[Q01_POS];
543     Q10 = quanttbl->quantval[Q10_POS];
544     Q20 = quanttbl->quantval[Q20_POS];
545     Q11 = quanttbl->quantval[Q11_POS];
546     Q02 = quanttbl->quantval[Q02_POS];
547     inverse_DCT = lossyd->inverse_DCT[ci];
548     output_ptr = output_buf[ci];
549     /* Loop over all DCT blocks to be processed. */
550     for (block_row = 0; block_row < block_rows; block_row++) {
551       buffer_ptr = buffer[block_row];
552       if (first_row && block_row == 0)
553   prev_block_row = buffer_ptr;
554       else
555   prev_block_row = buffer[block_row-1];
556       if (last_row && block_row == block_rows-1)
557   next_block_row = buffer_ptr;
558       else
559   next_block_row = buffer[block_row+1];
560       /* We fetch the surrounding DC values using a sliding-register approach.
561        * Initialize all nine here so as to do the right thing on narrow pics.
562        */
563       DC1 = DC2 = DC3 = (int) prev_block_row[0][0];
564       DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];
565       DC7 = DC8 = DC9 = (int) next_block_row[0][0];
566       output_col = 0;
567       last_block_column = compptr->width_in_data_units - 1;
568       for (block_num = 0; block_num <= last_block_column; block_num++) {
569   /* Fetch current DCT block into workspace so we can modify it. */
570   jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);
571   /* Update DC values */
572   if (block_num < last_block_column) {
573     DC3 = (int) prev_block_row[1][0];
574     DC6 = (int) buffer_ptr[1][0];
575     DC9 = (int) next_block_row[1][0];
576   }
577   /* Compute coefficient estimates per K.8.
578    * An estimate is applied only if coefficient is still zero,
579    * and is not known to be fully accurate.
580    */
581   /* AC01 */
582   if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {
583     num = 36 * Q00 * (DC4 - DC6);
584     if (num >= 0) {
585       pred = (int) (((Q01<<7) + num) / (Q01<<8));
586       if (Al > 0 && pred >= (1<<Al))
587         pred = (1<<Al)-1;
588     } else {
589       pred = (int) (((Q01<<7) - num) / (Q01<<8));
590       if (Al > 0 && pred >= (1<<Al))
591         pred = (1<<Al)-1;
592       pred = -pred;
593     }
594     workspace[1] = (JCOEF) pred;
595   }
596   /* AC10 */
597   if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {
598     num = 36 * Q00 * (DC2 - DC8);
599     if (num >= 0) {
600       pred = (int) (((Q10<<7) + num) / (Q10<<8));
601       if (Al > 0 && pred >= (1<<Al))
602         pred = (1<<Al)-1;
603     } else {
604       pred = (int) (((Q10<<7) - num) / (Q10<<8));
605       if (Al > 0 && pred >= (1<<Al))
606         pred = (1<<Al)-1;
607       pred = -pred;
608     }
609     workspace[8] = (JCOEF) pred;
610   }
611   /* AC20 */
612   if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {
613     num = 9 * Q00 * (DC2 + DC8 - 2*DC5);
614     if (num >= 0) {
615       pred = (int) (((Q20<<7) + num) / (Q20<<8));
616       if (Al > 0 && pred >= (1<<Al))
617         pred = (1<<Al)-1;
618     } else {
619       pred = (int) (((Q20<<7) - num) / (Q20<<8));
620       if (Al > 0 && pred >= (1<<Al))
621         pred = (1<<Al)-1;
622       pred = -pred;
623     }
624     workspace[16] = (JCOEF) pred;
625   }
626   /* AC11 */
627   if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {
628     num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);
629     if (num >= 0) {
630       pred = (int) (((Q11<<7) + num) / (Q11<<8));
631       if (Al > 0 && pred >= (1<<Al))
632         pred = (1<<Al)-1;
633     } else {
634       pred = (int) (((Q11<<7) - num) / (Q11<<8));
635       if (Al > 0 && pred >= (1<<Al))
636         pred = (1<<Al)-1;
637       pred = -pred;
638     }
639     workspace[9] = (JCOEF) pred;
640   }
641   /* AC02 */
642   if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {
643     num = 9 * Q00 * (DC4 + DC6 - 2*DC5);
644     if (num >= 0) {
645       pred = (int) (((Q02<<7) + num) / (Q02<<8));
646       if (Al > 0 && pred >= (1<<Al))
647         pred = (1<<Al)-1;
648     } else {
649       pred = (int) (((Q02<<7) - num) / (Q02<<8));
650       if (Al > 0 && pred >= (1<<Al))
651         pred = (1<<Al)-1;
652       pred = -pred;
653     }
654     workspace[2] = (JCOEF) pred;
655   }
656   /* OK, do the IDCT */
657   (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,
658       output_ptr, output_col);
659   /* Advance for next column */
660   DC1 = DC2; DC2 = DC3;
661   DC4 = DC5; DC5 = DC6;
662   DC7 = DC8; DC8 = DC9;
663   buffer_ptr++, prev_block_row++, next_block_row++;
664   output_col += compptr->codec_data_unit;
665       }
666       output_ptr += compptr->codec_data_unit;
667     }
668   }
669
670   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)
671     return JPEG_ROW_COMPLETED;
672   return JPEG_SCAN_COMPLETED;
673 }
674
675 #endif /* BLOCK_SMOOTHING_SUPPORTED */
676
677
678 /*
679  * Initialize coefficient buffer controller.
680  */
681
682 GLOBAL(void)
683 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
684 {
685   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
686   d_coef_ptr coef;
687
688   coef = (d_coef_ptr)
689     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
690         SIZEOF(d_coef_controller));
691   lossyd->coef_private = (void *) coef;
692   lossyd->coef_start_input_pass = start_input_pass;
693   lossyd->coef_start_output_pass = start_output_pass;
694 #ifdef BLOCK_SMOOTHING_SUPPORTED
695   coef->coef_bits_latch = NULL;
696 #endif
697
698   /* Create the coefficient buffer. */
699   if (need_full_buffer) {
700 #ifdef D_MULTISCAN_FILES_SUPPORTED
701     /* Allocate a full-image virtual array for each component, */
702     /* padded to a multiple of samp_factor DCT blocks in each direction. */
703     /* Note we ask for a pre-zeroed array. */
704     int ci, access_rows;
705     jpeg_component_info *compptr;
706
707     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
708    ci++, compptr++) {
709       access_rows = compptr->v_samp_factor;
710 #ifdef BLOCK_SMOOTHING_SUPPORTED
711       /* If block smoothing could be used, need a bigger window */
712       if (cinfo->process == JPROC_PROGRESSIVE)
713   access_rows *= 3;
714 #endif
715       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
716   ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,
717    (JDIMENSION) jround_up((long) compptr->width_in_data_units,
718         (long) compptr->h_samp_factor),
719    (JDIMENSION) jround_up((long) compptr->height_in_data_units,
720         (long) compptr->v_samp_factor),
721    (JDIMENSION) access_rows);
722     }
723     lossyd->pub.consume_data = consume_data;
724     lossyd->pub.decompress_data = decompress_data;
725     lossyd->coef_arrays = coef->whole_image; /* link to virtual arrays */
726 #else
727     ERREXIT(cinfo, JERR_NOT_COMPILED);
728 #endif
729   } else {
730     /* We only need a single-MCU buffer. */
731     JBLOCKROW buffer;
732     int i;
733
734     buffer = (JBLOCKROW)
735       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
736           D_MAX_DATA_UNITS_IN_MCU * SIZEOF(JBLOCK));
737     for (i = 0; i < D_MAX_DATA_UNITS_IN_MCU; i++) {
738       coef->MCU_buffer[i] = buffer + i;
739     }
740     lossyd->pub.consume_data = dummy_consume_data;
741     lossyd->pub.decompress_data = decompress_onepass;
742     lossyd->coef_arrays = NULL; /* flag for no virtual arrays */
743   }
744 }