// CTK includes #include "ctkWidgetsTestingUtilities.h" // Qt includes #include // STD includes #include namespace ctkWidgetsTestingUtilities { //---------------------------------------------------------------------------- */ bool CompareImages(const QImage& current, const QImage& expected, float percentThreshold) { if (current.width() != expected.width() || current.height() != expected.height() || current.format() != expected.format()) { return false; } if (current.format() != QImage::Format_RGB32) { std::cerr << "ERROR: CompareImages: Unsupported QImage::Format: " << static_cast(current.format()) << std::endl; return false; } // Compute number of pixels that differ, masking out alpha channel. // Based on QImage::operator== implementation. unsigned long numDiffs = 0; for (int line = 0; line < current.height(); line++) { int w = current.width(); const QRgb* p1 = reinterpret_cast(current.constScanLine(line)); const QRgb* p2 = reinterpret_cast(expected.constScanLine(line)); while (w--) { if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff)) { ++numDiffs; } } } const int numPixels = current.width() * current.height(); const float percentDifferent = (numPixels > 0) ? ((100.f * numDiffs) / numPixels) : 0.f; return (percentDifferent <= percentThreshold); } } // namespace ctkWidgetsTestingUtilities