PNGReader.cpp 3.4 KB

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