annotate Meerwald/dct.c @ 22:d8551fb39a5e default tip

Added tag v0.6 for changeset 1c4ccd635a68
author Peter Meerwald-Stadler <pmeerw@pmeerw.net>
date Sat, 28 Jan 2023 23:57:51 +0100
parents bd669312f068
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2 #include "dct.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4 #define INVROOT2 0.7071067814
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 #define SWAP(A, B) {double t = A; A = B; B = t;}
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 int N;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 int M;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 double *dct_NxN_tmp = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 double *dct_NxN_costable = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 int dct_NxN_log2N = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 static const unsigned int JPEG_lumin_quant_table[NJPEG][NJPEG] = {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 {16, 11, 10, 16, 24, 40, 51, 61},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 {12, 12, 14, 19, 26, 58, 60, 55},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 {14, 13, 16, 24, 40, 57, 69, 56},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 {14, 17, 22, 29, 51, 87, 80, 62},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19 {18, 22, 37, 56, 68, 109, 103, 77},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 {24, 35, 55, 64, 81, 104, 113, 92},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 {49, 64, 78, 87, 103, 121, 120, 101},
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22 {72, 92, 95, 98, 112, 100, 103, 99}};
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24 static void initcosarray()
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 int i,group,base,item,nitems,halfN;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 double factor;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 dct_NxN_log2N = -1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 do{
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 dct_NxN_log2N++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 if ((1<<dct_NxN_log2N)>N){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 fprintf(stderr, "dct_NxN: %d not a power of 2\n", N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 }while((1<<dct_NxN_log2N)<N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37 if (dct_NxN_costable) free(dct_NxN_costable);
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
38 dct_NxN_costable = malloc(N * sizeof(double));
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 if(!dct_NxN_costable){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 fprintf(stderr, "Unable to allocate C array\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 halfN=N/2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 for(i=0;i<=halfN-1;i++) dct_NxN_costable[halfN+i]=4*i+1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 for(group=1;group<=dct_NxN_log2N-1;group++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 base= 1<<(group-1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 nitems=base;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 factor = 1.0*(1<<(dct_NxN_log2N-group));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 for(item=1; item<=nitems;item++) dct_NxN_costable[base+item-1]=factor*dct_NxN_costable[halfN+item-1];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 for(i=1;i<=N-1;i++) dct_NxN_costable[i] = 1.0/(2.0*cos(dct_NxN_costable[i]*M_PI/(2.0*N)));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 void init_dct_NxN(int width, int height) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 if (width != height || width <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 fprintf(stderr, "init_dct_NxN(): dimensions out of range\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 if (dct_NxN_tmp && M != height)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 free(dct_NxN_tmp);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 N = width;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 M = height;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
71 dct_NxN_tmp = malloc(height * sizeof(double));
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 if (!dct_NxN_tmp) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 fprintf(stderr, "init_dct_NxN(): failed to allocate memory\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 initcosarray();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 static void bitrev(double *f, int len)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 int i,j,m;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 if (len<=2) return; /* No action necessary if n=1 or n=2 */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 j=1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 for(i=1; i<=len; i++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 if(i<j)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 SWAP(f[j-1], f[i-1]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 m = len>>1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 while(j>m){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 j=j-m;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 m=(m+1)>>1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96 j=j+m;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 static void inv_sums(double *f)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 int stepsize,stage,curptr,nthreads,thread,step,nsteps;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104 for(stage=1; stage <=dct_NxN_log2N-1; stage++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 nthreads = 1<<(stage-1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 stepsize = nthreads<<1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 nsteps = (1<<(dct_NxN_log2N-stage)) - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 for(thread=1; thread<=nthreads; thread++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109 curptr=N-thread;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 for(step=1; step<=nsteps; step++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 f[curptr] += f[curptr-stepsize];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 curptr -= stepsize;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 static void fwd_sums(double *f)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 int stepsize,stage,curptr,nthreads,thread,step,nsteps;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 for(stage=dct_NxN_log2N-1; stage >=1; stage--){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 nthreads = 1<<(stage-1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 stepsize = nthreads<<1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 nsteps = (1<<(dct_NxN_log2N-stage)) - 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 for(thread=1; thread<=nthreads; thread++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 curptr=nthreads +thread-1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 for(step=1; step<=nsteps; step++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 f[curptr] += f[curptr+stepsize];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 curptr += stepsize;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136 static void scramble(double *f,int len){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 int i,ii1,ii2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139 bitrev(f,len);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 bitrev(&f[0], len>>1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141 bitrev(&f[len>>1], len>>1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 ii1=len-1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 ii2=len>>1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
144 for(i=0; i<(len>>2); i++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
145 SWAP(f[ii1], f[ii2]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
146 ii1--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
147 ii2++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
148 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
149 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
150
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
151 static void unscramble(double *f,int len)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
152 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
153 int i,ii1,ii2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
154
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
155 ii1 = len-1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
156 ii2 = len>>1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
157 for(i=0; i<(len>>2); i++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
158 SWAP(f[ii1], f[ii2]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
159 ii1--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
160 ii2++;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
161 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
162 bitrev(&f[0], len>>1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
163 bitrev(&f[len>>1], len>>1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
164 bitrev(f,len);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
165 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
166
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
167 static void inv_butterflies(double *f)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
168 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
169 int stage,ii1,ii2,butterfly,ngroups,group,wingspan,increment,baseptr;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
170 double Cfac,T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
171
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
172 for(stage=1; stage<=dct_NxN_log2N;stage++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
173 ngroups=1<<(dct_NxN_log2N-stage);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
174 wingspan=1<<(stage-1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
175 increment=wingspan<<1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
176 for(butterfly=1; butterfly<=wingspan; butterfly++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
177 Cfac = dct_NxN_costable[wingspan+butterfly-1];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
178 baseptr=0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
179 for(group=1; group<=ngroups; group++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
180 ii1=baseptr+butterfly-1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
181 ii2=ii1+wingspan;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
182 T=Cfac * f[ii2];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
183 f[ii2]=f[ii1]-T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
184 f[ii1]=f[ii1]+T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
185 baseptr += increment;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
186 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
187 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
188 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
189 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
190
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
191 static void fwd_butterflies(double *f)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
192 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
193 int stage,ii1,ii2,butterfly,ngroups,group,wingspan,increment,baseptr;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
194 double Cfac,T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
195
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
196 for(stage=dct_NxN_log2N; stage>=1;stage--){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
197 ngroups=1<<(dct_NxN_log2N-stage);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
198 wingspan=1<<(stage-1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
199 increment=wingspan<<1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
200 for(butterfly=1; butterfly<=wingspan; butterfly++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
201 Cfac = dct_NxN_costable[wingspan+butterfly-1];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
202 baseptr=0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
203 for(group=1; group<=ngroups; group++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
204 ii1=baseptr+butterfly-1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
205 ii2=ii1+wingspan;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
206 T= f[ii2];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
207 f[ii2]=Cfac *(f[ii1]-T);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
208 f[ii1]=f[ii1]+T;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
209 baseptr += increment;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
210 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
211 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
212 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
213 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
214
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
215 static void ifct_noscale(double *f)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
216 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
217 f[0] *= INVROOT2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
218 inv_sums(f);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
219 bitrev(f,N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
220 inv_butterflies(f);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
221 unscramble(f,N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
222 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
223
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
224 static void fct_noscale(double *f)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
225 {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
226 scramble(f,N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
227 fwd_butterflies(f);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
228 bitrev(f,N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
229 fwd_sums(f);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
230 f[0] *= INVROOT2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
231 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
232
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
233 void fdct_NxN(gray **pixels, double **dcts) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
234 int u,v;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
235 double two_over_sqrtncolsnrows = 2.0/sqrt((double) N*M);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
236
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
237 for (u=0; u < N; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
238 for (v=0; v < M; v++)
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
239 dcts[u][v] = ((int) pixels[u][v] - 128);
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
240
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
241 for (u=0; u<=M-1; u++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
242 fct_noscale(dcts[u]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
243 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
244 for (v=0; v<=N-1; v++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
245 for (u=0; u<=M-1; u++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
246 dct_NxN_tmp[u] = dcts[u][v];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
247 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
248 fct_noscale(dct_NxN_tmp);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
249 for (u=0; u<=M-1; u++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
250 dcts[u][v] = dct_NxN_tmp[u]*two_over_sqrtncolsnrows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
251 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
252 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
253 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
254
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
255 void idct_NxN(double **dcts, gray **pixels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
256 int u,v;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
257 double two_over_sqrtncolsnrows = 2.0/sqrt((double) N*M);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
258
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
259 double **tmp;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
260
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
261 tmp = alloc_coeffs(N, N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
262 for (u=0;u<N;u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
263 for (v=0;v<M;v++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
264 tmp[u][v] = dcts[u][v];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
265
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
266 for (u=0; u<=M-1; u++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
267 ifct_noscale(tmp[u]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
268 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
269 for (v=0; v<=N-1; v++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
270 for (u=0; u<=M-1; u++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
271 dct_NxN_tmp[u] = tmp[u][v];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
272 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
273 ifct_noscale(dct_NxN_tmp);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
274 for (u=0; u<=M-1; u++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
275 tmp[u][v] = dct_NxN_tmp[u]*two_over_sqrtncolsnrows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
276 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
277 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
278
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
279 for (u=0;u<N;u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
280 for (v=0;v<M;v++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
281 pixels[u][v] = PIXELRANGE(tmp[u][v] + 128.5);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
282 free(tmp);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
283 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
284
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
285 void fdct_inplace_NxN(double **coeffs) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
286 int u,v;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
287 double two_over_sqrtncolsnrows = 2.0/sqrt((double) N*M);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
288
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
289 for (u=0; u<=M-1; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
290 fct_noscale(coeffs[u]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
291
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
292 for (v=0; v<=N-1; v++){
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
293 for (u=0; u<=M-1; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
294 dct_NxN_tmp[u] = coeffs[u][v];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
295
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
296 fct_noscale(dct_NxN_tmp);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
297 for (u=0; u<=M-1; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
298 coeffs[u][v] = dct_NxN_tmp[u]*two_over_sqrtncolsnrows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
299 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
300 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
301
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
302 void idct_inplace_NxN(double **coeffs) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
303 int u,v;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
304 double two_over_sqrtncolsnrows = 2.0/sqrt((double) N*M);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
305
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
306 for (u=0; u<=M-1; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
307 ifct_noscale(coeffs[u]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
308
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
309 for (v=0; v<=N-1; v++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
310 for (u=0; u<=M-1; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
311 dct_NxN_tmp[u] = coeffs[u][v];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
312
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
313 ifct_noscale(dct_NxN_tmp);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
314 for (u=0; u<=M-1; u++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
315 coeffs[u][v] = dct_NxN_tmp[u]*two_over_sqrtncolsnrows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
316 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
317
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
318 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
319
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
320 double **dct_NxM_costable_x = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
321 double **dct_NxM_costable_y = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
322
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
323 void init_dct_NxM(int cols, int rows) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
324 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
325 double cx = sqrt(2.0 / cols);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
326 double cy = sqrt(2.0 / rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
327
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
328 #ifdef DEBUG
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
329 if (cols <= 0 || rows <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
330 fprintf(stderr, "init_dct_NxM(): dimensions out of range\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
331 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
332 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
333 #endif
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
334
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
335 if (dct_NxM_costable_x && N != cols) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
336 free_coeffs(dct_NxM_costable_x);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
337 dct_NxM_costable_x = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
338 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
339
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
340 if (dct_NxM_costable_y && M != rows) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
341 free_coeffs(dct_NxM_costable_y);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
342 dct_NxM_costable_y = NULL;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
343 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
344
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
345 if (!dct_NxM_costable_x)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
346 dct_NxM_costable_x = alloc_coeffs(cols, cols);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
347 if (!dct_NxM_costable_y)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
348 dct_NxM_costable_y = alloc_coeffs(rows, rows);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
349
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
350 N = cols;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
351 M = rows;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
352
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
353 for (i = 0; i < cols; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
354 for (j = 0; j < cols; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
355 dct_NxM_costable_x[i][j] = cx * cos((M_PI * ((2*i + 1) * j)) / (double) (2 * N));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
356 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
357 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
358
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
359 for (i = 0; i < rows; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
360 for (j = 0; j < rows; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
361 dct_NxM_costable_y[i][j] = cy * cos((M_PI * ((2*i + 1) * j)) / (double) (2 * M));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
362 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
363 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
364 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
365
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
366 void fdct_NxM(gray **pixels, double **dcts) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
367 int x, y;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
368 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
369 double t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
370 double cx0 = sqrt(1.0 / N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
371 double cy0 = sqrt(1.0 / M);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
372
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
373 t = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
374 for (x = 0; x < N; x++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
375 for (y = 0; y < M; y++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
376 t += ((int) pixels[y][x] - 128);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
377 dcts[0][0] = cx0 * cy0 * t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
378
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
379 for (i = 1; i < N; i++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
380 t = 0.0;
12
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
381 for (x = 0; x < N; x++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
382 double s = 0.0;
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
383 for (y = 0; y < M; y++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
384 s += ((int) pixels[y][x] - 128);
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
385 }
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
386 t += s * dct_NxM_costable_x[x][i];
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
387 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
388 dcts[0][i] = cy0 * t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
389 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
390
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
391 for (j = 1; j < M; j++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
392 t = 0.0;
12
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
393 for (y = 0; y < M; y++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
394 double s = 0.0;
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
395 for (x = 0; x < N; x++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
396 s += ((int) pixels[y][x] - 128);
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
397 }
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
398 t += s * dct_NxM_costable_y[y][j];
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
399 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
400 dcts[j][0] = cx0 * t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
401 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
402
12
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
403 for (i = 1; i < N; i++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
404 for (j = 1; j < M; j++) {
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
405 t = 0.0;
12
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
406 for (x = 0; x < N; x++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
407 double s = 0;
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
408 for (y = 0; y < M; y++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
409 s += ((int) pixels[y][x] - 128) * dct_NxM_costable_y[y][j];
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
410 }
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
411 t += s * dct_NxM_costable_x[x][i];
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
412 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
413 dcts[j][i] = t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
414 }
12
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
415 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
416 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
417
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
418 void idct_NxM(double **dcts, gray **pixels) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
419 int x, y;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
420 int i, j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
421 double cx0 = sqrt(1.0 / N);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
422 double cy0 = sqrt(1.0 / M);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
423 double t;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
424
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
425 for (x = 0; x < N; x++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
426 for (y = 0; y < M; y++) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
427
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
428 t = cx0 * cy0 * dcts[0][0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
429
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
430 for (i = 1; i < N; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
431 t += cy0 * dcts[0][i] * dct_NxM_costable_x[x][i];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
432
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
433 for (j = 1; j < M; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
434 t += cx0 * dcts[j][0] * dct_NxM_costable_y[y][j];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
435
12
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
436 for (i = 1; i < N; i++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
437 double s = 0.0;
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
438 for (j = 1; j < M; j++) {
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
439 s += dcts[j][i] * dct_NxM_costable_y[y][j];
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
440 }
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
441 t += s * dct_NxM_costable_x[x][i];
6f5fea21a43c NxM DCT speedup
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 8
diff changeset
442 }
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
443
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
444 pixels[y][x] = PIXELRANGE((int) (t + 128.5));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
445 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
446 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
447 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
448
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
449 double C[NJPEG][NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
450 double Ct[NJPEG][NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
451 int Quantum[NJPEG][NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
452
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
453 void init_quantum_8x8(int quality) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
454 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
455 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
456
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
457 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
458 for ( j = 0 ; j < NJPEG ; j++ )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
459 Quantum[ i ][ j ] = 1 + ( ( 1 + i + j ) * quality );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
460 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
461
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
462 void init_quantum_JPEG_lumin(int quality) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
463 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
464 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
465
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
466 if (quality < 50)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
467 quality = 5000 / quality;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
468 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
469 quality = 200 - quality * 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
470
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
471 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
472 for (j = 0 ; j < NJPEG ; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
473 if (quality)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
474 Quantum[i][j] = (JPEG_lumin_quant_table[i][j] * quality + 50) / 100;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
475 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
476 Quantum[i][j] = JPEG_lumin_quant_table[i][j];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
477 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
478
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
479 void init_quantum_JPEG_chromin(int quality) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
480 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
481 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
482
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
483 if (quality < 50)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
484 quality = 5000 / quality;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
485 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
486 quality = 200 - quality * 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
487
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
488 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
489 for (j = 0 ; j < NJPEG ; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
490 if (quality)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
491 Quantum[i][j] = (JPEG_lumin_quant_table[i][j] * quality + 50) / 100;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
492 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
493 Quantum[i][j] = JPEG_lumin_quant_table[i][j];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
494 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
495
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
496 void quantize_8x8(double **transform) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
497 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
498 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
499
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
500 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
501 for (j = 0; j < NJPEG; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
502 transform[i][j] = ROUND(transform[i][j] / Quantum[i][j]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
503 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
504
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
505 void dequantize_8x8(double **transform) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
506 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
507 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
508
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
509 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
510 for (j = 0; j < NJPEG; j++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
511 transform[i][j] = ROUND(transform[i][j] * Quantum[i][j]);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
512 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
513
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
514 void init_dct_8x8() {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
515 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
516 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
517 double pi = atan( 1.0 ) * 4.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
518
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
519 for ( j = 0 ; j < NJPEG ; j++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
520 C[ 0 ][ j ] = 1.0 / sqrt( (double) NJPEG );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
521 Ct[ j ][ 0 ] = C[ 0 ][ j ];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
522 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
523
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
524 for ( i = 1 ; i < NJPEG ; i++ )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
525 for ( j = 0 ; j < NJPEG ; j++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
526 C[ i ][ j ] = sqrt( 2.0 / NJPEG ) * cos( pi * ( 2 * j + 1 ) * i / ( 2.0 * NJPEG ) );
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
527 Ct[ j ][ i ] = C[ i ][ j ];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
528 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
529 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
530
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
531 /*
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
532 * The Forward DCT routine implements the matrix function:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
533 *
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
534 * DCT = C * pixels * Ct
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
535 */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
536
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
537 void fdct_8x8(gray **input, double **output) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
538 double temp[NJPEG][NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
539 double temp1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
540 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
541 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
542 int k;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
543
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
544 /* MatrixMultiply( temp, input, Ct ); */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
545 for ( i = 0 ; i < NJPEG ; i++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
546 for ( j = 0 ; j < NJPEG ; j++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
547 temp[ i ][ j ] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
548 for ( k = 0 ; k < NJPEG ; k++ )
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
549 temp[ i ][ j ] += ( (int) input[ i ][ k ] - 128 ) *
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
550 Ct[ k ][ j ];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
551 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
552 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
553
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
554 /* MatrixMultiply( output, C, temp ); */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
555 for ( i = 0 ; i < NJPEG ; i++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
556 for ( j = 0 ; j < NJPEG ; j++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
557 temp1 = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
558 for ( k = 0 ; k < NJPEG ; k++ )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
559 temp1 += C[ i ][ k ] * temp[ k ][ j ];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
560 output[ i ][ j ] = temp1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
561 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
562 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
563 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
564
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
565 void fdct_block_8x8(gray **input, int col, int row, double **output) {
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 3
diff changeset
566 int i;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
567 gray *input_array[NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
568
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
569 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
570 input_array[i] = &input[row + i][col];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
571
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
572 fdct_8x8(input_array, output);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
573 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
574
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
575 /*
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
576 * The Inverse DCT routine implements the matrix function:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
577 *
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
578 * pixels = C * DCT * Ct
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
579 */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
580
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
581 void idct_8x8(double **input, gray **output) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
582 double temp[ NJPEG ][ NJPEG ];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
583 double temp1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
584 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
585 int j;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
586 int k;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
587
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
588 /* MatrixMultiply( temp, input, C ); */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
589 for ( i = 0 ; i < NJPEG ; i++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
590 for ( j = 0 ; j < NJPEG ; j++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
591 temp[ i ][ j ] = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
592 for ( k = 0 ; k < NJPEG ; k++ )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
593 temp[ i ][ j ] += input[ i ][ k ] * C[ k ][ j ];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
594 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
595 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
596
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
597 /* MatrixMultiply( output, Ct, temp ); */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
598 for ( i = 0 ; i < NJPEG ; i++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
599 for ( j = 0 ; j < NJPEG ; j++ ) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
600 temp1 = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
601 for ( k = 0 ; k < NJPEG ; k++ )
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
602 temp1 += Ct[ i ][ k ] * temp[ k ][ j ];
3
acb6967ee76d update to 0.5
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 0
diff changeset
603 temp1 += 128.0;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
604 output[i][j] = PIXELRANGE(ROUND(temp1));
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
605 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
606 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
607 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
608
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
609 void idct_block_8x8(double **input, gray **output, int col, int row) {
8
f83ef905a63d fixing many warnings
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents: 3
diff changeset
610 int i;
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
611 gray *output_array[NJPEG];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
612
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
613 for (i = 0; i < NJPEG; i++)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
614 output_array[i] = &output[row + i][col];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
615
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
616 idct_8x8(input, output_array);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
617 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
618
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
619 int is_middle_frequency_coeff_8x8(int coeff) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
620 switch (coeff) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
621 case 3:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
622 case 10:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
623 case 17:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
624 case 24:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
625 return 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
626 case 4:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
627 case 11:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
628 case 18:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
629 case 25:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
630 case 32:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
631 return 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
632 case 5:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
633 case 12:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
634 case 19:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
635 case 26:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
636 case 33:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
637 case 40:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
638 return 3;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
639 case 13:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
640 case 20:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
641 case 27:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
642 case 34:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
643 case 41:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
644 return 4;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
645 case 28:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
646 case 35:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
647 return 5;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
648 default:
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
649 return 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
650 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
651 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.