]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jdcoefct.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / jpeg6 / jdcoefct.cpp
1 /*\r
2 \r
3  * jdcoefct.c\r
4 \r
5  *\r
6 \r
7  * Copyright (C) 1994-1995, Thomas G. Lane.\r
8 \r
9  * This file is part of the Independent JPEG Group's software.\r
10 \r
11  * For conditions of distribution and use, see the accompanying README file.\r
12 \r
13  *\r
14 \r
15  * This file contains the coefficient buffer controller for decompression.\r
16 \r
17  * This controller is the top level of the JPEG decompressor proper.\r
18 \r
19  * The coefficient buffer lies between entropy decoding and inverse-DCT steps.\r
20 \r
21  *\r
22 \r
23  * In buffered-image mode, this controller is the interface between\r
24 \r
25  * input-oriented processing and output-oriented processing.\r
26 \r
27  * Also, the input side (only) is used when reading a file for transcoding.\r
28 \r
29  */\r
30 \r
31 \r
32 \r
33 #define JPEG_INTERNALS\r
34 \r
35 #include "jinclude.h"\r
36 \r
37 #include "radiant_jpeglib.h"\r
38 \r
39 \r
40 \r
41 /* Block smoothing is only applicable for progressive JPEG, so: */\r
42 \r
43 #ifndef D_PROGRESSIVE_SUPPORTED\r
44 \r
45 #undef BLOCK_SMOOTHING_SUPPORTED\r
46 \r
47 #endif\r
48 \r
49 \r
50 \r
51 /* Private buffer controller object */\r
52 \r
53 \r
54 \r
55 typedef struct {\r
56 \r
57   struct jpeg_d_coef_controller pub; /* public fields */\r
58 \r
59 \r
60 \r
61   /* These variables keep track of the current location of the input side. */\r
62 \r
63   /* cinfo->input_iMCU_row is also used for this. */\r
64 \r
65   JDIMENSION MCU_ctr;           /* counts MCUs processed in current row */\r
66 \r
67   int MCU_vert_offset;          /* counts MCU rows within iMCU row */\r
68 \r
69   int MCU_rows_per_iMCU_row;    /* number of such rows needed */\r
70 \r
71 \r
72 \r
73   /* The output side's location is represented by cinfo->output_iMCU_row. */\r
74 \r
75 \r
76 \r
77   /* In single-pass modes, it's sufficient to buffer just one MCU.\r
78 \r
79    * We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,\r
80 \r
81    * and let the entropy decoder write into that workspace each time.\r
82 \r
83    * (On 80x86, the workspace is FAR even though it's not really very big;\r
84 \r
85    * this is to keep the module interfaces unchanged when a large coefficient\r
86 \r
87    * buffer is necessary.)\r
88 \r
89    * In multi-pass modes, this array points to the current MCU's blocks\r
90 \r
91    * within the virtual arrays; it is used only by the input side.\r
92 \r
93    */\r
94 \r
95   JBLOCKROW MCU_buffer[D_MAX_BLOCKS_IN_MCU];\r
96 \r
97 \r
98 \r
99 #ifdef D_MULTISCAN_FILES_SUPPORTED\r
100 \r
101   /* In multi-pass modes, we need a virtual block array for each component. */\r
102 \r
103   jvirt_barray_ptr whole_image[MAX_COMPONENTS];\r
104 \r
105 #endif\r
106 \r
107 \r
108 \r
109 #ifdef BLOCK_SMOOTHING_SUPPORTED\r
110 \r
111   /* When doing block smoothing, we latch coefficient Al values here */\r
112 \r
113   int * coef_bits_latch;\r
114 \r
115 #define SAVED_COEFS  6          /* we save coef_bits[0..5] */\r
116 \r
117 #endif\r
118 \r
119 } my_coef_controller;\r
120 \r
121 \r
122 \r
123 typedef my_coef_controller * my_coef_ptr;\r
124 \r
125 \r
126 \r
127 /* Forward declarations */\r
128 \r
129 METHODDEF int decompress_onepass\r
130 \r
131         JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));\r
132 \r
133 #ifdef D_MULTISCAN_FILES_SUPPORTED\r
134 \r
135 METHODDEF int decompress_data\r
136 \r
137         JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));\r
138 \r
139 #endif\r
140 \r
141 #ifdef BLOCK_SMOOTHING_SUPPORTED\r
142 \r
143 LOCAL boolean smoothing_ok JPP((j_decompress_ptr cinfo));\r
144 \r
145 METHODDEF int decompress_smooth_data\r
146 \r
147         JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));\r
148 \r
149 #endif\r
150 \r
151 \r
152 \r
153 \r
154 \r
155 LOCAL void\r
156 \r
157 start_iMCU_row (j_decompress_ptr cinfo)\r
158 \r
159 /* Reset within-iMCU-row counters for a new row (input side) */\r
160 \r
161 {\r
162 \r
163   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
164 \r
165 \r
166 \r
167   /* In an interleaved scan, an MCU row is the same as an iMCU row.\r
168 \r
169    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.\r
170 \r
171    * But at the bottom of the image, process only what's left.\r
172 \r
173    */\r
174 \r
175   if (cinfo->comps_in_scan > 1) {\r
176 \r
177     coef->MCU_rows_per_iMCU_row = 1;\r
178 \r
179   } else {\r
180 \r
181     if (cinfo->input_iMCU_row < (cinfo->total_iMCU_rows-1))\r
182 \r
183       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;\r
184 \r
185     else\r
186 \r
187       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;\r
188 \r
189   }\r
190 \r
191 \r
192 \r
193   coef->MCU_ctr = 0;\r
194 \r
195   coef->MCU_vert_offset = 0;\r
196 \r
197 }\r
198 \r
199 \r
200 \r
201 \r
202 \r
203 /*\r
204 \r
205  * Initialize for an input processing pass.\r
206 \r
207  */\r
208 \r
209 \r
210 \r
211 METHODDEF void\r
212 \r
213 start_input_pass (j_decompress_ptr cinfo)\r
214 \r
215 {\r
216 \r
217   cinfo->input_iMCU_row = 0;\r
218 \r
219   start_iMCU_row(cinfo);\r
220 \r
221 }\r
222 \r
223 \r
224 \r
225 \r
226 \r
227 /*\r
228 \r
229  * Initialize for an output processing pass.\r
230 \r
231  */\r
232 \r
233 \r
234 \r
235 METHODDEF void\r
236 \r
237 start_output_pass (j_decompress_ptr cinfo)\r
238 \r
239 {\r
240 \r
241 #ifdef BLOCK_SMOOTHING_SUPPORTED\r
242 \r
243   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
244 \r
245 \r
246 \r
247   /* If multipass, check to see whether to use block smoothing on this pass */\r
248 \r
249   if (coef->pub.coef_arrays != NULL) {\r
250 \r
251     if (cinfo->do_block_smoothing && smoothing_ok(cinfo))\r
252 \r
253       coef->pub.decompress_data = decompress_smooth_data;\r
254 \r
255     else\r
256 \r
257       coef->pub.decompress_data = decompress_data;\r
258 \r
259   }\r
260 \r
261 #endif\r
262 \r
263   cinfo->output_iMCU_row = 0;\r
264 \r
265 }\r
266 \r
267 \r
268 \r
269 \r
270 \r
271 /*\r
272 \r
273  * Decompress and return some data in the single-pass case.\r
274 \r
275  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).\r
276 \r
277  * Input and output must run in lockstep since we have only a one-MCU buffer.\r
278 \r
279  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.\r
280 \r
281  *\r
282 \r
283  * NB: output_buf contains a plane for each component in image.\r
284 \r
285  * For single pass, this is the same as the components in the scan.\r
286 \r
287  */\r
288 \r
289 \r
290 \r
291 METHODDEF int\r
292 \r
293 decompress_onepass (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)\r
294 \r
295 {\r
296 \r
297   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
298 \r
299   JDIMENSION MCU_col_num;       /* index of current MCU within row */\r
300 \r
301   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;\r
302 \r
303   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;\r
304 \r
305   int blkn, ci, xindex, yindex, yoffset, useful_width;\r
306 \r
307   JSAMPARRAY output_ptr;\r
308 \r
309   JDIMENSION start_col, output_col;\r
310 \r
311   jpeg_component_info *compptr;\r
312 \r
313   inverse_DCT_method_ptr inverse_DCT;\r
314 \r
315 \r
316 \r
317   /* Loop to process as much as one whole iMCU row */\r
318 \r
319   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;\r
320 \r
321        yoffset++) {\r
322 \r
323     for (MCU_col_num = coef->MCU_ctr; MCU_col_num <= last_MCU_col;\r
324 \r
325          MCU_col_num++) {\r
326 \r
327       /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */\r
328 \r
329       jzero_far((void FAR *) coef->MCU_buffer[0],\r
330 \r
331                 (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));\r
332 \r
333       if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {\r
334 \r
335         /* Suspension forced; update state counters and exit */\r
336 \r
337         coef->MCU_vert_offset = yoffset;\r
338 \r
339         coef->MCU_ctr = MCU_col_num;\r
340 \r
341         return JPEG_SUSPENDED;\r
342 \r
343       }\r
344 \r
345       /* Determine where data should go in output_buf and do the IDCT thing.\r
346 \r
347        * We skip dummy blocks at the right and bottom edges (but blkn gets\r
348 \r
349        * incremented past them!).  Note the inner loop relies on having\r
350 \r
351        * allocated the MCU_buffer[] blocks sequentially.\r
352 \r
353        */\r
354 \r
355       blkn = 0;                 /* index of current DCT block within MCU */\r
356 \r
357       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {\r
358 \r
359         compptr = cinfo->cur_comp_info[ci];\r
360 \r
361         /* Don't bother to IDCT an uninteresting component. */\r
362 \r
363         if (! compptr->component_needed) {\r
364 \r
365           blkn += compptr->MCU_blocks;\r
366 \r
367           continue;\r
368 \r
369         }\r
370 \r
371         inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];\r
372 \r
373         useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width\r
374 \r
375                                                     : compptr->last_col_width;\r
376 \r
377         output_ptr = output_buf[ci] + yoffset * compptr->DCT_scaled_size;\r
378 \r
379         start_col = MCU_col_num * compptr->MCU_sample_width;\r
380 \r
381         for (yindex = 0; yindex < compptr->MCU_height; yindex++) {\r
382 \r
383           if (cinfo->input_iMCU_row < last_iMCU_row ||\r
384 \r
385               yoffset+yindex < compptr->last_row_height) {\r
386 \r
387             output_col = start_col;\r
388 \r
389             for (xindex = 0; xindex < useful_width; xindex++) {\r
390 \r
391               (*inverse_DCT) (cinfo, compptr,\r
392 \r
393                               (JCOEFPTR) coef->MCU_buffer[blkn+xindex],\r
394 \r
395                               output_ptr, output_col);\r
396 \r
397               output_col += compptr->DCT_scaled_size;\r
398 \r
399             }\r
400 \r
401           }\r
402 \r
403           blkn += compptr->MCU_width;\r
404 \r
405           output_ptr += compptr->DCT_scaled_size;\r
406 \r
407         }\r
408 \r
409       }\r
410 \r
411     }\r
412 \r
413     /* Completed an MCU row, but perhaps not an iMCU row */\r
414 \r
415     coef->MCU_ctr = 0;\r
416 \r
417   }\r
418 \r
419   /* Completed the iMCU row, advance counters for next one */\r
420 \r
421   cinfo->output_iMCU_row++;\r
422 \r
423   if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {\r
424 \r
425     start_iMCU_row(cinfo);\r
426 \r
427     return JPEG_ROW_COMPLETED;\r
428 \r
429   }\r
430 \r
431   /* Completed the scan */\r
432 \r
433   (*cinfo->inputctl->finish_input_pass) (cinfo);\r
434 \r
435   return JPEG_SCAN_COMPLETED;\r
436 \r
437 }\r
438 \r
439 \r
440 \r
441 \r
442 \r
443 /*\r
444 \r
445  * Dummy consume-input routine for single-pass operation.\r
446 \r
447  */\r
448 \r
449 \r
450 \r
451 METHODDEF int\r
452 \r
453 dummy_consume_data (j_decompress_ptr cinfo)\r
454 \r
455 {\r
456 \r
457   return JPEG_SUSPENDED;        /* Always indicate nothing was done */\r
458 \r
459 }\r
460 \r
461 \r
462 \r
463 \r
464 \r
465 #ifdef D_MULTISCAN_FILES_SUPPORTED\r
466 \r
467 \r
468 \r
469 /*\r
470 \r
471  * Consume input data and store it in the full-image coefficient buffer.\r
472 \r
473  * We read as much as one fully interleaved MCU row ("iMCU" row) per call,\r
474 \r
475  * ie, v_samp_factor block rows for each component in the scan.\r
476 \r
477  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.\r
478 \r
479  */\r
480 \r
481 \r
482 \r
483 METHODDEF int\r
484 \r
485 consume_data (j_decompress_ptr cinfo)\r
486 \r
487 {\r
488 \r
489   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
490 \r
491   JDIMENSION MCU_col_num;       /* index of current MCU within row */\r
492 \r
493   int blkn, ci, xindex, yindex, yoffset;\r
494 \r
495   JDIMENSION start_col;\r
496 \r
497   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];\r
498 \r
499   JBLOCKROW buffer_ptr;\r
500 \r
501   jpeg_component_info *compptr;\r
502 \r
503 \r
504 \r
505   /* Align the virtual buffers for the components used in this scan. */\r
506 \r
507   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {\r
508 \r
509     compptr = cinfo->cur_comp_info[ci];\r
510 \r
511     buffer[ci] = (*cinfo->mem->access_virt_barray)\r
512 \r
513       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],\r
514 \r
515        cinfo->input_iMCU_row * compptr->v_samp_factor,\r
516 \r
517        (JDIMENSION) compptr->v_samp_factor, TRUE);\r
518 \r
519     /* Note: entropy decoder expects buffer to be zeroed,\r
520 \r
521      * but this is handled automatically by the memory manager\r
522 \r
523      * because we requested a pre-zeroed array.\r
524 \r
525      */\r
526 \r
527   }\r
528 \r
529 \r
530 \r
531   /* Loop to process one whole iMCU row */\r
532 \r
533   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;\r
534 \r
535        yoffset++) {\r
536 \r
537     for (MCU_col_num = coef->MCU_ctr; MCU_col_num < cinfo->MCUs_per_row;\r
538 \r
539          MCU_col_num++) {\r
540 \r
541       /* Construct list of pointers to DCT blocks belonging to this MCU */\r
542 \r
543       blkn = 0;                 /* index of current DCT block within MCU */\r
544 \r
545       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {\r
546 \r
547         compptr = cinfo->cur_comp_info[ci];\r
548 \r
549         start_col = MCU_col_num * compptr->MCU_width;\r
550 \r
551         for (yindex = 0; yindex < compptr->MCU_height; yindex++) {\r
552 \r
553           buffer_ptr = buffer[ci][yindex+yoffset] + start_col;\r
554 \r
555           for (xindex = 0; xindex < compptr->MCU_width; xindex++) {\r
556 \r
557             coef->MCU_buffer[blkn++] = buffer_ptr++;\r
558 \r
559           }\r
560 \r
561         }\r
562 \r
563       }\r
564 \r
565       /* Try to fetch the MCU. */\r
566 \r
567       if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {\r
568 \r
569         /* Suspension forced; update state counters and exit */\r
570 \r
571         coef->MCU_vert_offset = yoffset;\r
572 \r
573         coef->MCU_ctr = MCU_col_num;\r
574 \r
575         return JPEG_SUSPENDED;\r
576 \r
577       }\r
578 \r
579     }\r
580 \r
581     /* Completed an MCU row, but perhaps not an iMCU row */\r
582 \r
583     coef->MCU_ctr = 0;\r
584 \r
585   }\r
586 \r
587   /* Completed the iMCU row, advance counters for next one */\r
588 \r
589   if (++(cinfo->input_iMCU_row) < cinfo->total_iMCU_rows) {\r
590 \r
591     start_iMCU_row(cinfo);\r
592 \r
593     return JPEG_ROW_COMPLETED;\r
594 \r
595   }\r
596 \r
597   /* Completed the scan */\r
598 \r
599   (*cinfo->inputctl->finish_input_pass) (cinfo);\r
600 \r
601   return JPEG_SCAN_COMPLETED;\r
602 \r
603 }\r
604 \r
605 \r
606 \r
607 \r
608 \r
609 /*\r
610 \r
611  * Decompress and return some data in the multi-pass case.\r
612 \r
613  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).\r
614 \r
615  * Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.\r
616 \r
617  *\r
618 \r
619  * NB: output_buf contains a plane for each component in image.\r
620 \r
621  */\r
622 \r
623 \r
624 \r
625 METHODDEF int\r
626 \r
627 decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)\r
628 \r
629 {\r
630 \r
631   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
632 \r
633   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;\r
634 \r
635   JDIMENSION block_num;\r
636 \r
637   int ci, block_row, block_rows;\r
638 \r
639   JBLOCKARRAY buffer;\r
640 \r
641   JBLOCKROW buffer_ptr;\r
642 \r
643   JSAMPARRAY output_ptr;\r
644 \r
645   JDIMENSION output_col;\r
646 \r
647   jpeg_component_info *compptr;\r
648 \r
649   inverse_DCT_method_ptr inverse_DCT;\r
650 \r
651 \r
652 \r
653   /* Force some input to be done if we are getting ahead of the input. */\r
654 \r
655   while (cinfo->input_scan_number < cinfo->output_scan_number ||\r
656 \r
657          (cinfo->input_scan_number == cinfo->output_scan_number &&\r
658 \r
659           cinfo->input_iMCU_row <= cinfo->output_iMCU_row)) {\r
660 \r
661     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)\r
662 \r
663       return JPEG_SUSPENDED;\r
664 \r
665   }\r
666 \r
667 \r
668 \r
669   /* OK, output from the virtual arrays. */\r
670 \r
671   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
672 \r
673        ci++, compptr++) {\r
674 \r
675     /* Don't bother to IDCT an uninteresting component. */\r
676 \r
677     if (! compptr->component_needed)\r
678 \r
679       continue;\r
680 \r
681     /* Align the virtual buffer for this component. */\r
682 \r
683     buffer = (*cinfo->mem->access_virt_barray)\r
684 \r
685       ((j_common_ptr) cinfo, coef->whole_image[ci],\r
686 \r
687        cinfo->output_iMCU_row * compptr->v_samp_factor,\r
688 \r
689        (JDIMENSION) compptr->v_samp_factor, FALSE);\r
690 \r
691     /* Count non-dummy DCT block rows in this iMCU row. */\r
692 \r
693     if (cinfo->output_iMCU_row < last_iMCU_row)\r
694 \r
695       block_rows = compptr->v_samp_factor;\r
696 \r
697     else {\r
698 \r
699       /* NB: can't use last_row_height here; it is input-side-dependent! */\r
700 \r
701       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);\r
702 \r
703       if (block_rows == 0) block_rows = compptr->v_samp_factor;\r
704 \r
705     }\r
706 \r
707     inverse_DCT = cinfo->idct->inverse_DCT[ci];\r
708 \r
709     output_ptr = output_buf[ci];\r
710 \r
711     /* Loop over all DCT blocks to be processed. */\r
712 \r
713     for (block_row = 0; block_row < block_rows; block_row++) {\r
714 \r
715       buffer_ptr = buffer[block_row];\r
716 \r
717       output_col = 0;\r
718 \r
719       for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {\r
720 \r
721         (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,\r
722 \r
723                         output_ptr, output_col);\r
724 \r
725         buffer_ptr++;\r
726 \r
727         output_col += compptr->DCT_scaled_size;\r
728 \r
729       }\r
730 \r
731       output_ptr += compptr->DCT_scaled_size;\r
732 \r
733     }\r
734 \r
735   }\r
736 \r
737 \r
738 \r
739   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)\r
740 \r
741     return JPEG_ROW_COMPLETED;\r
742 \r
743   return JPEG_SCAN_COMPLETED;\r
744 \r
745 }\r
746 \r
747 \r
748 \r
749 #endif /* D_MULTISCAN_FILES_SUPPORTED */\r
750 \r
751 \r
752 \r
753 \r
754 \r
755 #ifdef BLOCK_SMOOTHING_SUPPORTED\r
756 \r
757 \r
758 \r
759 /*\r
760 \r
761  * This code applies interblock smoothing as described by section K.8\r
762 \r
763  * of the JPEG standard: the first 5 AC coefficients are estimated from\r
764 \r
765  * the DC values of a DCT block and its 8 neighboring blocks.\r
766 \r
767  * We apply smoothing only for progressive JPEG decoding, and only if\r
768 \r
769  * the coefficients it can estimate are not yet known to full precision.\r
770 \r
771  */\r
772 \r
773 \r
774 \r
775 /*\r
776 \r
777  * Determine whether block smoothing is applicable and safe.\r
778 \r
779  * We also latch the current states of the coef_bits[] entries for the\r
780 \r
781  * AC coefficients; otherwise, if the input side of the decompressor\r
782 \r
783  * advances into a new scan, we might think the coefficients are known\r
784 \r
785  * more accurately than they really are.\r
786 \r
787  */\r
788 \r
789 \r
790 \r
791 LOCAL boolean\r
792 \r
793 smoothing_ok (j_decompress_ptr cinfo)\r
794 \r
795 {\r
796 \r
797   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
798 \r
799   boolean smoothing_useful = FALSE;\r
800 \r
801   int ci, coefi;\r
802 \r
803   jpeg_component_info *compptr;\r
804 \r
805   JQUANT_TBL * qtable;\r
806 \r
807   int * coef_bits;\r
808 \r
809   int * coef_bits_latch;\r
810 \r
811 \r
812 \r
813   if (! cinfo->progressive_mode || cinfo->coef_bits == NULL)\r
814 \r
815     return FALSE;\r
816 \r
817 \r
818 \r
819   /* Allocate latch area if not already done */\r
820 \r
821   if (coef->coef_bits_latch == NULL)\r
822 \r
823     coef->coef_bits_latch = (int *)\r
824 \r
825       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
826 \r
827                                   cinfo->num_components *\r
828 \r
829                                   (SAVED_COEFS * SIZEOF(int)));\r
830 \r
831   coef_bits_latch = coef->coef_bits_latch;\r
832 \r
833 \r
834 \r
835   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
836 \r
837        ci++, compptr++) {\r
838 \r
839     /* All components' quantization values must already be latched. */\r
840 \r
841     if ((qtable = compptr->quant_table) == NULL)\r
842 \r
843       return FALSE;\r
844 \r
845     /* Verify DC & first 5 AC quantizers are nonzero to avoid zero-divide. */\r
846 \r
847     for (coefi = 0; coefi <= 5; coefi++) {\r
848 \r
849       if (qtable->quantval[coefi] == 0)\r
850 \r
851         return FALSE;\r
852 \r
853     }\r
854 \r
855     /* DC values must be at least partly known for all components. */\r
856 \r
857     coef_bits = cinfo->coef_bits[ci];\r
858 \r
859     if (coef_bits[0] < 0)\r
860 \r
861       return FALSE;\r
862 \r
863     /* Block smoothing is helpful if some AC coefficients remain inaccurate. */\r
864 \r
865     for (coefi = 1; coefi <= 5; coefi++) {\r
866 \r
867       coef_bits_latch[coefi] = coef_bits[coefi];\r
868 \r
869       if (coef_bits[coefi] != 0)\r
870 \r
871         smoothing_useful = TRUE;\r
872 \r
873     }\r
874 \r
875     coef_bits_latch += SAVED_COEFS;\r
876 \r
877   }\r
878 \r
879 \r
880 \r
881   return smoothing_useful;\r
882 \r
883 }\r
884 \r
885 \r
886 \r
887 \r
888 \r
889 /*\r
890 \r
891  * Variant of decompress_data for use when doing block smoothing.\r
892 \r
893  */\r
894 \r
895 \r
896 \r
897 METHODDEF int\r
898 \r
899 decompress_smooth_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)\r
900 \r
901 {\r
902 \r
903   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;\r
904 \r
905   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;\r
906 \r
907   JDIMENSION block_num, last_block_column;\r
908 \r
909   int ci, block_row, block_rows, access_rows;\r
910 \r
911   JBLOCKARRAY buffer;\r
912 \r
913   JBLOCKROW buffer_ptr, prev_block_row, next_block_row;\r
914 \r
915   JSAMPARRAY output_ptr;\r
916 \r
917   JDIMENSION output_col;\r
918 \r
919   jpeg_component_info *compptr;\r
920 \r
921   inverse_DCT_method_ptr inverse_DCT;\r
922 \r
923   boolean first_row, last_row;\r
924 \r
925   JBLOCK workspace;\r
926 \r
927   int *coef_bits;\r
928 \r
929   JQUANT_TBL *quanttbl;\r
930 \r
931   INT32 Q00,Q01,Q02,Q10,Q11,Q20, num;\r
932 \r
933   int DC1,DC2,DC3,DC4,DC5,DC6,DC7,DC8,DC9;\r
934 \r
935   int Al, pred;\r
936 \r
937 \r
938 \r
939   /* Force some input to be done if we are getting ahead of the input. */\r
940 \r
941   while (cinfo->input_scan_number <= cinfo->output_scan_number &&\r
942 \r
943          ! cinfo->inputctl->eoi_reached) {\r
944 \r
945     if (cinfo->input_scan_number == cinfo->output_scan_number) {\r
946 \r
947       /* If input is working on current scan, we ordinarily want it to\r
948 \r
949        * have completed the current row.  But if input scan is DC,\r
950 \r
951        * we want it to keep one row ahead so that next block row's DC\r
952 \r
953        * values are up to date.\r
954 \r
955        */\r
956 \r
957       JDIMENSION delta = (cinfo->Ss == 0) ? 1 : 0;\r
958 \r
959       if (cinfo->input_iMCU_row > cinfo->output_iMCU_row+delta)\r
960 \r
961         break;\r
962 \r
963     }\r
964 \r
965     if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)\r
966 \r
967       return JPEG_SUSPENDED;\r
968 \r
969   }\r
970 \r
971 \r
972 \r
973   /* OK, output from the virtual arrays. */\r
974 \r
975   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
976 \r
977        ci++, compptr++) {\r
978 \r
979     /* Don't bother to IDCT an uninteresting component. */\r
980 \r
981     if (! compptr->component_needed)\r
982 \r
983       continue;\r
984 \r
985     /* Count non-dummy DCT block rows in this iMCU row. */\r
986 \r
987     if (cinfo->output_iMCU_row < last_iMCU_row) {\r
988 \r
989       block_rows = compptr->v_samp_factor;\r
990 \r
991       access_rows = block_rows * 2; /* this and next iMCU row */\r
992 \r
993       last_row = FALSE;\r
994 \r
995     } else {\r
996 \r
997       /* NB: can't use last_row_height here; it is input-side-dependent! */\r
998 \r
999       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);\r
1000 \r
1001       if (block_rows == 0) block_rows = compptr->v_samp_factor;\r
1002 \r
1003       access_rows = block_rows; /* this iMCU row only */\r
1004 \r
1005       last_row = TRUE;\r
1006 \r
1007     }\r
1008 \r
1009     /* Align the virtual buffer for this component. */\r
1010 \r
1011     if (cinfo->output_iMCU_row > 0) {\r
1012 \r
1013       access_rows += compptr->v_samp_factor; /* prior iMCU row too */\r
1014 \r
1015       buffer = (*cinfo->mem->access_virt_barray)\r
1016 \r
1017         ((j_common_ptr) cinfo, coef->whole_image[ci],\r
1018 \r
1019          (cinfo->output_iMCU_row - 1) * compptr->v_samp_factor,\r
1020 \r
1021          (JDIMENSION) access_rows, FALSE);\r
1022 \r
1023       buffer += compptr->v_samp_factor; /* point to current iMCU row */\r
1024 \r
1025       first_row = FALSE;\r
1026 \r
1027     } else {\r
1028 \r
1029       buffer = (*cinfo->mem->access_virt_barray)\r
1030 \r
1031         ((j_common_ptr) cinfo, coef->whole_image[ci],\r
1032 \r
1033          (JDIMENSION) 0, (JDIMENSION) access_rows, FALSE);\r
1034 \r
1035       first_row = TRUE;\r
1036 \r
1037     }\r
1038 \r
1039     /* Fetch component-dependent info */\r
1040 \r
1041     coef_bits = coef->coef_bits_latch + (ci * SAVED_COEFS);\r
1042 \r
1043     quanttbl = compptr->quant_table;\r
1044 \r
1045     Q00 = quanttbl->quantval[0];\r
1046 \r
1047     Q01 = quanttbl->quantval[1];\r
1048 \r
1049     Q10 = quanttbl->quantval[2];\r
1050 \r
1051     Q20 = quanttbl->quantval[3];\r
1052 \r
1053     Q11 = quanttbl->quantval[4];\r
1054 \r
1055     Q02 = quanttbl->quantval[5];\r
1056 \r
1057     inverse_DCT = cinfo->idct->inverse_DCT[ci];\r
1058 \r
1059     output_ptr = output_buf[ci];\r
1060 \r
1061     /* Loop over all DCT blocks to be processed. */\r
1062 \r
1063     for (block_row = 0; block_row < block_rows; block_row++) {\r
1064 \r
1065       buffer_ptr = buffer[block_row];\r
1066 \r
1067       if (first_row && block_row == 0)\r
1068 \r
1069         prev_block_row = buffer_ptr;\r
1070 \r
1071       else\r
1072 \r
1073         prev_block_row = buffer[block_row-1];\r
1074 \r
1075       if (last_row && block_row == block_rows-1)\r
1076 \r
1077         next_block_row = buffer_ptr;\r
1078 \r
1079       else\r
1080 \r
1081         next_block_row = buffer[block_row+1];\r
1082 \r
1083       /* We fetch the surrounding DC values using a sliding-register approach.\r
1084 \r
1085        * Initialize all nine here so as to do the right thing on narrow pics.\r
1086 \r
1087        */\r
1088 \r
1089       DC1 = DC2 = DC3 = (int) prev_block_row[0][0];\r
1090 \r
1091       DC4 = DC5 = DC6 = (int) buffer_ptr[0][0];\r
1092 \r
1093       DC7 = DC8 = DC9 = (int) next_block_row[0][0];\r
1094 \r
1095       output_col = 0;\r
1096 \r
1097       last_block_column = compptr->width_in_blocks - 1;\r
1098 \r
1099       for (block_num = 0; block_num <= last_block_column; block_num++) {\r
1100 \r
1101         /* Fetch current DCT block into workspace so we can modify it. */\r
1102 \r
1103         jcopy_block_row(buffer_ptr, (JBLOCKROW) workspace, (JDIMENSION) 1);\r
1104 \r
1105         /* Update DC values */\r
1106 \r
1107         if (block_num < last_block_column) {\r
1108 \r
1109           DC3 = (int) prev_block_row[1][0];\r
1110 \r
1111           DC6 = (int) buffer_ptr[1][0];\r
1112 \r
1113           DC9 = (int) next_block_row[1][0];\r
1114 \r
1115         }\r
1116 \r
1117         /* Compute coefficient estimates per K.8.\r
1118 \r
1119          * An estimate is applied only if coefficient is still zero,\r
1120 \r
1121          * and is not known to be fully accurate.\r
1122 \r
1123          */\r
1124 \r
1125         /* AC01 */\r
1126 \r
1127         if ((Al=coef_bits[1]) != 0 && workspace[1] == 0) {\r
1128 \r
1129           num = 36 * Q00 * (DC4 - DC6);\r
1130 \r
1131           if (num >= 0) {\r
1132 \r
1133             pred = (int) (((Q01<<7) + num) / (Q01<<8));\r
1134 \r
1135             if (Al > 0 && pred >= (1<<Al))\r
1136 \r
1137               pred = (1<<Al)-1;\r
1138 \r
1139           } else {\r
1140 \r
1141             pred = (int) (((Q01<<7) - num) / (Q01<<8));\r
1142 \r
1143             if (Al > 0 && pred >= (1<<Al))\r
1144 \r
1145               pred = (1<<Al)-1;\r
1146 \r
1147             pred = -pred;\r
1148 \r
1149           }\r
1150 \r
1151           workspace[1] = (JCOEF) pred;\r
1152 \r
1153         }\r
1154 \r
1155         /* AC10 */\r
1156 \r
1157         if ((Al=coef_bits[2]) != 0 && workspace[8] == 0) {\r
1158 \r
1159           num = 36 * Q00 * (DC2 - DC8);\r
1160 \r
1161           if (num >= 0) {\r
1162 \r
1163             pred = (int) (((Q10<<7) + num) / (Q10<<8));\r
1164 \r
1165             if (Al > 0 && pred >= (1<<Al))\r
1166 \r
1167               pred = (1<<Al)-1;\r
1168 \r
1169           } else {\r
1170 \r
1171             pred = (int) (((Q10<<7) - num) / (Q10<<8));\r
1172 \r
1173             if (Al > 0 && pred >= (1<<Al))\r
1174 \r
1175               pred = (1<<Al)-1;\r
1176 \r
1177             pred = -pred;\r
1178 \r
1179           }\r
1180 \r
1181           workspace[8] = (JCOEF) pred;\r
1182 \r
1183         }\r
1184 \r
1185         /* AC20 */\r
1186 \r
1187         if ((Al=coef_bits[3]) != 0 && workspace[16] == 0) {\r
1188 \r
1189           num = 9 * Q00 * (DC2 + DC8 - 2*DC5);\r
1190 \r
1191           if (num >= 0) {\r
1192 \r
1193             pred = (int) (((Q20<<7) + num) / (Q20<<8));\r
1194 \r
1195             if (Al > 0 && pred >= (1<<Al))\r
1196 \r
1197               pred = (1<<Al)-1;\r
1198 \r
1199           } else {\r
1200 \r
1201             pred = (int) (((Q20<<7) - num) / (Q20<<8));\r
1202 \r
1203             if (Al > 0 && pred >= (1<<Al))\r
1204 \r
1205               pred = (1<<Al)-1;\r
1206 \r
1207             pred = -pred;\r
1208 \r
1209           }\r
1210 \r
1211           workspace[16] = (JCOEF) pred;\r
1212 \r
1213         }\r
1214 \r
1215         /* AC11 */\r
1216 \r
1217         if ((Al=coef_bits[4]) != 0 && workspace[9] == 0) {\r
1218 \r
1219           num = 5 * Q00 * (DC1 - DC3 - DC7 + DC9);\r
1220 \r
1221           if (num >= 0) {\r
1222 \r
1223             pred = (int) (((Q11<<7) + num) / (Q11<<8));\r
1224 \r
1225             if (Al > 0 && pred >= (1<<Al))\r
1226 \r
1227               pred = (1<<Al)-1;\r
1228 \r
1229           } else {\r
1230 \r
1231             pred = (int) (((Q11<<7) - num) / (Q11<<8));\r
1232 \r
1233             if (Al > 0 && pred >= (1<<Al))\r
1234 \r
1235               pred = (1<<Al)-1;\r
1236 \r
1237             pred = -pred;\r
1238 \r
1239           }\r
1240 \r
1241           workspace[9] = (JCOEF) pred;\r
1242 \r
1243         }\r
1244 \r
1245         /* AC02 */\r
1246 \r
1247         if ((Al=coef_bits[5]) != 0 && workspace[2] == 0) {\r
1248 \r
1249           num = 9 * Q00 * (DC4 + DC6 - 2*DC5);\r
1250 \r
1251           if (num >= 0) {\r
1252 \r
1253             pred = (int) (((Q02<<7) + num) / (Q02<<8));\r
1254 \r
1255             if (Al > 0 && pred >= (1<<Al))\r
1256 \r
1257               pred = (1<<Al)-1;\r
1258 \r
1259           } else {\r
1260 \r
1261             pred = (int) (((Q02<<7) - num) / (Q02<<8));\r
1262 \r
1263             if (Al > 0 && pred >= (1<<Al))\r
1264 \r
1265               pred = (1<<Al)-1;\r
1266 \r
1267             pred = -pred;\r
1268 \r
1269           }\r
1270 \r
1271           workspace[2] = (JCOEF) pred;\r
1272 \r
1273         }\r
1274 \r
1275         /* OK, do the IDCT */\r
1276 \r
1277         (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) workspace,\r
1278 \r
1279                         output_ptr, output_col);\r
1280 \r
1281         /* Advance for next column */\r
1282 \r
1283         DC1 = DC2; DC2 = DC3;\r
1284 \r
1285         DC4 = DC5; DC5 = DC6;\r
1286 \r
1287         DC7 = DC8; DC8 = DC9;\r
1288 \r
1289         buffer_ptr++, prev_block_row++, next_block_row++;\r
1290 \r
1291         output_col += compptr->DCT_scaled_size;\r
1292 \r
1293       }\r
1294 \r
1295       output_ptr += compptr->DCT_scaled_size;\r
1296 \r
1297     }\r
1298 \r
1299   }\r
1300 \r
1301 \r
1302 \r
1303   if (++(cinfo->output_iMCU_row) < cinfo->total_iMCU_rows)\r
1304 \r
1305     return JPEG_ROW_COMPLETED;\r
1306 \r
1307   return JPEG_SCAN_COMPLETED;\r
1308 \r
1309 }\r
1310 \r
1311 \r
1312 \r
1313 #endif /* BLOCK_SMOOTHING_SUPPORTED */\r
1314 \r
1315 \r
1316 \r
1317 \r
1318 \r
1319 /*\r
1320 \r
1321  * Initialize coefficient buffer controller.\r
1322 \r
1323  */\r
1324 \r
1325 \r
1326 \r
1327 GLOBAL void\r
1328 \r
1329 jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)\r
1330 \r
1331 {\r
1332 \r
1333   my_coef_ptr coef;\r
1334 \r
1335 \r
1336 \r
1337   coef = (my_coef_ptr)\r
1338 \r
1339     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
1340 \r
1341                                 SIZEOF(my_coef_controller));\r
1342 \r
1343   cinfo->coef = (struct jpeg_d_coef_controller *) coef;\r
1344 \r
1345   coef->pub.start_input_pass = start_input_pass;\r
1346 \r
1347   coef->pub.start_output_pass = start_output_pass;\r
1348 \r
1349 #ifdef BLOCK_SMOOTHING_SUPPORTED\r
1350 \r
1351   coef->coef_bits_latch = NULL;\r
1352 \r
1353 #endif\r
1354 \r
1355 \r
1356 \r
1357   /* Create the coefficient buffer. */\r
1358 \r
1359   if (need_full_buffer) {\r
1360 \r
1361 #ifdef D_MULTISCAN_FILES_SUPPORTED\r
1362 \r
1363     /* Allocate a full-image virtual array for each component, */\r
1364 \r
1365     /* padded to a multiple of samp_factor DCT blocks in each direction. */\r
1366 \r
1367     /* Note we ask for a pre-zeroed array. */\r
1368 \r
1369     int ci, access_rows;\r
1370 \r
1371     jpeg_component_info *compptr;\r
1372 \r
1373 \r
1374 \r
1375     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
1376 \r
1377          ci++, compptr++) {\r
1378 \r
1379       access_rows = compptr->v_samp_factor;\r
1380 \r
1381 #ifdef BLOCK_SMOOTHING_SUPPORTED\r
1382 \r
1383       /* If block smoothing could be used, need a bigger window */\r
1384 \r
1385       if (cinfo->progressive_mode)\r
1386 \r
1387         access_rows *= 3;\r
1388 \r
1389 #endif\r
1390 \r
1391       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)\r
1392 \r
1393         ((j_common_ptr) cinfo, JPOOL_IMAGE, TRUE,\r
1394 \r
1395          (JDIMENSION) jround_up((long) compptr->width_in_blocks,\r
1396 \r
1397                                 (long) compptr->h_samp_factor),\r
1398 \r
1399          (JDIMENSION) jround_up((long) compptr->height_in_blocks,\r
1400 \r
1401                                 (long) compptr->v_samp_factor),\r
1402 \r
1403          (JDIMENSION) access_rows);\r
1404 \r
1405     }\r
1406 \r
1407     coef->pub.consume_data = consume_data;\r
1408 \r
1409     coef->pub.decompress_data = decompress_data;\r
1410 \r
1411     coef->pub.coef_arrays = coef->whole_image; /* link to virtual arrays */\r
1412 \r
1413 #else\r
1414 \r
1415     ERREXIT(cinfo, JERR_NOT_COMPILED);\r
1416 \r
1417 #endif\r
1418 \r
1419   } else {\r
1420 \r
1421     /* We only need a single-MCU buffer. */\r
1422 \r
1423     JBLOCKROW buffer;\r
1424 \r
1425     int i;\r
1426 \r
1427 \r
1428 \r
1429     buffer = (JBLOCKROW)\r
1430 \r
1431       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
1432 \r
1433                                   D_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));\r
1434 \r
1435     for (i = 0; i < D_MAX_BLOCKS_IN_MCU; i++) {\r
1436 \r
1437       coef->MCU_buffer[i] = buffer + i;\r
1438 \r
1439     }\r
1440 \r
1441     coef->pub.consume_data = dummy_consume_data;\r
1442 \r
1443     coef->pub.decompress_data = decompress_onepass;\r
1444 \r
1445     coef->pub.coef_arrays = NULL; /* flag for no virtual arrays */\r
1446 \r
1447   }\r
1448 \r
1449 }\r
1450 \r