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