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

📄 testview.cpp

📁 方向自适应的小波变换程序(5/3) 本人自己编写
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// testView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "testView.h"
#include "DlgDispResult.h"
#include "math.h"
#include "memory.h"
#include "Dialog1.h"

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

int **origin;
////////////////////////////////////////////////////
int **ori_HalPixel_interp;
int **ori_QuartPixel_interp;
int **Predict_direc;
int **Block_energy;

int **ori_QuartPixel_interp1;
int **Predict_direc1;
int **Block_energy1;

int **total_preDrection; //存储列预测的方向
int **total_preDrection1; //存储行预测的方向

double **Sinc_interpolation;//sinc插值的权值
int	M_sample;//sinc插值所需的样本值数
double PI=3.14159;
/////////////////////////////////////////////////////////////////////////////
int Predict_Direction[2][8]={0,1,0,2,1,2,0,1,1,2,0,1,0,-1,0,-2};
int Predict_Direction1[2][8]={0,1,1,3,1,3,1,1,1,3,1,1,0,-1,-1,-3};
///////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CScrollView)

BEGIN_MESSAGE_MAP(CTestView, CScrollView)
	//{{AFX_MSG_MAP(CTestView)
	ON_COMMAND(ID_INT_WAVELET_FORWARD, OnIntWaveletForward)
	ON_COMMAND(ID_INT_WAVELET_BACKWARD, OnIntWaveletBackward)
	ON_COMMAND(ID_INT_ADL_WAVELET_FORWARD, OnIntAdlWaveletForward)
	ON_COMMAND(ID_INT_ADL_WAVELET_BACKWARD, OnIntAdlWaveletBackward)
	ON_COMMAND(ID_INT_Quincunx_WAVELET_FORWARD, OnINTQuincunxWAVELETFORWARD)
	ON_COMMAND(ID_INT_Quincunx_WAVELET_BACKWARD, OnINTQuincunxWAVELETBACKWARD)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{

}

CTestView::~CTestView()
{
	  if(origin!=NULL)
	  {delete origin;}
	  if(ori_HalPixel_interp!=NULL)
	  {delete ori_HalPixel_interp;}
	  if(ori_QuartPixel_interp!=NULL)
	  {delete ori_QuartPixel_interp;}
	  if(Predict_direc!=NULL)
	  {delete Predict_direc;}
	  if(Block_energy!=NULL)
	  {delete Block_energy;}

	  if(ori_QuartPixel_interp1!=NULL)
	  {delete ori_QuartPixel_interp1;}
	  if(Predict_direc1!=NULL)
	  {delete Predict_direc1;}
	  if(Block_energy1!=NULL)
	  {delete Block_energy1;}
	 
	  if(total_preDrection!=NULL)
	  {delete total_preDrection;}
	  if(total_preDrection1!=NULL)
	  {delete total_preDrection1;}
	  
}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

void CTestView::OnDraw(CDC* pDC)
{
	// 显示等待光标
	BeginWaitCursor();
	
	// 获取文档
	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	// 获取DIB
	HDIB hDIB = pDoc->GetHDIB();
	
	// 判断DIB是否为空
	if (hDIB != NULL)
	{
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
		
		// 获取DIB宽度
		int cxDIB = (int) ::DIBWidth(lpDIB);
		
		// 获取DIB高度
		int cyDIB = (int) ::DIBHeight(lpDIB);

		::GlobalUnlock((HGLOBAL) hDIB);
		
		CRect rcDIB;
		rcDIB.top = rcDIB.left = 0;
		rcDIB.right = cxDIB;
		rcDIB.bottom = cyDIB;
		
		CRect rcDest;
		
		// 判断是否是打印
		if (pDC->IsPrinting())
		{
			// 是打印,计算输出图像的位置和大小,以便符合页面
			
			// 获取打印页面的水平宽度(象素)
			int cxPage = pDC->GetDeviceCaps(HORZRES);
			
			// 获取打印页面的垂直高度(象素)
			int cyPage = pDC->GetDeviceCaps(VERTRES);
			
			// 获取打印机每英寸象素数
			int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
			int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
			
			// 计算打印图像大小(缩放,根据页面宽度调整图像大小)
			rcDest.top = rcDest.left = 0;
			rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
					/ ((double)cxDIB * cxInch));
			rcDest.right = cxPage;
			
			// 计算打印图像位置(垂直居中)
			int temp = cyPage - (rcDest.bottom - rcDest.top);
			rcDest.bottom += temp/2;
			rcDest.top += temp/2;

		}
		else   
		// 非打印
		{
			// 不必缩放图像
			rcDest = rcDIB;
		}
		
		// 输出DIB
		::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
			&rcDIB, pDoc->GetDocPalette());
	}
	
	// 恢复正常光标
	EndWaitCursor();
	
}

void CTestView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
	return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers



//////////////////////////////////////////////////////////////////////////////////////////////
////以下是小波变换函数
void CTestView::wavelet_transform(LPSTR dst,const int width,const int height,const int levels)
{
    int l;
	LPSTR tmp_lastrow=(LPSTR)malloc((width+height+height)*sizeof(char));
	LPSTR tmp_column_in=tmp_lastrow+width;
	LPSTR tmp_column_out=tmp_lastrow+width+height;

	for(l=0;l<levels;l++)
	{  int w=width>>l;
	   int h=height>>l;
	   int i;

	   //首先是行变换
	   wavelet_row(tmp_lastrow,dst+(height-h)*width,w);
	   for(i=(height-h)*width;i<(height-1)*width;i+=width)
		   wavelet_row(dst+i,dst+i+width,w);  

       //然后是列变换
       for(i=0;i<w;i++)
	   {  int j;
	      
	      for(j=1;j<h;j++)
			  tmp_column_in[j-1]=dst[(height-j-1)*width+i];  //倒序处理
		  tmp_column_in[h-1]=tmp_lastrow[i];  //最后一列数据的处理
		  
		  wavelet_row(tmp_column_out,tmp_column_in,h);
		  for(j=0;j<h;j++)
			  dst[(height-j-1)*width+i]=tmp_column_out[j];
	   } 
	}
	free(tmp_lastrow);
}

void CTestView::wavelet_transform(int **dst,const int width,const int height,const int levels)
{
    int l;
	int* tmp_lastrow=(int*)malloc((width+height+height)*sizeof(int));
	int* tmp_column_in=tmp_lastrow+width;
	int* tmp_column_out=tmp_lastrow+width+height;

	for(l=0;l<levels;l++)
	{  int w=width>>l;
	   int h=height>>l;
	   int i;

	   //首先是行变换
	   wavelet_row(tmp_lastrow,dst[h-1],w);
	   for(i=h-2;i>=0;i--)
		   wavelet_row(dst[i+1],dst[i],w);  

       //然后是列变换
       for(i=0;i<w;i++)
	   {  int j;
	      
	      for(j=1;j<h;j++)
			  tmp_column_in[j-1]=dst[j][i];  //倒序处理
		  tmp_column_in[h-1]=tmp_lastrow[i];  //最后一列数据的处理
		  
		  wavelet_row(tmp_column_out,tmp_column_in,h);
		  for(j=0;j<h;j++)
			  dst[j][i]=tmp_column_out[j];
	   } 
	}
	free(tmp_lastrow);
}


void CTestView::wavelet_row(LPSTR dst, const LPSTR src, const int len)
{
    int i,mid;
	LPSTR ptr=src;
    LPSTR avg,det;

	mid=len/2;
	avg=dst;        //低频小波系数
	det=dst+mid;    //高频小波系数

	*det=(char)(ptr[1]-(ptr[2]+ptr[0])/2);  //预测信号高频
	*avg=(char)(ptr[0]+(det[0]+det[0])/4);  //进一步提升
	ptr+=2;
	det++;
	avg++;

	for(i=1;i<mid-1;i++)
	{  *det=(char)(ptr[1]-(ptr[2]+ptr[0])/2); 
	   *avg=(char)(ptr[0]+(det[0]+det[-1])/4);
	   ptr+=2;
	   det++;
	   avg++;
	}

	*det=(char)(ptr[1]-(ptr[0]+ptr[0])/2); 
	*avg=(char)(ptr[0]+(det[0]+det[-1])/4);
}

void CTestView::wavelet_row(int *dst, int *src, const int len)
{
    int i,mid;
	int* ptr=src;
    int* avg,*det;

	mid=len/2;
	avg=dst;        //低频小波系数
	det=dst+mid;    //高频小波系数

	
	*det=(ptr[1]-(ptr[2]+ptr[0])/2);  //预测信号高频
	*avg=(ptr[0]+(det[0]+det[0])/4);  //进一步提升
	ptr+=2;
	det++;
	avg++;

	for(i=1;i<mid-1;i++)
	{  *det=(ptr[1]-(ptr[2]+ptr[0])/2); 
	   *avg=(ptr[0]+(det[0]+det[-1])/4);
	   ptr+=2;
	   det++;
	   avg++;
	}

	*det=(ptr[1]-(ptr[0]+ptr[0])/2); 
	*avg=(ptr[0]+(det[0]+det[-1])/4);
}

void CTestView::inverse_wavelet_transform(LPSTR dst, const int width, const int height, const int levels)
{
    int l;
	LPSTR tmp_lastrow=(LPSTR)malloc((width+height+height)*sizeof(char));
	LPSTR tmp_column_in=tmp_lastrow+width;
	LPSTR tmp_column_out=tmp_lastrow+width+height;

	for(l=levels-1;l>=0;l--)
	{  int w=width>>l;
	   int h=height>>l;
	   int i;
	   //竖直方向逆变换
	   for(i=0;i<w;i++)
	   {  int j;
	      for(j=0;j<h;j++)
			  tmp_column_in[j]=dst[(height-j-1)*width+i];  //倒序处理
		  inverse_wavelet_row(tmp_column_out,tmp_column_in,h);
		  for(j=0;j<h-1;j++)
			  dst[(height-j-2)*width+i]=tmp_column_out[j];  //再一次倒序处理
		  tmp_lastrow[i]=tmp_column_out[h-1];  //保存最后一列数据的处理结果

	   }

	   //水平方向逆变换
	   for(i=(height-2)*width;i>=(height-h)*width;i-=width)
		   inverse_wavelet_row(dst+i+width,dst+i,w);
	   inverse_wavelet_row(dst+(height-h)*width,tmp_lastrow,w);
	}
	free(tmp_lastrow);

}

void CTestView::inverse_wavelet_transform(int** dst, const int width, const int height, const int levels)
{
    int l;
	int* tmp_lastrow=(int*)malloc((width+height+height)*sizeof(int));
	int* tmp_column_in=tmp_lastrow+width;
	int* tmp_column_out=tmp_lastrow+width+height;

	for(l=levels-1;l>=0;l--)
	{  int w=width>>l;
	   int h=height>>l;
	   int i;
	   //竖直方向逆变换
	   for(i=0;i<w;i++)
	   {  int j;
	      for(j=0;j<h;j++)
			  tmp_column_in[j]=dst[j][i];  //倒序处理
		  inverse_wavelet_row(tmp_column_out,tmp_column_in,h);
		  for(j=0;j<h-1;j++)
			  dst[j+1][i]=tmp_column_out[j];  //再一次倒序处理
		  tmp_lastrow[i]=tmp_column_out[h-1];  //保存最后一列数据的处理结果

	   }

	   //水平方向逆变换
	   for(i=1;i<h;i++)
		   inverse_wavelet_row(dst[i-1],dst[i],w);
	   inverse_wavelet_row(dst[h-1],tmp_lastrow,w);
	}
	free(tmp_lastrow);

}

void CTestView::inverse_wavelet_row(LPSTR dst,const LPSTR src,const int len)
{
    int i,mid;
	LPSTR ptr;
	LPSTR avg,det;
	
	mid=len/2;
    ptr=dst;
	avg=src;       //低频小波系数的逆变换结果
	det=src+mid;   //高频小波系数的逆变换结果
	
	//图像逆变换的边缘处理
    *ptr=(char)(avg[0]-(det[0]+det[0])/4);
    ptr+=2;
	det++;
	avg++;

	for(i=0;i<mid-1;i++)
	{  ptr[0]=(char)(avg[0]-(det[0]+det[-1])/4);
	   ptr[-1]=(char)(det[-1]+(ptr[0]+ptr[-2])/2);
	   ptr+=2;
	   det++;
	   avg++;
	}
	ptr[-1]=(char)(det[-1]+(ptr[-2]+ptr[-2])/2);   //边缘处理
}

void CTestView::inverse_wavelet_row(int *dst,int* src,const int len)
{
    int i,mid;
	int* ptr;
	int* avg,*det;
	
	mid=len/2;
    ptr=dst;
	avg=src;       //低频小波系数的逆变换结果
	det=src+mid;   //高频小波系数的逆变换结果
	
	//图像逆变换的边缘处理
    *ptr=(avg[0]-(det[0]+det[0])/4);
    ptr+=2;
	det++;
	avg++;

	for(i=0;i<mid-1;i++)
	{  ptr[0]=(avg[0]-(det[0]+det[-1])/4);
	   ptr[-1]=(det[-1]+(ptr[0]+ptr[-2])/2);
	   ptr+=2;
	   det++;
	   avg++;
	}
	ptr[-1]=(det[-1]+(ptr[-2]+ptr[-2])/2);   //边缘处理
}

void CTestView::wavelet_transform_overlap(int **dst, const int width, const int height, const int levels)
{
	int l;
	int* tmp_lastrow=(int*)malloc((width+height+height)*sizeof(int));
	int* tmp_column_in=tmp_lastrow+width;
	int* tmp_column_out=tmp_lastrow+width+height;

//	int t[16]={180,183,187,185,190,192,192,185,187,190,193,198,200,195,200,203};
//	for(int i=0;i<16;i++)
//		t[i]=2*(i+1)+200;

	
//	for(l=0;l<levels;l++)
//	{
//		int w=16>>l;
//		wavelet_row(tmp_lastrow,t,w);
//	    memcpy(t,tmp_lastrow,w*sizeof(int));
//	}
	for(l=0;l<levels;l++)
	{  int w=width>>l;
	   int h=height>>l;
	   int i;

	   //首先是行变换
	   for(i=0;i<h;i++)
	   { 
		  wavelet_row(tmp_lastrow,dst[i],w);
		  memcpy(dst[i],tmp_lastrow,w*sizeof(int));
	   }
	   
       //然后是列变换
       for(i=0;i<w;i++)
	   {  int j;
	      
	      for(j=0;j<h;j++)
			  tmp_column_in[j]=dst[j][i];  
		  
		  wavelet_row(tmp_column_out,tmp_column_in,h);

          for(j=0;j<h;j++)
			  dst[j][i]=tmp_column_out[j];
	   }  
	}
	free(tmp_lastrow);

}

void CTestView::inverse_wavelet_transform_overlap(int **dst, const int width, const int height, const int levels)
{
	int l;
	int* tmp_lastrow=(int*)malloc((width+height+height)*sizeof(int));
	int* tmp_column_in=tmp_lastrow+width;
	int* tmp_column_out=tmp_lastrow+width+height;

	for(l=levels-1;l>=0;l--)
	{  int w=width>>l;

⌨️ 快捷键说明

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