diff --git a/tracker-native/src/win_camera.cpp b/tracker-native/src/win_camera.cpp index ce6a37f..f93aecd 100644 --- a/tracker-native/src/win_camera.cpp +++ b/tracker-native/src/win_camera.cpp @@ -154,11 +154,13 @@ bool Camera::open(const std::wstring& symbolic_link, int width, int height) { SafeRelease(&cur); } - TraceLog("camera open OK: negotiated %dx%d default_stride=%ld (expect top-down +%d)", - width, height, stride, width * 4); + if (stride == 0) stride = width * 4; + TraceLog("camera open OK: negotiated %dx%d default_stride=%ld (%s)", + width, height, stride, stride < 0 ? "bottom-up -> flipped" : "top-down"); reader_ = reader; width_ = width; height_ = height; + stride_ = static_cast(stride); return true; } @@ -179,16 +181,22 @@ bool Camera::read_frame(std::vector& bgra, int& width, int& height) { BYTE* data = nullptr; DWORD max_len = 0, cur_len = 0; if (SUCCEEDED(buffer->Lock(&data, &max_len, &cur_len))) { - const size_t need = static_cast(width_) * height_ * 4u; + const size_t row_bytes = static_cast(width_) * 4u; + const size_t need = row_bytes * height_; + const int abs_stride = stride_ < 0 ? -stride_ : stride_; static unsigned logged = 0; if (logged < 3) { - TraceLog("read_frame: buffer=%lu bytes, need=%zu (%dx%d*4)", - static_cast(cur_len), need, width_, height_); + TraceLog("read_frame: buffer=%lu bytes, need=%zu (%dx%d*4) stride=%d", + static_cast(cur_len), need, width_, height_, stride_); ++logged; } - if (cur_len >= need) { + if (cur_len >= static_cast(static_cast(abs_stride) * height_)) { bgra.resize(need); - std::memcpy(bgra.data(), data, need); + for (int y = 0; y < height_; ++y) { + const int src_row = stride_ < 0 ? (height_ - 1 - y) : y; + std::memcpy(bgra.data() + static_cast(y) * row_bytes, + data + static_cast(src_row) * abs_stride, row_bytes); + } width = width_; height = height_; ok = true; diff --git a/tracker-native/src/win_camera.h b/tracker-native/src/win_camera.h index e20f9b0..a792354 100644 --- a/tracker-native/src/win_camera.h +++ b/tracker-native/src/win_camera.h @@ -36,6 +36,7 @@ class Camera { void* reader_ = nullptr; int width_ = 0; int height_ = 0; + int stride_ = 0; }; }