tracker-native: letterbox preview to preserve aspect ratio
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python root tests (pytest, no tracker deps) (push) Canceled after 0s
ci / Python tracker (pytest, tracker/.venv) (push) Canceled after 0s

This commit is contained in:
kinect-bot committed 2026-11-25 22:59:19 -05:00
1 parent b3bdd5f7dc
commit 6dc472921a
1 file changed
+31 -5
+31 -5
View File
@@ -257,7 +257,36 @@ void StopCapture() {
void PaintPreview(HWND hwnd, HDC hdc) {
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
if (g_app.preview_bgra.empty()) return;
RECT rc;
GetClientRect(hwnd, &rc);
const int top = 44;
const int avail_w = rc.right;
const int avail_h = rc.bottom - top;
if (avail_w <= 0 || avail_h <= 0) return;
// Fill the preview area black first so letterbox bars (and stale pixels from a
// previous resolution) don't show through.
const RECT area{0, top, rc.right, rc.bottom};
FillRect(hdc, &area, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
if (g_app.preview_bgra.empty() || g_app.preview_w <= 0 || g_app.preview_h <= 0) {
return;
}
// Scale to fit the box while preserving the frame's aspect ratio, centered.
const double src_ar = static_cast<double>(g_app.preview_w) / g_app.preview_h;
const double dst_ar = static_cast<double>(avail_w) / avail_h;
int draw_w, draw_h;
if (src_ar > dst_ar) {
draw_w = avail_w;
draw_h = static_cast<int>(avail_w / src_ar + 0.5);
} else {
draw_h = avail_h;
draw_w = static_cast<int>(avail_h * src_ar + 0.5);
}
const int off_x = (avail_w - draw_w) / 2;
const int off_y = top + (avail_h - draw_h) / 2;
BITMAPINFO bmi{};
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = g_app.preview_w;
@@ -265,11 +294,8 @@ void PaintPreview(HWND hwnd, HDC hdc) {
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
RECT rc;
GetClientRect(hwnd, &rc);
const int top = 44;
SetStretchBltMode(hdc, HALFTONE);
StretchDIBits(hdc, 0, top, rc.right, rc.bottom - top, 0, 0, g_app.preview_w,
StretchDIBits(hdc, off_x, off_y, draw_w, draw_h, 0, 0, g_app.preview_w,
g_app.preview_h, g_app.preview_bgra.data(), &bmi, DIB_RGB_COLORS,
SRCCOPY);
}