diff --git a/Source/Image.cc b/Source/Image.cc index 8d3f461..ad3b5b8 100644 --- a/Source/Image.cc +++ b/Source/Image.cc @@ -153,6 +153,49 @@ void Image::Destroy (void) //////////////////////////////////////////////////////////////////////////////// +// Find source from target (this=target, image=source) +// If found, returns relative coordinates(Point) from target image +// If not found, returns {-1,-1} +Point Image::Find(const Image & image) +{ + if (!IsValid() || !image.IsValid()) + return{ -1, -1 }; + + const auto targetWidth = GetWidth(); + const auto targetHeight = GetHeight(); + const auto sourceWidth = image.GetWidth(); + const auto sourceHeight = image.GetHeight(); + + if (targetWidth >= sourceWidth && targetHeight >= sourceHeight) + { + bool mismatch; + for (int y = 0; y < targetHeight - sourceHeight; y++) + { + for (int x = 0; x < targetWidth - sourceWidth; x++) + { + mismatch = false; + for (int y2 = 0; !mismatch && y2 < sourceHeight; y2++) + { + for (int x2 = 0; !mismatch && x2 < sourceWidth; x2++) + { + auto nCol1 = GetPixel(x + x2, y + y2); + auto nCol2 = image.GetPixel(x2, y2); + if (nCol1 != nCol2) + mismatch = true; + } + } + + if (mismatch == false) // == found + return{ x, y }; + } + } + } + + return{ -1, -1 }; +} + +//////////////////////////////////////////////////////////////////////////////// + Color Image::GetPixel (const Point& point) const { return GetPixel (point.X, point.Y); diff --git a/Source/Image.h b/Source/Image.h index cbc8609..ba4b75d 100644 --- a/Source/Image.h +++ b/Source/Image.h @@ -46,6 +46,8 @@ class ROBOT_EXPORT Image bool Create (uint16 w, uint16 h); void Destroy (void); + Point Find(const Image &image); + uint16 GetWidth (void) const { return mWidth; } uint16 GetHeight (void) const { return mHeight; } uint32 GetLength (void) const { return mLength; }