PNGReader.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "images/PNGReader.h"
  2. PNGReader::PNGReader()
  3. : path(""), width(0), height(0), channels(0), bitDepth(0), rowBytes(0),
  4. file(nullptr), read(nullptr), info(nullptr), rowPointers(nullptr) {
  5. }
  6. PNGReader::~PNGReader() {
  7. if(file != nullptr) {
  8. fclose(file);
  9. }
  10. if(rowPointers != nullptr) {
  11. png_free(read, rowPointers);
  12. }
  13. png_destroy_read_struct(&read, &info, nullptr);
  14. }
  15. Error PNGReader::load(const char* path) {
  16. if(file != nullptr) {
  17. Error error = {"file '"};
  18. error.message.append(path).append("' is alreaded loaded");
  19. return error;
  20. }
  21. PNGReader::path = path;
  22. file = fopen(path, "rb");
  23. if(file == nullptr) {
  24. Error error = {"file '"};
  25. error.message.append(path)
  26. .append("' cannot be read: ")
  27. .append(static_cast<const char*>(strerror(errno)));
  28. return error;
  29. }
  30. png_byte buffer[8];
  31. if(fread(buffer, sizeof(png_byte), 8, file) != 8) {
  32. Error error = {"cannot read signature of file '"};
  33. error.message.append(path).append("'");
  34. return error;
  35. }
  36. if(png_sig_cmp(buffer, 0, 8)) {
  37. Error error = {"file '"};
  38. error.message.append(path).append("' is not a png");
  39. return error;
  40. }
  41. read = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
  42. nullptr);
  43. if(read == nullptr) {
  44. return {"cannot create png read data structure"};
  45. }
  46. info = png_create_info_struct(read);
  47. if(info == nullptr) {
  48. return {"cannot create png info structure"};
  49. }
  50. if(setjmp(png_jmpbuf(read))) {
  51. Error error = {"png file '"};
  52. error.message.append(path).append("' has used error callback");
  53. return error;
  54. }
  55. png_init_io(read, file);
  56. png_set_sig_bytes(read, 8);
  57. png_read_info(read, info);
  58. png_set_expand(read);
  59. width = png_get_image_width(read, info);
  60. height = png_get_image_height(read, info);
  61. channels = png_get_channels(read, info);
  62. bitDepth = png_get_bit_depth(read, info);
  63. if(png_get_bit_depth(read, info) == 16) {
  64. png_set_strip_16(read);
  65. }
  66. rowBytes = png_get_rowbytes(read, info);
  67. if(channels < 1 || channels > 4) {
  68. Error error = {"'"};
  69. error.message.append(path)
  70. .append("' has unsupported number of channels: ")
  71. .append(channels);
  72. return error;
  73. } else if(width < 1 || width > 2048 || height < 1 || height > 2048) {
  74. Error error = {"width and height of '"};
  75. error.message.append(path).append("' are too big");
  76. return error;
  77. } else if(bitDepth != 8 && bitDepth != 16) {
  78. Error error = {"bit depth of '"};
  79. error.message.append(path).append("' is neither 8 or 16");
  80. return error;
  81. } else if(getBufferSize() != (rowBytes * height * 8 / bitDepth)) {
  82. Error error = {"'"};
  83. error.message.append(path).append("' needs an unexpected buffer size");
  84. return error;
  85. }
  86. return {};
  87. }
  88. int PNGReader::getWidth() const {
  89. return width;
  90. }
  91. int PNGReader::getHeight() const {
  92. return height;
  93. }
  94. int PNGReader::getChannels() const {
  95. return channels;
  96. }
  97. int PNGReader::getBufferSize() const {
  98. return width * height * channels;
  99. }
  100. Error PNGReader::readData(ColorChannel* buffer) {
  101. if(setjmp(png_jmpbuf(read))) {
  102. Error error = {"png file '"};
  103. error.message.append(path).append("' has used error callback");
  104. return error;
  105. }
  106. rowPointers = static_cast<ColorChannel**>(
  107. png_malloc(read, height * sizeof(ColorChannel*)));
  108. for(int i = 0; i < height; i++) {
  109. rowPointers[i] = (buffer + i * width * channels);
  110. }
  111. png_set_rows(read, info, reinterpret_cast<png_bytepp>(rowPointers));
  112. png_read_image(read, reinterpret_cast<png_bytepp>(rowPointers));
  113. return {};
  114. }