From 6dc472921a501bd73453e1e271c528f522d25a4c Mon Sep 17 00:00:00 2001 From: kinect-bot Date: Wed, 25 Nov 2026 22:59:19 -0500 Subject: [PATCH] tracker-native: letterbox preview to preserve aspect ratio --- tracker-native/src/win_main.cpp | 36 ++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/tracker-native/src/win_main.cpp b/tracker-native/src/win_main.cpp index 8e993c3..a82369c 100644 --- a/tracker-native/src/win_main.cpp +++ b/tracker-native/src/win_main.cpp @@ -257,7 +257,36 @@ void StopCapture() { void PaintPreview(HWND hwnd, HDC hdc) { std::lock_guard 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(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(g_app.preview_w) / g_app.preview_h; + const double dst_ar = static_cast(avail_w) / avail_h; + int draw_w, draw_h; + if (src_ar > dst_ar) { + draw_w = avail_w; + draw_h = static_cast(avail_w / src_ar + 0.5); + } else { + draw_h = avail_h; + draw_w = static_cast(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); }