| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "hwdrivers-precomp.h" // Precompiled headers |
| 11 |
// |
| 12 |
#include <mrpt/config.h> |
| 13 |
#include <mrpt/hwdrivers/CImageGrabber_dc1394.h> |
| 14 |
|
| 15 |
// Include the libdc1394-2 headers: |
| 16 |
#if MRPT_HAS_LIBDC1394_2 |
| 17 |
#include <dc1394/control.h> |
| 18 |
#include <dc1394/conversions.h> |
| 19 |
#include <dc1394/register.h> |
| 20 |
#include <dc1394/utils.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
using namespace std; |
| 24 |
using namespace mrpt; |
| 25 |
using namespace mrpt::hwdrivers; |
| 26 |
|
| 27 |
#define THE_CAMERA static_cast<dc1394camera_t*>(m_dc1394camera) |
| 28 |
#define THE_CONTEXT static_cast<dc1394_t*>(m_dc1394_lib_context) |
| 29 |
|
| 30 |
/*------------------------------------------------------------- |
| 31 |
Constructor |
| 32 |
-------------------------------------------------------------*/ |
| 33 |
CImageGrabber_dc1394::CImageGrabber_dc1394( |
| 34 |
uint64_t cameraGUID, uint16_t cameraUnit, const TCaptureOptions_dc1394& options, bool verbose) : |
| 35 |
m_options(options) |
| 36 |
{ |
| 37 |
MRPT_START |
| 38 |
|
| 39 |
#if MRPT_HAS_LIBDC1394_2 |
| 40 |
// Open lib: |
| 41 |
m_dc1394_lib_context = dc1394_new(); |
| 42 |
ASSERT_(m_dc1394_lib_context); |
| 43 |
|
| 44 |
// Enumerate cameras: |
| 45 |
dc1394camera_list_t* list; |
| 46 |
dc1394error_t err; |
| 47 |
|
| 48 |
err = dc1394_camera_enumerate(THE_CONTEXT, &list); |
| 49 |
if (err != DC1394_SUCCESS) |
| 50 |
{ |
| 51 |
cerr << "[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras " |
| 52 |
"(Maybe your user has no rights to access IEEE1394?)." |
| 53 |
<< endl; |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if (!list->num) |
| 58 |
{ |
| 59 |
cerr << "[CImageGrabber_dc1394] ERROR: No cameras found." << endl; |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Open the first camera or the one given by the user: |
| 64 |
if (!cameraGUID) |
| 65 |
{ |
| 66 |
// Default: first one: |
| 67 |
m_dc1394camera = dc1394_camera_new(THE_CONTEXT, list->ids[0].guid); |
| 68 |
if (!THE_CAMERA) |
| 69 |
{ |
| 70 |
dc1394_camera_free_list(list); |
| 71 |
cerr << "[CImageGrabber_dc1394] ERROR: Failed to initialize camera " |
| 72 |
"with GUID " |
| 73 |
<< list->ids[0].guid << "\n"; |
| 74 |
return; |
| 75 |
} |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
// Look for user-given IDs: |
| 80 |
for (uint32_t i = 0; i < list->num; i++) |
| 81 |
{ |
| 82 |
if (list->ids[i].guid == cameraGUID && list->ids[i].unit == cameraUnit) |
| 83 |
{ |
| 84 |
m_dc1394camera = dc1394_camera_new_unit(THE_CONTEXT, cameraGUID, cameraUnit); |
| 85 |
if (!THE_CAMERA) |
| 86 |
{ |
| 87 |
dc1394_camera_free_list(list); |
| 88 |
cerr << "[CImageGrabber_dc1394] ERROR: Failed to " |
| 89 |
"initialize camera with GUID " |
| 90 |
<< list->ids[0].guid << "\n"; |
| 91 |
return; |
| 92 |
} |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (!m_dc1394camera) |
| 98 |
{ |
| 99 |
dc1394_camera_free_list(list); |
| 100 |
cerr << "[CImageGrabber_dc1394] ERROR: Camera with GUID=" << cameraGUID |
| 101 |
<< " and UNIT=" << cameraUnit << " not found.\n"; |
| 102 |
return; |
| 103 |
} |
| 104 |
} |
| 105 |
dc1394_camera_free_list(list); |
| 106 |
|
| 107 |
// Camera open OK: |
| 108 |
if (verbose) |
| 109 |
{ |
| 110 |
dc1394_camera_print_info(THE_CAMERA, stdout); |
| 111 |
} |
| 112 |
|
| 113 |
// get all supported modes: |
| 114 |
dc1394video_modes_t modes; |
| 115 |
err = dc1394_video_get_supported_modes(THE_CAMERA, &modes); |
| 116 |
if (err != DC1394_SUCCESS) |
| 117 |
{ |
| 118 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not get list of modes." << endl; |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
// Is mode7? treat differently: |
| 123 |
if (options.mode7 >= 0) |
| 124 |
{ |
| 125 |
m_desired_mode = DC1394_VIDEO_MODE_FORMAT7_MIN + options.mode7; |
| 126 |
if (verbose) cout << "[CImageGrabber_dc1394] Mode is mode7: " << options.mode7 << endl; |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
#define TEST_MODE(W, H, COLORMODEL) \ |
| 131 |
else if ( \ |
| 132 |
options.frame_width == W && options.frame_height == H && \ |
| 133 |
options.color_coding == COLOR_CODING_##COLORMODEL) m_desired_mode = \ |
| 134 |
DC1394_VIDEO_MODE_##W##x##H##_##COLORMODEL; |
| 135 |
|
| 136 |
if (false) |
| 137 |
{ |
| 138 |
} |
| 139 |
TEST_MODE(160, 120, YUV444) |
| 140 |
TEST_MODE(320, 240, YUV422) |
| 141 |
TEST_MODE(640, 480, YUV411) |
| 142 |
TEST_MODE(640, 480, YUV422) |
| 143 |
TEST_MODE(640, 480, RGB8) |
| 144 |
TEST_MODE(640, 480, MONO8) |
| 145 |
TEST_MODE(640, 480, MONO16) |
| 146 |
TEST_MODE(800, 600, YUV422) |
| 147 |
TEST_MODE(800, 600, RGB8) |
| 148 |
TEST_MODE(800, 600, MONO8) |
| 149 |
TEST_MODE(800, 600, MONO16) |
| 150 |
TEST_MODE(1024, 768, YUV422) |
| 151 |
TEST_MODE(1024, 768, RGB8) |
| 152 |
TEST_MODE(1024, 768, MONO8) |
| 153 |
TEST_MODE(1024, 768, MONO16) |
| 154 |
TEST_MODE(1280, 960, YUV422) |
| 155 |
TEST_MODE(1280, 960, RGB8) |
| 156 |
TEST_MODE(1280, 960, MONO8) |
| 157 |
TEST_MODE(1280, 960, MONO16) |
| 158 |
TEST_MODE(1600, 1200, YUV422) |
| 159 |
TEST_MODE(1600, 1200, RGB8) |
| 160 |
TEST_MODE(1600, 1200, MONO8) |
| 161 |
TEST_MODE(1600, 1200, MONO16) |
| 162 |
} |
| 163 |
// Display all supported modes and chosen: |
| 164 |
if (verbose) cout << "------ Supported video modes ------" << endl; |
| 165 |
bool valid_video_mode = false; |
| 166 |
for (uint32_t i = 0; i < modes.num; i++) |
| 167 |
{ |
| 168 |
string mode; |
| 169 |
switch (modes.modes[i]) |
| 170 |
{ |
| 171 |
case DC1394_VIDEO_MODE_160x120_YUV444: |
| 172 |
mode = "160x120_YUV444"; |
| 173 |
break; |
| 174 |
case DC1394_VIDEO_MODE_320x240_YUV422: |
| 175 |
mode = "320x240_YUV422"; |
| 176 |
break; |
| 177 |
case DC1394_VIDEO_MODE_640x480_YUV411: |
| 178 |
mode = "640x480_YUV411"; |
| 179 |
break; |
| 180 |
case DC1394_VIDEO_MODE_640x480_YUV422: |
| 181 |
mode = "640x480_YUV422"; |
| 182 |
break; |
| 183 |
case DC1394_VIDEO_MODE_640x480_RGB8: |
| 184 |
mode = "640x480_RGB8"; |
| 185 |
break; |
| 186 |
case DC1394_VIDEO_MODE_640x480_MONO8: |
| 187 |
mode = "640x480_MONO8"; |
| 188 |
break; |
| 189 |
case DC1394_VIDEO_MODE_640x480_MONO16: |
| 190 |
mode = "640x480_MONO16"; |
| 191 |
break; |
| 192 |
case DC1394_VIDEO_MODE_800x600_YUV422: |
| 193 |
mode = "800x600_YUV422"; |
| 194 |
break; |
| 195 |
case DC1394_VIDEO_MODE_800x600_RGB8: |
| 196 |
mode = "800x600_RGB8"; |
| 197 |
break; |
| 198 |
case DC1394_VIDEO_MODE_800x600_MONO8: |
| 199 |
mode = "800x600_MONO8"; |
| 200 |
break; |
| 201 |
case DC1394_VIDEO_MODE_1024x768_YUV422: |
| 202 |
mode = "1024x768_YUV422"; |
| 203 |
break; |
| 204 |
case DC1394_VIDEO_MODE_1024x768_RGB8: |
| 205 |
mode = "1024x768_RGB8"; |
| 206 |
break; |
| 207 |
case DC1394_VIDEO_MODE_1024x768_MONO8: |
| 208 |
mode = "1024x768_MONO8"; |
| 209 |
break; |
| 210 |
case DC1394_VIDEO_MODE_800x600_MONO16: |
| 211 |
mode = "800x600_MONO16"; |
| 212 |
break; |
| 213 |
case DC1394_VIDEO_MODE_1024x768_MONO16: |
| 214 |
mode = "1024x768_MONO16"; |
| 215 |
break; |
| 216 |
case DC1394_VIDEO_MODE_1280x960_YUV422: |
| 217 |
mode = "1280x960_YUV422"; |
| 218 |
break; |
| 219 |
case DC1394_VIDEO_MODE_1280x960_RGB8: |
| 220 |
mode = "1280x960_RGB8"; |
| 221 |
break; |
| 222 |
case DC1394_VIDEO_MODE_1280x960_MONO8: |
| 223 |
mode = "1280x960_MONO8"; |
| 224 |
break; |
| 225 |
case DC1394_VIDEO_MODE_1600x1200_YUV422: |
| 226 |
mode = "1600x1200_YUV422"; |
| 227 |
break; |
| 228 |
case DC1394_VIDEO_MODE_1600x1200_RGB8: |
| 229 |
mode = "1600x1200_RGB8"; |
| 230 |
break; |
| 231 |
case DC1394_VIDEO_MODE_1600x1200_MONO8: |
| 232 |
mode = "1600x1200_MONO8"; |
| 233 |
break; |
| 234 |
case DC1394_VIDEO_MODE_1280x960_MONO16: |
| 235 |
mode = "1280x960_MONO16"; |
| 236 |
break; |
| 237 |
case DC1394_VIDEO_MODE_1600x1200_MONO16: |
| 238 |
mode = "1600x1200_MONO16"; |
| 239 |
break; |
| 240 |
case DC1394_VIDEO_MODE_EXIF: |
| 241 |
mode = "EXIF"; |
| 242 |
break; |
| 243 |
case DC1394_VIDEO_MODE_FORMAT7_0: |
| 244 |
mode = "FORMAT7_0"; |
| 245 |
break; |
| 246 |
case DC1394_VIDEO_MODE_FORMAT7_1: |
| 247 |
mode = "FORMAT7_1"; |
| 248 |
break; |
| 249 |
case DC1394_VIDEO_MODE_FORMAT7_2: |
| 250 |
mode = "FORMAT7_2"; |
| 251 |
break; |
| 252 |
case DC1394_VIDEO_MODE_FORMAT7_3: |
| 253 |
mode = "FORMAT7_3"; |
| 254 |
break; |
| 255 |
case DC1394_VIDEO_MODE_FORMAT7_4: |
| 256 |
mode = "FORMAT7_4"; |
| 257 |
break; |
| 258 |
case DC1394_VIDEO_MODE_FORMAT7_5: |
| 259 |
mode = "FORMAT7_5"; |
| 260 |
break; |
| 261 |
case DC1394_VIDEO_MODE_FORMAT7_6: |
| 262 |
mode = "FORMAT7_6"; |
| 263 |
break; |
| 264 |
case DC1394_VIDEO_MODE_FORMAT7_7: |
| 265 |
mode = "FORMAT7_7"; |
| 266 |
break; |
| 267 |
default: |
| 268 |
cerr << "[CImageGrabber_dc1394] ERROR: Requested video mode is " |
| 269 |
"not valid." |
| 270 |
<< endl; |
| 271 |
return; |
| 272 |
} |
| 273 |
if (modes.modes[i] == m_desired_mode) valid_video_mode = true; |
| 274 |
if (verbose) |
| 275 |
{ |
| 276 |
if (modes.modes[i] == m_desired_mode) |
| 277 |
cout << mode << " (*)" << endl; |
| 278 |
else |
| 279 |
cout << mode << endl; |
| 280 |
} |
| 281 |
} |
| 282 |
if (!valid_video_mode) |
| 283 |
{ |
| 284 |
cerr << mrpt::format( |
| 285 |
"[CImageGrabber_dc1394] ERROR: Requested mode %ix%i " |
| 286 |
"color_model:%i is not available for this camera.", |
| 287 |
options.frame_width, options.frame_height, int(options.color_coding)) |
| 288 |
<< endl; |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
// Reset to bus just in case: |
| 293 |
// And only once in a program, at start up: |
| 294 |
// static bool reset_1394bus = true; |
| 295 |
// if (reset_1394bus) |
| 296 |
{ |
| 297 |
// reset_1394bus = false; |
| 298 |
// dc1394_reset_bus(THE_CAMERA); |
| 299 |
} |
| 300 |
|
| 301 |
/*----------------------------------------------------------------------- |
| 302 |
* setup capture |
| 303 |
*-----------------------------------------------------------------------*/ |
| 304 |
const int SIZE_RING_BUFFER = options.ring_buffer_size; |
| 305 |
|
| 306 |
err = dc1394_video_set_iso_speed(THE_CAMERA, DC1394_ISO_SPEED_400); |
| 307 |
if (err != DC1394_SUCCESS) |
| 308 |
{ |
| 309 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not set iso speed." << endl; |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
err = dc1394_video_set_mode(THE_CAMERA, dc1394video_mode_t(m_desired_mode)); |
| 314 |
// This checking only assures that m_desired_mode is inside |
| 315 |
// dc1394video_mode_t enum range |
| 316 |
if (err != DC1394_SUCCESS) |
| 317 |
{ |
| 318 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not set video mode." << endl; |
| 319 |
return; |
| 320 |
} |
| 321 |
|
| 322 |
dc1394framerate_t the_framerate; |
| 323 |
switch (m_options.framerate) |
| 324 |
{ |
| 325 |
case FRAMERATE_1_875: |
| 326 |
the_framerate = DC1394_FRAMERATE_1_875; |
| 327 |
break; |
| 328 |
case FRAMERATE_3_75: |
| 329 |
the_framerate = DC1394_FRAMERATE_3_75; |
| 330 |
break; |
| 331 |
case FRAMERATE_7_5: |
| 332 |
the_framerate = DC1394_FRAMERATE_7_5; |
| 333 |
break; |
| 334 |
case FRAMERATE_15: |
| 335 |
the_framerate = DC1394_FRAMERATE_15; |
| 336 |
break; |
| 337 |
case FRAMERATE_30: |
| 338 |
the_framerate = DC1394_FRAMERATE_30; |
| 339 |
break; |
| 340 |
case FRAMERATE_60: |
| 341 |
the_framerate = DC1394_FRAMERATE_60; |
| 342 |
break; |
| 343 |
case FRAMERATE_120: |
| 344 |
the_framerate = DC1394_FRAMERATE_120; |
| 345 |
break; |
| 346 |
case FRAMERATE_240: |
| 347 |
the_framerate = DC1394_FRAMERATE_240; |
| 348 |
break; |
| 349 |
|
| 350 |
default: |
| 351 |
cerr << "[CImageGrabber_dc1394] ERROR: Requested framerate is not " |
| 352 |
"valid." |
| 353 |
<< endl; |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
err = dc1394_video_set_framerate(THE_CAMERA, the_framerate); |
| 358 |
if (err != DC1394_SUCCESS) |
| 359 |
{ |
| 360 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not set framerate." << endl; |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
err = dc1394_capture_setup(THE_CAMERA, SIZE_RING_BUFFER, DC1394_CAPTURE_FLAGS_DEFAULT); |
| 365 |
if (err != DC1394_SUCCESS) |
| 366 |
{ |
| 367 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not setup camera-\nmake " |
| 368 |
"sure that the video mode and framerate are\nsupported by your " |
| 369 |
"camera." |
| 370 |
<< endl; |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
cout << "------ Other options ------" << endl; |
| 375 |
uint32_t iso_chan; |
| 376 |
if ((err = dc1394_video_get_iso_channel(THE_CAMERA, &iso_chan)) == DC1394_SUCCESS) |
| 377 |
if (verbose) cout << "ISO Channel: " << iso_chan << endl; |
| 378 |
|
| 379 |
dc1394speed_t iso_speed; |
| 380 |
if ((err = dc1394_video_get_iso_speed(THE_CAMERA, &iso_speed)) == DC1394_SUCCESS) |
| 381 |
if (verbose) cout << "ISO Speed: " << iso_speed << endl; |
| 382 |
|
| 383 |
// set trigger options: |
| 384 |
#define SET_TRIGGER(opt, OPT, TYPE) \ |
| 385 |
if (options.trigger_##opt >= 0) \ |
| 386 |
{ \ |
| 387 |
err = dc1394_external_trigger_set_##opt( \ |
| 388 |
THE_CAMERA, static_cast<dc1394trigger_##opt##_t>( \ |
| 389 |
DC1394_TRIGGER_##TYPE##_MIN + options.trigger_##opt)); \ |
| 390 |
DC1394_WRN( \ |
| 391 |
err, \ |
| 392 |
"[CImageGrabber_dc1394::changeCaptureOptions] Could not set " \ |
| 393 |
"trigger opt"); \ |
| 394 |
} |
| 395 |
SET_TRIGGER(mode, MODE, MODE) |
| 396 |
SET_TRIGGER(source, SOURCE, SOURCE) |
| 397 |
SET_TRIGGER(polarity, POLARITY, ACTIVE) |
| 398 |
if (options.trigger_power >= 0) |
| 399 |
{ |
| 400 |
err = dc1394_external_trigger_set_power(THE_CAMERA, dc1394switch_t(options.trigger_power)); |
| 401 |
DC1394_WRN( |
| 402 |
err, |
| 403 |
"[CImageGrabber_dc1394::changeCaptureOptions] Could not set " |
| 404 |
"trigger power"); |
| 405 |
} |
| 406 |
#undef SET_TRIGGER |
| 407 |
|
| 408 |
/*----------------------------------------------------------------------- |
| 409 |
* have the camera start sending us data |
| 410 |
*-----------------------------------------------------------------------*/ |
| 411 |
err = dc1394_video_set_transmission(THE_CAMERA, DC1394_ON); |
| 412 |
if (err != DC1394_SUCCESS) |
| 413 |
{ |
| 414 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not start camera iso " |
| 415 |
"transmission." |
| 416 |
<< endl; |
| 417 |
return; |
| 418 |
} |
| 419 |
|
| 420 |
// remember that we successfully initialized everything |
| 421 |
m_bInitialized = true; |
| 422 |
|
| 423 |
changeCaptureOptions(m_options); |
| 424 |
|
| 425 |
// Camera current features: |
| 426 |
if (verbose) |
| 427 |
{ |
| 428 |
dc1394featureset_t features; |
| 429 |
if ((err = dc1394_feature_get_all(THE_CAMERA, &features)) == DC1394_SUCCESS) |
| 430 |
dc1394_feature_print_all(&features, stdout); |
| 431 |
} |
| 432 |
|
| 433 |
#else |
| 434 |
THROW_EXCEPTION( |
| 435 |
"[CImageGrabber_dc1394] ERROR: MRPT compiled with " |
| 436 |
"MRPT_HAS_LIBDC1394_2=0 !"); |
| 437 |
#endif |
| 438 |
MRPT_END |
| 439 |
} |
| 440 |
|
| 441 |
/*------------------------------------------------------------- |
| 442 |
Destructor |
| 443 |
-------------------------------------------------------------*/ |
| 444 |
CImageGrabber_dc1394::~CImageGrabber_dc1394() |
| 445 |
{ |
| 446 |
#if MRPT_HAS_LIBDC1394_2 |
| 447 |
m_bInitialized = false; |
| 448 |
if (THE_CAMERA) |
| 449 |
{ |
| 450 |
dc1394_video_set_transmission(THE_CAMERA, DC1394_OFF); |
| 451 |
dc1394_capture_stop(THE_CAMERA); |
| 452 |
|
| 453 |
// Release BW: |
| 454 |
// uint32_t val; |
| 455 |
// if (dc1394_video_get_bandwidth_usage(THE_CAMERA, &val) == |
| 456 |
// DC1394_SUCCESS) |
| 457 |
// dc1394_iso_release_bandwidth(THE_CAMERA, val); |
| 458 |
// if ( dc1394_video_get_iso_channel(THE_CAMERA, &val) == |
| 459 |
// DC1394_SUCCESS) |
| 460 |
// dc1394_iso_release_channel(THE_CAMERA, val); |
| 461 |
|
| 462 |
dc1394_camera_free(THE_CAMERA); |
| 463 |
} |
| 464 |
if (THE_CONTEXT) |
| 465 |
{ |
| 466 |
dc1394_free(THE_CONTEXT); |
| 467 |
} |
| 468 |
#endif |
| 469 |
} |
| 470 |
|
| 471 |
/*------------------------------------------------------------- |
| 472 |
get the image - MONO |
| 473 |
-------------------------------------------------------------*/ |
| 474 |
bool CImageGrabber_dc1394::getObservation(mrpt::obs::CObservationImage& out_observation) |
| 475 |
{ |
| 476 |
MRPT_START |
| 477 |
|
| 478 |
if (!m_bInitialized) return false; |
| 479 |
|
| 480 |
#if MRPT_HAS_LIBDC1394_2 |
| 481 |
dc1394video_frame_t* frame = nullptr; |
| 482 |
dc1394error_t err; |
| 483 |
|
| 484 |
err = dc1394_video_set_transmission(THE_CAMERA, DC1394_ON); |
| 485 |
if (err != DC1394_SUCCESS) |
| 486 |
{ |
| 487 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not start camera iso " |
| 488 |
"transmission." |
| 489 |
<< endl; |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
// get frame from ring buffer: |
| 494 |
err = dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame); |
| 495 |
// dc1394error_t err=dc1394_capture_dequeue(THE_CAMERA, |
| 496 |
// DC1394_CAPTURE_POLICY_POLL, &frame); |
| 497 |
if (err != DC1394_SUCCESS) |
| 498 |
{ |
| 499 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not capture a frame" << endl; |
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
out_observation.timestamp = mrpt::Clock::now(); |
| 504 |
|
| 505 |
const unsigned int width = frame->size[0]; |
| 506 |
const unsigned int height = frame->size[1]; |
| 507 |
|
| 508 |
if (!m_options.deinterlace_stereo) |
| 509 |
{ |
| 510 |
/*----------------------------------------------------------------------- |
| 511 |
* convert the image from what ever format it is to its RGB8 |
| 512 |
*-----------------------------------------------------------------------*/ |
| 513 |
// dc1394_get_image_size_from_video_mode(THE_CAMERA, m_desired_mode, |
| 514 |
// &width, &height); |
| 515 |
|
| 516 |
auto* new_frame = static_cast<dc1394video_frame_t*>(calloc(1, sizeof(dc1394video_frame_t))); |
| 517 |
new_frame->color_coding = DC1394_COLOR_CODING_RGB8; |
| 518 |
dc1394_convert_frames(frame, new_frame); |
| 519 |
|
| 520 |
// Fill the output class: |
| 521 |
out_observation.image.loadFromMemoryBuffer( |
| 522 |
width, height, true, new_frame->image, true /* BGR -> RGB */); |
| 523 |
|
| 524 |
// Free temporary frame: |
| 525 |
free(new_frame->image); |
| 526 |
free(new_frame); |
| 527 |
} |
| 528 |
else |
| 529 |
{ |
| 530 |
// Stereo images: |
| 531 |
auto* imageBuf = new uint8_t[width * height * 2]; |
| 532 |
auto* imageBufRGB = new uint8_t[width * height * 2 * 3]; |
| 533 |
|
| 534 |
if ((err = dc1394_deinterlace_stereo(frame->image, imageBuf, width, 2 * height)) != |
| 535 |
DC1394_SUCCESS) |
| 536 |
{ |
| 537 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not deinterlace " |
| 538 |
"stereo images: " |
| 539 |
<< err << endl; |
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
if ((err = dc1394_bayer_decoding_8bit( |
| 544 |
imageBuf, imageBufRGB, width, 2 * height, |
| 545 |
DC1394_COLOR_FILTER_GBRG, // Has to be this value for |
| 546 |
// Bumblebee! |
| 547 |
DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS) |
| 548 |
{ |
| 549 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not apply Bayer " |
| 550 |
"conversion: " |
| 551 |
<< err << endl; |
| 552 |
return false; |
| 553 |
} |
| 554 |
|
| 555 |
out_observation.image.loadFromMemoryBuffer(width, height, true, |
| 556 |
imageBufRGB); // Left cam. |
| 557 |
// out_observation.image.loadFromMemoryBuffer(width,height,true, |
| 558 |
// imageBufRGB+ width*height*3 ); // Right cam. |
| 559 |
|
| 560 |
delete[] imageBuf; |
| 561 |
delete[] imageBufRGB; |
| 562 |
} |
| 563 |
|
| 564 |
// Now we can return the frame to the ring buffer: |
| 565 |
err = dc1394_capture_enqueue(THE_CAMERA, frame); |
| 566 |
if (err != DC1394_SUCCESS) |
| 567 |
{ |
| 568 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not enqueue the ring " |
| 569 |
"buffer frame" |
| 570 |
<< endl; |
| 571 |
return false; |
| 572 |
} |
| 573 |
|
| 574 |
return true; |
| 575 |
#else |
| 576 |
THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !"); |
| 577 |
#endif |
| 578 |
MRPT_END |
| 579 |
} |
| 580 |
|
| 581 |
/*------------------------------------------------------------- |
| 582 |
get the image - STEREO |
| 583 |
-------------------------------------------------------------*/ |
| 584 |
bool CImageGrabber_dc1394::getObservation(mrpt::obs::CObservationStereoImages& out_observation) |
| 585 |
{ |
| 586 |
MRPT_START |
| 587 |
|
| 588 |
if (!m_bInitialized) return false; |
| 589 |
|
| 590 |
#if MRPT_HAS_LIBDC1394_2 |
| 591 |
dc1394video_frame_t* frame = nullptr; |
| 592 |
|
| 593 |
// get frame from ring buffer: |
| 594 |
dc1394error_t err = dc1394_capture_dequeue(THE_CAMERA, DC1394_CAPTURE_POLICY_WAIT, &frame); |
| 595 |
if (err != DC1394_SUCCESS) |
| 596 |
{ |
| 597 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not capture a frame" << endl; |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
out_observation.timestamp = mrpt::Clock::now(); |
| 602 |
|
| 603 |
const unsigned int width = frame->size[0]; |
| 604 |
const unsigned int height = frame->size[1]; |
| 605 |
|
| 606 |
if (!m_options.deinterlace_stereo) |
| 607 |
{ |
| 608 |
THROW_EXCEPTION( |
| 609 |
"Call to getObservation(stereo) but the camera was not set as " |
| 610 |
"stereo!"); |
| 611 |
} |
| 612 |
else |
| 613 |
{ |
| 614 |
// Stereo images: |
| 615 |
auto* imageBuf = new uint8_t[width * height * 2]; |
| 616 |
auto* imageBufRGB = new uint8_t[width * height * 2 * 3]; |
| 617 |
|
| 618 |
if ((err = dc1394_deinterlace_stereo(frame->image, imageBuf, width, 2 * height)) != |
| 619 |
DC1394_SUCCESS) |
| 620 |
{ |
| 621 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not deinterlace " |
| 622 |
"stereo images: " |
| 623 |
<< err << endl; |
| 624 |
return false; |
| 625 |
} |
| 626 |
|
| 627 |
if ((err = dc1394_bayer_decoding_8bit( |
| 628 |
imageBuf, imageBufRGB, width, 2 * height, |
| 629 |
DC1394_COLOR_FILTER_GBRG, // Has to be this value for |
| 630 |
// Bumblebee! |
| 631 |
DC1394_BAYER_METHOD_HQLINEAR)) != DC1394_SUCCESS) |
| 632 |
{ |
| 633 |
cerr << "[CImageGrabber_dc1394] ERROR: Could not apply Bayer " |
| 634 |
"conversion: " |
| 635 |
<< err << endl; |
| 636 |
return false; |
| 637 |
} |
| 638 |
|
| 639 |
out_observation.hasImageRight = true; |
| 640 |
out_observation.imageLeft.loadFromMemoryBuffer(width, height, true, |
| 641 |
imageBufRGB); // Left cam. |
| 642 |
out_observation.imageRight.loadFromMemoryBuffer( |
| 643 |
width, height, true, |
| 644 |
imageBufRGB + width * height * 3); // Right cam. |
| 645 |
|
| 646 |
delete[] imageBuf; |
| 647 |
delete[] imageBufRGB; |
| 648 |
} |
| 649 |
|
| 650 |
// Now we can return the frame to the ring buffer: |
| 651 |
dc1394_capture_enqueue(THE_CAMERA, frame); |
| 652 |
|
| 653 |
return true; |
| 654 |
#else |
| 655 |
THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !"); |
| 656 |
#endif |
| 657 |
MRPT_END |
| 658 |
} |
| 659 |
|
| 660 |
/*------------------------------------------------------------- |
| 661 |
changeCaptureOptions |
| 662 |
-------------------------------------------------------------*/ |
| 663 |
bool CImageGrabber_dc1394::changeCaptureOptions(const TCaptureOptions_dc1394& options) |
| 664 |
{ |
| 665 |
MRPT_START |
| 666 |
|
| 667 |
if (!m_bInitialized) return false; |
| 668 |
|
| 669 |
#if MRPT_HAS_LIBDC1394_2 |
| 670 |
dc1394error_t err; |
| 671 |
// set features modes: |
| 672 |
#define SET_MODE(feat, FEAT) \ |
| 673 |
if (options.feat##_mode >= 0) \ |
| 674 |
{ \ |
| 675 |
err = dc1394_feature_set_mode( \ |
| 676 |
THE_CAMERA, DC1394_FEATURE_##FEAT, \ |
| 677 |
static_cast<dc1394feature_mode_t>(DC1394_FEATURE_MODE_MIN + options.feat##_mode)); \ |
| 678 |
DC1394_WRN( \ |
| 679 |
err, \ |
| 680 |
"[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat " \ |
| 681 |
"mode"); \ |
| 682 |
} |
| 683 |
SET_MODE(shutter, SHUTTER) |
| 684 |
SET_MODE(gain, GAIN) |
| 685 |
SET_MODE(gamma, GAMMA) |
| 686 |
SET_MODE(brightness, BRIGHTNESS) |
| 687 |
SET_MODE(exposure, EXPOSURE) |
| 688 |
SET_MODE(sharpness, SHARPNESS) |
| 689 |
SET_MODE(white_balance, WHITE_BALANCE) |
| 690 |
#undef SET_MODE |
| 691 |
|
| 692 |
// Set features values: |
| 693 |
#define SET_VALUE(feat, FEAT) \ |
| 694 |
if (options.feat >= 0) \ |
| 695 |
{ \ |
| 696 |
err = dc1394_feature_set_value(THE_CAMERA, DC1394_FEATURE_##FEAT, options.feat); \ |
| 697 |
DC1394_WRN( \ |
| 698 |
err, \ |
| 699 |
"[CImageGrabber_dc1394::changeCaptureOptions] Could not set feat " \ |
| 700 |
"value"); \ |
| 701 |
} |
| 702 |
SET_VALUE(shutter, SHUTTER) |
| 703 |
SET_VALUE(gain, GAIN) |
| 704 |
SET_VALUE(gamma, GAMMA) |
| 705 |
SET_VALUE(brightness, BRIGHTNESS) |
| 706 |
SET_VALUE(exposure, EXPOSURE) |
| 707 |
SET_VALUE(sharpness, SHARPNESS) |
| 708 |
SET_VALUE(white_balance, WHITE_BALANCE) |
| 709 |
#undef SET_VALUE |
| 710 |
|
| 711 |
return true; |
| 712 |
#else |
| 713 |
THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !"); |
| 714 |
#endif |
| 715 |
MRPT_END |
| 716 |
} |
| 717 |
|
| 718 |
/*------------------------------------------------------------- |
| 719 |
setSoftwareTriggerLevel |
| 720 |
-------------------------------------------------------------*/ |
| 721 |
bool CImageGrabber_dc1394::setSoftwareTriggerLevel(bool level) |
| 722 |
{ |
| 723 |
MRPT_START |
| 724 |
|
| 725 |
if (!m_bInitialized) return false; |
| 726 |
|
| 727 |
#if MRPT_HAS_LIBDC1394_2 |
| 728 |
dc1394error_t err; |
| 729 |
err = dc1394_software_trigger_set_power(THE_CAMERA, (dc1394switch_t)level); |
| 730 |
DC1394_WRN( |
| 731 |
err, |
| 732 |
"[CImageGrabber_dc1394::setSoftwareTriggerLevel] Could not set " |
| 733 |
"software trigger level"); |
| 734 |
|
| 735 |
return true; |
| 736 |
#else |
| 737 |
THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !"); |
| 738 |
#endif |
| 739 |
MRPT_END |
| 740 |
} |
| 741 |
|
| 742 |
/** Generates a list with the information on all the existing (Firewire) cameras |
| 743 |
* in the system. |
| 744 |
* \exception std::runtime_error On any error calling libdc1394. |
| 745 |
*/ |
| 746 |
void CImageGrabber_dc1394::enumerateCameras(TCameraInfoList& out_list) |
| 747 |
{ |
| 748 |
MRPT_START |
| 749 |
#if MRPT_HAS_LIBDC1394_2 |
| 750 |
|
| 751 |
dc1394_t* lib_context = nullptr; |
| 752 |
dc1394camera_list_t* list = nullptr; |
| 753 |
out_list.clear(); |
| 754 |
|
| 755 |
try |
| 756 |
{ |
| 757 |
lib_context = dc1394_new(); |
| 758 |
if (!lib_context) |
| 759 |
throw std::runtime_error( |
| 760 |
"[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras " |
| 761 |
"(Maybe your user has no rights to access IEEE1394?)."); |
| 762 |
|
| 763 |
// Enumerate cameras: |
| 764 |
dc1394error_t err; |
| 765 |
|
| 766 |
err = dc1394_camera_enumerate(lib_context, &list); |
| 767 |
if (err != DC1394_SUCCESS) |
| 768 |
throw std::runtime_error( |
| 769 |
"[CImageGrabber_dc1394] ERROR: Failed to enumerate cameras " |
| 770 |
"(Maybe your user has no rights to access IEEE1394?)."); |
| 771 |
|
| 772 |
for (unsigned int i = 0; i < list->num; i++) |
| 773 |
{ |
| 774 |
TCameraInfo info; |
| 775 |
|
| 776 |
info.guid = list->ids[i].guid; |
| 777 |
info.unit = list->ids[i].unit; |
| 778 |
|
| 779 |
// Try to open it: |
| 780 |
dc1394camera_t* cam = |
| 781 |
dc1394_camera_new_unit(lib_context, list->ids[i].guid, list->ids[i].unit); |
| 782 |
if (!cam) |
| 783 |
throw std::runtime_error(format( |
| 784 |
"[CImageGrabber_dc1394] ERROR: Failed to query camera " |
| 785 |
"with GUID %u\n", |
| 786 |
static_cast<unsigned int>(list->ids[i].guid))); |
| 787 |
|
| 788 |
info.unit_spec_ID = cam->unit_spec_ID; |
| 789 |
info.unit_sw_version = cam->unit_sw_version; |
| 790 |
info.unit_sub_sw_version = cam->unit_sub_sw_version; |
| 791 |
info.command_registers_base = cam->command_registers_base; |
| 792 |
info.unit_directory = cam->unit_directory; |
| 793 |
info.unit_dependent_directory = cam->unit_dependent_directory; |
| 794 |
info.advanced_features_csr = cam->advanced_features_csr; |
| 795 |
info.PIO_control_csr = cam->PIO_control_csr; |
| 796 |
info.SIO_control_csr = cam->SIO_control_csr; |
| 797 |
info.strobe_control_csr = cam->strobe_control_csr; |
| 798 |
for (int j = 0; j < DC1394_VIDEO_MODE_FORMAT7_NUM; j++) |
| 799 |
info.format7_csr[j] = cam->format7_csr[j]; |
| 800 |
info.iidc_version = cam->iidc_version; |
| 801 |
info.vendor = std::string(cam->vendor ? cam->vendor : ""); |
| 802 |
info.model = std::string(cam->model ? cam->model : ""); |
| 803 |
info.vendor_id = cam->vendor_id; |
| 804 |
info.model_id = cam->model_id; |
| 805 |
info.bmode_capable = cam->bmode_capable; |
| 806 |
info.one_shot_capable = cam->one_shot_capable; |
| 807 |
info.multi_shot_capable = cam->multi_shot_capable; |
| 808 |
info.can_switch_on_off = cam->can_switch_on_off; |
| 809 |
info.has_vmode_error_status = cam->has_vmode_error_status; |
| 810 |
info.has_feature_error_status = cam->has_feature_error_status; |
| 811 |
info.max_mem_channel = cam->max_mem_channel; |
| 812 |
|
| 813 |
// dc1394_camera_print_info(cam,stdout); |
| 814 |
|
| 815 |
dc1394_camera_free(cam); // Close camera |
| 816 |
|
| 817 |
out_list.push_back(info); |
| 818 |
} |
| 819 |
|
| 820 |
// Free context: |
| 821 |
dc1394_free(lib_context); |
| 822 |
lib_context = nullptr; |
| 823 |
dc1394_camera_free_list(list); |
| 824 |
list = nullptr; |
| 825 |
} |
| 826 |
catch (...) |
| 827 |
{ |
| 828 |
if (list) dc1394_camera_free_list(list); |
| 829 |
if (lib_context) dc1394_free(lib_context); |
| 830 |
throw; |
| 831 |
} |
| 832 |
#else |
| 833 |
THROW_EXCEPTION("MRPT has been compiled with MRPT_HAS_LIBDC1394_2=0 !"); |
| 834 |
#endif |
| 835 |
MRPT_END |
| 836 |
} |