diff --git a/src/libYARP_sig/src/yarp/sig/Image.cpp b/src/libYARP_sig/src/yarp/sig/Image.cpp index 8768ae18aa..2c3c26eb41 100644 --- a/src/libYARP_sig/src/yarp/sig/Image.cpp +++ b/src/libYARP_sig/src/yarp/sig/Image.cpp @@ -590,6 +590,27 @@ Image& Image::operator=(Image&& other) noexcept return *this; } +bool Image::operator==(const Image& alt) const +{ + //test general properties + if (width() != alt.width()) return false; + if (height() != alt.height()) return false; + if (imgPixelCode != alt.imgPixelCode) return false; + size_t raw1size = getRawImageSize(); + size_t raw2size = alt.getRawImageSize(); + if (raw1size != raw2size) + { + return false; + } + //test byte per byte + unsigned char* raw1 = getRawImage(); + unsigned char* raw2 = alt.getRawImage(); + for (size_t i = 0; i < raw1size; i++, raw1++, raw2++) + { + if (*raw1 != *raw2) { return false; } + } + return true; +} Image& Image::operator=(const Image& alt) { diff --git a/src/libYARP_sig/src/yarp/sig/Image.h b/src/libYARP_sig/src/yarp/sig/Image.h index 3a3f265547..12768ac5a8 100644 --- a/src/libYARP_sig/src/yarp/sig/Image.h +++ b/src/libYARP_sig/src/yarp/sig/Image.h @@ -115,6 +115,14 @@ class YARP_sig_API yarp::sig::Image : public yarp::os::Portable { */ Image& operator=(Image &&other) noexcept; + /** + * Comparison operator. + * N.B. Comparing two images can be computationally intensive. + * This method is mainly used for CI purposes. + * @return true if the two images are identical + */ + bool operator==(const Image& alt) const; + /** * Destructor. */ diff --git a/src/libYARP_sig/tests/ImageTest.cpp b/src/libYARP_sig/tests/ImageTest.cpp index 81da558775..cea25732ff 100644 --- a/src/libYARP_sig/tests/ImageTest.cpp +++ b/src/libYARP_sig/tests/ImageTest.cpp @@ -873,5 +873,34 @@ TEST_CASE("sig::ImageTest", "[yarp::sig]") CHECK(ok); } + SECTION("test Image::operator ==()") + { + ImageOf Img1; + ImageOf Img2; + ImageOf Img3; + Img1.resize(32, 16); + Img2.resize(32, 16); + Img3.resize(32, 16); + + for (size_t iy = 0; iy < Img1.height(); iy++) + for (size_t ix = 0; ix < Img1.width(); ix++) + { + Img1.pixel(ix, iy).r = 10; + Img1.pixel(ix, iy).g = 11; + Img1.pixel(ix, iy).b = 12; + + Img2.pixel(ix, iy).r = 10; + Img2.pixel(ix, iy).g = 11; + Img2.pixel(ix, iy).b = 12; + + Img3.pixel(ix, iy).r = 20; + Img3.pixel(ix, iy).g = 21; + Img3.pixel(ix, iy).b = 22; + } + + CHECK (Img1 == Img2); + CHECK(!(Img1 == Img3)); + } + NetworkBase::setLocalMode(false); }