ViSP  2.6.2
vp1394CMUGrabber.cpp
1 /****************************************************************************
2  *
3  * $Id: vp1394CMUGrabber.cpp 3719 2012-05-10 05:50:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
36  *
37  * Authors:
38  * Lucas Lopes Lemos FEMTO-ST, AS2M departement, Besancon
39  * Guillaume Laurent FEMTO-ST, AS2M departement, Besancon
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 #include <visp/vpConfig.h>
45 
46 #ifdef VISP_HAVE_CMU1394
47 
48 #include <iostream>
49 
50 #include <visp/vpImageIo.h>
51 #include <visp/vpImageConvert.h>
52 #include <visp/vp1394CMUGrabber.h>
53 
58 {
59  // public members
60  init = false;
61 
62  // protected members
63  width = height = -1;
64 
65  // private members
66  camera = new C1394Camera;
67  index = 0; // If a camera was not selected the first one (index = 0) will be used
68  _format = _mode = _fps = -1;
69  _modeauto=true;
70 
71 }
72 
77 {
78  close();
79  // delete camera instance
80  delete camera;
81 
82 }
83 
88 void
90 {
91  int camerror;
92 
93  index = cam_id ;
94 
95  camerror = camera->SelectCamera(index);
96  if ( camerror!= CAM_SUCCESS)
97  {
98  switch (camerror)
99  {
100  case CAM_ERROR_PARAM_OUT_OF_RANGE:
101  vpERROR_TRACE("vp1394CMUGrabber error: Found no camera number %i",index);
102  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is not present") );
103  break;
104  case CAM_ERROR_BUSY:
105  vpERROR_TRACE("vp1394CMUGrabber error: The camera %i is busy",index);
106  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is in use by other application") );
107  break;
108  case CAM_ERROR:
109  vpERROR_TRACE("vp1394CMUGrabber error: General I/O error when selecting camera number %i",index);
110  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"Resolve camera can not be used") );
111  break;
112  }
113  close();
114  }
115 } // end camera select
116 
120 void
121 vp1394CMUGrabber::initCamera()
122 {
123  int camerror;
124  unsigned long width, height;
125 
126 
127  if (camera->CheckLink() != CAM_SUCCESS)
128  {
129  vpERROR_TRACE("C1394Camera error: Found no cameras on the 1394 bus");
130  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The is no detected camera") );
131  }
132 
133  camerror = camera->InitCamera();
134  if ( camerror != CAM_SUCCESS )
135  {
136  switch (camerror)
137  {
138  case CAM_ERROR_NOT_INITIALIZED:
139  vpERROR_TRACE("vp1394CMUGrabber error: No camera selected",index);
140  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The is no selected camera") );
141  break;
142  case CAM_ERROR_BUSY:
143  vpERROR_TRACE("vp1394CMUGrabber error: The camera %i is busy",index);
144  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is in use by other application") );
145  break;
146  case CAM_ERROR:
147  vpERROR_TRACE("vp1394CMUGrabber error: General I/O error when selecting camera number %i",index);
148  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"Resolve camera can not be used") );
149  break;
150  }
151  close();
152  }
153 
154  if (camera->Has1394b())
155  camera->Set1394b(TRUE);
156 
157  // Set format and mode
158  if ((_format != -1) && (_mode != -1))
159  {
160  if (!camera->HasVideoMode(_format, _mode))
161  {
162  close();
163  vpERROR_TRACE("vp1394CMUGrabber error: The image format is not supported by the IEEE 1394 camera number %i",index);
164  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Video mode not supported") );
165  }
166 
167  if (camera->SetVideoFormat(_format) != CAM_SUCCESS)
168  {
169  close();
170  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video format of IEEE 1394 camera number %i",index);
171  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video format") );
172  }
173 
174  if (camera->SetVideoMode(_mode) != CAM_SUCCESS)
175  {
176  close();
177  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video mode of IEEE 1394 camera number %i",index);
178  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video mode") );
179  }
180  }
181  else {
182  // Get the current format and mode
183  _format = camera->GetVideoFormat();
184  _mode = camera->GetVideoMode();
185  }
186 
187  // Update the color coding
188  _color = getVideoColorCoding();
189  //std::cout << "color coding: " << _color << std::endl;
190 
191  // Set fps
192  if (_fps!=-1)
193  {
194  if (!camera->HasVideoFrameRate(_format,_mode,_fps))
195  {
196  close();
197  vpERROR_TRACE("vp1394CMUGrabber error: The frame rate is not supported by the IEEE 1394 camera number %i for the selected image format",index);
198  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"The frame rate is not supported") );
199  }
200 
201  if (camera->SetVideoFrameRate(_fps) != CAM_SUCCESS)
202  {
203  close();
204  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video frame rate of IEEE 1394 camera number %i",index);
205  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video frame rate") );
206  }
207  }
208 
209  // Set shutter and gain
210  if ( _modeauto == false )
211  {
212  unsigned short min,max;
213  C1394CameraControl *Control;
214 
215  Control = camera->GetCameraControl(FEATURE_GAIN);
216  Control->Inquire();
217  Control->GetRange(&min,&max);
218 
219  if (_gain<min)
220  {
221  _gain = min;
222  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be less than " << _gain << std::endl;
223  } else
224  if (_gain>max)
225  {
226  _gain = max;
227  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be greater than " << _gain << std::endl;
228  }
229 
230  Control->SetAutoMode(false);
231  if(Control->SetValue(_gain) != CAM_SUCCESS)
232  {
233  std::cout << "vp1394CMUGrabber warning: Can't set gain register value of IEEE 1394 camera number " << index << std::endl;
234  }
235 
236  Control = camera->GetCameraControl(FEATURE_SHUTTER);
237  Control->Inquire();
238  Control->GetRange(&min,&max);
239 
240  if (_shutter<min)
241  {
242  _shutter = min;
243  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be less than " << _shutter << std::endl;
244  }
245  else if (_shutter>max)
246  {
247  _shutter = max;
248  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be greater than " << _shutter << std::endl;
249  }
250  Control->SetAutoMode(false);
251  if(Control->SetValue(_shutter) != CAM_SUCCESS)
252  {
253  std::cout << "vp1394CMUGrabber warning: Can't set exposure time register value of IEEE 1394 camera number " << index << std::endl;
254  }
255  }
256  else
257  {
258  camera->GetCameraControl(FEATURE_SHUTTER)->SetAutoMode(true);
259  camera->GetCameraControl(FEATURE_GAIN)->SetAutoMode(true);
260  }
261 
262  // Set trigger off
263  camera->GetCameraControlTrigger()->SetOnOff(false);
264 
265 
266  camera->GetVideoFrameDimensions(&width,&height);
267  this->height = height;
268  this->width = width;
269 
270  init = true;
271 
272 } // end camera init
273 
274 
279 void
281 {
282  initCamera();
283  I.init(height,width);
284 
285  // start acquisition
286  if (camera->StartImageAcquisition() != CAM_SUCCESS)
287  {
288  close();
289  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
291  "Error while strating image acquisition") );
292  }
293 }
294 
299 void
301 {
302  initCamera();
303  I.init(this->height,this->width);
304 
305  // start acquisition
306  if (camera->StartImageAcquisition() != CAM_SUCCESS)
307  {
308  close();
309  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
311  "Error while strating image acquisition") );
312  }
313 }
314 
322 void
324 {
325  // get image data
326  unsigned long length;
327  unsigned char *rawdata = NULL ;
328  int dropped;
329  unsigned int size;
330 
331  if(init == false){
332  close();
334  "Initialization not done") );
335  }
336 
337  camera->AcquireImageEx(TRUE,&dropped);
338  rawdata = camera->GetRawData(&length);
339 
340  size = I.getWidth() * I.getHeight();
341  switch(_color) {
343  memcpy(I.bitmap, (unsigned char *) rawdata, size);
344  break;
346  vpImageConvert::MONO16ToGrey(rawdata, I.bitmap, size);
347  break;
348 
350  vpImageConvert::YUV411ToGrey(rawdata, I.bitmap, size);
351  break;
352 
354  vpImageConvert::YUV422ToGrey(rawdata, I.bitmap, size);
355  break;
356 
358  vpImageConvert::YUV444ToGrey(rawdata, I.bitmap, size);
359  break;
360 
362  vpImageConvert::RGBToGrey(rawdata, I.bitmap, size);
363  break;
364 
365  default:
366  close();
367  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
369  "Format conversion not implemented. "
370  "Acquisition failed.") );
371  break;
372  };
373 
374  //unsigned short depth = 0;
375  //camera->GetVideoDataDepth(&depth);
376  //std::cout << "depth: " << depth << " computed: " << (float)(length/(I.getHeight() * I.getWidth())) << std::endl;
377 
378 
379  //memcpy(I.bitmap,rawdata,length);
380 
381 }
382 
391 void
393 {
394  // get image data
395  unsigned long length;
396  unsigned char *rawdata = NULL;
397  int dropped;
398  unsigned int size;
399 
400  if(init == false){
401  close();
403  "Initialization not done") );
404  }
405 
406  camera->AcquireImageEx(TRUE,&dropped);
407  rawdata = camera->GetRawData(&length);
408  size = I.getWidth() * I.getHeight();
409 
410  switch (_color) {
412  vpImageConvert::GreyToRGBa(rawdata, (unsigned char *)I.bitmap, size);
413  break;
414 
416  vpImageConvert::MONO16ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
417  break;
418 
420  vpImageConvert::YUV411ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
421  break;
422 
424  vpImageConvert::YUV422ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
425  break;
426 
428  vpImageConvert::YUV444ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
429  break;
430 
432  size = length / 3;
433  vpImageConvert::RGBToRGBa(rawdata, (unsigned char *)I.bitmap, size);
434  break;
435 
436  default:
437  close();
438  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
440  "Format conversion not implemented. "
441  "Acquisition failed.") );
442  break;
443  };
444 }
445 
449 void
451 {
452  // stop acquisition
453  camera->StopImageAcquisition();
454 
455  if (camera->StopImageAcquisition() != CAM_SUCCESS)
456  {
458  "vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera") );
459  }
460 
461  init = false;
462 
463 }
464 
468 void
469 vp1394CMUGrabber::setControl(unsigned short gain, unsigned short shutter)
470 {
471  /*
472  unsigned short min,max;
473  C1394CameraControl *Control;
474 
475  if ( modeauto == false )
476  {
477  Control = camera->GetCameraControl(FEATURE_GAIN);
478  Control->Inquire();
479  Control->GetRange(&min,&max);
480 
481  if (gain<min)
482  {
483  gain = min;
484  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be less than " << gain << std::endl;
485  } else
486  if (gain>max)
487  {
488  gain = max;
489  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be greater than " << gain << std::endl;
490  }
491 
492  Control->SetAutoMode(false);
493  if(Control->SetValue(gain) != CAM_SUCCESS)
494  {
495  std::cout << "vp1394CMUGrabber warning: Can't set gain register value of IEEE 1394 camera number " << index << std::endl;
496  }
497 
498  Control = camera->GetCameraControl(FEATURE_SHUTTER);
499  Control->Inquire();
500  Control->GetRange(&min,&max);
501 
502  if (shutter<min)
503  {
504  shutter = min;
505  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be less than " << shutter << std::endl;
506  }
507  else if (shutter>max)
508  {
509  shutter = max;
510  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be greater than " << shutter << std::endl;
511  }
512  Control->SetAutoMode(false);
513  if(Control->SetValue(shutter) != CAM_SUCCESS)
514  {
515  std::cout << "vp1394CMUGrabber warning: Can't set exposure time register value of IEEE 1394 camera number " << index << std::endl;
516  }
517  }
518  else
519  {
520  camera->GetCameraControl(FEATURE_SHUTTER)->SetAutoMode(true);
521  camera->GetCameraControl(FEATURE_GAIN)->SetAutoMode(true);
522  } */
523  _shutter=shutter;
524  _gain=gain;
525 }
526 
530 int
532 {
533  int n_cam = camera->RefreshCameraList();
534 
535  /*if( n_cam >= 0 )
536  {
537  std::cout << "vp1394CMUGrabber: Found " << n_cam << " Camera";
538 
539  if( n_cam > 1 ) std::cout << "s." << std::endl;
540  else std::cout << "." << std::endl;
541  }
542  else {
543  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"vp1394CMUGrabber: Found no cameras on the 1394 bus(es)"));
544  }*/
545 
546  return n_cam;
547 }
548 
552 void
554 {
555  char buf[512];
556 
557 
558  if( camera->GetNumberCameras() > cam_id )
559  {
560  camera->GetNodeDescription(cam_id,buf,512);
561  std::cout << "Camera " << cam_id << ": " << buf << std::endl ;
562 
563  }
564  else {
565  std::cout << "Camera " << cam_id << ": camera not found" << std::endl ;
566  }
567 }
568 
569 
573 void
575 {
576  char vendor[256] , model[256] , buf[256];
577  LARGE_INTEGER ID;
578 
579  camera->GetCameraName(model,sizeof(model));
580  camera->GetCameraVendor(vendor,sizeof(vendor));
581  camera->GetCameraUniqueID(&ID);
582 
583  std::cout << "Vendor: " << vendor << std::endl;
584  std::cout << "Model: " << model << std::endl;
585 
586  sprintf(buf,"%08X%08X",ID.HighPart,ID.LowPart);
587  std::cout << "UniqueID: " << buf << std::endl;
588 
589 }
590 
591 
630 void
631 vp1394CMUGrabber::setVideoMode( unsigned long format, unsigned long mode )
632 {
633  _format = format ;
634  _mode = mode ;
635 }
636 
657 void
659 {
660  _fps = fps;
661 }
662 
663 #endif
664 
int getNumberOfConnectedCameras() const
void setVideoMode(unsigned long format, unsigned long mode)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
unsigned int getWidth() const
Definition: vpImage.h:154
void init(unsigned int height, unsigned int width)
set the size of the image
Definition: vpImage.h:372
#define vpERROR_TRACE
Definition: vpDebug.h:379
void open(vpImage< unsigned char > &I)
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
Type * bitmap
points toward the bitmap
Definition: vpImage.h:115
static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size)
vpColorCodingType getVideoColorCoding() const
Get the video color coding format.
static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int size)
static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
Error that can be emited by the vpFrameGrabber class and its derivates.
void displayCameraDescription(int cam_id)
static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
virtual ~vp1394CMUGrabber()
void setFramerate(unsigned long fps)
void acquire(vpImage< unsigned char > &I)
unsigned int height
Number of rows in the image.
static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba, unsigned int size)
bool init
Set to true if the frame grabber has been initialized.
static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
unsigned int getHeight() const
Definition: vpImage.h:145
static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
unsigned int width
Number of columns in the image.
void setControl(unsigned short gain, unsigned short shutter)
void selectCamera(int cam_id)