1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
40 //M*/// ImageBaseView.cpp : implementation file
45 #include "ImageBaseView.h"
46 #include "HMMDemoDoc.h"
52 static char THIS_FILE[] = __FILE__;
55 /////////////////////////////////////////////////////////////////////////////
58 IMPLEMENT_DYNCREATE(CImageBaseView, CScrollView)
60 CImageBaseView::CImageBaseView()
62 m_canvas.Create( GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 8 );
63 m_rgbcanvas.Create( GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 24 );
65 IplImage* rgb = m_rgbcanvas.GetImage();
66 strcpy( rgb->colorModel, "RGB" );
67 strcpy( rgb->channelSeq, "BGR" );
69 IplImage* gray = m_canvas.GetImage();
70 strncpy( gray->colorModel, "GRAY", 4 );
71 strncpy( gray->channelSeq, "GRAY", 4 );
74 m_base_size = CSize( 100, 120 );
79 m_pen = CreatePen( PS_SOLID, 3, RGB(255,0,255));
87 CImageBaseView::~CImageBaseView()
89 DeleteObject( m_pen );
93 BEGIN_MESSAGE_MAP(CImageBaseView, CScrollView)
94 //{{AFX_MSG_MAP(CImageBaseView)
102 /////////////////////////////////////////////////////////////////////////////
103 // CImageBaseView drawing
105 void CImageBaseView::RefreshView()
107 CHMMDemoDoc* doc = GetDocument();
108 CSize virt_size = CSize( 10, 10 );
115 m_count = doc->GetFaceBase().GetPersonList().GetCount();
119 CPerson* person = doc->GetFaceBase().GetPerson( m_index );
125 m_count = person->GetImgList().GetCount();
128 GetClientRect( &win_rect );
130 m_margin = CSize((m_pic_delta.cx - m_pic_size.cx)/2,
131 (m_pic_delta.cy - m_pic_size.cy)/2);
133 m_nx = (win_rect.Width() - m_margin.cx +
134 m_pic_delta.cx - m_pic_size.cx)/m_pic_delta.cx;
136 m_nx = MAX( m_nx, 1 );
140 m_ny = (m_count + m_nx - 1)/m_nx;
141 virt_size.cx = win_rect.Width();
142 virt_size.cy = MAX( m_ny, 1 )*m_pic_delta.cy;
145 SetScrollSizes( MM_TEXT, virt_size );
149 void CImageBaseView::OnInitialUpdate()
151 CScrollView::OnInitialUpdate();
152 m_count = m_nx = m_ny = 0;
160 void CImageBaseView::OnDraw(CDC* pDC)
162 CHMMDemoDoc* doc = GetDocument();
165 CFaceBase& base = doc->GetFaceBase();
168 int y_pos = GetScrollPosition().y;
171 dc = ::GetDC( m_hWnd );
173 GetClientRect( &win_rect );
174 win_size = CSize( win_rect.Width(), win_rect.Height() );
176 m_canvas.Fill( GetSysColor( COLOR_WINDOW ));
177 base.Draw( m_index, m_canvas, win_size, y_pos, m_pic_size, m_pic_delta );
179 if( m_canvas.GetImage() && m_rgbcanvas.GetImage() )
181 IplImage* src = m_canvas.GetImage();
182 IplImage* dst = m_rgbcanvas.GetImage();
183 CvRect sr = cvGetImageROI( src ), dr = cvGetImageROI( dst );
185 cvSetImageROI( src, RectToCvRect( win_rect ));
186 cvSetImageROI( dst, RectToCvRect( win_rect ));
188 cvCvtColor( src, dst, CV_GRAY2BGR );
189 cvSetImageROI( src, sr );
190 cvSetImageROI( dst, dr );
192 m_rgbcanvas.Show( dc, 0, 0, win_size.cx, win_size.cy, 0, 0 );
197 HGDIOBJ pen = SelectObject( dc, m_pen );
198 CRect rect = GetRectByIndex( m_active );
201 pt[0] = CPoint( rect.left, rect.top - y_pos );
202 pt[1] = CPoint( rect.right, rect.top - y_pos );
203 pt[2] = CPoint( rect.right, rect.bottom - y_pos );
204 pt[3] = CPoint( rect.left, rect.bottom - y_pos );
207 Polyline( dc, pt, 5 );
208 SelectObject( dc, pen );
211 int trained_index = base.GetTrainedIndex();
213 if( m_index >= 0 && trained_index >= 0 )
215 CRect rect = GetRectByIndex( trained_index );
216 if( !rect.IsRectEmpty())
218 CPerson* person = base.GetPerson( m_index );
219 ASSERT( person != 0 );
221 CPersonImgList& list = person->GetImgList();
222 CPersonImage* image = list.GetAt( list.FindIndex(trained_index));
224 ASSERT( image != 0 );
226 image->CalcRect( win_size, rect.TopLeft(), m_pic_size, sr, dr );
228 if( !sr.IsRectEmpty() && !dr.IsRectEmpty())
230 SetStretchBltMode( dc, COLORONCOLOR );
231 base.GetTrainedImage().DrawToHDC( dc, &dr );
236 ::ReleaseDC( m_hWnd, dc );
240 /////////////////////////////////////////////////////////////////////////////
241 // CImageBaseView message handlers
243 void CImageBaseView::RecalcPictureSize()
245 double scale = pow( sqrt(2), m_log_shift );
246 m_pic_size.cx = (int)(m_base_size.cx*scale + 0.5);
247 m_pic_size.cy = (int)(m_base_size.cy*scale + 0.5);
248 m_pic_delta.cx = m_pic_size.cx*5/4;
249 m_pic_delta.cy = m_pic_size.cy*5/4;
252 void CImageBaseView::Zoom( bool zoom_in )
256 m_log_shift = MIN( ZOOM_MAX, m_log_shift + 1 );
260 m_log_shift = MAX( ZOOM_MIN, m_log_shift - 1 );
268 void CImageBaseView::OnSize(UINT nType, int cx, int cy)
270 CScrollView::OnSize(nType, cx, cy);
275 BOOL CImageBaseView::OnEraseBkgnd(CDC* /*pDC*/)
281 CRect CImageBaseView::GetRectByIndex( int index )
283 CRect rect = CRect(0,0,0,0);
284 if( (unsigned)index >= (unsigned)m_count ) return rect;
286 rect.left = index % m_nx;
287 rect.top = index / m_nx;
289 rect.left = rect.left*m_pic_delta.cx + m_margin.cx;
290 rect.top = rect.top*m_pic_delta.cy + m_margin.cy;
292 rect.right = rect.left + m_pic_size.cx;
293 rect.bottom = rect.top + m_pic_size.cy;
299 int CImageBaseView::GetIndexByPoint( CPoint pt )
301 int y_pos = GetScrollPosition().y;
303 int x = (pt.x - m_margin.cx)/m_pic_delta.cx;
304 int y = (pt.y + y_pos - m_margin.cy)/m_pic_delta.cy;
307 if( x >= m_nx || y >= m_ny ) return -1;
309 if( pt.x > (x+1)*m_pic_delta.cx - m_margin.cx ||
310 y_pos + pt.y > (y+1)*m_pic_delta.cy - m_margin.cy ) return -1;
313 if( index >= m_count ) return -1;
319 void CImageBaseView::OnLButtonDown(UINT nFlags, CPoint point)
321 CScrollView::OnLButtonDown(nFlags, point);
323 int index = GetIndexByPoint( point );
332 void CImageBaseView::SwitchMode( int index, bool to_personal )
334 CHMMDemoDoc* doc = GetDocument();
338 CPerson* person = doc->GetFaceBase().GetPerson( index );
349 m_view_mode = VIEW_PERSON;
353 if( person->IsModified())
357 person->UnloadRest();
360 m_view_mode = VIEW_BASE;
367 void CImageBaseView::OnLButtonDblClk(UINT nFlags, CPoint point)
369 CScrollView::OnLButtonDblClk(nFlags, point);
371 int index = GetIndexByPoint( point );
372 if( index >= 0 && m_index == -1 )
374 SwitchMode( index, true );
376 else if( index < 0 && m_index >= 0 )
378 SwitchMode( m_index, false );
382 int CImageBaseView::GetMode()