Update to 2.0.0 tree from current Fremantle build
[opencv] / src / highgui / cvcap_tyzx.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 #include <DeepSeaIF.h>
44
45 #if _MSC_VER >= 1200
46         #pragma comment(lib,"DeepSeaIF.lib")
47 #endif
48
49
50 /****************** Capturing video from TYZX stereo camera  *******************/
51 /** Initially developed by Roman Stanchak rstanchak@yahoo.com                  */
52
53 class CvCaptureCAM_TYZX : public CvCapture
54 {
55 public:
56     CvCaptureCAM_TYZX() { index = -1; image = 0; }
57     virtual ~CvCaptureCAM_TYZX() { close(); }
58
59     virtual bool open( int _index );
60     virtual void close();
61     bool isOpened() { return index >= 0; }
62
63     virtual double getProperty(int);
64     virtual bool setProperty(int, double) { return false; }
65     virtual bool grabFrame();
66     virtual IplImage* retrieveFrame(int);
67         virtual int getCaptureDomain() { return CV_CAP_TYZX; } // Return the type of the capture object: CV_CAP_VFW, etc...
68
69 protected:
70     virtual bool allocateImage();
71
72         int index;
73     IplImage* image;
74 }
75 CvCaptureCAM_TYZX;
76
77 DeepSeaIF * g_tyzx_camera   = 0;
78 int         g_tyzx_refcount = 0;
79
80 bool CvCaptureCAM_TYZX::open( int _index )
81 {
82         close();
83
84     if(!g_tyzx_camera){
85                 g_tyzx_camera = new DeepSeaIF;
86                 if(!g_tyzx_camera) return false;
87
88                 if(!g_tyzx_camera->initializeSettings(NULL)){
89                         delete g_tyzx_camera;
90                         return false;
91                 }
92
93                 // set initial sensor mode
94                 // TODO is g_tyzx_camera redundant?
95                 g_tyzx_camera->setSensorMode(g_tyzx_camera->getSensorMode());
96
97                 // mm's
98                 g_tyzx_camera->setZUnits((int) 1000);
99
100             g_tyzx_camera->enableLeftColor(true);
101                 g_tyzx_camera->setColorMode(DeepSeaIF::BGRcolor);
102                 g_tyzx_camera->setDoIntensityCrop(true);
103                 g_tyzx_camera->enable8bitImages(true);
104                 if(!g_tyzx_camera->startCapture()){
105                         return false;
106                 }
107                 g_tyzx_refcount++;
108         }
109         index = _index;
110         return true;
111 }
112
113 void CvCaptureCAM_TYZX::close()
114 {
115         if( isOpened() )
116         {
117                 cvReleaseImage( &image );
118                 g_tyzx_refcount--;
119                 if(g_tyzx_refcount==0){
120                         delete g_tyzx_camera;
121                 }
122         }
123 }
124
125 bool CvCaptureCAM_TYZX::grabFrame()
126 {
127         return isOpened() && g_tyzx_camera && g_tyzx_camera->grab();
128 }
129
130 bool CvCaptureCAM_TYZX::allocateImage()
131 {
132         int depth, nch;
133         CvSize size;
134
135         // assume we want to resize
136     cvReleaseImage(&image);
137
138         // figure out size depending on index provided
139         switch(index){
140                 case CV_TYZX_RIGHT:
141                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
142                         depth = 8;
143                         nch = 1;
144                         break;
145                 case CV_TYZX_Z:
146                         size = cvSize(g_tyzx_camera->zWidth(), g_tyzx_camera->zHeight());
147                         depth = IPL_DEPTH_16S;
148                         nch = 1;
149                         break;
150                 case CV_TYZX_LEFT:
151                 default:
152                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
153                         depth = 8;
154                         nch = 1;
155                         break;
156         }
157         image = cvCreateImage(size, depth, nch);
158     return image != 0;
159 }
160
161 /// Copy 'grabbed' image into capture buffer and return it.
162 IplImage * CvCaptureCAM_TYZX::retrieveFrame(int)
163 {
164         if(!isOpened() || !g_tyzx_camera) return 0;
165
166         if(!image && !alocateImage())
167         return 0;
168
169         // copy camera image into buffer.
170         // tempting to reference TYZX memory directly to avoid copying.
171         switch (index)
172         {
173                 case CV_TYZX_RIGHT:
174                         memcpy(image->imageData, g_tyzx_camera->getRImage(), image->imageSize);
175                         break;
176                 case CV_TYZX_Z:
177                         memcpy(image->imageData, g_tyzx_camera->getZImage(), image->imageSize);
178                         break;
179                 case CV_TYZX_LEFT:
180                 default:
181                         memcpy(image->imageData, g_tyzx_camera->getLImage(), image->imageSize);
182                         break;
183         }
184
185         return image;
186 }
187
188 double CvCaptureCAM_TYZX::getProperty(int property_id)
189 {
190         CvSize size;
191         switch(capture->index)
192         {
193                 case CV_TYZX_LEFT:
194                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
195                         break;
196                 case CV_TYZX_RIGHT:
197                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
198                         break;
199                 case CV_TYZX_Z:
200                         size = cvSize(g_tyzx_camera->zWidth(), g_tyzx_camera->zHeight());
201                         break;
202                 default:
203                         size = cvSize(0,0);
204         }
205
206         switch( property_id )
207         {
208                 case CV_CAP_PROP_FRAME_WIDTH:
209                         return size.width;
210                 case CV_CAP_PROP_FRAME_HEIGHT:
211                         return size.height;
212         }
213
214         return 0;
215 }
216
217 bool CvCaptureCAM_TYZX::setProperty( int, double )
218 {
219         return false;
220 }
221
222 CvCapture * cvCreateCameraCapture_TYZX (int index)
223 {
224         CvCaptureCAM_TYZX * capture = new CvCaptureCAM_TYZX;
225     if( capture->open(index) )
226         return capture;
227
228     delete capture;
229     return 0;
230 }