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.h>
10 
11 namespace 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;
26  freenect_frame_mode metadata;
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 /
48  registration.zero_plane_info.reference_pixel_size;
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:
73  case FREENECT_VIDEO_BAYER:
74  case FREENECT_VIDEO_YUV_RGB:
75  case FREENECT_VIDEO_IR_8BIT:
76  case FREENECT_VIDEO_IR_10BIT:
77  case FREENECT_VIDEO_IR_10BIT_PACKED:
78  switch (resolution) {
79  case FREENECT_RESOLUTION_HIGH:
80  case FREENECT_RESOLUTION_MEDIUM:
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:
103  case FREENECT_VIDEO_BAYER:
104  case FREENECT_VIDEO_YUV_RGB:
105  buffer.focal_length = getRGBFocalLength(buffer.metadata.width);
106  break;
107  case FREENECT_VIDEO_IR_8BIT:
108  case FREENECT_VIDEO_IR_10BIT:
109  case FREENECT_VIDEO_IR_10BIT_PACKED:
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) {
138  case FREENECT_DEPTH_11BIT:
139  case FREENECT_DEPTH_10BIT:
140  case FREENECT_DEPTH_11BIT_PACKED:
141  case FREENECT_DEPTH_10BIT_PACKED:
142  case FREENECT_DEPTH_REGISTERED:
143  case FREENECT_DEPTH_MM:
144  switch (resolution) {
145  case FREENECT_RESOLUTION_MEDIUM:
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) {
167  case FREENECT_DEPTH_11BIT:
168  case FREENECT_DEPTH_10BIT:
169  case FREENECT_DEPTH_11BIT_PACKED:
170  case FREENECT_DEPTH_10BIT_PACKED:
171  case FREENECT_DEPTH_MM:
172  buffer.focal_length =
173  getDepthFocalLength(registration, buffer.metadata.width);
174  buffer.is_registered = false;
175  break;
176  case FREENECT_DEPTH_REGISTERED:
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 freenect_camera */
190 
191 #endif /* end of include guard: IMAGE_BUFFER_6RYGHM2V */
void fillImage(const ImageBuffer &buffer, void *data)
float getRGBFocalLength(int width)
const float WIDTH_SXGA
float getDepthFocalLength(const freenect_registration &registration, int width)
void allocateBufferDepth(ImageBuffer &buffer, const freenect_depth_format &format, const freenect_resolution &resolution, const freenect_registration &registration)
boost::shared_array< unsigned char > image_buffer
Holds an image buffer with all the metadata required to transmit the image over ROS channels...
const float RGB_FOCAL_LENGTH_SXGA
void allocateBufferVideo(ImageBuffer &buffer, const freenect_video_format &format, const freenect_resolution &resolution, const freenect_registration &registration)
freenect_frame_mode metadata


freenect_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu (original openni_camera driver)., Piyush Khandelwal (libfreenect port).
autogenerated on Fri Mar 20 2020 03:22:26