// MainFrm.cpp : implementation of class CMainFrame // #include "stdafx.h" #include "IMTDemo.h" #include "MainFrm.h" #include "Resource.h" // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) ON_COMMAND(ID_APP_EXIT, OnClose) ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_WM_CREATE() ON_WM_SETCURSOR() ON_WM_SETFOCUS() ON_WM_MOVE() ON_COMMAND(ID_OPENFILE, OnOpenfile) //}}AFX_MSG_MAP END_MESSAGE_MAP() // CMainFrame constructor CMainFrame::CMainFrame() { firstPoint = 1; m_canChange = FALSE; m_initialized = FALSE; } CMainFrame::~CMainFrame() { } void CMainFrame::SetRect( RECT& rect ) { m_wRect = rect; m_cRect = rect; m_cRect.DeflateRect( 0, 0, 0, 32 ); } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; static UINT indicators[] = { ID_SEPARATOR, ID_SEPARATOR }; m_sBar.Create( this ); m_sBar.SetIndicators( indicators, 2 ); m_sBar.SetPaneInfo( 1, m_sBar.GetItemID( 1 ), SBPS_NORMAL, 300 ); m_sBar.SetPaneInfo( 0, m_sBar.GetItemID( 0 ), SBPS_STRETCH, NULL ); m_hArrow = AfxGetApp()->LoadStandardCursor( IDC_ARROW ); m_hCross = AfxGetApp()->LoadStandardCursor( IDC_CROSS ); if ( !eimInterface.IsValid() ) { AfxMessageBox( _T( "LibIMTDLL.dll not found in the directory of the executable" ) ); return -1; } return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; cs.style = WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU; return TRUE; } // Message manager for CMainFrame void CMainFrame::OnLButtonUp( UINT nFlags, CPoint point ) { CPoint pt = point; ClientToScreen( &pt ); m_canChange = m_cRect.PtInRect( pt ); if ( m_canChange && m_initialized ) { if ( firstPoint ) { if ( eimInterface.GetResult()->numberOfPoints ) { DrawCurves(); PrintCoordinates( point ); } eimInterface.setFirstPoint( point ); } else { eimInterface.setSecondPoint( point ); DrawCurves(); } firstPoint ^= 1; } } void CMainFrame::OnMouseMove(UINT nFlags, CPoint point) { CPoint pt = point; ClientToScreen( &pt ); m_canChange = m_cRect.PtInRect( pt ); if ( firstPoint || !m_initialized ) { PrintCoordinates( point ); } else if ( m_canChange && !firstPoint ) { CDC *pDC = GetDC(); CRect rect; GetClientRect(&rect); m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() ); CPen pen( PS_SOLID, 1, RGB( 0, 255, 0 ) ); CPen *pOldPen = pDC->SelectObject( &pen ); imt::Point p = eimInterface.GetResult()->p0; pDC->MoveTo( p.x, p.y ); pDC->LineTo( point ); pDC->SelectObject( pOldPen ); PrintDistance( point ); } } BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { if ( m_initialized && firstPoint && m_canChange && ( nHitTest == HTCLIENT ) ) { ::SetCursor( m_hCross ); return TRUE; } return CFrameWnd::OnSetCursor( pWnd, nHitTest, message ); } void CMainFrame::OnSetFocus( CWnd* wnd ) { CFrameWnd::OnSetFocus( wnd ); if ( !m_image.IsNull() ) { DrawCurves(); } } void CMainFrame::OnMove( int cx, int cy ) { CFrameWnd::OnMove( cx, cy ); if ( !m_image.IsNull() ) { DrawCurves(); } } void CMainFrame::OnDraw( CDC* pDC ) { if ( !m_image.IsNull() ) { DrawCurves(); } } CRect CMainFrame::GetTextRect() { CRect rect( 0, m_cRect.Height(), m_wRect.Width(), m_wRect.Height() ); return rect; } void CMainFrame::DrawPoint( CDC *pDC, CPoint& pt, COLORREF color ) { pDC->SetPixel( pt, color ); } void CMainFrame::DrawCurves() { CDC *pDC = GetDC(); CRect rect; GetClientRect(&rect); m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() ); CBrush brush( RGB( 0, 0, 0 ) ); pDC->FillRect( GetTextRect(), &brush ); imt::IMTResult* eimResult = eimInterface.GetResult(); if ( eimResult->numberOfPoints ) { int i; COLORREF c_yellow = RGB(255,255,0); COLORREF c_green = RGB(0,255,0); COLORREF c_cyan = RGB(255,0,255); CPen pen(PS_SOLID, 1, c_yellow); CPen *pOldPen = pDC->SelectObject(&pen); for ( i = 0; i < eimResult->numberOfPoints; i++ ) { imt::Point p = eimResult->vect_intima[i]; if ( p.x >= 0 && p.y >= 0 ) // is a valid point ? { CPoint pt( p.x, p.y ); DrawPoint( pDC, pt, c_yellow); } } CPen pen3(PS_SOLID, 1, c_cyan); pDC->SelectObject(&pen3); for ( i = 0; i < eimResult->numberOfPoints; i++ ) { imt::Point p = eimResult->vect_media[i]; if ( p.x >= 0 && p.y >= 0 ) // is a valid point ? { CPoint pt( p.x, p.y ); DrawPoint( pDC, pt, c_cyan); } } CPen pen2(PS_SOLID, 1, c_green); pDC->SelectObject(&pen2); for ( i = 0; i < eimResult->numberOfPoints; i++ ) { imt::Point p = eimResult->vect_adventitia[i]; if ( p.x >= 0 && p.y >= 0 ) // is a valid point ? { CPoint pt( p.x, p.y ); DrawPoint( pDC, pt, c_green); } } pDC->MoveTo(eimResult->p0.x, eimResult->p0.y); pDC->LineTo(eimResult->p1.x, eimResult->p1.y); pDC->SelectObject(pOldPen); DrawText(); } } void CMainFrame::DrawText() { CDC *pDC = GetDC(); CString text; imt::IMTResult* eimResult = eimInterface.GetResult(); int i, nValid = 0, n = eimResult->numberOfPoints; for ( i = 0; i < n; i++ ) { imt::Point p = eimResult->vect_intima[i]; if ( p.x >= 0 && p.y >= 0 ) { nValid++; } } text.Format( _T( "IMT: max=%.3lf ; mean=%.3lf ; std=%.3lf ; QI=%.3lf ; dist=%.3lf ;\nmean IT=%.3lf ; mean MT=%.3lf ; nValid=%d" ), eimResult->imt_max, eimResult->imt_mean, eimResult->imt_standardDeviation, eimResult->qualityIndex, eimResult->distance, eimResult->intima_mean, eimResult->media_mean, nValid ); PrintText( text ); } void CMainFrame::PrintCoordinates( CPoint pt ) { CString text; text.Format( IDS_DEMO_COORDINATES, pt.x, pt.y ); m_sBar.SetPaneText( 1, text ); } void CMainFrame::PrintDistance( CPoint pt ) { CString text; float d = eimInterface.getDistanceToFirstPoint( pt.x, pt.y ); text.Format( IDS_DEMO_DISTANCE, pt.x, pt.y, d ); m_sBar.SetPaneText( 1, text ); } void CMainFrame::PrintText( CString text ) { CDC *pDC = GetDC(); DRAWTEXTPARAMS textParam; textParam.cbSize = sizeof( DRAWTEXTPARAMS ); textParam.iLeftMargin = 0; textParam.iRightMargin = 0; textParam.iTabLength = 3; textParam.uiLengthDrawn = 0; COLORREF colorText = pDC->SetTextColor( RGB( 255, 255, 255 ) ); COLORREF colorBk = pDC->SetBkColor( RGB( 0, 0, 0 ) ); CFont newFont; newFont.CreateFont( 12, 0, 0, 0, FW_THIN, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, NONANTIALIASED_QUALITY ,FIXED_PITCH | FF_MODERN, _T( "Courier" ) ); CFont* pOldFont = pDC->SelectObject( &newFont ); pDC->DrawTextEx( text, GetTextRect(), DT_NOCLIP | DT_VCENTER, &textParam ); pDC->SelectObject( pOldFont ); if ( newFont.m_hObject != NULL ) newFont.Detach(); pDC->SetTextColor( colorText ); pDC->SetBkColor( colorBk ); } void CMainFrame::OnOpenfile() { CFileDialog cfd( TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER, _T( "Test Dicom file|*|Test raw|*.jpg;*.jpeg;*.bmp||" ), NULL ); if ( cfd.DoModal() == IDOK ) { USES_CONVERSION; if ( cfd.GetOFN().nFilterIndex == 1 ) { m_initialized = eimInterface.initializeFromFile( T2A( cfd.GetPathName() ) ); if ( m_initialized ) { int x, dimX = eimInterface.getImageWidth(); int y, dimY = eimInterface.getImageHeight(); m_image.Create( dimX, dimY, eimInterface.getImageBitsPerPixel() ); char* buffer = eimInterface.getPixelArray(); for ( y = 0; y < dimY; y++ ) { for ( x = 0; x < dimX; x++ ) { m_image.SetPixelRGB( x, y, *buffer++, *buffer++, *buffer++ ); } } } } else { if ( SUCCEEDED( m_image.Load( cfd.GetPathName() ) ) ) { char* buffer = NULL; int width = m_image.GetWidth(); int height = m_image.GetHeight(); int x, y, bpp; unsigned long count = width * height; // test for a 24 bits buffer bpp = 24; buffer = new char[ 3 * count ]; if ( buffer ) { char* p = buffer; for ( y = 0; y < height; y++ ) { for ( x = 0; x < width; x++ ) { *p++ = GetRValue( m_image.GetPixel( x, y ) ); *p++ = GetGValue( m_image.GetPixel( x, y ) ); *p++ = GetBValue( m_image.GetPixel( x, y ) ); } } } if ( buffer ) { m_initialized = eimInterface.initializeFromRaw( buffer, width, height, bpp, FALSE, 0.093385, 0.093385 ); } } } if ( m_initialized ) { eimInterface.Clear(); ResizeWindow(); Invalidate( FALSE ); } else { AfxMessageBox( _T( "File cannot be loaded" ), MB_ICONSTOP | MB_OK ); } } } void CMainFrame::ResizeWindow() { CRect winRect; CRect cRect; CRect sRect; GetWindowRect( &winRect ); GetClientRect( &cRect ); int imgWidth = eimInterface.getImageWidth(); int imgHeight = eimInterface.getImageHeight(); m_sBar.GetWindowRect( &sRect ); m_cRect = CRect( 0, 0, imgWidth, imgHeight ); m_wRect = m_cRect; m_wRect.InflateRect( 0, 0, 0, 32 ); ClientToScreen( &m_cRect ); ClientToScreen( &m_wRect ); SetWindowPos( NULL, 0, 0, winRect.Width() + imgWidth - cRect.Width(), winRect.Height() + imgHeight + 32 + sRect.Height() - cRect.Height(), SWP_NOMOVE | SWP_NOZORDER ); DrawCurves(); }