#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; const size_t maxDim = IsX ? texture.width : texture.height; long x = std::clamp(centerPoint.x, 1, static_cast(texture.width - 2)); long y = std::clamp(centerPoint.y, 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) { auto function = perChannel ? &DetectEdgesInternal : DetectEdgesInternal; return function(texture, centerPoint, tolerance); }