MainFrm.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // MainFrm.cpp : implementation of class CMainFrame
  2. //
  3. #include "stdafx.h"
  4. #include "IMTDemo.h"
  5. #include "MainFrm.h"
  6. #include "Resource.h"
  7. // CMainFrame
  8. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  9. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  10. //{{AFX_MSG_MAP(CMainFrame)
  11. ON_COMMAND(ID_APP_EXIT, OnClose)
  12. ON_WM_LBUTTONUP()
  13. ON_WM_MOUSEMOVE()
  14. ON_WM_CREATE()
  15. ON_WM_SETCURSOR()
  16. ON_WM_SETFOCUS()
  17. ON_WM_MOVE()
  18. ON_COMMAND(ID_OPENFILE, OnOpenfile)
  19. //}}AFX_MSG_MAP
  20. END_MESSAGE_MAP()
  21. // CMainFrame constructor
  22. CMainFrame::CMainFrame()
  23. {
  24. firstPoint = 1;
  25. m_canChange = FALSE;
  26. m_initialized = FALSE;
  27. }
  28. CMainFrame::~CMainFrame()
  29. {
  30. }
  31. void CMainFrame::SetRect( RECT& rect )
  32. {
  33. m_wRect = rect;
  34. m_cRect = rect;
  35. m_cRect.DeflateRect( 0, 0, 0, 32 );
  36. }
  37. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  38. {
  39. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  40. return -1;
  41. static UINT indicators[] =
  42. {
  43. ID_SEPARATOR,
  44. ID_SEPARATOR
  45. };
  46. m_sBar.Create( this );
  47. m_sBar.SetIndicators( indicators, 2 );
  48. m_sBar.SetPaneInfo( 1, m_sBar.GetItemID( 1 ), SBPS_NORMAL, 300 );
  49. m_sBar.SetPaneInfo( 0, m_sBar.GetItemID( 0 ), SBPS_STRETCH, NULL );
  50. m_hArrow = AfxGetApp()->LoadStandardCursor( IDC_ARROW );
  51. m_hCross = AfxGetApp()->LoadStandardCursor( IDC_CROSS );
  52. if ( !eimInterface.IsValid() )
  53. {
  54. AfxMessageBox( _T( "LibIMTDLL.dll not found in the directory of the executable" ) );
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  60. {
  61. if( !CFrameWnd::PreCreateWindow(cs) )
  62. return FALSE;
  63. cs.style = WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
  64. return TRUE;
  65. }
  66. // Message manager for CMainFrame
  67. void CMainFrame::OnLButtonUp( UINT nFlags, CPoint point )
  68. {
  69. CPoint pt = point;
  70. ClientToScreen( &pt );
  71. m_canChange = m_cRect.PtInRect( pt );
  72. if ( m_canChange && m_initialized )
  73. {
  74. if ( firstPoint )
  75. {
  76. if ( eimInterface.GetResult()->numberOfPoints )
  77. {
  78. DrawCurves();
  79. PrintCoordinates( point );
  80. }
  81. eimInterface.setFirstPoint( point );
  82. }
  83. else
  84. {
  85. eimInterface.setSecondPoint( point );
  86. DrawCurves();
  87. }
  88. firstPoint ^= 1;
  89. }
  90. }
  91. void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
  92. {
  93. CPoint pt = point;
  94. ClientToScreen( &pt );
  95. m_canChange = m_cRect.PtInRect( pt );
  96. if ( firstPoint || !m_initialized )
  97. {
  98. PrintCoordinates( point );
  99. }
  100. else if ( m_canChange && !firstPoint )
  101. {
  102. CDC *pDC = GetDC();
  103. CRect rect;
  104. GetClientRect(&rect);
  105. m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() );
  106. CPen pen( PS_SOLID, 1, RGB( 0, 255, 0 ) );
  107. CPen *pOldPen = pDC->SelectObject( &pen );
  108. imt::Point p = eimInterface.GetResult()->p0;
  109. pDC->MoveTo( p.x, p.y );
  110. pDC->LineTo( point );
  111. pDC->SelectObject( pOldPen );
  112. PrintDistance( point );
  113. }
  114. }
  115. BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  116. {
  117. if ( m_initialized && firstPoint && m_canChange && ( nHitTest == HTCLIENT ) )
  118. {
  119. ::SetCursor( m_hCross );
  120. return TRUE;
  121. }
  122. return CFrameWnd::OnSetCursor( pWnd, nHitTest, message );
  123. }
  124. void CMainFrame::OnSetFocus( CWnd* wnd )
  125. {
  126. CFrameWnd::OnSetFocus( wnd );
  127. if ( !m_image.IsNull() )
  128. {
  129. DrawCurves();
  130. }
  131. }
  132. void CMainFrame::OnMove( int cx, int cy )
  133. {
  134. CFrameWnd::OnMove( cx, cy );
  135. if ( !m_image.IsNull() )
  136. {
  137. DrawCurves();
  138. }
  139. }
  140. void CMainFrame::OnDraw( CDC* pDC )
  141. {
  142. if ( !m_image.IsNull() )
  143. {
  144. DrawCurves();
  145. }
  146. }
  147. CRect CMainFrame::GetTextRect()
  148. {
  149. CRect rect( 0, m_cRect.Height(),
  150. m_wRect.Width(), m_wRect.Height() );
  151. return rect;
  152. }
  153. void CMainFrame::DrawPoint( CDC *pDC, CPoint& pt, COLORREF color )
  154. {
  155. pDC->SetPixel( pt, color );
  156. }
  157. void CMainFrame::DrawCurves()
  158. {
  159. CDC *pDC = GetDC();
  160. CRect rect;
  161. GetClientRect(&rect);
  162. m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() );
  163. CBrush brush( RGB( 0, 0, 0 ) );
  164. pDC->FillRect( GetTextRect(), &brush );
  165. imt::IMTResult* eimResult = eimInterface.GetResult();
  166. if ( eimResult->numberOfPoints )
  167. {
  168. int i;
  169. COLORREF c_yellow = RGB(255,255,0);
  170. COLORREF c_green = RGB(0,255,0);
  171. COLORREF c_cyan = RGB(255,0,255);
  172. CPen pen(PS_SOLID, 1, c_yellow);
  173. CPen *pOldPen = pDC->SelectObject(&pen);
  174. for ( i = 0; i < eimResult->numberOfPoints; i++ )
  175. {
  176. imt::Point p = eimResult->vect_intima[i];
  177. if ( p.x >= 0 && p.y >= 0 ) // is a valid point ?
  178. {
  179. CPoint pt( p.x, p.y );
  180. DrawPoint( pDC, pt, c_yellow);
  181. }
  182. }
  183. CPen pen3(PS_SOLID, 1, c_cyan);
  184. pDC->SelectObject(&pen3);
  185. for ( i = 0; i < eimResult->numberOfPoints; i++ )
  186. {
  187. imt::Point p = eimResult->vect_media[i];
  188. if ( p.x >= 0 && p.y >= 0 ) // is a valid point ?
  189. {
  190. CPoint pt( p.x, p.y );
  191. DrawPoint( pDC, pt, c_cyan);
  192. }
  193. }
  194. CPen pen2(PS_SOLID, 1, c_green);
  195. pDC->SelectObject(&pen2);
  196. for ( i = 0; i < eimResult->numberOfPoints; i++ )
  197. {
  198. imt::Point p = eimResult->vect_adventitia[i];
  199. if ( p.x >= 0 && p.y >= 0 ) // is a valid point ?
  200. {
  201. CPoint pt( p.x, p.y );
  202. DrawPoint( pDC, pt, c_green);
  203. }
  204. }
  205. pDC->MoveTo(eimResult->p0.x, eimResult->p0.y);
  206. pDC->LineTo(eimResult->p1.x, eimResult->p1.y);
  207. pDC->SelectObject(pOldPen);
  208. DrawText();
  209. }
  210. }
  211. void CMainFrame::DrawText()
  212. {
  213. CDC *pDC = GetDC();
  214. CString text;
  215. imt::IMTResult* eimResult = eimInterface.GetResult();
  216. int i, nValid = 0, n = eimResult->numberOfPoints;
  217. for ( i = 0; i < n; i++ )
  218. {
  219. imt::Point p = eimResult->vect_intima[i];
  220. if ( p.x >= 0 && p.y >= 0 )
  221. {
  222. nValid++;
  223. }
  224. }
  225. text.Format( _T( "IMT: max=%.3lf ; mean=%.3lf ; std=%.3lf ; QI=%.3lf ; dist=%.3lf ;\nmean IT=%.3lf ; mean MT=%.3lf ; nValid=%d" ),
  226. eimResult->imt_max,
  227. eimResult->imt_mean,
  228. eimResult->imt_standardDeviation,
  229. eimResult->qualityIndex,
  230. eimResult->distance,
  231. eimResult->intima_mean,
  232. eimResult->media_mean,
  233. nValid );
  234. PrintText( text );
  235. }
  236. void CMainFrame::PrintCoordinates( CPoint pt )
  237. {
  238. CString text;
  239. text.Format( IDS_DEMO_COORDINATES, pt.x, pt.y );
  240. m_sBar.SetPaneText( 1, text );
  241. }
  242. void CMainFrame::PrintDistance( CPoint pt )
  243. {
  244. CString text;
  245. float d = eimInterface.getDistanceToFirstPoint( pt.x, pt.y );
  246. text.Format( IDS_DEMO_DISTANCE, pt.x, pt.y, d );
  247. m_sBar.SetPaneText( 1, text );
  248. }
  249. void CMainFrame::PrintText( CString text )
  250. {
  251. CDC *pDC = GetDC();
  252. DRAWTEXTPARAMS textParam;
  253. textParam.cbSize = sizeof( DRAWTEXTPARAMS );
  254. textParam.iLeftMargin = 0;
  255. textParam.iRightMargin = 0;
  256. textParam.iTabLength = 3;
  257. textParam.uiLengthDrawn = 0;
  258. COLORREF colorText = pDC->SetTextColor( RGB( 255, 255, 255 ) );
  259. COLORREF colorBk = pDC->SetBkColor( RGB( 0, 0, 0 ) );
  260. CFont newFont;
  261. newFont.CreateFont( 12, 0, 0, 0, FW_THIN, FALSE, FALSE, FALSE,
  262. ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  263. NONANTIALIASED_QUALITY ,FIXED_PITCH | FF_MODERN,
  264. _T( "Courier" ) );
  265. CFont* pOldFont = pDC->SelectObject( &newFont );
  266. pDC->DrawTextEx( text, GetTextRect(), DT_NOCLIP | DT_VCENTER, &textParam );
  267. pDC->SelectObject( pOldFont );
  268. if ( newFont.m_hObject != NULL )
  269. newFont.Detach();
  270. pDC->SetTextColor( colorText );
  271. pDC->SetBkColor( colorBk );
  272. }
  273. void CMainFrame::OnOpenfile()
  274. {
  275. CFileDialog cfd( TRUE, NULL, NULL,
  276. OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
  277. OFN_FILEMUSTEXIST | OFN_EXPLORER,
  278. _T( "Test Dicom file|*|Test raw|*.jpg;*.jpeg;*.bmp||" ), NULL );
  279. if ( cfd.DoModal() == IDOK )
  280. {
  281. USES_CONVERSION;
  282. if ( cfd.GetOFN().nFilterIndex == 1 )
  283. {
  284. m_initialized = eimInterface.initializeFromFile( T2A( cfd.GetPathName() ) );
  285. if ( m_initialized )
  286. {
  287. int x, dimX = eimInterface.getImageWidth();
  288. int y, dimY = eimInterface.getImageHeight();
  289. m_image.Create( dimX, dimY, eimInterface.getImageBitsPerPixel() );
  290. char* buffer = eimInterface.getPixelArray();
  291. for ( y = 0; y < dimY; y++ )
  292. {
  293. for ( x = 0; x < dimX; x++ )
  294. {
  295. m_image.SetPixelRGB( x, y, *buffer++, *buffer++, *buffer++ );
  296. }
  297. }
  298. }
  299. }
  300. else
  301. {
  302. if ( SUCCEEDED( m_image.Load( cfd.GetPathName() ) ) )
  303. {
  304. char* buffer = NULL;
  305. int width = m_image.GetWidth();
  306. int height = m_image.GetHeight();
  307. int x, y, bpp;
  308. unsigned long count = width * height;
  309. // test for a 24 bits buffer
  310. bpp = 24;
  311. buffer = new char[ 3 * count ];
  312. if ( buffer )
  313. {
  314. char* p = buffer;
  315. for ( y = 0; y < height; y++ )
  316. {
  317. for ( x = 0; x < width; x++ )
  318. {
  319. *p++ = GetRValue( m_image.GetPixel( x, y ) );
  320. *p++ = GetGValue( m_image.GetPixel( x, y ) );
  321. *p++ = GetBValue( m_image.GetPixel( x, y ) );
  322. }
  323. }
  324. }
  325. if ( buffer )
  326. {
  327. m_initialized = eimInterface.initializeFromRaw( buffer, width, height, bpp, FALSE, 0.093385, 0.093385 );
  328. }
  329. }
  330. }
  331. if ( m_initialized )
  332. {
  333. eimInterface.Clear();
  334. ResizeWindow();
  335. Invalidate( FALSE );
  336. }
  337. else
  338. {
  339. AfxMessageBox( _T( "File cannot be loaded" ), MB_ICONSTOP | MB_OK );
  340. }
  341. }
  342. }
  343. void CMainFrame::ResizeWindow()
  344. {
  345. CRect winRect;
  346. CRect cRect;
  347. CRect sRect;
  348. GetWindowRect( &winRect );
  349. GetClientRect( &cRect );
  350. int imgWidth = eimInterface.getImageWidth();
  351. int imgHeight = eimInterface.getImageHeight();
  352. m_sBar.GetWindowRect( &sRect );
  353. m_cRect = CRect( 0, 0, imgWidth, imgHeight );
  354. m_wRect = m_cRect;
  355. m_wRect.InflateRect( 0, 0, 0, 32 );
  356. ClientToScreen( &m_cRect );
  357. ClientToScreen( &m_wRect );
  358. SetWindowPos( NULL, 0, 0,
  359. winRect.Width() + imgWidth - cRect.Width(),
  360. winRect.Height() + imgHeight + 32 +
  361. sRect.Height() - cRect.Height(),
  362. SWP_NOMOVE | SWP_NOZORDER );
  363. DrawCurves();
  364. }