3 { Original : jcdctmgr.c ; Copyright (C) 1994-1996, 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.
8 This file contains the forward-DCT management logic.
9 This code selects a particular DCT implementation to be used,
10 and it performs related housekeeping chores including coefficient
11 quantization. }
13 interface
15 {$N+}
16 {$I imjconfig.inc}
18 uses
19 imjmorecfg,
20 imjinclude,
21 imjdeferr,
22 imjerror,
23 imjpeglib,
27 { Initialize FDCT manager. }
29 {GLOBAL}
32 implementation
35 { Private subobject for this module }
37 type
42 { Pointer to the DCT routine actually in use }
45 { The actual post-DCT divisors --- not identical to the quant table
46 entries, because of scaling (especially for an unnormalized DCT).
47 Each table is given in normal array order. }
51 {$ifdef DCT_FLOAT_SUPPORTED}
52 { Same as above for the floating-point case. }
55 {$endif}
59 { Initialize for a processing pass.
60 Verify that all referenced Q-tables are present, and set up
61 the divisor table for each one.
62 In the current implementation, DCT of all components is done during
63 the first pass, even if only some components will be output in the
64 first scan. Hence all components should be examined here. }
66 {METHODDEF}
68 var
74 {$ifdef DCT_IFAST_SUPPORTED}
75 const
87 {SHIFT_TEMPS}
89 { Descale and correctly round an INT32 value that's scaled by N bits.
90 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
91 the fudge factor is correct for either sign of X. }
94 var
96 begin
98 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
101 else
102 {$endif}
106 {$endif}
107 {$ifdef DCT_FLOAT_SUPPORTED}
108 var
111 const
115 {$endif}
116 begin
120 begin
122 { Make sure specified quantization table is present }
127 { Compute divisors for this quant table }
128 { We may do this more than once for same table, but it's not a big deal }
130 {$ifdef DCT_ISLOW_SUPPORTED}
131 JDCT_ISLOW:
132 begin
133 { For LL&M IDCT method, divisors are equal to raw quantization
134 coefficients multiplied by 8 (to counteract scaling). }
137 begin
144 begin
148 {$endif}
149 {$ifdef DCT_IFAST_SUPPORTED}
150 JDCT_IFAST:
151 begin
152 { For AA&N IDCT method, divisors are equal to quantization
153 coefficients scaled by scalefactor[row]*scalefactor[col], where
154 scalefactor[0] := 1
155 scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
156 We apply a further scale factor of 8. }
160 begin
167 begin
169 {MULTIPLY16V16}
174 {$endif}
175 {$ifdef DCT_FLOAT_SUPPORTED}
177 JDCT_FLOAT:
178 begin
179 { For float AA&N IDCT method, divisors are equal to quantization
180 coefficients scaled by scalefactor[row]*scalefactor[col], where
181 scalefactor[0] := 1
182 scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
183 We apply a further scale factor of 8.
184 What's actually stored is 1/divisor so that the inner loop can
185 use a multiplication rather than a division. }
188 begin
196 begin
198 begin
206 {$endif}
207 else
215 { Perform forward DCT on one or more blocks of a component.
217 The input samples are taken from the sample_data[] array starting at
218 position start_row/start_col, and moving to the right for any additional
219 blocks. The quantized coefficients are returned in coef_blocks[]. }
221 {METHODDEF}
229 { This version is used for integer DCT implementations. }
230 var
231 { This routine is heavily used, so it's worth coding it tightly. }
237 var
241 {$ifndef DCTSIZE_IS_8}
242 var
244 {$endif}
245 var
249 begin
257 begin
259 { Load data into workspace, applying unsigned->signed conversion }
263 begin
289 {Inc(elemptr); - Value never used }
290 {$else}
292 begin
297 {$endif}
300 { Perform the DCT }
303 { Quantize/descale the coefficients, and store into coef_blocks[] }
307 begin
310 { Divide the coefficient value by qval, ensuring proper rounding.
311 Since C does not specify the direction of rounding for negative
312 quotients, we have to force the dividend positive for portability.
314 In most files, at least half of the output values will be zero
315 (at default quantization settings, more like three-quarters...)
316 so we should ensure that this case is fast. On many machines,
317 a comparison is enough cheaper than a divide to make a special test
318 a win. Since both inputs will be nonnegative, we need only test
319 for a < b to discover whether a/b is 0.
320 If your machine's division is fast enough, define FAST_DIVIDE. }
323 begin
326 {DIVIDE_BY(temp, qval);}
327 {$ifdef FAST_DIVIDE}
329 {$else}
332 else
334 {$endif}
336 end
337 else
338 begin
340 {DIVIDE_BY(temp, qval);}
341 {$ifdef FAST_DIVIDE}
343 {$else}
346 else
348 {$endif}
357 {$ifdef DCT_FLOAT_SUPPORTED}
359 {METHODDEF}
367 { This version is used for floating-point DCT implementations. }
368 var
369 { This routine is heavily used, so it's worth coding it tightly. }
375 var
379 {$ifndef DCTSIZE_IS_8}
380 var
382 {$endif}
383 var
387 begin
395 begin
396 { Load data into workspace, applying unsigned->signed conversion }
400 begin
426 {Inc(elemptr); - value never used }
427 {$else}
429 begin
435 {$endif}
439 { Perform the DCT }
442 { Quantize/descale the coefficients, and store into coef_blocks[] }
447 begin
448 { Apply the quantization and scaling factor }
450 { Round to nearest integer.
451 Since C does not specify the direction of rounding for negative
452 quotients, we have to force the dividend positive for portability.
453 The maximum coefficient size is +-16K (for 12-bit data), so this
454 code should work for either 16-bit or 32-bit ints. }
464 { Initialize FDCT manager. }
466 {GLOBAL}
468 var
471 begin
479 {$ifdef DCT_ISLOW_SUPPORTED}
480 JDCT_ISLOW:
481 begin
485 {$endif}
486 {$ifdef DCT_IFAST_SUPPORTED}
487 JDCT_IFAST:
488 begin
492 {$endif}
493 {$ifdef DCT_FLOAT_SUPPORTED}
494 JDCT_FLOAT:
495 begin
499 {$endif}
500 else
504 { Mark divisor tables unallocated }
506 begin
508 {$ifdef DCT_FLOAT_SUPPORTED}
510 {$endif}