Browse Source

TgaImageConverter: ported to OpenGL ES.

pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
51075b91c5
  1. 15
      src/Plugins/TgaImageConverter/Test/TgaImageConverterTest.cpp
  2. 26
      src/Plugins/TgaImageConverter/TgaImageConverter.cpp

15
src/Plugins/TgaImageConverter/Test/TgaImageConverterTest.cpp

@ -48,7 +48,12 @@ class TgaImageConverterTest: public TestSuite::Tester {
}; };
namespace { namespace {
const Image2D original({2, 3}, ImageFormat::BGR, ImageType::UnsignedByte, new char[18] { #ifndef MAGNUM_TARGET_GLES
const Image2D original({2, 3}, ImageFormat::BGR, ImageType::UnsignedByte, new char[18]
#else
const Image2D original({2, 3}, ImageFormat::RGB, ImageType::UnsignedByte, new char[18]
#endif
{
1, 2, 3, 2, 3, 4, 1, 2, 3, 2, 3, 4,
3, 4, 5, 4, 5, 6, 3, 4, 5, 4, 5, 6,
5, 6, 7, 6, 7, 8 5, 6, 7, 6, 7, 8
@ -98,7 +103,11 @@ void TgaImageConverterTest::data() {
CORRADE_VERIFY(converted); CORRADE_VERIFY(converted);
CORRADE_COMPARE(converted->size(), Vector2i(2, 3)); CORRADE_COMPARE(converted->size(), Vector2i(2, 3));
#ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(converted->format(), ImageFormat::BGR); CORRADE_COMPARE(converted->format(), ImageFormat::BGR);
#else
CORRADE_COMPARE(converted->format(), ImageFormat::RGB);
#endif
CORRADE_COMPARE(converted->type(), ImageType::UnsignedByte); CORRADE_COMPARE(converted->type(), ImageType::UnsignedByte);
CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(converted->data()), 2*3*3), CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(converted->data()), 2*3*3),
std::string(reinterpret_cast<const char*>(original.data()), 2*3*3)); std::string(reinterpret_cast<const char*>(original.data()), 2*3*3));
@ -115,7 +124,11 @@ void TgaImageConverterTest::file() {
CORRADE_VERIFY(converted); CORRADE_VERIFY(converted);
CORRADE_COMPARE(converted->size(), Vector2i(2, 3)); CORRADE_COMPARE(converted->size(), Vector2i(2, 3));
#ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(converted->format(), ImageFormat::BGR); CORRADE_COMPARE(converted->format(), ImageFormat::BGR);
#else
CORRADE_COMPARE(converted->format(), ImageFormat::RGB);
#endif
CORRADE_COMPARE(converted->type(), ImageType::UnsignedByte); CORRADE_COMPARE(converted->type(), ImageType::UnsignedByte);
CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(converted->data()), 2*3*3), CORRADE_COMPARE(std::string(reinterpret_cast<const char*>(converted->data()), 2*3*3),
std::string(reinterpret_cast<const char*>(original.data()), 2*3*3)); std::string(reinterpret_cast<const char*>(original.data()), 2*3*3));

26
src/Plugins/TgaImageConverter/TgaImageConverter.cpp

@ -29,6 +29,11 @@
#include <Image.h> #include <Image.h>
#include <ImageFormat.h> #include <ImageFormat.h>
#ifdef MAGNUM_TARGET_GLES
#include <algorithm>
#include <Swizzle.h>
#endif
#include "TgaImporter/TgaHeader.h" #include "TgaImporter/TgaHeader.h"
namespace Magnum { namespace Trade { namespace TgaImageConverter { namespace Magnum { namespace Trade { namespace TgaImageConverter {
@ -42,9 +47,16 @@ TgaImageConverter::Features TgaImageConverter::features() const {
} }
std::pair<const unsigned char*, std::size_t> TgaImageConverter::convertToData(const Image2D* const image) const { std::pair<const unsigned char*, std::size_t> TgaImageConverter::convertToData(const Image2D* const image) const {
#ifndef MAGNUM_TARGET_GLES
if(image->format() != ImageFormat::BGR && if(image->format() != ImageFormat::BGR &&
image->format() != ImageFormat::BGRA && image->format() != ImageFormat::BGRA &&
image->format() != ImageFormat::Red) { image->format() != ImageFormat::Red)
#else
if(image->format() != ImageFormat::RGB &&
image->format() != ImageFormat::RGBA &&
image->format() != ImageFormat::Red)
#endif
{
Error() << "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image format" << image->format(); Error() << "Trade::TgaImageConverter::TgaImageConverter::convertToData(): unsupported image format" << image->format();
return {nullptr, 0}; return {nullptr, 0};
} }
@ -69,6 +81,18 @@ std::pair<const unsigned char*, std::size_t> TgaImageConverter::convertToData(co
/* Fill data */ /* Fill data */
std::copy(image->data(), image->data()+pixelSize*image->size().product(), data+sizeof(TgaImporter::TgaHeader)); std::copy(image->data(), image->data()+pixelSize*image->size().product(), data+sizeof(TgaImporter::TgaHeader));
#ifdef MAGNUM_TARGET_GLES
if(image->format() == ImageFormat::RGB) {
auto pixels = reinterpret_cast<Math::Vector3<UnsignedByte>*>(data+sizeof(TgaImporter::TgaHeader));
std::transform(pixels, pixels + image->size().product(), pixels,
[](Math::Vector3<UnsignedByte> pixel) { return swizzle<'b', 'g', 'r'>(pixel); });
} else if(image->format() == ImageFormat::RGBA) {
auto pixels = reinterpret_cast<Math::Vector4<UnsignedByte>*>(data+sizeof(TgaImporter::TgaHeader));
std::transform(pixels, pixels + image->size().product(), pixels,
[](Math::Vector4<UnsignedByte> pixel) { return swizzle<'b', 'g', 'r', 'a'>(pixel); });
}
#endif
return {data, size}; return {data, size};
} }

Loading…
Cancel
Save