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 *******************/
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 class CvCaptureCAM_CMU : public CvCapture
64 virtual ~CvCaptureCAM_CMU()
69 virtual bool open(int cameraId);
71 virtual double getProperty(int);
72 virtual bool setProperty(int, double);
73 virtual bool grabFrame();
74 virtual IplImage* retrieveFrame();
77 C1394Camera* camera();
82 bool setVideoSize(int, int);
83 bool setMode(int mode);
84 bool setFrameRate(int rate);
85 bool setFormat(int format);
89 int format; // 0-2, 7 ?
94 // CMU 1394 camera stuff.
95 // This firewire capability added by Philip Gruebele (pgruebele@cox.net)
96 // and modified by Roman Stanchak (rstanchak@yahoo.com).
97 // For this to work you need to install the CMU firewire DCAM drivers,
98 // located at http://www-2.cs.cmu.edu/~iwan/1394/.
99 #define CMU_MAX_CAMERAS 20
100 int CMU_numCameras = 0;
101 int CMU_numActiveCameras = 0;
102 bool CMU_useCameraFlags[CMU_MAX_CAMERAS];
103 C1394Camera *CMU_theCamera = 0;
105 // stupid defines for mode, format, FPS
106 #define CV_CAP_IEEE1394_FPS_1_875 0
107 #define CV_CAP_IEEE1394_FPS_3_75 1
108 #define CV_CAP_IEEE1394_FPS_7_5 2
109 #define CV_CAP_IEEE1394_FPS_15 3
110 #define CV_CAP_IEEE1394_FPS_30 4
111 #define CV_CAP_IEEE1394_FPS_60 5
113 // index by size, color
114 #define CV_CAP_IEEE1394_COLOR_MONO 0
115 #define CV_CAP_IEEE1394_COLOR_MONO16 1
116 #define CV_CAP_IEEE1394_COLOR_YUV444 2
117 #define CV_CAP_IEEE1394_COLOR_YUV422 3
118 #define CV_CAP_IEEE1394_COLOR_YUV411 4
119 #define CV_CAP_IEEE1394_COLOR_RGB 5
121 #define CV_CAP_IEEE1394_SIZE_160X120 0
122 #define CV_CAP_IEEE1394_SIZE_320X240 1
123 #define CV_CAP_IEEE1394_SIZE_640X480 2
124 #define CV_CAP_IEEE1394_SIZE_800X600 3
125 #define CV_CAP_IEEE1394_SIZE_1024X768 4
126 #define CV_CAP_IEEE1394_SIZE_1280X960 5
127 #define CV_CAP_IEEE1394_SIZE_1600X1200 6
129 // given color, size, output format
130 // 1 16 444 422 411 RGB
131 static char CV_CAP_IEEE1394_FORMAT[7][6] =
133 {-1, -1, 0, -1, -1, -1}, // 160x120
134 {-1, -1, -1, 0, -1, -1}, // 320x240
135 { 0, 0, -1, 0, 0, 0}, // 640x480
136 { 1, 1, -1, 1, -1, 1}, // 800x600
137 { 1, 1, -1, 1, -1, 1}, // 1024x768
138 { 2, 2, -1, 2, -1, 2}, // 1280x960
139 { 2, 2, -1, 2, -1, 2} // 1600x1200
142 // given color, size, output corresponding mode
143 static char CV_CAP_IEEE1394_MODE[7][6] =
145 {-1, -1, 0, -1, -1, -1}, // 160x120
146 {-1, -1, -1, 1, -1, -1}, // 320x240
147 { 5, 6, -1, 3, 2, 4}, // 640x480
148 { 2, 6, -1, 0, -1, 1}, // 800x600
149 { 5, 7, -1, 3, -1, 4}, // 1024x768
150 { 2, 6, -1, 0, -1, 1}, // 1280x960
151 { 5, 7, -1, 3, -1, 4} // 1600x1200
154 // given format, mode, return COLOR
155 static char CV_CAP_IEEE1394_COLOR[2][8] =
158 CV_CAP_IEEE1394_COLOR_YUV444,
159 CV_CAP_IEEE1394_COLOR_YUV422,
160 CV_CAP_IEEE1394_COLOR_YUV411,
161 CV_CAP_IEEE1394_COLOR_YUV422,
162 CV_CAP_IEEE1394_COLOR_RGB,
163 CV_CAP_IEEE1394_COLOR_MONO,
164 CV_CAP_IEEE1394_COLOR_MONO16
167 CV_CAP_IEEE1394_COLOR_YUV422,
168 CV_CAP_IEEE1394_COLOR_RGB,
169 CV_CAP_IEEE1394_COLOR_MONO,
170 CV_CAP_IEEE1394_COLOR_YUV422,
171 CV_CAP_IEEE1394_COLOR_RGB,
172 CV_CAP_IEEE1394_COLOR_MONO,
173 CV_CAP_IEEE1394_COLOR_MONO16,
174 CV_CAP_IEEE1394_COLOR_MONO16
178 // convert frame rate to suitable enum
179 /*static int icvFrameRateToIndex_CMU(double framerate){
180 if(framerate > 30) return CV_CAP_IEEE1394_FPS_60;
181 else if(framerate > 15) return CV_CAP_IEEE1394_FPS_30;
182 else if(framerate > 7.5) return CV_CAP_IEEE1394_FPS_15;
183 else if(framerate > 3.75) return CV_CAP_IEEE1394_FPS_7_5;
184 else if(framerate > 1.875) return CV_CAP_IEEE1394_FPS_3_75;
185 return CV_CAP_IEEE1394_FPS_1_875;
189 #pragma comment(lib,"1394camera.lib")
192 C1394Camera* CvCaptureCAM_CMU::camera()
194 return CMU_theCamera && index >= 0 ? &CMU_theCamera[index] : 0;
197 // return the size of the image
198 CvSize CvCaptureCAM_CMU::getSize()
200 C1394Camera* cmucam = camera();
201 unsigned long width = 0, height = 0;
202 cmucam->GetVideoFrameDimensions( &width, &height );
203 return cvSize((int)width, (int)height);
206 // return the opencv depth flag corresponding to the camera format
207 int CvCaptureCAM_CMU::getDepth()
209 C1394Camera* cmucam = camera();
210 int format = cmucam->GetVideoFormat();
211 int mode = cmucam->GetVideoMode();
218 // irrelvant to depth
222 if( CV_CAP_IEEE1394_COLOR[format][mode]==CV_CAP_IEEE1394_COLOR_MONO16 )
223 return IPL_DEPTH_16S;
228 // return the number of channels for camera
229 int CvCaptureCAM_CMU::getNChannels()
231 C1394Camera* cmucam = camera();
232 int format = cmucam->GetVideoFormat();
233 int mode = cmucam->GetVideoMode();
240 // irrelvant to nchannels
244 switch(CV_CAP_IEEE1394_COLOR[format][mode]){
245 case CV_CAP_IEEE1394_COLOR_RGB:
247 case CV_CAP_IEEE1394_COLOR_MONO:
248 case CV_CAP_IEEE1394_COLOR_MONO16:
250 case CV_CAP_IEEE1394_COLOR_YUV422:
251 case CV_CAP_IEEE1394_COLOR_YUV444:
252 case CV_CAP_IEEE1394_COLOR_YUV411:
260 bool CvCaptureCAM_CMU::open( int _index )
264 // if first time, then allocate all available cameras
265 if( CMU_numCameras == 0 )
267 CMU_numActiveCameras = 0;
268 CMU_theCamera = new C1394Camera[CMU_MAX_CAMERAS];
270 ////////////////////////////////////////////////////////////////////////////////////////////////////////
271 // create all cameras
275 if( CMU_theCamera[0].CheckLink() != CAM_SUCCESS )
278 // we have one pin per camera
279 CMU_numCameras = CMU_theCamera[0].GetNumberCameras();
281 // allocate remaining cameras
282 for(int i = 1; i < CMU_numCameras && i<CMU_MAX_CAMERAS; i++ )
284 CMU_useCameraFlags[i] = false;
285 if (CMU_theCamera[i].CheckLink() != CAM_SUCCESS)
291 // free any allocated cameras
302 // pick first unused camera
304 for(int i = 0; i < CMU_numCameras; i++ )
306 if( !CMU_useCameraFlags[i] ){
313 // no empty camera found
317 if (CMU_theCamera[_index].SelectCamera(_index) != CAM_SUCCESS)
320 if (CMU_theCamera[_index].InitCamera() != CAM_SUCCESS)
323 // set initial format -- try to pick best frame rate first, then color, then size
324 bool found_format = false;
325 for (int rate=5; rate>=0 && !found_format; rate--)
327 for (int color=CV_CAP_IEEE1394_COLOR_RGB; color>=0 && !found_format; color--)
329 for (int size=CV_CAP_IEEE1394_SIZE_1600X1200; size>=0 && !found_format; size--)
331 int format = CV_CAP_IEEE1394_FORMAT[size][color];
332 int mode = CV_CAP_IEEE1394_MODE[size][color];
333 if (format!=-1 && mode!=-1 &&
334 CMU_theCamera[_index].HasVideoFrameRate(format,mode,rate))
336 CMU_theCamera[_index].SetVideoFormat(format);
337 CMU_theCamera[_index].SetVideoMode(mode);
338 CMU_theCamera[_index].SetVideoFrameRate(rate);
339 found_format = (CMU_theCamera[_index].StartImageAcquisition() == CAM_SUCCESS);
347 CMU_theCamera[_index].SetVideoFormat(7);
348 CMU_theCamera[_index].SetVideoMode(0);
349 if(CMU_theCamera[_index].StartImageAcquisition() != CAM_SUCCESS){
357 // allocate image frame
358 image = cvCreateImage( size, 8, 3 );
361 // successfully activated camera
362 CMU_numActiveCameras++;
363 CMU_useCameraFlags[_index] = true;
373 void CvCaptureCAM_CMU::close()
375 C1394Camera* cmucam = camera();
378 cvReleaseImage( &image );
379 cmucam->StopImageAcquisition();
380 CMU_useCameraFlags[index] = false;
383 if( --CMU_numActiveCameras == 0 )
385 delete[] CMU_theCamera;
393 bool CvCaptureCAM_CMU::grabFrame()
395 C1394Camera* cmucam = camera();
396 return cmucam ? cmucam->AcquireImage() == CAM_SUCCESS : false;
399 /*static void swapRedBlue(IplImage * im)
401 uchar * ptr = (uchar *) im->imageData;
403 for(int i=0; i<im->height; i++){
404 ptr = (uchar *) im->imageData+im->widthStep*i;
405 for(int j=0; j<im->width; j++){
414 IplImage* CvCaptureCAM_CMU::retrieveFrame()
416 C1394Camera* cmucam = camera();
419 cmucam->getRGB((uchar*)image->imageData, image->imageSize);
420 cvConvertImage( image, image, CV_CVTIMG_SWAP_RB );
425 double CvCaptureCAM_CMU::getProperty( int property_id )
427 C1394Camera* cmucam = camera();
430 switch( property_id )
432 case CV_CAP_PROP_FRAME_WIDTH:
434 case CV_CAP_PROP_FRAME_HEIGHT:
435 return image->height;
436 case CV_CAP_PROP_FPS:
437 return cmucam->GetVideoFrameRate();
438 case CV_CAP_PROP_MODE:
439 return cmucam->GetVideoMode();
440 case CV_CAP_PROP_FORMAT:
441 return cmucam->GetVideoFormat();
446 bool CvCaptureCAM_CMU::setVideoSize(int, int)
451 bool CvCaptureCAM_CMU::setMode(int mode)
454 C1394Camera* cmucam = camera();
457 format = cmucam->GetVideoFormat();
458 if( mode < 0 || mode > 7 || !cmucam->HasVideoMode(format, mode))
460 cmucam->StopImageAcquisition();
461 cmucam->SetVideoMode(mode);
462 cmucam->StartImageAcquisition();
466 bool CvCaptureCAM_CMU::setFrameRate(int rate)
469 C1394Camera* cmucam = camera();
472 mode = cmucam->GetVideoMode();
473 format = cmucam->GetVideoFormat();
474 if( rate < 0 || rate > 5 || !cmucam->HasVideoFrameRate(format, mode, rate) )
476 cmucam->StopImageAcquisition();
477 cmucam->SetVideoFrameRate(rate);
478 cmucam->StartImageAcquisition();
482 bool CvCaptureCAM_CMU::setFormat(int format)
484 C1394Camera* cmucam = camera();
487 if( format < 0 || format > 2 || !cmucam->HasVideoFormat(format) )
489 cmucam->StopImageAcquisition();
490 cmucam->SetVideoFormat(format);
491 cmucam->StartImageAcquisition();
495 bool CvCaptureCAM_CMU::setProperty( int property_id, double value )
498 int ival = cvRound(value);
499 C1394Camera* cmucam = camera();
503 switch (property_id) {
504 case CV_CAP_PROP_FRAME_WIDTH:
505 case CV_CAP_PROP_FRAME_HEIGHT:
508 if (property_id == CV_CAP_PROP_FRAME_WIDTH)
517 retval = setVideoSize(width, height);
520 case CV_CAP_PROP_FPS:
521 retval = setFrameRate(ival);
523 case CV_CAP_PROP_MODE:
524 retval = setMode(ival);
526 case CV_CAP_PROP_FORMAT:
527 retval = setFormat(ival);
531 // resize image if its not the right size anymore
532 CvSize size = getSize();
533 if( !image || image->width != size.width || image->height != size.height )
535 cvReleaseImage( &image );
536 image = cvCreateImage( size, 8, 3 );
541 CvCapture * cvCreateCameraCapture_CMU (int index)
543 CvCaptureCAM_CMU* capture = new CvCaptureCAM_CMU;
544 if( !capture->open(index) )