Skip to content

Commit

Permalink
serialize watermark as golden standard
Browse files Browse the repository at this point in the history
  • Loading branch information
siers committed Oct 2, 2024
1 parent 98dd1c3 commit c51650a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
30 changes: 30 additions & 0 deletions src/SipiImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,36 @@ bool SipiImage::operator==(const SipiImage &rhs) const

/*==========================================================================*/

std::optional<double> SipiImage::compare(const SipiImage &rhs) const
{
if ((nx != rhs.nx) || (ny != rhs.ny) || (nc != rhs.nc) || (bps != rhs.bps) || (photo != rhs.photo)) { return {}; }

double diff = 0;
double niters = 0;

switch (bps) {
case 8: {
byte *ltmp1 = pixels;
byte *ltmp2 = rhs.pixels;
for (size_t j = 0; j < ny; j++) {
for (size_t i = 0; i < nx; i++) {
for (size_t k = 0; k < nc; k++) {
niters++;
diff += abs(ltmp1[nc * (j * nx + i) + k] - ltmp2[nc * (j * nx + i) + k]) / 255.;
}
}
}
break;
}
case 16:
return 1;
}

return diff / niters;
}

/*==========================================================================*/


std::ostream &operator<<(std::ostream &outstr, const SipiImage &rhs)
{
Expand Down
8 changes: 7 additions & 1 deletion src/SipiImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,20 @@ class SipiImage
*/
bool toBitonal();

/*!
* Conclude similarity of two SipiImages, used in tests. Only tested with small differences.
*
* \returns Returns similarity index, returns in [0..1]
*/
std::optional<double> compare(const SipiImage &rhs) const;

/*!
* Add a watermark to a file...
*
* \param[in] wmfilename Path to watermarkfile (which must be a TIFF file at the moment)
*/
void add_watermark(const std::string &wmfilename);


/*!
* Calculates the difference between 2 images.
*
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions test/unit/sipiimage/sipiimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ TEST(SipiImage, Watermark)
Sipi::SipiImage img3;
Sipi::SipiImage img4;

std::string maori = "../../../../test/_test_data/images/unit/MaoriFigureInAKaitakaCloak.jpg";
std::string maori = "../../../../test/_test_data/images/unit/MaoriFigure.jpg";
std::string gradstars = "../../../../test/_test_data/images/unit/gradient-stars.tif";
std::string maoriWater = "../../../../test/_test_data/images/unit/MaoriFigureWatermark.jpg";

EXPECT_TRUE(exists_file(maori));
EXPECT_TRUE(exists_file(gradstars));
Expand All @@ -323,8 +324,17 @@ TEST(SipiImage, Watermark)
EXPECT_NO_THROW(img2.add_watermark(watermark_correct));

ASSERT_NO_THROW(img3.read(maori));
ASSERT_NO_THROW(img3.rotate(90));

EXPECT_NO_THROW(img3.add_watermark(gradstars));
/* ASSERT_NO_THROW(img3.write("jpg", maoriWater)); */

ASSERT_NO_THROW(img4.read(maoriWater));
EXPECT_TRUE(img4.compare(img3).value_or(1000) < 0.007); // 0.00605

ASSERT_NO_THROW(img3.read(maori));
EXPECT_TRUE(img4.compare(img3) > 0.017); // 0.0174

ASSERT_NO_THROW(img3.rotate(90));
}

TEST(SipiImage, CMYK_With_Alpha_Conversion)
Expand Down

0 comments on commit c51650a

Please sign in to comment.