PNGReaderTests.cpp 4.8 KB

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