PNGReader.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <iostream>
  2. #include <png.h>
  3. #include <cstring>
  4. #include "client/utils/PNGReader.h"
  5. static bool checkSignature(const char* path, FILE* file) {
  6. unsigned char buffer[8];
  7. if(fread(buffer, sizeof (char), 8, file) != 8) {
  8. std::cout << "cannot read signature of texture '" << path << "'\n";
  9. return true;
  10. }
  11. if(png_sig_cmp(buffer, 0, 8)) {
  12. std::cout << "file '" << path << "' is not a texture\n";
  13. return true;
  14. }
  15. return false;
  16. }
  17. static bool load(const char* path, FILE* file, u32* buffer, u32& maxWidth, u32& maxHeight) {
  18. if(checkSignature(path, file)) {
  19. return false;
  20. }
  21. png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  22. if(png == nullptr) {
  23. std::cout << "cannot create texture data structure\n";
  24. return false;
  25. }
  26. png_infop info = png_create_info_struct(png);
  27. if(info == nullptr) {
  28. std::cout << "cannot create image info structure\n";
  29. return false;
  30. }
  31. u32** rowPointers = nullptr;
  32. if(setjmp(png_jmpbuf(png))) { // set callback for errors
  33. if(rowPointers != nullptr) {
  34. png_free(png, rowPointers);
  35. }
  36. png_destroy_read_struct(&png, &info, nullptr);
  37. std::cout << "texture '" << path << "' has used error callback\n";
  38. return false;
  39. }
  40. png_init_io(png, file); // set reading function
  41. png_set_sig_bytes(png, 8); // notify about already used signature bytes
  42. png_read_info(png, info); // read info data
  43. u32 width = png_get_image_width(png, info);
  44. u32 height = png_get_image_height(png, info);
  45. if(width > maxWidth || height > maxHeight) {
  46. std::cout << "texture '" << path << "' is too big: " << maxWidth << " x " << maxHeight << " allowed\n";
  47. return false;
  48. }
  49. maxWidth = width;
  50. maxHeight = height;
  51. // allocate and set row pointer to correct places in block
  52. rowPointers = static_cast<u32**> (png_malloc(png, height * (sizeof (u32*))));
  53. for(uint i = 0; i < height; i++) {
  54. rowPointers[i] = (buffer + i * width);
  55. }
  56. png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));
  57. png_read_image(png, reinterpret_cast<png_bytepp> (rowPointers));
  58. png_free(png, rowPointers);
  59. png_destroy_read_struct(&png, &info, nullptr);
  60. return true;
  61. }
  62. bool PNGReader::load(const char* path, u32* buffer, u32& maxWidth, u32& maxHeight) {
  63. FILE* file = fopen(path, "r");
  64. if(file == nullptr) {
  65. std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
  66. return false;
  67. }
  68. bool result = load(path, file, buffer, maxWidth, maxHeight);
  69. fclose(file);
  70. return result;
  71. }