#include<stdio.h>
#include<math.h>
#include<io.h>
#include<stdlib.h>
#include<malloc.h>
#include<memory.h>
#include<windows.h>
#include<time.h>
const int Height=9092,Width=8835;
int GetSize(int Width,int Height)
{
int n,Size,max;
float ntemp;
max=(Height>Width)?Height:Width;
ntemp=log10(max)/log10(2.0);
n=(int) ntemp;
if(n==ntemp) Size=(int)(pow(2,n));
else Size=(int)(pow(2,n+1));
return Size;
}
float GetData(float **image,int i,int j)
{
if((i>=Height)||(j>=Width)) return 0.0;
else return (image[i][j]);
}
float hh(int i,int m,float h1[30])
{
return h1[i+m];
}
float gg(int i,int m,float h1[30])
{
char flag;
if(i%2)
flag=-1;
else
flag=1;
return (hh(-1*i+1,m,h1)*flag);
}
void hhh1(int level,int SIZE,int filterlen,float *hhh,float h1[30])
{
int len,m,i;
float temp;
int sign;
len=SIZE>>level;
m=filterlen/2;
for(sign=0;sign<len;sign++)
{
temp=0;
for(i=(-1*m+1);i<=m;i++)
{
if(((sign-i)%len)==0)
temp+=hh(i-1,m,h1);
}
hhh[sign]=temp;
}
}
void ggg1(int level,int SIZE,int filterlen,float *ggg,float h1[30])
{
int len,m,i;
float temp;
int sign;
len=SIZE>>level;
m=filterlen/2;
for(sign=0;sign<len;sign++)
{
temp=0;
for(i=(-1*m+3);i<=(m+2);i++)
{
if(((sign-i)%len)==0)
temp+=gg(i-1,m,h1);
}
ggg[sign]=temp;
}
}
void codehr(float **image,float *temp,float *hhh,int flen,int start,int len,int row,int filterlen)
{
int i,i2,m,k,len1;
float temp1;
m=filterlen/2;
len1=len>>1;
for(i=0;i<len1;i++)
{
i2=2*i;
temp[i+start]=0;
if(len>flen)
{
for(k=i2-len;k<=i2-len+m;k++)
{
if((k>=0)&&(k<len))
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,row,k+start);
}
for(k=i2-m+1;k<=i2+m;k++)
{
if((k>=0)&&(k<len))
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,row,k+start);
}
for(k=i2+len-m+1;k<=i2+len;k++)
{
if((k>=0)&&(k<len))
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,row,k+start);
}
}
else{
for(k=0;k<len;k++)
{
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,row,k+start);
}
}
}
}
void codegr(float **image,float *temp,float *ggg,int flen,int start,int len,int row,int filterlen)
{
int i,i2,m,k,len1;
float temp1;
len1=len>>1;
m=flen/2;
for(i=0;i<len1;i++)
{
i2=2*i;
temp[i+start+len1]=0;
if(len>flen)
{
for(k=i2-len;k<=i2-len+m+2;k++)
{
if((k>=0)&&(k<len))
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,row,k+start);
}
for(k=i2-m+3;k<=i2+m+2;k++)
{
if((k>=0)&&(k<len))
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,row,k+start);
}
for(k=i2+len-m+3;k<=i2+len;k++)
{
if((k>=0)&&(k<len))
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,row,k+start);
}
}
else{
for(k=0;k<len;k++)
{
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,row,k+start);
}
}
}
}
void codehc(float **image,float *temp,float *hhh,int flen,int start,int len,int col,int filterlen)
{
int i,i2,m,k,len1;
float temp1;
m=filterlen/2;
len1=len>>1;
for(i=0;i<len1;i++)
{
i2=2*i;
temp[i+start]=0;
if(len>flen)
{
for(k=i2-len;k<=i2-len+m;k++)
{
if((k>=0)&&(k<len))
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,k+start,col);
}
for(k=i2-m+1;k<=i2+m;k++)
{
if((k>=0)&&(k<len))
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,k+start,col);
}
for(k=i2+len-m+1;k<=i2+len;k++)
{
if((k>=0)&&(k<len))
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,k+start,col);
}
}
else{
for(k=0;k<len;k++)
{
if((temp1=hhh[(k-i2+len)%len])!=0)
temp[i+start]+=temp1*GetData(image,k+start,col);
}
}
}
}
void codegc(float **image,float *temp,float *ggg,int flen,int start,int len,int col,int filterlen)
{
int i,i2,m,k,len1;
float temp1;
len1=len>>1;
m=flen/2;
for(i=0;i<len1;i++)
{
i2=2*i;
temp[i+start+len1]=0;
if(len>flen)
{
for(k=i2-len;k<=i2-len+m+2;k++)
{
if((k>=0)&&(k<len))
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,k+start,col);
}
for(k=i2-m+3;k<=i2+m+2;k++)
{
if((k>=0)&&(k<len))
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,k+start,col);
}
for(k=i2+len-m+3;k<=i2+len;k++)
{
if((k>=0)&&(k<len))
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,k+start,col);
}
}
else{
for(k=0;k<len;k++)
{
if((temp1=ggg[(k-i2+len)%len])!=0)
temp[i+start+len1]+=temp1*GetData(image,k+start,col);
}
}
}
}
void stager(int row,int stage,int numx,int SIZE,int filterlen,float **image,float *temp,float *hhh,float *ggg)
{
int i,start,end;
int flen,len;
flen=filterlen;
len=SIZE>>stage;
start=len*numx;
end=start+len;
codehr(image,temp,hhh,flen,start,len,row,filterlen);
codegr(image,temp,ggg,flen,start,len,row,filterlen);
for(i=start;(i<end)&&(i<Width);i++)
image[row][i]=temp[i];
}
void stagec(int col,int stage,int numx,int SIZE,int filterlen,float **image,float *temp,float *hhh,float *ggg)
{
int i,start,end;
int flen,len;
flen=filterlen;
len=SIZE>>stage;
start=len*numx;
end=start+len;
codehc(image,temp,hhh,flen,start,len,col,filterlen);
codegc(image,temp,ggg,flen,start,len,col,filterlen);
for(i=start;(i<end)&&(i<Height);i++)
image[i][col]=temp[i];
}
void comp2(int stage,int SIZE,int filterlen,float **image,float *temp,float *hhh,float *ggg,float h1[30])
{
int row,col;
hhh1(stage,SIZE,filterlen,hhh,h1);
ggg1(stage,SIZE,filterlen,ggg,h1);
for(row=0;(row<(SIZE>>stage))&&(row<Height);row++)
stager(row,stage,0,SIZE,filterlen,image,temp,hhh,ggg);
for(col=0;(col<(SIZE>>stage))&&(col<Width);col++)
stagec(col,stage,0,SIZE,filterlen,image,temp,hhh,ggg);
}
void decomp(int STAGE,int SIZE,int filterlen,float **image,float *temp,float *hhh,float *ggg,float h1[30])
{
int stage;
for(stage=0;stage<(STAGE-1);stage++)
{
comp2(stage,SIZE,filterlen,image,temp,hhh,ggg,h1);
}
}
int rond(int stage,int p,int q)
{
int n,n1;
n=2/p;
if(stage==7)
return q;
else {
n1=rond(stage+1,p,q);
n1>>=n;
if(n1<=1)
n1=1;
return n1;
}
}
void quan(int stage,int SIZE,int numx,int numy,int p,int q,float **image) //量化某个小波细节版本
{
int len,startx,endx,starty,endy;
int row,col;