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

📄 ed256view.cpp

📁 编程实现边界区分
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////
// CED256View class (17/10/2000)
// Author: James Matthews
//
// Implements a simple edge detection algorithm and 
// prototyping system.
//
// Written for Generation5 (http://www.generation5.org/)
//
// See http://www.generation5.org/vision.shtml
//     http://www.generation5.org/edgedetect.shtml for details.
//

#include "stdafx.h"
#include "ED256.h"

#include "ED256Doc.h"
#include "ED256View.h"
#include "Mainfrm.h"

#include <math.h>
#include <memory.h>

#include "FilterDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CED256View

IMPLEMENT_DYNCREATE(CED256View, CView)

BEGIN_MESSAGE_MAP(CED256View, CView)
	//{{AFX_MSG_MAP(CED256View)
	ON_COMMAND(ID_EDIT_EDGEDETECT, OnEditEdgeDetect)
	ON_COMMAND(ID_COLOURTYPE, OnColourType)
	ON_COMMAND(ID_FORMULA, OnFormula)
	ON_COMMAND(ID_EDIT_FORMULATYPE_DIFFERENCEAND, OnEditFormulatypeDifferenceand)
	ON_COMMAND(ID_EDIT_FORMULATYPE_DIFFERENCEOR, OnEditFormulatypeDifferenceor)
	ON_COMMAND(ID_EDIT_FORMULATYPE_SUMMATION, OnEditFormulatypeSummation)
	ON_COMMAND(ID_INVERSE, OnInverse)
	ON_COMMAND(ID_FILTER, OnFilter)
	ON_UPDATE_COMMAND_UI(ID_COLOURTYPE, OnUpdateColourtype)
	ON_UPDATE_COMMAND_UI(ID_INVERSE, OnUpdateInverse)
	ON_COMMAND(ID_EDIT_OUTPUT_BLACKANDWHITE, OnEditOutputBlackandwhite)
	ON_COMMAND(ID_EDIT_OUTPUT_COLOUR, OnEditOutputColour)
	ON_UPDATE_COMMAND_UI(ID_EDIT_OUTPUT_BLACKANDWHITE, OnUpdateEditOutputBlackandwhite)
	ON_UPDATE_COMMAND_UI(ID_EDIT_OUTPUT_COLOUR, OnUpdateEditOutputColour)
	ON_UPDATE_COMMAND_UI(ID_EDIT_FORMULATYPE_DIFFERENCEAND, OnUpdateEditFormulatypeDifferenceand)
	ON_UPDATE_COMMAND_UI(ID_EDIT_FORMULATYPE_DIFFERENCEOR, OnUpdateEditFormulatypeDifferenceor)
	ON_UPDATE_COMMAND_UI(ID_EDIT_FORMULATYPE_SUMMATION, OnUpdateEditFormulatypeSummation)
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_EDIT_PROTOTYPING, OnEditPrototyping)
	ON_COMMAND(ID_CHANGERESOLUTION, OnChangeResolution)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_UPDATE_COMMAND_UI(ID_RES_LOW, OnUpdateResLow)
	ON_UPDATE_COMMAND_UI(ID_RES_MEDIUM, OnUpdateResMedium)
	ON_UPDATE_COMMAND_UI(ID_RES_HIGH, OnUpdateResHigh)
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(ID_RES_LOW, ID_RES_HIGH, OnResolution)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CED256View construction/destruction

CED256View::CED256View() {
	m_iFormula = 0;
	m_iResolution = 4;
	m_bColour = true;
	m_bInverse = false;

	m_crPlot = RGB(0,0,0);
	m_crNoPlot = RGB(255,255,255);

	m_fFilter[0] = 1/6.0f;
	m_fFilter[1] = 2/3.0f;
	m_fFilter[2] = 1/6.0f;
	m_fFilter[3] = 2/3.0f;
	m_fFilter[4] = -10/3.0f;
	m_fFilter[5] = 2/3.0f;
	m_fFilter[6] = 1/6.0f;
	m_fFilter[7] = 2/3.0f;
	m_fFilter[8] = 1/6.0f;

	m_crProtoColours[0] = RGB(255,0,0);
	m_crProtoColours[1] = RGB(128,0,0);
	m_crProtoColours[2] = RGB(0,255,0);
	m_crProtoColours[3] = RGB(0,128,0);
	m_crProtoColours[4] = RGB(0,0,255);
	m_crProtoColours[5] = RGB(0,0,128);
	m_crProtoColours[6] = RGB(255,0,255);
	m_crProtoColours[7] = RGB(128,0,128);
	m_crProtoColours[8] = RGB(0,0,0);
	m_crProtoColours[9] = RGB(255,255,255);
}

CED256View::~CED256View() {}

BOOL CED256View::PreCreateWindow(CREATESTRUCT& cs) {
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CED256View drawing

void CED256View::OnDraw(CDC* pDC) {
	CED256Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CRect rect;
	GetClientRect(&rect);

	CDC dc;
	CBitmap bmp, *oldbmp;
	dc.CreateCompatibleDC(pDC);
	bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	oldbmp = dc.SelectObject(&bmp);
	
	dc.FillSolidRect(rect, RGB(255,255,255));

	int cxDIB = 0, cyDIB = 0;
	HDIB hDIB = pDoc->GetHDIB();
	if (hDIB) {
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
		cxDIB = (int) ::DIBWidth(lpDIB);
		cyDIB = (int) ::DIBHeight(lpDIB);
		::GlobalUnlock((HGLOBAL) hDIB);
		CRect rcDIB;
		rcDIB.top = rcDIB.left = 0;
		rcDIB.right = cxDIB;
		rcDIB.bottom = cyDIB;
		CRect rcDest = rcDIB;
		
		if (cxDIB * 2 <= rect.Width() || !(HBITMAP(m_bmpEdges))) {
			::PaintDIB(dc.m_hDC, &rcDest, hDIB,
				&rcDIB, pDoc->GetDocPalette());
		}
	}

	if ((HBITMAP)(m_bmpEdges)) {
		CDC cdc;
		CBitmap *oldbmp;

		cdc.CreateCompatibleDC(pDC);
		oldbmp = cdc.SelectObject(&m_bmpEdges);
		
		if (cxDIB * 2 < rect.Width()) {
			dc.BitBlt(cxDIB+1,0,cxDIB,cyDIB+m_iOffset,&cdc,0,0,SRCCOPY);
		} else {
			dc.BitBlt(0,0,cxDIB,cyDIB+m_iOffset,&cdc,0,0,SRCCOPY);
		}
	}

	pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);
	dc.SelectObject(oldbmp);
}

/////////////////////////////////////////////////////////////////////////////
// CED256View diagnostics

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

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

CED256Doc* CED256View::GetDocument() {
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CED256Doc)));
	return (CED256Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CED256View message handlers

void CED256View::OnEditEdgeDetect() {
	CWaitCursor wait;
	
	CMainFrame* pMfm = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());

	m_iOffset = 0;
	int thresh = pMfm->m_iThreshold;
	
	CDC sc1,sc2; 
	CClientDC dc(this);

	sc1.CreateCompatibleDC(&dc);
	sc2.CreateCompatibleDC(&dc);

	CBitmap *src1old, bmsrc, *src2old;

	int cxDIB, cyDIB;

	CED256Doc* pDoc = GetDocument();
	HDIB hDIB = pDoc->GetHDIB();

	GetDIBSize(cxDIB, cyDIB);

	if ((HBITMAP)(m_bmpEdges)) m_bmpEdges.Detach();
	
	bmsrc.CreateCompatibleBitmap(&dc, cxDIB, cyDIB);
	m_bmpEdges.CreateCompatibleBitmap(&dc, cxDIB, cyDIB);
	   
	src1old = sc1.SelectObject(&bmsrc);
	src2old = sc2.SelectObject(&m_bmpEdges);

	::PaintDIB(sc1.m_hDC, &CRect(0,0,cxDIB,cyDIB), hDIB,
	&CRect(0,0,cxDIB,cyDIB), pDoc->GetDocPalette());

	float red, blue, green;
	//	int red, blue, green;
	for (int i=1;i<cxDIB-1;i++) {
		for (int j=1;j<cyDIB-1;j++) {
			red = blue = green = 0;

			UpdateTotals(red, blue, green, m_fFilter[4], sc1.GetPixel(i,j));

			UpdateTotals(red, blue, green, m_fFilter[0], sc1.GetPixel(i-1,j));
			UpdateTotals(red, blue, green, m_fFilter[2], sc1.GetPixel(i+1,j));
			UpdateTotals(red, blue, green, m_fFilter[6], sc1.GetPixel(i,j-1));
			UpdateTotals(red, blue, green, m_fFilter[8], sc1.GetPixel(i,j+1));

			UpdateTotals(red, blue, green, m_fFilter[1], sc1.GetPixel(i-1,j-1));
			UpdateTotals(red, blue, green, m_fFilter[3], sc1.GetPixel(i+1,j-1));
			UpdateTotals(red, blue, green, m_fFilter[5], sc1.GetPixel(i-1,j+1));
			UpdateTotals(red, blue, green, m_fFilter[7], sc1.GetPixel(i+1,j+1));
		
			// Get the threshold value, and check whether any of the
			// values exceed the threshold value.
			if (Plot(int(red),int(green),int(blue),thresh)) {
				if (m_bColour)
					sc2.SetPixel(i,j, RGB(int(red),int(blue),int(green)));
				else sc2.SetPixel(i,j, m_crPlot);
			} else {
				sc2.SetPixel(i,j, m_crNoPlot);
			}
		}

		pMfm->PercentComplete(i / float(cxDIB) * 100);
	}

	CBrush br(RGB(0,0,0));
	sc2.FrameRect(&CRect(0,0,cxDIB, cyDIB), &br);
	
	sc1.SelectObject(src1old);
	sc2.SelectObject(src2old);

	Invalidate();

	pMfm->SetStatusMessage(AFX_IDS_IDLEMESSAGE);
}

void CED256View::UpdateTotals(float &r, float &g, float &b, float filter, COLORREF cr) {
	r += GetRValue(cr)*filter;
	b += GetBValue(cr)*filter;
	g += GetGValue(cr)*filter;
}

void CED256View::OnColourType() {
	m_bColour = !m_bColour;
}

void CED256View::OnFormula() {
	CMainFrame* pMfm = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());

	PullDownToolBar(pMfm->GetToolbar(), ID_FORMULA, IDR_FORMULA, m_iFormula);
}

void CED256View::PullDownToolBar(CToolBar *bar, UINT uButton, UINT uMenu, int bold) {
	// To be professional, get the index from the command ID.
	int ind = bar->CommandToIndex(uButton);

	if (ind == -1) return;

	CRect rect;
	bar->GetItemRect(ind,&rect);
	bar->ClientToScreen(&rect);

	CMenu menu;
	VERIFY(menu.LoadMenu(uMenu));
	CMenu* pPopup = menu.GetSubMenu(0);
	ASSERT(pPopup != NULL); 

	if (bold > -1) SetMenuDefaultItem(pPopup->m_hMenu, bold, true);
	
	pPopup->TrackPopupMenu(TPM_LEFTALIGN, rect.TopLeft().x, rect.BottomRight().y, this);
}

bool CED256View::Plot(int r, int g, int b, int t) {
	switch(m_iFormula) {
		case 0: return (r > t && g > t && b > t);
		case 1: return (r > t || g > t || b > t);
		case 2: return (r+g+b > t);
	}

  return false;
}

void CED256View::OnEditFormulatypeDifferenceand() {
	m_iFormula = 0;
}

void CED256View::OnEditFormulatypeDifferenceor() {
	m_iFormula = 1;
}

void CED256View::OnEditFormulatypeSummation() {
	m_iFormula = 2;
}

void CED256View::OnInverse() {
	m_bInverse = !m_bInverse;
	
	if (!m_bInverse) {
		m_crPlot = RGB(0,0,0);

⌨️ 快捷键说明

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