Im trying to implement a forward and inverse Discrete Cosine Transform (DCT) in C. The code is to transorm a single input block of pixels to the transformation matrix via the dct() function and then back to the original pixel values via the idct() function. Please see the attached code. My output form the idct are consecutive values of 244, 116, 244, 116 etc. From the look of the idct values, it doesnt look like my program is working.. Could someone help me out and give me an idea of what results i should be expecting after each function? Obviously after the idct, i should be getting prety close to the original input Matrix.
Thanks
# include <stdio.h>
# define PI 3.14
void dct(float [][]); // Function prototypes
void idct(float [][]); // Function prototypes
void dct(float inMatrix[8][8]){
double dct,
Cu,
sum,
Cv;
int i,
j,
u,
h = 0,
v;
FILE * fp = fopen("mydata.csv", "w");
float dctMatrix[8][8],
greyLevel;
for (u = 0; u < 8; ++u) {
for (v = 0; v < 8; ++v) {
if (u == 0) {
Cu = 1.0 / sqrt(2.0);
} else {
Cu = 1.0;
}
if (v == 0) {
Cv = 1.0 / sqrt(2.0);
} else {
Cu = (1.0);
}
sum = 0.0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
// Level around 0
greyLevel = inMatrix[i][j];
dct = greyLevel * cos((2 * i + 1) * u * PI / 16.0) *
cos((2 * j + 1) * v * PI / 16.0);
sum += dct;
}
}
dctMatrix[u][v] = 0.25 * Cu * Cv * sum;
fprintf(fp, "\n %f", dctMatrix[u][v]);
}
fprintf(fp, "\n");
}
idct(dctMatrix);
}
void idct(float dctMatrix[8][8]){
double idct,
Cu,
sum,
Cv;
int i,
j,
u,
v;
float idctMatrix[8][8],
greyLevel;
FILE * fp = fopen("mydata.csv", "a");
fprintf(fp, "\n Inverse DCT");
for (i = 0; i < 8; ++i) {
for (j = 0; j < 8; ++j) {
sum = 0.0;
for (u = 0; u < 8; u++) {
for (v = 0; v < 8; v++) {
if (u == 0) {
Cu = 1.0 / sqrt(2.0);
} else {
Cu = 1.0;
}
if (v == 0) {
Cv = 1.0 / sqrt(2.0);
} else {
Cu = (1.0);
}
// Level around 0
greyLevel = dctMatrix[u][v];
idct = (greyLevel * cos((2 * i + 1) * u * PI / 16.0) *
cos((2 * j + 1) * v * PI / 16.0));
sum += idct;
}
}
idctMatrix[i][j] = 0.25 * Cu * Cv * sum;
fprintf(fp, "\n %f", idctMatrix[i][j]);
}
fprintf(fp, "\n");
}
}
int main() {
float
testBlockA[8][8] = { {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} },
testBlockB[8][8] = {{255, 0, 255, 0, 255, 0, 255, 0},
{0, 255, 0, 255, 0, 255, 0, 255},
{255, 0, 255, 0, 255, 0, 255, 0},
{0, 255, 0, 255, 0, 255, 0, 255},
{255, 0, 255, 0, 255, 0, 255, 0},
{0, 255, 0, 255, 0, 255, 0, 255},
{255, 0, 255, 0, 255, 0, 255, 0},
{0, 255, 0, 255, 0, 255, 0, 255} };
dct(testBlockB);
}