Update the changelog
[opencv] / filters / Tracker3dFilter / trackers / BlobTracker / BlobTrackerPropertyPage.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) 2002, 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 //   * Redistributions of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistributions 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 #include <string>
43 #include <stdio.h>
44 #include "cvstreams.h"
45 #include <initguid.h>
46 #include "autorelease.h"
47 #include "ITracker3dFilter.h"
48 #include "IBlobTracker.h"
49 #include "BlobTrackerPropertyPage.h"
50 #include "resource.h"
51
52 //
53 // CreateInstance
54 //
55 // Used by the DirectShow base classes to create instances
56 //
57 CUnknown *BlobTrackerPropertyPage::CreateInstance(IUnknown *outer, HRESULT *phr)
58 {
59     CUnknown *punk = new BlobTrackerPropertyPage(outer, phr);
60     if (punk == NULL) {
61         *phr = E_OUTOFMEMORY;
62     }
63     return punk;
64
65 }
66
67 //
68 // Constructor
69 //
70 BlobTrackerPropertyPage::BlobTrackerPropertyPage(IUnknown *outer, HRESULT *phr) :
71     CBasePropertyPage(NAME("Blob Tracker Property Page"), outer, IDD_BlobTrackerPropertyPage, IDS_TITLE),
72     m_en_change(false),
73     m_output_options(IBlobTracker::OUTPUT_CROSSHAIRS),
74     m_num_objects(0)
75 {
76 }
77
78 BlobTrackerPropertyPage::~BlobTrackerPropertyPage()
79 {
80     for (int i = 0; i < m_trackers.size(); i++)
81         SAFE_RELEASE(m_trackers[i]);
82 }
83
84 //
85 // OnReceiveMessage
86 //
87 // Handles the messages for our property window
88 //
89 BOOL BlobTrackerPropertyPage::OnReceiveMessage(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
90 {
91     int i;
92     if (msg == WM_COMMAND)
93     {
94         switch (HIWORD(w))
95         {
96             case BN_CLICKED:
97                 switch (LOWORD(w))
98                 {
99                     case IDC_COLOR_CLASSIFIED_PIXELS:
100                     case IDC_BOUNDING_BOX:
101                         SetOutputOptions();
102                         break;
103                 }
104                 break;
105
106             case EN_CHANGE:
107                 if (!m_en_change)
108                 {
109                     m_en_change = true;
110
111                     char text[20];
112                     GetWindowText(GetDlgItem(m_Dlg, LOWORD(w)), text, sizeof(text));
113                     int n = atoi(text);
114
115                     switch (LOWORD(w))
116                     {
117                         case IDC_NUMBER_OF_OBJECTS:
118                             if (n != m_num_objects)
119                             {
120                                 m_num_objects = n;
121
122                                 for (int i = 0; i < m_trackers.size(); i++)
123                                     m_trackers[i]->SetNumberOfObjects(m_num_objects);
124                             }
125                             break;
126
127                         case IDC_MIN_SIZE:
128                             for (i = 0; i < m_trackers.size(); i++)
129                                 m_trackers[i]->SetSizeThreshold(n);
130                             break;
131
132                         case IDC_THRESHOLD:
133                             for (i = 0; i < m_trackers.size(); i++)
134                                 m_trackers[i]->SetPixelThreshold(n);
135                             break;
136                     }
137
138                     m_en_change = false;
139                 }
140                 break;
141         }
142     }
143
144     return CBasePropertyPage::OnReceiveMessage(hwnd, msg, w, l);
145 }
146
147
148 // SetOutputOptions
149 void BlobTrackerPropertyPage::SetOutputOptions()
150 {
151     m_output_options = IBlobTracker::OUTPUT_CROSSHAIRS;
152
153     if (IsDlgButtonChecked(m_Dlg, IDC_COLOR_CLASSIFIED_PIXELS))
154         m_output_options |= IBlobTracker::OUTPUT_COLOR_PIXELS;
155
156     if (IsDlgButtonChecked(m_Dlg, IDC_BOUNDING_BOX))
157         m_output_options |= IBlobTracker::OUTPUT_BOUNDING_BOX;
158
159     for (int i = 0; i < m_trackers.size(); i++)
160         m_trackers[i]->SetOutputOptions(m_output_options);
161 }
162
163 //
164 // OnConnect
165 //
166 // Called when we connect to the filter
167 //
168 HRESULT BlobTrackerPropertyPage::OnConnect(IUnknown *pUnknown)
169 {
170     ITracker3dFilter *tracker_filter;
171
172     HRESULT hr = pUnknown->QueryInterface(IID_ITracker3dFilter, (void **) &tracker_filter);
173     if (FAILED(hr))
174         return hr;
175     
176     std::vector<ITracker *> trackers;
177     tracker_filter->GetTrackers(trackers);
178     m_trackers.resize(0);
179     m_trackers.reserve(trackers.size());
180     for (int i = 0; i < trackers.size(); i++)
181     {
182         // Ensure that we are the correct property page for each filter
183         // Ignore any that request a different page
184         GUID page = GUID_NULL;
185         trackers[i]->GetPropertyPage(&page);
186         if (page == CLSID_BlobTrackerPropertyPage)
187         {
188             IBlobTracker *p;
189             trackers[i]->QueryInterface(IID_IBlobTracker, (void **)&p);
190             m_trackers.push_back(p);
191         }
192         trackers[i]->Release();
193     }
194
195     tracker_filter->Release();
196
197     return NOERROR;
198 }
199
200
201 //
202 // OnDisconnect
203 //
204 // Called when we disconnect from the filter
205 //
206 HRESULT BlobTrackerPropertyPage::OnDisconnect()
207 {
208     for (int i = 0; i < m_trackers.size(); i++)
209         SAFE_RELEASE(m_trackers[i]);
210     m_trackers.clear();
211
212     return NOERROR;
213 }
214
215 //
216 // OnActivate
217 //
218 // We are being activated
219 //
220 HRESULT BlobTrackerPropertyPage::OnActivate()
221 {
222     SetWindowText(GetDlgItem(m_Dlg, IDC_NUMBER_OF_OBJECTS), "1");
223     SetOutputOptions();
224
225     if (m_trackers.size() > 0 && m_trackers[0] != NULL)
226     {
227         int i;
228         m_en_change = true;
229
230         m_trackers[0]->GetPixelThreshold(i);
231         SetDlgItemInt(m_Dlg, IDC_THRESHOLD, i, false);
232
233         m_trackers[0]->GetSizeThreshold(i);
234         SetDlgItemInt(m_Dlg, IDC_MIN_SIZE, i, false);
235
236         m_en_change = false;
237     }
238     
239     return NOERROR;
240 }
241
242 //
243 // OnDeactivate
244 //
245 // We are being deactivated
246 //
247 HRESULT BlobTrackerPropertyPage::OnDeactivate(void)
248 {
249     return NOERROR;
250 }
251
252 //
253 // OnApplyChanges
254 //
255 // Apply any changes so far made
256 //
257 HRESULT BlobTrackerPropertyPage::OnApplyChanges()
258 {
259     return NOERROR;
260 }