From 232a8038e46875c10a47f14d8ed839ce3462ff0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 19 Aug 2015 20:56:26 +0200 Subject: [PATCH] TgaImageConverter: properly handle also pixel storage skip. And error out on byte swap, because that's nonsense. --- .../Test/TgaImageConverterTest.cpp | 6 +++++- .../TgaImageConverter/TgaImageConverter.cpp | 14 ++++++++++++-- .../TgaImageConverter/TgaImageConverter.h | 3 ++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp b/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp index 37770c202..b71bfaa1e 100644 --- a/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp +++ b/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp @@ -54,6 +54,9 @@ class TgaImageConverterTest: public TestSuite::Tester { namespace { /* Padded to four byte alignment (the resulting file is *not* padded) */ constexpr char OriginalDataRGB[] = { + /* Skip */ + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 2, 3, 2, 3, 4, 0, 0, 3, 4, 5, 4, 5, 6, 0, 0, 5, 6, 7, 6, 7, 8, 0, 0 @@ -64,7 +67,8 @@ namespace { 5, 6, 7, 6, 7, 8 }; - const ImageView2D OriginalRGB{PixelFormat::RGB, PixelType::UnsignedByte, {2, 3}, OriginalDataRGB}; + const ImageView2D OriginalRGB{PixelStorage{}.setSkip({0, 1, 0}), + PixelFormat::RGB, PixelType::UnsignedByte, {2, 3}, OriginalDataRGB}; constexpr char OriginalDataRGBA[] = { 1, 2, 3, 4, 2, 3, 4, 5, diff --git a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp index 517ce9c73..13e6a486d 100644 --- a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp +++ b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp @@ -46,6 +46,13 @@ TgaImageConverter::TgaImageConverter(PluginManager::AbstractManager& manager, st auto TgaImageConverter::doFeatures() const -> Features { return Feature::ConvertData; } Containers::Array TgaImageConverter::doExportToData(const ImageView2D& image) const { + #ifndef MAGNUM_TARGET_GLES + if(image.storage().swapBytes()) { + Error() << "Trade::TgaImageConverter::exportToData(): pixel byte swap is not supported"; + return nullptr; + } + #endif + if(image.format() != PixelFormat::RGB && image.format() != PixelFormat::RGBA #if !(defined(MAGNUM_TARGET_WEBGL) && defined(MAGNUM_TARGET_GLES2)) @@ -90,13 +97,16 @@ Containers::Array TgaImageConverter::doExportToData(const ImageView2D& ima header->width = UnsignedShort(Utility::Endianness::littleEndian(image.size().x())); header->height = UnsignedShort(Utility::Endianness::littleEndian(image.size().y())); + /* Image data pointer including skip */ + const char* imageData = image.data() + std::get<0>(image.dataProperties()); + /* Fill data or copy them row by row if we need to drop the padding */ const std::size_t rowSize = image.size().x()*pixelSize; const std::size_t rowStride = std::get<1>(image.dataProperties()).x(); if(rowStride != rowSize) { for(std::int_fast32_t y = 0; y != image.size().y(); ++y) - std::copy_n(image.data() + y*rowStride, rowSize, data.begin() + sizeof(TgaHeader) + y*rowSize); - } else std::copy(image.data().data(), image.data()+pixelSize*image.size().product(), data.begin() + sizeof(TgaHeader)); + std::copy_n(imageData + y*rowStride, rowSize, data.begin() + sizeof(TgaHeader) + y*rowSize); + } else std::copy_n(imageData, pixelSize*image.size().product(), data.begin() + sizeof(TgaHeader)); if(image.format() == PixelFormat::RGB) { auto pixels = reinterpret_cast*>(data.begin()+sizeof(TgaHeader)); diff --git a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h index 85f4e2c60..082035afb 100644 --- a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h +++ b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h @@ -53,7 +53,8 @@ namespace Magnum { namespace Trade { Supports images with format @ref PixelFormat::RGB, @ref PixelFormat::RGBA or @ref PixelFormat::Red (or @ref PixelFormat::Luminance in OpenGL ES 2.0 and -WebGL 1.0) and type @ref PixelType::UnsignedByte. +WebGL 1.0) and type @ref PixelType::UnsignedByte. Does *not* support +non-default @ref PixelStorage::swapBytes() values. This plugin is built if `WITH_TGAIMAGECONVERTER` is enabled when building Magnum. To use dynamic plugin, you need to load `TgaImageConverter` plugin