PNGReader.cpp 3.4 KB

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