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