DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / JpegLib / imjddctmgr.pas
1 unit imjddctmgr;
3 { Original : jddctmgr.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
5 { This file contains the inverse-DCT management logic.
6 This code selects a particular IDCT implementation to be used,
7 and it performs related housekeeping chores. No code in this file
8 is executed per IDCT step, only during output pass setup.
10 Note that the IDCT routines are responsible for performing coefficient
11 dequantization as well as the IDCT proper. This module sets up the
12 dequantization multiplier table needed by the IDCT routine. }
14 interface
16 {$I imjconfig.inc}
18 {$N+}
20 uses
21 imjmorecfg,
22 imjinclude,
23 imjdeferr,
24 imjerror,
25 imjpeglib,
26 imjdct, { Private declarations for DCT subsystem }
27 imjidctfst,
28 {$IFDEF BASM}
29 imjidctasm,
30 {$ELSE}
31 imjidctint,
32 {$ENDIF}
33 imjidctflt,
34 imjidctred;
38 { Initialize IDCT manager. }
40 {GLOBAL}
41 procedure jinit_inverse_dct (cinfo : j_decompress_ptr);
44 implementation
46 { The decompressor input side (jdinput.c) saves away the appropriate
47 quantization table for each component at the start of the first scan
48 involving that component. (This is necessary in order to correctly
49 decode files that reuse Q-table slots.)
50 When we are ready to make an output pass, the saved Q-table is converted
51 to a multiplier table that will actually be used by the IDCT routine.
52 The multiplier table contents are IDCT-method-dependent. To support
53 application changes in IDCT method between scans, we can remake the
54 multiplier tables if necessary.
55 In buffered-image mode, the first output pass may occur before any data
56 has been seen for some components, and thus before their Q-tables have
57 been saved away. To handle this case, multiplier tables are preset
58 to zeroes; the result of the IDCT will be a neutral gray level. }
61 { Private subobject for this module }
63 type
64 my_idct_ptr = ^my_idct_controller;
65 my_idct_controller = record
66 pub : jpeg_inverse_dct; { public fields }
68 { This array contains the IDCT method code that each multiplier table
69 is currently set up for, or -1 if it's not yet set up.
70 The actual multiplier tables are pointed to by dct_table in the
71 per-component comp_info structures. }
73 cur_method : array[0..MAX_COMPONENTS-1] of int;
74 end; {my_idct_controller;}
77 { Allocated multiplier tables: big enough for any supported variant }
79 type
80 multiplier_table = record
81 case byte of
82 0:(islow_array : array[0..DCTSIZE2-1] of ISLOW_MULT_TYPE);
83 {$ifdef DCT_IFAST_SUPPORTED}
84 1:(ifast_array : array[0..DCTSIZE2-1] of IFAST_MULT_TYPE);
85 {$endif}
86 {$ifdef DCT_FLOAT_SUPPORTED}
87 2:(float_array : array[0..DCTSIZE2-1] of FLOAT_MULT_TYPE);
88 {$endif}
89 end;
92 { The current scaled-IDCT routines require ISLOW-style multiplier tables,
93 so be sure to compile that code if either ISLOW or SCALING is requested. }
95 {$ifdef DCT_ISLOW_SUPPORTED}
96 {$define PROVIDE_ISLOW_TABLES}
97 {$else}
98 {$ifdef IDCT_SCALING_SUPPORTED}
99 {$define PROVIDE_ISLOW_TABLES}
100 {$endif}
101 {$endif}
104 { Prepare for an output pass.
105 Here we select the proper IDCT routine for each component and build
106 a matching multiplier table. }
108 {METHODDEF}
109 procedure start_pass (cinfo : j_decompress_ptr);
110 var
111 idct : my_idct_ptr;
112 ci, i : int;
113 compptr : jpeg_component_info_ptr;
114 method : J_DCT_METHOD;
115 method_ptr : inverse_DCT_method_ptr;
116 qtbl : JQUANT_TBL_PTR;
117 {$ifdef PROVIDE_ISLOW_TABLES}
118 var
119 ismtbl : ISLOW_MULT_TYPE_FIELD_PTR;
120 {$endif}
121 {$ifdef DCT_IFAST_SUPPORTED}
122 const
123 CONST_BITS = 14;
124 const
125 aanscales : array[0..DCTSIZE2-1] of INT16 =
126 ({ precomputed values scaled up by 14 bits }
127 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
128 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
129 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
130 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
131 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
132 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
133 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
134 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247);
135 var
136 ifmtbl : IFAST_MULT_TYPE_FIELD_PTR;
137 {SHIFT_TEMPS}
139 { Descale and correctly round an INT32 value that's scaled by N bits.
140 We assume RIGHT_SHIFT rounds towards minus infinity, so adding
141 the fudge factor is correct for either sign of X. }
143 function DESCALE(x : INT32; n : int) : INT32;
144 var
145 shift_temp : INT32;
146 begin
147 {$ifdef RIGHT_SHIFT_IS_UNSIGNED}
148 shift_temp := x + (INT32(1) shl (n-1));
149 if shift_temp < 0 then
150 Descale := (shift_temp shr n) or ((not INT32(0)) shl (32-n))
151 else
152 Descale := (shift_temp shr n);
153 {$else}
154 Descale := (x + (INT32(1) shl (n-1)) shr n;
155 {$endif}
156 end;
158 {$endif}
159 {$ifdef DCT_FLOAT_SUPPORTED}
160 const
161 aanscalefactor : array[0..DCTSIZE-1] of double =
162 (1.0, 1.387039845, 1.306562965, 1.175875602,
163 1.0, 0.785694958, 0.541196100, 0.275899379);
164 var
165 fmtbl : FLOAT_MULT_TYPE_FIELD_PTR;
166 row, col : int;
167 {$endif}
168 begin
169 idct := my_idct_ptr (cinfo^.idct);
170 method := J_DCT_METHOD(0);
171 method_ptr := NIL;
172 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
174 for ci := 0 to pred(cinfo^.num_components) do
175 begin
176 { Select the proper IDCT routine for this component's scaling }
177 case (compptr^.DCT_scaled_size) of
178 {$ifdef IDCT_SCALING_SUPPORTED}
179 1:begin
180 method_ptr := jpeg_idct_1x1;
181 method := JDCT_ISLOW; { jidctred uses islow-style table }
182 end;
183 2:begin
184 method_ptr := jpeg_idct_2x2;
185 method := JDCT_ISLOW; { jidctred uses islow-style table }
186 end;
187 4:begin
188 method_ptr := jpeg_idct_4x4;
189 method := JDCT_ISLOW; { jidctred uses islow-style table }
190 end;
191 {$endif}
192 DCTSIZE:
193 case (cinfo^.dct_method) of
194 {$ifdef DCT_ISLOW_SUPPORTED}
195 JDCT_ISLOW:
196 begin
197 method_ptr := @jpeg_idct_islow;
198 method := JDCT_ISLOW;
199 end;
200 {$endif}
201 {$ifdef DCT_IFAST_SUPPORTED}
202 JDCT_IFAST:
203 begin
204 method_ptr := @jpeg_idct_ifast;
205 method := JDCT_IFAST;
206 end;
207 {$endif}
208 {$ifdef DCT_FLOAT_SUPPORTED}
209 JDCT_FLOAT:
210 begin
211 method_ptr := @jpeg_idct_float;
212 method := JDCT_FLOAT;
213 end;
214 {$endif}
215 else
216 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
217 end;
218 else
219 ERREXIT1(j_common_ptr(cinfo), JERR_BAD_DCTSIZE, compptr^.DCT_scaled_size);
220 end;
221 idct^.pub.inverse_DCT[ci] := method_ptr;
222 { Create multiplier table from quant table.
223 However, we can skip this if the component is uninteresting
224 or if we already built the table. Also, if no quant table
225 has yet been saved for the component, we leave the
226 multiplier table all-zero; we'll be reading zeroes from the
227 coefficient controller's buffer anyway. }
229 if (not compptr^.component_needed) or (idct^.cur_method[ci] = int(method)) then
230 continue;
231 qtbl := compptr^.quant_table;
232 if (qtbl = NIL) then { happens if no data yet for component }
233 continue;
234 idct^.cur_method[ci] := int(method);
235 case (method) of
236 {$ifdef PROVIDE_ISLOW_TABLES}
237 JDCT_ISLOW:
238 begin
239 { For LL&M IDCT method, multipliers are equal to raw quantization
240 coefficients, but are stored as ints to ensure access efficiency. }
242 ismtbl := ISLOW_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
243 for i := 0 to pred(DCTSIZE2) do
244 begin
245 ismtbl^[i] := ISLOW_MULT_TYPE (qtbl^.quantval[i]);
246 end;
247 end;
248 {$endif}
249 {$ifdef DCT_IFAST_SUPPORTED}
250 JDCT_IFAST:
251 begin
252 { For AA&N IDCT method, multipliers are equal to quantization
253 coefficients scaled by scalefactor[row]*scalefactor[col], where
254 scalefactor[0] := 1
255 scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7
256 For integer operation, the multiplier table is to be scaled by
257 IFAST_SCALE_BITS. }
259 ifmtbl := IFAST_MULT_TYPE_FIELD_PTR (compptr^.dct_table);
261 for i := 0 to pred(DCTSIZE2) do
262 begin
263 ifmtbl^[i] := IFAST_MULT_TYPE(
264 DESCALE( INT32 (qtbl^.quantval[i]) * INT32 (aanscales[i]),
265 CONST_BITS-IFAST_SCALE_BITS) );
266 end;
267 end;
268 {$endif}
269 {$ifdef DCT_FLOAT_SUPPORTED}
270 JDCT_FLOAT:
271 begin
272 { For float AA&N IDCT method, multipliers are equal to quantization
273 coefficients scaled by scalefactor[row]*scalefactor[col], where
274 scalefactor[0] := 1
275 scalefactor[k] := cos(k*PI/16) * sqrt(2) for k=1..7 }
277 fmtbl := FLOAT_MULT_TYPE_FIELD_PTR(compptr^.dct_table);
279 i := 0;
280 for row := 0 to pred(DCTSIZE) do
281 begin
282 for col := 0 to pred(DCTSIZE) do
283 begin
284 fmtbl^[i] := {FLOAT_MULT_TYPE} (
285 {double} qtbl^.quantval[i] *
286 aanscalefactor[row] * aanscalefactor[col] );
287 Inc(i);
288 end;
289 end;
290 end;
291 {$endif}
292 else
293 ERREXIT(j_common_ptr(cinfo), JERR_NOT_COMPILED);
294 break;
295 end;
296 Inc(compptr);
297 end;
298 end;
301 { Initialize IDCT manager. }
303 {GLOBAL}
304 procedure jinit_inverse_dct (cinfo : j_decompress_ptr);
305 var
306 idct : my_idct_ptr;
307 ci : int;
308 compptr : jpeg_component_info_ptr;
309 begin
310 idct := my_idct_ptr(
311 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
312 SIZEOF(my_idct_controller)) );
313 cinfo^.idct := jpeg_inverse_dct_ptr (idct);
314 idct^.pub.start_pass := start_pass;
316 compptr := jpeg_component_info_ptr(cinfo^.comp_info);
317 for ci := 0 to pred(cinfo^.num_components) do
318 begin
319 { Allocate and pre-zero a multiplier table for each component }
320 compptr^.dct_table :=
321 cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
322 SIZEOF(multiplier_table));
323 MEMZERO(compptr^.dct_table, SIZEOF(multiplier_table));
324 { Mark multiplier table not yet set up for any method }
325 idct^.cur_method[ci] := -1;
326 Inc(compptr);
327 end;
328 end;
330 end.