From 3f34bcf58030f869d42866aa46fbd7ce4f30bcc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 19 Aug 2015 19:30:08 +0200 Subject: [PATCH] TgaImporter: adjust pixel storage if row size is not four byte aligned. I would set the alignment to 1 every time, but 99% of image data has sane dimensions and this would only bring needless state changes. Note that this setting is not yet respected when uploading the texture data, that will be done in later "pixel storage support" commits. --- src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp | 3 +++ src/MagnumPlugins/TgaImporter/TgaImporter.cpp | 7 ++++++- src/MagnumPlugins/TgaImporter/TgaImporter.h | 10 +++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp b/src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp index b7c837049..1db66974b 100644 --- a/src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp +++ b/src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp @@ -146,6 +146,7 @@ void TgaImporterTest::colorBits24() { std::optional image = importer.image2D(0); CORRADE_VERIFY(image); + CORRADE_COMPARE(image->storage().alignment(), 1); CORRADE_COMPARE(image->format(), PixelFormat::RGB); CORRADE_COMPARE(image->size(), Vector2i(2, 3)); CORRADE_COMPARE(image->type(), PixelType::UnsignedByte); @@ -170,6 +171,7 @@ void TgaImporterTest::colorBits32() { std::optional image = importer.image2D(0); CORRADE_VERIFY(image); + CORRADE_COMPARE(image->storage().alignment(), 4); CORRADE_COMPARE(image->format(), PixelFormat::RGBA); CORRADE_COMPARE(image->size(), Vector2i(2, 3)); CORRADE_COMPARE(image->type(), PixelType::UnsignedByte); @@ -189,6 +191,7 @@ void TgaImporterTest::grayscaleBits8() { std::optional image = importer.image2D(0); CORRADE_VERIFY(image); + CORRADE_COMPARE(image->storage().alignment(), 1); #ifndef MAGNUM_TARGET_GLES2 CORRADE_COMPARE(image->format(), PixelFormat::Red); #else diff --git a/src/MagnumPlugins/TgaImporter/TgaImporter.cpp b/src/MagnumPlugins/TgaImporter/TgaImporter.cpp index 9a8ceb5e3..ffc3f2709 100644 --- a/src/MagnumPlugins/TgaImporter/TgaImporter.cpp +++ b/src/MagnumPlugins/TgaImporter/TgaImporter.cpp @@ -136,6 +136,11 @@ std::optional TgaImporter::doImage2D(UnsignedInt) { Containers::Array data{dataSize}; in->read(data, dataSize); + /* Adjust pixel storage if row size is not four byte aligned */ + PixelStorage storage; + if((header.width*header.bpp/8)%4 != 0) + storage.setAlignment(1); + Vector2i size(header.width, header.height); if(format == PixelFormat::RGB) { @@ -148,7 +153,7 @@ std::optional TgaImporter::doImage2D(UnsignedInt) { [](Math::Vector4 pixel) { return Math::swizzle<'b', 'g', 'r', 'a'>(pixel); }); } - return ImageData2D{format, PixelType::UnsignedByte, size, std::move(data)}; + return ImageData2D{storage, format, PixelType::UnsignedByte, size, std::move(data)}; } }} diff --git a/src/MagnumPlugins/TgaImporter/TgaImporter.h b/src/MagnumPlugins/TgaImporter/TgaImporter.h index 4c3e80c71..e8cd587ea 100644 --- a/src/MagnumPlugins/TgaImporter/TgaImporter.h +++ b/src/MagnumPlugins/TgaImporter/TgaImporter.h @@ -64,9 +64,13 @@ package in CMake and link to `${MAGNUM_TGAIMPORTER_LIBRARIES}`. See The images are imported with @ref PixelType::UnsignedByte and @ref PixelFormat::RGB, @ref PixelFormat::RGBA or @ref PixelFormat::Red, respectively. Grayscale images -require extension @extension{ARB,texture_rg}. In OpenGL ES 2.0, if -@es_extension{EXT,texture_rg} is not supported and in WebGL 1.0, grayscale -images use @ref PixelFormat::Luminance instead of @ref PixelFormat::Red. +require extension @extension{ARB,texture_rg}. Imported images are imported with +default @ref PixelStorage parameters except for alignment, which may be changed +to `1` if the data require it. + +In OpenGL ES 2.0, if @es_extension{EXT,texture_rg} is not supported and in +WebGL 1.0, grayscale images use @ref PixelFormat::Luminance instead of +@ref PixelFormat::Red. */ class MAGNUM_TGAIMPORTER_EXPORT TgaImporter: public AbstractImporter { public: