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