From 469ec7c522a971b5a50261f20f83a0c7f1770852 Mon Sep 17 00:00:00 2001 From: Maria Dimashova Date: Wed, 11 Jul 2012 09:04:25 +0000 Subject: [PATCH] fixed vfw (#2156) --- modules/highgui/src/cap_vfw.cpp | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/highgui/src/cap_vfw.cpp b/modules/highgui/src/cap_vfw.cpp index 2debbc1471..8e5b1e4723 100644 --- a/modules/highgui/src/cap_vfw.cpp +++ b/modules/highgui/src/cap_vfw.cpp @@ -182,6 +182,12 @@ bool CvCaptureAVI_VFW::open( const char* filename ) getframe = AVIStreamGetFrameOpen( avistream, &bmihdr ); if( getframe != 0 ) return true; + + // Attempt to open as 8-bit AVI. + bmihdr = icvBitmapHeader( size.width, size.height, 8); + getframe = AVIStreamGetFrameOpen( avistream, &bmihdr ); + if( getframe != 0 ) + return true; } } } @@ -201,14 +207,28 @@ IplImage* CvCaptureAVI_VFW::retrieveFrame(int) { if( avistream && bmih ) { + bool isColor = bmih->biBitCount == 24; + int nChannels = (isColor) ? 3 : 1; IplImage src; cvInitImageHeader( &src, cvSize( bmih->biWidth, bmih->biHeight ), - IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 ); - cvSetData( &src, (char*)(bmih + 1), src.widthStep ); + IPL_DEPTH_8U, nChannels, IPL_ORIGIN_BL, 4 ); + + char* dataPtr = (char*)(bmih + 1); + + // Only account for the color map size if we are an 8-bit image and the color map is used + if (!isColor) + { + static int RGBQUAD_SIZE_PER_BYTE = sizeof(RGBQUAD)/sizeof(BYTE); + int offsetFromColormapToData = (int)bmih->biClrUsed*RGBQUAD_SIZE_PER_BYTE; + dataPtr += offsetFromColormapToData; + } + + cvSetData( &src, dataPtr, src.widthStep ); + if( !frame || frame->width != src.width || frame->height != src.height ) { cvReleaseImage( &frame ); - frame = cvCreateImage( cvGetSize(&src), 8, 3 ); + frame = cvCreateImage( cvGetSize(&src), 8, nChannels ); } cvFlip( &src, frame, 0 );