DEADSOFTWARE

Net: Buffer outgoing messages
[d2df-sdl.git] / src / lib / vampimg / JpegLib / imjfdctflt.pas
1 unit imjfdctflt;
3 {$N+}
4 { This file contains a floating-point implementation of the
5 forward DCT (Discrete Cosine Transform).
7 This implementation should be more accurate than either of the integer
8 DCT implementations. However, it may not give the same results on all
9 machines because of differences in roundoff behavior. Speed will depend
10 on the hardware's floating point capacity.
12 A 2-D DCT can be done by 1-D DCT on each row followed by 1-D DCT
13 on each column. Direct algorithms are also available, but they are
14 much more complex and seem not to be any faster when reduced to code.
16 This implementation is based on Arai, Agui, and Nakajima's algorithm for
17 scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
18 Japanese, but the algorithm is described in the Pennebaker & Mitchell
19 JPEG textbook (see REFERENCES section in file README). The following code
20 is based directly on figure 4-8 in P&M.
21 While an 8-point DCT cannot be done in less than 11 multiplies, it is
22 possible to arrange the computation so that many of the multiplies are
23 simple scalings of the final outputs. These multiplies can then be
24 folded into the multiplications or divisions by the JPEG quantization
25 table entries. The AA&N method leaves only 5 multiplies and 29 adds
26 to be done in the DCT itself.
27 The primary disadvantage of this method is that with a fixed-point
28 implementation, accuracy is lost due to imprecise representation of the
29 scaled quantization values. However, that problem does not arise if
30 we use floating point arithmetic. }
32 { Original : jfdctflt.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
34 interface
36 {$I imjconfig.inc}
38 uses
39 imjmorecfg,
40 imjinclude,
41 imjpeglib,
42 imjdct; { Private declarations for DCT subsystem }
45 { Perform the forward DCT on one block of samples.}
47 {GLOBAL}
48 procedure jpeg_fdct_float (var data : array of FAST_FLOAT);
50 implementation
52 { This module is specialized to the case DCTSIZE = 8. }
54 {$ifndef DCTSIZE_IS_8}
55 Sorry, this code only copes with 8x8 DCTs. { deliberate syntax err }
56 {$endif}
59 { Perform the forward DCT on one block of samples.}
61 {GLOBAL}
62 procedure jpeg_fdct_float (var data : array of FAST_FLOAT);
63 type
64 PWorkspace = ^TWorkspace;
65 TWorkspace = array [0..DCTSIZE2-1] of FAST_FLOAT;
66 var
67 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7 : FAST_FLOAT;
68 tmp10, tmp11, tmp12, tmp13 : FAST_FLOAT;
69 z1, z2, z3, z4, z5, z11, z13 : FAST_FLOAT;
70 dataptr : PWorkspace;
71 ctr : int;
72 begin
73 { Pass 1: process rows. }
75 dataptr := PWorkspace(@data);
76 for ctr := DCTSIZE-1 downto 0 do
77 begin
78 tmp0 := dataptr^[0] + dataptr^[7];
79 tmp7 := dataptr^[0] - dataptr^[7];
80 tmp1 := dataptr^[1] + dataptr^[6];
81 tmp6 := dataptr^[1] - dataptr^[6];
82 tmp2 := dataptr^[2] + dataptr^[5];
83 tmp5 := dataptr^[2] - dataptr^[5];
84 tmp3 := dataptr^[3] + dataptr^[4];
85 tmp4 := dataptr^[3] - dataptr^[4];
87 { Even part }
89 tmp10 := tmp0 + tmp3; { phase 2 }
90 tmp13 := tmp0 - tmp3;
91 tmp11 := tmp1 + tmp2;
92 tmp12 := tmp1 - tmp2;
94 dataptr^[0] := tmp10 + tmp11; { phase 3 }
95 dataptr^[4] := tmp10 - tmp11;
97 z1 := (tmp12 + tmp13) * ({FAST_FLOAT}(0.707106781)); { c4 }
98 dataptr^[2] := tmp13 + z1; { phase 5 }
99 dataptr^[6] := tmp13 - z1;
101 { Odd part }
103 tmp10 := tmp4 + tmp5; { phase 2 }
104 tmp11 := tmp5 + tmp6;
105 tmp12 := tmp6 + tmp7;
107 { The rotator is modified from fig 4-8 to avoid extra negations. }
108 z5 := (tmp10 - tmp12) * ( {FAST_FLOAT}(0.382683433)); { c6 }
109 z2 := {FAST_FLOAT}(0.541196100) * tmp10 + z5; { c2-c6 }
110 z4 := {FAST_FLOAT}(1.306562965) * tmp12 + z5; { c2+c6 }
111 z3 := tmp11 * {FAST_FLOAT} (0.707106781); { c4 }
113 z11 := tmp7 + z3; { phase 5 }
114 z13 := tmp7 - z3;
116 dataptr^[5] := z13 + z2; { phase 6 }
117 dataptr^[3] := z13 - z2;
118 dataptr^[1] := z11 + z4;
119 dataptr^[7] := z11 - z4;
121 Inc(FAST_FLOAT_PTR(dataptr), DCTSIZE); { advance pointer to next row }
122 end;
124 { Pass 2: process columns. }
126 dataptr := PWorkspace(@data);
127 for ctr := DCTSIZE-1 downto 0 do
128 begin
129 tmp0 := dataptr^[DCTSIZE*0] + dataptr^[DCTSIZE*7];
130 tmp7 := dataptr^[DCTSIZE*0] - dataptr^[DCTSIZE*7];
131 tmp1 := dataptr^[DCTSIZE*1] + dataptr^[DCTSIZE*6];
132 tmp6 := dataptr^[DCTSIZE*1] - dataptr^[DCTSIZE*6];
133 tmp2 := dataptr^[DCTSIZE*2] + dataptr^[DCTSIZE*5];
134 tmp5 := dataptr^[DCTSIZE*2] - dataptr^[DCTSIZE*5];
135 tmp3 := dataptr^[DCTSIZE*3] + dataptr^[DCTSIZE*4];
136 tmp4 := dataptr^[DCTSIZE*3] - dataptr^[DCTSIZE*4];
138 { Even part }
140 tmp10 := tmp0 + tmp3; { phase 2 }
141 tmp13 := tmp0 - tmp3;
142 tmp11 := tmp1 + tmp2;
143 tmp12 := tmp1 - tmp2;
145 dataptr^[DCTSIZE*0] := tmp10 + tmp11; { phase 3 }
146 dataptr^[DCTSIZE*4] := tmp10 - tmp11;
148 z1 := (tmp12 + tmp13) * {FAST_FLOAT} (0.707106781); { c4 }
149 dataptr^[DCTSIZE*2] := tmp13 + z1; { phase 5 }
150 dataptr^[DCTSIZE*6] := tmp13 - z1;
152 { Odd part }
154 tmp10 := tmp4 + tmp5; { phase 2 }
155 tmp11 := tmp5 + tmp6;
156 tmp12 := tmp6 + tmp7;
158 { The rotator is modified from fig 4-8 to avoid extra negations. }
159 z5 := (tmp10 - tmp12) * {FAST_FLOAT} (0.382683433); { c6 }
160 z2 := {FAST_FLOAT} (0.541196100) * tmp10 + z5; { c2-c6 }
161 z4 := {FAST_FLOAT} (1.306562965) * tmp12 + z5; { c2+c6 }
162 z3 := tmp11 * {FAST_FLOAT} (0.707106781); { c4 }
164 z11 := tmp7 + z3; { phase 5 }
165 z13 := tmp7 - z3;
167 dataptr^[DCTSIZE*5] := z13 + z2; { phase 6 }
168 dataptr^[DCTSIZE*3] := z13 - z2;
169 dataptr^[DCTSIZE*1] := z11 + z4;
170 dataptr^[DCTSIZE*7] := z11 - z4;
172 Inc(FAST_FLOAT_PTR(dataptr)); { advance pointer to next column }
173 end;
174 end;
176 end.