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

📄 imgwarpview.cpp

📁 visual c++数字图像与图形处理中的光盘内容
💻 CPP
字号:
// ImgWarpView.cpp : implementation of the CImgWarpView class
//

#include "stdafx.h"
#include "ImgWarp.h"

#include "ImgWarpDoc.h"
#include "ImgWarpView.h"

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

#include "ImageWarp.h"
#include "ImageWarpDialog.h"

/////////////////////////////////////////////////////////////////////////////
// CImgWarpView

IMPLEMENT_DYNCREATE(CImgWarpView,  CView)

BEGIN_MESSAGE_MAP(CImgWarpView,  CView)
	//{{AFX_MSG_MAP(CImgWarpView)
	ON_COMMAND(IDM_WARP,  OnWarp)
	ON_UPDATE_COMMAND_UI(IDM_WARP,  OnUpdateWarp)
	ON_COMMAND(IDM_IMAGE_RESTORE,  OnImageRestore)
	ON_UPDATE_COMMAND_UI(IDM_IMAGE_RESTORE,  OnUpdateImageRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgWarpView construction/destruction

CImgWarpView::CImgWarpView()
{
	// TODO: add construction code here
	m_nWidth = 800;
	m_nHeight = 600;
	m_nOperation = 0;
	m_bWarp = FALSE;
	m_nQuality = 0;

	m_fAmplitude = 20.0f;
	m_fPeriod = 18.0f;
	m_fPhase = 0.0f;
}

CImgWarpView::~CImgWarpView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CImgWarpView drawing

void CImgWarpView::OnDraw(CDC* pDC)
{
	CImgWarpDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CDib* pDib = pDoc->m_pDib;
	if(pDib)
	{
		BeginWaitCursor();
		m_nWidth = (int)pDib->GetWidth();
		m_nHeight = (int)pDib->GetHeight(); 

		CDC memDC;
		memDC.CreateCompatibleDC(pDC);
		CBitmap ddb;
		ddb.CreateCompatibleBitmap(pDC, m_nWidth, m_nHeight);
		CBitmap* pOldBitmap = memDC.SelectObject(&ddb);

		pDib->Draw(memDC.m_hDC, 0, 0, m_nWidth, m_nHeight, 
					0, 0, m_nWidth, m_nHeight, DIB_RGB_COLORS, SRCCOPY);
		
		if(!m_bWarp)
			pDC->BitBlt(0, 0, m_nWidth, m_nHeight, &memDC, 0, 0, SRCCOPY);
		else
			Warp(pDC, pDib, 0, 0, m_nWidth, m_nHeight);
		
		memDC.SelectObject(pOldBitmap);
		ddb.DeleteObject();
		EndWaitCursor();
	}

}

/////////////////////////////////////////////////////////////////////////////
// CImgWarpView diagnostics

#ifdef _DEBUG
void CImgWarpView::AssertValid() const
{
	CView::AssertValid();
}

void CImgWarpView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CImgWarpView message handlers

void CImgWarpView::OnWarp() 
{
	// TODO: Add your command handler code here
	CImageWarpDialog dlg;
	
	dlg.m_fAmplitude = m_fAmplitude;
	dlg.m_fPeriod = m_fPeriod;
	dlg.m_fPhase = m_fPhase;	

	dlg.m_nQuality = m_nQuality;
	dlg.m_nOperation = m_nOperation;


	int responeDlg = dlg.DoModal();
	if(responeDlg == IDOK)
	{
		m_fAmplitude = dlg.m_fAmplitude;
		m_fPeriod = dlg.m_fPeriod;
		m_fPhase = dlg.m_fPhase;	

		m_nQuality = dlg.m_nQuality;
		m_nOperation = dlg.m_nOperation;
		m_bWarp = TRUE;
		Invalidate();
	}
}

void CImgWarpView::OnUpdateWarp(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_bWarp)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	

}


void CImgWarpView::OnImageRestore() 
{
	// TODO: Add your command handler code here
	m_bWarp = FALSE;
	Invalidate();

}

void CImgWarpView::OnUpdateImageRestore(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(!m_bWarp)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}

void CImgWarpView::Warp(CDC *pDC,  CDib *pDib,  int x,  int y,  int nWidth,  int nHeight)
{
	ASSERT(pDib);

	//限制处理的区域----防止任意填写数据而使用访问无效
	if((x > (m_nWidth - 1)) || (y > (m_nHeight - 1))) return ;
	//实际处理的宽度和高度
	if(nWidth >(m_nWidth - x))
	{
		MessageBox("区域宽度越界");
		return;
	}
	if(nHeight > (m_nHeight - y))
	{
		MessageBox("区域高度越界");
		return;
	}
	
	// 32位颜色数据:输入源
	DWORD dwSizeSrc = m_nWidth * m_nHeight * 4;

	//分配全局内存 32位源数据
	BYTE* pbyBitsSrc32 =  new BYTE[dwSizeSrc];
	if(pbyBitsSrc32 == NULL) return;
	memset(pbyBitsSrc32, 0, dwSizeSrc);


	//CDib类只提供了一个全部数据的函数
	pDib->GetDdbData32(pbyBitsSrc32);

	int nWidthDst = nWidth; 
	int nHeightDst = nHeight;

	//warp x
	if(m_nOperation == 0)
		nWidthDst += 2 * (int)ABS(m_fAmplitude);
	//warp y
	else
		nHeightDst += 2 * (int)ABS(m_fAmplitude);

	//分配目标缓冲区
	DWORD dwSizeDst = ((DWORD)(nWidthDst * nHeightDst)) * 4;
	
	BYTE* pbyBitsDst32 =  new BYTE[dwSizeDst];
	if(pbyBitsDst32 == NULL) return;
	memset(pbyBitsDst32, 255,  dwSizeDst);

	CImageWarp* iw = new CImageWarp();

	//设置插值质量
	if(m_nQuality == 0)
		iw->SetQuality( IMAGE_GEOMETRY_NEAREST_NEIGHBOR_INTERPOLATE );
	else if(m_nQuality == 1)
		iw->SetQuality( IMAGE_GEOMETRY_BILINEAR_INTERPOLATE );
	else if(m_nQuality == 2)
		iw->SetQuality( IMAGE_GEOMETRY_THREE_ORDER_INTERPOLATE );

	iw->SetParam4f(m_fAmplitude, m_fPeriod, m_fPhase, ABS(m_fAmplitude));
	DWORD dwOperation;
	if(m_nOperation == 0)
	{
		dwOperation = IMAGE_GEOMETRY_WARP_X;
		iw->SetOperation(dwOperation);
		iw->Transform(pbyBitsSrc32, x, y, nWidth, nHeight, m_nWidth, m_nHeight, pbyBitsDst32, nWidthDst, nHeightDst);
	}
	else if(m_nOperation == 1)
	{
		dwOperation = IMAGE_GEOMETRY_WARP_Y;
		iw->SetOperation(dwOperation);
		iw->Transform(pbyBitsSrc32, x, y, nWidth, nHeight, m_nWidth, m_nHeight, pbyBitsDst32, nWidthDst, nHeightDst);
	}

	CClientDC dc(this);
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	HBITMAP hBitmap = iw->CreateDdb(pDC->m_hDC, nWidthDst, nHeightDst, pbyBitsDst32);
	HBITMAP hOldBitmap = (HBITMAP)memDC.SelectObject(hBitmap);
	pDC->BitBlt(0, 0, nWidthDst, nHeightDst, &memDC, 0, 0, SRCCOPY);
	::DeleteObject(hBitmap);
	memDC.SelectObject(hOldBitmap);

	delete iw;
	delete[] pbyBitsDst32;
	delete[] pbyBitsSrc32;
	ReleaseDC(&dc);
}

⌨️ 快捷键说明

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