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

📄 cdrawrec.cpp

📁 face recognition test source code
💻 CPP
字号:
#include "stdafx.h"

#include "error.h"
#include "CDrawRec.h"
#include <math.h>  

BEGIN_MESSAGE_MAP(CDrawReconstruction, CFrameWnd)
	//{{AFX_MSG_MAP(CDrawReconstruction)
	ON_WM_PAINT()
	ON_WM_VSCROLL()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CDrawReconstruction::CDrawReconstruction()
	{	
	}  
	
int CDrawReconstruction::OnCreate(LPCREATESTRUCT lpCreateStruct)
	{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	/*Set the vertical scroll range*/
	GEOMPARAMS gp;
	RECT rect;
	
	GetGeomParams(&gp);
	nMaxVPos = gp.yNum + ((gp.lastNum > 0) ? 1:0);
	GetClientRect(&rect);
	if (nMaxVPos - (float)rect.bottom/(float)Getylen() + 2 > 1)
		SetScrollRange(SB_VERT, 1, nMaxVPos - (float)rect.bottom/(float)Getylen() + 2);
	else
		SetScrollRange(SB_VERT, 1, 1);
	nVPos = 1;

	/*Set the horizontal scroll range*/
	nMaxHPos = gp.xNum;
	nHPos = 1;
	if (nMaxHPos - (float)rect.right/(float)Getxlen() + 2 > 1)
		SetScrollRange(SB_HORZ, 1, nMaxHPos - (float)rect.right/(float)Getxlen() + 2);
	else
		SetScrollRange(SB_HORZ, 1, 1);
	
	/*Create the font*/
	LPSTR pszFace = "MS Sans Serif";
	HDC hdc = ::GetDC(GetSafeHwnd());
	
	MapModePrevious = SetMapMode(hdc, MM_TWIPS);
	hfont = CreateFont(-12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "MS Sans Serif");
	SetMapMode(hdc, MapModePrevious);
	
	::ReleaseDC(GetSafeHwnd(), hdc);

	return 0;
	}

void CDrawReconstruction::OnSize(UINT nType, int cx, int cy)
	{
	CFrameWnd::OnSize(nType, cx, cy);
	
	/*Reset the vertical scroll range*/
	if (nMaxVPos - (float)cy/(float)Getylen() + 2 > 1)
		SetScrollRange(SB_VERT, 1, nMaxVPos - (float)cy/(float)Getylen() + 2);
	else
		SetScrollRange(SB_VERT, 1, 1);

	/*Reset the horizontal scroll range*/
	if (nMaxHPos - (float)cx/(float)Getxlen() + 2 > 1)
		SetScrollRange(SB_HORZ, 1, nMaxHPos - (float)cx/(float)Getxlen() + 2);
	else
		SetScrollRange(SB_HORZ, 1, 1);
	}

/*************************************************************************************************
* Draws the area that must be redrawn. This fct is called by windows
*************************************************************************************************/
void CDrawReconstruction::OnPaint()
	{    
	CPaintDC dc(this); 										// device context for painting
	RECT area;
	typeface *pData = NULL; 
	long x, y, xlen, ylen, nFace;
	COLORREF color;
	CORNER c;
	RECT DrawRect, FaceRect, TextRect;
	int nNumFace;
	double scaletxt = 1.0;
	char name[64];
	unsigned int val;
	float shift = 0.0;
	float scale = 1.0;
	CFace *pFace = NULL;
	CFaceDB *DB[2];
	int nDB;
	
	DB[0] = pFaceDB1;
	DB[1] = pFaceDB2;
	
	GetUpdateRect(&area, TRUE);							/*Put in area the rectangle that must be redrawn*/
	if (IsRectEmpty(&area))
		GetClientRect(&area);
	GetGeomParams(&gp);
	
	nNumFace = pFaceDB1->GetNumFace();
		
	/*Select the font*/
	hfontOld = (HFONT)::SelectObject(dc.m_hDC, (HGDIOBJ)hfont);

	for (nFace = nVPos; nFace <= nNumFace; nFace++)
		{
		for (nDB = nHPos-1; nDB <= nMaxHPos-1; nDB++)
			{
			DB[nDB]->Expand(nFace, &scale, &shift, 0, 255);
			pFace = DB[nDB]->GetFace(nFace);
			strcpy(name, pFace->GetName());
			xlen = pFace->Getxlen();
			ylen = pFace->Getylen();
			pFace->Lock();
			
			GetCornerFace(&c, nFace, nDB+1);
			c.y -= (nVPos-1) * Getylen();					/*The coordinates must take into account*/
			c.x -= (nHPos-1) * Getxlen();					/* the horizontal and vertical scrolling*/
			
			SetRect(&FaceRect, c.x, c.y, c.x + xlen, c.y + ylen);
			if (IntersectRect(&DrawRect, &area, &FaceRect))
																	/*Assigns to DrawRect the rectangle that must be drawn*/ 
				{													/*The face has to be drawn because it is inside the drawing area*/									
				for (y = DrawRect.top - FaceRect.top + 1L; y <= DrawRect.bottom - FaceRect.top; y++)
					{
					for (x = DrawRect.left - FaceRect.left + 1L; x <= DrawRect.right - FaceRect.left; x++)
						{
						val = (unsigned int)(shift + scale*pFace->GetAt(x, y));
						
						/*the value of a single pixel is set in this loop, the grey level range is from 0 to 255*/
						color = RGB(val, val, val);
						dc.SetPixel(x + c.x, y + c.y, color);
																	/*Set the pixel's value*/
						}
					}
				
				/* Draw the face's name at the bottom of it */
				GetCornerName(&c, nFace, nDB+1);
				c.y -= (nVPos-1) * Getylen();          /*The coordinates must take into account*/
				c.x -= (nHPos-1) * Getxlen();				/* the horizontal and vertical scrolling*/
				SetRect(&TextRect, c.x, c.y, xlen, YTEXT);
				dc.DrawText(name, strlen(name), &TextRect, DT_NOCLIP | DT_NOPREFIX | DT_SINGLELINE);
				}
			pFace->Unlock();
			}
		}
	SelectObject(dc.m_hDC, hfontOld);					/*deselect the font*/
	}

void CDrawReconstruction::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
	{
	RECT rect;
	int nLinePage, nPrevVPos;
	
	GetClientRect(&rect);
	nLinePage = floor((float)(rect.bottom - rect.top) / (float)Getylen());
   
	nPrevVPos = nVPos;
	switch (nSBCode)
		{
		case SB_LINEDOWN:
			nVPos++;
			break;
		
		case SB_LINEUP:
			nVPos--;
			break;
		
		case SB_PAGEDOWN:
			nVPos += nLinePage;
			break;
		
		case SB_PAGEUP:
			nVPos -= nLinePage;
			break;
		
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			nVPos = nPos;
			break;
		}
	if (nVPos < 1)
		nVPos = 1;
   if (nVPos > nMaxVPos - nLinePage + 2)
   	nVPos = nMaxVPos - nLinePage + 2;
   
	SetScrollPos(SB_VERT, nVPos);
   ScrollWindow(0, -(nVPos - nPrevVPos)*Getylen());
   
	CFrameWnd::OnVScroll(nSBCode, nPos, pScrollBar);
	}

void CDrawReconstruction::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
	{  
	RECT rect;
	int nPrevHPos;
	
	GetClientRect(&rect);

	nPrevHPos = nHPos;
	switch (nSBCode)
		{
		case SB_LINERIGHT:
			nHPos++;
			break;
		
		case SB_LINELEFT:
			nHPos--;
			break;
		
		case SB_PAGERIGHT:
			nHPos += (float)rect.right/(float)Getxlen();
			break;
		
		case SB_PAGEUP:
			nHPos -= (float)rect.right/(float)Getxlen();
			break;
		
		case SB_THUMBPOSITION:
		case SB_THUMBTRACK:
			nHPos = nPos;
			break;
		}
	if (nHPos < 1)
		nHPos = 1;
   if (nHPos > nMaxHPos - (float)rect.right/(float)Getxlen() + 2)
   	nHPos = nMaxHPos - (float)rect.right/(float)Getxlen() + 2;
   
	SetScrollPos(SB_HORZ, nHPos);
   ScrollWindow(-(nHPos - nPrevHPos)*Getxlen(), 0);
	
	CFrameWnd::OnHScroll(nSBCode, nPos, pScrollBar);
	}

/*************************************************************************************************
* Puts in the CORNER struct c (basically a point) the point where the face whose number is nFace
* must be drawn in the face database drawing area.
* num gives the number of the face database
*************************************************************************************************/
void CDrawReconstruction::GetCornerFace(CORNER *c, int nFace, int num)
	{
	c->x = (num-1)*Getxlen();
	c->y = (nFace-1)*Getylen();
	}

/*************************************************************************************************
* Puts in the CORNER struct c (basically a point) the point where the face's name whose number is 
* nFace must be drawn in the face database drawing area.
* num gives the number of the face database
*************************************************************************************************/
void CDrawReconstruction::GetCornerName(CORNER *c, int nFace, int num)
	{
	long lval;
	
	GetCornerFace(c, nFace, num);
	
	lval = pFaceDB1->Getylen();
	c->y += lval + YFACETEXT;
	}

/*************************************************************************************************
* Returns the width of a face as it is displayed in the face database drawing area (including the 
* space around it).
*************************************************************************************************/
int CDrawReconstruction::Getxlen()
	{
	long lval;
	
	lval = pFaceDB1->Getxlen();
	return XSPACE + lval;
	}

/*************************************************************************************************
* Returns the length of a face as it is displayed in the face database drawing area (including the 
* space around it, the space for the face's name and for the little area between the face and its 
* name).
*************************************************************************************************/
int CDrawReconstruction::Getylen()
	{
	long lval;
	lval = pFaceDB1->Getylen();
	return YSPACE + lval + YFACETEXT + YTEXT;
	}

/*************************************************************************************************
* Puts in a struct GEOMPARAMS the number of faces displayed in the two directions and the number 
* of faces which are displayed on the last line.
*************************************************************************************************/
void CDrawReconstruction::GetGeomParams(GEOMPARAMS *gp)
	{
	gp->yNum = pFaceDB1->GetNumFace();
	gp->xNum = 2;
	gp->lastNum = 0;
	}
	
/*****************************************************************************
* SetFaceDB initialise the face database data (pFaceDB).
******************************************************************************/
void CDrawReconstruction::SetFaceDB(CFaceDB *pFaceDB1t, CFaceDB *pFaceDB2t)
	{
	pFaceDB1 = pFaceDB1t;
	pFaceDB2 = pFaceDB2t;
	}	

⌨️ 快捷键说明

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