PNGReader.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 u32* load(const char* path, FILE* file, u32& width, u32& height) {
  18. if(checkSignature(path, file)) {
  19. return nullptr;
  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 nullptr;
  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 nullptr;
  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 nullptr;
  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. width = png_get_image_width(png, info);
  44. height = png_get_image_height(png, info);
  45. // allocate and set row pointer to correct places in block
  46. u32* data = new u32[width * height];
  47. rowPointers = static_cast<u32**> (png_malloc(png, height * (sizeof (u32*))));
  48. for(uint i = 0; i < height; i++) {
  49. rowPointers[i] = (data + i * width);
  50. }
  51. png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));
  52. png_read_image(png, reinterpret_cast<png_bytepp> (rowPointers));
  53. png_free(png, rowPointers);
  54. png_destroy_read_struct(&png, &info, nullptr);
  55. return data;
  56. }
  57. u32* PNGReader::load(const char* path, u32& width, u32& height) {
  58. FILE* file = fopen(path, "r");
  59. if(file == nullptr) {
  60. std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
  61. return nullptr;
  62. }
  63. u32* data = load(path, file, width, height);
  64. fclose(file);
  65. return data;
  66. }