Skip to content

Commit

Permalink
added Image::operator==
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Nov 12, 2024
1 parent 91decec commit e8eeaeb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/libYARP_sig/src/yarp/sig/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions src/libYARP_sig/src/yarp/sig/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
29 changes: 29 additions & 0 deletions src/libYARP_sig/tests/ImageTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,5 +873,34 @@ TEST_CASE("sig::ImageTest", "[yarp::sig]")
CHECK(ok);
}

SECTION("test Image::operator ==()")
{
ImageOf<PixelRgb> Img1;
ImageOf<PixelRgb> Img2;
ImageOf<PixelRgb> 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);
}

0 comments on commit e8eeaeb

Please sign in to comment.