1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
40 //M*/// MainFrm.cpp : implementation of the CMainFrame class
48 #include "ProxyTransuids.h"
60 static char THIS_FILE[] = __FILE__;
63 #define WM_GRAPHNOTIFY WM_USER+13
65 /////////////////////////////////////////////////////////////////////////////
68 IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
70 BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
71 //{{AFX_MSG_MAP(CMainFrame)
74 ON_COMMAND(ID_VIDEO_PP, OnVideoPp)
75 ON_UPDATE_COMMAND_UI(ID_VIDEO_PP, OnUpdateVideoPp)
76 ON_COMMAND(ID_PIN_PP, OnPinPp)
77 ON_UPDATE_COMMAND_UI(ID_PIN_PP, OnUpdatePinPp)
81 static UINT indicators[] =
83 ID_SEPARATOR, // status line indicator
84 ID_SEPARATOR, // x-coordinate of the cursor
85 ID_SEPARATOR, // y-coordinate of the cursor
89 /////////////////////////////////////////////////////////////////////////////
90 // CMainFrame construction/destruction
92 CMainFrame::CMainFrame() : m_SourceFilter(0), m_FilterGraph(0), m_GraphBuilder(0),
93 m_MediaControl(0), m_MediaEventEx(0), m_VideoWindow(0), m_ProxyTrans(0),
94 m_isIntRun(FALSE), m_isDSRunning(0), m_pRunDoc(0)
96 // TODO: add member initialization code here
100 CMainFrame::~CMainFrame()
104 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
106 if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
109 if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
110 | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
111 !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
113 TRACE0("Failed to create toolbar\n");
114 return -1; // fail to create
117 if (!m_eicToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
118 | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
119 !m_eicToolBar.LoadToolBar(IDR_EICTOOLBAR))
121 TRACE0("Failed to create toolbar\n");
122 return -1; // fail to create
125 if (!m_wndStatusBar.Create(this) ||
126 !m_wndStatusBar.SetIndicators(indicators,
127 sizeof(indicators)/sizeof(UINT)))
129 TRACE0("Failed to create status bar\n");
130 return -1; // fail to create
133 // Setup the status bar
134 m_wndStatusBar.SetPaneInfo(1, ID_SEPARATOR, SBPS_NORMAL, 20);
135 m_wndStatusBar.SetPaneInfo(2, ID_SEPARATOR, SBPS_NORMAL, 20);
136 // TODO: Delete these three lines if you don't want the toolbar to
138 m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
139 m_eicToolBar.EnableDocking(CBRS_ALIGN_ANY);
140 EnableDocking(CBRS_ALIGN_ANY);
141 DockControlBar(&m_wndToolBar);
142 DockControlBar(&m_eicToolBar);
144 // Initialize DirectShow
146 /*if(CreateCamera() && CreateFilterGraph())
148 //Camera video source is available
159 BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
161 if( !CMDIFrameWnd::PreCreateWindow(cs) )
163 // TODO: Modify the Window class or styles here by modifying
164 // the CREATESTRUCT cs
169 /////////////////////////////////////////////////////////////////////////////
170 // CMainFrame diagnostics
173 void CMainFrame::AssertValid() const
175 CMDIFrameWnd::AssertValid();
178 void CMainFrame::Dump(CDumpContext& dc) const
180 CMDIFrameWnd::Dump(dc);
185 IPin* get_pin( IBaseFilter* pFilter, PIN_DIRECTION dir )
187 IEnumPins* pEnumPins = 0;
192 pFilter->EnumPins( &pEnumPins );
198 PIN_DIRECTION pinDir = PIN_DIRECTION(-1);
201 pEnumPins->Next( 1, &pPin, &cFetched );
202 if( cFetched == 1 && pPin != 0 )
204 pPin->QueryDirection( &pinDir );
205 if( pinDir == dir ) break;
208 else if(cFetched == 0)
213 pEnumPins->Release();
220 BOOL CMainFrame::CreateCamera()
222 ICreateDevEnum* pCreateDevEnum = 0;
223 IEnumMoniker* pEnumMon = 0;
227 SafeRelease( m_SourceFilter );
229 if(FAILED(CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
230 IID_ICreateDevEnum, (void**)&pCreateDevEnum )))
235 ASSERT(pCreateDevEnum);
236 /* Create capture device */
237 if(FAILED(pCreateDevEnum->CreateClassEnumerator(
238 CLSID_VideoInputDeviceCategory, &pEnumMon, 0)) || !pEnumMon)
240 SafeRelease(pCreateDevEnum);
245 if(SUCCEEDED( pEnumMon->Next(1, &pMon, &cFetched)) && cFetched == 1 && pMon)
248 pMon->BindToObject(0, 0, IID_IBaseFilter, (void **)&m_SourceFilter );
251 SafeRelease(pCreateDevEnum);
252 SafeRelease(pEnumMon);
256 m_SourceFilter->QueryFilterInfo(&info);
260 SafeRelease(pCreateDevEnum);
261 SafeRelease(pEnumMon);
265 // Create a proxy transform filter to enable scripts process images
266 if(FAILED(CoCreateInstance(CLSID_ProxyTransform, NULL, CLSCTX_INPROC_SERVER,
267 IID_IProxyTransform, (void**)&m_ProxyTrans)))
269 SafeRelease(pCreateDevEnum);
270 SafeRelease(pEnumMon);
271 SafeRelease(m_SourceFilter);
277 SafeRelease( pEnumMon );
278 SafeRelease( pCreateDevEnum );
284 BOOL CMainFrame::CreateFilterGraph()
287 if(FAILED(CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
288 IID_IGraphBuilder, (void **)&m_GraphBuilder )))
290 SafeRelease(m_GraphBuilder);
294 ASSERT(m_GraphBuilder);
295 m_GraphBuilder->QueryInterface(IID_IMediaControl,(void**)&m_MediaControl);
296 m_GraphBuilder->QueryInterface(IID_IMediaEventEx,(void**)&m_MediaEventEx);
297 m_GraphBuilder->QueryInterface(IID_IVideoWindow, (void**)&m_VideoWindow);
298 m_GraphBuilder->QueryInterface(IID_IFilterGraph, (void**)&m_FilterGraph);
300 IBaseFilter* pProxyTrans = 0;
301 m_ProxyTrans->QueryInterface(IID_IBaseFilter, (void**)&pProxyTrans);
303 /* if( m_MediaEventEx )
305 // Have the graph signal event via window callbacks for performance
306 m_MediaEventEx->SetNotifyWindow((OAHWND)m_hWnd, WM_GRAPHNOTIFY, 0);
314 hr = m_FilterGraph->AddFilter( m_SourceFilter, L"Video Source" );
316 IPin* pSourceOut = get_pin( m_SourceFilter, PINDIR_OUTPUT );
318 IPin* pProxyTransIn = get_pin( pProxyTrans, PINDIR_INPUT );
319 IPin* pProxyTransOut = get_pin( pProxyTrans, PINDIR_OUTPUT );
321 if( pSourceOut && pProxyTransIn && pProxyTransOut )
323 hr = m_FilterGraph->AddFilter( pProxyTrans, L"Script processing");
324 hr = m_GraphBuilder->Connect(pSourceOut, pProxyTransIn);
325 hr = m_GraphBuilder->Render( pProxyTransOut );
327 pSourceOut->ConnectionMediaType(&amt);
328 pProxyTransOut->ConnectionMediaType(&amt);
329 VIDEOINFOHEADER* vih = (VIDEOINFOHEADER*)amt.pbFormat;
330 if(!vih || vih->bmiHeader.biBitCount != 24)
332 SafeRelease( pSourceOut );
333 SafeRelease( pProxyTransIn );
334 SafeRelease( pProxyTransOut );
339 SafeRelease( pSourceOut );
340 SafeRelease( pProxyTransIn );
341 SafeRelease( pProxyTransOut );
347 void CMainFrame::StartGraph(CWnd* pWnd)
351 m_VideoWindow->put_Owner((OAHWND)pWnd->m_hWnd);
353 m_VideoWindow->get_WindowStyle(&flags);
354 m_VideoWindow->put_WindowStyle( flags | WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN);
355 // m_VideoWindow->put_MessageDrain((OAHWND)pWnd->m_hWnd);
356 // m_CamShiftProp->SetDrawBox(false);
358 SetVideoWindowSize(pWnd->m_hWnd);
359 m_MediaControl->Run();
360 m_isDSRunning = TRUE;
364 void CMainFrame::SetVideoWindowSize(HWND hWnd)
366 CRect rc = GetVideoWindowSize(hWnd);
369 m_VideoWindow->SetWindowPosition( rc.left, rc.top, rc.right, rc.bottom );
373 CRect CMainFrame::GetVideoWindowSize(HWND hWnd)
378 // int width, height;
379 // GetScaleFactor( width, height );
381 ::GetClientRect( hWnd, &rc );
383 // rc.right = rc.right * width / 100;
384 // rc.bottom = rc.bottom * height / 100;
390 /////////////////////////////////////////////////////////////////////////////
391 // CMainFrame message handlers
394 CStatusBar* CMainFrame::GetStatusBar()
396 return &m_wndStatusBar;
399 BOOL CMainFrame::IsDirectShowOn()
404 void CMainFrame::SetTransformCallback(void (__cdecl *cb)(void *))
406 m_ProxyTrans->set_transform(cb, 0);
409 void CMainFrame::StopGraph()
413 m_MediaControl->StopWhenReady();
414 m_VideoWindow->put_Visible(OAFALSE);
415 m_VideoWindow->put_Owner(NULL);
416 m_VideoWindow->put_MessageDrain(0);
417 m_isDSRunning = FALSE;
421 void CMainFrame::StartGraph(HWND hWnd)
425 m_VideoWindow->put_Owner((OAHWND)hWnd);
427 m_MediaEventEx->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY, 0);
428 m_MediaEventEx->SetNotifyFlags(0x00);
429 m_MediaEventEx->CancelDefaultHandling(EC_COMPLETE);
430 m_VideoWindow->get_WindowStyle(&flags);
431 m_VideoWindow->put_WindowStyle(flags & (~WS_CAPTION) | WS_CHILD);
432 m_VideoWindow->put_MessageDrain((OAHWND)hWnd);
434 SetVideoWindowSize(hWnd);
435 m_MediaControl->Run();
436 m_isDSRunning = TRUE;
440 void CMainFrame::OnClose()
442 // Wait while all interpreters stop
447 PostMessage(WM_CLOSE);
451 CHawkApp* app = (CHawkApp*)AfxGetApp();
452 app->CloseEiCHandles();
455 if(WaitForSingleObject(m_pRunDoc->m_hScriptExec, 0) == WAIT_TIMEOUT)
457 //TerminateThread(m_pRunDoc->m_hScriptExec, 0);
458 int f = TerminateThread(m_pRunDoc->m_hScriptExec, 0);
460 if(WaitForSingleObject(m_pRunDoc->m_hThread, 0) == WAIT_TIMEOUT)
462 //TerminateThread(m_pRunDoc->m_hThread, 0);
463 int f = TerminateThread(m_pRunDoc->m_hThread, 0);
466 cvDestroyAllWindows();
467 CMDIFrameWnd::OnClose();
470 BOOL CMainFrame::IsIntRun()
475 void CMainFrame::SetIntRun(BOOL isRun)
480 void CMainFrame::OnVideoPp()
483 ISpecifyPropertyPages* pspp = 0;
484 m_SourceFilter->QueryInterface(IID_ISpecifyPropertyPages, (void**)&pspp);
490 pspp->GetPages(&uuID);
493 OleCreatePropertyFrame(NULL, 0, 0, L"Video Source", 1,
494 (IUnknown**)&m_SourceFilter, uuID.cElems, uuID.pElems,
498 void CMainFrame::OnUpdateVideoPp(CCmdUI* pCmdUI)
500 pCmdUI->Enable(IsDirectShowOn());
503 void CMainFrame::SetDocRun(CHawkDoc *pDoc)
508 static int __cdecl DS_ON_SIZE(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, int* ret)
510 CMainFrame* frame = (CMainFrame*)AfxGetMainWnd();
511 if(hWnd == frame->GetDSHWND() && msg == WM_SIZE)
513 frame->SetVideoWindowSize(hWnd);
519 void CMainFrame::RunDS(void(*cb)(void*), BOOL ret)
521 CHawkApp* app = (CHawkApp*)AfxGetApp();
522 char *wndName = "DirectShow";
523 cvNamedWindow(wndName, 0);
524 m_DSWnd = (HWND)cvGetWindowHandle(wndName);
525 SetTransformCallback(cb);
526 set_postprocess_func(DS_ON_SIZE);
532 /* Organize message processing... */
536 if(PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
538 TranslateMessage(&message);
539 DispatchMessage(&message);
542 if(!cvGetWindowHandle("DirectShow"))
544 // The ds window has been destroyed...
545 /* OAFilterState state;
546 hres = pimc->GetState(INFINITE, &state);
547 hres = pimc->Pause();
548 hres = pimc->GetState(INFINITE, &state);
550 hres = pimc->GetState(INFINITE, &state);
551 ASSERT(state == State_Stopped);*/
559 void CMainFrame::OnPinPp()
561 //Find the output pit that is connected to the next filter...
563 ISpecifyPropertyPages* pspp = 0;
564 IPin* pPin = get_pin(m_SourceFilter, PINDIR_OUTPUT);
565 pPin->QueryInterface(IID_ISpecifyPropertyPages, (void**)&pspp);
571 pspp->GetPages(&uuID);
574 /* Disconnect the pin to enable possible changes in the output format... */
576 IBaseFilter* pFilter;
577 m_ProxyTrans->QueryInterface(IID_IBaseFilter, (void**)&pFilter);
578 IPin* pProxyPin = get_pin(pFilter, PINDIR_INPUT);
579 pProxyPin->Disconnect();
580 OleCreatePropertyFrame(NULL, 0, 0, L"Video Source", 1,
581 (IUnknown**)&pPin, uuID.cElems, uuID.pElems,
583 m_GraphBuilder->Connect(pPin, pProxyPin);
586 void CMainFrame::OnUpdatePinPp(CCmdUI* pCmdUI)
588 pCmdUI->Enable(IsDirectShowOn());
591 HWND CMainFrame::GetDSHWND()