📄 ezwcode.cpp
字号:
{
for(k=i*2;k<2*i+(int)pow(2,ElgFactor);k++)
for(l=j*2;l<2*j+(int)pow(2,ElgFactor);l++)//依次寻找子孙节点
if(abs(EzwIn[k*datwidth+l])>=(int)pow(2,thr))
return 0;
i*=2;
j*=2;
ElgFactor+=1;
}
return 1;
}
void CEzwCode::FirstFactor(int datheight,int datwidth,int *Ez)
{ //寻找最大量化阈值
int max=0;
for(int i=0;i<datheight;i++)
for(int j=0;j<datwidth;j++)
if(abs(Ez[i*datwidth+j])>max)
{
max=abs(Ez[i*datwidth+j]);
TRACE("(%d %d)",Ez[i*datwidth+j],max);
}
TreFstFtr=int(ceil(log10(max)/log10(2))-1);
//找到最大值max,log2(max)向上取整-1;
TRACE("the max is %d ,the TreFstFtr is %d\n",max,TreFstFtr);
}
int CEzwCode::CalSV( int nowthr,int i,int j,int datheight,int datwidth)
{
//计算附表值
//三个门限值,此时阈值的1 1.5 2倍
int low=(int)(pow(2,nowthr));
int middle=(int)(pow(2,nowthr)*1.5);
int high=(int)(pow(2,nowthr)*2);
while(high<=(int)pow(2,TreFstFtr+1))//不大于最高阈值
{
if((abs(EzwBigIn[i*datwidth+j])>=low)&&(abs(EzwBigIn[i*datwidth+j])<high))
if(abs(EzwBigIn[i*datwidth+j])>=middle)
return 1;//大于中门限
else
return 0;//小于中门限
low+=int(pow(2,nowthr));//实际价值有待研究
//TRACE("I do not thick I can get here!\n");
middle+=int(pow(2,nowthr));
high+=int(pow(2,nowthr));
}
return -1;//处错
}
bool CEzwCode::PutBit(char bit,CFile *pf)
{
//以一个字节为单位输出数据
//开始掩模 mask=128=1000 0000
//开始output_byte=0 =0000 0000
//TRACE("the mask is %d",int(mask));
if (bit=='1')
{
output_byte |= mask;//位或
ones++;
}
else
zeroes++;
//ones zeros 用于计算压缩率
mask >>= 1;//右移一位
if (mask==0)
{
//每8位输出一次,并把mask和output_byte初始化
pf->Write(&output_byte,sizeof(output_byte));
//fwrite(&output_byte,sizeof(output_byte),1,fp);
output_byte = 0;
mask = 0x80;
}
return 1;
}
////////////////////
int CEzwCode::BitToEzwDV(CFile *pf)
{
//Reads a code from the input stream,output DV
switch (GetBit(pf)) //need to change to common style of cal
{
case '1':
switch (GetBit(pf))
{
case '0':
return (1);
case '1':
switch (GetBit(pf))
{
case '0':
return (2);
case '1':
return (3);
}
}
break;
case '0':
return (0);
break;
}
//You should never get here.
return 1;
//出错
}
int CEzwCode::BitToEzwSV(CFile *pf)
{
//Reads a code from the input stream,output SV
switch (GetBit(pf)) //need to change to common style of cal
{
case '1':
return (1);
break;
case '0':
return (0);
break;
}
//You should never get here.
return 0;
}
char CEzwCode::GetBit(CFile *pf)
{
//以一个字节为单位读入数据
unsigned char bit;
if (mask==0)
{
//每8位读一次
pf->Read (&input_byte,sizeof(input_byte));
mask = 0x80;//128=1000 0000
}
if ((input_byte&mask)==0)
{
bit = '0';
zeroes++;
}
else
{
bit = '1';
ones++;
}
mask >>= 1;
return (bit);
}
bool CEzwCode::HuffmanDecode(int minthr,int datheight,int datwidth)
{
int i,j,thr,Thresholdnum;
//int EzwDVOfPro[4];
bool *EzwSVFlag=(bool *)HeapAlloc(GetProcessHeap(),0,datheight*datwidth*sizeof(bool));
//EzwSVFlag=new bool [datheight*datwidth];//判断是否存在附表的标志位数组
input_byte = 0;
mask = 0;
ones=0;
zeroes=0;
CFile *pFile=new CFile();
LPCTSTR lpszFilter="EZW Files(*.ezw)|*.ezw|任何文件|*.*||";
CFileDialog *SaveDlg=new CFileDialog (TRUE, lpszFilter, "LENA",OFN_HIDEREADONLY|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR,lpszFilter );
if(SaveDlg->DoModal()==IDOK)
pFile->Open(SaveDlg->GetPathName(),CFile::modeRead|CFile::typeBinary,NULL);
else
{
::AfxMessageBox("cannot open file to read !");
return 0;
}
//if((fp=fopen("Huffman1.ezw","rb"))!=NULL)
{
//读头文件
pFile->Read (&TreFstFtr,sizeof(TreFstFtr));//读最大量化阈值
Thresholdnum=TreFstFtr-minthr+1;
BufDv=(int *)HeapAlloc(GetProcessHeap(),0,Thresholdnum*datheight*datwidth*sizeof(int));
//生成对BufDv的二维数组索引:
EzwDV=new int*[Thresholdnum];
for(i=0;i<Thresholdnum;i++)
EzwDV[i]=BufDv+i*datheight*datwidth;
BufSv=(int *)HeapAlloc(GetProcessHeap(),0,Thresholdnum*datheight*datwidth*sizeof(int));
//生成对bufSv的二维数组索引:
EzwSV=new int*[Thresholdnum];
for(i=0;i<Thresholdnum;i++)
EzwSV[i]=BufSv+i*datheight*datwidth;
//EzwDV=new int [Thresholdnum][datheight*datwidth];//为主附表分配空间
//EzwSV=new int [Thresholdnum][datheight*datwidth];
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
{
//初始化主附表数组
EzwSVFlag[i*datwidth+j]=0;
for(thr=TreFstFtr-minthr;thr>=0;thr--)
{
EzwDV[thr][i*datwidth+j]=NUL;
EzwSV[thr][i*datwidth+j]=-1;
}
}
//for(i=0;i<4;i++)
//读入概率分配表,整数表示,参照头文件中的定义
pFile->Read (EzwDVOfPro,4*sizeof(int));
for(thr=TreFstFtr-minthr;thr>=0;thr--)
{
//读主表
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
if(!PrtsIsZTR(thr,i,j,datheight,datwidth))
//如果不是零树的子孙
{
EzwDV[thr][i*datwidth+j]=EzwDVOfPro[BitToEzwDV(pFile)];//读取主表数据
if((EzwDV[thr][i*datwidth+j]==POS)||(EzwDV[thr][i*datwidth+j]==NEG))
EzwSVFlag[i*datwidth+j]=1;//重要数据对应附表
}
//读附表
if((thr!=0)||(minthr!=0))
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
if(EzwSVFlag[i*datwidth+j]==1)
EzwSV[thr][i*datwidth+j]=BitToEzwSV(pFile);
}
HeapFree(GetProcessHeap(),0,EzwSVFlag);
//delete []EzwSVFlag;
}
//else
// ::AfxMessageBox("cannot find Huffman.ezw!");
//fclose(fp);
//calculate compression and bpp
char temp[100];
float compression,bpp;
compression=(float)datheight*datwidth*8/(ones+zeroes);
bpp=(float)(ones+zeroes)/datheight/datwidth;
sprintf(temp,"compression:%2.2f,bpp: %1.2f",compression,bpp);
::AfxMessageBox(temp);
delete SaveDlg;
pFile->Close();
delete pFile;
return 1;
}
bool CEzwCode::EzwInverseQualitition(int minthr,int datheight,int datwidth)
{
//零树反量化
int i,j,thr,*FlagOut,*EzwOut;
FlagOut=(int *)HeapAlloc(GetProcessHeap(),0,datheight*datwidth*sizeof(int));
EzwOut=(int *)HeapAlloc(GetProcessHeap(),0,datheight*datwidth*sizeof(int));
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
{
FlagOut[i*datwidth+j]=-1;
EzwOut[i*datwidth+j]=0;
}
for(thr=TreFstFtr-minthr;thr>=0;thr--)
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
if((thr!=0)||(minthr!=0))
if((EzwSV[thr][i*datwidth+j]==1)||(EzwSV[thr][i*datwidth+j]==0))
{
if(FlagOut[i*datwidth+j]==-1)//第一次赋初值
FlagOut[i*datwidth+j]=EzwSV[thr][i*datwidth+j];
else
{
//第一次以后累加
FlagOut[i*datwidth+j]=FlagOut[i*datwidth+j]*2+2+EzwSV[thr][i*datwidth+j];
//TRACE("I think I can not get here.\n");
}
EzwOut[i*datwidth+j]=(int)(pow(2,thr+minthr)*(1.25+FlagOut[i*datwidth+j]*0.5));
}
//delete []FlagOut;
HeapFree(GetProcessHeap(),0,FlagOut);
delete [] EzwSV;
HeapFree(GetProcessHeap(),0,BufSv);
for(thr=TreFstFtr-minthr;thr>=0;thr--)
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
{
if((thr==0)&&(minthr==0))
if((EzwDV[thr][i*datwidth+j]==POS)||(EzwDV[thr][i*datwidth+j]==NEG))
EzwOut[i*datwidth+j]=1;
if(EzwDV[thr][i*datwidth+j]==NEG)
EzwOut[i*datwidth+j]*=-1;
}
delete [] EzwDV;
HeapFree(GetProcessHeap(),0,BufDv);
//Write EzwOut to File
CFile *SaveFile=new CFile();
LPCTSTR lpszFilter="Disc Wavelet Trans Files(*.dwt)|*.dwt|任何文件|*.*||";
CFileDialog *SaveDlg=new CFileDialog (FALSE, lpszFilter, "LENA_IQ",OFN_HIDEREADONLY|OFN_PATHMUSTEXIST|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR,lpszFilter );
if(SaveDlg->DoModal()==IDOK)
SaveFile->Open(SaveDlg->GetPathName(),CFile::modeCreate |CFile::modeWrite|CFile::typeBinary,NULL);
else
return 0;
for(i=0;i<datheight;i++)
for(j=0;j<datwidth;j++)
{
SaveFile->Write(&EzwOut[i*datwidth+j],sizeof(int));
//TRACE("out[%d][%d]=%d\n",i,j,EzwOut[i*datwidth+j]);
}
delete SaveDlg;
SaveFile->Close();
delete SaveFile;
//delete []EzwOut;
HeapFree(GetProcessHeap(),0,EzwOut);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -