Skip to content

Commit

Permalink
Apply performance improve proposed in ra1nty/DXcam#62
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidelxyz committed Nov 8, 2023
1 parent 1988fa4 commit 9e9a953
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/core/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,46 @@

namespace DXCam {

cv::Mat Processor::process(const DXGI_MAPPED_RECT &rect, const int width,
const int height, const Region &region,
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 &region, 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;
Expand All @@ -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;
Expand Down

0 comments on commit 9e9a953

Please sign in to comment.