1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2008, Xavier Delacour, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 // 2008-04-27 Xavier Delacour <xavier.delacour@gmail.com>
52 #define CV_WARN(message)
54 #define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
57 struct CvCapture_Unicap : public CvCapture
59 CvCapture_Unicap() { init(); }
60 virtual ~CvCapture_Unicap() { close(); }
62 virtual bool open( int index );
65 virtual double getProperty(int);
66 virtual bool setProperty(int, double);
67 virtual bool grabFrame();
68 virtual IplImage* retrieveFrame(int);
69 virtual int getCaptureDomain() { return CV_CAP_UNICAP; } // Return the type of the capture object: CV_CAP_VFW, etc...
71 bool shutdownDevice();
76 device_initialized = false;
78 desired_size = cvSize(0,0);
82 memset( &device, 0, sizeof(device) );
83 memset( &format_spec, 0, sizeof(format_spec) );
84 memset( &format, 0, sizeof(format) );
85 memset( &raw_buffer, 0, sizeof(raw_buffer) );
86 memset( &buffer, 0, sizeof(buffer) );
88 raw_frame = frame = 0;
91 bool device_initialized;
98 unicap_handle_t handle;
99 unicap_device_t device;
100 unicap_format_t format_spec;
101 unicap_format_t format;
102 unicap_data_buffer_t raw_buffer;
103 unicap_data_buffer_t buffer;
109 bool CvCapture_Unicap::shutdownDevice() {
111 CV_FUNCNAME("CvCapture_Unicap::shutdownDevice");
114 if (!SUCCESS(unicap_stop_capture(handle)))
115 CV_ERROR(CV_StsError, "unicap: failed to stop capture on device\n");
117 if (!SUCCESS(unicap_close(handle)))
118 CV_ERROR(CV_StsError, "unicap: failed to close the device\n");
120 cvReleaseImage(&raw_frame);
121 cvReleaseImage(&frame);
123 device_initialized = false;
130 bool CvCapture_Unicap::initDevice() {
132 CV_FUNCNAME("CvCapture_Unicap::initDevice");
135 if (device_initialized && !shutdownDevice())
138 if(!SUCCESS(unicap_enumerate_devices(NULL, &device, desired_device)))
139 CV_ERROR(CV_StsError, "unicap: failed to get info for device\n");
141 if(!SUCCESS(unicap_open( &handle, &device)))
142 CV_ERROR(CV_StsError, "unicap: failed to open device\n");
144 unicap_void_format(&format_spec);
146 if (!SUCCESS(unicap_enumerate_formats(handle, &format_spec, &format, desired_format))) {
148 CV_ERROR(CV_StsError, "unicap: failed to get video format\n");
152 for (i = format.size_count - 1; i > 0; i--)
153 if (format.sizes[i].width == desired_size.width &&
154 format.sizes[i].height == desired_size.height)
156 format.size.width = format.sizes[i].width;
157 format.size.height = format.sizes[i].height;
159 if (!SUCCESS(unicap_set_format(handle, &format))) {
161 CV_ERROR(CV_StsError, "unicap: failed to set video format\n");
164 memset(&raw_buffer, 0x0, sizeof(unicap_data_buffer_t));
165 raw_frame = cvCreateImage(cvSize(format.size.width,
168 memcpy(&raw_buffer.format, &format, sizeof(raw_buffer.format));
169 raw_buffer.data = (unsigned char*)raw_frame->imageData;
170 raw_buffer.buffer_size = format.size.width *
171 format.size.height * format.bpp / 8;
173 memset(&buffer, 0x0, sizeof(unicap_data_buffer_t));
174 memcpy(&buffer.format, &format, sizeof(buffer.format));
176 buffer.format.fourcc = UCIL_FOURCC('B','G','R','3');
177 buffer.format.bpp = 24;
178 // * todo support greyscale output
179 // buffer.format.fourcc = UCIL_FOURCC('G','R','E','Y');
180 // buffer.format.bpp = 8;
182 frame = cvCreateImage(cvSize(buffer.format.size.width,
183 buffer.format.size.height),
184 8, buffer.format.bpp / 8);
185 buffer.data = (unsigned char*)frame->imageData;
186 buffer.buffer_size = buffer.format.size.width *
187 buffer.format.size.height * buffer.format.bpp / 8;
189 if(!SUCCESS(unicap_start_capture(handle))) {
191 CV_ERROR(CV_StsError, "unicap: failed to start capture on device\n");
194 device_initialized = true;
200 void CvCapture_Unicap::close() {
201 if(device_initialized)
205 bool CvCapture_Unicap::grabFrame() {
208 CV_FUNCNAME("CvCapture_Unicap::grabFrame");
211 unicap_data_buffer_t *returned_buffer;
213 int retry_count = 100;
215 while (retry_count--) {
216 if(!SUCCESS(unicap_queue_buffer(handle, &raw_buffer)))
217 CV_ERROR(CV_StsError, "unicap: failed to queue a buffer on device\n");
219 if(SUCCESS(unicap_wait_buffer(handle, &returned_buffer)))
225 CV_WARN("unicap: failed to wait for buffer on device\n");
233 IplImage * CvCapture_Unicap::retrieveFrame(int) {
235 ucil_convert_buffer(&buffer, &raw_buffer);
241 double CvCapture_Unicap::getProperty(int id) {
243 case CV_CAP_PROP_POS_MSEC: break;
244 case CV_CAP_PROP_POS_FRAMES: break;
245 case CV_CAP_PROP_POS_AVI_RATIO: break;
246 case CV_CAP_PROP_FRAME_WIDTH:
247 return desired_size.width;
248 case CV_CAP_PROP_FRAME_HEIGHT:
249 return desired_size.height;
250 case CV_CAP_PROP_FPS: break;
251 case CV_CAP_PROP_FOURCC: break;
252 case CV_CAP_PROP_FRAME_COUNT: break;
253 case CV_CAP_PROP_FORMAT:
254 return desired_format;
255 case CV_CAP_PROP_MODE: break;
256 case CV_CAP_PROP_BRIGHTNESS: break;
257 case CV_CAP_PROP_CONTRAST: break;
258 case CV_CAP_PROP_SATURATION: break;
259 case CV_CAP_PROP_HUE: break;
260 case CV_CAP_PROP_GAIN: break;
261 case CV_CAP_PROP_CONVERT_RGB:
268 bool CvCapture_Unicap::setProperty(int id, double value) {
272 case CV_CAP_PROP_POS_MSEC: break;
273 case CV_CAP_PROP_POS_FRAMES: break;
274 case CV_CAP_PROP_POS_AVI_RATIO: break;
275 case CV_CAP_PROP_FRAME_WIDTH:
276 desired_size.width = (int)value;
279 case CV_CAP_PROP_FRAME_HEIGHT:
280 desired_size.height = (int)value;
283 case CV_CAP_PROP_FPS: break;
284 case CV_CAP_PROP_FOURCC: break;
285 case CV_CAP_PROP_FRAME_COUNT: break;
286 case CV_CAP_PROP_FORMAT:
290 case CV_CAP_PROP_MODE: break;
291 case CV_CAP_PROP_BRIGHTNESS: break;
292 case CV_CAP_PROP_CONTRAST: break;
293 case CV_CAP_PROP_SATURATION: break;
294 case CV_CAP_PROP_HUE: break;
295 case CV_CAP_PROP_GAIN: break;
296 case CV_CAP_PROP_CONVERT_RGB:
297 convert_rgb = value != 0;
301 if (reinit && !initDevice())
307 bool CvCapture_Unicap::open(int index)
310 device_initialized = false;
312 desired_device = index < 0 ? 0 : index;
314 desired_size = cvSize(320, 240);
321 CvCapture * cvCreateCameraCapture_Unicap(const int index)
323 CvCapture_Unicap *cap = new CvCapture_Unicap;
324 if( cap->open(index) )