www.pudn.com > SobelKirsch.rar > SobelKirschView.cpp


// SobelKirschView.cpp : implementation of the CSobelKirschView class 
// 
 
#include "stdafx.h" 
#include "SobelKirsch.h" 
 
#include "SobelKirschDoc.h" 
#include "SobelKirschView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#include "SpecialDetectionFilter.h" 
 
///////////////////////////////////////////////////////////////////////////// 
// CSobelKirschView 
 
IMPLEMENT_DYNCREATE(CSobelKirschView,  CView) 
 
BEGIN_MESSAGE_MAP(CSobelKirschView,  CView) 
	//{{AFX_MSG_MAP(CSobelKirschView) 
	ON_COMMAND(IDM_SOBEL_DETECT,  OnSobelDetect) 
	ON_UPDATE_COMMAND_UI(IDM_SOBEL_DETECT,  OnUpdateSobelDetect) 
	ON_COMMAND(IDM_KIRSCH_DETECT,  OnKirschDetect) 
	ON_UPDATE_COMMAND_UI(IDM_KIRSCH_DETECT,  OnUpdateKirschDetect) 
	ON_COMMAND(IDM_IMAGE_RESTORE,  OnImageRestore) 
	ON_UPDATE_COMMAND_UI(IDM_IMAGE_RESTORE,  OnUpdateImageRestore) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSobelKirschView construction/destruction 
 
CSobelKirschView::CSobelKirschView() 
{ 
	// TODO: add construction code here 
	m_nWidth = 800; 
	m_nHeight = 600; 
	m_dwOperation = 0L; 
 
} 
 
CSobelKirschView::~CSobelKirschView() 
{ 
} 
 
BOOL CSobelKirschView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSobelKirschView drawing 
 
void CSobelKirschView::OnDraw(CDC* pDC) 
{ 
	CSobelKirschDoc* 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 
			Detect(pDC, pDib, 0, 0, m_nWidth, m_nHeight); 
		 
		memDC.SelectObject(pOldBitmap); 
		ddb.DeleteObject(); 
		EndWaitCursor(); 
	} 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CSobelKirschView diagnostics 
 
#ifdef _DEBUG 
void CSobelKirschView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CSobelKirschView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CSobelKirschDoc* CSobelKirschView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSobelKirschDoc))); 
	return (CSobelKirschDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CSobelKirschView message handlers 
 
void CSobelKirschView::OnSobelDetect()  
{ 
	// TODO: Add your command handler code here 
	m_dwOperation = IMAGE_SOBEL_EDGE_DETECT; 
	Invalidate();	 
} 
 
void CSobelKirschView::OnUpdateSobelDetect(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	if(m_dwOperation == IMAGE_SOBEL_EDGE_DETECT) 
		pCmdUI ->SetCheck(TRUE); 
	else 
		pCmdUI ->SetCheck(FALSE);		 
} 
 
void CSobelKirschView::OnKirschDetect()  
{ 
	// TODO: Add your command handler code here 
	m_dwOperation = IMAGE_KIRSCH_EDGE_DETECT; 
	Invalidate(); 
} 
 
void CSobelKirschView::OnUpdateKirschDetect(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	if(m_dwOperation == IMAGE_KIRSCH_EDGE_DETECT) 
		pCmdUI ->SetCheck(TRUE); 
	else 
		pCmdUI ->SetCheck(FALSE);		 
} 
 
void CSobelKirschView::OnImageRestore()  
{ 
	// TODO: Add your command handler code here 
	m_dwOperation = 0L; 
	Invalidate();	 
} 
 
void CSobelKirschView::OnUpdateImageRestore(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	if(m_dwOperation == 0L) 
		pCmdUI ->SetCheck(TRUE); 
	else 
		pCmdUI ->SetCheck(FALSE);	 
} 
 
void CSobelKirschView::Detect(CDC *pDC,  CDib *pDib,  int x,  int y,  int nWidth,  int nHeight) 
{ 
	ASSERT(pDib); 
		//限制处理的区域----防止任意填写数据而使用访问无效 
	if((x > (m_nWidth - 1)) || (y > (m_nHeight - 1))) return ; 
	//实际处理的宽度和高度 
	int w = min(nWidth, m_nWidth - x); 
	int h = min(nHeight, m_nHeight - y); 
	 
	// 32位颜色数据 
	DWORD dwSize = m_nWidth * m_nHeight * 4; 
 
	//分配全局内存 32位源数据 
	BYTE* pbyBits32 =  new BYTE[dwSize]; 
	if(pbyBits32 == 0) return; 
	memset(pbyBits32, 0, dwSize); 
 
	//CDib类只提供了一个全部数据的函数 
	pDib->GetDdbData32(pbyBits32); 
 
	//特殊检测器 
	CSpecialDetectionFilter* sdf = new CSpecialDetectionFilter();	//No 11 
	 
	//获取数据后, 其基本参考点变为(0, 0). 
	sdf->SetOperation(m_dwOperation);								//No 12 
	sdf->Filter(pbyBits32, x, y, w, h, m_nWidth, m_nHeight);				//No 13 
	sdf->Binchange(pbyBits32, x, y, m_nWidth, m_nHeight, m_nWidth, m_nHeight, 55); 
 
	CClientDC dc(this); 
	CDC memDC; 
	memDC.CreateCompatibleDC(&dc); 
	HBITMAP hBitmap = sdf->CreateDdb(pDC->m_hDC, m_nWidth, m_nHeight, pbyBits32); //No 17 
 
	HBITMAP hOldBitmap = (HBITMAP)memDC.SelectObject(hBitmap); 
	pDC->BitBlt(0, 0, m_nWidth, m_nHeight, &memDC, 0, 0, SRCCOPY); 
	::DeleteObject(hBitmap); 
	memDC.SelectObject(hOldBitmap); 
 
	delete sdf; 
	delete[] pbyBits32; 
	ReleaseDC(&dc); 
 
}