PNGReader.cpp 3.5 KB

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