12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #include <iostream>
- #include <png.h>
- #include <cstring>
- #include "client/utils/PNGReader.h"
- static bool checkSignature(const char* path, FILE* file) {
- unsigned char buffer[8];
- if(fread(buffer, sizeof (char), 8, file) != 8) {
- std::cout << "cannot read signature of texture '" << path << "'\n";
- return true;
- }
- if(png_sig_cmp(buffer, 0, 8)) {
- std::cout << "file '" << path << "' is not a texture\n";
- return true;
- }
- return false;
- }
- static u32* load(const char* path, FILE* file, u32& width, u32& height) {
- if(checkSignature(path, file)) {
- return nullptr;
- }
- png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
- if(png == nullptr) {
- std::cout << "cannot create texture data structure\n";
- return nullptr;
- }
- png_infop info = png_create_info_struct(png);
- if(info == nullptr) {
- std::cout << "cannot create image info structure\n";
- return nullptr;
- }
- u32** rowPointers = nullptr;
- if(setjmp(png_jmpbuf(png))) { // set callback for errors
- if(rowPointers != nullptr) {
- png_free(png, rowPointers);
- }
- png_destroy_read_struct(&png, &info, nullptr);
- std::cout << "texture '" << path << "' has used error callback\n";
- return nullptr;
- }
- png_init_io(png, file); // set reading function
- png_set_sig_bytes(png, 8); // notify about already used signature bytes
- png_read_info(png, info); // read info data
- width = png_get_image_width(png, info);
- height = png_get_image_height(png, info);
- // allocate and set row pointer to correct places in block
- u32* data = new u32[width * height];
- rowPointers = static_cast<u32**> (png_malloc(png, height * (sizeof (u32*))));
- for(uint i = 0; i < height; i++) {
- rowPointers[i] = (data + i * width);
- }
- png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));
- png_read_image(png, reinterpret_cast<png_bytepp> (rowPointers));
- png_free(png, rowPointers);
- png_destroy_read_struct(&png, &info, nullptr);
- return data;
- }
- u32* PNGReader::load(const char* path, u32& width, u32& height) {
- FILE* file = fopen(path, "r");
- if(file == nullptr) {
- std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
- return nullptr;
- }
- u32* data = load(path, file, width, height);
- fclose(file);
- return data;
- }
|