Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / highgui / src / cap_vfw.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #include <vfw.h>
45
46 #ifdef __GNUC__
47 #define WM_CAP_FIRSTA              (WM_USER)
48 #define capSendMessage(hwnd,m,w,l) (IsWindow(hwnd)?SendMessage(hwnd,m,w,l):0)
49 #endif
50
51 #if defined _M_X64 && defined _MSC_VER
52 #pragma optimize("",off)
53 #pragma warning(disable: 4748)
54 #endif
55
56 /********************* Capturing video from AVI via VFW ************************/
57
58 static BITMAPINFOHEADER icvBitmapHeader( int width, int height, int bpp, int compression = BI_RGB )
59 {
60     BITMAPINFOHEADER bmih;
61     memset( &bmih, 0, sizeof(bmih));
62     bmih.biSize = sizeof(bmih);
63     bmih.biWidth = width;
64     bmih.biHeight = height;
65     bmih.biBitCount = (WORD)bpp;
66     bmih.biCompression = compression;
67     bmih.biPlanes = 1;
68
69     return bmih;
70 }
71
72
73 static void icvInitCapture_VFW()
74 {
75     static int isInitialized = 0;
76     if( !isInitialized )
77     {
78         AVIFileInit();
79         isInitialized = 1;
80     }
81 }
82
83
84 class CvCaptureAVI_VFW : public CvCapture
85 {
86 public:
87     CvCaptureAVI_VFW()
88     {
89       CoInitialize(NULL);
90       init();
91     }
92
93     virtual ~CvCaptureAVI_VFW()
94     {
95         close();
96         CoUninitialize();
97     }
98
99     virtual bool open( const char* filename );
100     virtual void close();
101
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...
107
108 protected:
109     void init();
110
111     PAVIFILE            avifile;
112     PAVISTREAM          avistream;
113     PGETFRAME           getframe;
114     AVISTREAMINFO       aviinfo;
115     BITMAPINFOHEADER  * bmih;
116     CvSlice             film_range;
117     double              fps;
118     int                 pos;
119     IplImage*           frame;
120     CvSize              size;
121 };
122
123
124 void CvCaptureAVI_VFW::init()
125 {
126     avifile = 0;
127     avistream = 0;
128     getframe = 0;
129     memset( &aviinfo, 0, sizeof(aviinfo) );
130     bmih = 0;
131     film_range = cvSlice(0,0);
132     fps = 0;
133     pos = 0;
134     frame = 0;
135     size = cvSize(0,0);
136 }
137
138
139 void CvCaptureAVI_VFW::close()
140 {
141     if( getframe )
142         AVIStreamGetFrameClose( getframe );
143
144     if( avistream )
145         AVIStreamRelease( avistream );
146
147     if( avifile )
148         AVIFileRelease( avifile );
149
150     if (frame)
151         cvReleaseImage( &frame );
152
153     init();
154 }
155
156
157 bool CvCaptureAVI_VFW::open( const char* filename )
158 {
159     close();
160     icvInitCapture_VFW();
161
162     if( !filename )
163         return false;
164
165     HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL );
166     if( SUCCEEDED(hr))
167     {
168         hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 );
169         if( SUCCEEDED(hr))
170         {
171             hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo));
172             if( SUCCEEDED(hr))
173             {
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 );
177
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 );
183                 if( getframe != 0 )
184                     return true;
185
186                 // Attempt to open as 8-bit AVI.
187                 bmihdr = icvBitmapHeader( size.width, size.height, 8);
188                 getframe = AVIStreamGetFrameOpen( avistream, &bmihdr );
189                 if( getframe != 0 )
190                     return true;
191             }
192         }
193     }
194
195     close();
196     return false;
197 }
198
199 bool CvCaptureAVI_VFW::grabFrame()
200 {
201     if( avistream )
202         bmih = (BITMAPINFOHEADER*)AVIStreamGetFrame( getframe, pos++ );
203     return bmih != 0;
204 }
205
206 IplImage* CvCaptureAVI_VFW::retrieveFrame(int)
207 {
208     if( avistream && bmih )
209     {
210         bool isColor = bmih->biBitCount == 24;
211         int nChannels = (isColor) ? 3 : 1;
212         IplImage src;
213         cvInitImageHeader( &src, cvSize( bmih->biWidth, bmih->biHeight ),
214                            IPL_DEPTH_8U, nChannels, IPL_ORIGIN_BL, 4 );
215
216         char* dataPtr = (char*)(bmih + 1);
217
218         // Only account for the color map size if we are an 8-bit image and the color map is used
219         if (!isColor)
220         {
221             static int RGBQUAD_SIZE_PER_BYTE = sizeof(RGBQUAD)/sizeof(BYTE);
222             int offsetFromColormapToData = (int)bmih->biClrUsed*RGBQUAD_SIZE_PER_BYTE;
223             dataPtr += offsetFromColormapToData;
224         }
225
226         cvSetData( &src, dataPtr, src.widthStep );
227
228         if( !frame || frame->width != src.width || frame->height != src.height )
229         {
230             cvReleaseImage( &frame );
231             frame = cvCreateImage( cvGetSize(&src), 8, nChannels );
232         }
233
234         cvFlip( &src, frame, 0 );
235         return frame;
236     }
237
238     return 0;
239 }
240
241 double CvCaptureAVI_VFW::getProperty( int property_id )
242 {
243     switch( property_id )
244     {
245     case CV_CAP_PROP_POS_MSEC:
246         return cvRound(pos*1000./fps);
247     case CV_CAP_PROP_POS_FRAMES:
248         return pos;
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:
253         return size.width;
254     case CV_CAP_PROP_FRAME_HEIGHT:
255         return size.height;
256     case CV_CAP_PROP_FPS:
257         return 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;
262     }
263     return 0;
264 }
265
266 bool CvCaptureAVI_VFW::setProperty( int property_id, double value )
267 {
268     switch( property_id )
269     {
270     case CV_CAP_PROP_POS_MSEC:
271     case CV_CAP_PROP_POS_FRAMES:
272     case CV_CAP_PROP_POS_AVI_RATIO:
273         {
274             switch( property_id )
275             {
276             case CV_CAP_PROP_POS_MSEC:
277                 pos = cvRound(value*fps*0.001);
278                 break;
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);
283                 break;
284             default:
285                 pos = cvRound(value);
286             }
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;
291         }
292         break;
293     default:
294         return false;
295     }
296
297     return true;
298 }
299
300 CvCapture* cvCreateFileCapture_VFW (const char* filename)
301 {
302     CvCaptureAVI_VFW* capture = new CvCaptureAVI_VFW;
303     if( capture->open(filename) )
304         return capture;
305     delete capture;
306     return 0;
307 }
308
309
310 /********************* Capturing video from camera via VFW *********************/
311
312 class CvCaptureCAM_VFW : public CvCapture
313 {
314 public:
315     CvCaptureCAM_VFW() { init(); }
316     virtual ~CvCaptureCAM_VFW() { close(); }
317
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...
325
326 protected:
327     void init();
328     void closeHIC();
329     static LRESULT PASCAL frameCallback( HWND hWnd, VIDEOHDR* hdr );
330
331     CAPDRIVERCAPS caps;
332     HWND   capWnd;
333     VIDEOHDR* hdr;
334     DWORD  fourcc;
335     HIC    hic;
336     IplImage* frame;
337 };
338
339
340 void CvCaptureCAM_VFW::init()
341 {
342     memset( &caps, 0, sizeof(caps) );
343     capWnd = 0;
344     hdr = 0;
345     fourcc = 0;
346     hic = 0;
347     frame = 0;
348 }
349
350 void CvCaptureCAM_VFW::closeHIC()
351 {
352     if( hic )
353     {
354         ICDecompressEnd( hic );
355         ICClose( hic );
356         hic = 0;
357     }
358 }
359
360
361 LRESULT PASCAL CvCaptureCAM_VFW::frameCallback( HWND hWnd, VIDEOHDR* hdr )
362 {
363     CvCaptureCAM_VFW* capture = 0;
364
365     if (!hWnd) return FALSE;
366
367     capture = (CvCaptureCAM_VFW*)capGetUserData(hWnd);
368     capture->hdr = hdr;
369
370     return (LRESULT)TRUE;
371 }
372
373
374 // Initialize camera input
375 bool CvCaptureCAM_VFW::open( int wIndex )
376 {
377     char szDeviceName[80];
378     char szDeviceVersion[80];
379     HWND hWndC = 0;
380
381     close();
382
383     if( (unsigned)wIndex >= 10 )
384         wIndex = 0;
385
386     for( ; wIndex < 10; wIndex++ )
387     {
388         if( capGetDriverDescription( wIndex, szDeviceName,
389             sizeof (szDeviceName), szDeviceVersion,
390             sizeof (szDeviceVersion)))
391         {
392             hWndC = capCreateCaptureWindow ( "My Own Capture Window",
393                 WS_POPUP | WS_CHILD, 0, 0, 320, 240, 0, 0);
394             if( capDriverConnect (hWndC, wIndex))
395                 break;
396             DestroyWindow( hWndC );
397             hWndC = 0;
398         }
399     }
400
401     if( hWndC )
402     {
403         capWnd = hWndC;
404         hdr = 0;
405         hic = 0;
406         fourcc = (DWORD)-1;
407
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 );
413         CAPTUREPARMS p;
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);
420     }
421     return capWnd != 0;
422 }
423
424
425 void CvCaptureCAM_VFW::close()
426 {
427     if( capWnd )
428     {
429         capSetCallbackOnFrame( capWnd, NULL );
430         capDriverDisconnect( capWnd );
431         DestroyWindow( capWnd );
432         closeHIC();
433     }
434     cvReleaseImage( &frame );
435     init();
436 }
437
438
439 bool CvCaptureCAM_VFW::grabFrame()
440 {
441     if( capWnd )
442     {
443         SendMessage( capWnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0 );
444         return true;
445     }
446     return false;
447 }
448
449
450 IplImage* CvCaptureCAM_VFW::retrieveFrame(int)
451 {
452     BITMAPINFO vfmt;
453     memset( &vfmt, 0, sizeof(vfmt));
454     BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader;
455     int sz, prevWidth, prevHeight;
456
457     if( !capWnd )
458         return 0;
459
460     sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt));
461     prevWidth = frame ? frame->width : 0;
462     prevHeight = frame ? frame->height : 0;
463
464     if( !hdr || hdr->lpData == 0 || sz == 0 )
465         return 0;
466
467     if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight )
468     {
469         cvReleaseImage( &frame );
470         frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 );
471     }
472
473     if( vfmt.bmiHeader.biCompression != BI_RGB ||
474         vfmt.bmiHeader.biBitCount != 24 )
475     {
476         BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 );
477
478         if( hic == 0 || fourcc != vfmt0.biCompression ||
479             prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight )
480         {
481             closeHIC();
482             hic = ICOpen( MAKEFOURCC('V','I','D','C'),
483                           vfmt0.biCompression, ICMODE_DECOMPRESS );
484             if( hic )
485             {
486                 if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK )
487                 {
488                     closeHIC();
489                     return 0;
490                 }
491             }
492         }
493
494         if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData,
495             &vfmt1, frame->imageData ) != ICERR_OK )
496         {
497             closeHIC();
498             return 0;
499         }
500
501         cvFlip( frame, frame, 0 );
502     }
503     else
504     {
505         IplImage src;
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 );
510     }
511
512     return frame;
513 }
514
515
516 double CvCaptureCAM_VFW::getProperty( int property_id )
517 {
518     switch( property_id )
519     {
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:
525         return fourcc;
526     }
527     return 0;
528 }
529
530
531 CvCapture* cvCreateCameraCapture_VFW( int index )
532 {
533     CvCaptureCAM_VFW* capture = new CvCaptureCAM_VFW;
534
535     if( capture->open( index ))
536         return capture;
537
538     delete capture;
539     return 0;
540 }
541
542
543 /*************************** writing AVIs ******************************/
544
545 class CvVideoWriter_VFW : public CvVideoWriter
546 {
547 public:
548     CvVideoWriter_VFW() { init(); }
549     virtual ~CvVideoWriter_VFW() { close(); }
550
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* );
555
556 protected:
557     void init();
558     bool createStreams( CvSize frameSize, bool isColor );
559
560     PAVIFILE      avifile;
561     PAVISTREAM    compressed;
562     PAVISTREAM    uncompressed;
563     double        fps;
564     IplImage*     tempFrame;
565     long          pos;
566     int           fourcc;
567 };
568
569
570 void CvVideoWriter_VFW::init()
571 {
572     avifile = 0;
573     compressed = uncompressed = 0;
574     fps = 0;
575     tempFrame = 0;
576     pos = 0;
577     fourcc = 0;
578 }
579
580 void CvVideoWriter_VFW::close()
581 {
582     if( uncompressed )
583         AVIStreamRelease( uncompressed );
584     if( compressed )
585         AVIStreamRelease( compressed );
586     if( avifile )
587         AVIFileRelease( avifile );
588     cvReleaseImage( &tempFrame );
589     init();
590 }
591
592
593 // philipg.  Made this code capable of writing 8bpp gray scale bitmaps
594 struct BITMAPINFO_8Bit
595 {
596     BITMAPINFOHEADER bmiHeader;
597     RGBQUAD          bmiColors[256];
598 };
599
600
601 bool CvVideoWriter_VFW::open( const char* filename, int _fourcc, double _fps, CvSize frameSize, bool isColor )
602 {
603     close();
604
605     icvInitCapture_VFW();
606     if( AVIFileOpen( &avifile, filename, OF_CREATE | OF_WRITE, 0 ) == AVIERR_OK )
607     {
608         fourcc = _fourcc;
609         fps = _fps;
610         if( frameSize.width > 0 && frameSize.height > 0 &&
611             !createStreams( frameSize, isColor ) )
612         {
613             close();
614             return false;
615         }
616         return true;
617     }
618     else
619         return false;
620 }
621
622
623 bool CvVideoWriter_VFW::createStreams( CvSize frameSize, bool isColor )
624 {
625     if( !avifile )
626         return false;
627     AVISTREAMINFO aviinfo;
628
629     BITMAPINFO_8Bit bmih;
630     bmih.bmiHeader = icvBitmapHeader( frameSize.width, frameSize.height, isColor ? 24 : 8 );
631     for( int i = 0; i < 256; i++ )
632     {
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;
637     }
638
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;
648
649     if( AVIFileCreateStream( avifile, &uncompressed, &aviinfo ) == AVIERR_OK )
650     {
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));
660         copts.lpParms = 0;
661         copts.cbParms = 0;
662         copts.dwInterleaveEvery = 0;
663
664         if( fourcc != -1 || AVISaveOptions( 0, 0, 1, &uncompressed, &pcopts ) == TRUE )
665         {
666             if( AVIMakeCompressedStream( &compressed, uncompressed, pcopts, 0 ) == AVIERR_OK &&
667                 AVIStreamSetFormat( compressed, 0, &bmih, sizeof(bmih)) == AVIERR_OK )
668             {
669                 fps = fps;
670                 fourcc = (int)copts.fccHandler;
671                 frameSize = frameSize;
672                 tempFrame = cvCreateImage( frameSize, 8, (isColor ? 3 : 1) );
673                 return true;
674             }
675         }
676     }
677     return false;
678 }
679
680
681 bool CvVideoWriter_VFW::writeFrame( const IplImage* image )
682 {
683     bool result = false;
684     CV_FUNCNAME( "CvVideoWriter_VFW::writeFrame" );
685
686     __BEGIN__;
687
688     if( !image )
689         EXIT;
690
691     if( !compressed && !createStreams( cvGetSize(image), image->nChannels > 1 ))
692         EXIT;
693
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" );
697
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))
702     {
703         cvConvertImage( image, tempFrame, image->origin == 0 ? CV_CVTIMG_FLIP : 0 );
704         image = (const IplImage*)tempFrame;
705     }
706
707     result = AVIStreamWrite( compressed, pos++, 1, image->imageData,
708                              image->imageSize, AVIIF_KEYFRAME, 0, 0 ) == AVIERR_OK;
709
710     __END__;
711
712     return result;
713 }
714
715 CvVideoWriter* cvCreateVideoWriter_VFW( const char* filename, int fourcc,
716                                         double fps, CvSize frameSize, int isColor )
717 {
718     CvVideoWriter_VFW* writer = new CvVideoWriter_VFW;
719     if( writer->open( filename, fourcc, fps, frameSize, isColor != 0 ))
720         return writer;
721     delete writer;
722     return 0;
723 }