Skip to content

Commit 8d8558f

Browse files
committed
OpenNI2-FreenectDriver: Convert all printf and cout statements to log messages
Signed-off-by: Benn Snyder <[email protected]>
1 parent ee8db7e commit 8d8558f

File tree

6 files changed

+232
-132
lines changed

6 files changed

+232
-132
lines changed

OpenNI2-FreenectDriver/src/ColorStream.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
#include <string>
12
#include "ColorStream.hpp"
23

34
using namespace FreenectDriver;
45

56

6-
ColorStream::ColorStream(Freenect::FreenectDevice* pDevice) : VideoStream(pDevice) {
7+
ColorStream::ColorStream(Freenect::FreenectDevice* pDevice) : VideoStream(pDevice)
8+
{
79
video_mode = makeOniVideoMode(ONI_PIXEL_FORMAT_RGB888, 640, 480, 30);
810
setVideoMode(video_mode);
911
}
1012

1113
// Add video modes here as you implement them
12-
ColorStream::FreenectVideoModeMap ColorStream::getSupportedVideoModes() {
14+
ColorStream::FreenectVideoModeMap ColorStream::getSupportedVideoModes()
15+
{
1316
FreenectVideoModeMap modes;
1417
// pixelFormat, resolutionX, resolutionY, fps freenect_video_format, freenect_resolution
1518
modes[makeOniVideoMode(ONI_PIXEL_FORMAT_RGB888, 640, 480, 30)] = std::pair<freenect_video_format, freenect_resolution>(FREENECT_VIDEO_RGB, FREENECT_RESOLUTION_MEDIUM);
@@ -24,7 +27,8 @@ ColorStream::FreenectVideoModeMap ColorStream::getSupportedVideoModes() {
2427
*/
2528
}
2629

27-
OniStatus ColorStream::setVideoMode(OniVideoMode requested_mode) {
30+
OniStatus ColorStream::setVideoMode(OniVideoMode requested_mode)
31+
{
2832
FreenectVideoModeMap supported_video_modes = getSupportedVideoModes();
2933
FreenectVideoModeMap::const_iterator matched_mode_iter = supported_video_modes.find(requested_mode);
3034
if (matched_mode_iter == supported_video_modes.end())
@@ -34,31 +38,36 @@ OniStatus ColorStream::setVideoMode(OniVideoMode requested_mode) {
3438
freenect_resolution resolution = matched_mode_iter->second.second;
3539

3640
try { device->setVideoFormat(format, resolution); }
37-
catch (std::runtime_error e) {
38-
printf("format-resolution combination not supported by libfreenect: %d-%d\n", format, resolution);
41+
catch (std::runtime_error e)
42+
{
43+
LogError("Format " + format + std::string(" and resolution " + resolution) + " combination not supported by libfreenect");
3944
return ONI_STATUS_NOT_SUPPORTED;
4045
}
4146
video_mode = requested_mode;
4247
return ONI_STATUS_OK;
4348
}
4449

45-
void ColorStream::populateFrame(void* data, OniFrame* frame) const {
50+
void ColorStream::populateFrame(void* data, OniFrame* frame) const
51+
{
4652
frame->sensorType = sensor_type;
4753
frame->stride = video_mode.resolutionX*3;
4854
frame->cropOriginX = frame->cropOriginY = 0;
4955
frame->croppingEnabled = FALSE;
5056

5157
// copy stream buffer from freenect
52-
switch (video_mode.pixelFormat) {
58+
switch (video_mode.pixelFormat)
59+
{
5360
default:
54-
printf("pixelFormat %d not supported by populateFrame\n", video_mode.pixelFormat);
61+
LogError(std::string("Pixel format " + video_mode.pixelFormat) + " not supported by populateFrame()");
5562
return;
5663

5764
case ONI_PIXEL_FORMAT_RGB888:
5865
unsigned char* data_ptr = static_cast<unsigned char*>(data);
5966
unsigned char* frame_data = static_cast<unsigned char*>(frame->data);
60-
if (mirroring) {
61-
for (int i = 0; i < frame->dataSize; i += 3) {
67+
if (mirroring)
68+
{
69+
for (int i = 0; i < frame->dataSize; i += 3)
70+
{
6271
// find corresponding mirrored pixel
6372
unsigned int pixel = i / 3;
6473
unsigned int row = pixel / video_mode.resolutionX;
@@ -71,7 +80,9 @@ void ColorStream::populateFrame(void* data, OniFrame* frame) const {
7180
}
7281
}
7382
else
83+
{
7484
std::copy(data_ptr, data_ptr+frame->dataSize, frame_data);
85+
}
7586

7687
return;
7788
}

OpenNI2-FreenectDriver/src/ColorStream.hpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#include "VideoStream.hpp"
88

99

10-
namespace FreenectDriver {
11-
class ColorStream : public VideoStream {
10+
namespace FreenectDriver
11+
{
12+
class ColorStream : public VideoStream
13+
{
1214
public:
1315
// from NUI library & converted to radians
1416
static const float DIAGONAL_FOV = 73.9 * (M_PI / 180);
@@ -30,7 +32,8 @@ namespace FreenectDriver {
3032
ColorStream(Freenect::FreenectDevice* pDevice);
3133
//~ColorStream() { }
3234

33-
static OniSensorInfo getSensorInfo() {
35+
static OniSensorInfo getSensorInfo()
36+
{
3437
FreenectVideoModeMap supported_modes = getSupportedVideoModes();
3538
OniVideoMode* modes = new OniVideoMode[supported_modes.size()];
3639
std::transform(supported_modes.begin(), supported_modes.end(), modes, RetrieveKey());
@@ -39,8 +42,10 @@ namespace FreenectDriver {
3942
}
4043

4144
// from StreamBase
42-
OniBool isPropertySupported(int propertyId) {
43-
switch(propertyId) {
45+
OniBool isPropertySupported(int propertyId)
46+
{
47+
switch(propertyId)
48+
{
4449
default:
4550
return VideoStream::isPropertySupported(propertyId);
4651

@@ -63,7 +68,7 @@ namespace FreenectDriver {
6368
{
6469
if (*pDataSize != sizeof(float))
6570
{
66-
printf("Unexpected size: %d != %lu\n", *pDataSize, sizeof(float));
71+
LogError("Unexpected size for ONI_STREAM_PROPERTY_HORIZONTAL_FOV");
6772
return ONI_STATUS_ERROR;
6873
}
6974
*(static_cast<float*>(data)) = HORIZONTAL_FOV;
@@ -73,7 +78,7 @@ namespace FreenectDriver {
7378
{
7479
if (*pDataSize != sizeof(float))
7580
{
76-
printf("Unexpected size: %d != %lu\n", *pDataSize, sizeof(float));
81+
LogError("Unexpected size for ONI_STREAM_PROPERTY_VERTICAL_FOV");
7782
return ONI_STATUS_ERROR;
7883
}
7984
*(static_cast<float*>(data)) = VERTICAL_FOV;
@@ -85,7 +90,7 @@ namespace FreenectDriver {
8590
{
8691
if (*pDataSize != sizeof(OniBool))
8792
{
88-
printf("Unexpected size: %d != %lu\n", *pDataSize, sizeof(OniBool));
93+
LogError("Unexpected size for ONI_STREAM_PROPERTY_AUTO_WHITE_BALANCE");
8994
return ONI_STATUS_ERROR;
9095
}
9196
*(static_cast<OniBool*>(data)) = auto_white_balance;
@@ -95,7 +100,7 @@ namespace FreenectDriver {
95100
{
96101
if (*pDataSize != sizeof(OniBool))
97102
{
98-
printf("Unexpected size: %d != %lu\n", *pDataSize, sizeof(OniBool));
103+
LogError("Unexpected size for ONI_STREAM_PROPERTY_AUTO_EXPOSURE");
99104
return ONI_STATUS_ERROR;
100105
}
101106
*(static_cast<OniBool*>(data)) = auto_exposure;
@@ -116,7 +121,7 @@ namespace FreenectDriver {
116121
{
117122
if (dataSize != sizeof(OniBool))
118123
{
119-
printf("Unexpected size: %d != %lu\n", dataSize, sizeof(OniBool));
124+
LogError("Unexpected size for ONI_STREAM_PROPERTY_AUTO_WHITE_BALANCE");
120125
return ONI_STATUS_ERROR;
121126
}
122127
auto_white_balance = *(static_cast<const OniBool*>(data));
@@ -127,7 +132,7 @@ namespace FreenectDriver {
127132
{
128133
if (dataSize != sizeof(OniBool))
129134
{
130-
printf("Unexpected size: %d != %lu\n", dataSize, sizeof(OniBool));
135+
LogError("Unexpected size for ONI_STREAM_PROPERTY_AUTO_EXPOSURE");
131136
return ONI_STATUS_ERROR;
132137
}
133138
auto_exposure = *(static_cast<const OniBool*>(data));

OpenNI2-FreenectDriver/src/DepthStream.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#include <string>
12
#include "DepthStream.hpp"
23

34
using namespace FreenectDriver;
45

56

6-
DepthStream::DepthStream(Freenect::FreenectDevice* pDevice) : VideoStream(pDevice) {
7+
DepthStream::DepthStream(Freenect::FreenectDevice* pDevice) : VideoStream(pDevice)
8+
{
79
video_mode = makeOniVideoMode(ONI_PIXEL_FORMAT_DEPTH_1_MM, 640, 480, 30);
810
image_registration_mode = ONI_IMAGE_REGISTRATION_OFF;
911
setVideoMode(video_mode);
@@ -12,7 +14,8 @@ DepthStream::DepthStream(Freenect::FreenectDevice* pDevice) : VideoStream(pDevic
1214
// Add video modes here as you implement them
1315
// Note: if image_registration_mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR,
1416
// setVideoFormat() will try FREENECT_DEPTH_REGISTERED first then fall back on what is set here.
15-
DepthStream::FreenectDepthModeMap DepthStream::getSupportedVideoModes() {
17+
DepthStream::FreenectDepthModeMap DepthStream::getSupportedVideoModes()
18+
{
1619
FreenectDepthModeMap modes;
1720
// pixelFormat, resolutionX, resolutionY, fps
1821
modes[makeOniVideoMode(ONI_PIXEL_FORMAT_DEPTH_1_MM, 640, 480, 30)] = std::pair<freenect_depth_format, freenect_resolution>(FREENECT_DEPTH_MM, FREENECT_RESOLUTION_MEDIUM);
@@ -21,7 +24,8 @@ DepthStream::FreenectDepthModeMap DepthStream::getSupportedVideoModes() {
2124
return modes;
2225
}
2326

24-
OniStatus DepthStream::setVideoMode(OniVideoMode requested_mode) {
27+
OniStatus DepthStream::setVideoMode(OniVideoMode requested_mode)
28+
{
2529
FreenectDepthModeMap supported_video_modes = getSupportedVideoModes();
2630
FreenectDepthModeMap::const_iterator matched_mode_iter = supported_video_modes.find(requested_mode);
2731
if (matched_mode_iter == supported_video_modes.end())
@@ -33,10 +37,12 @@ OniStatus DepthStream::setVideoMode(OniVideoMode requested_mode) {
3337
format = FREENECT_DEPTH_REGISTERED;
3438

3539
try { device->setDepthFormat(format, resolution); }
36-
catch (std::runtime_error e) {
37-
printf("format-resolution combination not supported by libfreenect: %d-%d\n", format, resolution);
38-
if (image_registration_mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR) {
39-
printf("could not use image registration format; disabling registration and falling back to format defined in getSupportedVideoModes()\n");
40+
catch (std::runtime_error e)
41+
{
42+
LogError("Format " + format + std::string(" and resolution " + resolution) + " combination not supported by libfreenect");
43+
if (image_registration_mode == ONI_IMAGE_REGISTRATION_DEPTH_TO_COLOR)
44+
{
45+
LogError("Could not enable image registration format; falling back to format defined in getSupportedVideoModes()");
4046
image_registration_mode = ONI_IMAGE_REGISTRATION_OFF;
4147
return setVideoMode(requested_mode);
4248
}
@@ -46,7 +52,8 @@ OniStatus DepthStream::setVideoMode(OniVideoMode requested_mode) {
4652
return ONI_STATUS_OK;
4753
}
4854

49-
void DepthStream::populateFrame(void* data, OniFrame* frame) const {
55+
void DepthStream::populateFrame(void* data, OniFrame* frame) const
56+
{
5057
frame->sensorType = sensor_type;
5158
frame->stride = video_mode.resolutionX * sizeof(uint16_t);
5259

@@ -69,11 +76,14 @@ void DepthStream::populateFrame(void* data, OniFrame* frame) const {
6976
unsigned short* target = static_cast<unsigned short*>(frame->data);
7077
const unsigned int skipWidth = video_mode.resolutionX - frame->width;
7178

72-
if (mirroring) {
79+
if (mirroring)
80+
{
7381
target += frame->width;
7482

75-
for (int y = 0; y < frame->height; y++) {
76-
for (int x = 0; x < frame->width; x++) {
83+
for (int y = 0; y < frame->height; y++)
84+
{
85+
for (int x = 0; x < frame->width; x++)
86+
{
7787
unsigned short value = *(source++);
7888
*(target--) = value < DepthStream::MAX_VALUE ? value : 0;
7989
}
@@ -82,9 +92,12 @@ void DepthStream::populateFrame(void* data, OniFrame* frame) const {
8292
target += 2 * frame->width;
8393
}
8494
}
85-
else {
86-
for (int y = 0; y < frame->height; y++) {
87-
for (int x = 0; x < frame->width; x++) {
95+
else
96+
{
97+
for (int y = 0; y < frame->height; y++)
98+
{
99+
for (int x = 0; x < frame->width; x++)
100+
{
88101
unsigned short value = *(source++);
89102
*(target++) = value < DepthStream::MAX_VALUE ? value : 0;
90103
}

0 commit comments

Comments
 (0)