PNGReaderTests.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "tests/PNGReaderTests.h"
  2. #include "images/PNGReader.h"
  3. #include "tests/Test.h"
  4. #include "utils/List.h"
  5. #include "utils/StringBuffer.h"
  6. static void testRead(Test& test, PNGReader& png, const char* text) {
  7. List<ColorChannel> buffer;
  8. buffer.resize(png.getBufferSize());
  9. test.checkEqual(true, png.getBufferSize() > 0, text);
  10. test.checkEqual(png.getBufferSize(), buffer.getLength(), text);
  11. if(png.getBufferSize() == buffer.getLength()) {
  12. test.checkEqual(false, png.readData(buffer.begin()), text);
  13. }
  14. }
  15. static void testReadRGB8(Test& test, const char* path) {
  16. PNGReader png(StringBuffer<200>(path).append("rgb8.png"));
  17. if(png.hasError()) {
  18. test.checkEqual(false, true, "read rgb8 error");
  19. return;
  20. }
  21. test.checkEqual(32, png.getWidth(), "rgb8 width");
  22. test.checkEqual(64, png.getHeight(), "rgb8 height");
  23. test.checkEqual(3, png.getChannels(), "rgb8 channels");
  24. testRead(test, png, "rgb8 read");
  25. }
  26. static void testReadRGB16(Test& test, const char* path) {
  27. PNGReader png(StringBuffer<200>(path).append("rgb16.png"));
  28. if(png.hasError()) {
  29. test.checkEqual(false, true, "read rgb16 error");
  30. return;
  31. }
  32. test.checkEqual(32, png.getWidth(), "rgb16 width");
  33. test.checkEqual(64, png.getHeight(), "rgb16 height");
  34. test.checkEqual(3, png.getChannels(), "rgb16 channels");
  35. test.checkEqual(32 * 64 * 3, png.getBufferSize(), "rgb16 channels");
  36. testRead(test, png, "rgb16 read");
  37. }
  38. static void testReadRGBA8(Test& test, const char* path) {
  39. PNGReader png(StringBuffer<200>(path).append("rgba8.png"));
  40. if(png.hasError()) {
  41. test.checkEqual(false, true, "read rgba8 error");
  42. return;
  43. }
  44. test.checkEqual(32, png.getWidth(), "rgba8 width");
  45. test.checkEqual(64, png.getHeight(), "rgba8 height");
  46. test.checkEqual(4, png.getChannels(), "rgba8 channels");
  47. test.checkEqual(32 * 64 * 4, png.getBufferSize(), "rgba8 channels");
  48. testRead(test, png, "rgba8 read");
  49. }
  50. static void testReadRGBA16(Test& test, const char* path) {
  51. PNGReader png(StringBuffer<200>(path).append("rgba16.png"));
  52. if(png.hasError()) {
  53. test.checkEqual(false, true, "read rgba16 error");
  54. return;
  55. }
  56. test.checkEqual(32, png.getWidth(), "rgba16 width");
  57. test.checkEqual(64, png.getHeight(), "rgba16 height");
  58. test.checkEqual(4, png.getChannels(), "rgba16 channels");
  59. test.checkEqual(32 * 64 * 4, png.getBufferSize(), "rgba16 channels");
  60. testRead(test, png, "rgba16 read");
  61. }
  62. static void testReadGray8(Test& test, const char* path) {
  63. PNGReader png(StringBuffer<200>(path).append("gray8.png"));
  64. if(png.hasError()) {
  65. test.checkEqual(false, true, "read gray8 error");
  66. return;
  67. }
  68. test.checkEqual(32, png.getWidth(), "gray8 width");
  69. test.checkEqual(64, png.getHeight(), "gray8 height");
  70. test.checkEqual(1, png.getChannels(), "gray8 channels");
  71. test.checkEqual(32 * 64, png.getBufferSize(), "gray8 channels");
  72. testRead(test, png, "gray8 read");
  73. }
  74. static void testReadGray16(Test& test, const char* path) {
  75. PNGReader png(StringBuffer<200>(path).append("gray16.png"));
  76. if(png.hasError()) {
  77. test.checkEqual(false, true, "read gray16 error");
  78. return;
  79. }
  80. test.checkEqual(32, png.getWidth(), "gray16 width");
  81. test.checkEqual(64, png.getHeight(), "gray16 height");
  82. test.checkEqual(1, png.getChannels(), "gray16 channels");
  83. test.checkEqual(32 * 64, png.getBufferSize(), "gray16 channels");
  84. testRead(test, png, "gray16 read");
  85. }
  86. static void testReadGrayA8(Test& test, const char* path) {
  87. PNGReader png(StringBuffer<200>(path).append("graya8.png"));
  88. if(png.hasError()) {
  89. test.checkEqual(false, true, "read graya8 error");
  90. return;
  91. }
  92. test.checkEqual(32, png.getWidth(), "graya8 width");
  93. test.checkEqual(64, png.getHeight(), "graya8 height");
  94. test.checkEqual(2, png.getChannels(), "graya8 channels");
  95. test.checkEqual(32 * 64 * 2, png.getBufferSize(), "graya8 channels");
  96. testRead(test, png, "graya8 read");
  97. }
  98. static void testReadGrayA16(Test& test, const char* path) {
  99. PNGReader png(StringBuffer<200>(path).append("graya16.png"));
  100. if(png.hasError()) {
  101. test.checkEqual(false, true, "read graya16 error");
  102. return;
  103. }
  104. test.checkEqual(32, png.getWidth(), "graya16 width");
  105. test.checkEqual(64, png.getHeight(), "graya16 height");
  106. test.checkEqual(2, png.getChannels(), "graya16 channels");
  107. test.checkEqual(32 * 64 * 2, png.getBufferSize(), "graya16 channels");
  108. testRead(test, png, "graya16 read");
  109. }
  110. void PNGReaderTests::test(const char* path) {
  111. Test test("PNGReader");
  112. testReadRGB8(test, path);
  113. testReadRGB16(test, path);
  114. testReadRGBA8(test, path);
  115. testReadRGBA16(test, path);
  116. testReadGray8(test, path);
  117. testReadGray16(test, path);
  118. testReadGrayA8(test, path);
  119. testReadGrayA16(test, path);
  120. test.finalize();
  121. }