diff --git a/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp b/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp index 85c238fbd..11a62843f 100644 --- a/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp +++ b/src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include "Magnum/ImageView.h" @@ -51,6 +52,8 @@ struct TgaImageConverterTest: TestSuite::Tester { void rgb(); void rgba(); + void unsupportedMetadata(); + /* Explicitly forbid system-wide plugin dependencies */ PluginManager::Manager _converterManager{"nonexistent"}; PluginManager::Manager _importerManager{"nonexistent"}; @@ -68,6 +71,15 @@ constexpr struct { "Trade::TgaImageConverter::convertToData(): converting from RGBA to BGRA\n"} }; +const struct { + const char* name; + ImageFlags2D flags; + const char* message; +} UnsupportedMetadataData[]{ + {"1D array", ImageFlag2D::Array, + "1D array images are unrepresentable in TGA, saving as a regular 2D image"} +}; + TgaImageConverterTest::TgaImageConverterTest() { addTests({&TgaImageConverterTest::wrongFormat}); @@ -76,6 +88,9 @@ TgaImageConverterTest::TgaImageConverterTest() { &TgaImageConverterTest::rgba}, Containers::arraySize(VerboseData)); + addInstancedTests({&TgaImageConverterTest::unsupportedMetadata}, + Containers::arraySize(UnsupportedMetadataData)); + /* Load the plugin directly from the build tree. Otherwise it's static and already loaded. */ #ifdef TGAIMAGECONVERTER_PLUGIN_FILENAME @@ -184,6 +199,21 @@ void TgaImageConverterTest::rgba() { CORRADE_COMPARE(out.str(), data.message32); } +void TgaImageConverterTest::unsupportedMetadata() { + auto&& data = UnsupportedMetadataData[testCaseInstanceId()]; + setTestCaseDescription(data.name); + + Containers::Pointer converter = _converterManager.instantiate("TgaImageConverter"); + + const char imageData[4]{}; + ImageView2D image{PixelFormat::RGBA8Unorm, {1, 1}, imageData, data.flags}; + + std::ostringstream out; + Warning redirectWarning{&out}; + CORRADE_VERIFY(converter->convertToData(image)); + CORRADE_COMPARE(out.str(), Utility::formatString("Trade::TgaImageConverter::convertToData(): {}\n", data.message)); +} + }}}} CORRADE_TEST_MAIN(Magnum::Trade::Test::TgaImageConverterTest) diff --git a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp index 2a9863fb8..e487fc91c 100644 --- a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp +++ b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp @@ -47,6 +47,11 @@ TgaImageConverter::TgaImageConverter(PluginManager::AbstractManager& manager, co ImageConverterFeatures TgaImageConverter::doFeatures() const { return ImageConverterFeature::Convert2DToData; } Containers::Optional> TgaImageConverter::doConvertToData(const ImageView2D& image) { + /* Warn about lost metadata */ + if(image.flags() & ImageFlag2D::Array) { + Warning{} << "Trade::TgaImageConverter::convertToData(): 1D array images are unrepresentable in TGA, saving as a regular 2D image"; + } + /* Initialize data buffer */ const auto pixelSize = UnsignedByte(image.pixelSize()); Containers::Array data{ValueInit, sizeof(Implementation::TgaHeader) + pixelSize*image.size().product()}; diff --git a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h index c583b5b9b..43767e24d 100644 --- a/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h +++ b/src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h @@ -93,6 +93,10 @@ information. The output is always uncompressed. If you want to make use of RLE compression and have the files smaller, use the @ref StbImageConverter plugin instead. + +The TGA file format doesn't have a way to distinguish between 2D and 1D array +images. If an image has @ref ImageFlag2D::Array set, a warning is printed and +the file is saved as a regular 2D image. */ class MAGNUM_TGAIMAGECONVERTER_EXPORT TgaImageConverter: public AbstractImageConverter { public: