Move the sources to trunk
[opencv] / filters / Tracker3dFilter / trackers / CamShiftTracker / CamShiftTrackerPropertyPage.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 // ////////////////////////////////////////////////////////////////////////////
43 //
44 // CamShiftTrackerPropertyPage
45 //
46 // This is a property page for the COM wrapper for the OpenCV CamShift tracker.
47 // This is intended as sample code only. It only supports calibration of
48 // the tracker.
49 // A complete implementation would have additional controls to control the
50 // CamShift tracker.
51 //
52 // ////////////////////////////////////////////////////////////////////////////
53
54 #include <string>
55 #include <stdio.h>
56 #include <streams.h>
57 #include <initguid.h>
58 #include "autorelease.h"
59 #include "ITracker3dFilter.h"
60 #include "ICamShiftTracker.h"
61 #include "CamShiftTrackerPropertyPage.h"
62 #include "resource.h"
63
64 //
65 // CreateInstance
66 //
67 // Used by the DirectShow base classes to create instances
68 //
69 CUnknown *CamShiftTrackerPropertyPage::CreateInstance(IUnknown *outer, HRESULT *phr)
70 {
71     CUnknown *punk = new CamShiftTrackerPropertyPage(outer, phr);
72     if (punk == NULL) {
73         *phr = E_OUTOFMEMORY;
74     }
75     return punk;
76
77 }
78
79 //
80 // Constructor
81 //
82 CamShiftTrackerPropertyPage::CamShiftTrackerPropertyPage(IUnknown *outer, HRESULT *phr) :
83     CBasePropertyPage(NAME("CamShift Tracker Property Page"), outer, IDD_CamShiftTrackerPropertyPage, IDS_TITLE)
84 {
85 }
86
87 CamShiftTrackerPropertyPage::~CamShiftTrackerPropertyPage()
88 {
89     for (int i = 0; i < m_trackers.size(); i++)
90         SAFE_RELEASE(m_trackers[i]);
91 }
92
93 //
94 // OnReceiveMessage
95 //
96 // Handles the messages for our property window
97 //
98 BOOL CamShiftTrackerPropertyPage::OnReceiveMessage(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
99 {
100     if (msg == WM_COMMAND)
101     {
102         switch (HIWORD(w))
103         {
104             case BN_CLICKED:
105                 switch (LOWORD(w))
106                 {
107                     case IDC_CALIBRATE:
108                         for (int i = 0; i < m_trackers.size(); i++)
109                             m_trackers[i]->Calibrate();
110                         break;
111                 }
112                 break;
113         }
114     }
115
116     return CBasePropertyPage::OnReceiveMessage(hwnd, msg, w, l);
117 }
118
119 //
120 // OnConnect
121 //
122 // Called when we connect to the filter
123 //
124 HRESULT CamShiftTrackerPropertyPage::OnConnect(IUnknown *pUnknown)
125 {
126     ITracker3dFilter *tracker_filter;
127
128     HRESULT hr = pUnknown->QueryInterface(IID_ITracker3dFilter, (void **) &tracker_filter);
129     if (FAILED(hr))
130         return hr;
131
132     std::vector<ITracker *> trackers;
133     tracker_filter->GetTrackers(trackers);
134     m_trackers.resize(0);
135     m_trackers.reserve(trackers.size());
136     for (int i = 0; i < trackers.size(); i++)
137     {
138         // Ensure that we are the correct property page for each filter
139         // Ignore any that request a different page
140         GUID page = GUID_NULL;
141         trackers[i]->GetPropertyPage(&page);
142         if (page == CLSID_CamShiftTrackerPropertyPage)
143         {
144             ICamShiftTracker *p;
145             trackers[i]->QueryInterface(IID_ICamShiftTracker, (void **)&p);
146             m_trackers.push_back(p);
147         }
148         trackers[i]->Release();
149     }
150
151     tracker_filter->Release();
152
153     return NOERROR;
154 }
155
156
157 //
158 // OnDisconnect
159 //
160 // Called when we disconnect from the filter
161 //
162 HRESULT CamShiftTrackerPropertyPage::OnDisconnect()
163 {
164     for (int i = 0; i < m_trackers.size(); i++)
165         SAFE_RELEASE(m_trackers[i]);
166     m_trackers.clear();
167
168     return NOERROR;
169 }
170
171 //
172 // OnActivate
173 //
174 // We are being activated
175 //
176 HRESULT CamShiftTrackerPropertyPage::OnActivate()
177 {
178     return NOERROR;
179 }
180
181 //
182 // OnDeactivate
183 //
184 // We are being deactivated
185 //
186 HRESULT CamShiftTrackerPropertyPage::OnDeactivate(void)
187 {
188     return NOERROR;
189 }
190
191 //
192 // OnApplyChanges
193 //
194 // Apply any changes so far made
195 //
196 HRESULT CamShiftTrackerPropertyPage::OnApplyChanges()
197 {
198     return NOERROR;
199 }