Browse Source

TgaImageConverter: properly handle also pixel storage skip.

And error out on byte swap, because that's nonsense.
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
232a8038e4
  1. 6
      src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp
  2. 14
      src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp
  3. 3
      src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h

6
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,

14
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<char> 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<char> 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<Math::Vector3<UnsignedByte>*>(data.begin()+sizeof(TgaHeader));

3
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

Loading…
Cancel
Save