faceView.cpp
上传用户:yipeng6868
上传日期:2013-02-16
资源大小:222k
文件大小:10k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // faceView.cpp : implementation of the CFaceView class
  2. //
  3. #include "stdafx.h"
  4. #include "face.h"
  5. #include "DIB.h"
  6. #include "faceDoc.h"
  7. #include "faceView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. extern class DIB gDib;
  14. extern HANDLE hDIB;
  15. extern HANDLE hDIBTemp;
  16. extern int gheight;
  17. extern int gwidth;
  18. extern WORD gwBytesPerLine;
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CFaceView
  21. IMPLEMENT_DYNCREATE(CFaceView, CScrollView)
  22. BEGIN_MESSAGE_MAP(CFaceView, CScrollView)
  23. //{{AFX_MSG_MAP(CFaceView)
  24. ON_COMMAND(ID_ERASION, OnErasion)
  25. ON_COMMAND(ID_EYEMAP, OnEyemap)
  26. ON_COMMAND(ID_EYEMAPC, OnEyemapc)
  27. ON_COMMAND(ID_EYEMAPL, OnEyemapl)
  28. ON_COMMAND(ID_GET_FACE_AREA, OnGetFaceArea)
  29. ON_COMMAND(ID_LIGHTINGCONPENSATE, OnLightingconpensate)
  30. ON_COMMAND(ID_SKINTONE, OnSkintone)
  31. ON_COMMAND(ID_DELETE_FALSE_AREA, OnDeleteFalseArea)
  32. ON_COMMAND(ID_DILATION, OnDilation)
  33. ON_COMMAND(ID_ERASION_AGAIN, OnErasionAgain)
  34. ON_COMMAND(ID_DILATION_FIRST, OnDilationFirst)
  35. ON_COMMAND(ID_EYE_CB, OnEyeCb)
  36. ON_COMMAND(ID_EYE_Cr, OnEYECr)
  37. ON_COMMAND(ID_DELETE_FALSE_EYE, OnDeleteFalseEye)
  38. ON_COMMAND(ID_DELETESCATER, OnDeletescater)
  39. ON_COMMAND(ID_MOUSEMAP, OnMousemap)
  40. ON_COMMAND(ID_ERASION_MOUSE, OnErasionMouse)
  41. ON_COMMAND(ID_MOUTH_CENTER, OnMouthCenter)
  42. ON_COMMAND(ID_DILATION_EYE, OnDilationEye)
  43. ON_COMMAND(ID_EYE_CENTER, OnEyeCenter)
  44. ON_COMMAND(ID_GOULE_FACE, OnGouleFace)
  45. //}}AFX_MSG_MAP
  46. // Standard printing commands
  47. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  48. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  49. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CFaceView construction/destruction
  53. CFaceView::CFaceView()
  54. {
  55. lpRgb = NULL;
  56. lpYcc = NULL;
  57. faceNum = 0;
  58. for(int i=0; i<ImgRange; i++)
  59. for (int j=0; j<ImgRange; j++)
  60. {
  61. this->emymapc[i][j] = false;
  62. this->emymapl[i][j] = false;
  63. this->lab[i][j] = false;
  64. }
  65. this->result = false;
  66. }
  67. CFaceView::~CFaceView()
  68. {
  69. }
  70. BOOL CFaceView::PreCreateWindow(CREATESTRUCT& cs)
  71. {
  72. // TODO: Modify the Window class or styles here by modifying
  73. //  the CREATESTRUCT cs
  74. return CScrollView::PreCreateWindow(cs);
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CFaceView drawing
  78. void CFaceView::OnDraw(CDC* pDC)
  79. {
  80. CFaceDoc* pDoc = GetDocument();
  81. ASSERT_VALID(pDoc);
  82. if(hDIB)
  83. gDib.PaintDIBTrue(pDC->GetSafeHdc(),CRect(0,0,gwidth,gheight),hDIB,CRect(0,0,gwidth,gheight),SRCCOPY);
  84. // TODO: add draw code for native data here
  85. }
  86. void CFaceView::OnInitialUpdate()
  87. {
  88. CScrollView::OnInitialUpdate();
  89. CSize sizeTotal;
  90. // TODO: calculate the total size of this view
  91. sizeTotal.cx = sizeTotal.cy = 100;
  92. SetScrollSizes(MM_TEXT, sizeTotal);
  93. }
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CFaceView printing
  96. BOOL CFaceView::OnPreparePrinting(CPrintInfo* pInfo)
  97. {
  98. // default preparation
  99. return DoPreparePrinting(pInfo);
  100. }
  101. void CFaceView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  102. {
  103. // TODO: add extra initialization before printing
  104. }
  105. void CFaceView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  106. {
  107. // TODO: add cleanup after printing
  108. }
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CFaceView diagnostics
  111. #ifdef _DEBUG
  112. void CFaceView::AssertValid() const
  113. {
  114. CScrollView::AssertValid();
  115. }
  116. void CFaceView::Dump(CDumpContext& dc) const
  117. {
  118. CScrollView::Dump(dc);
  119. }
  120. CFaceDoc* CFaceView::GetDocument() // non-debug version is inline
  121. {
  122. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFaceDoc)));
  123. return (CFaceDoc*)m_pDocument;
  124. }
  125. #endif //_DEBUG
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CFaceView message handlers
  128. void CFaceView::OnErasion() 
  129. {
  130. gDib.Erasion(hDIB);
  131. gDib.Erasion(hDIB);
  132. gDib.Erasion(hDIB);
  133. Invalidate();
  134. }
  135. void CFaceView::OnEyemap() 
  136. {
  137. LPBYTE lpData;
  138. long lOffset;
  139. lpData = gDib.FindDIBBits(hDIB);
  140. for (int i=0; i<gheight; i++)
  141. for (int j=0; j<gwidth; j++)
  142. {
  143. lOffset = gDib.PixelOffset(i, j, gwBytesPerLine);
  144. if( this->emymapc[i][j] && this->emymapl[i][j])
  145. {
  146. *(lpData + lOffset++) = 255;
  147. *(lpData + lOffset++) = 255;
  148. *(lpData + lOffset++) = 255;
  149. }
  150. else
  151. {
  152. *(lpData + lOffset++) = 0;
  153. *(lpData + lOffset++) = 0;
  154. *(lpData + lOffset++) = 0;
  155. }
  156. }
  157. Invalidate();
  158. }
  159. void CFaceView::OnEyemapc() 
  160. {
  161. LPBYTE lpData;
  162. if(lpYcc)
  163. delete[] lpYcc;
  164. lpYcc = new BYTE[gwBytesPerLine * gheight];
  165.  gDib.RgbtoYcb(hDIBTemp,lpYcc);
  166.  
  167. GlobalLock(hDIB);
  168. lpData = gDib.FindDIBBits(hDIB);
  169. //memcpy(lpData,gDib.YcctoRgb(lpYcc,gwBytesPerLine,gheight,gwidth),gwBytesPerLine*gheight);
  170. gDib.EyeMapC(lpData,lpYcc,gwBytesPerLine,CRect(0,0,gwidth-1,gheight-1));
  171. for(int i=0; i<gheight; i++)
  172. for (int j=0; j<gwidth; j++)
  173. {
  174. long lOffset;
  175. lOffset = gDib.PixelOffset(i, j, gwBytesPerLine);
  176. if(*(lpData + lOffset) == 255)
  177. {
  178. this->emymapc[i][j] = true;
  179. }
  180. }
  181. GlobalUnlock(hDIB);
  182. Invalidate();
  183. }
  184. void CFaceView::OnEyemapl() 
  185. {
  186. LPBYTE lpDataR,lpDataD;
  187. if(lpYcc)
  188. delete[] lpYcc;
  189. GlobalLock(hDIBTemp);
  190. lpDataR = gDib.FindDIBBits(hDIBTemp);
  191. lpYcc = new BYTE[gwBytesPerLine * gheight];
  192. gDib.RgbtoYcb(hDIBTemp,lpYcc);
  193. lpDataD = gDib.FindDIBBits(hDIB);
  194. memcpy(lpDataD,lpDataR,gwBytesPerLine*gheight);
  195. gDib.EyeMapL(lpDataD,gwBytesPerLine, CRect(0,0,gwidth-1,gheight-1));
  196. for (int i=0; i<gheight; i++)
  197. for (int j=0; j<gwidth; j++)
  198. {
  199. long lOffset;
  200. lOffset = gDib.PixelOffset(i, j, gwBytesPerLine);
  201. if(*(lpDataD + lOffset) == 255)
  202. {
  203. this->emymapl[i][j] = true;
  204. }
  205. }
  206. GlobalUnlock(hDIBTemp);
  207. Invalidate();
  208. }
  209. void CFaceView::OnGetFaceArea() 
  210. {
  211. gDib.FaceLocate(hDIB,faceLocation,faceNum);
  212. Invalidate();
  213. }
  214. void CFaceView::OnLightingconpensate() 
  215. {
  216. hDIBTemp = gDib.CopyHandle(hDIB);
  217. gDib.LightingCompensate(hDIB);
  218. GlobalUnlock(hDIB);
  219. Invalidate();
  220. }
  221. void CFaceView::OnSkintone() 
  222. {
  223. lpYcc = new BYTE[gwBytesPerLine * gheight];
  224. gDib.RgbtoYcb(hDIB,lpYcc);
  225. gDib.YccTransform(lpYcc, gwBytesPerLine,gheight,gwidth);
  226. gDib.faceear(lpYcc,gwBytesPerLine,gheight,gwidth,flag);
  227. LPBYTE lpData = (LPBYTE)GlobalLock(hDIB)+sizeof(BITMAPINFOHEADER);
  228. for (int i=0; i<gheight; i++)
  229. for (int j=0; j<gwidth; j++)
  230. {
  231. long lOffset = gDib.PixelOffset(i, j, gwBytesPerLine);
  232. if (flag[i][j] == true)
  233. {
  234. *(lpData + lOffset++) = 255;
  235. *(lpData + lOffset++) = 255;
  236. *(lpData + lOffset++) = 255;
  237. }
  238. else
  239. {
  240. *(lpData + lOffset++) = 0;
  241. *(lpData + lOffset++) = 0;
  242. *(lpData + lOffset++) = 0;
  243. }
  244. }
  245. GlobalUnlock(hDIB);
  246. Invalidate();
  247. }
  248. void CFaceView::OnDeleteFalseArea() 
  249. {
  250. gDib.ErasionFalseArea(hDIB);
  251. Invalidate();
  252. }
  253. void CFaceView::OnDilation() 
  254. {
  255. gDib.Dilation(hDIB);
  256. gDib.Dilation(hDIB);
  257. gDib.Dilation(hDIB);
  258. gDib.Dilation(hDIB);
  259. gDib.Dilation(hDIB);
  260. gDib.Dilation(hDIB);
  261. gDib.Dilation(hDIB);
  262. gDib.Dilation(hDIB);
  263. gDib.Dilation(hDIB);
  264. gDib.Dilation(hDIB);
  265. gDib.Dilation(hDIB);
  266. gDib.Dilation(hDIB);
  267. gDib.Dilation(hDIB);
  268. Invalidate();
  269. }
  270. void CFaceView::OnErasionAgain() 
  271. {
  272. gDib.Erasion(hDIB);
  273. gDib.Erasion(hDIB);
  274. gDib.Erasion(hDIB);
  275. gDib.Erasion(hDIB);
  276. gDib.Erasion(hDIB);
  277. gDib.Erasion(hDIB);
  278. gDib.Erasion(hDIB);
  279. gDib.Erasion(hDIB);
  280. gDib.Erasion(hDIB);
  281. gDib.Erasion(hDIB);
  282. gDib.Erasion(hDIB);
  283. gDib.Erasion(hDIB);
  284. gDib.Erasion(hDIB);
  285. gDib.Erasion(hDIB);
  286. LPBYTE lpData;
  287. GlobalLock(hDIB);
  288. lpData = gDib.FindDIBBits(hDIB);
  289. for (int i=0;i<gheight; i++)
  290. for (int j=0; j<gwidth; j++)
  291. {
  292. long lOffset = gDib.PixelOffset(i, j, gwBytesPerLine);
  293. if(*(lpData + lOffset) == 255)
  294. {
  295. this->lab[i][j] = true;
  296. }
  297. }
  298. GlobalUnlock(hDIB);
  299. Invalidate();
  300. }
  301. void CFaceView::OnDilationFirst() 
  302. {
  303. gDib.Dilation(hDIB);
  304. gDib.Dilation(hDIB);
  305. Invalidate();
  306. }
  307. void CFaceView::OnEyeCb() 
  308. {
  309. lpRB = new BYTE[gwBytesPerLine * gheight];
  310. gDib.RgbtoYcb(hDIBTemp,lpRB);
  311. LPBYTE lpData =gDib.FindDIBBits(hDIB);
  312. gDib.EyeMapb(lpData,lpRB,gwBytesPerLine,CRect(0,0,gwidth-1,gheight-1));
  313. Invalidate();
  314. }
  315. void CFaceView::OnEYECr() 
  316. {
  317. lpRB = new BYTE[gwBytesPerLine * gheight];
  318. gDib.RgbtoYcb(hDIBTemp,lpRB);
  319. LPBYTE lpData =gDib.FindDIBBits(hDIB);
  320. gDib.EyeMapR(lpData,lpRB,gwBytesPerLine,CRect(0,0,gwidth-1,gheight-1));
  321. Invalidate();
  322. }
  323. void CFaceView::OnDeleteFalseEye() 
  324. {
  325. gDib.DeleteFasleEye(hDIB,faceLocation[0]);
  326. Invalidate();
  327. }
  328. void CFaceView::OnDeletescater() 
  329. {
  330. gDib.DeleteScatePoint(hDIB);
  331. gDib.Dilation(hDIB);
  332. Invalidate();
  333. }
  334. void CFaceView::OnMousemap() 
  335. {
  336. if(lpYcc)
  337. delete [] lpYcc;
  338. lpYcc = new BYTE[gwBytesPerLine * gheight];
  339. if(!lpYcc)
  340. return;
  341. gDib.RgbtoYcb(hDIBTemp,lpYcc);
  342. LPBYTE lpData = gDib.FindDIBBits(hDIB);
  343. gDib.MouseMap(lpData,lpYcc,gwBytesPerLine,CRect(0,0,gwidth-1,gheight-1));
  344. //gDib.Erasion2(hDIB);
  345. Invalidate();
  346. }
  347. void CFaceView::OnErasionMouse() 
  348. {
  349. gDib.Erasion2(hDIB);
  350. //gDib.DeleteScatePoint(hDIB);
  351. //gDib.Dilation(hDIB);
  352. //gDib.Dilation(hDIB);
  353. //gDib.DeleteScatePoint(hDIB);
  354. Invalidate();
  355. }
  356. void CFaceView::OnMouthCenter() 
  357. {
  358. gDib.MouthCenter(hDIB,CRect(0,0,gwidth-1,gheight-1),mouthLocation);
  359. this->result = true;
  360. Invalidate();
  361. }
  362. void CFaceView::OnDilationEye() 
  363. {
  364. gDib.Dilation(hDIB);
  365. Invalidate();
  366. }
  367. void CFaceView::OnEyeCenter() 
  368. {
  369. gDib.EyeCenter(hDIB,CRect(0,0,gwidth-1,gheight-1),eye1,eye2);
  370. Invalidate();
  371. }
  372. void CFaceView::OnGouleFace() 
  373. {
  374. gDib.EllipseFace(hDIBTemp,this->mouthLocation,this->eye1,this->eye2);
  375. if(hDIB)
  376. GlobalFree(hDIB);
  377. hDIB = gDib.CopyHandle(hDIBTemp);
  378. Invalidate();
  379. }