-
Notifications
You must be signed in to change notification settings - Fork 1
/
dct.c
175 lines (155 loc) · 4.47 KB
/
dct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "dct.h"
/*2D forward DCT transform*/
void fdct(const double *inblock, double *outblock)
{
double s[8],t[8],r[8];
const double *p;
int j;
/*Horizontal direction*/
p=inblock;
for(j=0;j<64;j+=8)
{
/*First stage*/
s[0]=(*(p)+*(p+7));
s[1]=(*(p+1)+*(p+6));
s[2]=(*(p+2)+*(p+5));
s[3]=(*(p+3)+*(p+4));
s[4]=(*(p+3)-*(p+4));
s[5]=(*(p+2)-*(p+5));
s[6]=(*(p+1)-*(p+6));
s[7]=(*(p)-*(p+7));
/*Second stage*/
t[0]=s[0]+s[3];
t[1]=s[1]+s[2];
t[2]=s[1]-s[2];
t[3]=s[0]-s[3];
t[5]=(s[6]-s[5])*W3;
t[6]=(s[6]+s[5])*W3;
/*Third stage*/
r[4]=s[4]+t[5];
r[5]=s[4]-t[5];
r[6]=s[7]-t[6];
r[7]=s[7]+t[6];
/*Fourth stage*/
outblock[j]=(t[0]+t[1])*W3;
outblock[4+j]=(t[0]-t[1])*W3;
outblock[1+j]=(r[4]*W5+r[7]*W4);
outblock[7+j]=(r[7]*W5-r[4]*W4);
outblock[3+j]=(r[6]*W6-r[5]*W7);
outblock[5+j]=(r[5]*W6+r[6]*W7);
outblock[2+j]=(t[2]*W2+t[3]*W1);
outblock[6+j]=(t[3]*W2-t[2]*W1);
p +=8;
}
/*Vertical direction*/
for(j=0;j<8;j++)
{
/*First stage*/
s[0]=outblock[j]+outblock[j+56];
s[1]=outblock[j+8]+outblock[j+48];
s[2]=outblock[j+16]+outblock[j+40];
s[3]=outblock[j+24]+outblock[j+32];
s[4]=outblock[j+24]-outblock[j+32];
s[5]=outblock[j+16]-outblock[j+40];
s[6]=outblock[j+8]-outblock[j+48];
s[7]=outblock[j]-outblock[j+56];
/*Second stage*/
t[0]=s[0]+s[3];
t[1]=s[1]+s[2];
t[2]=s[1]-s[2];
t[3]=s[0]-s[3];
t[5]=(s[6]-s[5])*W3;
t[6]=(s[6]+s[5])*W3;
/*Third stage*/
r[4]=s[4]+t[5];
r[5]=s[4]-t[5];
r[6]=s[7]-t[6];
r[7]=s[7]+t[6];
/*Fourth stage, transform coefficients scaled by 2/N*/
outblock[j]=(t[0]+t[1])*W3/4.0;
outblock[32+j]=(t[0]-t[1])*W3/4.0;
outblock[8+j]=(r[4]*W5+r[7]*W4)/4.0;
outblock[56+j]=(r[7]*W5-r[4]*W4)/4.0;
outblock[24+j]=(r[6]*W6-r[5]*W7)/4.0;
outblock[40+j]=(r[5]*W6+r[6]*W7)/4.0;
outblock[16+j]=(t[2]*W2+t[3]*W1)/4.0;
outblock[48+j]=(t[3]*W2-t[2]*W1)/4.0;
}
}
/*2D inverse DCT transfrom*/
void idct(const double *inblock, double *outblock)
{
double s[8],t[8],r[8];
const double *p;
int j;
/*Horizontal direction*/
p=inblock;
for(j=0;j<64;j+=8)
{
/*First stage*/
r[4]=*(p+1)*W5-*(p+7)*W4;
r[5]=*(p+5)*W6-*(p+3)*W7;
r[6]=*(p+3)*W6+*(p+5)*W7;
r[7]=*(p+7)*W5+*(p+1)*W4;
/*Second stage*/
t[0]=(*(p)+*(p+4))*W3;
t[1]=(*(p)-*(p+4))*W3;
t[2]=*(p+2)*W2-*(p+6)*W1;
t[3]=*(p+2)*W1+*(p+6)*W2;
t[4]=(r[4]+r[5]);
t[5]=(r[4]-r[5]);
t[6]=(r[7]-r[6]);
t[7]=(r[6]+r[7]);
/*Third stage*/
s[0]=(t[0]+t[3]);
s[1]=(t[1]+t[2]);
s[2]=(t[1]-t[2]);
s[3]=(t[0]-t[3]);
s[5]=(t[6]-t[5])*W3;
s[6]=(t[5]+t[6])*W3;
/*Fourth stage*/
outblock[j]=(s[0]+t[7]);
outblock[j+1]=(s[1]+s[6]);
outblock[j+2]=(s[2]+s[5]);
outblock[j+3]=(s[3]+t[4]);
outblock[j+4]=(s[3]-t[4]);
outblock[j+5]=(s[2]-s[5]);
outblock[j+6]=(s[1]-s[6]);
outblock[j+7]=(s[0]-t[7]);
p +=8;
}
/*Vertical direction*/
for(j=0;j<8;j++)
{
/*First stage*/
r[4]=outblock[j+8]*W5-outblock[j+56]*W4;
r[5]=outblock[j+40]*W6-outblock[j+24]*W7;
r[6]=outblock[j+24]*W6+outblock[j+40]*W7;
r[7]=outblock[j+56]*W5+outblock[j+8]*W4;
/*Second stage*/
t[0]=(outblock[j]+outblock[j+32])*W3;
t[1]=(outblock[j]-outblock[j+32])*W3;
t[2]=outblock[j+16]*W2-outblock[j+48]*W1;
t[3]=outblock[j+16]*W1+outblock[j+48]*W2;
t[4]=(r[4]+r[5]);
t[5]=(r[4]-r[5]);
t[6]=(r[7]-r[6]);
t[7]=(r[6]+r[7]);
/*Third stage*/
s[0]=(t[0]+t[3]);
s[1]=(t[1]+t[2]);
s[2]=(t[1]-t[2]);
s[3]=(t[0]-t[3]);
s[5]=(t[6]-t[5])*W3;
s[6]=(t[5]+t[6])*W3;
/*Fourth stage, pixel values scaled by 2/N*/
outblock[j]=(s[0]+t[7])/4.0;
outblock[j+8]=(s[1]+s[6])/4.0;
outblock[j+16]=(s[2]+s[5])/4.0;
outblock[j+24]=(s[3]+t[4])/4.0;
outblock[j+32]=(s[3]-t[4])/4.0;
outblock[j+40]=(s[2]-s[5])/4.0;
outblock[j+48]=(s[1]-s[6])/4.0;
outblock[j+56]=(s[0]-t[7])/4.0;
}
}