ctkWidgetsTestingUtilitiesTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // CTK includes
  15. #include "ctkWidgetsTestingUtilities.h"
  16. // Qt includes
  17. #include <QColor>
  18. #include <QImage>
  19. // STD includes
  20. #include <iostream>
  21. using namespace ctkWidgetsTestingUtilities;
  22. //----------------------------------------------------------------------------
  23. bool TestCheckImagesEqual();
  24. //----------------------------------------------------------------------------
  25. int ctkWidgetsTestingUtilitiesTest(int , char * [])
  26. {
  27. bool res = true;
  28. res = res && TestCheckImagesEqual();
  29. return res ? EXIT_SUCCESS : EXIT_FAILURE;
  30. }
  31. //----------------------------------------------------------------------------
  32. bool TestCheckImagesEqual()
  33. {
  34. {
  35. // Invalid format
  36. QImage a(1, 1, QImage::Format_Invalid);
  37. QImage b(1, 1, QImage::Format_Invalid);
  38. if (CheckImagesEqual(a, b))
  39. {
  40. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  41. return false;
  42. }
  43. }
  44. {
  45. // Unsupported format
  46. QImage a(1, 1, QImage::Format_ARGB32);
  47. QImage b(1, 1, QImage::Format_ARGB32);
  48. if (CheckImagesEqual(a, b))
  49. {
  50. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  51. return false;
  52. }
  53. }
  54. {
  55. // One image in unsupported format
  56. QImage a(1, 1, QImage::Format_RGB32);
  57. QImage b(1, 1, QImage::Format_ARGB32);
  58. if (CheckImagesEqual(a, b))
  59. {
  60. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  61. return false;
  62. }
  63. QImage c(1, 1, QImage::Format_ARGB32);
  64. QImage d(1, 1, QImage::Format_RGB32);
  65. if (CheckImagesEqual(c, d))
  66. {
  67. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  68. return false;
  69. }
  70. }
  71. {
  72. // Images of different size
  73. QImage a(1, 1, QImage::Format_RGB32);
  74. QImage b(2, 1, QImage::Format_RGB32);
  75. if (CheckImagesEqual(a, b))
  76. {
  77. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  78. return false;
  79. }
  80. QImage c(1, 2, QImage::Format_ARGB32);
  81. QImage d(1, 1, QImage::Format_RGB32);
  82. if (CheckImagesEqual(c, d))
  83. {
  84. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  85. return false;
  86. }
  87. }
  88. {
  89. // Identical images
  90. QImage a(10, 10, QImage::Format_RGB32);
  91. a.fill(Qt::green);
  92. QImage b(10, 10, QImage::Format_RGB32);
  93. b.fill(Qt::green);
  94. if (!CheckImagesEqual(a, b, 0.0f))
  95. {
  96. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  97. return false;
  98. }
  99. // Change one pixel in first image
  100. a.setPixel(2, 3, qRgb(255, 0, 0));
  101. if (CheckImagesEqual(a, b, 0.f))
  102. {
  103. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  104. return false;
  105. }
  106. // Percent threshold not met
  107. if (CheckImagesEqual(a, b, 0.5f))
  108. {
  109. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  110. return false;
  111. }
  112. // Percent threshold met
  113. if (!CheckImagesEqual(a, b, 1.f))
  114. {
  115. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  116. return false;
  117. }
  118. // Change one pixel in other image
  119. // Percent threshold not met
  120. b.setPixel(4, 5, qRgb(255, 255, 0));
  121. if (CheckImagesEqual(a, b, 1.f))
  122. {
  123. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  124. return false;
  125. }
  126. // Percent threshold met
  127. if (!CheckImagesEqual(a, b, 2.f))
  128. {
  129. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  130. return false;
  131. }
  132. // Change one pixel in first image to match second image
  133. a.setPixel(4, 5, qRgb(255, 255, 0));
  134. if (!CheckImagesEqual(a, b, 1.f))
  135. {
  136. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  137. return false;
  138. }
  139. // Identical images
  140. b.setPixel(2, 3, qRgb(255, 0, 0));
  141. if (!CheckImagesEqual(a, b, 0.f))
  142. {
  143. std::cerr << "Line " << __LINE__ << " - CheckImagesEqual failed" << std::endl;
  144. return false;
  145. }
  146. }
  147. return true;
  148. }