image_buffer.hpp
Go to the documentation of this file.
1 #ifndef IMAGE_BUFFER_6RYGHM2V
2 #define IMAGE_BUFFER_6RYGHM2V
3 
4 #include <stdexcept>
5 #include <boost/thread/mutex.hpp>
6 #include <boost/shared_ptr.hpp>
7 #include <boost/lexical_cast.hpp>
8 
9 #include <libfreenect/libfreenect.h>
10 
11 namespace xiaoqiang_freenect_camera {
12 
13  const float RGB_FOCAL_LENGTH_SXGA = 1050;
14  const float WIDTH_SXGA = 1280;
15 
22  struct ImageBuffer {
23  boost::mutex mutex;
25  int valid;
27  float focal_length;
29  };
30 
31 
35  float getRGBFocalLength(int width) {
36  float scale = width / WIDTH_SXGA;
37  return RGB_FOCAL_LENGTH_SXGA * scale;
38  }
39 
44  const freenect_registration& registration, int width) {
45 
46  float depth_focal_length_sxga =
47  registration.zero_plane_info.reference_distance /
49  float scale = width / WIDTH_SXGA;
50  return depth_focal_length_sxga * scale;
51  }
52 
57  ImageBuffer& buffer,
58  const freenect_video_format& format,
59  const freenect_resolution& resolution,
60  const freenect_registration& registration) {
61 
62  // Obtain a lock on the buffer. This is mostly for debugging, as allocate
63  // buffer should only be called when the buffer is not being used by the
64  // freenect thread
65  boost::lock_guard<boost::mutex> buffer_lock(buffer.mutex);
66 
67  // Deallocate the buffer incase an exception happens (the buffer should no
68  // longer be valid)
69  buffer.image_buffer.reset();
70 
71  switch (format) {
72  case FREENECT_VIDEO_RGB:
78  switch (resolution) {
81  buffer.metadata =
82  freenect_find_video_mode(resolution, format);
83  if (!buffer.metadata.is_valid) {
84  throw std::runtime_error("libfreenect: Invalid video fmt, res: " +
85  boost::lexical_cast<std::string>(format) + "," +
86  boost::lexical_cast<std::string>(resolution));
87  }
88  break;
89  default:
90  throw std::runtime_error("libfreenect: Invalid video resolution: " +
91  boost::lexical_cast<std::string>(resolution));
92  }
93  break;
94  default:
95  throw std::runtime_error("libfreenect: Invalid video format: " +
96  boost::lexical_cast<std::string>(format));
97  }
98 
99  // All is good, reallocate the buffer and calculate other pieces of info
100  buffer.image_buffer.reset(new unsigned char[buffer.metadata.bytes]);
101  switch(format) {
102  case FREENECT_VIDEO_RGB:
105  buffer.focal_length = getRGBFocalLength(buffer.metadata.width);
106  break;
110  buffer.focal_length = getDepthFocalLength(registration,
111  buffer.metadata.width);
112  break;
113  default:
114  throw std::runtime_error("libfreenect: shouldn't reach here");
115  }
116  buffer.is_registered = false;
117  }
118 
123  ImageBuffer& buffer,
124  const freenect_depth_format& format,
125  const freenect_resolution& resolution,
126  const freenect_registration& registration) {
127 
128  // Obtain a lock on the buffer. This is mostly for debugging, as allocate
129  // buffer should only be called when the buffer is not being used by the
130  // freenect thread
131  boost::lock_guard<boost::mutex> buffer_lock(buffer.mutex);
132 
133  // Deallocate the buffer incase an exception happens (the buffer should no
134  // longer be valid)
135  buffer.image_buffer.reset();
136 
137  switch (format) {
143  case FREENECT_DEPTH_MM:
144  switch (resolution) {
146  buffer.metadata =
147  freenect_find_depth_mode(resolution, format);
148  if (!buffer.metadata.is_valid) {
149  throw std::runtime_error("libfreenect: Invalid depth fmt, res: " +
150  boost::lexical_cast<std::string>(format) + "," +
151  boost::lexical_cast<std::string>(resolution));
152  }
153  break;
154  default:
155  throw std::runtime_error("libfreenect: Invalid depth resolution: " +
156  boost::lexical_cast<std::string>(resolution));
157  }
158  break;
159  default:
160  throw std::runtime_error("libfreenect: Invalid depth format: " +
161  boost::lexical_cast<std::string>(format));
162  }
163 
164  // All is good, reallocate the buffer and calculate other pieces of info
165  buffer.image_buffer.reset(new unsigned char[buffer.metadata.bytes]);
166  switch(format) {
171  case FREENECT_DEPTH_MM:
172  buffer.focal_length =
173  getDepthFocalLength(registration, buffer.metadata.width);
174  buffer.is_registered = false;
175  break;
177  buffer.focal_length = getRGBFocalLength(buffer.metadata.width);
178  buffer.is_registered = true;
179  break;
180  default:
181  throw std::runtime_error("libfreenect: shouldn't reach here");
182  }
183  }
184 
185  void fillImage(const ImageBuffer& buffer, void* data) {
186  memcpy(data, buffer.image_buffer.get(), buffer.metadata.bytes);
187  }
188 
189 } /* end namespace xiaoqiang_freenect_camera */
190 
191 #endif /* end of include guard: IMAGE_BUFFER_6RYGHM2V */
void allocateBufferDepth(ImageBuffer &buffer, const freenect_depth_format &format, const freenect_resolution &resolution, const freenect_registration &registration)
FREENECT_VIDEO_RGB
void allocateBufferVideo(ImageBuffer &buffer, const freenect_video_format &format, const freenect_resolution &resolution, const freenect_registration &registration)
void fillImage(const ImageBuffer &buffer, void *data)
freenect_zero_plane_info zero_plane_info
FREENECT_DEPTH_10BIT
freenect_video_format
FREENECT_DEPTH_10BIT_PACKED
FREENECT_VIDEO_IR_8BIT
FREENECT_DEPTH_11BIT_PACKED
const float RGB_FOCAL_LENGTH_SXGA
float getDepthFocalLength(const freenect_registration &registration, int width)
FREENECT_VIDEO_IR_10BIT
Holds an image buffer with all the metadata required to transmit the image over ROS channels...
freenect_depth_format
float getRGBFocalLength(int width)
freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt)
freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt)
boost::shared_array< unsigned char > image_buffer
FREENECT_VIDEO_YUV_RGB
FREENECT_VIDEO_BAYER
freenect_resolution
FREENECT_RESOLUTION_MEDIUM
FREENECT_DEPTH_11BIT
FREENECT_VIDEO_IR_10BIT_PACKED
FREENECT_DEPTH_MM
FREENECT_RESOLUTION_HIGH
FREENECT_DEPTH_REGISTERED


xiaoqiang_freenect_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu (original openni_camera driver)., Piyush Khandelwal (libfreenect port).
autogenerated on Mon Jun 10 2019 15:53:18