Move the sources to trunk
[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 typedef struct CvCaptureCAM_TYZX
54 {
55     CvCaptureVTable* vtable;
56         int index;
57     IplImage* image;
58 }
59 CvCaptureCAM_TYZX;
60
61 static int        icvOpenCAM_TYZX          (CvCaptureCAM_TYZX * capture, int wIndex );
62 static int        icvSetPropertyCAM_TYZX   (CvCaptureCAM_TYZX* capture, int property_id, double value );
63 static void       icvCloseCAM_TYZX         (CvCaptureCAM_TYZX* capture );
64 static int        icvGrabFrameCAM_TYZX     (CvCaptureCAM_TYZX* capture );
65 static IplImage * icvRetrieveFrameCAM_TYZX (CvCaptureCAM_TYZX* capture ); 
66 static double     icvGetPropertyCAM_TYZX   (CvCaptureCAM_TYZX* capture, int property_id );
67 static int        icvSetPropertyCAM_TYZX   (CvCaptureCAM_TYZX* capture, int property_id, double value );
68
69 static CvCaptureVTable captureCAM_TYZX_vtable =
70 {
71         6,
72         (CvCaptureCloseFunc)icvCloseCAM_TYZX,
73         (CvCaptureGrabFrameFunc)icvGrabFrameCAM_TYZX,
74         (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_TYZX,
75         (CvCaptureGetPropertyFunc)icvGetPropertyCAM_TYZX,
76         (CvCaptureSetPropertyFunc)icvSetPropertyCAM_TYZX,
77         (CvCaptureGetDescriptionFunc)0
78 };
79
80
81
82 DeepSeaIF * g_tyzx_camera   = 0;
83 int         g_tyzx_refcount = 0;
84
85 static int icvOpenCAM_TYZX(CvCaptureCAM_TYZX * capture, int index )
86 {
87         if(!g_tyzx_camera){
88                 g_tyzx_camera = new DeepSeaIF;
89                 if(!g_tyzx_camera) return 0;
90         
91                 if(!g_tyzx_camera->initializeSettings(NULL)){
92                         delete g_tyzx_camera;
93                         return 0;
94                 }
95         
96                 // set initial sensor mode
97                 // TODO is g_tyzx_camera redundant?
98                 g_tyzx_camera->setSensorMode(g_tyzx_camera->getSensorMode());
99
100                 // mm's
101                 g_tyzx_camera->setZUnits((int) 1000);
102                 
103             g_tyzx_camera->enableLeftColor(true);
104                 g_tyzx_camera->setColorMode(DeepSeaIF::BGRcolor);
105                 g_tyzx_camera->setDoIntensityCrop(true);
106                 g_tyzx_camera->enable8bitImages(true);
107                 if(!g_tyzx_camera->startCapture()){
108                         return 0;
109                 }
110                 g_tyzx_refcount++;
111         }
112         capture->index=index;
113         return 1;
114 }
115
116 static void icvCloseCAM_TYZX( CvCaptureCAM_TYZX* capture )
117 {
118         if( capture && capture->image )
119         {
120                 cvReleaseImage( &capture->image );
121                 capture->image = 0;
122                 g_tyzx_refcount--;
123                 if(g_tyzx_refcount==0){
124                         delete g_tyzx_camera;
125                 }
126         }
127 }
128
129 static int icvGrabFrameCAM_TYZX (CvCaptureCAM_TYZX * )
130 {
131         return g_tyzx_camera && g_tyzx_camera->grab();
132 }
133
134 static void icvAllocateImageCAM_TYZX (CvCaptureCAM_TYZX * capture)
135 {
136         int depth, nch;
137         CvSize size;
138
139         // assume we want to resize
140         if(capture->image)
141         {
142                 cvReleaseImage(&(capture->image));
143         }
144
145         // figure out size depending on index provided
146         switch(capture->index){
147                 case CV_TYZX_RIGHT:
148                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
149                         depth = 8;
150                         nch = 1;
151                         break;
152                 case CV_TYZX_Z:
153                         size = cvSize(g_tyzx_camera->zWidth(), g_tyzx_camera->zHeight());
154                         depth = IPL_DEPTH_16S;
155                         nch = 1;
156                         break;
157                 case CV_TYZX_LEFT:
158                 default:
159                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
160                         depth = 8;
161                         nch = 1;
162                         break;
163         }
164         capture->image = cvCreateImage(size, depth, nch);
165 }
166
167 /// Copy 'grabbed' image into capture buffer and return it.
168 static IplImage * icvRetrieveFrameCAM_TYZX (CvCaptureCAM_TYZX * capture)
169 {
170         if(!capture || !g_tyzx_camera) return 0;
171
172         if(!capture->image){
173                 icvAllocateImageCAM_TYZX(capture);
174                 if(!capture->image) return 0;
175         }
176
177         // copy camera image into buffer.
178         // tempting to reference TYZX memory directly to avoid copying.
179         switch (capture->index)
180         {
181                 case CV_TYZX_RIGHT:
182                         memcpy(capture->image->imageData, g_tyzx_camera->getRImage(), capture->image->imageSize);
183                         break;
184                 case CV_TYZX_Z:
185                         memcpy(capture->image->imageData, g_tyzx_camera->getZImage(), capture->image->imageSize);
186                         break;
187                 case CV_TYZX_LEFT:
188                 default:
189                         memcpy(capture->image->imageData, g_tyzx_camera->getLImage(), capture->image->imageSize);
190                         break;
191         }
192
193         return capture->image;
194 }
195
196 static double icvGetPropertyCAM_TYZX (CvCaptureCAM_TYZX * capture, int property_id)
197 {
198         CvSize size;
199         switch(capture->index)
200         {
201                 case CV_TYZX_LEFT:
202                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
203                         break;
204                 case CV_TYZX_RIGHT:
205                         size = cvSize(g_tyzx_camera->intensityWidth(), g_tyzx_camera->intensityHeight());
206                         break;
207                 case CV_TYZX_Z:
208                         size = cvSize(g_tyzx_camera->zWidth(), g_tyzx_camera->zHeight());
209                         break;
210                 default:
211                         size = cvSize(0,0);
212         }
213         
214         switch( property_id )
215         {
216                 case CV_CAP_PROP_FRAME_WIDTH:
217                         return size.width;
218                 case CV_CAP_PROP_FRAME_HEIGHT:
219                         return size.height;
220         }
221         
222         return 0;
223 }
224
225 static int icvSetPropertyCAM_TYZX (CvCaptureCAM_TYZX *, int, double )
226 {
227         int retval = -1;
228         return retval;
229 }
230
231 CvCapture * cvCaptureFromCAM_TYZX (int index)
232 {
233         CvCaptureCAM_TYZX * capture = (CvCaptureCAM_TYZX*) cvAlloc( sizeof(*capture));
234         memset( capture, 0, sizeof(*capture));
235         capture->vtable = &captureCAM_TYZX_vtable;
236
237         if (icvOpenCAM_TYZX( capture, index ))
238                 return (CvCapture*)capture;
239
240         cvReleaseCapture( (CvCapture**)&capture );
241         return 0;
242 }