Move the sources to trunk
[opencv] / filters / Kalman / Kalmanprop.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*/
41
42 // This class implements the property page for the CKalmTrack filter
43
44 #include <cvstreams.h>
45 #include <commctrl.h>
46 #include <stdio.h>
47 #include <olectl.h>
48 #include <memory.h>
49 #include "resource.h"
50 #include "iKalman.h"
51 #include "Kalman.h"
52 #include "Kalmanprop.h"
53 #include "Kalmanuids.h"
54
55 //
56 // CreateInstance
57 //
58 // This goes in the factory template table to create new filter instances
59 //
60 CUnknown * WINAPI CKalmTrackProperties::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
61 {
62     CUnknown *punk = new CKalmTrackProperties(lpunk, phr);
63     if (punk == NULL) {
64     *phr = E_OUTOFMEMORY;
65     }
66     return punk;
67
68 } // CreateInstance
69
70
71 void CKalmTrackProperties::InitSlider( int id, int lower, int upper, int tic_freq )
72 {
73     HWND slider = GetDlgItem( m_hWnd, id );
74     if( slider )
75     {
76         SendMessage( slider, TBM_SETRANGE, TRUE, MAKELONG(lower, upper) );
77
78         if( tic_freq > 0 )
79         {
80             SendMessage( slider, TBM_SETTICFREQ, tic_freq, 0 );
81         }
82     }
83 }
84
85
86 void CKalmTrackProperties::SetSliderPos( int id, int pos )
87 {
88     HWND slider = GetDlgItem( m_hWnd, id );
89     if( slider )
90     {
91         SendMessage( slider, TBM_SETPOS, TRUE, pos );
92     }
93 }
94
95
96 int CKalmTrackProperties::GetSliderPos( int id )
97 {
98     HWND slider = GetDlgItem( m_hWnd, id );
99     int pos = 0;
100
101     if( slider )
102     {
103         pos = SendMessage( slider, TBM_GETPOS, 0, 0 );
104     }
105
106     return pos;
107 }
108
109
110 void CKalmTrackProperties::ReadParamsFromControls()
111 {
112     m_params.x = ((float)GetSliderPos( IDC_WIN_LEFT ))/SLIDER_MAX;
113     m_params.y = ((float)GetSliderPos( IDC_WIN_TOP ))/SLIDER_MAX;
114     m_params.width = ((float)GetSliderPos( IDC_WIN_WIDTH ))/SLIDER_MAX;
115     m_params.height = ((float)GetSliderPos( IDC_WIN_HEIGHT ))/SLIDER_MAX;
116
117     m_params.Smin = GetSliderPos( IDC_S_MIN );
118     m_params.Vmin = GetSliderPos( IDC_V_MIN );
119     m_params.view = IsDlgButtonChecked( m_hWnd, IDC_BACKPR );
120 }
121
122
123 void CKalmTrackProperties::WriteParamsToControls()
124 {
125     SetSliderPos( IDC_WIN_LEFT, cvRound( m_params.x * SLIDER_MAX ));
126     SetSliderPos( IDC_WIN_TOP,  cvRound( m_params.y * SLIDER_MAX ));
127     SetSliderPos( IDC_WIN_WIDTH, cvRound( m_params.width * SLIDER_MAX ));
128     SetSliderPos( IDC_WIN_HEIGHT, cvRound( m_params.height * SLIDER_MAX ));
129     
130     SetSliderPos( IDC_S_MIN, m_params.Smin );
131     SetSliderPos( IDC_V_MIN, m_params.Vmin );
132     CheckRadioButton( m_hWnd, IDC_NORMAL, IDC_BACKPR, m_params.view + IDC_NORMAL );
133 }
134
135 //
136 // Constructor
137 //
138 CKalmTrackProperties::CKalmTrackProperties(LPUNKNOWN pUnk, HRESULT *phr) :
139     CBasePropertyPage(NAME("Kalman Property Page"),pUnk,
140                       IDD_KALMAN,
141                       IDS_TITLE),
142     m_pCKalmTrack(NULL)
143 {
144     m_hWnd = 0;
145 } // (Constructor)
146
147
148 //
149 // SetDirty
150 //
151 // Sets m_bDirty and notifies the property page site of the change
152 //
153 void CKalmTrackProperties::SetDirty()
154 {
155     m_bDirty = TRUE;
156     if (m_pPageSite) {
157         m_pPageSite->OnStatusChange(PROPPAGESTATUS_DIRTY);
158     }
159
160 } // SetDirty
161
162
163 //
164 // OnReceiveMessage
165 //
166 // Virtual method called by base class with Window messages
167 //
168 BOOL CKalmTrackProperties::OnReceiveMessage(HWND hwnd,
169                                                   UINT uMsg,
170                                                   WPARAM wParam,
171                                                   LPARAM lParam)
172 {
173     switch (uMsg)
174     {        
175         case WM_INITDIALOG:
176             m_hWnd = hwnd;
177             
178             InitSlider( IDC_WIN_LEFT, 0, 1000, 0 );
179             InitSlider( IDC_WIN_TOP, 0, 1000, 0 );
180             InitSlider( IDC_WIN_WIDTH, 0, 1000, 0 );
181             InitSlider( IDC_WIN_HEIGHT, 0, 1000, 0 );
182
183             InitSlider( IDC_S_MIN, 0, 255, 5 );
184             InitSlider( IDC_V_MIN, 0, 255, 5 );
185
186             WriteParamsToControls();
187             break;
188         
189         case WM_HSCROLL:
190             OnApplyChanges();
191             break;
192         
193         case WM_COMMAND:
194
195             OnApplyChanges();
196             if(LOWORD(wParam) == IDC_START)
197             {
198                 m_pCKalmTrack->StartTracking();
199             }
200             else if (LOWORD(wParam) == IDC_STOP)
201             {
202                 m_pCKalmTrack->StopTracking();
203             }
204             break;
205     }
206     return CBasePropertyPage::OnReceiveMessage(hwnd,uMsg,wParam,lParam);
207
208 } // OnReceiveMessage
209
210
211 //
212 // OnConnect
213 //
214 // Called when the property page connects to a filter
215 //
216 HRESULT CKalmTrackProperties::OnConnect(IUnknown *pUnknown)
217 {
218     ASSERT(m_pCKalmTrack == NULL);
219
220     HRESULT hr = pUnknown->QueryInterface(IID_ICKalmTrack, (void **) &m_pCKalmTrack);
221     if (FAILED(hr)) {
222         return E_NOINTERFACE;
223     }
224
225     ASSERT(m_pCKalmTrack);
226     m_pCKalmTrack->GetParams(&m_params);
227    
228     return NOERROR;
229
230 } // OnConnect
231
232
233 //
234 // OnDisconnect
235 //
236 // Called when we're disconnected from a filter
237 //
238 HRESULT CKalmTrackProperties::OnDisconnect()
239 {
240     // Release of Interface after setting the appropriate contrast value
241
242     if (m_pCKalmTrack == NULL) {
243         return E_UNEXPECTED;
244     }
245     OnApplyChanges();
246     m_pCKalmTrack->Release();
247     m_pCKalmTrack = NULL;
248     return NOERROR;
249
250 } // OnDisconnect
251
252
253 //
254 // OnDeactivate
255 //
256 // We are being deactivated
257 //
258 HRESULT CKalmTrackProperties::OnDeactivate(void)
259 {
260     return NOERROR;
261
262 } // OnDeactivate
263
264
265 //
266 // OnApplyChanges
267 //
268 // Changes made should be kept. Change the  variable
269 //
270 HRESULT CKalmTrackProperties::OnApplyChanges()
271 {
272     ReadParamsFromControls();
273     m_pCKalmTrack->SetParams(&m_params);
274
275     m_bDirty = FALSE;
276     return(NOERROR);
277
278 } // OnApplyChanges
279
280
281