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

📄 fpdirectionimage.cpp

📁 基于块方向的图像无损压缩代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FPDirectionImage.cpp: implementation of the CFPDirectionImage class.
//
//*************************  This module task: *********************************//
//					Caculate direction weight,pointwise direction,
//					and continuous and discrete blockwise direction,
//					and smooth blockwise direction 
//					and other process associated with direction
//*****************************************************************************//

//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "fpedit.h"
#include "FP_PreProcess.h"
#include "math.h"
#include "FPDirectionImage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

BOOL BackBlock[SEG_BLOCK_NO][SEG_BLOCK_NO];
extern BOOL ErrorSet;


// Function:  to give discrete direction-image and weight
// Method:   Mehtre Method
// Input parameter:  Image[][IMAGE_SIZE], BackBlock[][SEG_BLOCK_NO];
// Output parameter: lpDirectionImage[][DIR_IMAGE_SIZE], 
//                   DWeight[][DIR_IMAGE_SIZE]; 
void CreateDirectionImage17v2(PBYTE Image[IMAGE_SIZE],
												 PBOOL BackBlock[SEG_BLOCK_NO],
												 PBYTE lpDirectionImage[DIR_IMAGE_SIZE],
												 DWEIGHT *DWeight[DIR_IMAGE_SIZE])
{
	unsigned long Direction[8];
	short x,y,xBlock,yBlock,ux,uy;      
	BYTE i;
	BYTE Max;
	for(xBlock=0;xBlock<SEG_BLOCK_NO;xBlock++)   //#define SEG_BLOCK_NO	32  in preprocess.h
	  for(yBlock=0;yBlock<SEG_BLOCK_NO;yBlock++)
	  {
        if (BackBlock[xBlock][yBlock]!=TRUE && BackBlock[xBlock][yBlock]!=REJECT)
        {
          ux = xBlock * SEG_BLOCK_SIZE + START_POINT;  //#define SEG_BLOCK_SIZE	16 in preprocess.h
          uy = yBlock * SEG_BLOCK_SIZE + START_POINT;  //#define START_POINT	16 in preprocess.h
          for(x=ux; x<ux+SEG_BLOCK_SIZE; x++)
            for(y=uy; y<uy+SEG_BLOCK_SIZE; y++)
            {
			Direction[0]=abs((short)Image[x-6][y]-Image[x-8][y])+
						 abs((short)Image[x-4][y]-Image[x-6][y])+
						 abs((short)Image[x-2][y]-Image[x-4][y])+
						 abs((short)Image[x][y]-Image[x-2][y])+
						 abs((short)Image[x+2][y]-Image[x][y])+
						 abs((short)Image[x+4][y]-Image[x+2][y])+
                         abs((short)Image[x+6][y]-Image[x+4][y]);

            Direction[1]=abs((short)Image[x-5][y-3]-(short)Image[x-7][y-3])+
						 abs((short)Image[x-3][y-2]-(short)Image[x-5][y-3])+
						 abs((short)Image[x-1][y-1]-(short)Image[x-3][y-2])+
						 abs((short)Image[x][y]-(short)Image[x-1][y-1])+
						 abs((short)Image[x+2][y]-(short)Image[x][y])+
						 abs((short)Image[x+4][y+1]-(short)Image[x+2][y])+
                         abs((short)Image[x+6][y+2]-(short)Image[x+4][y+1]);

            Direction[2]=abs((short)Image[x-4][y-4]-(short)Image[x-6][y-6])+
						 abs((short)Image[x-3][y-3]-(short)Image[x-4][y-4])+
						 abs((short)Image[x-2][y-2]-(short)Image[x-3][y-3])+
						 abs((short)Image[x][y]-(short)Image[x-2][y-2])+
						 abs((short)Image[x+1][y+1]-(short)Image[x][y])+
						 abs((short)Image[x+3][y+3]-(short)Image[x+1][y+1])+
                         abs((short)Image[x+4][y+4]-(short)Image[x+3][y+3]);

            Direction[3]=abs((short)Image[x-3][y-5]-(short)Image[x-3][y-7])+
						 abs((short)Image[x-2][y-3]-(short)Image[x-3][y-5])+
						 abs((short)Image[x-1][y-1]-(short)Image[x-2][y-3])+
						 abs((short)Image[x][y]-(short)Image[x-1][y-1])+
						 abs((short)Image[x][y+2]-(short)Image[x][y])+
						 abs((short)Image[x+1][y+4]-(short)Image[x][y+2])+
                         abs((short)Image[x+2][y+6]-(short)Image[x+1][y+4]);

            Direction[4]=abs((short)Image[x][y-6]-(short)Image[x][y-8])+
						 abs((short)Image[x][y-4]-(short)Image[x][y-6])+
						 abs((short)Image[x][y-2]-(short)Image[x][y-4])+
						 abs((short)Image[x][y]  -(short)Image[x][y-2])+
						 abs((short)Image[x][y+2]-(short)Image[x][y])+
						 abs((short)Image[x][y+4]-(short)Image[x][y+2])+
                         abs((short)Image[x][y+6]-(short)Image[x][y+4]);

            Direction[5]=abs((short)Image[x+3][y-5] -(short)Image[x+3][y-7])+
						 abs((short)Image[x+2][y-3] -(short)Image[x+3][y-5])+
						 abs((short)Image[x+1][y-1] -(short)Image[x+2][y-3])+
						 abs((short)Image[x][y]     -(short)Image[x+1][y-1])+
						 abs((short)Image[x][y+2] -(short)Image[x][y])+
						 abs((short)Image[x-1][y+4] -(short)Image[x][y+2])+
                         abs((short)Image[x-2][y+6] -(short)Image[x-1][y+4]);

            Direction[6]=abs((short)Image[x+4][y-4]-(short)Image[x+6][y-6])+
						 abs((short)Image[x+3][y-3]-(short)Image[x+4][y-4])+
						 abs((short)Image[x+2][y-2]-(short)Image[x+3][y-3])+
						 abs((short)Image[x][y]    -(short)Image[x+2][y-2])+
						 abs((short)Image[x-1][y+1]-(short)Image[x][y])+
						 abs((short)Image[x-3][y+3]-(short)Image[x-1][y+1])+
                         abs((short)Image[x-4][y+4]-(short)Image[x-3][y+3]);

            Direction[7]=abs((short)Image[x+5][y-3] -(short)Image[x+7][y-3])+
						 abs((short)Image[x+3][y-2] -(short)Image[x+5][y-3])+
						 abs((short)Image[x+1][y-1] -(short)Image[x+3][y-2])+
						 abs((short)Image[x][y]     -(short)Image[x+1][y-1])+
						 abs((short)Image[x-2][y] -(short)Image[x][y])+
						 abs((short)Image[x-4][y+1] -(short)Image[x-2][y])+
                         abs((short)Image[x-6][y+2] -(short)Image[x-4][y+1]);
            Max=0;
            for(i=1;i<8;i++)
            	{                               
            	 if(Direction[i]<=Direction[Max])   
            	  	 Max=i;
            	 }               
            lpDirectionImage[x-START_POINT][y-START_POINT]=Max;      //  output parameter 
            for(i=0;i<8;i++)
              DWeight[x-START_POINT][y-START_POINT].Direction[i] = Direction[i];   //output parameter
            }	
        }
      }
}


//Function: to create Direction-Image with Lawrence method.
//Method: Lawrence
//Input parameter:  Image[][]
//Output parameter: lpDirectionImage[][] ,Weight[][]
void CreateDirectionImageEasyLawrence2(PBYTE Image[IMAGE_SIZE],
		WEIGHT *Weight[DIR_IMAGE_SIZE], PBYTE lpDirectionImage[DIR_IMAGE_SIZE])
{
	unsigned long dwSize;
	float  *W0[ IMAGE_SIZE-START_POINT*2+12 ];
	float  *W1[ IMAGE_SIZE-START_POINT*2+12 ];
	float  *W2[ IMAGE_SIZE-START_POINT*2+12 ];
	HANDLE hW0[ IMAGE_SIZE-START_POINT*2+12 ];
	HANDLE hW1[ IMAGE_SIZE-START_POINT*2+12 ];
	HANDLE hW2[ IMAGE_SIZE-START_POINT*2+12 ];

	short x,y;
	float weight0,weight1,weight2;
	float AngleAvg;
	
    float WeightMax,WeightMid,WeightMin;
    short AngleMax,AngleMid,AngleMin;

	dwSize=((long)IMAGE_SIZE-START_POINT*2+12)*sizeof(float);
	for(int i=0;i<IMAGE_SIZE-START_POINT*2 +12; i++)
	{
		hW0[i]=(HGLOBAL)GlobalAlloc(GHND,dwSize);
		if(!hW0[i])
		{
			MessageBox((HWND)NULL, "not enough memory1 in dirweigh!", NULL, MB_OK);
			ErrorSet = TRUE;
			return;
		}
		W0[i] = (float *)GlobalLock(hW0[i]);									
		if(W0[i] == NULL)
		{                    
			MessageBox((HWND)NULL, "not enough memory2!", NULL, MB_OK);
			ErrorSet = TRUE;
			return;
		}
	}

	for(i=0;i<IMAGE_SIZE-START_POINT*2 +12; i++)
	{
		hW1[i]=(HGLOBAL)GlobalAlloc(GHND,dwSize);
		if(!hW1[i])
		{
			MessageBox((HWND)NULL, "not enough memory1 in dirweigh!", NULL, MB_OK);
			ErrorSet = TRUE;
			return;
		}
		W1[i] = (float *)GlobalLock(hW1[i]);
		if(W1[i] == NULL)
		{
			MessageBox((HWND)NULL, "not enough memory2!", NULL, MB_OK);
			ErrorSet = TRUE;
			return;
		}
    }

	for(i=0;i<IMAGE_SIZE-START_POINT*2 +12; i++)
	{
		hW2[i]=(HGLOBAL)GlobalAlloc(GHND,dwSize);
		if(!hW2[i])
		{
			MessageBox((HWND)NULL, "not enough memory1 in dirweigh!", NULL, MB_OK);
			ErrorSet = TRUE;
			return;
		}
		W2[i] = (float *)GlobalLock(hW2[i]);									
		if(W2[i] == NULL)
		{
			MessageBox((HWND)NULL, "not enough memory2!", NULL, MB_OK);
			ErrorSet = TRUE;
			return;
		}
    }

	for (x=START_POINT - 6;  x<IMAGE_SIZE - START_POINT + 6 ;  x++)
		for (y=START_POINT - 6;  y<IMAGE_SIZE - START_POINT + 6  ;y++)
		{
			W0[x - START_POINT + 6][y - START_POINT + 6]=
					+(float)abs((short)Image[x-6][y]-Image[x-8][y])
					+abs((short)Image[x-4][y]-Image[x-6][y])
					+abs((short)Image[x-2][y]-Image[x-4][y])
					+abs((short)Image[x][y]  -Image[x-2][y])
					+abs((short)Image[x+2][y]-Image[x][y])
					+abs((short)Image[x+4][y]-Image[x+2][y])
					+abs((short)Image[x+6][y]-Image[x+4][y]);
			
			W1[x - START_POINT + 6][y - START_POINT + 6]=
					+(float)abs((short)Image[x-3][y-5]-Image[x-4][y-7])
					+abs((short)Image[x-2][y-4]-Image[x-3][y-5])
					+abs((short)Image[x-1][y-2]-Image[x-2][y-4])
					+abs((short)Image[x][y]    -Image[x-1][y-2])
					+abs((short)Image[x+1][y+2]-Image[x][y])
					+abs((short)Image[x+2][y+3]-Image[x+1][y+2])
					+abs((short)Image[x+3][y+5]-Image[x+2][y+3]);
			
			W2[x - START_POINT + 6][y - START_POINT + 6]=
					+(float)abs((short)Image[x-3][y+5]-Image[x-4][y+7])
					+abs((short)Image[x-2][y+4]-Image[x-3][y+5])
					+abs((short)Image[x-1][y+2]-Image[x-2][y+4])
					+abs((short)Image[x][y]    -Image[x-1][y+2])
					+abs((short)Image[x+1][y-2]-Image[x][y])
					+abs((short)Image[x+2][y-3]-Image[x+1][y-2])
					+abs((short)Image[x+3][y-5]-Image[x+2][y-3]);
		}
		
	for (x=0;  x<IMAGE_SIZE - START_POINT * 2;  x++)
		for (y=0;  y<IMAGE_SIZE - START_POINT * 2;  y++)
		{
			weight0= W0[x+6][y+6-6]+W0[x+6][y+6-4]+W0[x+6][y+6-2]+W0[x+6][y+6]
					+W0[x+6][y+6+6]+W0[x+6][y+6+4]+W0[x+6][y+6+2];
			weight1= W1[x+6-5][y+6+3]+W1[x+6-3][y+6+2]+W1[x+6-2][y+6+1]+W1[x+6][y+6]
					+W1[x+6+5][y+6-3]+W1[x+6+4][y+6-2]+W1[x+6+2][y+6-1];
			weight2= W2[x+6-5][y+6-3]+W2[x+6-3][y+6-2]+W2[x+6-2][y+6-1]+W2[x+6][y+6]
					+W2[x+6+5][y+6+3]+W2[x+6+4][y+6+2]+W2[x+6+2][y+6+1];
					
	        if(weight0>weight1)
			{
    	        if(weight2>weight0)
    			{
    			    WeightMax=weight2;
    			    WeightMid=weight0;
    			    WeightMin=weight1;
    			    AngleMax=30;
    			    AngleMid=90;
    			    AngleMin=150;
    			}
    	        else if(weight2<weight1)
				{
    			    WeightMax=weight0;
    			    WeightMid=weight1;
    			    WeightMin=weight2;
    			    AngleMax=90;
    			    AngleMid=150;
    			    AngleMin=30;
				}
    	        else
    			{
    			    WeightMax=weight0;	
    			    WeightMid=weight2;
    			    WeightMin=weight1;
    			    AngleMax=90;
    			    AngleMid=30;
    			    AngleMin=150;
    			} 
			}	
            else
			{ 
     	        if(weight2<weight0)		 		
    			{
    			    WeightMax=weight1;
    			    WeightMid=weight0;
    			    WeightMin=weight2;
    			    AngleMax=150;
    			    AngleMid=90;
    			    AngleMin=30;
				}
    	        else if(weight2>weight1)
    			{
    			    WeightMax=weight2;
    			    WeightMid=weight1;
    			    WeightMin=weight0;
    			    AngleMax=30;
    			    AngleMid=150;
    			    AngleMin=90;
    			}
    	        else
    			{
    			    WeightMax=weight1;
    			    WeightMid=weight2;
    			    WeightMin=weight0;
    			    AngleMax=150;
    			    AngleMid=30;
    			    AngleMin=90;
    			}
			}	             
            if(fabs(WeightMax-WeightMin) > 1 )
	    	{
	    		if( (AngleMax-AngleMid == 60) ||  (AngleMax-AngleMid == -120) )
    				AngleAvg=(float)AngleMax-(float)30*(WeightMid-WeightMin)/(WeightMax-WeightMin);
    			else
    			    AngleAvg=(float)AngleMax+(float)30*(WeightMid-WeightMin)/(WeightMax-WeightMin);
	        }   
	        else
		        AngleAvg=360;            
		    		            
            if(AngleAvg < 190 )
			{
    			if( AngleAvg >= 78.75)                                
 		   		{
    			   	if(AngleAvg < 101.25)
    					lpDirectionImage[x][y]=4;
    				else if(AngleAvg <= 123.75)
    					lpDirectionImage[x][y]=5;
    				else if(AngleAvg <= 146.25)
    					lpDirectionImage[x][y]=6;
    				else if(AngleAvg <= 168.75)
    					lpDirectionImage[x][y]=7;
    				else
    					lpDirectionImage[x][y]=0;
    			}
    			else 
    			{
    				if(AngleAvg >= 56.25)
    			   		lpDirectionImage[x][y]=3;
    				else if(AngleAvg >= 33.75)    

⌨️ 快捷键说明

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