3 {$N+}
4 { This file contains a floating-point implementation of the
5 inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
6 must also perform dequantization of the input coefficients.
8 This implementation should be more accurate than either of the integer
9 IDCT implementations. However, it may not give the same results on all
10 machines because of differences in roundoff behavior. Speed will depend
11 on the hardware's floating point capacity.
13 A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
14 on each row (or vice versa, but it's more convenient to emit a row at
15 a time). Direct algorithms are also available, but they are much more
16 complex and seem not to be any faster when reduced to code.
18 This implementation is based on Arai, Agui, and Nakajima's algorithm for
19 scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
20 Japanese, but the algorithm is described in the Pennebaker & Mitchell
21 JPEG textbook (see REFERENCES section in file README). The following code
22 is based directly on figure 4-8 in P&M.
23 While an 8-point DCT cannot be done in less than 11 multiplies, it is
24 possible to arrange the computation so that many of the multiplies are
25 simple scalings of the final outputs. These multiplies can then be
26 folded into the multiplications or divisions by the JPEG quantization
27 table entries. The AA&N method leaves only 5 multiplies and 29 adds
28 to be done in the DCT itself.
29 The primary disadvantage of this method is that with a fixed-point
30 implementation, accuracy is lost due to imprecise representation of the
31 scaled quantization values. However, that problem does not arise if
32 we use floating point arithmetic. }
34 { Original: jidctflt.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
36 interface
38 {$I imjconfig.inc}
40 uses
41 imjmorecfg,
42 imjinclude,
43 imjpeglib,
46 { Perform dequantization and inverse DCT on one block of coefficients. }
48 {GLOBAL}
55 implementation
57 { This module is specialized to the case DCTSIZE = 8. }
59 {$ifndef DCTSIZE_IS_8}
61 {$endif}
64 { Dequantize a coefficient by multiplying it by the multiplier-table
65 entry; produce a float result. }
68 begin
72 { Descale and correctly round an INT32 value that's scaled by N bits.
73 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
74 the fudge factor is correct for either sign of X. }
77 var
79 begin
80 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
84 else
86 {$else}
88 {$endif}
92 { Perform dequantization and inverse DCT on one block of coefficients. }
94 {GLOBAL}
100 type
103 var
114 {SHIFT_TEMPS}
115 var
117 begin
118 { Each IDCT routine is responsible for range-limiting its results and
119 converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
120 be quite far out of range if the input data is corrupt, so a bulletproof
121 range-limiting step is required. We use a mask-and-table-lookup method
122 to do the combined operations quickly. See the comments with
123 prepare_range_limit_table (in jdmaster.c) for more info. }
127 { Pass 1: process columns from input, store into work array. }
133 begin
134 { Due to quantization, we will usually find that many of the input
135 coefficients are zero, especially the AC terms. We can exploit this
136 by short-circuiting the IDCT calculation for any column in which all
137 the AC terms are zero. In that case each output is equal to the
138 DC coefficient (with scale factor as needed).
139 With typical images and quantization tables, half or more of the
140 column DCT calculations can be simplified this way. }
146 begin
147 { AC terms all zero }
162 continue;
165 { Even part }
183 { Odd part }
220 { Pass 2: process rows from work array, store into output array. }
221 { Note that we must descale the results by a factor of 8 = 2**3. }
225 begin
227 { Rows of zeroes can be exploited in the same way as we did with columns.
228 However, the column calculation has created many nonzero AC terms, so
229 the simplification applies less often (typically 5% to 10% of the time).
230 And testing floats for zero is relatively expensive, so we don't bother. }
232 { Even part }
245 { Odd part }
263 { Final output stage: scale down by a factor of 8 and range-limit }