www.pudn.com > MyImageDB(imageobject).rar > MyImageDBView.cpp


// MyImageDBView.cpp : implementation of the CMyImageDBView class 
// 
 
#include "stdafx.h" 
#include "MyImageDB.h" 
 
#include "MyImageDBDoc.h" 
#include "MyImageDBView.h" 
#include "mainfrm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImageDBView 
 
IMPLEMENT_DYNCREATE(CMyImageDBView, CView) 
 
BEGIN_MESSAGE_MAP(CMyImageDBView, CView) 
	//{{AFX_MSG_MAP(CMyImageDBView) 
	ON_WM_CREATE() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_HSCROLL() 
	ON_WM_VSCROLL() 
	ON_WM_LBUTTONDBLCLK() 
	ON_WM_KEYDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_WM_KEYUP() 
	ON_WM_RBUTTONUP() 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImageDBView construction/destruction 
 
CMyImageDBView::CMyImageDBView() 
{ 
	isDrawCurSelTrainPt = isCtrlKeyDown = FALSE; 
	sX = sY = 0; 
	// TODO: add construction code here 
	neiWidth = neiHeight = 5; 
} 
 
CMyImageDBView::~CMyImageDBView() 
{ 
} 
 
BOOL CMyImageDBView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
	return CScrollView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImageDBView drawing 
 
void CMyImageDBView::OnDraw(CDC* pDC) 
{ 
	CMainFrame *pFrame; 
	pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();              
	pFrame->pImageView = this; 
 
	CMyImageDBDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
	//刷新背景; 
	CBrush bkBrush( RGB(58,110,165) ); 
	CRect clientRect ; 
	GetClientRect(&clientRect); 
	int w = 0; 
	int h = 0; 
	if( pDoc->myImageObject!=NULL) 
	{ 
		w = pDoc->myImageObject->GetWidth(); 
        h = pDoc->myImageObject->GetHeight(); 
	} 
	w = w > clientRect.Width()  ? w : clientRect.Width(); 
	h = h > clientRect.Height() ? h : clientRect.Height(); 
	clientRect.right = w; 
	clientRect.bottom = h; 
	pDC->FillRect( &clientRect, &bkBrush ); 
 
	if( !pDoc->isImageLoaded ) 
	{ 
		return; 
	} 
 
	CSize sizet; 
   	sizet.cx = pDoc->myImageObject->GetWidth(); 
	sizet.cy = pDoc->myImageObject->GetHeight(); 
	SetScrollSizes (MM_TEXT, sizet); 
		 
	//OnPrepareDC (pDC); 
	CRect rect; 
	GetClientRect(&rect); 
	int width, height; 
	width = pDoc->myImageObject->GetWidth(); 
	height = pDoc->myImageObject->GetHeight(); 
	 
	if(widthmyImageObject->SetPalette( pDC ); 
	}	     
	pDoc->myImageObject->Draw(pDC, iX, iY); 
 
	//以下绘制在训练点表中选中的训练点 
	if (isDrawCurSelTrainPt) 
	{ 
		CPen *mypen, *oldpen; 
		mypen = new CPen; 
		mypen->CreatePen(PS_SOLID, 2, RGB(255,0,0)); 
		oldpen = pDC->SelectObject(mypen); 
		CBrush* mybrush = new CBrush; 
		mybrush->CreateSolidBrush(RGB(255,0,0)); 
 
        CRect myrect; 
		myrect.left = iX + curSelTrainPt.posx - 5; 
		myrect.right = iX + curSelTrainPt.posx + 5; 
		myrect.top = iY + curSelTrainPt.posy - 5; 
		myrect.bottom = iY + curSelTrainPt.posy + 5; 
 
		//pDC->DrawFocusRect(myrect); 
		//pDC->Ellipse(myrect); 
		pDC->FrameRect(myrect, mybrush); 
 
		delete mybrush; 
		mybrush = NULL; 
		delete mypen; 
		mypen = NULL; 
		pDC->SelectObject(oldpen); 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImageDBView printing 
 
BOOL CMyImageDBView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMyImageDBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMyImageDBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImageDBView diagnostics 
 
#ifdef _DEBUG 
void CMyImageDBView::AssertValid() const 
{ 
	CScrollView::AssertValid(); 
} 
 
void CMyImageDBView::Dump(CDumpContext& dc) const 
{ 
	CScrollView::Dump(dc); 
} 
 
CMyImageDBDoc* CMyImageDBView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyImageDBDoc))); 
	return (CMyImageDBDoc*)m_pDocument; 
} 
 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyImageDBView message handlers 
 
int CMyImageDBView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	myDropTarget.Register(this); 
	 
	return 0; 
} 
 
BOOL CMyImageDBView::OnDrop(COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point)  
{ 
	int i = 0; 
	return CView::OnDrop(pDataObject, dropEffect, point); 
} 
 
void CMyImageDBView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	CMainFrame *pFrame; 
	pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();              
 
	//判断是否为某点类添加训练样本点 
	//如是,则计算各种相关信息,加入到相应类的样本点集中去; 
	if (isCtrlKeyDown) 
	{ 
		//当前的现存点类列表中是否有某项被选中; 
		if (pFrame->mySegmentPara==NULL) 
		{ 
			return; 
		} 
		CString classname = pFrame->mySegmentPara->GetExistListSelect(); 
		if ( classname!="" ) 
		{ 
			//添加训练点; 
			//首先得到各相关信息,然后将各信息加入到数据库中去; 
			MyLUV templuv; 
			GetLuvAtCursor(templuv); 
			MyLUV texluv; 
			texluv = GetDocument()->GetMinmaxTexLUV(cX, cY);//得到当前点处的最小最大纹理; 
			pFrame->AddTrainPoints(classname, pFrame->selName, 
				cX, cY, templuv, texluv); 
			pFrame->AddExistClass(classname, templuv, texluv); 
		}	 
	} 
 
	CView::OnLButtonDown(nFlags, point); 
} 
 
DROPEFFECT CMyImageDBView::OnDragEnter(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	return CView::OnDragEnter(pDataObject, dwKeyState, point); 
} 
 
void CMyImageDBView::OnDragLeave()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	CView::OnDragLeave(); 
} 
 
DROPEFFECT CMyImageDBView::OnDragOver(COleDataObject* pDataObject, DWORD dwKeyState, CPoint point)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	return CView::OnDragOver(pDataObject, dwKeyState, point); 
} 
 
 
void CMyImageDBView::OnInitialUpdate()  
{ 
	CScrollView::OnInitialUpdate(); 
 
	CSize sizeTotal; 
	// TODO: calculate the total size of this view 
	sizeTotal.cx = sizeTotal.cy = 100; 
	SetScrollSizes(MM_TEXT, sizeTotal); 
	 
} 
 
BOOL CMyImageDBView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	INT tempx = GetScrollPos(SB_HORZ);//04.05.25, 好麻烦!:) 
	INT tempy = GetScrollPos(SB_VERT); 
	sX = tempx; 
	sY = tempy; 
	return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll); 
} 
 
BOOL CMyImageDBView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	return CScrollView::OnScrollBy(sizeScroll, bDoScroll); 
} 
 
void CMyImageDBView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
   // Get the minimum and maximum scroll-bar positions. 
   CScrollView::OnHScroll(nSBCode, nPos, pScrollBar); 
   //CView::OnHScroll(nSBCode, nPos, pScrollBar); 
} 
 
void CMyImageDBView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CScrollView::OnVScroll(nSBCode, nPos, pScrollBar); 
} 
 
void CMyImageDBView::OnLButtonDblClk(UINT nFlags, CPoint point)  
{ 
	//将当前图像加入到图像数据库中去; 
 
	GetDocument()->AddImageToDb(); 
	CView::OnLButtonDblClk(nFlags, point); 
} 
 
void CMyImageDBView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if ( nChar == 17 ) 
	{ 
		//“CTRL”键; 
		isCtrlKeyDown = TRUE; 
		::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS)); 
	} 
	 
	CView::OnKeyDown(nChar, nRepCnt, nFlags); 
} 
 
BOOL CMyImageDBView::GetLuvAtCursor(MyLUV& outluv) 
{ 
	CMyImageDBDoc* pdoc = GetDocument(); 
	if (pdoc->myImageObject == NULL) 
	{ 
		outluv.l = outluv.u = outluv.v = 0; 
		return FALSE; 
	} 
 
	int h = pdoc->myImageObject->GetHeight(); 
	int w = pdoc->myImageObject->GetWidth(); 
 
    if (cX<=0 || cX>=w || cY<=0 || cY>=h || pdoc->luvData==NULL) 
	{ 
		outluv.l = outluv.u = outluv.v = 0; 
		return FALSE; 
	} 
	outluv = GetDocument()->luvData[cY*w + cX]; 
 
    return TRUE; 
} 
 
BOOL CMyImageDBView::GetColorAtCursor(RGBQUAD &color) 
{ 
	if (GetDocument()->myImageObject == NULL) 
	{ 
		return FALSE; 
	} 
 
	int h = GetDocument()->myImageObject->GetHeight(); 
	int w = GetDocument()->myImageObject->GetWidth(); 
 
    if (cX<0 || cX>w || cY<0 || cY>h) 
	{ 
		color.rgbBlue = color.rgbGreen = color.rgbRed = color.rgbReserved = 0; 
		return FALSE; 
	} 
 
    color = GetDocument()->myImageObject->GetPixelColor(cX, cY); 
    return TRUE; 
} 
 
BYTE* CMyImageDBView::GetNeiMatrixAtCursor(INT& width, INT& height) 
{ 
	CMyImageDBDoc* pdoc = GetDocument(); 
	if (pdoc->myImageObject == NULL) 
	{ 
		return NULL; 
	} 
 
	int h = pdoc->myImageObject->GetHeight(); 
	int w = pdoc->myImageObject->GetWidth(); 
 
    if (cX<0 || cX>w || cY<0 || cY>h) 
	{ 
		return NULL; 
	} 
 
	if (pdoc->imageNei==NULL) 
	{ 
		return NULL; 
	} 
 
	width = neiWidth; 
	height = neiHeight; 
	INT radius = NEIRADIUS; 
	BYTE* temparr; 
    pdoc->GetNearPixelsExt(cX, cY, pdoc->imageData, w, h, radius, &temparr); 
	return temparr; 
} 
 
void CMyImageDBView::UpdateColorPalette() 
{ 
	CMainFrame *pFrame; 
	pFrame = (CMainFrame*) AfxGetApp()->GetMainWnd();              
	 
	RGBQUAD temprgb; 
	GetColorAtCursor(temprgb); 
	MyLUV templuv; 
	GetLuvAtCursor(templuv); 
 
	pFrame->myPaletteBar.OnUpdateColor(temprgb, cX, cY, templuv); 
		//, NULL, 0, 0);//修改颜色板中的数据; 
} 
 
void CMyImageDBView::UpdatePostion() 
{ 
	if (GetDocument()->myImageObject == NULL) 
	{ 
		return; 
	} 
	 
	POINT xpoint; 
	if (!GetCursorPos(&xpoint)) 
	{ 
		return; 
	} 
 
	ScreenToClient(&xpoint); 
 
	CSize sizeTotal; 
	CRect clientRect ; 
	GetClientRect(&clientRect); 
	int w = GetDocument()->myImageObject->GetWidth(); 
	int h = GetDocument()->myImageObject->GetHeight(); 
	sizeTotal.cx = w > clientRect.Width() ? w : clientRect.Width(); 
	sizeTotal.cy = h > clientRect.Height() ? h : clientRect.Height(); 
 
	CRect rect(0, 0, sizeTotal.cx, sizeTotal.cy); 
	if (!rect.PtInRect(xpoint)) 
	{ 
		return; 
	} 
	 
    cX = xpoint.x - iX + sX + 1; 
    cY = xpoint.y - iY + sY + 1; 
	 
} 
void CMyImageDBView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	UpdatePostion();//修改当前光标在图像中的位置; 
	UpdateColorPalette(); 
 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CMyImageDBView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	if ( nChar == 17 ) 
	{ 
		//“CTRL”键; 
		isCtrlKeyDown = FALSE; 
        ::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW)); 
	} 
	 
	CView::OnKeyUp(nChar, nRepCnt, nFlags); 
} 
 
void CMyImageDBView::ClearCurTrainPt() 
//清除当前选择的训练点; 
{ 
	curSelTrainPt.id = -1; 
	curSelTrainPt.imagename = ""; 
	curSelTrainPt.posx = 0; 
	curSelTrainPt.posy = 0; 
	curSelTrainPt.regionclass = ""; 
	isDrawCurSelTrainPt = FALSE; 
 
	Invalidate(FALSE); 
} 
 
void CMyImageDBView::SetCurSelTrainPoint(LONG pointid, CString imagename, INT posx, INT posy, CString regionclass) 
//设置当前选择的训练点; 
{ 
	curSelTrainPt.id = pointid; 
	curSelTrainPt.imagename = imagename; 
	curSelTrainPt.posx = posx; 
	curSelTrainPt.posy = posy; 
	curSelTrainPt.regionclass = regionclass; 
	isDrawCurSelTrainPt = TRUE; 
 
	Invalidate(FALSE); 
} 
 
void CMyImageDBView::OnRButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	//得到当前光标点的邻域; 
	INT radius = NEIRADIUS; 
	CMyImageDBDoc* pdoc = GetDocument(); 
	//以下得到邻域在各个模板下的方向与熵; 
	//四个模板,每个模板两个矩阵; 
	INT modwidth = 2 * radius + 1; 
	BYTE* orgnei = NULL; 
	pdoc->GetNearPixelsGreenExt(cX, cY 
		, pdoc->imageData, pdoc->imageWidth 
		, pdoc->imageHeight, radius, &orgnei); 
	INT blockwidth = 2*radius + 1; 
 
	FLOAT blocke; 
	INT blockd; 
	myTexture.GetDirection(orgnei, blockwidth 
		, blockwidth, blockd, blocke); 
 
	BYTE* processarr = new BYTE[blockwidth*blockwidth]; 
 
	//计算水平方向模板的相关信息; 
	myMath.RevertCopyMatrix(orgnei 
		, blockwidth, processarr, 0, 0); 
	FLOAT blocke0; 
	INT blockd0; 
	myTexture.GetDirection(processarr, blockwidth 
		, blockwidth, blockd0, blocke0); 
 
	//计算45度方向模板的相关信息; 
	myMath.RevertCopyMatrix(orgnei 
		, blockwidth, processarr, 1, 0); 
	FLOAT blocke1; 
	INT blockd1; 
	myTexture.GetDirection(processarr, blockwidth 
		, blockwidth, blockd1, blocke1); 
 
	//计算90度方向模板的相关信息; 
	myMath.RevertCopyMatrix(orgnei 
		, blockwidth, processarr, 2, 0); 
	FLOAT blocke2; 
	INT blockd2; 
	myTexture.GetDirection(processarr, blockwidth 
		, blockwidth, blockd2, blocke2); 
 
	//计算135度方向模板的相关信息; 
	myMath.RevertCopyMatrix(orgnei 
		, blockwidth, processarr, 3, 0); 
	FLOAT blocke3; 
	INT blockd3; 
	myTexture.GetDirection(processarr, blockwidth 
		, blockwidth, blockd3, blocke3); 
 
	delete [] processarr; processarr = NULL; 
 
	BYTE* curnei = NULL; 
	pdoc->GetNearPixelsExt(cX, cY 
		, pdoc->imageData, pdoc->imageWidth 
		, pdoc->imageHeight, radius, &curnei); 
	 
	//邻域处理(周围各点与中心点的差值); 
	INT tempw = (2*radius+1); 
	INT centerx, centery, centerpos;//中心点位置; 
	centerx = centery = tempw/2; 
	centerpos = centery*tempw + centerx; 
	for (INT y=0; yGetMainWnd();              
	pFrame->myPaletteBar.UpdateNeiData(curnei, radius 
		, (FLOAT)blockd, blocke 
		, (FLOAT)blockd0, blocke0, (FLOAT)blockd1, blocke1 
		, (FLOAT)blockd2, blocke2, (FLOAT)blockd3, blocke3); 
	 
	CView::OnRButtonUp(nFlags, point); 
}