|
@@ -17,21 +17,21 @@ static bool checkSignature(const char* path, FILE* file) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-static u32* load(const char* path, FILE* file, u32& width, u32& height) {
|
|
|
+static bool load(const char* path, FILE* file, u32* buffer, u32& maxWidth, u32& maxHeight) {
|
|
|
if(checkSignature(path, file)) {
|
|
|
- return nullptr;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
png_infop info = png_create_info_struct(png);
|
|
|
if(info == nullptr) {
|
|
|
std::cout << "cannot create image info structure\n";
|
|
|
- return nullptr;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
u32** rowPointers = nullptr;
|
|
@@ -42,21 +42,27 @@ static u32* load(const char* path, FILE* file, u32& width, u32& height) {
|
|
|
}
|
|
|
png_destroy_read_struct(&png, &info, nullptr);
|
|
|
std::cout << "texture '" << path << "' has used error callback\n";
|
|
|
- return nullptr;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
-
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ maxWidth = width;
|
|
|
+ maxHeight = height;
|
|
|
+
|
|
|
// 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);
|
|
|
+ rowPointers[i] = (buffer + i * width);
|
|
|
}
|
|
|
png_set_rows(png, info, reinterpret_cast<png_bytepp> (rowPointers));
|
|
|
|
|
@@ -64,16 +70,16 @@ static u32* load(const char* path, FILE* file, u32& width, u32& height) {
|
|
|
|
|
|
png_free(png, rowPointers);
|
|
|
png_destroy_read_struct(&png, &info, nullptr);
|
|
|
- return data;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-u32* PNGReader::load(const char* path, u32& width, u32& height) {
|
|
|
+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 nullptr;
|
|
|
+ return false;
|
|
|
}
|
|
|
- u32* data = load(path, file, width, height);
|
|
|
+ bool result = load(path, file, buffer, maxWidth, maxHeight);
|
|
|
fclose(file);
|
|
|
- return data;
|
|
|
+ return result;
|
|
|
}
|