From 6f7a97d0b46b5f5764220042fc8b4b1841b6fff0 Mon Sep 17 00:00:00 2001 From: Fidel Yin Date: Tue, 7 Nov 2023 20:29:40 -0500 Subject: [PATCH] Apply optimization proposed in https://github.com/ra1nty/DXcam/pull/62 --- src/core/Processor.cpp | 50 ++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/core/Processor.cpp b/src/core/Processor.cpp index 3f5783a..91abceb 100644 --- a/src/core/Processor.cpp +++ b/src/core/Processor.cpp @@ -6,20 +6,46 @@ namespace DXCam { -cv::Mat Processor::process(const DXGI_MAPPED_RECT &rect, const int width, - const int height, const Region ®ion, - const int rotation_angle) { - const auto pitch = rect.Pitch / 4; +cv::Mat Processor::process(const DXGI_MAPPED_RECT &rect, int width, int height, + const Region ®ion, const int rotation_angle) { + auto pitch = rect.Pitch; + // Pre-crop + int offset = 0; + switch (rotation_angle) { + case 0: + offset = region.top * pitch; + height = region.bottom - region.top; + break; + case 90: + offset = (width - region.right) * pitch; + width = region.right - region.left; + break; + case 180: + offset = (height - region.bottom) * pitch; + height = region.bottom - region.top; + break; + case 270: + offset = region.left * pitch; + width = region.right - region.left; + break; + default: + assert(false); // never reach + break; + } + + pitch /= 4; // number of channels cv::Mat image; if (rotation_angle == 0 || rotation_angle == 180) { - image = cv::Mat(height, pitch, CV_8UC4, rect.pBits); + image = cv::Mat(height, pitch, CV_8UC4, rect.pBits + offset); } else { - image = cv::Mat(width, pitch, CV_8UC4, rect.pBits); + image = cv::Mat(width, pitch, CV_8UC4, rect.pBits + offset); } - // TODO untested + // Rotation switch (rotation_angle) { + case 0: + break; case 90: cv::rotate(image, image, cv::ROTATE_90_CLOCKWISE); break; @@ -30,13 +56,15 @@ cv::Mat Processor::process(const DXGI_MAPPED_RECT &rect, const int width, cv::rotate(image, image, cv::ROTATE_90_COUNTERCLOCKWISE); break; default: + assert(false); // never reach break; } - if (region.right - region.left != width || - region.bottom - region.top != height) { - image = image(cv::Range(region.top, region.bottom), - cv::Range(region.left, region.right)); + // Crop + if (region.right - region.left != width) { + image = image.colRange(region.left, region.right); + } else if (region.bottom - region.top != height) { + image = image.rowRange(region.top, region.bottom); } return image;