initial commit
This commit is contained in:
58
client/core/depth/DepthService.cpp
Normal file
58
client/core/depth/DepthService.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "depth/DepthService.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace core {
|
||||
|
||||
QImage DepthService::computeFakeDepth(const QSize& size) {
|
||||
if (size.isEmpty() || size.width() <= 0 || size.height() <= 0) {
|
||||
return {};
|
||||
}
|
||||
QImage depth(size, QImage::Format_Grayscale8);
|
||||
if (depth.isNull()) {
|
||||
return {};
|
||||
}
|
||||
depth.fill(0);
|
||||
return depth;
|
||||
}
|
||||
|
||||
QImage DepthService::computeFakeDepthFromBackground(const QImage& background) {
|
||||
if (background.isNull()) {
|
||||
return {};
|
||||
}
|
||||
return computeFakeDepth(background.size());
|
||||
}
|
||||
|
||||
QImage DepthService::depthToColormapOverlay(const QImage& depth8, int alpha) {
|
||||
if (depth8.isNull()) {
|
||||
return {};
|
||||
}
|
||||
const QImage src = (depth8.format() == QImage::Format_Grayscale8) ? depth8 : depth8.convertToFormat(QImage::Format_Grayscale8);
|
||||
if (src.isNull()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const int a = std::clamp(alpha, 0, 255);
|
||||
QImage out(src.size(), QImage::Format_ARGB32_Premultiplied);
|
||||
if (out.isNull()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
for (int y = 0; y < src.height(); ++y) {
|
||||
const uchar* row = src.constScanLine(y);
|
||||
QRgb* dst = reinterpret_cast<QRgb*>(out.scanLine(y));
|
||||
for (int x = 0; x < src.width(); ++x) {
|
||||
const int d = static_cast<int>(row[x]); // 0..255
|
||||
// depth=0(远)-> 蓝;depth=255(近)-> 红
|
||||
const int r = d;
|
||||
const int g = 0;
|
||||
const int b = 255 - d;
|
||||
dst[x] = qRgba(r, g, b, a);
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
|
||||
20
client/core/depth/DepthService.h
Normal file
20
client/core/depth/DepthService.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <QImage>
|
||||
#include <QSize>
|
||||
|
||||
namespace core {
|
||||
|
||||
class DepthService final {
|
||||
public:
|
||||
// 生成 8-bit 深度图:0 最远,255 最近。当前实现为全 0(假深度)。
|
||||
static QImage computeFakeDepth(const QSize& size);
|
||||
static QImage computeFakeDepthFromBackground(const QImage& background);
|
||||
|
||||
// 把 8-bit 深度(Grayscale8)映射为伪彩色 ARGB32(带 alpha),用于叠加显示。
|
||||
// 约定:depth=0(最远)-> 蓝,depth=255(最近)-> 红(线性插值)。
|
||||
static QImage depthToColormapOverlay(const QImage& depth8, int alpha /*0-255*/);
|
||||
};
|
||||
|
||||
} // namespace core
|
||||
|
||||
Reference in New Issue
Block a user