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

📄 wavelet2ddlg.cpp

📁 二维小波变换算法实现压缩解压的程序
💻 CPP
字号:
// Wavelet2DDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Wavelet2D.h"
#include "Wavelet2DDlg.h"
#include "LiftDwt97.h"
#include "IntDwt97.h"
#include "IntDwt53.h"
#include "Dwt.h"
#include "BinDwt97.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WIDTHBYTES(i,j) ((i*j+31)/32*4)
#define COLORNUMS(i) (1<<i)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWavelet2DDlg dialog

CWavelet2DDlg::CWavelet2DDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWavelet2DDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWavelet2DDlg)
	m_Level = _T("1");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON);
}

void CWavelet2DDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWavelet2DDlg)
	DDX_Control(pDX, IDC_EDIT1, m_Bmpfile);
	DDX_Control(pDX, IDC_COMBO1, m_SelectWavelet);
	DDX_CBString(pDX, IDC_COMBO2, m_Level);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWavelet2DDlg, CDialog)
	//{{AFX_MSG_MAP(CWavelet2DDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_WAVELET2D, OnWavelet2D)
	ON_BN_CLICKED(IDC_INVERSEWAVELET2D, OnInverseWavelet2D)
	ON_BN_CLICKED(IDC_READFILE, OnReadfile)
	ON_BN_CLICKED(IDC_OPENIMAGE, OnBrowseBmpFile)
	ON_BN_CLICKED(IDC_COPYRIGHT, OnCopyright)
	ON_BN_CLICKED(IDC_CANCEL, OnCancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWavelet2DDlg message handlers

BOOL CWavelet2DDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    m_lpBuf=NULL;
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
    //初始化控件
	   
	    m_Bmpfile.SetWindowText(m_strBmpFile);
	   			
		m_SelectWavelet.InsertString(0,"9/7浮点小波分解");
		m_SelectWavelet.SetItemData(0,1);
		m_SelectWavelet.InsertString(1,"5/3整数小波分解");
		m_SelectWavelet.SetItemData(1,2);
    	m_SelectWavelet.InsertString(2,"9/7整数小波分解");
		m_SelectWavelet.SetItemData(2,3);
        m_SelectWavelet.InsertString(3,"9/7提升小波分解");
		m_SelectWavelet.SetItemData(3,4);
		m_SelectWavelet.InsertString(4,"9/7二进制小波分解");
		m_SelectWavelet.SetItemData(4,5);
		m_SelectWavelet.SetCurSel(0);
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWavelet2DDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWavelet2DDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWavelet2DDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CWavelet2DDlg::OnWavelet2D() 
{
	// TODO: Add your control notification handler code here
    
	UpdateData(TRUE);
	
	nIndex = m_SelectWavelet.GetCurSel();   //获得控件变量的值
    level=atoi(m_Level);                    //将字符串变量转化成整形变量
    image1=new int[iwidth * iheight];
	image2=new double[iwidth * iheight];

    switch(nIndex)
	{
	   case CB_ERR:
		   {
	           FLOATDWT.Dwt2D(data2,iheight,iwidth,level);    //9/7浮点小波分解
			   memcpy(image2,data2,iwidth*iheight*8);
	           uint8(image2,iheight,iwidth);                  //图像归一化
               ShowImage2(iwidth,iheight,image2);
	           break;
		   }
    
        case 0:    
		   {
	           FLOATDWT.Dwt2D(data2,iheight,iwidth,level);    //9/7浮点小波分解
               memcpy(image2,data2,iwidth*iheight*8);
	           uint8(image2,iheight,iwidth);
               ShowImage2(iwidth,iheight,image2);
	           break;
		   }
    
	   case 1:
		   { 
	           INTDWT53.Dwt2D(data1, iheight,iwidth,level);   //5/3整数小波分解
			   memcpy(image1,data1,iwidth*iheight*4);
               uint8(image1,iheight,iwidth);
               ShowImage1(iwidth,iheight,image1);
	           break;
		   }

       case 2:
		   {
	           INTDWT97.Dwt2D(data1, iheight,iwidth,level);   //9/7整数小波分解
               memcpy(image1,data1,iwidth*iheight*4);
			   uint8(image1,iheight,iwidth);             
               ShowImage1(iwidth,iheight,image1);
	           break;
		   }
    
	   case 3:
		   {
	           LIFTNSDWT.Dwt2D(data2, iheight,iwidth,level);  //9/7提升小波分解
			   memcpy(image2,data2,iwidth*iheight*8);
               uint8(image2,iheight,iwidth);
               ShowImage2(iwidth,iheight,image2);
	           break;
		   }
    
	   case 4:
		   {
	           BINDWT.Dwt2D(data1, iheight,iwidth,level);     //9/7二进制小波分解
			   memcpy(image1,data1,iwidth*iheight*4);
               uint8(image1,iheight,iwidth);
               ShowImage1(iwidth,iheight,image1);
	           break;
		   }
	}
	
	UpdateData(FALSE);
	
}


void CWavelet2DDlg::uint8(int *Imagebuffer, int height, int width)
{
	int i,j,max,min;
	
	//搜索最大值和最小值
	max=min=0;
	for(i=0;i<height;i++)
	  for(j=0;j<width;j++)
	  {
		if (*(Imagebuffer+i*width+j)>max)
	    	  max=*(Imagebuffer+i*width+j);
		if (*(Imagebuffer+i*width+j)<min)
		      min=*(Imagebuffer+i*width+j);
	  }
    
	//进行规一化
    for(i=0;i<iheight;i++)
	    for(j=0;j<iwidth;j++)
         *(Imagebuffer+i*iwidth+j)=(*(Imagebuffer+i*iwidth+j)*255)/(max-min)+128;
	
	  
	//像素幅值控制
	for(i=0;i<height;i++)
    	for(j=0;j<width;j++)
		{
           if(*(Imagebuffer+i*width+j)>255)
		    *(Imagebuffer+i*width+j)=255;
		   if(*(Imagebuffer+i*width+j)<0)
			*(Imagebuffer+i*width+j)=0;	
		}

}

void CWavelet2DDlg::uint8(double *Imagebuffer, int height, int width)
{
	int i,j;
	double max,min;
	max=min=0;
	
	//搜索最大值和最小值
	for(i=0;i<height;i++)
	  for(j=0;j<width;j++)
	  {
		if (*(Imagebuffer+i*width+j)>max)
	    	  max=*(Imagebuffer+i*width+j);
		if (*(Imagebuffer+i*width+j)<min)
		      min=*(Imagebuffer+i*width+j);
	  }

    //进行规一化
	  for(i=0;i<iheight;i++)
	    for(j=0;j<iwidth;j++)
         *(Imagebuffer+i*iwidth+j)=(*(Imagebuffer+i*iwidth+j)*255)/(max-min)+128;

	//像素幅值控制
	for(i=0;i<height;i++)
    	for(j=0;j<width;j++)
		{
           if(*(Imagebuffer+i*width+j)>255)
		    *(Imagebuffer+i*width+j)=255;
		   if(*(Imagebuffer+i*width+j)<0)
			*(Imagebuffer+i*width+j)=0;	
		}

}

void CWavelet2DDlg::OnCancel() 
{
	// TODO: Add your control notification handler code here
/*	delete []data1;
	delete []data2;
	delete []image1;
	delete []image2;
  	delete []m_lpBuf;
 */	
	OnOK();
	
}

void CWavelet2DDlg::OnInverseWavelet2D() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	
	switch(nIndex)
	{
	   case CB_ERR:
		   {
        	   FLOATDWT.IDwt2D(data2,iheight,iwidth,level);
			   uint8(data2,iheight,iwidth);
               ShowImage2(iwidth,iheight,data2);
			   break;
		   }
    
	   case 0:
		   {
	           FLOATDWT.IDwt2D(data2,iheight,iwidth,level);
			   uint8(data2,iheight,iwidth);
               ShowImage2(iwidth,iheight,data2);
			   break;
		   }

       case 1:
		   {
	           INTDWT53.IDwt2D(data1, iheight,iwidth,level);
			   uint8(data1,iheight,iwidth);
               ShowImage1(iwidth,iheight,data1);
			   break;
		   }

       case 2:
		   {
	           INTDWT97.IDwt2D(data1, iheight,iwidth,level);
			   uint8(data1,iheight,iwidth);
               ShowImage1(iwidth,iheight,data1);
			   break;
		   }
    
	   case 3:
		   {
	           LIFTNSDWT.IDwt2D(data2, iheight,iwidth,level);
			   uint8(data2,iheight,iwidth);
               ShowImage2(iwidth,iheight,data2);
			   break;
		   }
    
	   case 4:
		   {
	           BINDWT.IDwt2D(data1, iheight,iwidth,level);
			   uint8(data1,iheight,iwidth);
               ShowImage1(iwidth,iheight,data1);
			   break;
		   }
	}
	
	   UpdateData(FALSE);
}

void CWavelet2DDlg::OnReadfile() 
{
	// TODO: Add your control notification handler code here
	CFile file;
	if(!file.Open(strPathname,CFile::modeRead))
	{
		MessageBox("请先选择位图文件路径!");
		return;
	}
 
	BITMAPFILEHEADER bmf;
	if(file.Read(&bmf,sizeof(bmf))!=sizeof(bmf))
	{
		file.Close();
		return;
	}
	if(bmf.bfType!=0x4d42)
	{
		file.Close();
		return;
	}
    if(file.Read((LPSTR)&m_bmi,sizeof(m_bmi))!=sizeof(m_bmi))
	{
		file.Close();
		return;
	}
	if(m_bmi.biBitCount!=8)
	{
		MessageBox("只对8位灰度图进行小波变换");
		file.Close();
		return;
	}
	if(m_bmi.biHeight>256)
	{
		MessageBox("只处理256*256图");
		file.Close();
		return;
	}

	int nHeapSize=sizeof(m_bmi)+COLORNUMS(m_bmi.biBitCount)*sizeof(RGBQUAD)+
		          WIDTHBYTES(m_bmi.biWidth,m_bmi.biBitCount)*m_bmi.biHeight;
	if(m_lpBuf!=NULL)
	{
		delete m_lpBuf;
		m_lpBuf=NULL;
	}
	m_lpBuf=new BYTE[nHeapSize];
    memcpy(m_lpBuf,&m_bmi,sizeof(m_bmi));
	if(file.Read(m_lpBuf+sizeof(m_bmi),nHeapSize-sizeof(m_bmi))!=(nHeapSize-sizeof(m_bmi)))
	{
		delete m_lpBuf;
		m_lpBuf=NULL;
		file.Close();
		return;
	}
	
	file.Close();
	lpDibBits=m_lpBuf+sizeof(m_bmi)+COLORNUMS(m_bmi.biBitCount)*sizeof(RGBQUAD);//数据区
	lpImageBuffer=m_lpBuf+sizeof(m_bmi)+COLORNUMS(m_bmi.biBitCount)*sizeof(RGBQUAD);
	CDC* pDC;
	pDC=GetDC();
	SetStretchBltMode(pDC->m_hDC,COLORONCOLOR);
	StretchDIBits(pDC->m_hDC,54,33,256,256,0,0,m_bmi.biWidth,m_bmi.biHeight,lpDibBits,(LPBITMAPINFO)m_lpBuf,DIB_RGB_COLORS,SRCCOPY);
	ReleaseDC(pDC);
    
	
	iwidth=m_bmi.biWidth;
    iheight=m_bmi.biHeight;
	
	data1=new int[iwidth * iheight];
	data2=new double[iwidth * iheight];
		
	for(int i=0;i<iheight;i++)
		for(int j=0;j<iwidth;j++)
		{
		  data1[i*iwidth+j]=GetXY(i,j)-128;
		  data2[i*iwidth+j]=GetXY(i,j)-128;
		}
	m_hasopened=TRUE;
}

void CWavelet2DDlg::OnBrowseBmpFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fileopenbox(TRUE,NULL,NULL,OFN_HIDEREADONLY,"files(*.bmp)|*.bmp|",NULL);	
	if(fileopenbox.DoModal()!=IDOK) return;
	strPathname=fileopenbox.GetPathName();         //获得位图像文件的当前路径
	m_strBmpFile=strPathname;
	m_Bmpfile.SetWindowText(m_strBmpFile);
}

void CWavelet2DDlg::OnCopyright() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();	
}



BYTE CWavelet2DDlg::GetXY(int x, int y)
{
   y=m_bmi.biWidth-y-1;
   return lpDibBits[y*m_bmi.biWidth+x];

}



void CWavelet2DDlg::SetXY(int x, int y, BYTE val)
{
	y=m_bmi.biWidth-y-1;
   lpImageBuffer[y*m_bmi.biWidth+x]=val;

}



void CWavelet2DDlg::ShowImage1(int nWidth, int nHeight, int *pImageBuffer)
{
	m_bmi.biWidth=nWidth;
	m_bmi.biHeight=nHeight;
    
	for(int i=0;i<iheight;i++)
	{
		for(int j=0;j<iwidth;j++)
		{
			SetXY(i,j,(BYTE)pImageBuffer[i*iwidth+j]);
		}
	}
	
	CDC* pDC;
	pDC=GetDC();
	SetStretchBltMode(pDC->m_hDC,COLORONCOLOR);
	StretchDIBits(pDC->m_hDC,54,312,256,256,0,0,m_bmi.biWidth,m_bmi.biHeight,lpImageBuffer,(LPBITMAPINFO)m_lpBuf,DIB_RGB_COLORS,SRCCOPY);
	ReleaseDC(pDC);
}

void CWavelet2DDlg::ShowImage2(int nWidth, int nHeight, double *pImageBuffer)
{ 
	m_bmi.biWidth=nWidth;
	m_bmi.biHeight=nHeight;
    
	for(int i=0;i<iheight;i++)
	{
		for(int j=0;j<iwidth;j++)
		{
			SetXY(i,j,(BYTE)pImageBuffer[i*iwidth+j]);
		}
	}
	
	CDC* pDC;
	pDC=GetDC();
	SetStretchBltMode(pDC->m_hDC,COLORONCOLOR);
	StretchDIBits(pDC->m_hDC,54,312,256,256,0,0,m_bmi.biWidth,m_bmi.biHeight,lpImageBuffer,(LPBITMAPINFO)m_lpBuf,DIB_RGB_COLORS,SRCCOPY);
	ReleaseDC(pDC);

}




⌨️ 快捷键说明

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