| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- // 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();
- }
|