-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFreenectV1.cpp
More file actions
229 lines (198 loc) · 7.46 KB
/
FreenectV1.cpp
File metadata and controls
229 lines (198 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// FreenectV1.cpp
// FreenectTD
//
// Created by marte on 27/07/2025.
//
#include "FreenectV1.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <chrono>
#include <Accelerate/Accelerate.h>
// depthFormatEnum enum definition (shared between v1 and v2)
enum class depthFormatEnum {
Raw,
RawUndistorted,
Registered
};
// MyFreenectDevice class constructor
MyFreenectDevice::MyFreenectDevice
(freenect_context* ctx, int index,
std::atomic<bool>& rgbFlag,
std::atomic<bool>& depthFlag) :
FreenectDevice(ctx, index),
rgbReady(rgbFlag),
depthReady(depthFlag),
rgbBuffer(WIDTH * HEIGHT * 3),
depthBuffer(WIDTH * HEIGHT),
hasNewRGB(false),
hasNewDepth(false)
{
setVideoFormat(FREENECT_VIDEO_RGB);
setDepthFormat(FREENECT_DEPTH_MM);
}
// MyFreenectDevice class destructor
MyFreenectDevice::~MyFreenectDevice() {
stop();
}
// VideoCallback method to handle RGB data
void MyFreenectDevice::VideoCallback(void* rgb, uint32_t) {
std::lock_guard<std::mutex> lock(mutex);
if (!rgb) return;
auto ptr = static_cast<uint8_t*>(rgb);
std::copy(ptr, ptr + rgbBuffer.size(), rgbBuffer.begin());
hasNewRGB = true;
rgbReady = true;
}
// DepthCallback method to handle depth data
void MyFreenectDevice::DepthCallback(void* depth, uint32_t) {
std::lock_guard<std::mutex> lock(mutex);
if (!depth) return;
auto ptr = static_cast<uint16_t*>(depth);
std::copy(ptr, ptr + depthBuffer.size(), depthBuffer.begin());
hasNewDepth = true;
depthReady = true;
}
// Start video and depth streams (using libfreenect.hpp API)
bool MyFreenectDevice::start() {
startVideo();
startDepth();
return true;
}
// Stop video and depth streams (using libfreenect.hpp API)
void MyFreenectDevice::stop() {
stopVideo();
stopDepth();
}
// Set RGB, depth and IR resolutions
void MyFreenectDevice::setResolutions(int rgbWidth, int rgbHeight, int depthWidth, int depthHeight, int irWidth, int irHeight) {
rgbWidth_ = rgbWidth;
rgbHeight_ = rgbHeight;
depthWidth_ = depthWidth;
depthHeight_ = depthHeight;
irWidth_ = irWidth;
irHeight_ = irHeight;
}
// Get RGB data
bool MyFreenectDevice::getRGB(std::vector<uint8_t>& out) {
std::lock_guard<std::mutex> lock(mutex); // Lock the mutex to ensure thread safety
if (!hasNewRGB) return false; // Check if new RGB data is available
out = rgbBuffer; // Copy the RGB buffer to the output vector
hasNewRGB = false; // Reset the flag indicating new RGB data
return true;
}
// Get depth data
bool MyFreenectDevice::getDepth(std::vector<uint16_t>& out) {
std::lock_guard<std::mutex> lock(mutex); // Lock the mutex to ensure thread safety
if (!hasNewDepth) return false; // Check if new depth data is available
out = depthBuffer; // Copy the depth buffer to the output vector
hasNewDepth = false; // Reset the flag indicating new depth data
return true;
}
// Get color frame
bool MyFreenectDevice::getColorFrame(std::vector<uint8_t>& out, fn1_colorType type) {
const int srcWidth = WIDTH, srcHeight = HEIGHT;
const int dstWidth = rgbWidth_, dstHeight = rgbHeight_;
/*switch (type) {
case fn1_colorType::RGB:
MyFreenectDevice::setVideoFormat(FREENECT_VIDEO_RGB);
break;
case fn1_colorType::IR:
MyFreenectDevice::setVideoFormat(FREENECT_VIDEO_IR_10BIT);
break;
default:
MyFreenectDevice::setVideoFormat(FREENECT_VIDEO_RGB);
break;
}*/
std::lock_guard<std::mutex> lock(mutex);
if (!hasNewRGB) return false;
const size_t dstPixelCount = static_cast<size_t>(dstWidth) * dstHeight;
out.resize(dstPixelCount * 4);
// Source buffer (RGB888)
vImage_Buffer src = {
.data = rgbBuffer.data(),
.height = (vImagePixelCount)srcHeight,
.width = (vImagePixelCount)srcWidth,
.rowBytes = static_cast<size_t>(srcWidth * 3)
};
// Temporary ARGB buffer (same size as source)
std::vector<uint8_t> tmpARGB(srcWidth * srcHeight * 4);
vImage_Buffer tmpARGBbuf = {
.data = tmpARGB.data(),
.height = (vImagePixelCount)srcHeight,
.width = (vImagePixelCount)srcWidth,
.rowBytes = static_cast<size_t>(srcWidth * 4)
};
// Destination RGBA buffer (scaled)
vImage_Buffer dst = {
.data = out.data(),
.height = (vImagePixelCount)dstHeight,
.width = (vImagePixelCount)dstWidth,
.rowBytes = static_cast<size_t>(dstWidth * 4)
};
// Convert RGB → ARGB
vImageConvert_RGB888toARGB8888(&src, nullptr, 255, &tmpARGBbuf, false, kvImageNoFlags);
// Scale ARGB
if (dstWidth != srcWidth || dstHeight != srcHeight) {
vImageScale_ARGB8888(&tmpARGBbuf, &dst, nullptr, kvImageHighQualityResampling | kvImageDoNotTile);
} else {
// Same size, copy directly
std::memcpy(out.data(), tmpARGB.data(), tmpARGB.size());
}
// Convert ARGB → RGBA in place (safe, same 4 bytes per pixel)
vImagePermuteChannels_ARGB8888(&dst, &dst, (uint8_t[]){1, 2, 3, 0}, kvImageNoFlags);
hasNewRGB = false;
return true;
}
// Get depth frame
bool MyFreenectDevice::getDepthFrame(std::vector<uint16_t>& out, depthFormatEnum type, float depthThreshMin, float depthThreshMax) {
const int srcWidth = WIDTH, srcHeight = HEIGHT;
const int dstWidth = depthWidth_, dstHeight = depthHeight_;
if (type == depthFormatEnum::Registered) {
MyFreenectDevice::setDepthFormat(FREENECT_DEPTH_REGISTERED);
} else {
// Both Raw and RawUndistorted use FREENECT_DEPTH_MM for v1
MyFreenectDevice::setDepthFormat(FREENECT_DEPTH_MM);
}
std::lock_guard<std::mutex> lock(mutex);
if (!hasNewDepth) return false;
const size_t srcPixelCount = static_cast<size_t>(srcWidth) * srcHeight;
const size_t dstPixelCount = static_cast<size_t>(dstWidth) * dstHeight;
out.resize(dstPixelCount);
// Step 1. Normalize depth data into 16-bit linear buffer
std::vector<uint16_t> tmp(srcPixelCount);
#pragma omp parallel for if(srcPixelCount > 100000)
for (size_t i = 0; i < srcPixelCount; ++i) {
uint16_t val = depthBuffer[i];
const float min_mm = depthThreshMin;
const float max_mm = depthThreshMax;
if (val >= min_mm && val <= max_mm) {
tmp[i] = static_cast<uint16_t>(
(static_cast<float>(val) - min_mm) / (max_mm - min_mm) * 65535.0f
);
} else {
tmp[i] = 0;
}
}
// Step 2. Use vImage to scale depth map (single channel 16-bit)
vImage_Buffer srcBuf = {
.data = tmp.data(),
.height = (vImagePixelCount)srcHeight,
.width = (vImagePixelCount)srcWidth,
.rowBytes = static_cast<size_t>(srcWidth * sizeof(uint16_t))
};
vImage_Buffer dstBuf = {
.data = out.data(),
.height = (vImagePixelCount)dstHeight,
.width = (vImagePixelCount)dstWidth,
.rowBytes = static_cast<size_t>(dstWidth * sizeof(uint16_t))
};
if (dstWidth != srcWidth || dstHeight != srcHeight) {
vImageScale_Planar16U(&srcBuf, &dstBuf, nullptr, kvImageHighQualityResampling | kvImageDoNotTile);
} else {
std::memcpy(out.data(), tmp.data(), tmp.size() * sizeof(uint16_t));
}
hasNewDepth = false;
return true;
}