60 #define NUM_DMA_BUFFERS 4 64 #define CAM_EXCEPT(except, msg) \ 67 snprintf(buf, 100, "[Camera1394Stereo::%s]: " msg, __FUNCTION__); \ 72 #define CAM_EXCEPT_ARGS(except, msg, ...) \ 75 snprintf(buf, 100, "[Camera1394Stereo::%s]: " msg, __FUNCTION__, __VA_ARGS__); \ 97 BayerPattern_ = (dc1394color_filter_t) DC1394_COLOR_FILTER_NUM;
98 if (0 == strcmp(bayer,
"bggr"))
102 else if (0 == strcmp(bayer,
"grbg"))
106 else if (0 == strcmp(bayer,
"rggb"))
110 else if (0 == strcmp(bayer,
"gbrg"))
114 else if (0 != strcmp(bayer,
""))
116 ROS_ERROR(
"unknown bayer pattern [%s]", bayer);
123 bool DoBayer =
false;
124 if (0 != strcmp(method,
"")
129 ROS_WARN(
"[%s] Bayer decoding in the driver is DEPRECATED;" 130 " image_proc decoding preferred.", method);
133 if (!strcmp(method,
"DownSample"))
135 else if (!strcmp(method,
"Simple"))
137 else if (!strcmp(method,
"Bilinear"))
139 else if (!strcmp(method,
"HQ"))
141 else if (!strcmp(method,
"VNG"))
143 else if (!strcmp(method,
"AHD"))
147 ROS_ERROR(
"Unknown Bayer method [%s]. Using ROS image_proc instead.",
157 bool doStereo =
true;
158 if (0 == strcmp(method,
"Interlaced"))
162 else if (0 == strcmp(method,
"Field"))
190 const char *guid = newconfig.guid.c_str();
193 dc1394camera_list_t *list;
200 "Could not initialize dc1394_context.\n" 201 "Make sure /dev/raw1394 exists, you have access permission,\n" 202 "and libraw1394 development package is installed.");
205 err = dc1394_camera_enumerate(d, &list);
206 if (err != DC1394_SUCCESS)
208 CAM_EXCEPT(camera1394stereo::Exception,
"Could not get camera list");
214 CAM_EXCEPT(camera1394stereo::Exception,
"No cameras found");
218 char* temp=(
char*)malloc(1024*
sizeof(
char));
219 for (
unsigned i=0; i < list->num; i++)
222 camera_ = dc1394_camera_new (d, list->ids[i].guid);
225 << std::hex << list->ids[i].guid);
228 << std::hex << list->ids[i].guid);
232 value[0]=
camera_->guid & 0xffffffff;
233 value[1]= (
camera_->guid >>32) & 0x000000ff;
234 value[2]= (
camera_->guid >>40) & 0xfffff;
236 sprintf(temp,
"%06x%02x%08x", value[2], value[1], value[0]);
238 if (strcmp(guid,
"")==0)
246 ROS_DEBUG(
"Comparing %s to %s",guid,temp);
247 if (strcmp(temp,guid)==0)
255 dc1394_camera_free_list (list);
259 if (strcmp(guid,
"")==0)
261 CAM_EXCEPT(camera1394stereo::Exception,
"Could not find camera");
266 "Could not find camera with guid %s", guid);
279 if (newconfig.reset_on_open
280 && DC1394_SUCCESS != dc1394_camera_reset(
camera_))
283 ROS_WARN(
"Unable to reset camera (continuing).");
291 "Unable to set ISO speed; is the camera plugged in?");
300 CAM_EXCEPT(camera1394stereo::Exception,
"Failed to set video mode");
312 if (dc1394_is_video_mode_scalable(
videoMode_) == DC1394_TRUE)
318 CAM_EXCEPT(camera1394stereo::Exception,
"Format7 start failed");
328 CAM_EXCEPT(camera1394stereo::Exception,
"Failed to set frame rate");
336 if (DC1394_SUCCESS !=
340 CAM_EXCEPT(camera1394stereo::Exception,
"Format7 start failed");
350 DC1394_CAPTURE_FLAGS_DEFAULT))
353 CAM_EXCEPT(camera1394stereo::Exception,
"Failed to open device!");
358 if (DC1394_SUCCESS != dc1394_video_set_transmission(
camera_, DC1394_ON))
361 CAM_EXCEPT(camera1394stereo::Exception,
"Failed to start device!");
394 if (DC1394_SUCCESS != dc1394_video_set_transmission(
camera_, DC1394_OFF)
395 || DC1394_SUCCESS != dc1394_capture_stop(
camera_))
405 std::string
bayer_string(dc1394color_filter_t pattern,
unsigned int bits)
411 case DC1394_COLOR_FILTER_RGGB:
413 case DC1394_COLOR_FILTER_GBRG:
415 case DC1394_COLOR_FILTER_GRBG:
417 case DC1394_COLOR_FILTER_BGGR:
434 sensor_msgs::Image& image,
435 sensor_msgs::Image& image2)
439 dc1394video_frame_t * frame = NULL;
443 dc1394_capture_dequeue (
camera_, DC1394_CAPTURE_POLICY_POLL, &frame);
444 if (!frame)
return false;
449 dc1394_capture_dequeue (
camera_, DC1394_CAPTURE_POLICY_WAIT, &frame);
452 CAM_EXCEPT(camera1394stereo::Exception,
"Unable to capture frame");
458 dc1394video_frame_t frame1 = *frame;
463 size_t frame1_size = frame->total_bytes;
464 frame1.image = (
unsigned char *) malloc(frame1_size);
465 frame1.allocated_image_bytes = frame1_size;
466 switch (frame->color_coding)
468 case DC1394_COLOR_CODING_MONO16:
469 frame1.color_coding = DC1394_COLOR_CODING_MONO8;
471 case DC1394_COLOR_CODING_RAW16:
472 frame1.color_coding = DC1394_COLOR_CODING_RAW8;
475 ROS_WARN(
"Stereo extract in a non 16bit video mode");
477 int err = dc1394_deinterlace_stereo_frames(frame, &frame1,
stereoMethod_);
478 if (err != DC1394_SUCCESS)
481 dc1394_capture_enqueue(
camera_, frame);
482 CAM_EXCEPT(camera1394stereo::Exception,
"Could not extract stereo frames");
487 uint8_t* capture_buffer =
reinterpret_cast<uint8_t *
>(frame1.image);
492 dc1394video_frame_t frame2;
493 size_t frame2_size = (frame1.size[0] * frame1.size[1]
494 * 3 *
sizeof(
unsigned char));
495 frame2.image = (
unsigned char *) malloc(frame2_size);
496 frame2.allocated_image_bytes = frame2_size;
497 frame2.color_coding = DC1394_COLOR_CODING_RGB8;
499 int err = dc1394_debayer_frames(&frame1, &frame2,
BayerMethod_);
500 if (err != DC1394_SUCCESS)
503 dc1394_capture_enqueue(
camera_, frame);
504 CAM_EXCEPT(camera1394stereo::Exception,
"Could not convert/debayer frames");
507 capture_buffer =
reinterpret_cast<uint8_t *
>(frame2.image);
512 image.header.stamp =
ros::Time(
double(frame->timestamp) * 1.e-6 );
513 image.width = frame->size[0];
514 image.height = frame->size[1];
515 image2.header.stamp = image.header.stamp;
523 case DC1394_COLOR_CODING_YUV444:
524 image.step=image.width*3;
525 image_size = image.height*image.step;
527 image.data.resize(image_size);
528 yuv::uyv2rgb(reinterpret_cast<unsigned char *> (capture_buffer),
529 reinterpret_cast<unsigned char *> (&image.data[0]),
530 image.width * image.height);
532 case DC1394_COLOR_CODING_YUV411:
533 image.step=image.width*3;
534 image_size = image.height*image.step;
536 image.data.resize(image_size);
538 reinterpret_cast<unsigned char *> (&image.data[0]),
539 image.width * image.height);
541 case DC1394_COLOR_CODING_YUV422:
542 image.step=image.width*3;
543 image_size = image.height*image.step;
545 image.data.resize(image_size);
546 yuv::uyvy2rgb(reinterpret_cast<unsigned char *> (capture_buffer),
547 reinterpret_cast<unsigned char *> (&image.data[0]),
548 image.width * image.height);
550 case DC1394_COLOR_CODING_RGB8:
551 image.step=image.width*3;
552 image_size = image.height*image.step;
554 image.data.resize(image_size);
555 memcpy(&image.data[0], capture_buffer, image_size);
557 case DC1394_COLOR_CODING_MONO8:
558 case DC1394_COLOR_CODING_RAW8:
561 image.step=image.width;
562 image_size = image.height*image.step;
565 image.data.resize(image_size);
566 memcpy(&image.data[0], capture_buffer, image_size);
570 image.step=image.width*3;
571 image_size = image.height*image.step;
573 image.data.resize(image_size);
574 memcpy(&image.data[0], capture_buffer, image_size);
577 case DC1394_COLOR_CODING_MONO16:
578 case DC1394_COLOR_CODING_RAW16:
581 image2.width = image.width;
582 image2.height = image.height;
583 image2.step = image.step = image.width*3;
584 image_size = image.height*image.step;
586 image.data.resize(image_size);
587 image2.data.resize(image_size);
588 memcpy(&image.data[0], capture_buffer, image_size);
589 memcpy(&image2.data[0], capture_buffer+image_size, image_size);
593 image2.width = image.width;
594 image2.height = image.height;
595 image.step = image.width;
596 image2.step = image.step;
597 image_size = image.height*image.step;
599 image2.encoding = image.encoding;
600 image.data.resize(image_size);
601 image2.data.resize(image_size);
602 memcpy(&image.data[0], capture_buffer, image_size);
603 memcpy(&image2.data[0], capture_buffer+image_size, image_size);
608 image.step=image.width*3;
609 image_size = image.height*image.step;
611 image.data.resize(image_size);
612 memcpy(&image.data[0], capture_buffer, image_size);
616 image.step=image.width*2;
617 image_size = image.height*image.step;
619 image.is_bigendian =
false;
620 image.data.resize(image_size);
621 memcpy(&image.data[0], capture_buffer, image_size);
626 CAM_EXCEPT(camera1394stereo::Exception,
"Unknown image mode and coding");
631 dc1394_capture_enqueue(
camera_, frame);
635 free(capture_buffer);
const std::string BAYER_GRBG8
libdc1394 enumerated modes interface
dc1394stereo_method_t stereoMethod_
boost::shared_ptr< Features > features_
dc1394color_filter_t BayerPattern_
bool readData(sensor_msgs::Image &image, sensor_msgs::Image &image2)
void uyvy2rgb(unsigned char *src, unsigned char *dest, unsigned long long int NumPixels)
bool setIsoSpeed(dc1394camera_t *camera, int &iso_speed)
const std::string BAYER_GBRG8
Camera1394 features interface.
std::string bayer_string(dc1394color_filter_t pattern, unsigned int bits)
bool findStereoMethod(const char *method)
IEEE 1394 digital camera library interface.
void findBayerPattern(const char *)
#define ROS_ASSERT_MSG(cond,...)
YUV to RGB conversion functions.
dc1394bayer_method_t BayerMethod_
Camera1394 Features class.
#define ROS_WARN_STREAM(args)
#define CAM_EXCEPT(except, msg)
Macro for throwing an exception with a message.
dc1394color_coding_t colorCoding_
const std::string BAYER_BGGR8
#define ROS_INFO_STREAM(args)
bool setFrameRate(dc1394camera_t *camera, dc1394video_mode_t video_mode, double &frame_rate)
void uyyvyy2rgb(const unsigned char *src, unsigned char *dest, unsigned long long int NumPixels)
int open(camera1394stereo::Camera1394StereoConfig &newconfig)
const std::string BAYER_RGGB8
dc1394video_mode_t videoMode_
bool findBayerMethod(const char *)
dc1394video_mode_t getVideoMode(dc1394camera_t *camera, std::string &video_mode)
void uyv2rgb(const unsigned char *src, unsigned char *dest, unsigned long long int NumPixels)
#define CAM_EXCEPT_ARGS(except, msg,...)
Macro for throwing an exception with a message, passing args.