#include "synth/synth.h" #include #include #include #include "mapping/mapping.h" namespace nui::synth { namespace { constexpr uint16_t kMinBodyDepthMm = 800; constexpr uint16_t kMaxBodyDepthMm = 4000; struct ProjectedJoint { float x; float y; float depth_mm; }; std::array project_joints( const contract::SkeletonFrame& skeleton, ImageSize size) { std::array projected{}; for (std::size_t index = 0; index < projected.size(); ++index) { const auto point = mapping::skeleton_to_depth_image( skeleton.joints[index].position, static_cast(size.width), static_cast(size.height)); projected[index] = { static_cast(std::min(point.x, static_cast(size.width) - 1)), static_cast(std::min(point.y, static_cast(size.height) - 1)), static_cast(std::clamp( NuiDepthPixelToDepth(point.packed_depth), kMinBodyDepthMm, kMaxBodyDepthMm)), }; } return projected; } uint16_t interpolated_depth( uint32_t x, uint32_t y, const std::array& joints) { float weighted_depth = 0.0f; float total_weight = 0.0f; for (const auto& joint : joints) { const float delta_x = static_cast(x) - joint.x; const float delta_y = static_cast(y) - joint.y; const float weight = 1.0f / (delta_x * delta_x + delta_y * delta_y + 1.0f); weighted_depth += joint.depth_mm * weight; total_weight += weight; } return static_cast(std::clamp( static_cast(std::lround(weighted_depth / total_weight)), static_cast(kMinBodyDepthMm), static_cast(kMaxBodyDepthMm))); } } // namespace uint16_t pack_depth(uint16_t depth_mm, uint8_t player_index) { return static_cast( ((depth_mm & 0x1FFF) << 3) | (player_index & 0x7)); } uint16_t NuiDepthPixelToDepth(uint16_t packed_depth) { return static_cast(packed_depth >> 3); } uint8_t NuiDepthPixelToPlayerIndex(uint16_t packed_depth) { return static_cast(packed_depth & 0x7); } DepthRasterizer::DepthRasterizer(std::size_t temporal_hold_frames) : stabilizer_(temporal_hold_frames) {} DepthFrame DepthRasterizer::rasterize( const SegmentationMask& mask, const contract::SkeletonFrame& skeleton, DepthRasterRequest request) { stable_mask_ = stabilizer_.update(mask, request.size); DepthFrame frame{ request.size, request.size.width * static_cast(sizeof(uint16_t)), std::vector(pixel_count(request.size), 0), }; if (!has_body(stable_mask_) || request.size.width == 0 || request.size.height == 0) { return frame; } const auto joints = project_joints(skeleton, request.size); const uint8_t player_index = static_cast(std::min(request.skeleton_slot, 5) + 1); for (uint32_t y = 0; y < request.size.height; ++y) { for (uint32_t x = 0; x < request.size.width; ++x) { const std::size_t index = static_cast(y) * request.size.width + x; if (stable_mask_.pixels[index] != 0) { frame.pixels[index] = pack_depth(interpolated_depth(x, y, joints), player_index); } } } return frame; } const BinaryMask& DepthRasterizer::stable_mask() const { return stable_mask_; } BgraFrame passthrough_color( const BgraFrame& source, ImageSize requested_size) { BgraFrame result{ requested_size, requested_size.width * 4, std::vector(pixel_count(requested_size) * 4, 0), }; if (source.size.width == 0 || source.size.height == 0 || source.pixels.size() != pixel_count(source.size) * 4 || requested_size.width == 0 || requested_size.height == 0) { return result; } for (uint32_t y = 0; y < requested_size.height; ++y) { const uint32_t source_y = static_cast(y) * source.size.height / requested_size.height; for (uint32_t x = 0; x < requested_size.width; ++x) { const uint32_t source_x = static_cast(x) * source.size.width / requested_size.width; const std::size_t source_index = (static_cast(source_y) * source.size.width + source_x) * 4; const std::size_t target_index = (static_cast(y) * requested_size.width + x) * 4; std::copy_n( source.pixels.begin() + static_cast(source_index), 4, result.pixels.begin() + static_cast(target_index)); } } return result; } InfraredFrame synthesize_infrared( const BgraFrame& source, ImageSize requested_size) { const auto color = passthrough_color(source, requested_size); InfraredFrame result{ requested_size, requested_size.width * static_cast(sizeof(uint16_t)), std::vector(pixel_count(requested_size), 0), }; for (std::size_t index = 0; index < result.pixels.size(); ++index) { const std::size_t color_index = index * 4; const uint32_t luminance = 77u * color.pixels[color_index + 2] + 150u * color.pixels[color_index + 1] + 29u * color.pixels[color_index]; result.pixels[index] = static_cast(luminance); } return result; } } // namespace nui::synth