CImageView.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // CImageView.cpp: 实现文件
  2. //
  3. #include "pch.h"
  4. #include "CImageView.h"
  5. // CImageView
  6. IMPLEMENT_DYNAMIC(CImageView, CWnd)
  7. CImageView::CImageView()
  8. {
  9. m_pData16 = nullptr;
  10. m_pData8 = nullptr;
  11. m_width = 0;
  12. m_height = 0;
  13. m_bits = 0;
  14. m_bDrawRect = false;
  15. m_nROIAverage = 0;
  16. m_wfRation = 0.0f;
  17. m_hfRation = 0.0f;
  18. }
  19. CImageView::~CImageView()
  20. {
  21. if (m_pData16)
  22. {
  23. delete[] m_pData16;
  24. m_pData16 = nullptr;
  25. }
  26. if (m_pData8)
  27. {
  28. delete[] m_pData8;
  29. m_pData8 = nullptr;
  30. }
  31. if (!m_Image.IsNull())
  32. {
  33. m_Image.Destroy();
  34. }
  35. }
  36. BEGIN_MESSAGE_MAP(CImageView, CWnd)
  37. ON_WM_PAINT()
  38. ON_WM_MOUSEMOVE()
  39. ON_WM_LBUTTONDOWN()
  40. ON_WM_LBUTTONUP()
  41. END_MESSAGE_MAP()
  42. // CImageView 消息处理程序
  43. void CImageView::OnPaint()
  44. {
  45. CPaintDC dc(this);
  46. CRect rect;
  47. GetClientRect(&rect);
  48. SetStretchBltMode(dc.m_hDC, HALFTONE);
  49. m_Image.StretchBlt(dc, 0, 0, rect.Width(), rect.Height());
  50. if (m_bDrawRect)
  51. {
  52. dc.MoveTo(m_rtDraw.left, m_rtDraw.top);
  53. dc.LineTo(m_rtDraw.right, m_rtDraw.top);
  54. dc.LineTo(m_rtDraw.right, m_rtDraw.bottom);
  55. dc.LineTo(m_rtDraw.left, m_rtDraw.bottom);
  56. dc.LineTo(m_rtDraw.left, m_rtDraw.top);
  57. }
  58. return;
  59. }
  60. void CImageView::OnMouseMove(UINT nFlags, CPoint point)
  61. {
  62. if (m_bDrawRect)
  63. {
  64. m_ptEnd = point;
  65. CreatRect(m_ptStart, m_ptEnd);
  66. Invalidate();
  67. }
  68. CWnd::OnMouseMove(nFlags, point);
  69. }
  70. void CImageView::OnLButtonDown(UINT nFlags, CPoint point)
  71. {
  72. m_ptStart = point;
  73. m_bDrawRect = true;
  74. CWnd::OnLButtonDown(nFlags, point);
  75. }
  76. void CImageView::OnLButtonUp(UINT nFlags, CPoint point)
  77. {
  78. m_bDrawRect = false;
  79. m_nROIAverage = CaculateROIAvg();
  80. Invalidate();
  81. ::SendMessage(this->GetParent()->m_hWnd, MSG_RETANGE, NULL, (LPARAM)&m_nROIAverage);
  82. CWnd::OnLButtonUp(nFlags, point);
  83. }
  84. bool CImageView::SetImageData(unsigned short* pdata, int width, int height, int bits)
  85. {
  86. if (!pdata)
  87. return false;
  88. m_width = width;
  89. m_height = height;
  90. m_bits = bits;
  91. m_pData16 = new unsigned short[width * height];
  92. memcpy(m_pData16, pdata, sizeof(unsigned short) * width * height);
  93. CreateImage(m_pData16, width, height);
  94. CRect rect;
  95. GetClientRect(&rect);
  96. m_wfRation = (float)rect.Width() / width;
  97. m_hfRation = (float)rect.Height() / height;
  98. return true;
  99. }
  100. int CImageView::CreateImage(unsigned short* data, int width, int height)
  101. {
  102. if (!data)
  103. return 1;
  104. int lAll = width * height;
  105. if (!m_pData8)
  106. delete[]m_pData8;
  107. m_pData8 = new unsigned char[lAll];
  108. for (int i = 0; i < lAll; ++i)
  109. {
  110. m_pData8[i] = 255 - ((data[i] & (0x3FFF)) >> 6);
  111. }
  112. CreatBitImage(m_pData8, m_width, m_height);
  113. return 0;
  114. }
  115. int CImageView::CreatBitImage(unsigned char* pdata, int width, int height)
  116. {
  117. if (!m_Image.IsNull())
  118. {
  119. m_Image.Destroy();
  120. }
  121. m_Image.Create(width, height, 8);
  122. RGBQUAD clrTable[256];
  123. for (int i = 0; i < 256; ++i)
  124. {
  125. clrTable[i].rgbBlue = i;
  126. clrTable[i].rgbGreen = i;
  127. clrTable[i].rgbRed = i;
  128. clrTable[i].rgbReserved = 0;
  129. }
  130. m_Image.SetColorTable(0, 256, clrTable);
  131. //set image data
  132. int nLine = m_Image.GetPitch();
  133. BYTE* pDstData = (BYTE*)m_Image.GetPixelAddress(0, 0);
  134. BYTE* pSrcData = (BYTE*)pdata;
  135. for (int i = 0; i < height; ++i)
  136. {
  137. memcpy(pDstData, pSrcData, width * sizeof(BYTE));
  138. pDstData += nLine;
  139. pSrcData += width;
  140. }
  141. m_Image.Save(("d:\\test.bmp"));
  142. return 0;
  143. }
  144. int CImageView::CreatRect(CPoint ps, CPoint pe)
  145. {
  146. CRect rect;
  147. int left, top, right, bottom;
  148. if (ps.x < pe.x)
  149. {
  150. left = ps.x;
  151. right = pe.x;
  152. }
  153. else
  154. {
  155. left = pe.x;
  156. right = ps.x;
  157. }
  158. if (ps.y < pe.y)
  159. {
  160. top = ps.y;
  161. bottom = pe.y;
  162. }
  163. else
  164. {
  165. top = pe.y;
  166. bottom = ps.y;
  167. }
  168. m_rtDraw = CRect(left, top, right, bottom);
  169. int Orgleft = left / m_wfRation;
  170. if (Orgleft < 0)
  171. Orgleft = 0;
  172. if (Orgleft > m_width)
  173. Orgleft = m_width;
  174. int Orgright = right / m_wfRation;
  175. if (Orgright < 0)
  176. Orgright = 0;
  177. if (Orgright > m_width)
  178. Orgright = m_width;
  179. int OrgTop = top / m_hfRation;
  180. if (OrgTop < 0)
  181. OrgTop = 0;
  182. if (OrgTop > m_height)
  183. OrgTop = m_height;
  184. int OrgBottom = bottom / m_hfRation;
  185. if (OrgBottom < 0)
  186. OrgBottom = 0;
  187. if (OrgBottom > m_height)
  188. OrgBottom = m_height;
  189. m_rtOrgDraw = CRect(Orgleft, OrgTop, Orgright, OrgBottom);
  190. return 0;
  191. }
  192. int CImageView::CaculateROIAvg()
  193. {
  194. int average = 0;
  195. double total = 0;
  196. int count = 0;
  197. for (int i = m_rtOrgDraw.top; i < m_rtOrgDraw.bottom; ++i)
  198. {
  199. for (int j = m_rtOrgDraw.left; j < m_rtOrgDraw.right; ++j)
  200. {
  201. total += m_pData16[i * m_width + j];
  202. count++;
  203. }
  204. }
  205. average = total / count;
  206. return average;
  207. }