DepthStream.cpp
Go to the documentation of this file.
1 #include <string>
2 #include "DepthStream.hpp"
3 
4 using namespace FreenectDriver;
5 
6 
8 {
12  pDevice->startDepth();
13 }
14 
15 // Add video modes here as you implement them
16 // Note: if image_registration_mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR,
17 // setVideoFormat() will try FREENECT_DEPTH_REGISTERED first then fall back on what is set here.
19 {
21  // pixelFormat, resolutionX, resolutionY, fps
22  modes[makeOniVideoMode(ONI_PIXEL_FORMAT_DEPTH_1_MM, 640, 480, 30)] = std::pair<freenect_depth_format, freenect_resolution>(FREENECT_DEPTH_MM, FREENECT_RESOLUTION_MEDIUM);
23 
24 
25  return modes;
26 }
27 
29 {
31  FreenectDepthModeMap::const_iterator matched_mode_iter = supported_video_modes.find(requested_mode);
32  if (matched_mode_iter == supported_video_modes.end())
34 
35  freenect_depth_format format = matched_mode_iter->second.first;
36  freenect_resolution resolution = matched_mode_iter->second.second;
37  if (image_registration_mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR) // try forcing registration mode
39 
40  try { device->setDepthFormat(format, resolution); }
41  catch (std::runtime_error e)
42  {
43  LogError("Format " + to_string(format) + " and resolution " + to_string(resolution) + " combination not supported by libfreenect");
45  {
46  LogError("Could not enable image registration format; falling back to format defined in getSupportedVideoModes()");
48  return setVideoMode(requested_mode);
49  }
51  }
52  video_mode = requested_mode;
53  return ONI_STATUS_OK;
54 }
55 
56 void DepthStream::populateFrame(void* data, OniFrame* frame) const
57 {
58  frame->sensorType = sensor_type;
59  frame->stride = video_mode.resolutionX * sizeof(uint16_t);
60 
61  if (cropping.enabled)
62  {
63  frame->height = cropping.height;
64  frame->width = cropping.width;
65  frame->cropOriginX = cropping.originX;
66  frame->cropOriginY = cropping.originY;
67  frame->croppingEnabled = true;
68  }
69  else
70  {
71  frame->cropOriginX = 0;
72  frame->cropOriginY = 0;
73  frame->croppingEnabled = false;
74  }
75 
76 
77  // copy stream buffer from freenect
78 
79  uint16_t* source = static_cast<uint16_t*>(data) + frame->cropOriginX + frame->cropOriginY * video_mode.resolutionX;
80  uint16_t* target = static_cast<uint16_t*>(frame->data);
81  const unsigned int skipWidth = video_mode.resolutionX - frame->width;
82 
83  if (mirroring)
84  {
85  target += frame->width;
86 
87  for (int y = 0; y < frame->height; y++)
88  {
89  for (int x = 0; x < frame->width; x++)
90  {
91  *target-- = *source++;
92  }
93 
94  source += skipWidth;
95  target += 2 * frame->width;
96  }
97  }
98  else
99  {
100  for (int y = 0; y < frame->height; y++)
101  {
102  for (int x = 0; x < frame->width; x++)
103  {
104  *target++ = *source++;
105  }
106 
107  source += skipWidth;
108  }
109  }
110 
111  /*
112  uint16_t* data_ptr = static_cast<uint16_t*>(data);
113  uint16_t* frame_data = static_cast<uint16_t*>(frame->data);
114  if (mirroring)
115  {
116  for (unsigned int i = 0; i < frame->dataSize / 2; i++)
117  {
118  // find corresponding mirrored pixel
119  unsigned int row = i / video_mode.resolutionX;
120  unsigned int col = video_mode.resolutionX - (i % video_mode.resolutionX);
121  unsigned int target = (row * video_mode.resolutionX) + col;
122  // copy it to this pixel
123  frame_data[i] = data_ptr[target];
124  }
125  }
126  else
127  std::copy(data_ptr, data_ptr+frame->dataSize / 2, frame_data);
128  */
129 }
130 
131 
132 /* depth video modes reference
133 
134 FREENECT_DEPTH_11BIT = 0, //< 11 bit depth information in one uint16_t/pixel
135 FREENECT_DEPTH_10BIT = 1, //< 10 bit depth information in one uint16_t/pixel
136 FREENECT_DEPTH_11BIT_PACKED = 2, //< 11 bit packed depth information
137 FREENECT_DEPTH_10BIT_PACKED = 3, //< 10 bit packed depth information
138 FREENECT_DEPTH_REGISTERED = 4, //< processed depth data in mm, aligned to 640x480 RGB
139 FREENECT_DEPTH_MM = 5, //< depth to each pixel in mm, but left unaligned to RGB image
140 FREENECT_DEPTH_DUMMY = 2147483647, //< Dummy value to force enum to be 32 bits wide
141 
142 ONI_PIXEL_FORMAT_DEPTH_1_MM = 100,
143 ONI_PIXEL_FORMAT_DEPTH_100_UM = 101,
144 ONI_PIXEL_FORMAT_SHIFT_9_2 = 102,
145 ONI_PIXEL_FORMAT_SHIFT_9_3 = 103,
146 */
OniImageRegistrationMode image_registration_mode
Definition: DepthStream.hpp:37
static void LogError(std::string error)
Definition: Utility.hpp:66
static const OniSensorType sensor_type
Definition: DepthStream.hpp:36
unsigned short uint16_t
int stride
Definition: OniCTypes.h:112
std::map< OniVideoMode, std::pair< freenect_depth_format, freenect_resolution > > FreenectDepthModeMap
Definition: DepthStream.hpp:35
int height
Definition: OniCTypes.h:105
int cropOriginX
Definition: OniCTypes.h:109
int frame
Definition: regview.c:72
static FreenectDepthModeMap getSupportedVideoModes()
Definition: DepthStream.cpp:18
freenect_depth_format
Definition: libfreenect.h:99
void populateFrame(void *data, OniFrame *frame) const
Definition: DepthStream.cpp:56
string target
Definition: fwfetcher.py:543
OniBool croppingEnabled
Definition: OniCTypes.h:108
int cropOriginY
Definition: OniCTypes.h:110
OniSensorType sensorType
Definition: OniCTypes.h:100
Freenect::FreenectDevice * device
Definition: VideoStream.hpp:20
OniStatus setVideoMode(OniVideoMode requested_mode)
Definition: DepthStream.cpp:28
void * data
Definition: OniCTypes.h:98
int resolutionX
Definition: OniCTypes.h:62
static freenect_frame_mode supported_video_modes[video_mode_count]
Definition: cameras.c:42
freenect_resolution
Definition: libfreenect.h:77
int width
Definition: OniCTypes.h:104
static OniVideoMode makeOniVideoMode(OniPixelFormat pixel_format, int resolution_x, int resolution_y, int frames_per_second)
Definition: Utility.hpp:10
DepthStream(Freenect::FreenectDevice *pDevice)
Definition: DepthStream.cpp:7
static std::string to_string(const T &n)
Definition: Utility.hpp:48
OniStatus
Definition: OniCEnums.h:25
void setDepthFormat(freenect_depth_format requested_format, freenect_resolution requested_resolution=FREENECT_RESOLUTION_MEDIUM)


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Mon Jun 10 2019 13:46:42