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