|
|
|
|
@ -44,51 +44,37 @@
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Trade { |
|
|
|
|
|
|
|
|
|
TgaImporter::TgaImporter(): in(nullptr) {} |
|
|
|
|
TgaImporter::TgaImporter() = default; |
|
|
|
|
|
|
|
|
|
TgaImporter::TgaImporter(PluginManager::AbstractManager& manager, std::string plugin): AbstractImporter(manager, std::move(plugin)), in(nullptr) {} |
|
|
|
|
TgaImporter::TgaImporter(PluginManager::AbstractManager& manager, std::string plugin): AbstractImporter{manager, std::move(plugin)} {} |
|
|
|
|
|
|
|
|
|
TgaImporter::~TgaImporter() { close(); } |
|
|
|
|
TgaImporter::~TgaImporter() = default; |
|
|
|
|
|
|
|
|
|
auto TgaImporter::doFeatures() const -> Features { return Feature::OpenData; } |
|
|
|
|
|
|
|
|
|
bool TgaImporter::doIsOpened() const { return in; } |
|
|
|
|
bool TgaImporter::doIsOpened() const { return _in; } |
|
|
|
|
|
|
|
|
|
void TgaImporter::doOpenData(const Containers::ArrayView<const char> data) { |
|
|
|
|
in = new std::istringstream{{data, data.size()}}; |
|
|
|
|
} |
|
|
|
|
void TgaImporter::doClose() { _in = nullptr; } |
|
|
|
|
|
|
|
|
|
void TgaImporter::doOpenFile(const std::string& filename) { |
|
|
|
|
in = new std::ifstream(filename, std::ifstream::binary); |
|
|
|
|
if(in->good()) return; |
|
|
|
|
|
|
|
|
|
Error() << "Trade::TgaImporter::openFile(): cannot open file" << filename; |
|
|
|
|
close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void TgaImporter::doClose() { |
|
|
|
|
delete in; |
|
|
|
|
in = nullptr; |
|
|
|
|
void TgaImporter::doOpenData(const Containers::ArrayView<const char> data) { |
|
|
|
|
_in = Containers::Array<char>{data.size()}; |
|
|
|
|
std::copy(data.begin(), data.end(), _in.begin()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
UnsignedInt TgaImporter::doImage2DCount() const { return 1; } |
|
|
|
|
|
|
|
|
|
std::optional<ImageData2D> TgaImporter::doImage2D(UnsignedInt) { |
|
|
|
|
/* Check if the file is long enough */ |
|
|
|
|
in->seekg(0, std::istream::end); |
|
|
|
|
std::streamoff filesize = in->tellg(); |
|
|
|
|
in->seekg(0, std::istream::beg); |
|
|
|
|
if(filesize < std::streamoff(sizeof(TgaHeader))) { |
|
|
|
|
Error() << "Trade::TgaImporter::image2D(): the file is too short:" << filesize << "bytes"; |
|
|
|
|
if(_in.size() < std::streamoff(sizeof(TgaHeader))) { |
|
|
|
|
Error() << "Trade::TgaImporter::image2D(): the file is too short:" << _in.size() << "bytes"; |
|
|
|
|
return std::nullopt; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TgaHeader header; |
|
|
|
|
in->read(reinterpret_cast<char*>(&header), sizeof(TgaHeader)); |
|
|
|
|
const TgaHeader& header = *reinterpret_cast<const TgaHeader*>(_in.data()); |
|
|
|
|
|
|
|
|
|
/* Convert to machine endian */ |
|
|
|
|
header.width = Utility::Endianness::littleEndian(header.width); |
|
|
|
|
header.height = Utility::Endianness::littleEndian(header.height); |
|
|
|
|
/* Size in machine endian */ |
|
|
|
|
const Vector2i size{Utility::Endianness::littleEndian(header.width), |
|
|
|
|
Utility::Endianness::littleEndian(header.height)}; |
|
|
|
|
|
|
|
|
|
/* Image format */ |
|
|
|
|
PixelFormat format; |
|
|
|
|
@ -132,17 +118,14 @@ std::optional<ImageData2D> TgaImporter::doImage2D(UnsignedInt) {
|
|
|
|
|
return std::nullopt; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const std::size_t dataSize = header.width*header.height*header.bpp/8; |
|
|
|
|
Containers::Array<char> data{dataSize}; |
|
|
|
|
in->read(data, dataSize); |
|
|
|
|
Containers::Array<char> data{std::size_t(size.product())*header.bpp/8}; |
|
|
|
|
std::copy_n(_in + sizeof(TgaHeader), data.size(), data.begin()); |
|
|
|
|
|
|
|
|
|
/* Adjust pixel storage if row size is not four byte aligned */ |
|
|
|
|
PixelStorage storage; |
|
|
|
|
if((header.width*header.bpp/8)%4 != 0) |
|
|
|
|
if((size.x()*header.bpp/8)%4 != 0) |
|
|
|
|
storage.setAlignment(1); |
|
|
|
|
|
|
|
|
|
Vector2i size(header.width, header.height); |
|
|
|
|
|
|
|
|
|
if(format == PixelFormat::RGB) { |
|
|
|
|
auto pixels = reinterpret_cast<Math::Vector3<UnsignedByte>*>(data.data()); |
|
|
|
|
std::transform(pixels, pixels + size.product(), pixels, |
|
|
|
|
|