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.
42 #include "precomp.hpp"
47 #define WM_CAP_FIRSTA (WM_USER)
48 #define capSendMessage(hwnd,m,w,l) (IsWindow(hwnd)?SendMessage(hwnd,m,w,l):0)
51 #if defined _M_X64 && defined _MSC_VER
52 #pragma optimize("",off)
53 #pragma warning(disable: 4748)
56 /********************* Capturing video from AVI via VFW ************************/
58 static BITMAPINFOHEADER icvBitmapHeader( int width, int height, int bpp, int compression = BI_RGB )
60 BITMAPINFOHEADER bmih;
61 memset( &bmih, 0, sizeof(bmih));
62 bmih.biSize = sizeof(bmih);
64 bmih.biHeight = height;
65 bmih.biBitCount = (WORD)bpp;
66 bmih.biCompression = compression;
73 static void icvInitCapture_VFW()
75 static int isInitialized = 0;
84 class CvCaptureAVI_VFW : public CvCapture
93 virtual ~CvCaptureAVI_VFW()
99 virtual bool open( const char* filename );
100 virtual void close();
102 virtual double getProperty(int);
103 virtual bool setProperty(int, double);
104 virtual bool grabFrame();
105 virtual IplImage* retrieveFrame(int);
106 virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
112 PAVISTREAM avistream;
114 AVISTREAMINFO aviinfo;
115 BITMAPINFOHEADER * bmih;
124 void CvCaptureAVI_VFW::init()
129 memset( &aviinfo, 0, sizeof(aviinfo) );
131 film_range = cvSlice(0,0);
139 void CvCaptureAVI_VFW::close()
142 AVIStreamGetFrameClose( getframe );
145 AVIStreamRelease( avistream );
148 AVIFileRelease( avifile );
151 cvReleaseImage( &frame );
157 bool CvCaptureAVI_VFW::open( const char* filename )
160 icvInitCapture_VFW();
165 HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL );
168 hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 );
171 hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo));
174 size.width = aviinfo.rcFrame.right - aviinfo.rcFrame.left;
175 size.height = aviinfo.rcFrame.bottom - aviinfo.rcFrame.top;
176 BITMAPINFOHEADER bmihdr = icvBitmapHeader( size.width, size.height, 24 );
178 film_range.start_index = (int)aviinfo.dwStart;
179 film_range.end_index = film_range.start_index + (int)aviinfo.dwLength;
180 fps = (double)aviinfo.dwRate/aviinfo.dwScale;
181 pos = film_range.start_index;
182 getframe = AVIStreamGetFrameOpen( avistream, &bmihdr );
186 // Attempt to open as 8-bit AVI.
187 bmihdr = icvBitmapHeader( size.width, size.height, 8);
188 getframe = AVIStreamGetFrameOpen( avistream, &bmihdr );
199 bool CvCaptureAVI_VFW::grabFrame()
202 bmih = (BITMAPINFOHEADER*)AVIStreamGetFrame( getframe, pos++ );
206 IplImage* CvCaptureAVI_VFW::retrieveFrame(int)
208 if( avistream && bmih )
210 bool isColor = bmih->biBitCount == 24;
211 int nChannels = (isColor) ? 3 : 1;
213 cvInitImageHeader( &src, cvSize( bmih->biWidth, bmih->biHeight ),
214 IPL_DEPTH_8U, nChannels, IPL_ORIGIN_BL, 4 );
216 char* dataPtr = (char*)(bmih + 1);
218 // Only account for the color map size if we are an 8-bit image and the color map is used
221 static int RGBQUAD_SIZE_PER_BYTE = sizeof(RGBQUAD)/sizeof(BYTE);
222 int offsetFromColormapToData = (int)bmih->biClrUsed*RGBQUAD_SIZE_PER_BYTE;
223 dataPtr += offsetFromColormapToData;
226 cvSetData( &src, dataPtr, src.widthStep );
228 if( !frame || frame->width != src.width || frame->height != src.height )
230 cvReleaseImage( &frame );
231 frame = cvCreateImage( cvGetSize(&src), 8, nChannels );
234 cvFlip( &src, frame, 0 );
241 double CvCaptureAVI_VFW::getProperty( int property_id )
243 switch( property_id )
245 case CV_CAP_PROP_POS_MSEC:
246 return cvRound(pos*1000./fps);
247 case CV_CAP_PROP_POS_FRAMES:
249 case CV_CAP_PROP_POS_AVI_RATIO:
250 return (pos - film_range.start_index)/
251 (film_range.end_index - film_range.start_index + 1e-10);
252 case CV_CAP_PROP_FRAME_WIDTH:
254 case CV_CAP_PROP_FRAME_HEIGHT:
256 case CV_CAP_PROP_FPS:
258 case CV_CAP_PROP_FOURCC:
259 return aviinfo.fccHandler;
260 case CV_CAP_PROP_FRAME_COUNT:
261 return film_range.end_index - film_range.start_index;
266 bool CvCaptureAVI_VFW::setProperty( int property_id, double value )
268 switch( property_id )
270 case CV_CAP_PROP_POS_MSEC:
271 case CV_CAP_PROP_POS_FRAMES:
272 case CV_CAP_PROP_POS_AVI_RATIO:
274 switch( property_id )
276 case CV_CAP_PROP_POS_MSEC:
277 pos = cvRound(value*fps*0.001);
279 case CV_CAP_PROP_POS_AVI_RATIO:
280 pos = cvRound(value*(film_range.end_index -
281 film_range.start_index) +
282 film_range.start_index);
285 pos = cvRound(value);
287 if( pos < film_range.start_index )
288 pos = film_range.start_index;
289 if( pos > film_range.end_index )
290 pos = film_range.end_index;
300 CvCapture* cvCreateFileCapture_VFW (const char* filename)
302 CvCaptureAVI_VFW* capture = new CvCaptureAVI_VFW;
303 if( capture->open(filename) )
310 /********************* Capturing video from camera via VFW *********************/
312 class CvCaptureCAM_VFW : public CvCapture
315 CvCaptureCAM_VFW() { init(); }
316 virtual ~CvCaptureCAM_VFW() { close(); }
318 virtual bool open( int index );
319 virtual void close();
320 virtual double getProperty(int);
321 virtual bool setProperty(int, double) { return false; }
322 virtual bool grabFrame();
323 virtual IplImage* retrieveFrame(int);
324 virtual int getCaptureDomain() { return CV_CAP_VFW; } // Return the type of the capture object: CV_CAP_VFW, etc...
329 static LRESULT PASCAL frameCallback( HWND hWnd, VIDEOHDR* hdr );
340 void CvCaptureCAM_VFW::init()
342 memset( &caps, 0, sizeof(caps) );
350 void CvCaptureCAM_VFW::closeHIC()
354 ICDecompressEnd( hic );
361 LRESULT PASCAL CvCaptureCAM_VFW::frameCallback( HWND hWnd, VIDEOHDR* hdr )
363 CvCaptureCAM_VFW* capture = 0;
365 if (!hWnd) return FALSE;
367 capture = (CvCaptureCAM_VFW*)capGetUserData(hWnd);
370 return (LRESULT)TRUE;
374 // Initialize camera input
375 bool CvCaptureCAM_VFW::open( int wIndex )
377 char szDeviceName[80];
378 char szDeviceVersion[80];
383 if( (unsigned)wIndex >= 10 )
386 for( ; wIndex < 10; wIndex++ )
388 if( capGetDriverDescription( wIndex, szDeviceName,
389 sizeof (szDeviceName), szDeviceVersion,
390 sizeof (szDeviceVersion)))
392 hWndC = capCreateCaptureWindow ( "My Own Capture Window",
393 WS_POPUP | WS_CHILD, 0, 0, 320, 240, 0, 0);
394 if( capDriverConnect (hWndC, wIndex))
396 DestroyWindow( hWndC );
408 memset( &caps, 0, sizeof(caps));
409 capDriverGetCaps( hWndC, &caps, sizeof(caps));
410 ::MoveWindow( hWndC, 0, 0, 320, 240, TRUE );
411 capSetUserData( hWndC, (size_t)this );
412 capSetCallbackOnFrame( hWndC, frameCallback );
414 capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
415 p.dwRequestMicroSecPerFrame = 66667/2;
416 capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
417 //capPreview( hWndC, 1 );
418 capPreviewScale(hWndC,FALSE);
419 capPreviewRate(hWndC,1);
425 void CvCaptureCAM_VFW::close()
429 capSetCallbackOnFrame( capWnd, NULL );
430 capDriverDisconnect( capWnd );
431 DestroyWindow( capWnd );
434 cvReleaseImage( &frame );
439 bool CvCaptureCAM_VFW::grabFrame()
443 SendMessage( capWnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0 );
450 IplImage* CvCaptureCAM_VFW::retrieveFrame(int)
453 memset( &vfmt, 0, sizeof(vfmt));
454 BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader;
455 int sz, prevWidth, prevHeight;
460 sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt));
461 prevWidth = frame ? frame->width : 0;
462 prevHeight = frame ? frame->height : 0;
464 if( !hdr || hdr->lpData == 0 || sz == 0 )
467 if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight )
469 cvReleaseImage( &frame );
470 frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 );
473 if( vfmt.bmiHeader.biCompression != BI_RGB ||
474 vfmt.bmiHeader.biBitCount != 24 )
476 BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 );
478 if( hic == 0 || fourcc != vfmt0.biCompression ||
479 prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight )
482 hic = ICOpen( MAKEFOURCC('V','I','D','C'),
483 vfmt0.biCompression, ICMODE_DECOMPRESS );
486 if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK )
494 if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData,
495 &vfmt1, frame->imageData ) != ICERR_OK )
501 cvFlip( frame, frame, 0 );
506 cvInitImageHeader( &src, cvSize(vfmt0.biWidth, vfmt0.biHeight),
507 IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
508 cvSetData( &src, hdr->lpData, src.widthStep );
509 cvFlip( &src, frame, 0 );
516 double CvCaptureCAM_VFW::getProperty( int property_id )
518 switch( property_id )
520 case CV_CAP_PROP_FRAME_WIDTH:
521 return frame ? frame->width : 0;
522 case CV_CAP_PROP_FRAME_HEIGHT:
523 return frame ? frame->height : 0;
524 case CV_CAP_PROP_FOURCC:
531 CvCapture* cvCreateCameraCapture_VFW( int index )
533 CvCaptureCAM_VFW* capture = new CvCaptureCAM_VFW;
535 if( capture->open( index ))
543 /*************************** writing AVIs ******************************/
545 class CvVideoWriter_VFW : public CvVideoWriter
548 CvVideoWriter_VFW() { init(); }
549 virtual ~CvVideoWriter_VFW() { close(); }
551 virtual bool open( const char* filename, int fourcc,
552 double fps, CvSize frameSize, bool isColor );
553 virtual void close();
554 virtual bool writeFrame( const IplImage* );
558 bool createStreams( CvSize frameSize, bool isColor );
561 PAVISTREAM compressed;
562 PAVISTREAM uncompressed;
570 void CvVideoWriter_VFW::init()
573 compressed = uncompressed = 0;
580 void CvVideoWriter_VFW::close()
583 AVIStreamRelease( uncompressed );
585 AVIStreamRelease( compressed );
587 AVIFileRelease( avifile );
588 cvReleaseImage( &tempFrame );
593 // philipg. Made this code capable of writing 8bpp gray scale bitmaps
594 struct BITMAPINFO_8Bit
596 BITMAPINFOHEADER bmiHeader;
597 RGBQUAD bmiColors[256];
601 bool CvVideoWriter_VFW::open( const char* filename, int _fourcc, double _fps, CvSize frameSize, bool isColor )
605 icvInitCapture_VFW();
606 if( AVIFileOpen( &avifile, filename, OF_CREATE | OF_WRITE, 0 ) == AVIERR_OK )
610 if( frameSize.width > 0 && frameSize.height > 0 &&
611 !createStreams( frameSize, isColor ) )
623 bool CvVideoWriter_VFW::createStreams( CvSize frameSize, bool isColor )
627 AVISTREAMINFO aviinfo;
629 BITMAPINFO_8Bit bmih;
630 bmih.bmiHeader = icvBitmapHeader( frameSize.width, frameSize.height, isColor ? 24 : 8 );
631 for( int i = 0; i < 256; i++ )
633 bmih.bmiColors[i].rgbBlue = (BYTE)i;
634 bmih.bmiColors[i].rgbGreen = (BYTE)i;
635 bmih.bmiColors[i].rgbRed = (BYTE)i;
636 bmih.bmiColors[i].rgbReserved = 0;
639 memset( &aviinfo, 0, sizeof(aviinfo));
640 aviinfo.fccType = streamtypeVIDEO;
641 aviinfo.fccHandler = 0;
642 // use highest possible accuracy for dwRate/dwScale
643 aviinfo.dwScale = (DWORD)((double)0x7FFFFFFF / fps);
644 aviinfo.dwRate = cvRound(fps * aviinfo.dwScale);
645 aviinfo.rcFrame.top = aviinfo.rcFrame.left = 0;
646 aviinfo.rcFrame.right = frameSize.width;
647 aviinfo.rcFrame.bottom = frameSize.height;
649 if( AVIFileCreateStream( avifile, &uncompressed, &aviinfo ) == AVIERR_OK )
651 AVICOMPRESSOPTIONS copts, *pcopts = &copts;
652 copts.fccType = streamtypeVIDEO;
653 copts.fccHandler = fourcc != -1 ? fourcc : 0;
654 copts.dwKeyFrameEvery = 1;
655 copts.dwQuality = 10000;
656 copts.dwBytesPerSecond = 0;
657 copts.dwFlags = AVICOMPRESSF_VALID;
658 copts.lpFormat = &bmih;
659 copts.cbFormat = (isColor ? sizeof(BITMAPINFOHEADER) : sizeof(bmih));
662 copts.dwInterleaveEvery = 0;
664 if( fourcc != -1 || AVISaveOptions( 0, 0, 1, &uncompressed, &pcopts ) == TRUE )
666 if( AVIMakeCompressedStream( &compressed, uncompressed, pcopts, 0 ) == AVIERR_OK &&
667 AVIStreamSetFormat( compressed, 0, &bmih, sizeof(bmih)) == AVIERR_OK )
670 fourcc = (int)copts.fccHandler;
671 frameSize = frameSize;
672 tempFrame = cvCreateImage( frameSize, 8, (isColor ? 3 : 1) );
681 bool CvVideoWriter_VFW::writeFrame( const IplImage* image )
684 CV_FUNCNAME( "CvVideoWriter_VFW::writeFrame" );
691 if( !compressed && !createStreams( cvGetSize(image), image->nChannels > 1 ))
694 if( image->width != tempFrame->width || image->height != tempFrame->height )
695 CV_ERROR( CV_StsUnmatchedSizes,
696 "image size is different from the currently set frame size" );
698 if( image->nChannels != tempFrame->nChannels ||
699 image->depth != tempFrame->depth ||
700 image->origin == 0 ||
701 image->widthStep != cvAlign(image->width*image->nChannels*((image->depth & 255)/8), 4))
703 cvConvertImage( image, tempFrame, image->origin == 0 ? CV_CVTIMG_FLIP : 0 );
704 image = (const IplImage*)tempFrame;
707 result = AVIStreamWrite( compressed, pos++, 1, image->imageData,
708 image->imageSize, AVIIF_KEYFRAME, 0, 0 ) == AVIERR_OK;
715 CvVideoWriter* cvCreateVideoWriter_VFW( const char* filename, int fourcc,
716 double fps, CvSize frameSize, int isColor )
718 CvVideoWriter_VFW* writer = new CvVideoWriter_VFW;
719 if( writer->open( filename, fourcc, fps, frameSize, isColor != 0 ))