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

📄 f_encode.cpp

📁 基于块方向的图像无损压缩代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                
    if (bOpen==0)
    {
		ErrorSet = TRUE;
		return ;
	}

	BITMAPFILEHEADER bHead;
	BITMAPINFOHEADER bi;
//	RGBQUAD bmiColors[256];

	//read the header of the BITMAP file
	cFile.Read(&bHead,sizeof(BITMAPFILEHEADER));
	cFile.Read(&bi,sizeof(BITMAPINFOHEADER));
//	cFile.Read(bmiColors,4*256);
	cFile.Seek(4*256,CFile::current);

	wide=IMAGE_WIDE=bi.biWidth;
	deep=IMAGE_DEEP=bi.biHeight;

	//ALLOCATE THE WORK BUFFER
	v2=(long)wide*deep;

	if((pc = new unsigned char [v2])==NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }       

	if((buff=new float [v2]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }

//	cFile.Seek(1078,CFile::begin);
	//read the data stream of the file
	if(wide%4==0)
		cFile.ReadHuge(pc,v2);
	else
	{
		long temp=(wide+3)/4*4-wide,temp1;
		temp1=0;
		for(i=0;i<deep;i++)
		{

			cFile.ReadHuge(pc+temp1,wide);
			cFile.Seek(temp,CFile::current);
			temp1+=wide;
		}
	}
	cFile.Close();


	// calculate the mean
                   
    for(i=0;i<v2;i++) 
		buff[i]=(float)pc[i];

	M=(float)CalculateMean(buff,wide,deep); 

	for(i=0;i<v2;i++) 
		buff[i]=(float)buff[i]-M; 

	//calculate the  max ABS
	float MM;
	MM=Calculate_BAS_MAX(buff,wide,deep);
	R=(float)(MM/128.0);
	
	for(i=0;i<v2;i++) 
		*(buff+i)=(*(buff+i))/R; 
	
	delete pc;

	// wavelet the image
	long xlength,ylength;
	xlength=((wide+1)/2+1)/2;
	ylength=((deep+1)/2+1)/2;

	if((im1=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
	}           

	if((im2=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;                         
     }          

	if((im3=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");		
		ErrorSet = TRUE;
		return ;
    }          

	if((im4=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im5=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im6=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im7=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im8=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im9=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }                                

	if((im10=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }               

	if((im11=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }           

	if((im12=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;                         
    }          

	if((im13=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im14=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }          

	if((im15=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("Globl lock error!");
		ErrorSet = TRUE;
		return ;
    }          


	if((im16=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }               

	float * buff1;
	float * buff2;
	float * buff3; 

	if((buff1=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }               
  
	if((buff2=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }               
              
	if((buff3=new float [xlength*ylength]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }               

	/****(0-50),51-64*******/
	long xSubbandLength[17],ySubbandLength[17];

	long xlen[17],ylen[17];

	CalculateSubbandLength_8(wide,deep,xSubbandLength,ySubbandLength);


	changelength_8(xlen,xSubbandLength,1,2,3,4,6,5,8,7,11,12,9,10,16,15,14,13);
	changelength_8(ylen,ySubbandLength,1,2,3,4,6,5,8,7,11,12,9,10,16,15,14,13);
	
	W8(buff,wide,deep,buff1,buff2,buff3,im4,im6,im5,im8,im7,im11,im12,im9,im10,im16,im15,im14,im13,h,g);	
	WR_FILE(buff,buff1,buff2,buff3,im4,im5,im6,im7,im8,im9,im10,im11,im12,im13,im14,im15,im16,xlen,ylen);

	///***35-50******/
	xlength=((wide+1)/2+1)/2;
	ylength=((deep+1)/2)/2;

	CalculateSubbandLength_8(xlength,ylength,xSubbandLength,ySubbandLength);

	changelength_8(xlen,xSubbandLength,11,12,9,10,16,15,14,13,1,2,3,4,6,5,8,7);
	changelength_8(ylen,ySubbandLength,11,12,9,10,16,15,14,13,1,2,3,4,6,5,8,7);
	
	W8(buff3,xlength,ylength,im11,im12,im9,im10,im16,im15,im14,im13,im1,im2,im3,im4,im6,im5,im8,im7,h,g);

	WR_FILE(buff3,im1,im2,im3,im4,im5,im6,im7,im8,im9,im10,im11,im12,im13,im14,im15,im16,xlen,ylen);

	long start,length;
	
	start=((wide+1)/2)*(((deep+1)/2+1)/2);
	length=xlength*ylength;
	
	for(i=0;i<length;i++)   
	   *(buff+i+start)=*(buff3+i);   
	
	/////***19-34*****/
	xlength=((wide+1)/2)/2;
	ylength=((deep+1)/2+1)/2;

	CalculateSubbandLength_8(xlength,ylength,xSubbandLength,ySubbandLength);

	changelength_8(xlen,xSubbandLength,6,5,8,7,1,2,3,4,16,15,14,13,11,12,9,10);
	changelength_8(ylen,ySubbandLength,6,5,8,7,1,2,3,4,16,15,14,13,11,12,9,10);

	W8(buff2,xlength,ylength,im6,im5,im8,im7,im1,im2,im3,im4,im16,im15,im14,im13,im11,im12,im9,im10,h,g);
	WR_FILE(buff2,im1,im2,im3,im4,im5,im6,im7,im8,im9,im10,im11,im12,im13,im14,im15,im16,xlen,ylen);

	start=(((wide+1)/2+1)/2)*(((deep+1)/2+1)/2);
	length=xlength*ylength;
	
	for(i=0;i<length;i++)   
       *(buff+i+start)=*(buff2+i);
	
	//*******(0,3),4-18********/
	xlength=((wide+1)/2+1)/2;
	ylength=((deep+1)/2+1)/2;

	CalculateSubbandLength_8(xlength,ylength,xSubbandLength,ySubbandLength);

	changelength_8(xlen,xSubbandLength,1,2,3,4,6,5,8,7,11,12,9,10,16,15,14,13);
	changelength_8(ylen,ySubbandLength,1,2,3,4,6,5,8,7,11,12,9,10,16,15,14,13);

	W8(buff1,xlength,ylength,im1,im2,im3,im4,im6,im5,im8,im7,im11,im12,im9,im10,im16,im15,im14,im13,h,g);
	WR_FILE(buff,im1,im2,im3,im4,im5,im6,im7,im8,im9,im10,im11,im12,im13,im14,im15,im16,xlen,ylen);
	
//	length=xlength*ylength;
//	for(i=0;i<length;i++)            
//		*(buff+i)=*(buff1+i);

	////0-3 subband
	xlength=((xlength+1)/2+1)/2;
	ylength=((ylength+1)/2+1)/2;

	wt(xlength,ylength,buff,h,g);
	CHANGE_DATA(buff,im1,im2,im3,im4,xlength,ylength);

	length=(xlength+1)/2;
	length*=(ylength+1)/2;
	for(i=0;i<length;i++) 
		*(buff+i)=*(im1+i);

	start=length;
	length=xlength/2;
	length*=(ylength+1)/2;
	for(i=0;i<length;i++)  
		*(buff+i+start)=*(im2+i);

	start+=length;
	length=(xlength+1)/2;
	length*=ylength/2;
	for(i=0;i<length;i++) 
		*(buff+i+start)=*(im3+i);

	start+=length;
	length=xlength/2;
	length*=ylength/2;
	for(i=0;i<length;i++) 
		*(buff+i+start)=*(im4+i);

	delete buff1,buff2,buff3;
	delete im1,im2,im3,im4,im5,im6,im7,im8,im9;
	delete im10,im11,im12,im13,im14,im15,im16;                   
                             
/****************************************
quantify the wavelet-transform coefficients
*****************************************/
	float    QQ[64],ZZ[64];
	short int     *QUANTIZATION_COEFFICIENT;

	if((QUANTIZATION_COEFFICIENT=new short int [wide*deep]) == NULL)
    {
		AfxMessageBox("memory allocate error!");
		ErrorSet = TRUE;
		return ;
    }            
     

	ImageQuantization(buff,QUANTIZATION_COEFFICIENT,QQ,ZZ,wide,deep);//,average);

	delete buff;

/*************************************************************/
/*         write Mark data to stream                         */
/*************************************************************/
	unsigned short int CodeStreamLength;
	unsigned char *CodeStream;

	CodeStream=new unsigned char [1000];
	
	CodeStreamLength=0;
	CodeStream[CodeStreamLength++]=0xff;//SOI
	CodeStream[CodeStreamLength++]=0xa0;

	CodeStream[CodeStreamLength++]=0xff;//DTT
	CodeStream[CodeStreamLength++]=0xa4;

	Lt=4+(H0_Half+H1_Half)*6;//2+1+1+(H0_last+H1_last)*6
	CodeStream[CodeStreamLength++]=Lt>>8;//Lt
	CodeStream[CodeStreamLength++]=Lt;
	CodeStream[CodeStreamLength++]=L0;//L0
	CodeStream[CodeStreamLength++]=L1;

	for(i=0;i<H0_Half;i++)
	{
		CodeStream[CodeStreamLength++]=H0_Snk[i];
		CodeStream[CodeStreamLength++]=H0_Exk[i];
		CodeStream[CodeStreamLength++]=H0[i]>>24;
		CodeStream[CodeStreamLength++]=H0[i]>>16;
		CodeStream[CodeStreamLength++]=H0[i]>>8;
		CodeStream[CodeStreamLength++]=H0[i];
	}
	for(i=0;i<H1_Half;i++)
	{
		CodeStream[CodeStreamLength++]=H1_Snk[i];
		CodeStream[CodeStreamLength++]=H1_Exk[i];
		CodeStream[CodeStreamLength++]=H1[i]>>24;
		CodeStream[CodeStreamLength++]=H1[i]>>16;
		CodeStream[CodeStreamLength++]=H1[i]>>8;
		CodeStream[CodeStreamLength++]=H1[i];
	}

	CodeStream[CodeStreamLength++]=0xff;//DQT
	CodeStream[CodeStreamLength++]=0xa5;
	short int Lq;
	unsigned char Ec;
	unsigned short int CC;
	unsigned char Eq,Ez;
	unsigned short int Q,Z;
	float ZZZ;
	Lq=389;//2+1+2+(1+2+1+2)*64;
	Ec=5;
	CC=44000;//C*pow(10,Ec);
	CodeStream[CodeStreamLength++]=Lq>>8;
	CodeStream[CodeStreamLength++]=Lq;
	CodeStream[CodeStreamLength++]=Ec;
	CodeStream[CodeStreamLength++]=CC>>8;
	CodeStream[CodeStreamLength++]=CC;
	for(i=0;i<64;i++)
	{
		ZZZ=ZZ[i];
		if(QQ[i]<0.65536 && QQ[i]>0.1){Eq=5;Q=QQ[i]*100000;} 
		else if(QQ[i]<6.5536){Eq=4;Q=QQ[i]*10000;} 
		else if(QQ[i]<65.536){Eq=3;Q=QQ[i]*1000;} 
		else if(QQ[i]<655.36){Eq=2;Q=QQ[i]*100;} 
		else if(QQ[i]<6553.6){Eq=1;Q=QQ[i]*10;} 
		else {Eq=0;Q=QQ[i];}

		if(ZZZ<0.65536 && ZZZ>0.1){Ez=5;Z=ZZZ*100000;} 
		else if(ZZZ<6.5536){Ez=4;Z=ZZZ*10000;} 
		else if(ZZZ<65.536){Ez=3;Z=ZZZ*1000;} 
		else if(ZZZ<655.36){Ez=2;Z=ZZZ*100;} 
		else if(ZZZ<6553.6){Ez=1;Z=ZZZ*10;} 
		else {Ez=0;Z=ZZZ;}
		
		CodeStream[CodeStreamLength++]=Eq;
		CodeStream[CodeStreamLength++]=Q>>8;
		CodeStream[CodeStreamLength++]=Q;
		CodeStream[CodeStreamLength++]=Ez;
		CodeStream[CodeStreamLength++]=Z>>8;
		CodeStream[CodeStreamLength++]=Z;
	}

	CodeStream[CodeStreamLength++]=0xff;//SOF
	CodeStream[CodeStreamLength++]=0xa2;
	unsigned short int Lf;
	unsigned char A,B,Em,Er,Ev;
	unsigned short int MStream,RStream,Sf;
	Lf=17;//2+1+1+2+2+1+2+1+2+1+2;
	A=0;
	B=255;

	if(M<0.65536 && M>0.1){Em=5;MStream=M*100000;} 
	else if(M<6.5536){Em=4;MStream=M*10000;} 
	else if(M<65.536){Em=3;MStream=M*1000;} 
	else if(M<655.36){Em=2;MStream=M*100;} 
	else if(M<6553.6){Em=1;MStream=M*10;} 
	else {Em=0;MStream=M;}

	if(R<0.65536 && R>0.1){Er=5;RStream=R*100000;} 
	else if(R<6.5536){Er=4;RStream=R*10000;} 
	else if(R<65.536){Er=3;RStream=R*1000;} 
	else if(R<655.36){Er=2;RStream=R*100;} 
	else if(R<6553.6){Er=1;RStream=R*10;} 
	else {Er=0;RStream=R;}

	Ev=56;
	Sf=38100;
	CodeStream[CodeStreamLength++]=Lf>>8;//Lf
	CodeStream[CodeStreamLength++]=Lf;
	CodeStream[CodeStreamLength++]=A;
	CodeStream[CodeStreamLength++]=B;
	CodeStream[CodeStreamLength++]=deep>>8;
	CodeStream[CodeStreamLength++]=deep;
	CodeStream[CodeStreamLength++]=wide>>8;
	CodeStream[CodeStreamLength++]=wide;
	CodeStream[CodeStreamLength++]=Em;
	CodeStream[CodeStreamLength++]=MStream>>8;
	CodeStream[CodeStreamLength++]=MStream;
	CodeStream[CodeStreamLength++]=Er;
	CodeStream[CodeStreamLength++]=RStream>>8;
	CodeStream[CodeStreamLength++]=RStream;
	CodeStream[CodeStreamLength++]=Ev;
	CodeStream[CodeStreamLength++]=Sf>>8;
	CodeStream[CodeStreamLength++]=Sf;
	
	CFile cInFile;
	cInFile.Open(OutputFileName,CFile::modeCreate | CFile::modeWrite);
	cInFile.WriteHuge(CodeStream,CodeStreamLength);
	cInFile.Close();

	delete CodeStream;
   
/************************************
encode the quantified  coefficients  
**************************************/

	DataStreamEncoding(QUANTIZATION_COEFFICIENT, OutputFileName,wide,deep);//encode with Huffman coder
	
	delete QUANTIZATION_COEFFICIENT;
}   

⌨️ 快捷键说明

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