ctkWidgetsTestingUtilities.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // CTK includes
  2. #include "ctkWidgetsTestingUtilities.h"
  3. // Qt includes
  4. #include <QImage>
  5. // STD includes
  6. #include <iostream>
  7. namespace ctkWidgetsTestingUtilities
  8. {
  9. //---------------------------------------------------------------------------- */
  10. bool CompareImages(const QImage& current, const QImage& expected,
  11. float percentThreshold)
  12. {
  13. if (current.width() != expected.width() ||
  14. current.height() != expected.height() ||
  15. current.format() != expected.format())
  16. {
  17. return false;
  18. }
  19. if (current.format() != QImage::Format_RGB32)
  20. {
  21. std::cerr << "ERROR: CompareImages: Unsupported QImage::Format: "
  22. << static_cast<int>(current.format()) << std::endl;
  23. return false;
  24. }
  25. // Compute number of pixels that differ, masking out alpha channel.
  26. // Based on QImage::operator== implementation.
  27. unsigned long numDiffs = 0;
  28. for (int line = 0; line < current.height(); line++)
  29. {
  30. int w = current.width();
  31. const QRgb* p1 = reinterpret_cast<const QRgb*>(current.constScanLine(line));
  32. const QRgb* p2 = reinterpret_cast<const QRgb*>(expected.constScanLine(line));
  33. while (w--)
  34. {
  35. if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff))
  36. {
  37. ++numDiffs;
  38. }
  39. }
  40. }
  41. const int numPixels = current.width() * current.height();
  42. const float percentDifferent = (numPixels > 0) ? ((100.f * numDiffs) / numPixels) : 0.f;
  43. return (percentDifferent <= percentThreshold);
  44. }
  45. } // namespace ctkWidgetsTestingUtilities