|
@@ -1,16 +1,18 @@
|
|
|
-#include <cstring>
|
|
|
-#include <iostream>
|
|
|
#include <libpng16/png.h>
|
|
|
|
|
|
#include "images/PNGReader.h"
|
|
|
+#include "utils/Logger.h"
|
|
|
+#include "utils/StringBuffer.h"
|
|
|
|
|
|
PNGReader::PNGReader(const char* path)
|
|
|
: path(path), width(0), height(0), channels(0), bitDepth(0), rowBytes(0),
|
|
|
file(fopen(path, "r")), read(nullptr), info(nullptr),
|
|
|
rowPointers(nullptr) {
|
|
|
if(file == nullptr) {
|
|
|
- std::cout << "file '" << path << "' cannot be read: " << strerror(errno)
|
|
|
- << "\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("file '")
|
|
|
+ .append(path)
|
|
|
+ .append("' cannot be read: ")
|
|
|
+ .append(static_cast<const char*>(strerror(errno))));
|
|
|
return;
|
|
|
}
|
|
|
if(checkSignature()) {
|
|
@@ -19,16 +21,18 @@ PNGReader::PNGReader(const char* path)
|
|
|
read = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr,
|
|
|
nullptr);
|
|
|
if(read == nullptr) {
|
|
|
- std::cout << "cannot create png read data structure\n";
|
|
|
+ LOG_WARNING("cannot create png read data structure");
|
|
|
return;
|
|
|
}
|
|
|
info = png_create_info_struct(read);
|
|
|
if(info == nullptr) {
|
|
|
- std::cout << "cannot create png info structure\n";
|
|
|
+ LOG_WARNING("cannot create png info structure");
|
|
|
return;
|
|
|
}
|
|
|
if(setjmp(png_jmpbuf(read))) {
|
|
|
- std::cout << "png file '" << path << "' has used error callback\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("png file '")
|
|
|
+ .append(path)
|
|
|
+ .append("' has used error callback"));
|
|
|
return;
|
|
|
}
|
|
|
png_init_io(read, file);
|
|
@@ -75,18 +79,24 @@ int PNGReader::getBufferSize() const {
|
|
|
|
|
|
bool PNGReader::hasError() const {
|
|
|
if(channels < 1 || channels > 4) {
|
|
|
- std::cout << "'" << path
|
|
|
- << "' has unsupported number of channels: " << channels
|
|
|
- << "\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("'")
|
|
|
+ .append(path)
|
|
|
+ .append("' has unsupported number of channels: ")
|
|
|
+ .append(channels));
|
|
|
return true;
|
|
|
} else if(width < 1 || width > 2048 || height < 1 || height > 2048) {
|
|
|
- std::cout << "width and height of '" << path << "' are too big\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("width and height of '")
|
|
|
+ .append(path)
|
|
|
+ .append("' are too big"));
|
|
|
return true;
|
|
|
} else if(bitDepth != 8 && bitDepth != 16) {
|
|
|
- std::cout << "bit depth of '" << path << "' is neither 8 or 16\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("bit depth of '")
|
|
|
+ .append(path)
|
|
|
+ .append("' is neither 8 or 16"));
|
|
|
return true;
|
|
|
} else if(getBufferSize() != (rowBytes * height * 8 / bitDepth)) {
|
|
|
- std::cout << "'" << path << "' needs an unexpected buffer size\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("'").append(path).append(
|
|
|
+ "' needs an unexpected buffer size"));
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
@@ -95,11 +105,14 @@ bool PNGReader::hasError() const {
|
|
|
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";
|
|
|
+ LOG_WARNING(StringBuffer<100>("cannot read signature of file '")
|
|
|
+ .append(path)
|
|
|
+ .append("'"));
|
|
|
return true;
|
|
|
}
|
|
|
if(png_sig_cmp(buffer, 0, 8)) {
|
|
|
- std::cout << "file '" << path << "' is not a png\n";
|
|
|
+ LOG_WARNING(
|
|
|
+ StringBuffer<100>("file '").append(path).append("' is not a png"));
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
@@ -107,7 +120,9 @@ bool PNGReader::checkSignature() {
|
|
|
|
|
|
bool PNGReader::readData(ColorChannel* buffer) {
|
|
|
if(setjmp(png_jmpbuf(read))) {
|
|
|
- std::cout << "png file '" << path << "' has used error callback\n";
|
|
|
+ LOG_WARNING(StringBuffer<100>("png file '")
|
|
|
+ .append(path)
|
|
|
+ .append("' has used error callback"));
|
|
|
return true;
|
|
|
}
|
|
|
rowPointers = static_cast<ColorChannel**>(
|