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) 2000, Intel Corporation, 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.
46 /****************** Capturing video from camera via CMU lib *******************/
48 #if defined(HAVE_CMU1394)
50 // This firewire capability added by Philip Gruebele (pgruebele@cox.net).
51 // For this to work you need to install the CMU firewire DCAM drivers,
52 // located at http://www-2.cs.cmu.edu/~iwan/1394/.
53 #include "1394camera.h"
55 typedef struct CvCaptureCAM_CMU
57 CvCaptureVTable* vtable;
60 int format; // 0-2, 7 ?
67 int icvOpenCAM_CMU(CvCaptureCAM_CMU * capture, int wIndex );
68 int icvSetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id, double value );
69 void icvCloseCAM_CMU( CvCaptureCAM_CMU* capture );
70 int icvGrabFrameCAM_CMU( CvCaptureCAM_CMU* capture );
71 IplImage* icvRetrieveFrameCAM_CMU( CvCaptureCAM_CMU* capture );
72 double icvGetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id );
73 int icvSetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id, double value );
75 static CvCaptureVTable captureCAM_CMU_vtable =
78 (CvCaptureCloseFunc)icvCloseCAM_CMU,
79 (CvCaptureGrabFrameFunc)icvGrabFrameCAM_CMU,
80 (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_CMU,
81 (CvCaptureGetPropertyFunc)icvGetPropertyCAM_CMU,
82 (CvCaptureSetPropertyFunc)icvSetPropertyCAM_CMU,
83 (CvCaptureGetDescriptionFunc)0
86 // CMU 1394 camera stuff.
87 // This firewire capability added by Philip Gruebele (pgruebele@cox.net)
88 // and modified by Roman Stanchak (rstanchak@yahoo.com).
89 // For this to work you need to install the CMU firewire DCAM drivers,
90 // located at http://www-2.cs.cmu.edu/~iwan/1394/.
91 #define CMU_MAX_CAMERAS 20
92 int CMU_numCameras = 0;
93 int CMU_numActiveCameras = 0;
94 bool CMU_useCameraFlags[CMU_MAX_CAMERAS];
95 C1394Camera *CMU_theCamera = 0;
97 // stupid defines for mode, format, FPS
98 #define CV_CAP_IEEE1394_FPS_1_875 0
99 #define CV_CAP_IEEE1394_FPS_3_75 1
100 #define CV_CAP_IEEE1394_FPS_7_5 2
101 #define CV_CAP_IEEE1394_FPS_15 3
102 #define CV_CAP_IEEE1394_FPS_30 4
103 #define CV_CAP_IEEE1394_FPS_60 5
105 // index by size, color
106 #define CV_CAP_IEEE1394_COLOR_MONO 0
107 #define CV_CAP_IEEE1394_COLOR_MONO16 1
108 #define CV_CAP_IEEE1394_COLOR_YUV444 2
109 #define CV_CAP_IEEE1394_COLOR_YUV422 3
110 #define CV_CAP_IEEE1394_COLOR_YUV411 4
111 #define CV_CAP_IEEE1394_COLOR_RGB 5
113 #define CV_CAP_IEEE1394_SIZE_160X120 0
114 #define CV_CAP_IEEE1394_SIZE_320X240 1
115 #define CV_CAP_IEEE1394_SIZE_640X480 2
116 #define CV_CAP_IEEE1394_SIZE_800X600 3
117 #define CV_CAP_IEEE1394_SIZE_1024X768 4
118 #define CV_CAP_IEEE1394_SIZE_1280X960 5
119 #define CV_CAP_IEEE1394_SIZE_1600X1200 6
121 // given color, size, output format
122 // 1 16 444 422 411 RGB
123 char CV_CAP_IEEE1394_FORMAT[7][6] = { {-1, -1, 0, -1, -1, -1}, // 160x120
124 {-1, -1, -1, 0, -1, -1}, // 320x240
125 { 0, 0, -1, 0, 0, 0}, // 640x480
126 { 1, 1, -1, 1, -1, 1}, // 800x600
127 { 1, 1, -1, 1, -1, 1}, // 1024x768
128 { 2, 2, -1, 2, -1, 2}, // 1280x960
129 { 2, 2, -1, 2, -1, 2}}; // 1600x1200
130 // given color, size, output corresponding mode
131 char CV_CAP_IEEE1394_MODE[7][6] = { {-1, -1, 0, -1, -1, -1}, // 160x120
132 {-1, -1, -1, 1, -1, -1}, // 320x240
133 { 5, 6, -1, 3, 2, 4}, // 640x480
134 { 2, 6, -1, 0, -1, 1}, // 800x600
135 { 5, 7, -1, 3, -1, 4}, // 1024x768
136 { 2, 6, -1, 0, -1, 1}, // 1280x960
137 { 5, 7, -1, 3, -1, 4}}; // 1600x1200
138 // given format, mode, return COLOR
139 char CV_CAP_IEEE1394_COLOR[2][8] = { { CV_CAP_IEEE1394_COLOR_YUV444,
140 CV_CAP_IEEE1394_COLOR_YUV422,
141 CV_CAP_IEEE1394_COLOR_YUV411,
142 CV_CAP_IEEE1394_COLOR_YUV422,
143 CV_CAP_IEEE1394_COLOR_RGB,
144 CV_CAP_IEEE1394_COLOR_MONO,
145 CV_CAP_IEEE1394_COLOR_MONO16 },
146 { CV_CAP_IEEE1394_COLOR_YUV422,
147 CV_CAP_IEEE1394_COLOR_RGB,
148 CV_CAP_IEEE1394_COLOR_MONO,
149 CV_CAP_IEEE1394_COLOR_YUV422,
150 CV_CAP_IEEE1394_COLOR_RGB,
151 CV_CAP_IEEE1394_COLOR_MONO,
152 CV_CAP_IEEE1394_COLOR_MONO16,
153 CV_CAP_IEEE1394_COLOR_MONO16 } };
155 // convert frame rate to suitable enum
156 /*static int icvFrameRateToIndex_CMU(double framerate){
157 if(framerate > 30) return CV_CAP_IEEE1394_FPS_60;
158 else if(framerate > 15) return CV_CAP_IEEE1394_FPS_30;
159 else if(framerate > 7.5) return CV_CAP_IEEE1394_FPS_15;
160 else if(framerate > 3.75) return CV_CAP_IEEE1394_FPS_7_5;
161 else if(framerate > 1.875) return CV_CAP_IEEE1394_FPS_3_75;
162 return CV_CAP_IEEE1394_FPS_1_875;
166 #pragma comment(lib,"1394camera.lib")
169 // return the size of the image
170 CvSize icvGetSize_CMU( C1394Camera * cmucam ){
171 //int format = cmucam->GetVideoFormat();
172 //int mode = cmucam->GetVideoMode();
174 // irrelvant to depth
175 // if( format > 1 ) format=1;
177 // for YUV, should we return a weirdly sized image?
178 //switch(CV_CAP_IEEE1394_COLOR[format][mode]){
179 //case CV_CAP_IEEE1394_COLOR_MONO16:
180 // return cvSize(cmucam->m_width*2, cmucam->m_height);
182 // return cvSize(cmucam->m_width, cmucam->m_height);
184 unsigned long width = 0, height = 0;
185 cmucam->GetVideoFrameDimensions( &width, &height );
186 return cvSize((int)width, (int)height);
189 // return the opencv depth flag corresponding to the camera format
190 int icvGetDepth_CMU( C1394Camera * cmucam ){
191 int format = cmucam->GetVideoFormat();
192 int mode = cmucam->GetVideoMode();
198 // irrelvant to depth
199 if( format > 1 ) format=1;
201 if(CV_CAP_IEEE1394_COLOR[format][mode]==CV_CAP_IEEE1394_COLOR_MONO16){
202 return IPL_DEPTH_16S;
207 // return the number of channels for camera
208 int icvGetNChannels_CMU( C1394Camera * cmucam ){
210 int format = cmucam->GetVideoFormat();
211 int mode = cmucam->GetVideoMode();
217 // irrelvant to nchannels
218 if( format > 1 ) format=1;
220 switch(CV_CAP_IEEE1394_COLOR[format][mode]){
221 case CV_CAP_IEEE1394_COLOR_RGB:
223 case CV_CAP_IEEE1394_COLOR_MONO:
224 case CV_CAP_IEEE1394_COLOR_MONO16:
226 case CV_CAP_IEEE1394_COLOR_YUV422:
227 case CV_CAP_IEEE1394_COLOR_YUV444:
228 case CV_CAP_IEEE1394_COLOR_YUV411:
236 int icvOpenCAM_CMU(CvCaptureCAM_CMU * capture, int index )
238 // if first time, then allocate all available cameras
239 if( CMU_numCameras == 0 )
241 CMU_numActiveCameras = 0;
242 CMU_theCamera = new C1394Camera[CMU_MAX_CAMERAS];
244 ////////////////////////////////////////////////////////////////////////////////////////////////////////
245 // create all cameras
249 if( CMU_theCamera[0].CheckLink() != CAM_SUCCESS )
252 // we have one pin per camera
253 CMU_numCameras = CMU_theCamera[0].GetNumberCameras();
255 // allocate remaining cameras
256 for(int i = 1; i < CMU_numCameras && i<CMU_MAX_CAMERAS; i++ )
258 CMU_useCameraFlags[i] = false;
259 if (CMU_theCamera[i].CheckLink() != CAM_SUCCESS)
265 // free any allocated cameras
274 // pick first unused camera
276 for(int i = 0; i < CMU_numCameras; i++ )
278 if( !CMU_useCameraFlags[i] ){
285 // no empty camera found
289 if (CMU_theCamera[index].SelectCamera(index) != CAM_SUCCESS)
292 if (CMU_theCamera[index].InitCamera() != CAM_SUCCESS)
295 // set initial format -- try to pick best frame rate first, then color, then size
296 bool found_format = false;
297 for (int rate=5; rate>=0 && !found_format; rate--){
298 for (int color=CV_CAP_IEEE1394_COLOR_RGB; color>=0 && !found_format; color--){
299 for (int size=CV_CAP_IEEE1394_SIZE_1600X1200; size>=0 && !found_format; size--){
300 int format = CV_CAP_IEEE1394_FORMAT[size][color];
301 int mode = CV_CAP_IEEE1394_MODE[size][color];
302 if (format!=-1 && mode!=-1 &&
303 CMU_theCamera[index].HasVideoFrameRate(format,mode,rate)){
304 CMU_theCamera[index].SetVideoFormat(format);
305 CMU_theCamera[index].SetVideoMode(mode);
306 CMU_theCamera[index].SetVideoFrameRate(rate);
307 found_format = (CMU_theCamera[index].StartImageAcquisition() == CAM_SUCCESS);
315 CMU_theCamera[index].SetVideoFormat(7);
316 CMU_theCamera[index].SetVideoMode(0);
317 if(CMU_theCamera[index].StartImageAcquisition() != CAM_SUCCESS){
323 // allocate image frame
324 capture->image = cvCreateImage(icvGetSize_CMU(&(CMU_theCamera[index])),
325 icvGetDepth_CMU(&(CMU_theCamera[index])),
326 icvGetNChannels_CMU(&(CMU_theCamera[index])));
329 // successfully activated camera
330 capture->index = index; //CMU_numActiveCameras;
331 CMU_numActiveCameras++;
332 CMU_useCameraFlags[index] = true;
343 void icvCloseCAM_CMU( CvCaptureCAM_CMU* capture )
345 if( capture && capture->image )
347 cvReleaseImage( &capture->image );
348 CMU_theCamera[capture->index].StopImageAcquisition();
349 CMU_useCameraFlags[capture->index] = false;
350 CMU_numActiveCameras--;
352 if (!CMU_numActiveCameras)
354 delete[] CMU_theCamera;
362 int icvGrabFrameCAM_CMU( CvCaptureCAM_CMU* capture )
364 return capture->image && CMU_theCamera &&
365 CMU_theCamera[capture->index].AcquireImage() == CAM_SUCCESS;
368 static void swapRedBlue(IplImage * im){
369 uchar * ptr = (uchar *) im->imageData;
371 for(int i=0; i<im->height; i++){
372 ptr = (uchar *) im->imageData+im->widthStep*i;
373 for(int j=0; j<im->width; j++){
382 IplImage* icvRetrieveFrameCAM_CMU( CvCaptureCAM_CMU* capture )
384 if( capture->image && CMU_theCamera )
386 //capture->image->imageData = (char *) CMU_theCamera[capture->index].GetRawData(0);
387 CMU_theCamera[capture->index].getRGB((unsigned char*)capture->image->imageData,
388 capture->image->imageSize);
389 swapRedBlue( capture->image );
390 return capture->image;
396 double icvGetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id )
398 switch( property_id )
400 case CV_CAP_PROP_FRAME_WIDTH:
401 return capture->image->width;
402 case CV_CAP_PROP_FRAME_HEIGHT:
403 return capture->image->height;
404 case CV_CAP_PROP_FPS:
405 return CMU_theCamera[capture->index].GetVideoFrameRate();
406 case CV_CAP_PROP_MODE:
407 return CMU_theCamera[capture->index].GetVideoMode();
408 case CV_CAP_PROP_FORMAT:
409 return CMU_theCamera[capture->index].GetVideoFormat();
414 static int icvSetVideoSize( CvCaptureCAM_CMU* capture, int, int) {
415 if (capture==0) return 0;
416 // change to closest size
420 static int icvSetMode(CvCaptureCAM_CMU * capture, int mode){
421 int format = CMU_theCamera[capture->index].GetVideoFormat();
422 if(mode < 0 || mode > 7){
425 if(CMU_theCamera[capture->index].HasVideoMode(format, mode)){
426 CMU_theCamera[capture->index].StopImageAcquisition();
427 CMU_theCamera[capture->index].SetVideoMode(mode);
428 CMU_theCamera[capture->index].StartImageAcquisition();
434 static int icvSetFrameRate(CvCaptureCAM_CMU * capture, int rate){
435 int mode = CMU_theCamera[capture->index].GetVideoMode();
436 int format = CMU_theCamera[capture->index].GetVideoFormat();
437 if(rate < 0 || rate > 5){
440 if(CMU_theCamera[capture->index].HasVideoFrameRate(format, mode, rate)){
441 CMU_theCamera[capture->index].StopImageAcquisition();
442 CMU_theCamera[capture->index].SetVideoFrameRate(rate);
443 CMU_theCamera[capture->index].StartImageAcquisition();
449 static int icvSetFormat(CvCaptureCAM_CMU * capture, int format){
450 if(format < 0 || format > 2){
453 if(CMU_theCamera[capture->index].HasVideoFormat(format)){
454 CMU_theCamera[capture->index].StopImageAcquisition();
455 CMU_theCamera[capture->index].SetVideoFormat(format);
456 CMU_theCamera[capture->index].StartImageAcquisition();
462 int icvSetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id, double value ){
464 int ival= cvRound(value);
465 switch (property_id) {
466 case CV_CAP_PROP_FRAME_WIDTH:
467 case CV_CAP_PROP_FRAME_HEIGHT:
470 if (property_id == CV_CAP_PROP_FRAME_WIDTH) {
478 retval = icvSetVideoSize(capture, width, height);
481 case CV_CAP_PROP_FPS:
482 retval =icvSetFrameRate(capture, ival);
484 case CV_CAP_PROP_MODE:
485 retval =icvSetMode(capture, ival);
487 case CV_CAP_PROP_FORMAT:
488 retval =icvSetFormat(capture, ival);
492 // resize image if its not the right size anymore
493 CvSize size = icvGetSize_CMU( &(CMU_theCamera[capture->index]) );
494 int depth = icvGetDepth_CMU( &(CMU_theCamera[capture->index]) );
495 int nch = icvGetNChannels_CMU( &(CMU_theCamera[capture->index]) );
496 if(size.width != capture->image->width ||
497 size.height != capture->image->height ||
498 depth != capture->image->depth ||
499 nch != capture->image->nChannels )
501 cvReleaseImage(&capture->image);
502 capture->image = cvCreateImage(size, depth, nch);
507 CvCapture * cvCaptureFromCAM_CMU (int index)
509 CvCaptureCAM_CMU* capture = (CvCaptureCAM_CMU*)cvAlloc( sizeof(*capture));
510 memset( capture, 0, sizeof(*capture));
511 capture->vtable = &captureCAM_CMU_vtable;
513 if( icvOpenCAM_CMU( capture, index ))
514 return (CvCapture*)capture;
516 cvReleaseCapture( (CvCapture**)&capture );