⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decompressdoc.cpp

📁 这是用提升小波变换与嵌入零树编码实现的256色灰度BMP图像压缩与解压缩的程序。
💻 CPP
📖 第 1 页 / 共 5 页
字号:
				   ofile<<endl;
			}
			ofile.close();
			ofile.open("d:\\fubiao1.txt");
			ofile<<"the size is"<<fubiao.GetSize()<<endl;
			for(tt=0;tt<fubiao.GetSize();tt++)
			{
				ofile<<fubiao.GetAt(tt);
			    if(((tt+1)%10)==0)
				    ofile<<"  ";
				if(((tt+1)%100)==0)
					ofile<<endl;
			}
			ofile.close();
            tempcoeff.Copy(zhycoeff);
			//下面接着是用无损编码把编码结果继续编码
			EZWEncode(T,zhubiao,fubiao);
			zhubiao.RemoveAll();
			fubiao.RemoveAll();
			zhycoeff.RemoveAll();
			zhubiao.SetSize(100,50);
            fubiao.SetSize(20,50);
			zhycoeff.SetSize(20,50);
		}
	    //释放内存
        for(k=0;k<pbi->bmiHeader.biHeight;k++)
		{
		   delete [] AllNode[k];
		}
	    delete [] AllNode;
	    finish=clock();
		duration=(double)(finish-start)/CLOCKS_PER_SEC;
}  //OnEZWEncode()



int CDecompressDoc::CheckZeroTree(CNode **AllNode, int x, int y, int T)
{
	int changeflag=0;
    int scale=AllNode[x][y].scale;
	while(scale>1)
	{
		if(AllNode[x][y].importflag==0 && abs(AllNode[x][y].coef)>=T)
		{
			changeflag=1;
			break;
		}
		else
		{
			
			if(AllNode[2*x][2*y].importflag==0 && abs(AllNode[2*x][2*y].coef)>=T)
			{   
			    changeflag=1;
				break;
			}
			else if(AllNode[2*x][2*y+1].importflag==0 && abs(AllNode[2*x][2*y+1].coef)>=T)
			{
			    changeflag=1;
				break;
			}
			else if(AllNode[2*x+1][2*y].importflag==0 && abs(AllNode[2*x+1][2*y].coef)>=T)
			{
				changeflag=1;
				break;
			}
			else if(AllNode[2*x+1][2*y+1].importflag==0 && abs(AllNode[2*x+1][2*y+1].coef)>=T)
			{
			    changeflag=1;
				break;
			}
			if(changeflag==1)
			break;
		}
		if(changeflag==0)
		{
			scale--;
			if(CheckZeroTree(AllNode,2*x,2*y,T)==1)
			{
				changeflag=1;
				break;
			}
			
		}
		if(changeflag==0)
		{
			scale--;
			if(CheckZeroTree(AllNode,2*x,2*y+1,T)==1)
			{
				changeflag=1;
				break;
			}
		}
		if(changeflag==0)
		{
			scale--;
			if(CheckZeroTree(AllNode,2*x+1,2*y,T)==1)
			{
				changeflag=1;
				break;
			}
		}
		if(changeflag==0)
		{
			scale--;
			if(CheckZeroTree(AllNode,2*x+1,2*y+1,T)==1)
			{
				changeflag=1;
				break;
			}
		}
	}
    return changeflag;
}

void CDecompressDoc::SetZeroTree(CNode **AllNode, int x, int y)
{
    while(AllNode[x][y].scale>1)
	{
		AllNode[x][y].flag=1;
		SetZeroTree(AllNode,2*x,2*y);
		AllNode[2*x][2*y].flag=1;
		SetZeroTree(AllNode,2*x,2*y+1);
		AllNode[2*x][2*y+1].flag=1;
	    SetZeroTree(AllNode,2*x+1,2*y);
		AllNode[2*x+1][2*y].flag=1;
        SetZeroTree(AllNode,2*x+1,2*y+1);
		AllNode[2*x+1][2*y+1].flag=1;
		break;
	}

}

void CDecompressDoc::ChangeMatrix()
{
	pbi->bmiHeader.biHeight=16;
	pbi->bmiHeader.biWidth=16;
	int Array[16][16]={{63,-34,49,10,7,13,-12,7,8,8,8,8,8,8,8,8}, {-31,23,14,-13,3,4,6,-1,8,8,8,8,8,8,8,8}, {15,14,3,-12,5,-7,3,9,8,8,8,8,8,8,8,8}, {-9,-7,-14,8,4,-2,3,2,8,8,8,8,8,8,8,8},{-5,9,-1,47,4,6,-2,2,8,8,8,8,8,8,8,8},{3,0,-3,2,3,-2,0,4,8,8,8,8,8,8,8,8},{2,-3,6,-4,3,6,3,6,8,8,8,8,8,8,8,8},{5,11,5,6,0,3,-4,4,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}
	,{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8},{8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}};

	for(int x=0;x<16;x++)
	{
		for(int y=0;y<16;y++)
			Coefficient[x][y]=Array[x][y];
	}


}


bool CDecompressDoc::LiangHuaQi(int coeff, int min, int max)
{
	int middle=(min+max)/2;
    if(abs(coeff)>=middle)
		return true;
	else 
		return false;
}



void CDecompressDoc::Order(CArray<int,int> &zhycoeff, int length,int min, int max)
{
    int index=0;
 	int iindex=0;
	int tempcoeff;
 	while(index<length)
 	{
 		if(abs(zhycoeff.GetAt(index))>=min && abs(zhycoeff.GetAt(index))<max)
 		{
 			if(abs(zhycoeff.GetAt(index))>=(min+max)/2)
 			    //index++;
				;
 		    else
 			{
 			    iindex=index;   //找到的当前的第一个比1.5T小的系数
 			    tempcoeff=zhycoeff.GetAt(iindex);
			    index++;  //从下一个系数开始找第一个比1.5T大的系数
				while(index<length)
 				{
 					if(abs(zhycoeff.GetAt(index))>=min && abs(zhycoeff.GetAt(index))<max)
 					{
 				        if(abs(zhycoeff.GetAt(index))<(min+max)/2)
 					       index++;
 			            else
 						{
                           zhycoeff.SetAtGrow(iindex,zhycoeff.GetAt(index));  //找到的两个位置交换系数,实现重排
 					       zhycoeff.SetAtGrow(index,tempcoeff);
 					       iindex=index;  //这时第一个比1.5T小的系数被放到了后面index处
 					       index++;
 						}//else
 					}//if
 				}//while
 			}//else
 		}//if
		index++;
 	}//while
}//Order

int CDecompressDoc::SecondScan(CNode **AllNode, int T, CArray<BYTE,BYTE> &zhubiao, CArray<BYTE,BYTE> &fubiao, CArray<int,int> &zhycoeff,int ScanFlag, int length)
{
	int linewidth=(pbi->bmiHeader.biWidth*pbi->bmiHeader.biBitCount+31)/32*4;
	int changeflag=0;
	int x,y;
	int zhubiaoIndex=0;
	int zhycoeffIndex=length;
	int fubiaoIndex=0;
	for(x=0;x<pbi->bmiHeader.biHeight;x++)
	{
		for(y=0;y<linewidth;y++)
			AllNode[x][y].flag=0;
	}
	//开始主扫描
	for(int iscale=scale+1;iscale>0;iscale--)
	{
		if(iscale==scale+1)
		{
			for(x=0;x<pbi->bmiHeader.biHeight>>scale;x++)
			{
				for(y=0;y<linewidth>>scale;y++)
				{
					if(AllNode[x][y].importflag==0)
					{
                        if(AllNode[x][y].flag==0)
						{
						    if(AllNode[x][y].coef>=T)
							{
					            zhubiao.SetAtGrow(zhubiaoIndex,'P');
								zhubiaoIndex++;
					            AllNode[x][y].importflag=1;  //该系数是正重要系数
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
				            else if(AllNode[x][y].coef<T && abs(AllNode[x][y].coef)>=T)
							{
					            zhubiao.SetAtGrow(zhubiaoIndex,'N');
								zhubiaoIndex++;
					            AllNode[x][y].importflag=1;  //该系数是负重要系数
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T)   //系数小于阈值的情况,
							{
							    int xchazhi=pbi->bmiHeader.biHeight>>scale;  //行差值
						        int ychazhi=linewidth>>scale;  //列差值
						        if(CheckZeroTree(AllNode,x,y+ychazhi,T) || CheckZeroTree(AllNode,x+xchazhi,y,T) || CheckZeroTree(AllNode,x+xchazhi,y+ychazhi,T))
								{
								     changeflag=1;
								}
						        if(changeflag==1)
								{
							        zhubiao.SetAtGrow(zhubiaoIndex,'Z'); //该系数是孤立零点
                                    zhubiaoIndex++;
								}
								else if(changeflag==0)
								{
							        zhubiao.SetAtGrow(zhubiaoIndex,'T');   //该系数是零树根,接着就要构造零树
								    zhubiaoIndex++;
									SetZeroTree(AllNode,x,y+ychazhi);//零树上的节点,标志设为1
							        SetZeroTree(AllNode,x+xchazhi,y);  
								    SetZeroTree(AllNode,x+xchazhi,y+ychazhi);
								}
							    changeflag=0;  
							} //else if
						}//if
					}//if
				}//for
			}//for
		}//if
        else 
		{
			int x1=0;
			int x2=pbi->bmiHeader.biHeight>>iscale;
			int y1=linewidth>>iscale;
			int y2=linewidth>>(iscale-1);
		    for(x=x1;x<x2;x++)
			{
	            for(y=y1;y<y2;y++)
				{
					if(AllNode[x][y].importflag==0)
					{
					    if(AllNode[x][y].flag==0)
						{
						    if(AllNode[x][y].coef>=T)  //正重要元素
							{
							    zhubiao.SetAtGrow(zhubiaoIndex,'P');
								zhubiaoIndex++;
							    AllNode[x][y].importflag=1;
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T && abs(AllNode[x][y].coef)>=T) //负重要元素
							{
							    zhubiao.SetAtGrow(zhubiaoIndex,'N');
								zhubiaoIndex++;
					            AllNode[x][y].importflag=1;
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T)
							{
							    if(AllNode[x][y].scale!=1)
								{
							        if(CheckZeroTree(AllNode,x,y,T))  //孤立零点
									{
								        zhubiao.SetAtGrow(zhubiaoIndex,'Z');
										zhubiaoIndex++;
									}
							        else
									{
								        zhubiao.SetAtGrow(zhubiaoIndex,'T');  //零树根
                                        zhubiaoIndex++;
										SetZeroTree(AllNode,x,y);  //其子孙节点的标志设置
									}
								}
							    else
								{
								    zhubiao.SetAtGrow(zhubiaoIndex,'Z');
									zhubiaoIndex++;
								}//else
							}  //else if
						}  //if
					}//if
				} //for
            }  //for	
			x1=pbi->bmiHeader.biHeight>>iscale;
			x2=pbi->bmiHeader.biHeight>>(iscale-1);
			y1=0;
			y2=linewidth>>iscale;
			for(x=x1;x<x2;x++)
			{
	            for(y=y1;y<y2;y++)
				{
					if(AllNode[x][y].importflag==0)
					{
					    if(AllNode[x][y].flag==0)
						{
						    if(AllNode[x][y].coef>=T)  //正重要元素
							{
							    zhubiao.SetAtGrow(zhubiaoIndex,'P');
								zhubiaoIndex++;
							    AllNode[x][y].importflag=1;
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T && abs(AllNode[x][y].coef)>=T) //负重要元素
							{
							    zhubiao.SetAtGrow(zhubiaoIndex,'N');
								zhubiaoIndex++;
					            AllNode[x][y].importflag=1;
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T)
							{
							    if(AllNode[x][y].scale!=1)
								{
							         if(CheckZeroTree(AllNode,x,y,T))  //孤立零点
									 {
								          zhubiao.SetAtGrow(zhubiaoIndex,'Z');
										  zhubiaoIndex++;
									 }
							         else
									 {
								          zhubiao.SetAtGrow(zhubiaoIndex,'T');  //零树根
										  zhubiaoIndex++;
								          SetZeroTree(AllNode,x,y);  //其子孙节点的标志设置
									 }
								}
							    else
								{
								     zhubiao.SetAtGrow(zhubiaoIndex,'Z');
									 zhubiaoIndex++;
								}//else
							}//else if
						}//if
					}//if
				}//for
            } //for	
			x1=pbi->bmiHeader.biHeight>>iscale;
            x2=pbi->bmiHeader.biHeight>>(iscale-1);
			y1=linewidth>>iscale;
			y2=linewidth>>(iscale-1);
			for(x=x1;x<x2;x++)
			{
	            for(y=y1;y<y2;y++)
				{
					if(AllNode[x][y].importflag==0)
					{
					    if(AllNode[x][y].flag==0)
						{
						    if(AllNode[x][y].coef>=T)  //正重要元素
							{
							    zhubiao.SetAtGrow(zhubiaoIndex,'P');
								zhubiaoIndex++;
							    AllNode[x][y].importflag=1;
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T && abs(AllNode[x][y].coef)>=T) //负重要元素
							{
							    zhubiao.SetAtGrow(zhubiaoIndex,'N');
								zhubiaoIndex++;
					            AllNode[x][y].importflag=1;
							    zhycoeff.SetAtGrow(zhycoeffIndex,AllNode[x][y].coef);
								zhycoeffIndex++;
							}
						    else if(AllNode[x][y].coef<T)
							{
							    if(AllNode[x][y].scale!=1)
								{
							        if(CheckZeroTree(AllNode,x,y,T))  //孤立零点
									{
							            zhubiao.SetAtGrow(zhubiaoIndex,'Z');
										zhubiaoIndex++;
									}
							        else
									{
							 	        zhubiao.SetAtGrow(zhubiaoIndex,'T');  //零树根
										zhubiaoIndex++;
								        SetZeroTree(AllNode,x,y);  //其子孙节点的标志设置
									}
								}
							    else
								{
								    zhubiao.SetAtGrow(zhubiaoIndex,'Z');
									zhubiaoIndex++;
								}//else
							}  //else if
						} //if
					}//if
				} //for
            } //for	
		} //else
	}  //for
	//开始辅扫描
	int time=1; 
	int f=1;
	while(f<ScanFlag)
	{
		time=1+time*2;
		f++;
	}
	int TT=time;  //TT用于后面的系数表重排
	int fazhi;  //每区间的左值
	int z=0;//重要系数表下标
    while(time>=1)
	{
		//z=0;
        fazhi=time*T;
	    while(z<zhycoeffIndex)
		{
			if(abs(zhycoeff.GetAt(z))>=fazhi && abs(zhycoeff.GetAt(z))<(fazhi+T))
			{
			   if(LiangHuaQi(zhycoeff.GetAt(z),fazhi,fazhi+T))
			   {
				   //fubiao.SetAtGrow(fubiaoIndex,88);  //88代表1
				   fubiao.SetAtGrow(fubiaoIndex,'1');
				   fubiaoIndex++;
				   z++;
			   }
			   else
			   {
				   //fubiao.SetAtGrow(fubiaoIndex,89);  //89代表0
				   fubiao.SetAtGrow(fubiaoIndex,'0');
				   fubiaoIndex++;
				   z++;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -