最近研究jpeg图像的编码解码,涉及到DCT即离散余弦变换,参考CSDN上一位calcular博客的文章,进行简易修改。
-
#include<iostream>
-
#include<math.h>
-
using namespace std;
-
#define PI 3.14159265354
-
-
#define DCT_SIZE 8
-
-
float DCT_Mat[DCT_SIZE][DCT_SIZE];
-
-
float DctMap[DCT_SIZE][DCT_SIZE]=
-
{
-
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,56,47,48,
-
49,50,51,52,53,54,55,56,
-
57,58,59,60,61,62,63,64
-
-
};
-
float DCT_COS[DCT_SIZE][DCT_SIZE] = {
-
0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f, 0.35355338f,
-
0.49039263f, 0.41573480f, 0.27778509f, 0.09754512f, -0.09754516f, -0.27778518f, -0.41573483f, -0.49039266f,
-
0.46193978f, 0.19134171f, -0.19134176f, -0.46193978f, -0.46193978f, -0.19134156f, 0.19134180f, 0.46193978f,
-
0.41573480f, -0.09754516f, -0.49039266f, -0.27778500f, 0.27778521f, 0.49039263f, 0.09754504f, -0.41573489f,
-
0.35355338f, -0.35355338f, -0.35355332f, 0.35355350f, 0.35355338f, -0.35355362f, -0.35355327f, 0.35355341f,
-
0.27778509f, -0.49039266f, 0.09754521f, 0.41573468f, -0.41573489f, -0.09754511f, 0.49039266f, -0.27778542f,
-
0.19134171f, -0.46193978f, 0.46193978f, -0.19134195f, -0.19134149f, 0.46193966f, -0.46193987f, 0.19134195f,
-
0.09754512f, -0.27778500f, 0.41573468f, -0.49039260f, 0.49039271f, -0.41573480f, 0.27778557f, -0.09754577f
-
};
-
float IDCT_COS[DCT_SIZE][DCT_SIZE] = {
-
0.35355338f, 0.49039263f, 0.46193978f, 0.41573480f, 0.35355338f, 0.27778509f, 0.19134171f, 0.09754512f,
-
0.35355338f, 0.41573480f, 0.19134171f, -0.09754516f, -0.35355338f, -0.49039266f, -0.46193978f, -0.27778500f,
-
0.35355338f, 0.27778509f, -0.19134176f, -0.49039266f, -0.35355332f, 0.09754521f, 0.46193978f, 0.41573468f,
-
0.35355338f, 0.09754512f, -0.46193978f, -0.27778500f, 0.35355350f, 0.41573468f, -0.19134195f, -0.49039260f,
-
0.35355338f, -0.09754516f, -0.46193978f, 0.27778521f, 0.35355338f, -0.41573489f, -0.19134149f, 0.49039271f,
-
0.35355338f, -0.27778518f, -0.19134156f, 0.49039263f, -0.35355362f, -0.09754511f, 0.46193966f, -0.41573480f,
-
0.35355338f, -0.41573483f, 0.19134180f, 0.09754504f, -0.35355327f, 0.49039266f, -0.46193987f, 0.27778557f,
-
0.35355338f, -0.49039266f, 0.46193978f, -0.41573489f, 0.35355341f, -0.27778542f, 0.19134195f, -0.09754577f
-
};
-
float DctMapTmp[DCT_SIZE][DCT_SIZE];
-
-
void InitDctMat()
-
{
-
int i,j,t;
-
for(i=0;i<DCT_SIZE;i++) DCT_Mat[0][i]=sqrt(2.0/DCT_SIZE)*sqrt(1.0/2);
-
for(i=1;i<DCT_SIZE;i++)
-
{
-
t=0;
-
for(j=0;j<DCT_SIZE;j++)
-
{
-
DCT_Mat[i][j]=sqrt(2.0/DCT_SIZE)*cos(PI/(2.0*DCT_SIZE)*(i+t));t+=2*i;
-
}
-
}
-
cout<<"=========initDctMat========"<<endl;
-
for(int i=0;i<DCT_SIZE;i++)
-
{
-
for(int j=0;j<DCT_SIZE;j++)
-
{
-
cout<<DCT_Mat[i][j]<<" ";
-
}
-
cout<<endl;
-
}
-
cout<<"=========initDctMat========"<<endl;
-
}
-
-
void DCT()
-
{
-
float t=0;
-
int i,j,k;
-
for(i=0;i<DCT_SIZE;i++){
-
for(j=0;j<DCT_SIZE;j++){
-
t=0;
-
for(k=0;k<DCT_SIZE;k++)
-
//t+=DCT_Mat[i][k]*DctMap[k][j];
-
t+=DCT_COS[i][k]*DctMap[k][j];
-
DctMapTmp[i][j]=t;
-
}
-
}
-
for(i=0;i<DCT_SIZE;i++){
-
for(j=0;j<DCT_SIZE;j++){
-
t=0;
-
for(k=0;k<DCT_SIZE;k++)
-
//t+=DctMapTmp[i][k]*DCT_Mat[j][k];
-
t+=DctMapTmp[i][k]*DCT_COS[j][k];
-
DctMap[i][j]=t;
-
}
-
}
-
-
cout<<"=========DCT========"<<endl;
-
for(int i=0;i<DCT_SIZE;i++)
-
{
-
for(int j=0;j<DCT_SIZE;j++)
-
{
-
cout<<DctMap[i][j]<<" ";
-
}
-
cout<<endl;
-
}
-
cout<<"=========DCT========"<<endl;
-
}
-
-
void IDCT()
-
{
-
float t=0;
-
int i,j,k;
-
for(i=0;i<DCT_SIZE;i++){
-
for(j=0;j<DCT_SIZE;j++){
-
t=0;
-
for(k=0;k<DCT_SIZE;k++)
-
//t+=DCT_Mat[k][i]*DctMap[k][j];
-
t+=IDCT_COS[i][k]*DctMap[k][j];
-
DctMapTmp[i][j]=t;
-
}
-
}
-
for(i=0;i<DCT_SIZE;i++){
-
for(j=0;j<DCT_SIZE;j++){
-
t=0;
-
for(k=0;k<DCT_SIZE;k++)
-
//t+=DctMapTmp[i][k]*DCT_Mat[k][j]
-
t+=DctMapTmp[i][k]*IDCT_COS[j][k];
-
DctMap[i][j]=(int)(t);
-
}
-
}
-
cout<<"=========IDCT========"<<endl;
-
for(int i=0;i<DCT_SIZE;i++)
-
{
-
for(int j=0;j<DCT_SIZE;j++)
-
{
-
cout<<DctMap[i][j]<<" ";
-
}
-
cout<<endl;
-
}
-
cout<<"=========IDCT========"<<endl;
-
}
-
-
int main()
-
{
-
//InitDctMat();
-
DCT();
-
IDCT();
-
-
}
阅读(255882) | 评论(0) | 转发(0) |