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