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.
47 #pragma warning( disable: 4711 )
51 #define WM_CAP_FIRSTA (WM_USER)
52 #define capSendMessage(hwnd,m,w,l) (IsWindow(hwnd)?SendMessage(hwnd,m,w,l):0)
55 #if (defined WIN64 || defined _WIN64) && defined _MSC_VER && !defined __ICL
56 #pragma optimize("",off)
59 /********************* Capturing video from AVI via VFW ************************/
61 static BITMAPINFOHEADER icvBitmapHeader( int width, int height, int bpp, int compression = BI_RGB )
63 BITMAPINFOHEADER bmih;
64 memset( &bmih, 0, sizeof(bmih));
65 bmih.biSize = sizeof(bmih);
67 bmih.biHeight = height;
68 bmih.biBitCount = (WORD)bpp;
69 bmih.biCompression = compression;
76 static void icvInitCapture_VFW()
78 static int isInitialized = 0;
87 class CvCaptureAVI_VFW : public CvCapture
96 virtual ~CvCaptureAVI_VFW()
102 virtual bool open( const char* filename );
103 virtual void close();
105 virtual double getProperty(int);
106 virtual bool setProperty(int, double);
107 virtual bool grabFrame();
108 virtual IplImage* retrieveFrame(int);
109 virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
115 PAVISTREAM avistream;
117 AVISTREAMINFO aviinfo;
118 BITMAPINFOHEADER * bmih;
127 void CvCaptureAVI_VFW::init()
132 memset( &aviinfo, 0, sizeof(aviinfo) );
134 film_range = cvSlice(0,0);
142 void CvCaptureAVI_VFW::close()
145 AVIStreamGetFrameClose( getframe );
148 AVIStreamRelease( avistream );
151 AVIFileRelease( avifile );
154 cvReleaseImage( &frame );
160 bool CvCaptureAVI_VFW::open( const char* filename )
163 icvInitCapture_VFW();
168 HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL );
171 hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 );
174 hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo));
177 size.width = aviinfo.rcFrame.right - aviinfo.rcFrame.left;
178 size.height = aviinfo.rcFrame.bottom - aviinfo.rcFrame.top;
179 BITMAPINFOHEADER bmih = icvBitmapHeader( size.width, size.height, 24 );
181 film_range.start_index = (int)aviinfo.dwStart;
182 film_range.end_index = film_range.start_index + (int)aviinfo.dwLength;
183 fps = (double)aviinfo.dwRate/aviinfo.dwScale;
184 pos = film_range.start_index;
185 getframe = AVIStreamGetFrameOpen( avistream, &bmih );
196 bool CvCaptureAVI_VFW::grabFrame()
199 bmih = (BITMAPINFOHEADER*)AVIStreamGetFrame( getframe, pos++ );
203 IplImage* CvCaptureAVI_VFW::retrieveFrame(int)
205 if( avistream && bmih )
208 cvInitImageHeader( &src, cvSize( bmih->biWidth, bmih->biHeight ),
209 IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
210 cvSetData( &src, (char*)(bmih + 1), src.widthStep );
211 if( !frame || frame->width != src.width || frame->height != src.height )
213 cvReleaseImage( &frame );
214 frame = cvCreateImage( cvGetSize(&src), 8, 3 );
217 cvFlip( &src, frame, 0 );
224 double CvCaptureAVI_VFW::getProperty( int property_id )
226 switch( property_id )
228 case CV_CAP_PROP_POS_MSEC:
229 return cvRound(pos*1000./fps);
230 case CV_CAP_PROP_POS_FRAMES:
232 case CV_CAP_PROP_POS_AVI_RATIO:
233 return (pos - film_range.start_index)/
234 (film_range.end_index - film_range.start_index + 1e-10);
235 case CV_CAP_PROP_FRAME_WIDTH:
237 case CV_CAP_PROP_FRAME_HEIGHT:
239 case CV_CAP_PROP_FPS:
241 case CV_CAP_PROP_FOURCC:
242 return aviinfo.fccHandler;
243 case CV_CAP_PROP_FRAME_COUNT:
244 return film_range.end_index - film_range.start_index;
249 bool CvCaptureAVI_VFW::setProperty( int property_id, double value )
251 switch( property_id )
253 case CV_CAP_PROP_POS_MSEC:
254 case CV_CAP_PROP_POS_FRAMES:
255 case CV_CAP_PROP_POS_AVI_RATIO:
257 switch( property_id )
259 case CV_CAP_PROP_POS_MSEC:
260 pos = cvRound(value*fps*0.001);
262 case CV_CAP_PROP_POS_AVI_RATIO:
263 pos = cvRound(value*(film_range.end_index -
264 film_range.start_index) +
265 film_range.start_index);
268 pos = cvRound(value);
270 if( pos < film_range.start_index )
271 pos = film_range.start_index;
272 if( pos > film_range.end_index )
273 pos = film_range.end_index;
283 CvCapture* cvCreateFileCapture_VFW (const char* filename)
285 CvCaptureAVI_VFW* capture = new CvCaptureAVI_VFW;
286 if( capture->open(filename) )
293 /********************* Capturing video from camera via VFW *********************/
295 class CvCaptureCAM_VFW : public CvCapture
298 CvCaptureCAM_VFW() { init(); }
299 virtual ~CvCaptureCAM_VFW() { close(); }
301 virtual bool open( int index );
302 virtual void close();
303 virtual double getProperty(int);
304 virtual bool setProperty(int, double) { return false; }
305 virtual bool grabFrame();
306 virtual IplImage* retrieveFrame(int);
307 virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
312 static LRESULT PASCAL frameCallback( HWND hWnd, VIDEOHDR* hdr );
323 void CvCaptureCAM_VFW::init()
325 memset( &caps, 0, sizeof(caps) );
333 void CvCaptureCAM_VFW::closeHIC()
337 ICDecompressEnd( hic );
344 LRESULT PASCAL CvCaptureCAM_VFW::frameCallback( HWND hWnd, VIDEOHDR* hdr )
346 CvCaptureCAM_VFW* capture = 0;
348 if (!hWnd) return FALSE;
350 capture = (CvCaptureCAM_VFW*)capGetUserData(hWnd);
353 return (LRESULT)TRUE;
357 // Initialize camera input
358 bool CvCaptureCAM_VFW::open( int wIndex )
360 char szDeviceName[80];
361 char szDeviceVersion[80];
366 if( (unsigned)wIndex >= 10 )
369 for( ; wIndex < 10; wIndex++ )
371 if( capGetDriverDescription( wIndex, szDeviceName,
372 sizeof (szDeviceName), szDeviceVersion,
373 sizeof (szDeviceVersion)))
375 hWndC = capCreateCaptureWindow ( "My Own Capture Window",
376 WS_POPUP | WS_CHILD, 0, 0, 320, 240, 0, 0);
377 if( capDriverConnect (hWndC, wIndex))
379 DestroyWindow( hWndC );
391 memset( &caps, 0, sizeof(caps));
392 capDriverGetCaps( hWndC, &caps, sizeof(&caps));
393 ::MoveWindow( hWndC, 0, 0, 320, 240, TRUE );
394 capSetUserData( hWndC, (size_t)this );
395 capSetCallbackOnFrame( hWndC, frameCallback );
397 capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
398 p.dwRequestMicroSecPerFrame = 66667/2;
399 capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
400 //capPreview( hWndC, 1 );
401 capPreviewScale(hWndC,FALSE);
402 capPreviewRate(hWndC,1);
408 void CvCaptureCAM_VFW::close()
412 capSetCallbackOnFrame( capWnd, NULL );
413 capDriverDisconnect( capWnd );
414 DestroyWindow( capWnd );
417 cvReleaseImage( &frame );
422 bool CvCaptureCAM_VFW::grabFrame()
426 SendMessage( capWnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0 );
433 IplImage* CvCaptureCAM_VFW::retrieveFrame(int)
436 memset( &vfmt, 0, sizeof(vfmt));
437 BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader;
438 int sz, prevWidth, prevHeight;
443 sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt));
444 prevWidth = frame ? frame->width : 0;
445 prevHeight = frame ? frame->height : 0;
447 if( !hdr || hdr->lpData == 0 || sz == 0 )
450 if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight )
452 cvReleaseImage( &frame );
453 frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 );
456 if( vfmt.bmiHeader.biCompression != BI_RGB ||
457 vfmt.bmiHeader.biBitCount != 24 )
459 BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 );
461 if( hic == 0 || fourcc != vfmt0.biCompression ||
462 prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight )
465 hic = ICOpen( MAKEFOURCC('V','I','D','C'),
466 vfmt0.biCompression, ICMODE_DECOMPRESS );
469 if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK )
477 if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData,
478 &vfmt1, frame->imageData ) != ICERR_OK )
484 cvFlip( frame, frame, 0 );
489 cvInitImageHeader( &src, cvSize(vfmt0.biWidth, vfmt0.biHeight),
490 IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
491 cvSetData( &src, hdr->lpData, src.widthStep );
492 cvFlip( &src, frame, 0 );
499 double CvCaptureCAM_VFW::getProperty( int property_id )
501 switch( property_id )
503 case CV_CAP_PROP_FRAME_WIDTH:
504 return frame ? frame->width : 0;
505 case CV_CAP_PROP_FRAME_HEIGHT:
506 return frame ? frame->height : 0;
507 case CV_CAP_PROP_FOURCC:
514 CvCapture* cvCreateCameraCapture_VFW( int index )
516 CvCaptureCAM_VFW* capture = new CvCaptureCAM_VFW;
518 if( capture->open( index ))
526 /*************************** writing AVIs ******************************/
528 class CvVideoWriter_VFW : public CvVideoWriter
531 CvVideoWriter_VFW() { init(); }
532 virtual ~CvVideoWriter_VFW() { close(); }
534 virtual bool open( const char* filename, int fourcc,
535 double fps, CvSize frameSize, bool isColor );
536 virtual void close();
537 virtual bool writeFrame( const IplImage* );
541 bool createStreams( CvSize frameSize, bool isColor );
544 PAVISTREAM compressed;
545 PAVISTREAM uncompressed;
553 void CvVideoWriter_VFW::init()
556 compressed = uncompressed = 0;
563 void CvVideoWriter_VFW::close()
566 AVIStreamRelease( uncompressed );
568 AVIStreamRelease( compressed );
570 AVIFileRelease( avifile );
571 cvReleaseImage( &tempFrame );
576 // philipg. Made this code capable of writing 8bpp gray scale bitmaps
577 struct BITMAPINFO_8Bit
579 BITMAPINFOHEADER bmiHeader;
580 RGBQUAD bmiColors[256];
584 bool CvVideoWriter_VFW::open( const char* filename, int _fourcc, double _fps, CvSize frameSize, bool isColor )
588 icvInitCapture_VFW();
589 if( AVIFileOpen( &avifile, filename, OF_CREATE | OF_WRITE, 0 ) == AVIERR_OK )
593 if( frameSize.width > 0 && frameSize.height > 0 &&
594 !createStreams( frameSize, isColor ) )
604 bool CvVideoWriter_VFW::createStreams( CvSize frameSize, bool isColor )
608 AVISTREAMINFO aviinfo;
610 BITMAPINFO_8Bit bmih;
611 bmih.bmiHeader = icvBitmapHeader( frameSize.width, frameSize.height, isColor ? 24 : 8 );
612 for( int i = 0; i < 256; i++ )
614 bmih.bmiColors[i].rgbBlue = (BYTE)i;
615 bmih.bmiColors[i].rgbGreen = (BYTE)i;
616 bmih.bmiColors[i].rgbRed = (BYTE)i;
617 bmih.bmiColors[i].rgbReserved = 0;
620 memset( &aviinfo, 0, sizeof(aviinfo));
621 aviinfo.fccType = streamtypeVIDEO;
622 aviinfo.fccHandler = 0;
623 // use highest possible accuracy for dwRate/dwScale
624 aviinfo.dwScale = (DWORD)((double)0x7FFFFFFF / fps);
625 aviinfo.dwRate = cvRound(fps * aviinfo.dwScale);
626 aviinfo.rcFrame.top = aviinfo.rcFrame.left = 0;
627 aviinfo.rcFrame.right = frameSize.width;
628 aviinfo.rcFrame.bottom = frameSize.height;
630 if( AVIFileCreateStream( avifile, &uncompressed, &aviinfo ) == AVIERR_OK )
632 AVICOMPRESSOPTIONS copts, *pcopts = &copts;
633 copts.fccType = streamtypeVIDEO;
634 copts.fccHandler = fourcc != -1 ? fourcc : 0;
635 copts.dwKeyFrameEvery = 1;
636 copts.dwQuality = 10000;
637 copts.dwBytesPerSecond = 0;
638 copts.dwFlags = AVICOMPRESSF_VALID;
639 copts.lpFormat = &bmih;
640 copts.cbFormat = (isColor ? sizeof(BITMAPINFOHEADER) : sizeof(bmih));
643 copts.dwInterleaveEvery = 0;
645 if( fourcc != -1 || AVISaveOptions( 0, 0, 1, &uncompressed, &pcopts ) == TRUE )
647 if( AVIMakeCompressedStream( &compressed, uncompressed, pcopts, 0 ) == AVIERR_OK &&
648 AVIStreamSetFormat( compressed, 0, &bmih, sizeof(bmih)) == AVIERR_OK )
651 fourcc = (int)copts.fccHandler;
652 frameSize = frameSize;
653 tempFrame = cvCreateImage( frameSize, 8, (isColor ? 3 : 1) );
662 bool CvVideoWriter_VFW::writeFrame( const IplImage* image )
665 CV_FUNCNAME( "CvVideoWriter_VFW::writeFrame" );
672 if( !compressed && !createStreams( cvGetSize(image), image->nChannels > 1 ))
675 if( image->width != tempFrame->width || image->height != tempFrame->height )
676 CV_ERROR( CV_StsUnmatchedSizes,
677 "image size is different from the currently set frame size" );
679 if( image->nChannels != tempFrame->nChannels ||
680 image->depth != tempFrame->depth ||
681 image->origin == 0 ||
682 image->widthStep != cvAlign(image->width*image->nChannels*((image->depth & 255)/8), 4))
684 cvConvertImage( image, tempFrame, image->origin == 0 ? CV_CVTIMG_FLIP : 0 );
685 image = (const IplImage*)tempFrame;
688 result = AVIStreamWrite( compressed, pos++, 1, image->imageData,
689 image->imageSize, AVIIF_KEYFRAME, 0, 0 ) == AVIERR_OK;
696 CvVideoWriter* cvCreateVideoWriter_VFW( const char* filename, int fourcc,
697 double fps, CvSize frameSize, int isColor )
699 CvVideoWriter_VFW* writer = new CvVideoWriter_VFW;
700 if( writer->open( filename, fourcc, fps, frameSize, isColor != 0 ))