Update the changelog
[opencv] / apps / HMMDemo / HMMDemoView.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/// HMMDemoView.cpp : implementation of the CHMMDemoView class
41 //
42
43 #include "stdafx.h"
44 #include "HMMDemo.h"
45
46 #include "HMMDemoDoc.h"
47 #include "HMMDemoView.h"
48
49 #ifdef _DEBUG
50 #define new DEBUG_NEW
51 #undef THIS_FILE
52 static char THIS_FILE[] = __FILE__;
53 #endif
54
55 /////////////////////////////////////////////////////////////////////////////
56 // CHMMDemoView
57
58 IMPLEMENT_DYNCREATE(CHMMDemoView, CView)
59
60 BEGIN_MESSAGE_MAP(CHMMDemoView, CView)
61         //{{AFX_MSG_MAP(CHMMDemoView)
62         ON_WM_DESTROY()
63         ON_WM_LBUTTONDOWN()
64         ON_WM_LBUTTONUP()
65         ON_WM_MOUSEMOVE()
66         ON_WM_ERASEBKGND()
67         //}}AFX_MSG_MAP
68 END_MESSAGE_MAP()
69
70 /////////////////////////////////////////////////////////////////////////////
71 // CHMMDemoView construction/destruction
72
73 CHMMDemoView::CHMMDemoView()
74 {
75     m_sel = m_tmp_sel = CRect(0,0,0,0);
76     m_imageList = 0;
77     m_TestPath = "";  
78 }
79
80 CHMMDemoView::~CHMMDemoView()
81 {
82     SetImageList(0);
83 }
84
85 BOOL CHMMDemoView::PreCreateWindow(CREATESTRUCT& cs)
86 {
87         return CView::PreCreateWindow(cs);
88 }
89
90 /////////////////////////////////////////////////////////////////////////////
91 // CHMMDemoView drawing
92
93 void CHMMDemoView::OnDraw(CDC* pDC)
94 {
95         CHMMDemoDoc* pDoc = GetDocument();
96         ASSERT_VALID(pDoc);
97     
98     HDC dstDC = pDC->m_hDC;
99     CImage& frame = m_camera.GetFrame();
100
101     CRect r;
102     GetClipBox( dstDC, &r );
103
104     if( m_imageList && m_imageList->GetCount() > 1 ) //several images was selected
105     {
106         pDC->Draw3dRect(0,0,frame.Width(),frame.Height(),RGB(0,0,0),RGB(128,128,128));
107     }
108
109         //force image to be top-left
110         IplImage* img = frame.GetImage();
111         if( img ) img->origin = IPL_ORIGIN_TL;
112         frame.Show( dstDC, r.left, r.top, r.Width(), r.Height(), r.left, r.top ); 
113
114     if( m_sel.Width() != 0 && m_sel.Height() != 0 )
115     {
116         HGDIOBJ old = SelectObject( dstDC, GetStockObject( WHITE_PEN ));
117         POINT pt[] = {
118             { m_sel.left - r.left, m_sel.top - r.top },
119             { m_sel.right - r.left - 1, m_sel.top - r.top },
120             { m_sel.right - r.left - 1, m_sel.bottom - r.top - 1},
121             { m_sel.left - r.left, m_sel.bottom - r.top - 1},
122             { m_sel.left - r.left, m_sel.top - r.top }
123         };
124
125         Polyline( dstDC, pt, 5 );
126         SelectObject( dstDC, old );
127     }
128
129     //::ReleaseDC( m_hWnd, dstDC );
130 }
131
132 /////////////////////////////////////////////////////////////////////////////
133 // CHMMDemoView message handlers
134
135 void CHMMDemoView::OnInitialUpdate() 
136 {
137     CView::OnInitialUpdate();
138
139     if( m_camera.Initialize( 320, 240, -1, m_hWnd ) == 0 )
140     {
141         MessageBox("Can't initialize camera. Try to change format","Error", MB_OK|MB_ICONERROR );
142     }
143     Camera().Start();
144 }
145
146 void CHMMDemoView::OnDestroy() 
147 {
148     m_camera.Uninitialize();
149     CView::OnDestroy();
150 }
151
152 void CHMMDemoView::OnLButtonDown(UINT nFlags, CPoint point) 
153 {
154     CView::OnLButtonDown(nFlags, point);                                       
155     /*(CPoint&)m_sel = */(CPoint&)m_tmp_sel.left = (CPoint&)m_tmp_sel.right = point;
156     CheckUpdate();
157 }
158
159 void CHMMDemoView::OnLButtonUp(UINT nFlags, CPoint point) 
160 {
161     CView::OnLButtonUp(nFlags, point);
162     (CPoint&)m_tmp_sel.right = point;
163     CheckUpdate();
164 }
165
166 void CHMMDemoView::OnMouseMove(UINT nFlags, CPoint point) 
167 {
168     CView::OnMouseMove(nFlags, point);
169     if( nFlags & MK_LBUTTON )
170     {
171         (CPoint&)m_tmp_sel.right = point;
172         CheckUpdate();
173     }
174 }
175
176
177 void CHMMDemoView::CheckUpdate()
178 {
179     m_sel = NormalizeRect( m_tmp_sel );
180     if( !Camera().IsRunning() )
181     {
182         CImage& img = Camera().GetFrame();
183         CRect ir( 0, 0, img.Width(), img.Height() );
184
185         InvalidateRect(&ir, FALSE);
186         UpdateWindow();
187     }
188 }
189
190 BOOL CHMMDemoView::OnEraseBkgnd(CDC* pDC) 
191 {
192         CImage& img = Camera().GetFrame();
193     CRect ir( 0, 0, img.Width(), img.Height() );
194     CRect r;
195     pDC->GetClipBox( &r );
196
197     if( ir.PtInRect( r.TopLeft()) && ir.PtInRect( r.BottomRight()))
198         return TRUE;
199
200     return CView::OnEraseBkgnd(pDC);
201 }
202
203 void CHMMDemoView::SetSelection( RECT* sel )
204 {
205     if( sel )
206     {
207         m_tmp_sel = NormalizeRect( *sel );
208     }
209     else
210     {
211         CImage& img = Camera().GetFrame();
212         m_tmp_sel = CRect( 0, 0, img.Width(), img.Height() );
213     }
214     CheckUpdate();
215 }
216
217 void  CHMMDemoView::SetImageList( CStringList* imageList )
218 {
219     if( m_imageList ) delete m_imageList;
220     m_imageList = imageList;
221 }
222
223
224 BOOL CHMMDemoView::SetTestPath(char* path)
225 {
226    m_TestPath = path;
227    return TRUE;
228 }
229