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

📄 f_encode.cpp

📁 基于块方向的图像无损压缩代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "fileapi.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include <time.h>
#include  "io.h" 
#include "bitio.h"
#include "afispath.h"
#include "errhand.h"
#include "liwei.h" 

#define  C 0.44         
#define  q  0.8     
//#define IMAGE_WIDE 640L
//#define IMAGE_DEEP 640L                              
short int  IMAGE_WIDE,IMAGE_DEEP;

/*extern */BOOL ErrorSet;
         
#define DD  9  /* DD is the wavelet filter length */

/*------------------------------------------
PURPOSE:TO QUANTIFY THE WAVELET-TRANSFORMED

        IMAGE
----------------------------------------*/                 
//extern void  ImageQuantization(float  *WAVELET_COEFFICIENT,
//                         char  *QUANTIZATION_COEFFICIENT,
//                         float *QQ,long wide,long deep,float average);                 
extern void ImageQuantization(float  *WltCoef, short int  *QuanCoef,float *Q,
                         float *Z,long wide,long deep);

extern void DataStreamEncoding(short int *Image, CString CompressedFileName,short int ImageWidth,
						short int ImageHeight);
extern void	CalculateSubbandLength_8(long xlength,long ylength,long *xSubbandLength,long *ySubbandLength);
/*------------------------------------------
PURPOSE:TO  ENCODE  THE 
        QUANTIZATION  IMAGE
----------------------------------------*/            
   
//void WAVELET_CODE(char *ObjectFile,long wide,long deep,HANDLE hq1);


/*--------------------------------------
  PURPOSE : TO PROCESS THE EDGE PIXELS.
--------------------------------------*/
static inline int extend_11(int idx, unsigned int len)
{        //(1,1)
	if (idx < 0)	idx = -idx;
	idx %= 2*(len-1);
	if ((unsigned int)idx >= len)	idx = 2*len-idx-2;
    
	return(idx);
}

int a(int x,int xsize)
{
   if(x<0)        x=-x;
   if(x>=xsize)   x=xsize*2-x-2;
   return(x);
}

/*-------------------------------------
  PURPOSE: TO DO THE THRESHOLD PROCESS
--------------------------------------*/
int s(float x)
 {

	if(x>0)      x=(float)(x+0.5);
	if(x<0)      x=(float)(x-0.5);
   	if(x>127)    return(127);
	if(x<-127)   return(-127);
    return((int)x);
 }


/********************************
FILL THE BUFFER BU WITH im

**********************************/
void WR_FILE(float *bu,float *im1,float *im2,float *im3,float *im4,
             float *im5, float *im6,float *im7,float *im8,float *im9,
             float *im10,float *im11,float *im12,float *im13,float *im14,
             float *im15,float *im16,long *xlen,long *ylen)
{
	long n;
	long start,length;

	length=xlen[0]*ylen[0];
	for(n=0;n<length;n++)
		*(bu+n)=*(im1+n);
	
	start=length;
	length=xlen[1]*ylen[1];

	for(n=0;n<length;n++)
		*(bu+n+start)=*(im2+n);

	start+=length;
	length=xlen[2]*ylen[2];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im3+n);

	start+=length;
	length=xlen[3]*ylen[3];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im4+n);

	start+=length;
	length=xlen[4]*ylen[4];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im5+n);
	
	start+=length;
	length=xlen[5]*ylen[5];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im6+n);
	
	start+=length;
	length=xlen[6]*ylen[6];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im7+n);
	
	start+=length;
	length=xlen[7]*ylen[7];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im8+n);
	
	start+=length;
	length=xlen[8]*ylen[8];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im9+n);
	
	start+=length;
	length=xlen[9]*ylen[9];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im10+n);

	start+=length;
	length=xlen[10]*ylen[10];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im11+n);
	
	start+=length;
	length=xlen[11]*ylen[11];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im12+n);

	start+=length;
	length=xlen[12]*ylen[12];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im13+n);

	start+=length;
	length=xlen[13]*ylen[13];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im14+n);

	start+=length;
	length=xlen[14]*ylen[14];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im15+n);
	
	start+=length;
	length=xlen[15]*ylen[15];
	for(n=0;n<length;n++)
		*(bu+n+start)=*(im16+n);
}

/***********************
REORDER THE DATA
***********************/
void CHANGE_DATA(float *bu,float *img1,float *img2,
                 float *img3,float *img4,long wide,long deep)
{
	long  i,n;
	long xLow,xHigh,yLow,yHigh;

	xLow=(wide+1)/2;
	yLow=(deep+1)/2;
	xHigh=wide-xLow;
	yHigh=deep-yLow;
   
	for(n=0;n<yLow;n++)
	{
		for(i=0;i<xLow;i++)
			*(img1+n*xLow+i)=*(bu+n*wide+i);
         
		for(i=xLow;i<wide;i++)
			*(img2+n*xHigh+i-xLow)=*(bu+n*wide+i);
	}

	for(n=yLow;n<deep;n++)
	{
		for(i=0;i<xLow;i++)
			*(img3+(n-yLow)*xLow+i)=*(bu+n*wide+i);
      
		for(i=xLow;i<wide;i++)
			*(img4+(n-yLow)*xHigh+i-xLow)=*(bu+n*wide+i);
	}
}

/***********************
WAVELET TRANSFORM
***********************/
void wt(long wide3,long deep3,float *buffert,float h[],float g[])
{
  
	long i,j,k,n;
	float temp1,temp2;
	float *bufferx;
	float *buffery;
	long xs,ys;
               
	 // bufferx buffery are the work buffers 
	xs=(long)wide3;ys=(long)deep3;
  
	if((bufferx=new float [xs+1])== NULL)
    {
		AfxMessageBox("Allocate memory error!");
 		ErrorSet = TRUE;
		return ;
	}
	if((buffery=new float [ys+1])== NULL)
    {
		delete bufferx;
		AfxMessageBox("Allocate memory error!");
 		ErrorSet = TRUE;
		return;
	}

  
  	// to filter in horizonal direction 
	for(n=0;n<ys;n++)
	{
		for(i=0;i<xs;i+=2)
		{
			temp1=h[(DD-1)/2]*(*(buffert+(long)(n*xs+i)));
			for(j=1;j<=(DD-1)/2;j++)
				temp1+=h[j+(DD-1)/2]*((*(buffert+(long)(n*xs+abs(i-j))))+
	                           (*(buffert+(long)(n*xs+a(i+j,xs)))));
         
			temp2=g[-1+(DD-1)/2]*(*(buffert+(long)(n*xs+a(i+1,xs))));
			for(j=0;j<=(DD-1)/2-2;j++)
				temp2+=g[j+(DD-1)/2]*((*(buffert+(long)(n*xs+abs(i-j))))
                                    +(*(buffert+(long)(n*xs+a(i+j+2,xs)))));
	        
			bufferx[i/2]=temp1;
			bufferx[i/2+(xs+1)/2]=temp2;
		}
		for(k=0;k<xs;k++)  
			*(buffert+(long)(n*xs+k))=bufferx[k]; //suspicios
	}
	
	// to filter in the vertical direction 
	for(n=0;n<xs;n++)
	{
		for(i=0;i<ys;i+=2)
		{
			temp1=h[(DD-1)/2]*(*(buffert+(long)(i*xs+n)));
			for(j=1;j<=(DD-1)/2;j++)
				temp1+=h[j+(DD-1)/2]*((*(buffert+(long)(a(i-j,ys)*xs+n)))
	                           +(*(buffert+(long)(a(i+j,ys)*xs+n))));
       
			temp2=g[-1+(DD-1)/2]*(*(buffert+(long)(a(i+1,ys)*xs+n)));
			for(j=0;j<=(DD-1)/2;j++)
				temp2+=g[j+(DD-1)/2]*((*(buffert+(long)(abs(i-j)*xs+n)))
	                           +(*(buffert+(long)(a(i+j+2,ys)*xs+n))));
	    
			buffery[i/2]=temp1;
			buffery[i/2+(ys+1)/2]=temp2;
		}
		for(k=0;k<ys;k++)
		{
			*(buffert+(long)(k*wide3+n))=buffery[k];
		}
	}

	// write buffer into four subimages  

	delete bufferx,buffery;
}

/*The function to decompose the image into eight subbands*/
void W8(float * buffer,long wide1,long deep1,float *im1,float *im2,
       float  *im3,float *im4,float *im5,float *im6,float  *im7,
       float *im8,float *im9,float *im10,
       float *im11,float *im12,float *im13,float *im14,float *im15,
       float *im16,float h[],float g[]) 

{
	float   *imag1;
	float   *imag2;
	float   *imag3;
	float   *imag4; 
	long xLow,xHigh,yLow,yHigh;

	xLow=(wide1+1)/2;
	yLow=(deep1+1)/2;
	xHigh=wide1-xLow;
	yHigh=deep1-yLow;

	if((imag1=new float [xLow*yLow]) == NULL)
    {
		AfxMessageBox("memory error!");
 		ErrorSet = TRUE;
		return ;
    }           

	if((imag2=new float [xLow*yLow]) == NULL)
    {
		AfxMessageBox("memory error!");
		ErrorSet = TRUE;
		return ;
    }                                      

	if((imag3=new float [xLow*yLow]) == NULL)
    {
		AfxMessageBox("memory error!");
 		ErrorSet = TRUE;
		return ;
    }                                     

	if((imag4=new float [xLow*yLow]) == NULL)
    {
		AfxMessageBox("Globl lock error!");
		ErrorSet = TRUE;
		return ;
    }                

	wt(wide1,deep1,buffer,h,g);
	CHANGE_DATA(buffer,imag1,imag2,imag3,imag4,wide1,deep1);      
  
	wt(xLow,yLow,imag1,h,g);
	wt(xHigh,yLow,imag2,h,g);
	wt(xLow,yHigh,imag3,h,g);      
	wt(xHigh,yHigh,imag4,h,g);

	CHANGE_DATA(imag1,im1,im2,im3,im4,xLow,yLow); 
	CHANGE_DATA(imag2,im5,im6,im7,im8,xHigh,yLow); 
	CHANGE_DATA(imag3,im9,im10,im11,im12,xLow,yHigh); 
	CHANGE_DATA(imag4,im13,im14,im15,im16,xHigh,yHigh);

	delete imag1,imag2,imag3,imag4;
}

/* calculate the Mean of the image*/
double  CalculateMean(float *buffer,short int ImageWidth,short int ImageHeight)
{                       
	long    i;
	double  mean=0; 
 
	for(i=0;i<ImageWidth*ImageHeight;i++)
		mean+=(double)(*(buffer+i));

	mean/=((double)(ImageWidth*ImageHeight)); 
 
	return(mean);
}

/*calculate the  max ABS*/
float Calculate_BAS_MAX(float *buffer,short int ImageWidth,short int ImageHeight)
{
	float   max=0;
	long    i;  
   
	for(i=0;i<ImageWidth*ImageHeight;i++)
		if(fabs(*(buffer+i))>max)
			max=(float)fabs(*(buffer+i));

	return(max);
}

void changelength_8(long *len,long *SubbandLength,char L1,char L2,char L3,char L4,char L5,char L6,
					 char L7,char L8,char L9,char L10,char L11,char L12,char L13,char L14,char L15,char L16)
{
	*(len+L1-1)=SubbandLength[1];
	*(len+L2-1)=SubbandLength[2];
	*(len+L3-1)=SubbandLength[3];
	*(len+L4-1)=SubbandLength[4];

	*(len+L5-1)=SubbandLength[5];
	*(len+L6-1)=SubbandLength[6];
	*(len+L7-1)=SubbandLength[7];
	*(len+L8-1)=SubbandLength[8];

	*(len+L9-1)=SubbandLength[9];
	*(len+L10-1)=SubbandLength[10];
	*(len+L11-1)=SubbandLength[11];
	*(len+L12-1)=SubbandLength[12];
	
	*(len+L13-1)=SubbandLength[13];
	*(len+L14-1)=SubbandLength[14];
	*(len+L15-1)=SubbandLength[15];
	*(len+L16-1)=SubbandLength[16];
}

/****************************
THE FOLLOWING IS THE PROCESSING OF  WAVELET TRANSFORM FOR A
FINGERPRINT IMAGE  ******************************/


void _GA_Compress(CString InputFileName,CString OutputFileName)
//void CompressFP(DWORD BarCode,BYTE FingerNumber)
{
    float   *im1;float *im2;float *im3;float *im4;float *im5;
    float   *im6;float *im7;
    float   *im8;float *im9;float *im10;float *im11;
    float  *im12;float *im13;float *im14;float *im15;
    float  *im16;

    float  h[DD]={ 0.037828455506995,
               	  -0.023849465019380,
               	  -0.11062440441842,
               	   0.37740285561265,          
               	   0.85269867900940,
	               0.37740285561265,
    	          -0.11062440441842,
        	      -0.023849465019380,
            	   0.037828455506995},
               
	       g[DD]={ 0.064538882628938,
            	  -0.040689417609558,
            	  -0.41809227322221,
            	   0.78848561640566,                
        	       -0.41809227322221,                
            	   -0.040689417609558,               
             	   0.064538882628938,
             	   0,
                   0
              	 };                 
	
//	float h[DD],g[DD];
	unsigned char L0=9,	L1=7;
	unsigned short int Lt;
	unsigned char H0_Half,H1_Half,
		H0_Snk[11]={0,0,1,1,0},
		H0_Exk[11]={9,10,10,11,11},
		H1_Snk[11]={0,1,1,0},
		H1_Exk[11]={9,10,11,10};
	unsigned int H0[11]={852698684,3774028420,1106244028,2384946495,3782845661},
		H1[11]={788485587,4180922806,4068941623,645388812};

	float          M,R;
    long           i, v2;
    float          *buff;
    unsigned char  *pc;
    unsigned short int      wide,deep; 

    CFile	 cFile;

    DWORD  dwTime1,dwTime2,count=0;
    BOOL bOpen=FALSE;

////////////////////////////////////////  
	H0_Half=(L0+1)/2;
	H1_Half=(L1+1)/2;
//////////////////////////////////

//	for(i=0;i<=8;i++)
//	{
//		h[i]=(float)(h[i]/sqrt(2.0));
//		g[i]=(float)(g[i]/sqrt(2.0));
//	}               

 
	do
	{
		bOpen = cFile.Open(InputFileName,CFile::modeRead|CFile::shareExclusive);
		dwTime1 = GetTickCount();   
		do
		{
			dwTime2 = GetTickCount();
		}while ((dwTime2-dwTime1)<300);
		
		count ++;
		if (count>=15) break;
	}while(bOpen == 0);

⌨️ 快捷键说明

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