From 3a2ae4ede1d7bb180bc312ddb8f3dd711ba12130 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 2 May 2022 01:02:46 +0200 Subject: [PATCH] WIP: More tests for lay::Image --- src/laybasic/laybasic/layImage.h | 3 +- src/laybasic/unit_tests/layImageTests.cc | 71 ++++++++++++++++++++++- testdata/lay/au.png | Bin 0 -> 777 bytes testdata/lay/au_diff.png | Bin 0 -> 789 bytes 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 testdata/lay/au.png create mode 100644 testdata/lay/au_diff.png diff --git a/src/laybasic/laybasic/layImage.h b/src/laybasic/laybasic/layImage.h index 7816a5eee..bc0a868f5 100644 --- a/src/laybasic/laybasic/layImage.h +++ b/src/laybasic/laybasic/layImage.h @@ -163,7 +163,8 @@ public: * * This feature produces a binary-alpha image of *this and other. The * result can be patched into this image to render the same image than - * "other". + * "other". The difference image will contains the pixels from other which + * are different from *this. * * alpha values from this and other are ignored. */ diff --git a/src/laybasic/unit_tests/layImageTests.cc b/src/laybasic/unit_tests/layImageTests.cc index 896116392..5e02999ed 100644 --- a/src/laybasic/unit_tests/layImageTests.cc +++ b/src/laybasic/unit_tests/layImageTests.cc @@ -25,7 +25,28 @@ #include "tlUnitTest.h" #if defined(HAVE_QT) -#include + +# include + +static bool compare_images (const QImage &qimg, const std::string &au) +{ + QImage qimg2; + qimg2.load (tl::to_qstring (au)); + + if (qimg2.width () == (int) qimg.width () && qimg2.height () == (int) qimg.height ()) { + for (int i = 0; i < qimg.width (); ++i) { + for (int j = 0; j < qimg.height (); ++j) { + if (((const lay::color_t *) qimg.scanLine (j))[i] != ((const lay::color_t *) qimg2.scanLine (j))[i]) { + return false; + } + } + } + return true; + } else { + return false; + } +} + #endif TEST(1) @@ -90,3 +111,51 @@ TEST(1) EXPECT_EQ (img4.height (), 16); EXPECT_EQ (img4.scan_line (5)[8], 0x102030); } + +#if defined(HAVE_QT) + +TEST(2) +{ + lay::Image img (227, 231); + + for (unsigned int i = 0; i < img.width (); ++i) { + for (unsigned int j = 0; j < img.height (); ++j) { + img.scan_line (j) [i] = 0xff000000 | (i << 16) | j; + } + } + + std::string tmp = tmp_file ("test.png"); + QImage qimg = img.to_image (); + qimg.save (tl::to_qstring (tmp)); + tl::info << "PNG file written to " << tmp; + + std::string au = tl::testsrc () + "/testdata/lay/au.png"; + tl::info << "PNG file read from " << au; + + EXPECT_EQ (compare_images (qimg, au), true); + + lay::Image img_saved (img); + img.scan_line (52) [42] = 0xff000000; + + lay::Image diff = img.diff (img_saved); + EXPECT_EQ (compare_images (img.to_image (), au), false); + EXPECT_EQ (compare_images (img_saved.to_image (), au), true); + + img.patch (diff); + EXPECT_EQ (compare_images (img.to_image (), au), true); + + img.fill (0xff000000); + img.patch (diff); + + tmp = tmp_file ("diff.png"); + qimg = img.to_image (); + qimg.save (tl::to_qstring (tmp)); + tl::info << "PNG file written to " << tmp; + + au = tl::testsrc () + "/testdata/lay/au_diff.png"; + tl::info << "PNG file read from " << au; + + EXPECT_EQ (compare_images (qimg, au), true); +} + +#endif diff --git a/testdata/lay/au.png b/testdata/lay/au.png new file mode 100644 index 0000000000000000000000000000000000000000..bc4c91684efb321915f281edf82db931084f3f7b GIT binary patch literal 777 zcmeAS@N?(olHy`uVBq!ia0vp^kAe6(2OE%lBmXc8NO2Z;L>4nJ@ErkR#;MwT(hLkt zyFFbTLn`LHy?l`KfB^%uA^WcXr`|4hej+C%;a`w`yL#Kqn|E#hGj8t`R64>TnAE~y w=+q*i=+q;i=++^i?9wqRJQ^kglOP^3bMdkz8g%lD0Mj;ur>mdKI;Vst0FE=x_5c6? literal 0 HcmV?d00001 diff --git a/testdata/lay/au_diff.png b/testdata/lay/au_diff.png new file mode 100644 index 0000000000000000000000000000000000000000..f29dacca61cec48e01a6a80d86d09c819f01b97c GIT binary patch literal 789 zcmeAS@N?(olHy`uVBq!ia0vp^kAe6(2OE%lBmXc8NO2Z;L>4nJ@ErkR#;MwT(hLkt z$30yfLn`LHy=utY5Fo;GkiBTd$9!HclL`ALNU~nf$(ubhyW#1#tIQ4U5{gbe0*Y=O z0?IBOf=Wj?1e01g44qmAO8DbUiGIs)|Az2f