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

📄 imgmirrorview.cpp

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

#include "stdafx.h"
#include "ImgMirror.h"

#include "ImgMirrorDoc.h"
#include "ImgMirrorView.h"

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

#include "ImageGeometry.h"

/////////////////////////////////////////////////////////////////////////////
// CImgMirrorView

IMPLEMENT_DYNCREATE(CImgMirrorView,  CView)

BEGIN_MESSAGE_MAP(CImgMirrorView,  CView)
	//{{AFX_MSG_MAP(CImgMirrorView)
	ON_COMMAND(IDM_MIRROR_X,  OnMirrorX)
	ON_UPDATE_COMMAND_UI(IDM_MIRROR_X,  OnUpdateMirrorX)
	ON_COMMAND(IDM_MIRROR_Y,  OnMirrorY)
	ON_UPDATE_COMMAND_UI(IDM_MIRROR_Y,  OnUpdateMirrorY)
	ON_COMMAND(IDM_IMAGE_RESTORE,  OnImageRestore)
	ON_UPDATE_COMMAND_UI(IDM_IMAGE_RESTORE,  OnUpdateImageRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgMirrorView construction/destruction

CImgMirrorView::CImgMirrorView()
{
	// TODO: add construction code here
	m_nWidth = 800;
	m_nHeight = 600;
	m_dwOperation = 0L;
}

CImgMirrorView::~CImgMirrorView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CImgMirrorView drawing

void CImgMirrorView::OnDraw(CDC* pDC)
{
	CImgMirrorDoc* 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_dwOperation == 0L)
			pDC->BitBlt(0, 0, m_nWidth, m_nHeight, &memDC, 0, 0, SRCCOPY);
		else
			Mirror(pDC, pDib, 0, 0, m_nWidth, m_nHeight);
		
		memDC.SelectObject(pOldBitmap);
		ddb.DeleteObject();
		EndWaitCursor();
	}

}

/////////////////////////////////////////////////////////////////////////////
// CImgMirrorView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CImgMirrorView message handlers

void CImgMirrorView::OnMirrorX() 
{
	// TODO: Add your command handler code here
	m_dwOperation = IMAGE_GEOMETRY_MIRROR_X;
	Invalidate();
}

void CImgMirrorView::OnUpdateMirrorX(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_dwOperation == IMAGE_GEOMETRY_MIRROR_X)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}

void CImgMirrorView::OnMirrorY() 
{
	// TODO: Add your command handler code here
	m_dwOperation = IMAGE_GEOMETRY_MIRROR_Y;
	Invalidate();
	
}

void CImgMirrorView::OnUpdateMirrorY(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_dwOperation == IMAGE_GEOMETRY_MIRROR_Y)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}

void CImgMirrorView::OnImageRestore() 
{
	// TODO: Add your command handler code here
	m_dwOperation = 0L;
	Invalidate();
}

void CImgMirrorView::OnUpdateImageRestore(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(m_dwOperation == 0L)
		pCmdUI ->SetCheck(TRUE);
	else
		pCmdUI ->SetCheck(FALSE);	
}

void CImgMirrorView::Mirror(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;


	//分配目标缓冲区
	DWORD dwSizeDst = ((DWORD)(nWidthDst * nHeightDst)) * 4;
	
	BYTE* pbyBitsDst32 =  new BYTE[dwSizeDst];
	if(pbyBitsDst32 == NULL) return;
	
	CImageGeometry* ig = new CImageGeometry();
	if(m_dwOperation == IMAGE_GEOMETRY_MIRROR_X)
		ig->MirrorX(pbyBitsSrc32, x, y, nWidth, nHeight, m_nWidth, m_nHeight, pbyBitsDst32);
	else if(m_dwOperation == IMAGE_GEOMETRY_MIRROR_Y)
		ig->MirrorY(pbyBitsSrc32, x, y, nWidth, nHeight, m_nWidth, m_nHeight, pbyBitsDst32);

	CClientDC dc(this);
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	HBITMAP hBitmap = ig->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 ig;
	delete[] pbyBitsSrc32;
	delete[] pbyBitsDst32;

	ReleaseDC(&dc);

}

⌨️ 快捷键说明

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