📄 dct_jpeg.cpp
字号:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MX 8
const double PI=asin(1)*2.0;
int *Addx=NULL;
struct MAT
{
int row,col;
double **pn;
};
/*************************/
bool MulMatrix(MAT a,MAT b,MAT &c)// a*b=c
{
int i=0,j=0,k=0;
double ex;
if(a.col!=b.row){puts("error mul matrix (a,b): row != col");return false;}
for(i=0;i<a.row;i++){
for(j=0;j<b.col;j++){
ex=0.0;
for(k=0;k<a.col;k++){
ex += a.pn[i][k] * b.pn[k][j];
}
c.pn[i][j]=ex; //printf("%8.3lf ",ex);
}
//puts("");
}
return true;
}
/*************************/
bool PutMatrix(MAT a)
{
int i,j;
for(i=0;i<a.row;i++){
for(j=0;j<a.col;j++){
printf("%9.2lf",a.pn[i][j]);
}
printf("\n");
}
return true;
}
/*************************/
bool GenMatrixA(MAT &a,MAT &at,int M)
{
int i,j;
double tmp=0.0;
a.row=M;a.col=M;
a.pn=(double**)malloc(sizeof(double*)*(a.row));
at.row=M;at.col=M;
at.pn=(double**)malloc(sizeof(double*)*(at.row));
for(i=0;i<at.row;i++)at.pn[i]=(double*)malloc(sizeof(double)*(at.col));
for(i=0;i<a.row;i++){
a.pn[i]=(double*)malloc(sizeof(double)*(a.col));
for(j=0;j<a.col;j++){
if(i==0)a.pn[i][j]=sqrt(0.5);
else a.pn[i][j]=1;
a.pn[i][j]*=sqrt(2.0/M);
tmp=PI;
tmp=tmp * (2.0*j+1) * i / (2.0 * M);
tmp=cos(tmp);
a.pn[i][j]*=tmp;
at.pn[j][i]=a.pn[i][j];
}
}
return true;
}
bool GenAddx(int n)
{
int i,a=0,n2=n+n;
Addx=(int*)malloc(sizeof(int)*(n2+1));
Addx[0]=n2;
for(i=1;i<=n;i++){
Addx[i]=i*(i-1)/2;
}
for(;i<=n2;i++){
Addx[i]=Addx[i-1]+n2-i+1;
}
return 0;
}
bool ScanZZ(MAT a)
{
int i,j,r,c,t;
int Num=a.row * a.col;
for(i=1;i<=Num;i++){
for(j=Addx[0];j>=2;j--)if(Addx[j]<=i)break;
r=i-Addx[j];
c=j+(i-Addx[j]);
if(j%2){
t=r;r=c;c=t;
}//odd number
}
return true;
}
///////////////////////////
int main()
{
int i,j;
FILE *fp;
MAT a,at,b,c;
fp=fopen("data.txt","rb");
if(!fp){puts("Error open file");exit(0);}
GenAddx(MX);
for(i=0;i<=Addx[0];i++)printf("%d ",Addx[i]);puts("");
GenMatrixA(a,at,MX);
//PutMatrix(a);puts("");
fscanf(fp,"%d%d",&b.row,&b.col);
b.pn=(double**)malloc(sizeof(double*)*(b.row));
for(i=0;i<b.row;i++){
b.pn[i]=(double*)malloc(sizeof(double)*(b.col));
for(j=0;j<b.col;j++){
fscanf(fp,"%lf",&b.pn[i][j]);
}
}
//PutMatrix(b);puts("");
c.row=a.row;
c.col=b.col;
c.pn=(double**)malloc(sizeof(double*)*(c.row));
for(i=0;i<c.row;i++)c.pn[i]=(double*)malloc(sizeof(double)*(c.col));
MulMatrix(a,b,c);
MulMatrix(c,at,b);
PutMatrix(b);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -