Here is the code I am using to apply DCT on 8x8 blocks, which I'm not sure is correct. I have tried to follow Wikipedia's discrete cosine transformation (DCT) formula as closely as possible. Please let me know if there are any changes to be made.
//round up a float type and show two decimal places
double rndup(double n)
{
double t;
double temp;
t=n-floor(n);
if (t>=0.5)
{
n*=100.0;//where n is the multi-decimal float
n=ceil(n);
temp = floor(n);
temp = n - temp;
n-=temp;
n/=100.0;
}
else
{
n*=100.0;//where n is the multi-decimal float
n=floor(n);
temp = floor(n);
temp = n - temp;
n-=temp;
n/=100.0;
}
return n;
}
//two functions that work together to apply DCT to a 8x8 block
void a_check(int u,int v, double *Cu, double *Cv)
{
if (u == 0) *Cu = sqrt(1.0 / 8.0); else *Cu = sqrt(2.0 / 8.0);
if (v == 0) *Cv = sqrt(1.0 / 8.0); else *Cv = sqrt(2.0 / 8.0);
}
void applyDCT(double *DCTMatrix, double *Matrix,int x, int y) //arguments: DCTMatrix = matrix where DCT values go, Matrix = input Matrix which needs to be processed
{
double Cu=0,Cv=0,cos_x=0,cos_y=0, DCT_value = 0;
for(int u=0;u<8;u++)
{
for(int v=0;v<8;v++)
{
a_check(u,v,&Cu,&Cv);
cos_x = cos( (3.14/8) * (double)u * ( (double) ( (x+u) + (1.0/2.0))));
cos_y = cos( (3.14/8) * (double)v * ( (double) ( (y+v) +(1.0/2.0))));
DCT_value = Cu * Cv * Matrix[u*8 + v] * cos_x * cos_y;
DCT_value = rndup(DCT_value);
DCTMatrix[u*8 + v] = DCT_value;
}
}
}