Visual Servoing Platform  version 3.0.0
vp1394CMUGrabber.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
32  *
33  * Authors:
34  * Lucas Lopes Lemos FEMTO-ST, AS2M departement, Besancon
35  * Guillaume Laurent FEMTO-ST, AS2M departement, Besancon
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #ifdef VISP_HAVE_CMU1394
43 
44 #include <iostream>
45 
46 #include <visp3/core/vpImageConvert.h>
47 #include <visp3/sensor/vp1394CMUGrabber.h>
48 
53 {
54  // public members
55  init = false;
56 
57  // protected members
58  width = height = -1;
59 
60  // private members
61  camera = new C1394Camera;
62  index = 0; // If a camera was not selected the first one (index = 0) will be used
63  _format = _mode = _fps = -1;
64  _modeauto=true;
65 }
66 
71 {
72  close();
73  // delete camera instance
74  if (camera) {
75  delete camera;
76  camera = NULL;
77  }
78 }
79 
84 void
86 {
87  int camerror;
88 
89  index = cam_id ;
90 
91  camerror = camera->SelectCamera(index);
92  if ( camerror!= CAM_SUCCESS)
93  {
94  switch (camerror)
95  {
96  case CAM_ERROR_PARAM_OUT_OF_RANGE:
97  vpERROR_TRACE("vp1394CMUGrabber error: Found no camera number %i",index);
98  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is not present") );
99  break;
100  case CAM_ERROR_BUSY:
101  vpERROR_TRACE("vp1394CMUGrabber error: The camera %i is busy",index);
102  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is in use by other application") );
103  break;
104  case CAM_ERROR:
105  vpERROR_TRACE("vp1394CMUGrabber error: General I/O error when selecting camera number %i",index);
106  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"Resolve camera can not be used") );
107  break;
108  }
109  close();
110  }
111 } // end camera select
112 
116 void
117 vp1394CMUGrabber::initCamera()
118 {
119  if (init == false)
120  {
121  int camerror;
122 
123  if (camera->CheckLink() != CAM_SUCCESS)
124  {
125  vpERROR_TRACE("C1394Camera error: Found no cameras on the 1394 bus");
126  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The is no detected camera") );
127  }
128 
129  camerror = camera->InitCamera();
130  if ( camerror != CAM_SUCCESS )
131  {
132  switch (camerror)
133  {
134  case CAM_ERROR_NOT_INITIALIZED:
135  vpERROR_TRACE("vp1394CMUGrabber error: No camera selected",index);
136  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The is no selected camera") );
137  break;
138  case CAM_ERROR_BUSY:
139  vpERROR_TRACE("vp1394CMUGrabber error: The camera %i is busy",index);
140  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"The required camera is in use by other application") );
141  break;
142  case CAM_ERROR:
143  vpERROR_TRACE("vp1394CMUGrabber error: General I/O error when selecting camera number %i",index);
144  throw (vpFrameGrabberException(vpFrameGrabberException::initializationError,"Resolve camera can not be used") );
145  break;
146  }
147  close();
148  }
149 
150  if (camera->Has1394b())
151  camera->Set1394b(TRUE);
152 
153  // Get the current settings
154  _format = camera->GetVideoFormat();
155  _mode = camera->GetVideoMode();
156  _color = getVideoColorCoding();
157  //std::cout << "format: " << _format << std::endl;
158  //std::cout << "mode: " << _mode << std::endl;
159  //std::cout << "color coding: " << _color << std::endl;
160 
161  // Set trigger off
162  camera->GetCameraControlTrigger()->SetOnOff(false);
163 
164  unsigned long w, h;
165  camera->GetVideoFrameDimensions(&w, &h);
166  this->width = w;
167  this->height = h;
168 
169  // start acquisition
170  if (camera->StartImageAcquisition() != CAM_SUCCESS)
171  {
172  close();
173  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
175  "Error while starting image acquisition") );
176  }
177 
178  init = true;
179  }
180 
181 } // end camera init
182 
183 
188 void
190 {
191  initCamera();
192  I.resize(this->height, this->width);
193 }
194 
199 void
201 {
202  initCamera();
203  I.resize(this->height, this->width);
204 }
205 
213 void
215 {
216  // get image data
217  unsigned long length;
218  unsigned char *rawdata = NULL ;
219  int dropped;
220  unsigned int size;
221 
222  open(I);
223 
224  camera->AcquireImageEx(TRUE,&dropped);
225  rawdata = camera->GetRawData(&length);
226 
227  size = I.getSize();
228  switch(_color) {
230  memcpy(I.bitmap, (unsigned char *) rawdata, size);
231  break;
233  vpImageConvert::MONO16ToGrey(rawdata, I.bitmap, size);
234  break;
235 
237  vpImageConvert::YUV411ToGrey(rawdata, I.bitmap, size);
238  break;
239 
241  vpImageConvert::YUV422ToGrey(rawdata, I.bitmap, size);
242  break;
243 
245  vpImageConvert::YUV444ToGrey(rawdata, I.bitmap, size);
246  break;
247 
249  vpImageConvert::RGBToGrey(rawdata, I.bitmap, size);
250  break;
251 
252  default:
253  close();
254  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
256  "Format conversion not implemented. "
257  "Acquisition failed.") );
258  break;
259  };
260 
261  //unsigned short depth = 0;
262  //camera->GetVideoDataDepth(&depth);
263  //std::cout << "depth: " << depth << " computed: " << (float)(length/(I.getHeight() * I.getWidth())) << std::endl;
264 
265 
266  //memcpy(I.bitmap,rawdata,length);
267 
268 }
269 
278 void
280 {
281  // get image data
282  unsigned long length;
283  unsigned char *rawdata = NULL;
284  int dropped;
285  unsigned int size;
286 
287  open(I);
288 
289  camera->AcquireImageEx(TRUE,&dropped);
290  rawdata = camera->GetRawData(&length);
291  size = I.getWidth() * I.getHeight();
292 
293  switch (_color) {
295  vpImageConvert::GreyToRGBa(rawdata, (unsigned char *)I.bitmap, size);
296  break;
297 
299  vpImageConvert::MONO16ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
300  break;
301 
303  vpImageConvert::YUV411ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
304  break;
305 
307  vpImageConvert::YUV422ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
308  break;
309 
311  vpImageConvert::YUV444ToRGBa(rawdata, (unsigned char *)I.bitmap, size);
312  break;
313 
315  size = length / 3;
316  vpImageConvert::RGBToRGBa(rawdata, (unsigned char *)I.bitmap, size);
317  break;
318 
319  default:
320  close();
321  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
323  "Format conversion not implemented. "
324  "Acquisition failed.") );
325  break;
326  };
327 }
328 
332 void
334 {
335  // stop acquisition
336  if (camera->IsAcquiring()) {
337  // stop acquisition
338  if (camera->StopImageAcquisition() != CAM_SUCCESS)
339  {
340  close();
341  vpERROR_TRACE("vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera number %i",index);
343  "Error while stopping image acquisition") );
344  }
345  }
346 
347  init = false;
348 }
349 
354 void
355 vp1394CMUGrabber::setControl(unsigned short gain, unsigned short shutter)
356 {
357  setShutter(shutter);
358  setGain(gain);
359 }
360 
364 int
366 {
367  int n_cam = camera->RefreshCameraList();
368 
369  return n_cam;
370 }
371 
377 void vp1394CMUGrabber::getGainMinMax(unsigned short &min, unsigned short &max)
378 {
379  initCamera();
380 
381  C1394CameraControl *Control;
382  Control = camera->GetCameraControl(FEATURE_GAIN);
383  Control->Inquire();
384  Control->GetRange(&min, &max);
385 }
392 {
393  initCamera();
394  camera->GetCameraControl(FEATURE_GAIN)->SetAutoMode(true);
395 }
401 void vp1394CMUGrabber::setGain(unsigned short gain)
402 {
403  initCamera();
404  _gain = gain;
405 
406  unsigned short min,max;
407  C1394CameraControl *Control;
408 
409  Control = camera->GetCameraControl(FEATURE_GAIN);
410  Control->Inquire();
411  Control->GetRange(&min,&max);
412 
413  if (_gain < min)
414  {
415  _gain = min;
416  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be less than " << _gain << std::endl;
417  }
418  else if (_gain > max)
419  {
420  _gain = max;
421  std::cout << "vp1394CMUGrabber warning: Desired gain register value of IEEE 1394 camera number " << index << " can't be greater than " << _gain << std::endl;
422  }
423 
424  Control->SetAutoMode(false);
425  if(Control->SetValue(_gain) != CAM_SUCCESS)
426  {
427  std::cout << "vp1394CMUGrabber warning: Can't set gain register value of IEEE 1394 camera number " << index << std::endl;
428  }
429 }
430 
436 void vp1394CMUGrabber::getShutterMinMax(unsigned short &min, unsigned short &max)
437 {
438  initCamera();
439 
440  C1394CameraControl *Control;
441  Control = camera->GetCameraControl(FEATURE_SHUTTER);
442  Control->Inquire();
443  Control->GetRange(&min, &max);
444 }
445 
452 {
453  initCamera();
454  camera->GetCameraControl(FEATURE_SHUTTER)->SetAutoMode(true);
455 }
461 void vp1394CMUGrabber::setShutter(unsigned short shutter)
462 {
463  initCamera();
464 
465  _shutter = shutter;
466 
467  unsigned short min,max;
468  C1394CameraControl *Control;
469 
470  Control = camera->GetCameraControl(FEATURE_SHUTTER);
471  Control->Inquire();
472  Control->GetRange(&min,&max);
473 
474  if (_shutter < min)
475  {
476  _shutter = min;
477  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be less than " << _shutter << std::endl;
478  }
479  else if (_shutter > max)
480  {
481  _shutter = max;
482  std::cout << "vp1394CMUGrabber warning: Desired exposure time register value of IEEE 1394 camera number " << index << " can't be greater than " << _shutter << std::endl;
483  }
484  Control->SetAutoMode(false);
485  if(Control->SetValue(_shutter) != CAM_SUCCESS)
486  {
487  std::cout << "vp1394CMUGrabber warning: Can't set exposure time register value of IEEE 1394 camera number " << index << std::endl;
488  }
489 }
490 
494 void
496 {
497  char buf[512];
498 
499  if( camera->GetNumberCameras() > cam_id )
500  {
501  camera->GetNodeDescription(cam_id,buf,512);
502  std::cout << "Camera " << cam_id << ": " << buf << std::endl ;
503 
504  }
505  else {
506  std::cout << "Camera " << cam_id << ": camera not found" << std::endl ;
507  }
508 }
509 
510 
514 void
516 {
517  char vendor[256] , model[256] , buf[256];
518  LARGE_INTEGER ID;
519 
520  camera->GetCameraName(model,sizeof(model));
521  camera->GetCameraVendor(vendor,sizeof(vendor));
522  camera->GetCameraUniqueID(&ID);
523 
524  std::cout << "Vendor: " << vendor << std::endl;
525  std::cout << "Model: " << model << std::endl;
526 
527  sprintf(buf,"%08X%08X",ID.HighPart,ID.LowPart);
528  std::cout << "UniqueID: " << buf << std::endl;
529 
530 }
531 
532 
571 void
572 vp1394CMUGrabber::setVideoMode( unsigned long format, unsigned long mode )
573 {
574  initCamera();
575 
576  _format = format ;
577  _mode = mode ;
578 
579  // Set format and mode
580  if ((_format != -1) && (_mode != -1))
581  {
582  if (!camera->HasVideoMode(_format, _mode))
583  {
584  close();
585  vpERROR_TRACE("vp1394CMUGrabber error: The image format is not supported by the IEEE 1394 camera number %i",index);
586  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Video mode not supported") );
587  }
588 
589  if (camera->IsAcquiring()) {
590  // stop acquisition
591  if (camera->StopImageAcquisition() != CAM_SUCCESS)
592  {
593  close();
594  vpERROR_TRACE("vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera number %i",index);
596  "Error while stopping image acquisition") );
597  }
598  }
599 
600  if (camera->SetVideoFormat(_format) != CAM_SUCCESS)
601  {
602  close();
603  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video format of IEEE 1394 camera number %i",index);
604  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video format") );
605  }
606 
607  if (camera->SetVideoMode(_mode) != CAM_SUCCESS)
608  {
609  close();
610  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video mode of IEEE 1394 camera number %i",index);
611  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video mode") );
612  }
613 
614  // start acquisition
615  if (camera->StartImageAcquisition() != CAM_SUCCESS)
616  {
617  close();
618  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
620  "Error while starting image acquisition") );
621  }
622 
623  // Update Image dimension
624  unsigned long w, h;
625  camera->GetVideoFrameDimensions(&w, &h);
626  this->width = w;
627  this->height = h;
628 
629  // Update the color coding
630  _color = getVideoColorCoding();
631  }
632 }
633 
655 void
657 {
658  initCamera();
659 
660  _fps = fps;
661 
662  // Set fps
663  if (_fps!=-1)
664  {
665  if (!camera->HasVideoFrameRate(_format,_mode,_fps))
666  {
667  close();
668  vpERROR_TRACE("vp1394CMUGrabber error: The frame rate is not supported by the IEEE 1394 camera number %i for the selected image format",index);
669  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"The frame rate is not supported") );
670  }
671 
672  if (camera->IsAcquiring()) {
673  // stop acquisition
674  if (camera->StopImageAcquisition() != CAM_SUCCESS)
675  {
676  close();
677  vpERROR_TRACE("vp1394CMUGrabber error: Can't stop image acquisition from IEEE 1394 camera number %i",index);
679  "Error while stopping image acquisition") );
680  }
681  }
682  if (camera->SetVideoFrameRate(_fps) != CAM_SUCCESS)
683  {
684  close();
685  vpERROR_TRACE("vp1394CMUGrabber error: Can't set video frame rate of IEEE 1394 camera number %i",index);
686  throw (vpFrameGrabberException(vpFrameGrabberException::settingError,"Can't set video frame rate") );
687  }
688  // start acquisition
689  if (camera->StartImageAcquisition() != CAM_SUCCESS)
690  {
691  close();
692  vpERROR_TRACE("vp1394CMUGrabber error: Can't start image acquisition from IEEE 1394 camera number %i",index);
694  "Error while starting image acquisition") );
695  }
696 
697  }
698 }
720 int
722 {
723  initCamera();
724  int fps = camera->GetVideoFrameRate();
725  return fps;
726 }
727 
728 #elif !defined(VISP_BUILD_SHARED_LIBS)
729 // Work arround to avoid warning: libvisp_sensor.a(vp1394CMUGrabber.cpp.o) has no symbols
730 void dummy_vp1394CMUGrabber() {};
731 #endif
732 
int getNumberOfConnectedCameras() const
void setVideoMode(unsigned long format, unsigned long mode)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
void setGain(unsigned short gain)
unsigned int getWidth() const
Definition: vpImage.h:161
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:116
static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size)
vpColorCodingType getVideoColorCoding() const
Get the video color coding format.
#define vpERROR_TRACE
Definition: vpDebug.h:391
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 setShutter(unsigned short shutter)
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 getGainMinMax(unsigned short &min, unsigned short &max)
void setFramerate(unsigned long fps)
unsigned int getSize() const
Definition: vpImage.h:189
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)
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
Definition: vpImage.h:616
bool init
Set to true if the frame grabber has been initialized.
static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
void getShutterMinMax(unsigned short &min, unsigned short &max)
static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
unsigned int getHeight() const
Definition: vpImage.h:152
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)