Update to 2.0.0 tree from current Fremantle build
[opencv] / src / highgui / cvcap_dshow.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 "_highgui.h"
43
44 #ifdef HAVE_VIDEOINPUT
45 #include "videoinput.h"
46
47 /********************* Capturing video from camera via VFW *********************/
48
49 class CvCaptureCAM_DShow : public CvCapture
50 {
51 public:
52     CvCaptureCAM_DShow();
53     virtual ~CvCaptureCAM_DShow() { close(); }
54
55     virtual bool open( int index );
56     virtual void close();
57     virtual double getProperty(int);
58     virtual bool setProperty(int, double);
59     virtual bool grabFrame();
60     virtual IplImage* retrieveFrame(int);
61         virtual int getCaptureDomain() { return CV_CAP_DSHOW; } // Return the type of the capture object: CV_CAP_VFW, etc...
62
63 protected:
64     void init();
65
66     int index;
67     IplImage* frame;
68     static videoInput VI;
69 };
70
71
72 struct SuppressVideoInputMessages
73 {
74     SuppressVideoInputMessages() { videoInput::setVerbose(false); }
75 };
76
77 static SuppressVideoInputMessages do_it;
78 videoInput CvCaptureCAM_DShow::VI;
79
80 CvCaptureCAM_DShow::CvCaptureCAM_DShow()
81 {
82     index = -1;
83     frame = 0;
84 }
85
86 void CvCaptureCAM_DShow::close()
87 {
88     if( index >= 0 )
89     {
90         VI.stopDevice(index);
91         index = -1;
92         cvReleaseImage(&frame);
93     }
94 }
95
96 // Initialize camera input
97 bool CvCaptureCAM_DShow::open( int _index )
98 {
99     close();
100     VI.setupDevice(_index);
101     if( !VI.isDeviceSetup(_index) )
102         return false;
103     index = _index;
104     return true;
105 }
106
107 bool CvCaptureCAM_DShow::grabFrame()
108 {
109     return true;
110 }
111
112
113 IplImage* CvCaptureCAM_DShow::retrieveFrame(int)
114 {
115     if( !frame || VI.getWidth(index) != frame->width || VI.getHeight(index) != frame->height )
116     {
117         if (frame)
118             cvReleaseImage( &frame );
119         int w = VI.getWidth(index), h = VI.getHeight(index);
120         frame = cvCreateImage( cvSize(w,h), 8, 3 );
121     }
122
123     VI.getPixels( index, (uchar*)frame->imageData, false, true );
124     return frame;
125 }
126
127 double CvCaptureCAM_DShow::getProperty( int property_id )
128 {
129     switch( property_id )
130     {
131     case CV_CAP_PROP_FRAME_WIDTH:
132         return VI.getWidth(index);
133     case CV_CAP_PROP_FRAME_HEIGHT:
134         return VI.getHeight(index);
135     case CV_CAP_PROP_FOURCC:
136         return 0;
137     }
138     return 0;
139 }
140
141 bool CvCaptureCAM_DShow::setProperty( int property_id, double value )
142 {
143     int width = 0, height = 0;
144
145     switch( property_id )
146     {
147     case CV_CAP_PROP_FRAME_WIDTH:
148         width = cvRound(value);
149         height = width*3/4;
150         break;
151     case CV_CAP_PROP_FRAME_HEIGHT:
152         height = cvRound(value);
153         width = height*4/3;
154     default:
155         return false;
156     }
157
158     if( width != VI.getWidth(index) || height != VI.getHeight(index) )
159     {
160         VI.stopDevice(index);
161         VI.setupDevice(index, width, height);
162     }
163     return VI.isDeviceSetup(index);
164 }
165
166
167 CvCapture* cvCreateCameraCapture_DShow( int index )
168 {
169     CvCaptureCAM_DShow* capture = new CvCaptureCAM_DShow;
170
171     if( capture->open( index ))
172         return capture;
173
174     delete capture;
175     return 0;
176 }
177
178 #ifdef _MSC_VER
179 #pragma comment(lib, "videoInput.lib")
180 #endif
181
182 #endif