#include "pch.h" #include "constants.h" #include "EdgeDetection.h" template inline long FindEdge(const BGRATextureView& texture, const POINT centerPoint, const uint8_t tolerance) { using namespace consts; long xOffset = 0; long yOffset = 0; // For continuous capture, we'll be a bit off center from the cursor so the cross we draw won't interfere with the measurement. if constexpr (ContinuousCapture) { if constexpr (IsX) { xOffset = Increment ? CURSOR_OFFSET_AMOUNT_X : -CURSOR_OFFSET_AMOUNT_X; yOffset = 1; } else { xOffset = 1; yOffset = Increment ? CURSOR_OFFSET_AMOUNT_Y : -CURSOR_OFFSET_AMOUNT_Y; } } const size_t maxDim = IsX ? texture.width : texture.height; long x = std::clamp(centerPoint.x + xOffset, 1, static_cast(texture.width - 2)); long y = std::clamp(centerPoint.y + yOffset, 1, static_cast(texture.height - 2)); const uint32_t startPixel = texture.GetPixel(x, y); while (true) { long oldX = x; long oldY = y; if constexpr (IsX) { if constexpr (Increment) { if (++x == maxDim) break; } else { if (--x == 0) break; } } else { if constexpr (Increment) { if (++y == maxDim) break; } else { if (--y == 0) break; } } const uint32_t nextPixel = texture.GetPixel(x, y); if (!texture.PixelsClose(startPixel, nextPixel, tolerance)) { return IsX ? oldX : oldY; } } return Increment ? static_cast(IsX ? texture.width : texture.height) - 1 : 0; } template inline RECT DetectEdgesInternal(const BGRATextureView& texture, const POINT centerPoint, const uint8_t tolerance) { return RECT{ .left = FindEdge(texture, centerPoint, tolerance), .top = FindEdge(texture, centerPoint, tolerance), .right = FindEdge(texture, centerPoint, tolerance), .bottom = FindEdge(texture, centerPoint, tolerance) }; } RECT DetectEdges(const BGRATextureView& texture, const POINT centerPoint, const bool perChannel, const uint8_t tolerance, const bool continuousCapture) { auto function = perChannel ? &DetectEdgesInternal : DetectEdgesInternal; if (continuousCapture) function = perChannel ? &DetectEdgesInternal : &DetectEdgesInternal; return function(texture, centerPoint, tolerance); }