-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
354 lines (300 loc) · 9.28 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <fenv.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#define private static
/** x */
#define M (8)
/** y */
#define N (8)
/** u, x */
private double cosine_x[M][M];
/** v, y */
private double cosine_y[N][N];
/** basis[v][u][y][x] */
private double basis[N][M][N][M];
private double sqrt_1_by_2;
private double sqrt_m_and_n;
private void __attribute__ ((nothrow))
init_cosine_x () {
uint_fast8_t u, x;
for (u = 0; u < M; u++)
for (x = 0; x < M; x++)
cosine_x[u][x] = cos (M_PI_2 * u / M * (double) (2 * x + 1));
}
private void __attribute__ ((nothrow))
init_cosine_y () {
uint_fast8_t v, y;
for (v = 0; v < N; v++)
for (y = 0; y < N; y++)
cosine_y[v][y] = cos (M_PI_2 * v / N * (double) (2 * y + 1));
}
/** Write a function to calculate the DCT basis values. I would suggest using
* the function to populate a four-dimensional array of floating point values.
* E.g., basis[u][v][x][y]. Since there are 4096 values, please, do NOT print
* them all to the screen. */
private void __attribute__ ((nothrow))
init_basis () {
uint_fast8_t v, u, y, x;
for (v = 0; v < N; v++)
for (u = 0; u < M; u++)
for (y = 0; y < N; y++)
for (x = 0; x < M; x++)
basis[v][u][y][x] = cosine_x[u][x] * cosine_y[v][y];
}
#ifdef DEBUG
private void __attribute__ ((nothrow))
print_basis () {
uint_fast8_t v, u, y, x;
for (v = 0; v < N; v++)
for (u = 0; u < M; u++) {
printf ("u=%u, v=%u\n", (unsigned int) u, (unsigned int) v);
for (y = 0; y < N; y++) {
for (x = 0; x < M - 1; x++)
printf ("%10g ", basis[v][u][y][x]);
printf ("%10g\n", basis[v][u][y][x]);
}
puts ("");
}
}
#endif
private signed char __attribute__ ((const, nothrow, warn_unused_result))
dct_signed (const unsigned char c) {
return (signed char) c - 128;
}
private void __attribute__ ((nothrow))
kahan (
double *const sum,
const double input,
double *const c
) {
const double y = input - *c;
const double t = *sum + y;
*c = (t - *sum) - y;
*sum = t;
}
private double __attribute__ ((nothrow, pure, warn_unused_result))
alpha (const uint_fast8_t u) {
if (u == 0)
return sqrt_1_by_2;
return 1;
}
private void __attribute__ ((nonnull, nothrow))
dct_transform (double dest[N][M], const unsigned char src[N][M]) {
uint_fast8_t v, u, y, x;
memset (dest, 0, N * M * sizeof (double));
for (v = 0; v < N; v++)
for (u = 0; u < M; u++) {
/*dest[v][u] = 0;*/
double error = 0;
for (y = 0; y < N; y++)
for (x = 0; x < M; x++)
/*kahan (&(dest[v][u]), dct_signed (src[y][x]) * cosine_x[u][x] * cosine_y[v][y], &error);*/
kahan (&(dest[v][u]), dct_signed (src[y][x]) * basis[v][u][y][x], &error);
dest[v][u] *= 2.0 / sqrt_m_and_n * alpha (v) * alpha (u);
dest[v][u] = nearbyint (dest[v][u]);
}
}
private void __attribute__ ((nonnull, nothrow))
print_orig (const unsigned char orig[N][M]) {
uint_fast8_t y, x;
for (y = 0; y < N; y++) {
for (x = 0; x < M - 1; x++)
printf ("%3u ", orig[y][x]);
printf ("%3u\n", orig[y][x]);
}
puts ("");
}
private void __attribute__ ((nonnull, nothrow))
print_dct (const double orig[N][M]) {
uint_fast8_t y, x;
for (y = 0; y < N; y++) {
for (x = 0; x < M - 1; x++)
printf ("%4g ", orig[y][x]);
printf ("%4g\n", orig[y][x]);
}
puts ("");
}
/** Write a function that takes as input a two-dimensional, 8 x 8 array of
* unsigned characters, and calculates the DCT. The output should be a two-
* -dimensional, 8 x 8 array of floating point values. Since the DCT is a
* /signed/ operation, you will first need to convert the unsigned characters
* to signed values (i.e., subtract 128 -- yes, it is THAT easy). This function
* should print the input matrix to the screen, then print the resulting DCT
* matrix. */
private void __attribute__ ((nonnull, nothrow))
dct_transform_bad_design (double dest[N][M], const unsigned char src[N][M]) {
print_orig (src);
dct_transform (dest, src);
print_dct (dest);
}
private unsigned char __attribute__ ((const, nothrow, warn_unused_result))
dct_signed (const signed char c) {
return (unsigned char) c - 128;
}
private void __attribute__ ((nonnull, nothrow))
idct_transform (unsigned char dest[N][M], const double src[N][M]) {
uint_fast8_t v, u, y, x;
for (x = 0; x < M; x++)
for (y = 0; y < N; y++) {
double temp = 0;
double error = 0;
signed char temp2;
for (v = 0; v < N; v++)
for (u = 0; u < M; u++)
/*kahan (&temp, alpha (v) * alpha (u) * src[v][u] * cosine_x[u][x] * cosine_y[v][y], &error);*/
kahan (&temp, alpha (v) * alpha (u) * src[v][u] * basis[v][u][y][x], &error);
temp *= 2.0 / N;
temp2 = nearbyint (temp);
dest[y][x] = dct_signed (temp2);
}
}
/** Write a function that takes as input a two-dimensional, 8 x 8 array of
* floating point numbers and calculates the inverse DCT. The output should be
* a two-dimensional, 8 x 8 array of unsigned characters. Since the IDCT is a
* /signed/ operation, you will need to convert the signed characters to
* unsigned values (i.e., subtract 128 -- yes, it is THAT easy, again). This
* function should print the input matrix to the screen, then print the
* resulting matrix. If called in succession to the other function, there is no
* need to print the floating point matrix twice. */
/*
private void __attribute__ ((nonnull, nothrow))
idct_transform_bad_design (unsigned char dest[N][M], const double src[N][M]) {
print_dct (src);
idct_transform (dest, src);
print_orig (dest);
}
*/
private void __attribute__ ((nonnull, nothrow))
dct_idct_transform (unsigned char dest[N][M], const unsigned char src[N][M]) {
double temp[N][M];
/*
print_orig (src);
dct_transform (temp, src);
print_dct (temp);
*/
dct_transform_bad_design (temp, src);
idct_transform (dest, temp);
print_orig (dest);
}
private int __attribute__ ((nothrow, warn_unused_result))
randint (const int max) {
int tmp;
do tmp = rand ();
while (tmp >= RAND_MAX - RAND_MAX % max);
return tmp % max;
}
private void __attribute__ ((nonnull, nothrow))
init_all_rand (unsigned char all_rand[N][M]) {
uint_fast8_t y, x;
for (y = 0; y < N; y++)
for (x = 0; x < M; x++)
all_rand[y][x] = randint (255);
}
int __attribute__ ((nonnull, nothrow, warn_unused_result))
main () {
#ifndef BENCHMARK
const unsigned char eyebrow[N][M] = {
{231, 224, 224, 217, 217, 203, 189, 196},
{210, 217, 203, 189, 203, 224, 217, 224},
{196, 217, 210, 224, 203, 203, 196, 189},
{210, 203, 196, 203, 182, 203, 182, 189},
{203, 224, 203, 217, 196, 175, 154, 140},
{182, 189, 168, 161, 154, 126, 119, 112},
{175, 154, 126, 105, 140, 105, 119, 84},
{154, 98, 105, 98, 105, 63, 112, 84}
};
const unsigned char eye[N][M] = {
{ 42, 28, 35, 28, 42, 49, 35, 42},
{ 49, 49, 35, 28, 35, 35, 35, 42},
{ 42, 21, 21, 28, 42, 35, 42, 28},
{ 21, 35, 35, 42, 42, 28, 28, 14},
{ 56, 70, 77, 84, 91, 28, 28, 21},
{ 70, 126, 133, 147, 161, 91, 35, 14},
{126, 203, 189, 182, 175, 175, 35, 21},
{ 49, 189, 245, 210, 182, 84, 21, 35}
};
const unsigned char nose[N][M] = {
{154, 154, 175, 182, 189, 168, 217, 175},
{154, 147, 168, 154, 168, 168, 196, 175},
{175, 154, 203, 175, 189, 182, 196, 182},
{175, 168, 168, 168, 140, 175, 168, 203},
{133, 168, 154, 196, 175, 189, 203, 154},
{168, 161, 161, 168, 154, 154, 189, 189},
{147, 161, 175, 182, 189, 175, 217, 175},
{175, 175, 203, 175, 189, 175, 175, 182}
};
const unsigned char all_clear[N][M] = {
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0}
};
const unsigned char all_set[N][M] = {
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255},
{255, 255, 255, 255, 255, 255, 255, 255}
};
#endif
unsigned char all_rand[N][M];
/*
double dct_eyebrow[N][M];
double dct_eye[N][M];
double dct_nose[N][M];
*/
#ifndef BENCHMARK
unsigned char dct_eyebrow[N][M];
unsigned char dct_eye[N][M];
unsigned char dct_nose[N][M];
unsigned char dct_all_clear[N][M];
unsigned char dct_all_set[N][M];
#endif
unsigned char dct_all_rand[N][M];
#ifdef BENCHMARK
int k;
#endif
srand (getpid () + time (NULL));
fesetround (FE_TONEAREST);
sqrt_1_by_2 = sqrt (1.0 / 2.0);
sqrt_m_and_n = sqrt (M * N);
init_cosine_x ();
init_cosine_y ();
init_basis ();
#ifdef DEBUG
print_basis ();
#endif
/*
dct_transform_bad_design (dct_eyebrow, eyebrow);
dct_transform_bad_design (dct_eye, eye);
dct_transform_bad_design (dct_nose, nose);
*/
#ifndef BENCHMARK
dct_idct_transform (dct_eyebrow, eyebrow);
dct_idct_transform (dct_eye, eye);
dct_idct_transform (dct_nose, nose);
dct_idct_transform (dct_all_clear, all_clear);
dct_idct_transform (dct_all_set, all_set);
#endif
#ifdef BENCHMARK
for (k = 0; k < 1000; k++) {
#endif
init_all_rand (all_rand);
dct_idct_transform (dct_all_rand, all_rand);
#ifdef BENCHMARK
}
#endif
return EXIT_SUCCESS;
}