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*/// Camera.cpp: implementation of the CCamera class.
42 //////////////////////////////////////////////////////////////////////
50 static char THIS_FILE[]=__FILE__;
54 //////////////////////////////////////////////////////////////////////
55 // Construction/Destruction
56 //////////////////////////////////////////////////////////////////////
72 void CCamera::UpdateParent( bool whole )
74 HWND parent = GetParent( m_capWnd );
75 RECT r = { 0, 0, 0, 0 };
76 IplImage* img = m_frame.GetImage();
81 r.bottom = img->height;
84 InvalidateRect( parent, whole ? 0 : &r, whole );
85 UpdateWindow( parent );
90 LRESULT PASCAL FrameCallbackProc( HWND hWnd, VIDEOHDR* hdr )
93 CCamera* camera = (CCamera*)capGetUserData(hWnd);
96 if (!hWnd) return FALSE;
97 if (camera && !camera->IsRunning() ) return FALSE;
99 sz = capGetVideoFormat( hWnd, &vfmt, sizeof(vfmt));
101 if( hdr && hdr->lpData && sz != 0 && camera )
102 camera->PutCompressedFrame( hdr, &vfmt );
104 return (LRESULT)TRUE;
108 void CCamera::OnFrame()
113 void CCamera::PutCompressedFrame( VIDEOHDR* hdr, BITMAPINFO* vfmt )
115 long code = ICERR_OK;
116 char* frame_data = (char*)hdr->lpData;
118 if( vfmt->bmiHeader.biCompression != BI_RGB ||
119 vfmt->bmiHeader.biBitCount != 24 )
121 BITMAPINFOHEADER& vfmt0 = vfmt->bmiHeader;
122 BITMAPINFOHEADER vfmt1;
125 memset( &vfmt1, 0, sizeof(vfmt1));
126 vfmt1.biSize = sizeof(vfmt1);
127 vfmt1.biWidth = vfmt0.biWidth;
128 vfmt1.biHeight = vfmt0.biHeight;
129 vfmt1.biBitCount = 24;
130 vfmt1.biCompression = BI_RGB;
133 if( m_hic == 0 || m_fourcc != vfmt0.biCompression || m_frame.GetImage() == 0 ||
134 vfmt0.biWidth != m_frame.GetImage()->width ||
135 vfmt0.biHeight != m_frame.GetImage()->height )
139 ICDecompressEnd( m_hic );
142 m_hic = ICOpen( MAKEFOURCC('V','I','D','C'),
143 vfmt0.biCompression, ICMODE_DECOMPRESS );
145 if( m_hic && ICDecompressBegin( m_hic, &vfmt0, &vfmt1 ) == ICERR_OK )
147 m_frame.Create( vfmt0.biWidth, vfmt0.biHeight, 24 );
148 m_frame.GetImage()->origin = IPL_ORIGIN_BL;
150 code = ICDecompress( m_hic, 0, &vfmt0, hdr->lpData,
151 &vfmt1, m_frame.GetImage()->imageData );
157 m_frame.Create( vfmt->bmiHeader.biWidth, vfmt->bmiHeader.biHeight, 24 );
158 memcpy( m_frame.GetImage()->imageData, hdr->lpData, m_frame.GetImage()->imageSize );
161 /*m_frame.GetImage()->origin = IPL_ORIGIN_BL;*/
162 cvFlip( m_frame.GetImage(), m_frame.GetImage(), 0 );
168 // Initialize camera input
169 bool CCamera::Initialize( int width, int height, int format, HWND parent, int wIndex )
171 char szDeviceName[80];
172 char szDeviceVersion[80];
175 if( (unsigned)wIndex < 10 )
178 for( wIndex = 0; wIndex < 10; wIndex++ )
181 if( capGetDriverDescription( wIndex, szDeviceName,
182 sizeof (szDeviceName), szDeviceVersion,
183 sizeof (szDeviceVersion)))
185 hWndC = capCreateCaptureWindow ( "My Own Capture Window",
186 WS_CHILD | WS_VISIBLE , 0, 0, 160, 120, parent, 0);
188 if( capDriverConnect (hWndC, wIndex))
192 //capPreviewRate(hWndC, 66); // rate, in milliseconds
193 memset( &bmi.bmiHeader, 0, sizeof(bmi.bmiHeader));
194 bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
195 bmi.bmiHeader.biBitCount = format == 0 ? 24 : 12;
196 bmi.bmiHeader.biWidth = width;
197 bmi.bmiHeader.biHeight = height;
198 bmi.bmiHeader.biPlanes = 1;
199 bmi.bmiHeader.biCompression = format;
200 bmi.bmiHeader.biSizeImage = ((bmi.bmiHeader.biWidth*
201 bmi.bmiHeader.biBitCount/8 + 3)&-4)*
202 bmi.bmiHeader.biHeight;
203 if( format == -1 || capSetVideoFormat( hWndC, &bmi, sizeof(bmi)-4)) break;
204 capDriverDisconnect( hWndC );*/
207 DestroyWindow( hWndC );
216 memset( &m_caps, 0, sizeof(m_caps));
217 capDriverGetCaps( hWndC, &m_caps, sizeof(m_caps));
218 capSetUserData( hWndC, (long)this );
219 capSetCallbackOnFrame( m_capWnd, FrameCallbackProc );
220 ::MoveWindow( m_capWnd, 0, 0, 1, 1, TRUE );
222 capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
223 p.dwRequestMicroSecPerFrame = 1;
224 capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
225 capPreviewScale(hWndC,FALSE);
226 capPreviewRate(hWndC,1);
228 return m_capWnd != 0;
232 // Uninitialize camera input
233 void CCamera::Uninitialize()
236 capSetCallbackOnFrame( m_capWnd, NULL );
237 capDriverDisconnect( m_capWnd );
238 DestroyWindow( m_capWnd );
242 ICDecompressEnd( m_hic );
249 void CCamera::Start()
267 void CCamera::VideoFormatDlg()
269 if( m_capWnd && m_caps.fHasDlgVideoFormat )
271 capDlgVideoFormat( m_capWnd );
276 void CCamera::VideoSourceDlg()
278 if( m_capWnd && m_caps.fHasDlgVideoSource )
280 capDlgVideoSource( m_capWnd );