DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / JpegLib / imjidctfst.pas
1 unit imjidctfst;
3 { This file contains a fast, not so accurate integer implementation of the
4 inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
5 must also perform dequantization of the input coefficients.
7 A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
8 on each row (or vice versa, but it's more convenient to emit a row at
9 a time). Direct algorithms are also available, but they are much more
10 complex and seem not to be any faster when reduced to code.
12 This implementation is based on Arai, Agui, and Nakajima's algorithm for
13 scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
14 Japanese, but the algorithm is described in the Pennebaker & Mitchell
15 JPEG textbook (see REFERENCES section in file README). The following code
16 is based directly on figure 4-8 in P&M.
17 While an 8-point DCT cannot be done in less than 11 multiplies, it is
18 possible to arrange the computation so that many of the multiplies are
19 simple scalings of the final outputs. These multiplies can then be
20 folded into the multiplications or divisions by the JPEG quantization
21 table entries. The AA&N method leaves only 5 multiplies and 29 adds
22 to be done in the DCT itself.
23 The primary disadvantage of this method is that with fixed-point math,
24 accuracy is lost due to imprecise representation of the scaled
25 quantization values. The smaller the quantization table entry, the less
26 precise the scaled value, so this implementation does worse with high-
27 quality-setting files than with low-quality ones. }
29 { Original : jidctfst.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
32 interface
34 {$I imjconfig.inc}
36 uses
37 imjmorecfg,
38 imjinclude,
39 imjpeglib,
40 imjdct; { Private declarations for DCT subsystem }
43 { Perform dequantization and inverse DCT on one block of coefficients. }
45 {GLOBAL}
46 procedure jpeg_idct_ifast (cinfo : j_decompress_ptr;
47 compptr : jpeg_component_info_ptr;
48 coef_block : JCOEFPTR;
49 output_buf : JSAMPARRAY;
50 output_col : JDIMENSION);
52 implementation
54 { This module is specialized to the case DCTSIZE = 8. }
56 {$ifndef DCTSIZE_IS_8}
57 Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
58 {$endif}
60 { Scaling decisions are generally the same as in the LL&M algorithm;
61 see jidctint.c for more details. However, we choose to descale
62 (right shift) multiplication products as soon as they are formed,
63 rather than carrying additional fractional bits into subsequent additions.
64 This compromises accuracy slightly, but it lets us save a few shifts.
65 More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
66 everywhere except in the multiplications proper; this saves a good deal
67 of work on 16-bit-int machines.
69 The dequantized coefficients are not integers because the AA&N scaling
70 factors have been incorporated. We represent them scaled up by PASS1_BITS,
71 so that the first and second IDCT rounds have the same input scaling.
72 For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
73 avoid a descaling shift; this compromises accuracy rather drastically
74 for small quantization table entries, but it saves a lot of shifts.
75 For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
76 so we use a much larger scaling factor to preserve accuracy.
78 A final compromise is to represent the multiplicative constants to only
79 8 fractional bits, rather than 13. This saves some shifting work on some
80 machines, and may also reduce the cost of multiplication (since there
81 are fewer one-bits in the constants). }
83 {$ifdef BITS_IN_JSAMPLE_IS_8}
84 const
85 CONST_BITS = 8;
86 PASS1_BITS = 2;
87 {$else}
88 const
89 CONST_BITS = 8;
90 PASS1_BITS = 1; { lose a little precision to avoid overflow }
91 {$endif}
94 const
95 FIX_1_082392200 = INT32(Round((INT32(1) shl CONST_BITS)*1.082392200)); {277}
96 FIX_1_414213562 = INT32(Round((INT32(1) shl CONST_BITS)*1.414213562)); {362}
97 FIX_1_847759065 = INT32(Round((INT32(1) shl CONST_BITS)*1.847759065)); {473}
98 FIX_2_613125930 = INT32(Round((INT32(1) shl CONST_BITS)*2.613125930)); {669}
101 { Descale and correctly round an INT32 value that's scaled by N bits.
102 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
103 the fudge factor is correct for either sign of X. }
105 function DESCALE(x : INT32; n : int) : INT32;
106 var
107 shift_temp : INT32;
108 begin
109 {$ifdef USE_ACCURATE_ROUNDING}
110 shift_temp := x + (INT32(1) shl (n-1));
111 {$else}
112 { We can gain a little more speed, with a further compromise in accuracy,
113 by omitting the addition in a descaling shift. This yields an incorrectly
114 rounded result half the time... }
115 shift_temp := x;
116 {$endif}
118 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
119 if shift_temp < 0 then
120 Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
121 else
122 {$endif}
123 Descale := (shift_temp shr n);
124 end;
127 { Multiply a DCTELEM variable by an INT32 constant, and immediately
128 descale to yield a DCTELEM result. }
130 {(DCTELEM( DESCALE((var) * (const), CONST_BITS))}
131 function Multiply(Avar, Aconst: Integer): DCTELEM;
132 begin
133 Multiply := DCTELEM( Avar*INT32(Aconst) div (INT32(1) shl CONST_BITS));
134 end;
137 { Dequantize a coefficient by multiplying it by the multiplier-table
138 entry; produce a DCTELEM result. For 8-bit data a 16x16->16
139 multiplication will do. For 12-bit data, the multiplier table is
140 declared INT32, so a 32-bit multiply will be used. }
142 {$ifdef BITS_IN_JSAMPLE_IS_8}
143 function DEQUANTIZE(coef,quantval : int) : int;
144 begin
145 Dequantize := ( IFAST_MULT_TYPE(coef) * quantval);
146 end;
147 {$else}
148 function DEQUANTIZE(coef,quantval : INT32) : int;
149 begin
150 Dequantize := DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS);
151 end;
152 {$endif}
155 { Like DESCALE, but applies to a DCTELEM and produces an int.
156 We assume that int right shift is unsigned if INT32 right shift is. }
158 function IDESCALE(x : DCTELEM; n : int) : int;
159 {$ifdef BITS_IN_JSAMPLE_IS_8}
160 const
161 DCTELEMBITS = 16; { DCTELEM may be 16 or 32 bits }
162 {$else}
163 const
164 DCTELEMBITS = 32; { DCTELEM must be 32 bits }
165 {$endif}
166 var
167 ishift_temp : DCTELEM;
168 begin
169 {$ifndef USE_ACCURATE_ROUNDING}
170 ishift_temp := x + (INT32(1) shl (n-1));
171 {$else}
172 { We can gain a little more speed, with a further compromise in accuracy,
173 by omitting the addition in a descaling shift. This yields an incorrectly
174 rounded result half the time... }
175 ishift_temp := x;
176 {$endif}
178 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
179 if ishift_temp < 0 then
180 IDescale := (ishift_temp shr n)
181 or ((not DCTELEM(0)) shl (DCTELEMBITS-n))
182 else
183 {$endif}
184 IDescale := (ishift_temp shr n);
185 end;
189 { Perform dequantization and inverse DCT on one block of coefficients. }
191 {GLOBAL}
192 procedure jpeg_idct_ifast (cinfo : j_decompress_ptr;
193 compptr : jpeg_component_info_ptr;
194 coef_block : JCOEFPTR;
195 output_buf : JSAMPARRAY;
196 output_col : JDIMENSION);
197 type
198 PWorkspace = ^TWorkspace;
199 TWorkspace = coef_bits_field; { buffers data between passes }
200 var
201 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : DCTELEM;
202 tmp10, tmp11, tmp12, tmp13 : DCTELEM;
203 z5, z10, z11, z12, z13 : DCTELEM;
204 inptr : JCOEFPTR;
205 quantptr : IFAST_MULT_TYPE_FIELD_PTR;
206 wsptr : PWorkspace;
207 outptr : JSAMPROW;
208 range_limit : JSAMPROW;
209 ctr : int;
210 workspace : TWorkspace; { buffers data between passes }
211 {SHIFT_TEMPS} { for DESCALE }
212 {ISHIFT_TEMPS} { for IDESCALE }
213 var
214 dcval : int;
215 var
216 dcval_ : JSAMPLE;
217 begin
218 { Each IDCT routine is responsible for range-limiting its results and
219 converting them to unsigned form (0..MAXJSAMPLE). The raw outputs could
220 be quite far out of range if the input data is corrupt, so a bulletproof
221 range-limiting step is required. We use a mask-and-table-lookup method
222 to do the combined operations quickly. See the comments with
223 prepare_range_limit_table (in jdmaster.c) for more info. }
225 range_limit := JSAMPROW(@(cinfo^.sample_range_limit^[CENTERJSAMPLE]));
226 { Pass 1: process columns from input, store into work array. }
228 inptr := coef_block;
229 quantptr := IFAST_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
230 wsptr := @workspace;
231 for ctr := pred(DCTSIZE) downto 0 do
232 begin
233 { Due to quantization, we will usually find that many of the input
234 coefficients are zero, especially the AC terms. We can exploit this
235 by short-circuiting the IDCT calculation for any column in which all
236 the AC terms are zero. In that case each output is equal to the
237 DC coefficient (with scale factor as needed).
238 With typical images and quantization tables, half or more of the
239 column DCT calculations can be simplified this way. }
241 if (inptr^[DCTSIZE*1]=0) and (inptr^[DCTSIZE*2]=0) and (inptr^[DCTSIZE*3]=0) and
242 (inptr^[DCTSIZE*4]=0) and (inptr^[DCTSIZE*5]=0) and (inptr^[DCTSIZE*6]=0) and
243 (inptr^[DCTSIZE*7]=0) then
244 begin
245 { AC terms all zero }
246 dcval := int(DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]));
248 wsptr^[DCTSIZE*0] := dcval;
249 wsptr^[DCTSIZE*1] := dcval;
250 wsptr^[DCTSIZE*2] := dcval;
251 wsptr^[DCTSIZE*3] := dcval;
252 wsptr^[DCTSIZE*4] := dcval;
253 wsptr^[DCTSIZE*5] := dcval;
254 wsptr^[DCTSIZE*6] := dcval;
255 wsptr^[DCTSIZE*7] := dcval;
257 Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
258 Inc(IFAST_MULT_TYPE_PTR(quantptr));
259 Inc(int_ptr(wsptr));
260 continue;
261 end;
263 { Even part }
265 tmp0 := DEQUANTIZE(inptr^[DCTSIZE*0], quantptr^[DCTSIZE*0]);
266 tmp1 := DEQUANTIZE(inptr^[DCTSIZE*2], quantptr^[DCTSIZE*2]);
267 tmp2 := DEQUANTIZE(inptr^[DCTSIZE*4], quantptr^[DCTSIZE*4]);
268 tmp3 := DEQUANTIZE(inptr^[DCTSIZE*6], quantptr^[DCTSIZE*6]);
270 tmp10 := tmp0 + tmp2; { phase 3 }
271 tmp11 := tmp0 - tmp2;
273 tmp13 := tmp1 + tmp3; { phases 5-3 }
274 tmp12 := MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; { 2*c4 }
276 tmp0 := tmp10 + tmp13; { phase 2 }
277 tmp3 := tmp10 - tmp13;
278 tmp1 := tmp11 + tmp12;
279 tmp2 := tmp11 - tmp12;
281 { Odd part }
283 tmp4 := DEQUANTIZE(inptr^[DCTSIZE*1], quantptr^[DCTSIZE*1]);
284 tmp5 := DEQUANTIZE(inptr^[DCTSIZE*3], quantptr^[DCTSIZE*3]);
285 tmp6 := DEQUANTIZE(inptr^[DCTSIZE*5], quantptr^[DCTSIZE*5]);
286 tmp7 := DEQUANTIZE(inptr^[DCTSIZE*7], quantptr^[DCTSIZE*7]);
288 z13 := tmp6 + tmp5; { phase 6 }
289 z10 := tmp6 - tmp5;
290 z11 := tmp4 + tmp7;
291 z12 := tmp4 - tmp7;
293 tmp7 := z11 + z13; { phase 5 }
294 tmp11 := MULTIPLY(z11 - z13, FIX_1_414213562); { 2*c4 }
296 z5 := MULTIPLY(z10 + z12, FIX_1_847759065); { 2*c2 }
297 tmp10 := MULTIPLY(z12, FIX_1_082392200) - z5; { 2*(c2-c6) }
298 tmp12 := MULTIPLY(z10, - FIX_2_613125930) + z5; { -2*(c2+c6) }
300 tmp6 := tmp12 - tmp7; { phase 2 }
301 tmp5 := tmp11 - tmp6;
302 tmp4 := tmp10 + tmp5;
304 wsptr^[DCTSIZE*0] := int (tmp0 + tmp7);
305 wsptr^[DCTSIZE*7] := int (tmp0 - tmp7);
306 wsptr^[DCTSIZE*1] := int (tmp1 + tmp6);
307 wsptr^[DCTSIZE*6] := int (tmp1 - tmp6);
308 wsptr^[DCTSIZE*2] := int (tmp2 + tmp5);
309 wsptr^[DCTSIZE*5] := int (tmp2 - tmp5);
310 wsptr^[DCTSIZE*4] := int (tmp3 + tmp4);
311 wsptr^[DCTSIZE*3] := int (tmp3 - tmp4);
313 Inc(JCOEF_PTR(inptr)); { advance pointers to next column }
314 Inc(IFAST_MULT_TYPE_PTR(quantptr));
315 Inc(int_ptr(wsptr));
316 end;
318 { Pass 2: process rows from work array, store into output array. }
319 { Note that we must descale the results by a factor of 8 == 2**3, }
320 { and also undo the PASS1_BITS scaling. }
322 wsptr := @workspace;
323 for ctr := 0 to pred(DCTSIZE) do
324 begin
325 outptr := JSAMPROW(@output_buf^[ctr]^[output_col]);
326 { Rows of zeroes can be exploited in the same way as we did with columns.
327 However, the column calculation has created many nonzero AC terms, so
328 the simplification applies less often (typically 5% to 10% of the time).
329 On machines with very fast multiplication, it's possible that the
330 test takes more time than it's worth. In that case this section
331 may be commented out. }
333 {$ifndef NO_ZERO_ROW_TEST}
334 if (wsptr^[1]=0) and (wsptr^[2]=0) and (wsptr^[3]=0) and (wsptr^[4]=0) and
335 (wsptr^[5]=0) and (wsptr^[6]=0) and (wsptr^[7]=0) then
336 begin
337 { AC terms all zero }
338 dcval_ := range_limit^[IDESCALE(wsptr^[0], PASS1_BITS+3)
339 and RANGE_MASK];
341 outptr^[0] := dcval_;
342 outptr^[1] := dcval_;
343 outptr^[2] := dcval_;
344 outptr^[3] := dcval_;
345 outptr^[4] := dcval_;
346 outptr^[5] := dcval_;
347 outptr^[6] := dcval_;
348 outptr^[7] := dcval_;
350 Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
351 continue;
352 end;
353 {$endif}
355 { Even part }
357 tmp10 := (DCTELEM(wsptr^[0]) + DCTELEM(wsptr^[4]));
358 tmp11 := (DCTELEM(wsptr^[0]) - DCTELEM(wsptr^[4]));
360 tmp13 := (DCTELEM(wsptr^[2]) + DCTELEM(wsptr^[6]));
361 tmp12 := MULTIPLY(DCTELEM(wsptr^[2]) - DCTELEM(wsptr^[6]), FIX_1_414213562)
362 - tmp13;
364 tmp0 := tmp10 + tmp13;
365 tmp3 := tmp10 - tmp13;
366 tmp1 := tmp11 + tmp12;
367 tmp2 := tmp11 - tmp12;
369 { Odd part }
371 z13 := DCTELEM(wsptr^[5]) + DCTELEM(wsptr^[3]);
372 z10 := DCTELEM(wsptr^[5]) - DCTELEM(wsptr^[3]);
373 z11 := DCTELEM(wsptr^[1]) + DCTELEM(wsptr^[7]);
374 z12 := DCTELEM(wsptr^[1]) - DCTELEM(wsptr^[7]);
376 tmp7 := z11 + z13; { phase 5 }
377 tmp11 := MULTIPLY(z11 - z13, FIX_1_414213562); { 2*c4 }
379 z5 := MULTIPLY(z10 + z12, FIX_1_847759065); { 2*c2 }
380 tmp10 := MULTIPLY(z12, FIX_1_082392200) - z5; { 2*(c2-c6) }
381 tmp12 := MULTIPLY(z10, - FIX_2_613125930) + z5; { -2*(c2+c6) }
383 tmp6 := tmp12 - tmp7; { phase 2 }
384 tmp5 := tmp11 - tmp6;
385 tmp4 := tmp10 + tmp5;
387 { Final output stage: scale down by a factor of 8 and range-limit }
389 outptr^[0] := range_limit^[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
390 and RANGE_MASK];
391 outptr^[7] := range_limit^[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
392 and RANGE_MASK];
393 outptr^[1] := range_limit^[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
394 and RANGE_MASK];
395 outptr^[6] := range_limit^[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
396 and RANGE_MASK];
397 outptr^[2] := range_limit^[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
398 and RANGE_MASK];
399 outptr^[5] := range_limit^[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
400 and RANGE_MASK];
401 outptr^[4] := range_limit^[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
402 and RANGE_MASK];
403 outptr^[3] := range_limit^[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
404 and RANGE_MASK];
406 Inc(int_ptr(wsptr), DCTSIZE); { advance pointer to next row }
407 end;
408 end;
410 end.