|
@@ -4,82 +4,90 @@
|
|
|
|
|
|
#include "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;
|
|
|
+PNGReader::PNGReader(const char* path) : path(path), width(0), height(0), channels(0),
|
|
|
+file(fopen(path, "r")), read(nullptr), info(nullptr), rowPointers(nullptr) {
|
|
|
+ if(file == nullptr) {
|
|
|
+ std::cout << "file '" << path << "' cannot be read: " << strerror(errno) << "\n";
|
|
|
+ return;
|
|
|
}
|
|
|
- if(png_sig_cmp(buffer, 0, 8)) {
|
|
|
- std::cout << "file '" << path << "' is not a texture\n";
|
|
|
- return true;
|
|
|
+ if(checkSignature()) {
|
|
|
+ return;
|
|
|
}
|
|
|
- return false;
|
|
|
+ read = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
|
|
+ if(read == nullptr) {
|
|
|
+ std::cout << "cannot create png read data structure\n";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ info = png_create_info_struct(read);
|
|
|
+ if(info == nullptr) {
|
|
|
+ std::cout << "cannot create png info structure\n";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(setjmp(png_jmpbuf(read))) {
|
|
|
+ std::cout << "png file '" << path << "' has used error callback\n";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ png_init_io(read, file);
|
|
|
+ png_set_sig_bytes(read, 8);
|
|
|
+ png_read_info(read, info);
|
|
|
+ width = png_get_image_width(read, info);
|
|
|
+ height = png_get_image_height(read, info);
|
|
|
+ channels = png_get_channels(read, info);
|
|
|
}
|
|
|
|
|
|
-static bool load(const char* path, FILE* file, u32* buffer, u32& maxWidth, u32& maxHeight) {
|
|
|
- if(checkSignature(path, file)) {
|
|
|
- return false;
|
|
|
+PNGReader::~PNGReader() {
|
|
|
+ if(file != nullptr) {
|
|
|
+ fclose(file);
|
|
|
}
|
|
|
-
|
|
|
- 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 false;
|
|
|
+ if(rowPointers != nullptr) {
|
|
|
+ png_free(read, rowPointers);
|
|
|
}
|
|
|
+ png_destroy_read_struct(&read, &info, nullptr);
|
|
|
+}
|
|
|
|
|
|
- png_infop info = png_create_info_struct(png);
|
|
|
- if(info == nullptr) {
|
|
|
- std::cout << "cannot create image info structure\n";
|
|
|
- return false;
|
|
|
- }
|
|
|
+int PNGReader::getWidth() const {
|
|
|
+ return width;
|
|
|
+}
|
|
|
+
|
|
|
+int PNGReader::getHeight() const {
|
|
|
+ return height;
|
|
|
+}
|
|
|
|
|
|
- u32** rowPointers = nullptr;
|
|
|
+int PNGReader::getChannels() const {
|
|
|
+ return channels;
|
|
|
+}
|
|
|
|
|
|
- if(setjmp(png_jmpbuf(png))) {
|
|
|
- if(rowPointers != nullptr) {
|
|
|
- png_free(png, rowPointers);
|
|
|
- }
|
|
|
- png_destroy_read_struct(&png, &info, nullptr);
|
|
|
- std::cout << "texture '" << path << "' has used error callback\n";
|
|
|
- return false;
|
|
|
- }
|
|
|
+int PNGReader::getBufferSize() const {
|
|
|
+ return width * height * channels;
|
|
|
+}
|
|
|
|
|
|
- png_init_io(png, file);
|
|
|
- png_set_sig_bytes(png, 8);
|
|
|
+bool PNGReader::hasError() const {
|
|
|
+ return width == 0 || height == 0 || channels == 0;
|
|
|
+}
|
|
|
|
|
|
- png_read_info(png, info);
|
|
|
-
|
|
|
- u32 width = png_get_image_width(png, info);
|
|
|
- u32 height = png_get_image_height(png, info);
|
|
|
- if(width > maxWidth || height > maxHeight) {
|
|
|
- std::cout << "texture '" << path << "' is too big: " << maxWidth << " x " << maxHeight << " allowed\n";
|
|
|
- return false;
|
|
|
+bool PNGReader::checkSignature() {
|
|
|
+ png_byte buffer[8];
|
|
|
+ if(fread(buffer, sizeof (png_byte), 8, file) != 8) {
|
|
|
+ std::cout << "cannot read signature of file '" << path << "'\n";
|
|
|
+ return true;
|
|
|
}
|
|
|
- maxWidth = width;
|
|
|
- maxHeight = height;
|
|
|
-
|
|
|
-
|
|
|
- rowPointers = static_cast<u32**> (png_malloc(png, height * (sizeof (u32*))));
|
|
|
- for(uint i = 0; i < height; i++) {
|
|
|
- rowPointers[i] = (buffer + i * width);
|
|
|
+ if(png_sig_cmp(buffer, 0, 8)) {
|
|
|
+ std::cout << "file '" << path << "' is not a png\n";
|
|
|
+ return true;
|
|
|
}
|
|
|
- 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 true;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
-bool PNGReader::load(const char* path, u32* buffer, u32& maxWidth, u32& maxHeight) {
|
|
|
- FILE* file = fopen(path, "r");
|
|
|
- if(file == nullptr) {
|
|
|
- std::cout << "texture '" << path << "' cannot be read: " << strerror(errno) << "\n";
|
|
|
- return false;
|
|
|
+bool PNGReader::readData(char* buffer) {
|
|
|
+ if(setjmp(png_jmpbuf(read))) {
|
|
|
+ std::cout << "png file '" << path << "' has used error callback\n";
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ rowPointers = static_cast<char**> (png_malloc(read, height * (sizeof (char*))));
|
|
|
+ for(int i = 0; i < height; i++) {
|
|
|
+ rowPointers[i] = (buffer + i * width * channels);
|
|
|
}
|
|
|
- bool result = load(path, file, buffer, maxWidth, maxHeight);
|
|
|
- fclose(file);
|
|
|
- return result;
|
|
|
+ png_set_rows(read, info, reinterpret_cast<png_bytepp> (rowPointers));
|
|
|
+ png_read_image(read, reinterpret_cast<png_bytepp> (rowPointers));
|
|
|
+ return false;
|
|
|
}
|