ViSP  2.7.0
vp1394TwoGrabber.cpp
1 /****************************************************************************
2  *
3  * $Id: vp1394TwoGrabber.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 
48 #include <iostream>
49 
50 #include <visp/vpConfig.h>
51 
52 /*
53  * Interface with libdc1394 2.x
54  */
55 #if defined(VISP_HAVE_DC1394_2)
56 #include <unistd.h>
57 
58 #include <visp/vp1394TwoGrabber.h>
59 #include <visp/vpFrameGrabberException.h>
60 #include <visp/vpImageIo.h>
61 #include <visp/vpImageConvert.h>
62 #include <visp/vpTime.h>
63 
64 const char * vp1394TwoGrabber::strVideoMode[DC1394_VIDEO_MODE_NUM]= {
65  "MODE_160x120_YUV444",
66  "MODE_320x240_YUV422",
67  "MODE_640x480_YUV411",
68  "MODE_640x480_YUV422",
69  "MODE_640x480_RGB8",
70  "MODE_640x480_MONO8",
71  "MODE_640x480_MONO16",
72  "MODE_800x600_YUV422",
73  "MODE_800x600_RGB8",
74  "MODE_800x600_MONO8",
75  "MODE_1024x768_YUV422",
76  "MODE_1024x768_RGB8",
77  "MODE_1024x768_MONO8",
78  "MODE_800x600_MONO16",
79  "MODE_1024x768_MONO16",
80  "MODE_1280x960_YUV422",
81  "MODE_1280x960_RGB8",
82  "MODE_1280x960_MONO8",
83  "MODE_1600x1200_YUV422",
84  "MODE_1600x1200_RGB8",
85  "MODE_1600x1200_MONO8",
86  "MODE_1280x960_MONO16",
87  "MODE_1600x1200_MONO16",
88  "MODE_EXIF",
89  "MODE_FORMAT7_0",
90  "MODE_FORMAT7_1",
91  "MODE_FORMAT7_2",
92  "MODE_FORMAT7_3",
93  "MODE_FORMAT7_4",
94  "MODE_FORMAT7_5",
95  "MODE_FORMAT7_6",
96  "MODE_FORMAT7_7"
97 };
98 
99 const char * vp1394TwoGrabber::strFramerate[DC1394_FRAMERATE_NUM]= {
100  "FRAMERATE_1_875",
101  "FRAMERATE_3_75",
102  "FRAMERATE_7_5",
103  "FRAMERATE_15",
104  "FRAMERATE_30",
105  "FRAMERATE_60",
106  "FRAMERATE_120",
107  "FRAMERATE_240"
108 };
109 
110 const char * vp1394TwoGrabber::strColorCoding[DC1394_COLOR_CODING_NUM]= {
111  "COLOR_CODING_MONO8",
112  "COLOR_CODING_YUV411",
113  "COLOR_CODING_YUV422",
114  "COLOR_CODING_YUV444",
115  "COLOR_CODING_RGB8",
116  "COLOR_CODING_MONO16",
117  "COLOR_CODING_RGB16",
118  "COLOR_CODING_MONO16S",
119  "COLOR_CODING_RGB16S",
120  "COLOR_CODING_RAW8",
121  "COLOR_CODING_RAW16",
122 };
123 
124 
125 
171 {
172  // protected members
173  width = height = 0;
174 
175  // private members
176  num_cameras = 0;
177  cameras = NULL;
178  camera_id = 0;
179  verbose = false;//true;
180  camIsOpen = NULL;
181  init = false;
182  cameras = NULL;
183 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
184  d = NULL;
185  list = NULL;
186 #endif
187  num_buffers = 4; // ring buffer size
188 
189  isDataModified = NULL;
190  initialShutterMode = NULL;
191  dataCam = NULL;
192 
193  reset = false;
194  initialize(reset);
195 
196  // open();
197 }
198 
209 {
210 /* if(num_cameras >= 1){
211  delete[] isDataModified;
212  delete[] initialShutterMode;
213  delete[] dataCam;
214  }*/
215  close();
216 }
217 
339 void
340 vp1394TwoGrabber::setCamera(uint64_t camera_id)
341 {
342  // Suppose that if camera_id is a camera GUID, this value is greater
343  // than the number of cameras connected to the bus
344  if (camera_id >= num_cameras) {
345  // Check if camera_id is a camera guid
346  bool is_guid = false;
347  // check if the camera_id is a guid
348  for (unsigned int i=0; i< num_cameras; i++) {
349  if (cameras[i]->guid == camera_id) {
350  this->camera_id = i;
351  is_guid = true;
352  break;
353  }
354  }
355  if (is_guid == false) {
356  close();
357  vpERROR_TRACE("The camera with GUID 0x%x or id %u is not present",
358  camera_id, camera_id);
359  vpERROR_TRACE("Only %u camera on the bus.", num_cameras);
361  "The required camera is not present") );
362  }
363  }
364  else {
365  this->camera_id = camera_id;
366  }
367 
368  // create a pointer to the working camera
369  camera = cameras[this->camera_id];
370 }
371 
372 
387 void
388 vp1394TwoGrabber::getCamera(uint64_t &camera_id)
389 {
390  if (num_cameras) {
391  camera_id = this->camera_id;
392  }
393  else {
394  close();
395  vpERROR_TRACE("No cameras found");
397  "No cameras found") );
398  }
399 }
400 
415 uint64_t
417 {
418  if (num_cameras) {
419  return this->camera_id;
420  }
421  else {
422  close();
423  vpERROR_TRACE("No cameras found");
425  "No cameras found") );
426  }
427 }
428 
436 void
437 vp1394TwoGrabber::getNumCameras(unsigned int &ncameras)
438 {
439  if (! num_cameras) {
440  vpCTRACE << "No camera found..."<< std::endl;
441  ncameras = 0;
442  }
443 
444  ncameras = num_cameras;
445 }
446 
454 unsigned int
456 {
457  unsigned int ncameras = 0;
458  if (! num_cameras) {
459  vpCTRACE << "No camera found..."<< std::endl;
460  ncameras = 0;
461  }
462 
463  ncameras = num_cameras;
464  return ncameras;
465 }
466 
512 void
514 {
515  open();
516  if (! num_cameras) {
517  close();
518  vpERROR_TRACE("No camera found");
520  "No camera found") );
521  }
522  if (!isVideoModeSupported(videomode)){
523  vpERROR_TRACE("Video mode not supported by camera %d",camera_id);
525  "Video mode not supported") );
526  return ;
527  }
528  // Stop dma capture if started
529  setTransmission(DC1394_OFF);
530  setCapture(DC1394_OFF);
531 
532  if (dc1394_video_set_mode(camera, (dc1394video_mode_t) videomode) != DC1394_SUCCESS) {
533 
534  close();
535  vpERROR_TRACE("Can't set video mode");
537  "Can't set video mode") );
538  }
539 
540  setCapture(DC1394_ON);
541  setTransmission(DC1394_ON);
542 
543  // Updates image size from new video mode
544  if (dc1394_get_image_size_from_video_mode(camera,
545  (dc1394video_mode_t) videomode,
546  &this->width, &this->height)
547  != DC1394_SUCCESS) {
548 
549  close();
550  vpERROR_TRACE("Can't set video mode");
552  "Can't get image size") );
553  }
554 
555 }
556 
573 void
575 {
576  if (! num_cameras) {
577  close();
578  vpERROR_TRACE("No camera found");
580  "No camera found") );
581  }
582 
583  dc1394video_mode_t _videomode;
584  if (dc1394_video_get_mode(camera, &_videomode) != DC1394_SUCCESS) {
585 
586  close();
587  vpERROR_TRACE("Can't get current video mode");
589  "Can't get current video mode") );
590  }
591  videomode = (vp1394TwoVideoModeType) _videomode;
592 
593 }
594 
595 
596 
614 uint32_t
615 vp1394TwoGrabber::getVideoModeSupported(std::list<vp1394TwoVideoModeType> & videomodes)
616 {
617  // Refresh the list of supported modes
618  videomodes.clear();
619 
620  if (! num_cameras) {
621  close();
622  vpERROR_TRACE("No camera found");
624  "No camera found") );
625  }
626  dc1394video_modes_t _videomodes;
627 
628  // get video modes:
629  if (dc1394_video_get_supported_modes(camera, &_videomodes)!=DC1394_SUCCESS) {
630 
631  close();
632  vpERROR_TRACE("Can't get video modes");
634  "Can't get video modes") );
635  }
636 
637  // parse the video modes to add in the list
638  for (unsigned i=0; i < _videomodes.num; i++) {
639  vp1394TwoVideoModeType _mode = (vp1394TwoVideoModeType) _videomodes.modes[i];
640  videomodes.push_back( _mode );
641  }
642 
643  // return the number of available video modes
644  return _videomodes.num;
645 }
661 bool
663 {
664  if (! num_cameras) {
665  close();
666  vpERROR_TRACE("No camera found");
668  "No camera found") );
669  }
670  dc1394video_modes_t _videomodes;
671 
672  // get video modes:
673  if (dc1394_video_get_supported_modes(camera, &_videomodes)!=DC1394_SUCCESS) {
674 
675  close();
676  vpERROR_TRACE("Can't get video modes");
678  "Can't get video modes") );
679  }
680 
681  // parse the video modes to check with the desired
682  for (unsigned i=0; i < _videomodes.num; i++) {
683  if ((vp1394TwoVideoModeType) _videomodes.modes[i] == videomode)
684  return true;
685  }
686  return false;
687 }
688 
701 bool
703 {
704 
705  if (dc1394_is_video_mode_scalable((dc1394video_mode_t) videomode))
706  return true;
707 
708  return false;
709 }
710 
726 bool
728 {
730  getColorCoding(coding);
731 
732  switch (coding) {
736  case vpCOLOR_CODING_RAW8:
738  return false;
742  case vpCOLOR_CODING_RGB8:
745  return true;
746  }
747  return false;
748 }
749 
774 void
776 {
777  open();
778  if (! num_cameras) {
779  close();
780  vpERROR_TRACE("No camera found");
782  "No camera found") );
783  }
784 
785  vp1394TwoVideoModeType cur_videomode;
786  getVideoMode(cur_videomode);
787  if (isVideoModeFormat7(cur_videomode))
788  return;
789 
790  if (!isFramerateSupported(cur_videomode,fps)){
791  vpERROR_TRACE("Framerate not supported by camera %d",camera_id);
793  "Framerate not supported") );
794  return ;
795  }
796 
797  // Stop dma capture if started
798  setTransmission(DC1394_OFF);
799  setCapture(DC1394_OFF);
800 
801  if (dc1394_video_set_framerate(camera, (dc1394framerate_t) fps) != DC1394_SUCCESS) {
802 
803  close();
804  vpERROR_TRACE("Can't set framerate");
806  "Can't set framerate") );
807  }
808 
809  setCapture(DC1394_ON);
810  setTransmission(DC1394_ON);
811 }
812 
829 void
831 {
832  if (! num_cameras) {
833  close();
834  vpERROR_TRACE("No camera found");
836  "No camera found") );
837  }
838  dc1394framerate_t _fps;
839  if (dc1394_video_get_framerate(camera, &_fps) != DC1394_SUCCESS) {
840 
841  close();
842  vpERROR_TRACE("Can't get current framerate");
844  "Can't get current framerate") );
845  }
846  fps = (vp1394TwoFramerateType) _fps;
847 
848 }
849 
881 uint32_t
883  std::list<vp1394TwoFramerateType> & fps)
884 {
885  if (! num_cameras) {
886  close();
887  vpERROR_TRACE("No camera found");
889  "No camera found") );
890  }
891 
892  // Refresh the list of supported framerates
893  fps.clear();
894 
895  switch (mode) {
896  // Framerate not available for:
897  // - vpVIDEO_MODE_EXIF ie Format_6
898  // - vpVIDEO_MODE_FORMAT7... ie the Format_7
899  case vpVIDEO_MODE_EXIF:
908  return 0;
909  break;
910  default:
911  {
912  dc1394framerates_t _fps;
913  if (dc1394_video_get_supported_framerates(camera,
914  (dc1394video_mode_t)mode,
915  &_fps) != DC1394_SUCCESS) {
916  close();
917  vpERROR_TRACE("Could not query supported frametates for mode %d\n",
918  mode);
920  "Could not query supported framerates") );
921  }
922  if (_fps.num == 0)
923  return 0;
924 
925  for (unsigned int i = 0; i < _fps.num; i ++)
926  fps.push_back((vp1394TwoFramerateType)_fps.framerates[i]);
927 
928  return _fps.num;
929  }
930  break;
931  }
932 }
966 bool
969 {
970  if (! num_cameras) {
971  close();
972  vpERROR_TRACE("No camera found");
974  "No camera found") );
975  }
976 
977  switch (mode) {
978  // Framerate not available for:
979  // - vpVIDEO_MODE_EXIF ie Format_6
980  // - vpVIDEO_MODE_FORMAT7... ie the Format_7
981  case vpVIDEO_MODE_EXIF:
990  return 0;
991  break;
992  default:
993  {
994  dc1394framerates_t _fps;
995  if (dc1394_video_get_supported_framerates(camera,
996  (dc1394video_mode_t)mode,
997  &_fps) != DC1394_SUCCESS) {
998  close();
999  vpERROR_TRACE("Could not query supported frametates for mode %d\n",
1000  mode);
1002  "Could not query supported framerates") );
1003  }
1004  if (_fps.num == 0)
1005  return 0;
1006 
1007  for (unsigned int i = 0; i < _fps.num; i ++){
1008  if (fps==(vp1394TwoFramerateType)_fps.framerates[i]){
1009  return true;
1010  }
1011  }
1012  return false;
1013  }
1014  break;
1015  }
1016 }
1017 
1066 void
1068 {
1069  if (! num_cameras) {
1070  close();
1071  vpERROR_TRACE("No camera found");
1073  "No camera found") );
1074  }
1075 
1076  dc1394video_mode_t _videomode;
1077  if (dc1394_video_get_mode(camera, &_videomode) != DC1394_SUCCESS) {
1078 
1079  close();
1080  vpERROR_TRACE("Can't get current video mode");
1082  "Can't get current video mode") );
1083  }
1084 
1085  if (!isColorCodingSupported((vp1394TwoVideoModeType)_videomode,coding)){
1086  vpERROR_TRACE("Color coding not supported by camera %d",camera_id);
1088  "Color coding not supported") );
1089  return ;
1090  }
1091 
1092  // Format 7 video mode
1093  if (dc1394_is_video_mode_scalable(_videomode)) {
1094  setTransmission(DC1394_OFF);
1095  setCapture(DC1394_OFF);
1096 
1097  if (dc1394_format7_set_color_coding(camera, _videomode,
1098  (dc1394color_coding_t) coding)
1099  != DC1394_SUCCESS) {
1100 
1101  close();
1102  vpERROR_TRACE("Can't set color coding");
1104  "Can't set color coding") );
1105  }
1106 
1107  setCapture(DC1394_ON);
1108  setTransmission(DC1394_ON);
1109  }
1110 }
1111 
1129 void
1131 {
1132  if (! num_cameras) {
1133  close();
1134  vpERROR_TRACE("No camera found");
1136  "No camera found") );
1137  }
1138  dc1394video_mode_t _videomode;
1139  if (dc1394_video_get_mode(camera, &_videomode) != DC1394_SUCCESS) {
1140 
1141  close();
1142  vpERROR_TRACE("Can't get current video mode");
1144  "Can't get current video mode") );
1145  }
1146 
1147  dc1394color_coding_t _coding;
1148  if (dc1394_is_video_mode_scalable(_videomode)) {
1149  // Format 7 video mode
1150  if (dc1394_format7_get_color_coding(camera, _videomode, &_coding)
1151  != DC1394_SUCCESS) {
1152 
1153  close();
1154  vpERROR_TRACE("Can't get current color coding");
1156  "Can't query current color coding") );
1157  }
1158  }
1159  else if (dc1394_is_video_mode_still_image((dc1394video_mode_t)_videomode)) {
1161  "No color coding for format 6 video mode"));
1162  }
1163  else {
1164  // Not Format 7 and not Format 6 video modes
1165  if (dc1394_get_color_coding_from_video_mode(camera,
1166  (dc1394video_mode_t)_videomode,
1167  &_coding) != DC1394_SUCCESS) {
1168  close();
1169  vpERROR_TRACE("Could not query supported color coding for mode %d\n",
1170  _videomode);
1172  "Can't query current color coding"));
1173  }
1174  }
1175  coding = (vp1394TwoColorCodingType) _coding;
1176 }
1177 
1199 uint32_t
1201  std::list<vp1394TwoColorCodingType> & codings)
1202 {
1203  if (! num_cameras) {
1204  close();
1205  vpERROR_TRACE("No camera found");
1207  "No camera found") );
1208  }
1209 
1210  // Refresh the list of supported framerates
1211  codings.clear();
1212 
1213  if (dc1394_is_video_mode_scalable((dc1394video_mode_t)mode)) {
1214  // Format 7 video mode
1215  dc1394color_codings_t _codings;
1216  if (dc1394_format7_get_color_codings(camera,
1217  (dc1394video_mode_t)mode,
1218  &_codings) != DC1394_SUCCESS) {
1219  close();
1220  vpERROR_TRACE("Could not query supported color codings for mode %d\n",
1221  mode);
1223  "Could not query supported color codings") );
1224  }
1225  if (_codings.num == 0)
1226  return 0;
1227 
1228  for (unsigned int i = 0; i < _codings.num; i ++)
1229  codings.push_back((vp1394TwoColorCodingType)_codings.codings[i]);
1230 
1231  return _codings.num;
1232  }
1233  else if (dc1394_is_video_mode_still_image((dc1394video_mode_t)mode)) {
1234  // Format 6 video mode
1235  return 0;
1236  }
1237  else {
1238  // Not Format 7 and not Format 6 video modes
1239  dc1394color_coding_t _coding;
1240  if (dc1394_get_color_coding_from_video_mode(camera,
1241  (dc1394video_mode_t)mode,
1242  &_coding) != DC1394_SUCCESS) {
1243  close();
1244  vpERROR_TRACE("Could not query supported color coding for mode %d\n",
1245  mode);
1247  "Could not query supported color coding") );
1248  }
1249  codings.push_back((vp1394TwoColorCodingType)_coding);
1250  return 1;
1251  }
1252 }
1274 bool
1276  vp1394TwoColorCodingType coding)
1277 {
1278  if (! num_cameras) {
1279  close();
1280  vpERROR_TRACE("No camera found");
1282  "No camera found") );
1283  }
1284 
1285 
1286  if (dc1394_is_video_mode_scalable((dc1394video_mode_t)mode)) {
1287  // Format 7 video mode
1288  dc1394color_codings_t _codings;
1289  if (dc1394_format7_get_color_codings(camera,
1290  (dc1394video_mode_t)mode,
1291  &_codings) != DC1394_SUCCESS) {
1292  close();
1293  vpERROR_TRACE("Could not query supported color codings for mode %d\n",
1294  mode);
1296  "Could not query supported color codings") );
1297  }
1298  if (_codings.num == 0)
1299  return 0;
1300 
1301  for (unsigned int i = 0; i < _codings.num; i ++){
1302  if (coding==(vp1394TwoColorCodingType)_codings.codings[i])
1303  return true;
1304  }
1305  return false;
1306  }
1307  else if (dc1394_is_video_mode_still_image((dc1394video_mode_t)mode)) {
1308  // Format 6 video mode
1309  return false;
1310  }
1311  else {
1312  // Not Format 7 and not Format 6 video modes
1313  dc1394color_coding_t _coding;
1314  if (dc1394_get_color_coding_from_video_mode(camera,
1315  (dc1394video_mode_t)mode,
1316  &_coding) != DC1394_SUCCESS) {
1317  close();
1318  vpERROR_TRACE("Could not query supported color coding for mode %d\n",
1319  mode);
1321  "Could not query supported color coding") );
1322  return false;
1323  }
1324  if (coding==(vp1394TwoColorCodingType)_coding)
1325  return true;
1326 
1327  return false;
1328  }
1329 }
1330 
1331 
1363 void
1364 vp1394TwoGrabber::setFormat7ROI(unsigned int left, unsigned int top,
1365  unsigned int width, unsigned int height)
1366 {
1367  open();
1368  if (! num_cameras) {
1369  close();
1370  vpERROR_TRACE("No camera found");
1372  "No camera found") );
1373  }
1374 
1375  dc1394video_mode_t _videomode;
1376  if (dc1394_video_get_mode(camera, &_videomode) != DC1394_SUCCESS) {
1377 
1378  close();
1379  vpERROR_TRACE("Can't get current video mode");
1381  "Can't get current video mode") );
1382  }
1383  if (dc1394_is_video_mode_scalable(_videomode)) {
1384  // Stop dma capture if started
1385  setTransmission(DC1394_OFF);
1386  setCapture(DC1394_OFF);
1387  // Format 7 video mode
1388  unsigned int max_width, max_height;
1389  if (dc1394_format7_get_max_image_size(camera, _videomode,
1390  &max_width, &max_height)
1391  != DC1394_SUCCESS) {
1392 
1393  close();
1394  vpERROR_TRACE("Can't get format7 max image size");
1396  "Can't get format7 max image size") );
1397  }
1398 #if 0
1399  vpTRACE("left: %d top: %d width: %d height: %d", left, top,
1400  width == 0 ? DC1394_USE_MAX_AVAIL: width,
1401  height == 0 ? DC1394_USE_MAX_AVAIL : height);
1402  vpTRACE("max_width: %d max_height: %d", max_width, max_height);
1403 #endif
1404 
1405  if (left > max_width) {
1406  vpERROR_TRACE("Can't set format7 ROI");
1408  "Can't set format7 ROI") );
1409  }
1410  if (top > max_height) {
1411  vpERROR_TRACE("Can't set format7 ROI");
1413  "Can't set format7 ROI") );
1414  }
1415 
1416  int32_t roi_width;
1417  int32_t roi_height;
1418 
1419  if (width != 0) {
1420  // Check if roi width is acceptable (ie roi is contained in the image)
1421  if (width > (max_width - left))
1422  width = (max_width - left);
1423  roi_width = (int32_t)width;
1424  }
1425  else {
1426  roi_width = DC1394_USE_MAX_AVAIL;
1427  }
1428 
1429  if (height != 0) {
1430  // Check if roi height is acceptable (ie roi is contained in the image)
1431  if (height > (max_height - top))
1432  height = (max_height - top);
1433  roi_height = (int32_t)height;
1434  }
1435  else {
1436  roi_height = DC1394_USE_MAX_AVAIL;
1437  }
1438 
1439 
1440  if (dc1394_format7_set_roi(camera, _videomode,
1441  (dc1394color_coding_t) DC1394_QUERY_FROM_CAMERA, // color_coding
1442  DC1394_USE_MAX_AVAIL/*DC1394_QUERY_FROM_CAMERA*/, // bytes_per_packet
1443  (int32_t)left, // left
1444  (int32_t)top, // top
1445  roi_width,
1446  roi_height)
1447  != DC1394_SUCCESS) {
1448  close();
1449  vpERROR_TRACE("Can't set format7 roi");
1451  "Can't get current video mode") );
1452  }
1453  // Update the image size
1454  if (dc1394_format7_get_image_size(camera, _videomode,
1455  &this->width,
1456  &this->height)
1457  != DC1394_SUCCESS) {
1458  close();
1459  vpERROR_TRACE("Can't get format7 image size");
1461  "Can't get format7 image size") );
1462  }
1463 
1464  setCapture(DC1394_ON);
1465  setTransmission(DC1394_ON);
1466  }
1467 }
1482 void
1483 vp1394TwoGrabber::initialize(bool reset)
1484 {
1485  if (init == false){
1486  // Find cameras
1487 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
1488  if (d != NULL)
1489  dc1394_free (d);
1490 
1491  d = dc1394_new ();
1492  if (dc1394_camera_enumerate (d, &list) != DC1394_SUCCESS) {
1493  dc1394_camera_free_list (list);
1494  close();
1495  vpERROR_TRACE("Failed to enumerate cameras\n");
1497  "Failed to enumerate cameras") );
1498  }
1499 
1500  if (list->num == 0) {
1501  dc1394_camera_free_list (list);
1502  close();
1503  vpERROR_TRACE("No cameras found");
1505  "No cameras found") );
1506  }
1507 
1508  if (cameras != NULL)
1509  delete [] cameras;
1510 
1511  cameras = new dc1394camera_t * [list->num];
1512 
1513  num_cameras = 0;
1514 
1515  for (unsigned int i=0; i < list->num; i ++) {
1516  cameras[i] = dc1394_camera_new (d, list->ids[i].guid);
1517  if (!cameras[i]) {
1518  vpTRACE ("Failed to initialize camera with guid \"%ld\"\n",
1519  list->ids[i].guid);
1520  continue;
1521  }
1522  // Update the number of working cameras
1523  num_cameras ++;
1524  }
1525 
1526  if (reset) {
1527  // Reset the bus to make firewire working if the program was not properly
1528  // stopped by a CTRL-C. We reset here only the bus attached to the first
1529  // camera
1530  dc1394_reset_bus(cameras[0]);
1531  }
1532 
1533  if (list != NULL)
1534  dc1394_camera_free_list (list);
1535  list = NULL;
1536 
1537 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
1538  if (cameras != NULL)
1539  free(cameras);
1540  cameras = NULL;
1541  int err = dc1394_find_cameras(&cameras, &num_cameras);
1542 
1543  if (err!=DC1394_SUCCESS && err != DC1394_NO_CAMERA) {
1544  close();
1545  vpERROR_TRACE("Unable to look for cameras\n\n"
1546  "Please check \n"
1547  " - if the kernel modules `ieee1394',`raw1394' and `ohci1394' are loaded \n"
1548  " - if you have read/write access to /dev/raw1394\n\n");
1550  "Unable to look for cameras") );
1551 
1552  }
1553 #endif
1554 
1555  if (num_cameras == 0) {
1556  close();
1557  vpERROR_TRACE("No cameras found");
1559  "No cameras found") );
1560  }
1561 
1562  // allocation for the parameters
1563  isDataModified = new bool[num_cameras];
1564  for(unsigned int i=0; i<num_cameras; i++)
1565  isDataModified[i] = false;
1566  initialShutterMode = new dc1394feature_mode_t[num_cameras];
1567  dataCam = new vpDc1394TwoCameraParametersData[num_cameras];
1568 
1569  if (camera_id >= num_cameras) {
1570  // Bad camera id
1571  close();
1572  vpERROR_TRACE("Bad camera id: %u", camera_id);
1573  vpERROR_TRACE("Only %u camera on the bus.", num_cameras);
1575  "Bad camera id") );
1576  }
1577 
1578  if (verbose) {
1579  std::cout << "------ Bus information ------" << std::endl;
1580  std::cout << "Number of camera(s) on the bus : " << num_cameras <<std::endl;
1581  std::cout << "-----------------------------" << std::endl;
1582  }
1583 
1584  if (camIsOpen != NULL) delete [] camIsOpen;
1585  camIsOpen = new bool [num_cameras];
1586  for (unsigned int i = 0;i<num_cameras;i++){
1587  camIsOpen[i]=false;
1588  }
1589 
1590  init = true;
1591  }
1592 }
1602 void
1604 {
1605  if (init == false) initialize(false);
1606  if (camIsOpen[camera_id] == false){
1607  dc1394switch_t status = DC1394_OFF;
1608 
1609  //#ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
1610  dc1394_video_get_transmission(cameras[camera_id], &status);
1611  if (status != DC1394_OFF){
1612  //#endif
1613  if (dc1394_video_set_transmission(cameras[camera_id],DC1394_OFF)!=DC1394_SUCCESS)
1614  vpTRACE("Could not stop ISO transmission");
1615  else {
1616  vpTime::wait(500);
1617  if (dc1394_video_get_transmission(cameras[camera_id], &status)!=DC1394_SUCCESS)
1618  vpTRACE("Could get ISO status");
1619  else {
1620  if (status==DC1394_ON) {
1621  vpTRACE("ISO transmission refuses to stop");
1622  }
1623 #ifdef VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
1624  // No yet in the new API
1625  cameras[camera_id]->is_iso_on=status;
1626 #endif
1627  }
1628  //#ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
1629  }
1630  //#endif
1631  }
1632  setCamera(camera_id);
1633  //setIsoSpeed(DC1394_ISO_SPEED_400);
1634  setCapture(DC1394_ON);
1635  setTransmission(DC1394_ON);
1636  camIsOpen[camera_id] = true;
1637  }
1638 }
1647 void
1649 {
1650  if (init){
1651  if (num_cameras) {
1652  for (unsigned int i = 0; i < num_cameras;i++) {
1653  if (camIsOpen[i]) {
1654  camera = cameras[i];
1655  this->camera_id = i;// set camera id for the function updateDataStructToCam
1656  setTransmission(DC1394_OFF);
1657  setCapture(DC1394_OFF);
1658  if(isDataModified[i]){
1659  // reset values
1660  try{
1661  updateDataStructToCam();
1662  }
1663  catch(...){
1664  }
1665  // reset mode (manual, auto, ...)
1666  if (dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS, initialShutterMode[i]) != DC1394_SUCCESS ||
1667  dc1394_feature_set_mode(camera, DC1394_FEATURE_EXPOSURE, initialShutterMode[i]) != DC1394_SUCCESS ||
1668  dc1394_feature_set_mode(camera, DC1394_FEATURE_SHARPNESS, initialShutterMode[i]) != DC1394_SUCCESS ||
1669  dc1394_feature_set_mode(camera, DC1394_FEATURE_HUE, initialShutterMode[i]) != DC1394_SUCCESS ||
1670  dc1394_feature_set_mode(camera, DC1394_FEATURE_SATURATION, initialShutterMode[i]) != DC1394_SUCCESS ||
1671  dc1394_feature_set_mode(camera, DC1394_FEATURE_GAMMA, initialShutterMode[i]) != DC1394_SUCCESS ||
1672  dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER, initialShutterMode[i]) != DC1394_SUCCESS ||
1673  dc1394_feature_set_mode(camera, DC1394_FEATURE_GAIN, initialShutterMode[i]) != DC1394_SUCCESS ||
1674  dc1394_feature_set_mode(camera, DC1394_FEATURE_IRIS, initialShutterMode[i])){
1675 
1676  vpERROR_TRACE("Unable to reset the initial mode");
1678  "Unable to reset the initial mode"));
1679  }
1680  }
1681  if (dc1394_camera_set_power(camera, DC1394_OFF) != DC1394_SUCCESS)
1682  std::cout << "Unable to turn camera off" << std::endl;
1683  }
1684 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
1685  dc1394_camera_free(cameras[i]);
1686 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
1687  dc1394_free_camera(cameras[i]);
1688 #endif
1689  }
1690  }
1691  if (camIsOpen != NULL) {
1692  delete [] camIsOpen;
1693  camIsOpen = NULL;
1694  }
1695 
1696 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
1697  if (cameras != NULL) {
1698  delete [] cameras;
1699  cameras = NULL;
1700  }
1701  if (d != NULL) {
1702  dc1394_free (d);
1703  d = NULL;
1704  }
1705 
1706 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
1707  if (cameras != NULL) {
1708  free(cameras);
1709  cameras = NULL;
1710  }
1711 #endif
1712 
1713  camIsOpen = NULL;
1714  num_cameras = 0;
1715 
1716  // remove data for the parameters
1717  if(isDataModified != NULL){
1718  delete[] isDataModified;
1719  isDataModified = NULL;
1720  }
1721  if(initialShutterMode != NULL){
1722  delete[] initialShutterMode;
1723  initialShutterMode = NULL;
1724  }
1725  if(dataCam != NULL){
1726  delete[] dataCam;
1727  dataCam = NULL;
1728  }
1729 
1730  init = false;
1731  }
1732 }
1733 
1745 void
1747 {
1748  if (size < 1) {
1749  close();
1751  "Could not set ring buffer size") );
1752  }
1753 
1754  if (size != num_buffers) {
1755  // We need to change the ring buffer size
1756  num_buffers = size;
1757  if(camIsOpen[camera_id]){
1758  setCapture(DC1394_OFF);
1759  setCapture(DC1394_ON);
1760  }
1761  }
1762 }
1763 
1773 unsigned int
1775 {
1776  return num_buffers;
1777 }
1778 
1817 void
1818 vp1394TwoGrabber::setAutoShutter(unsigned int minvalue, unsigned int maxvalue)
1819 {
1820  if (! num_cameras) {
1821  close();
1822  vpERROR_TRACE("No camera found");
1824  "No camera found") );
1825  }
1826 
1827  if (dc1394_feature_set_power(camera, DC1394_FEATURE_SHUTTER, DC1394_ON)
1828  != DC1394_SUCCESS) {
1829  // vpERROR_TRACE("Cannot set shutter on. \n");
1830  close();
1832  "Cannot set shutter on") );
1833  }
1834 
1835 
1836  if (dc1394_feature_set_mode(camera,
1837  DC1394_FEATURE_SHUTTER,
1838  DC1394_FEATURE_MODE_AUTO)
1839  != DC1394_SUCCESS) {
1840  // vpERROR_TRACE("Cannot set auto shutter. \n");
1841  close();
1843  "Cannot set auto shutter") );
1844  }
1845 
1846  if (dc1394_avt_set_auto_shutter(camera, minvalue, maxvalue)
1847  != DC1394_SUCCESS) {
1848  // vpERROR_TRACE("Cannot set auto shutter min and max values. Is the camera an AVT one?\n");
1849  close();
1851  "Cannot set auto shutter min and max values") );
1852  }
1853 }
1854 
1872 void
1873 vp1394TwoGrabber::setCapture(dc1394switch_t _switch)
1874 {
1875  if (! num_cameras) {
1876  close();
1877  vpERROR_TRACE("No camera found");
1879  "No camera found") );
1880  }
1881 
1882  if (_switch == DC1394_ON) {
1883  //if (dc1394_capture_setup(camera, num_buffers) != DC1394_SUCCESS) {
1884  // To be compatible with libdc1394 svn 382 version
1885  if (dc1394_capture_setup(camera, num_buffers,
1886  DC1394_CAPTURE_FLAGS_DEFAULT) != DC1394_SUCCESS) {
1887  vpERROR_TRACE("Unable to setup camera capture-\n"
1888  "make sure that the video mode and framerate are "
1889  "supported by your camera.\n");
1890  close();
1892  "Could not setup dma capture") );
1893  }
1894  }
1895  else { // _switch == DC1394_OFF
1896  dc1394error_t code = dc1394_capture_stop(camera);
1897 
1898  if (code != DC1394_SUCCESS && code != DC1394_CAPTURE_IS_NOT_SET) {
1899  vpERROR_TRACE("Unable to stop camera capture\n");
1900  close();
1902  "Could not setup dma capture") );
1903  }
1904  }
1905 }
1906 
1907 
1922 void
1923 vp1394TwoGrabber::setTransmission(dc1394switch_t _switch)
1924 {
1925  if (! num_cameras) {
1926  close();
1927  vpERROR_TRACE("No camera found");
1929  "No camera found") );
1930  }
1931 
1932  dc1394switch_t status = DC1394_OFF;
1933 
1934  if (dc1394_video_get_transmission(camera, &status)!=DC1394_SUCCESS) {
1935  vpERROR_TRACE("Unable to get transmision status");
1936  close();
1938  "Could not setup dma capture") );
1939  }
1940 
1941  // if (status!=_switch){
1942  // Start dma capture if halted
1943  if (dc1394_video_set_transmission(camera, _switch) != DC1394_SUCCESS) {
1944  vpERROR_TRACE("Unable to setup camera capture-\n"
1945  "make sure that the video mode and framerate are "
1946  "supported by your camera.\n");
1947  close();
1949  "Could not setup dma capture") );
1950  }
1951 
1952  if (_switch == DC1394_ON) {
1953  status = DC1394_OFF;
1954 
1955  int i = 0;
1956  while ( status == DC1394_OFF && i++ < 5 ) {
1957  usleep(50000);
1958  if (dc1394_video_get_transmission(camera, &status)!=DC1394_SUCCESS) {
1959  vpERROR_TRACE("Unable to get transmision status");
1960  close();
1962  "Could not setup dma capture") );
1963  }
1964  }
1965  }
1966  // }
1967 }
1968 
2003 void
2005 {
2006  if (! num_cameras) {
2007  close();
2008  vpERROR_TRACE("No camera found");
2010  "No camera found") );
2011  }
2012 
2013  dc1394operation_mode_t op_mode;
2014  dc1394speed_t speed;
2015 
2016  // Check the speed to configure in B-mode or A-mode
2017  if (isospeed >= vpISO_SPEED_800) {
2018  if (camera->bmode_capable != DC1394_TRUE) {
2019 // vpERROR_TRACE("Camera is not 1394B mode capable. \n"
2020 // "Set the iso speed lower or equal to 400Mbps");
2021  close();
2023  "Camera is not 1394B mode capable") );
2024  }
2025 
2026  if(dc1394_video_set_operation_mode(camera,
2027  DC1394_OPERATION_MODE_1394B)
2028  != DC1394_SUCCESS) {
2029 // vpERROR_TRACE("Cannot set camera to 1394B mode. \n");
2030  close();
2032  "Cannot set camera to 1394B mode") );
2033  }
2034 
2035  if (dc1394_video_get_operation_mode(camera, &op_mode) != DC1394_SUCCESS) {
2036 // vpERROR_TRACE("Failed to set 1394B mode. \n");
2037  close();
2039  "Failed to set 1394B mode") );
2040  }
2041  }
2042  else {
2043  if (dc1394_video_set_operation_mode(camera,
2044  DC1394_OPERATION_MODE_LEGACY)
2045  != DC1394_SUCCESS) {
2046 // vpERROR_TRACE("Cannot set camera to 1394A mode. \n");
2047  close();
2049  "Cannot set camera to 1394A mode") );
2050  }
2051 
2052  if (dc1394_video_get_operation_mode(camera, &op_mode) != DC1394_SUCCESS) {
2053 // vpERROR_TRACE("Failed to set 1394A mode. \n");
2054  close();
2056  "Failed to set 1394A mode") );
2057  }
2058  }
2059 
2060  if (dc1394_video_set_iso_speed(camera, (dc1394speed_t) isospeed)
2061  != DC1394_SUCCESS) {
2062 // vpERROR_TRACE("Cannot set requested iso speed. \n");
2063  close();
2065  "Cannot set requested iso speed") );
2066  }
2067 
2068  if (dc1394_video_get_iso_speed(camera, &speed) != DC1394_SUCCESS) {
2069 // vpERROR_TRACE("Failed to set iso speed. \n");
2070  close();
2072  "Failed to set iso speed") );
2073  }
2074 }
2075 
2086 void
2088 {
2089  open();
2090  acquire(I);
2091 }
2092 
2103 void
2105 {
2106  open();
2107  acquire(I);
2108 }
2109 
2110 
2149 dc1394video_frame_t *
2151 {
2152 
2153  if (! num_cameras) {
2154  close();
2155  vpERROR_TRACE("No camera found");
2157  "No camera found") );
2158  }
2159 
2160  dc1394video_frame_t *frame = NULL;
2161 
2162  if (dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame)
2163  !=DC1394_SUCCESS) {
2164  vpERROR_TRACE ("Error: Failed to capture from camera %d\n", camera_id);
2165  }
2166 
2167  return frame;
2168 }
2169 
2211 dc1394video_frame_t *
2213 {
2214  uint64_t timestamp;
2215  uint32_t id;
2216 
2217  dc1394video_frame_t *frame;
2218 
2219  frame = dequeue(I, timestamp, id);
2220 
2221  return frame;
2222 }
2223 
2272 dc1394video_frame_t *
2274  uint64_t &timestamp,
2275  uint32_t &id)
2276 {
2277 
2278  open();
2279 
2280  dc1394video_frame_t *frame;
2281 
2282  frame = dequeue();
2283 
2284  // Timeval data structure providing the unix time
2285  // [microseconds] at which the frame was captured in the ring buffer.
2286  timestamp = frame->timestamp;
2287  id = frame->id;
2288 
2289  this->width = frame->size[0];
2290  this->height = frame->size[1];
2291  unsigned int size = this->width * this->height;
2292 
2293  if ((I.getWidth() != this->width)||(I.getHeight() != this->height))
2294  I.resize(this->height, this->width);
2295 
2296  switch(frame->color_coding) {
2297  case DC1394_COLOR_CODING_MONO8:
2298  case DC1394_COLOR_CODING_RAW8:
2299  memcpy(I.bitmap, (unsigned char *) frame->image,
2300  size*sizeof(unsigned char));
2301  break;
2302  case DC1394_COLOR_CODING_MONO16:
2303  case DC1394_COLOR_CODING_RAW16:
2304  vpImageConvert::MONO16ToGrey( (unsigned char *) frame->image,
2305  I.bitmap, size);
2306  break;
2307 
2308  case DC1394_COLOR_CODING_YUV411:
2309  vpImageConvert::YUV411ToGrey( (unsigned char *) frame->image,
2310  I.bitmap, size);
2311  break;
2312 
2313  case DC1394_COLOR_CODING_YUV422:
2314  vpImageConvert::YUV422ToGrey( (unsigned char *) frame->image,
2315  I.bitmap, size);
2316  break;
2317 
2318  case DC1394_COLOR_CODING_YUV444:
2319  vpImageConvert::YUV444ToGrey( (unsigned char *) frame->image,
2320  I.bitmap, size);
2321  break;
2322 
2323  case DC1394_COLOR_CODING_RGB8:
2324  vpImageConvert::RGBToGrey((unsigned char *) frame->image, I.bitmap, size);
2325  break;
2326 
2327 
2328  default:
2329  close();
2330  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
2332  "Format conversion not implemented. "
2333  "Acquisition failed.") );
2334  break;
2335  };
2336 
2337  return frame;
2338 }
2339 
2380 dc1394video_frame_t *
2382 {
2383  uint64_t timestamp;
2384  uint32_t id;
2385 
2386  dc1394video_frame_t *frame;
2387 
2388  frame = dequeue(I, timestamp, id);
2389 
2390  return frame;
2391 }
2392 
2441 dc1394video_frame_t *
2443  uint64_t &timestamp,
2444  uint32_t &id)
2445 {
2446 
2447  open();
2448 
2449  dc1394video_frame_t *frame;
2450 
2451  frame = dequeue();
2452 
2453  // Timeval data structure providing the unix time
2454  // [microseconds] at which the frame was captured in the ring buffer.
2455  timestamp = frame->timestamp;
2456  id = frame->id;
2457 
2458  this->width = frame->size[0];
2459  this->height = frame->size[1];
2460  unsigned int size = this->width * this->height;
2461 
2462  if ((I.getWidth() != width)||(I.getHeight() != height))
2463  I.resize(height, width);
2464 
2465  switch (frame->color_coding) {
2466  case DC1394_COLOR_CODING_MONO8:
2467  case DC1394_COLOR_CODING_RAW8:
2468  vpImageConvert::GreyToRGBa((unsigned char *) frame->image,
2469  (unsigned char *) I.bitmap, size);
2470  break;
2471 
2472  case DC1394_COLOR_CODING_MONO16:
2473  case DC1394_COLOR_CODING_RAW16:
2474  vpImageConvert::MONO16ToRGBa((unsigned char *) frame->image,
2475  (unsigned char *) I.bitmap, size);
2476  break;
2477 
2478  case DC1394_COLOR_CODING_YUV411:
2479  vpImageConvert::YUV411ToRGBa( (unsigned char *) frame->image,
2480  (unsigned char *) I.bitmap, size);
2481  break;
2482 
2483  case DC1394_COLOR_CODING_YUV422:
2484  vpImageConvert::YUV422ToRGBa( (unsigned char *) frame->image,
2485  (unsigned char *) I.bitmap, size);
2486  break;
2487 
2488  case DC1394_COLOR_CODING_YUV444:
2489  vpImageConvert::YUV444ToRGBa( (unsigned char *) frame->image,
2490  (unsigned char *) I.bitmap, size);
2491  break;
2492 
2493  case DC1394_COLOR_CODING_RGB8:
2494  vpImageConvert::RGBToRGBa((unsigned char *) frame->image,
2495  (unsigned char *) I.bitmap, size);
2496  break;
2497 
2498 
2499  default:
2500  close();
2501  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
2503  "Format conversion not implemented. "
2504  "Acquisition failed.") );
2505  break;
2506  };
2507 
2508  return frame;
2509 }
2510 
2521 void
2522 vp1394TwoGrabber::enqueue(dc1394video_frame_t *frame)
2523 {
2524 
2525  if (! num_cameras) {
2526  close();
2527  vpERROR_TRACE("No camera found");
2529  "No camera found") );
2530  }
2531 
2532  if (frame)
2533  dc1394_capture_enqueue(camera, frame);
2534 }
2535 
2536 
2550 void
2552 {
2553  uint64_t timestamp;
2554  uint32_t id;
2555 
2556  dc1394video_frame_t *frame;
2557 
2558  frame = dequeue(I, timestamp, id);
2559  enqueue(frame);
2560 }
2561 
2580 void
2582  uint64_t &timestamp,
2583  uint32_t &id)
2584 {
2585  dc1394video_frame_t *frame;
2586 
2587  open();
2588  frame = dequeue(I, timestamp, id);
2589  enqueue(frame);
2590 }
2591 
2592 
2593 
2607 void
2609 {
2610  uint64_t timestamp;
2611  uint32_t id;
2612  dc1394video_frame_t *frame;
2613 
2614  open();
2615  frame = dequeue(I, timestamp, id);
2616  enqueue(frame);
2617 }
2618 
2637 void
2639  uint64_t &timestamp,
2640  uint32_t &id)
2641 {
2642  dc1394video_frame_t *frame;
2643 
2644  open();
2645  frame = dequeue();
2646  // Timeval data structure providing the unix time
2647  // [microseconds] at which the frame was captured in the ring buffer.
2648  timestamp = frame->timestamp;
2649  id = frame->id;
2650 
2651  this->width = frame->size[0];
2652  this->height = frame->size[1];
2653  unsigned int size = this->width * this->height;
2654 
2655  if ((I.getWidth() != width)||(I.getHeight() != height))
2656  I.resize(height, width);
2657 
2658  switch (frame->color_coding) {
2659  case DC1394_COLOR_CODING_MONO8:
2660  case DC1394_COLOR_CODING_RAW8:
2661  vpImageConvert::GreyToRGBa((unsigned char *) frame->image,
2662  (unsigned char *) I.bitmap, size);
2663  break;
2664 
2665  case DC1394_COLOR_CODING_YUV411:
2666  vpImageConvert::YUV411ToRGBa( (unsigned char *) frame->image,
2667  (unsigned char *) I.bitmap, size);
2668  break;
2669 
2670  case DC1394_COLOR_CODING_YUV422:
2671  vpImageConvert::YUV422ToRGBa( (unsigned char *) frame->image,
2672  (unsigned char *) I.bitmap, size);
2673  break;
2674 
2675  case DC1394_COLOR_CODING_YUV444:
2676  vpImageConvert::YUV444ToRGBa( (unsigned char *) frame->image,
2677  (unsigned char *) I.bitmap, size);
2678  break;
2679 
2680  case DC1394_COLOR_CODING_RGB8:
2681  vpImageConvert::RGBToRGBa((unsigned char *) frame->image,
2682  (unsigned char *) I.bitmap, size);
2683  break;
2684 
2685 
2686  default:
2687  close();
2688  vpERROR_TRACE("Format conversion not implemented. Acquisition failed.");
2690  "Format conversion not implemented. "
2691  "Acquisition failed.") );
2692  break;
2693  };
2694 
2695  enqueue(frame);
2696 
2697 }
2698 
2715 void vp1394TwoGrabber::getWidth(unsigned int &width)
2716 {
2717  if (! num_cameras) {
2718  close();
2719  vpERROR_TRACE("No camera found");
2721  "No camera found") );
2722  }
2723 
2724  width = this->width;
2725 }
2726 
2744 {
2745  if (! num_cameras) {
2746  close();
2747  vpERROR_TRACE("No camera found");
2749  "No camera found") );
2750  }
2751 
2752  return this->width;
2753 }
2754 
2772 void vp1394TwoGrabber::getHeight(unsigned int &height)
2773 {
2774  if (! num_cameras) {
2775  close();
2776  vpERROR_TRACE("No camera found");
2778  "No camera found") );
2779  }
2780 
2781  height = this->height;
2782 }
2801 {
2802  if (! num_cameras) {
2803  close();
2804  vpERROR_TRACE("No camera found");
2806  "No camera found") );
2807  }
2808 
2809  return this->height;
2810 }
2811 
2817 void
2819 {
2820  std::cout << "----------------------------------------------------------"
2821  << std::endl
2822  << "----- Information for camera " << camera_id
2823  << " -----" << std::endl
2824  << "----------------------------------------------------------" << std::endl;
2825 
2826 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
2827  dc1394_camera_print_info( camera, stdout);
2828 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
2829  dc1394_print_camera_info( camera);
2830 #endif
2831 
2832  dc1394featureset_t features;
2833 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
2834  if (dc1394_feature_get_all(camera, &features) != DC1394_SUCCESS) {
2835 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
2836  if (dc1394_get_camera_feature_set(camera, &features) != DC1394_SUCCESS) {
2837 #endif
2838  close();
2839  vpERROR_TRACE("unable to get feature set for camera %d\n", camera_id);
2841  "Cannot get camera features") );
2842 
2843  } else {
2844 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
2845  dc1394_feature_print_all(&features, stdout);
2846 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
2847  dc1394_print_feature_set(&features);
2848 #endif
2849  }
2850  std::cout << "----------------------------------------------------------" << std::endl;
2851 }
2852 
2866 {
2867  std::string _str = "";
2868  dc1394video_mode_t _videomode = (dc1394video_mode_t) videomode;
2869 
2870  if ((_videomode >= DC1394_VIDEO_MODE_MIN)
2871  && (_videomode <= DC1394_VIDEO_MODE_MAX)) {
2872  _str = strVideoMode[_videomode - DC1394_VIDEO_MODE_MIN];
2873  }
2874  else {
2875  vpCERROR << "The video mode " << videomode
2876  << " is not supported by the camera" << std::endl;
2877  }
2878 
2879  return _str;
2880 }
2881 
2895 {
2896  std::string _str = "";
2897  dc1394framerate_t _fps = (dc1394framerate_t) fps;
2898 
2899  if ((_fps >= DC1394_FRAMERATE_MIN)
2900  && (_fps <= DC1394_FRAMERATE_MAX)) {
2901  _str = strFramerate[_fps - DC1394_FRAMERATE_MIN];
2902  }
2903  else {
2904  vpCERROR << "The framerate " << fps
2905  << " is not supported by the camera" << std::endl;
2906  }
2907 
2908  return _str;
2909 }
2910 
2924 {
2925  std::string _str = "";
2926  dc1394color_coding_t _coding = (dc1394color_coding_t) colorcoding;
2927 
2928  if ((_coding >= DC1394_COLOR_CODING_MIN)
2929  && (_coding <= DC1394_COLOR_CODING_MAX)) {
2930  _str = strColorCoding[_coding - DC1394_COLOR_CODING_MIN];
2931 
2932  }
2933  else {
2934  vpCERROR << "The color coding " << colorcoding
2935  << " is not supported by the camera" << std::endl;
2936  }
2937 
2938  return _str;
2939 }
2940 
2960 {
2962 
2963  for (int i = DC1394_VIDEO_MODE_MIN; i <= DC1394_VIDEO_MODE_MAX; i ++) {
2964  _id = (vp1394TwoVideoModeType) i;
2965  if (videomode.compare(videoMode2string(_id)) == 0)
2966  return _id;
2967  };
2968 
2970  "The required videomode is not valid") );
2971 
2972  return (vp1394TwoVideoModeType) 0;
2973 }
2974 
2975 
2995 {
2997 
2998  for (int i = DC1394_FRAMERATE_MIN; i <= DC1394_FRAMERATE_MAX; i ++) {
2999  _id = (vp1394TwoFramerateType) i;
3000  if (framerate.compare(framerate2string(_id)) == 0)
3001  return _id;
3002  };
3003 
3005  "The required framerate is not valid") );
3006 
3007  return (vp1394TwoFramerateType) 0;
3008 }
3009 
3029 {
3031 
3032  for (int i = DC1394_COLOR_CODING_MIN; i <= DC1394_COLOR_CODING_MAX; i ++) {
3033  _id = (vp1394TwoColorCodingType) i;
3034  if (colorcoding.compare(colorCoding2string(_id)) == 0)
3035  return _id;
3036  };
3037 
3039  "The required color coding is not valid") );
3040 
3041  return (vp1394TwoColorCodingType) 0;
3042 }
3043 
3077 {
3078  for (unsigned int i = 0; i < num_cameras;i++) {
3079  if (camIsOpen[i]) {
3080  camera = cameras[i];
3081  setTransmission(DC1394_OFF);
3082  setCapture(DC1394_OFF);
3083  }
3084  }
3085 #ifdef VISP_HAVE_DC1394_2_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
3086  setCamera(camera_id);
3087  // free the other cameras
3088  for (unsigned int i=0;i<num_cameras;i++){
3089  if (i!=camera_id) dc1394_camera_free(cameras[i]);
3090  }
3091 
3092  printf ("Reseting bus...\n");
3093  dc1394_reset_bus (camera);
3094 
3095  dc1394_camera_free (camera);
3096  dc1394_free (d);
3097  d = NULL;
3098  if (cameras != NULL)
3099  delete [] cameras;
3100  cameras = NULL ;
3101 #elif defined VISP_HAVE_DC1394_2_FIND_CAMERAS // old API <= libdc1394-2.0.0-rc7
3102 
3103  setCamera(camera_id);
3104  // free the other cameras
3105  for (unsigned int i=0;i<num_cameras;i++){
3106  if (i!=camera_id) dc1394_free_camera(cameras[i]);
3107  }
3108  free(cameras);
3109  cameras = NULL;
3110 
3111  dc1394_reset_bus(camera);
3112  dc1394_free_camera(camera);
3113 
3114 #endif
3115  if (camIsOpen != NULL)
3116  delete [] camIsOpen;
3117  camIsOpen = NULL ;
3118 
3119  num_cameras = 0;
3120 
3121  init = false;
3122  vpTime::wait(1000);
3123  initialize(false);
3124 }
3125 
3155 void vp1394TwoGrabber::setPanControl(unsigned int panControlValue)
3156 {
3157  open();
3158  if (! num_cameras) {
3159  close();
3160  vpERROR_TRACE("No camera found");
3162  "No camera found") );
3163  }
3164  uint64_t offset = 0x884;
3165  uint32_t value = 0x82000000 + (uint32_t)panControlValue;
3166  dc1394error_t err;
3167  err = dc1394_set_control_register(camera, offset, value);
3168  if (err != DC1394_SUCCESS) {
3169  vpERROR_TRACE("Unable to set PAN register");
3170  close();
3172  "Unable to set PAN register") );
3173  }
3174 }
3175 
3176 
3194 {
3195  if (! num_cameras) {
3196  close();
3197  vpERROR_TRACE("No camera found");
3199  "No camera found") );
3200  }
3201 
3202  uint32_t value;
3203  dc1394feature_t feature = (dc1394feature_t)param;
3204  dc1394error_t err;
3205  err = dc1394_feature_get_value(camera, feature, &value);
3206  if (err != DC1394_SUCCESS) {
3207  vpERROR_TRACE("Unable to get the information");
3208  close();
3210  "Unable to get the information") );
3211  }
3212  return (unsigned int)value;
3213 }
3214 
3215 
3237  unsigned int val)
3238 {
3239  if (! num_cameras) {
3240  close();
3241  vpERROR_TRACE("No camera found");
3243  "No camera found") );
3244  }
3245  uint32_t value = (uint32_t)val;
3246  dc1394feature_t feature = (dc1394feature_t)param;
3247  dc1394error_t err;
3248  dc1394bool_t hasManualMode = DC1394_FALSE;
3249  dc1394feature_modes_t modesAvailable;
3250 
3251  // test wether we can set the shutter value (manual mode available or not)
3252  err = dc1394_feature_get_modes(camera, feature, &modesAvailable);
3253  if (err != DC1394_SUCCESS) {
3254  vpERROR_TRACE("Unable to detect the manual mode information");
3255  close();
3257  "Unable to detect the manual mode information"));
3258  }
3259 
3260  for(unsigned int i=0; i<modesAvailable.num; i++){
3261  if(modesAvailable.modes[i] == DC1394_FEATURE_MODE_MANUAL){
3262  hasManualMode = DC1394_TRUE;
3263  }
3264  }
3265 
3266  if(hasManualMode == DC1394_TRUE){
3267 
3268  if(!isDataModified[camera_id]){// to ensure we save the first mode even after several set
3269  /* we update the structure */
3270  updateDataCamToStruct();
3271  err = dc1394_feature_get_mode(camera, feature, &(initialShutterMode[camera_id]));
3272  if (err != DC1394_SUCCESS) {
3273  vpERROR_TRACE("Unable to get the initial mode");
3274  close();
3276  "Unable to get the initial mode"));
3277  }
3278  isDataModified[camera_id] = true;
3279  }
3280 
3281  dc1394feature_mode_t manualMode = DC1394_FEATURE_MODE_MANUAL;
3282  err = dc1394_feature_set_mode(camera, feature, manualMode);
3283  if (err != DC1394_SUCCESS) {
3284  vpERROR_TRACE("Unable to set the muanual mode");
3285  close();
3287  "Unable to set the manual mode") );
3288  }
3289  err = dc1394_feature_set_value(camera, feature, value);
3290  if (err != DC1394_SUCCESS) {
3291  vpERROR_TRACE("Unable to set the shutter information");
3292  close();
3294  "Unable to set the shutter information") );
3295  }
3296  }
3297  else{
3298  vpERROR_TRACE("The camera does not have a manual mode.\nCannot change the value");
3300  "The camera does not have a manual mode"));
3301  }
3302 }
3310 void
3312 {
3313  if (! num_cameras) {
3314  close();
3315  vpERROR_TRACE("No camera found");
3317  "No camera found") );
3318  }
3319 
3320  guid = camera->guid;
3321 }
3322 
3330 uint64_t
3332 {
3333  if (! num_cameras) {
3334  close();
3335  vpERROR_TRACE("No camera found");
3337  "No camera found") );
3338  }
3339 
3340  return camera->guid;
3341 }
3342 
3343 
3348 inline void
3349 vp1394TwoGrabber::updateDataCamToStruct()
3350 {
3351  dataCam[camera_id].brightness = getParameterValue(vpFEATURE_BRIGHTNESS);
3352  dataCam[camera_id].exposure = getParameterValue(vpFEATURE_EXPOSURE);
3353  dataCam[camera_id].sharpness = getParameterValue(vpFEATURE_SHARPNESS);
3354  dataCam[camera_id].hue = getParameterValue(vpFEATURE_HUE);
3355  dataCam[camera_id].saturation = getParameterValue(vpFEATURE_SATURATION);
3356  dataCam[camera_id].gamma = getParameterValue(vpFEATURE_GAMMA);
3357  dataCam[camera_id].shutter = getParameterValue(vpFEATURE_SHUTTER);
3358  dataCam[camera_id].gain = getParameterValue(vpFEATURE_GAIN);
3359  dataCam[camera_id].iris = getParameterValue(vpFEATURE_IRIS);
3360 }
3361 
3366 inline void
3367 vp1394TwoGrabber::updateDataStructToCam()
3368 {
3369  setParameterValue(vpFEATURE_BRIGHTNESS, dataCam[camera_id].brightness);
3370  setParameterValue(vpFEATURE_EXPOSURE, dataCam[camera_id].exposure);
3371  setParameterValue(vpFEATURE_SHARPNESS, dataCam[camera_id].sharpness);
3372  setParameterValue(vpFEATURE_HUE, dataCam[camera_id].hue);
3373  setParameterValue(vpFEATURE_SATURATION, dataCam[camera_id].saturation);
3374  setParameterValue(vpFEATURE_GAMMA, dataCam[camera_id].gamma);
3375  setParameterValue(vpFEATURE_SHUTTER, dataCam[camera_id].shutter);
3376  setParameterValue(vpFEATURE_GAIN, dataCam[camera_id].gain);
3377  setParameterValue(vpFEATURE_IRIS, dataCam[camera_id].iris);
3378 }
3379 
3380 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
3381 
3399 uint32_t
3401 {
3402  // Refresh the list of supported modes
3403  videomodes.kill();
3404 
3405  if (! num_cameras) {
3406  close();
3407  vpERROR_TRACE("No camera found");
3409  "No camera found") );
3410  }
3411  dc1394video_modes_t _videomodes;
3412 
3413  // get video modes:
3414  if (dc1394_video_get_supported_modes(camera, &_videomodes)!=DC1394_SUCCESS) {
3415 
3416  close();
3417  vpERROR_TRACE("Can't get video modes");
3419  "Can't get video modes") );
3420  }
3421 
3422  // parse the video modes to add in the list
3423  for (unsigned i=0; i < _videomodes.num; i++) {
3424  vp1394TwoVideoModeType _mode = (vp1394TwoVideoModeType) _videomodes.modes[i];
3425  videomodes.addRight( _mode );
3426  }
3427 
3428  // return the number of available video modes
3429  return _videomodes.num;
3430 }
3431 
3464 uint32_t
3467 {
3468  if (! num_cameras) {
3469  close();
3470  vpERROR_TRACE("No camera found");
3472  "No camera found") );
3473  }
3474 
3475  // Refresh the list of supported framerates
3476  fps.kill();
3477 
3478  switch (mode) {
3479  // Framerate not available for:
3480  // - vpVIDEO_MODE_EXIF ie Format_6
3481  // - vpVIDEO_MODE_FORMAT7... ie the Format_7
3482  case vpVIDEO_MODE_EXIF:
3491  return 0;
3492  break;
3493  default:
3494  {
3495  dc1394framerates_t _fps;
3496  if (dc1394_video_get_supported_framerates(camera,
3497  (dc1394video_mode_t)mode,
3498  &_fps) != DC1394_SUCCESS) {
3499  close();
3500  vpERROR_TRACE("Could not query supported frametates for mode %d\n",
3501  mode);
3503  "Could not query supported framerates") );
3504  }
3505  if (_fps.num == 0)
3506  return 0;
3507 
3508  for (unsigned int i = 0; i < _fps.num; i ++)
3509  fps.addRight((vp1394TwoFramerateType)_fps.framerates[i]);
3510 
3511  return _fps.num;
3512  }
3513  break;
3514  }
3515 }
3516 
3541 uint32_t
3544 {
3545  if (! num_cameras) {
3546  close();
3547  vpERROR_TRACE("No camera found");
3549  "No camera found") );
3550  }
3551 
3552  // Refresh the list of supported framerates
3553  codings.kill();
3554 
3555  if (dc1394_is_video_mode_scalable((dc1394video_mode_t)mode)) {
3556  // Format 7 video mode
3557  dc1394color_codings_t _codings;
3558  if (dc1394_format7_get_color_codings(camera,
3559  (dc1394video_mode_t)mode,
3560  &_codings) != DC1394_SUCCESS) {
3561  close();
3562  vpERROR_TRACE("Could not query supported color codings for mode %d\n",
3563  mode);
3565  "Could not query supported color codings") );
3566  }
3567  if (_codings.num == 0)
3568  return 0;
3569 
3570  for (unsigned int i = 0; i < _codings.num; i ++)
3571  codings.addRight((vp1394TwoColorCodingType)_codings.codings[i]);
3572 
3573  return _codings.num;
3574  }
3575  else if (dc1394_is_video_mode_still_image((dc1394video_mode_t)mode)) {
3576  // Format 6 video mode
3577  return 0;
3578  }
3579  else {
3580  // Not Format 7 and not Format 6 video modes
3581  dc1394color_coding_t _coding;
3582  if (dc1394_get_color_coding_from_video_mode(camera,
3583  (dc1394video_mode_t)mode,
3584  &_coding) != DC1394_SUCCESS) {
3585  close();
3586  vpERROR_TRACE("Could not query supported color coding for mode %d\n",
3587  mode);
3589  "Could not query supported color coding") );
3590  }
3591  codings.addRight((vp1394TwoColorCodingType)_coding);
3592  return 1;
3593  }
3594 }
3595 #endif
3596 
3597 #endif
3598 
void getColorCoding(vp1394TwoColorCodingType &coding)
static const char * strFramerate[DC1394_FRAMERATE_NUM]
uint32_t getColorCodingSupported(vp1394TwoVideoModeType videomode, std::list< vp1394TwoColorCodingType > &codings)
static std::string colorCoding2string(vp1394TwoColorCodingType colorcoding)
static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size)
void setAutoShutter(unsigned int minvalue, unsigned int maxvalue)
void setIsoTransmissionSpeed(vp1394TwoIsoSpeedType isospeed)
static std::string framerate2string(vp1394TwoFramerateType fps)
unsigned int getWidth() const
Definition: vpImage.h:154
#define vpERROR_TRACE
Definition: vpDebug.h:379
static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size)
#define vpTRACE
Definition: vpDebug.h:401
Provide simple list management.
Definition: vpList.h:112
Type * bitmap
points toward the bitmap
Definition: vpImage.h:115
static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size)
#define vpCERROR
Definition: vpDebug.h:354
vp1394TwoGrabber(bool reset=true)
bool isVideoModeFormat7(vp1394TwoVideoModeType videomode)
void resize(const unsigned int height, const unsigned int width)
set the size of the image
Definition: vpImage.h:530
static const char * strColorCoding[DC1394_COLOR_CODING_NUM]
static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int size)
static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
void kill()
Destroy the list.
Definition: vpList.h:694
static int wait(double t0, double t)
Definition: vpTime.cpp:149
unsigned int getParameterValue(vp1394TwoParametersType param)
void acquire(vpImage< unsigned char > &I)
unsigned int getNumCameras()
static vp1394TwoColorCodingType string2colorCoding(std::string colorcoding)
Error that can be emited by the vpFrameGrabber class and its derivates.
uint32_t getVideoModeSupported(std::list< vp1394TwoVideoModeType > &videomodes)
static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
bool isFramerateSupported(vp1394TwoVideoModeType videomode, vp1394TwoFramerateType fps)
unsigned int getRingBufferSize()
#define vpCTRACE
Definition: vpDebug.h:327
void setColorCoding(vp1394TwoColorCodingType coding)
void open(vpImage< unsigned char > &I)
static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
void setRingBufferSize(unsigned int size)
static vp1394TwoVideoModeType string2videoMode(std::string videomode)
void setFormat7ROI(unsigned int left=0, unsigned int top=0, unsigned int width=0, unsigned int height=0)
unsigned int height
Number of rows in the image.
void setCamera(uint64_t camera)
static const char * strVideoMode[DC1394_VIDEO_MODE_NUM]
void addRight(const type &el)
add a new element in the list, at the right of the current one
Definition: vpList.h:480
static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba, unsigned int size)
void enqueue(dc1394video_frame_t *frame)
static vp1394TwoFramerateType string2framerate(std::string fps)
unsigned int getWidth()
void getFramerate(vp1394TwoFramerateType &fps)
static std::string videoMode2string(vp1394TwoVideoModeType videomode)
bool init
Set to true if the frame grabber has been initialized.
void setParameterValue(vp1394TwoParametersType param, unsigned int val)
static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
bool isColorCodingSupported(vp1394TwoVideoModeType videomode, vp1394TwoColorCodingType coding)
static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size)
void setFramerate(vp1394TwoFramerateType fps)
dc1394video_frame_t * dequeue()
void setVideoMode(vp1394TwoVideoModeType videomode)
void setPanControl(unsigned int panControlValue)
uint32_t getFramerateSupported(vp1394TwoVideoModeType videomode, std::list< vp1394TwoFramerateType > &fps)
unsigned int getHeight() const
Definition: vpImage.h:145
bool isVideoModeSupported(vp1394TwoVideoModeType videomode)
static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size)
void getVideoMode(vp1394TwoVideoModeType &videomode)
unsigned int width
Number of columns in the image.
unsigned int getHeight()