Browse Source

TgaImporter: ported to work also with OpenGL ES.

OpenGL ES doesn't have BGR and BGRA, swizzling pixel values to make it
RGB and RGBA there.
pull/34/head
Vladimír Vondruš 14 years ago
parent
commit
68ea16ea99
  1. 26
      src/Plugins/TgaImporter/Test/TgaImporterTest.cpp
  2. 22
      src/Plugins/TgaImporter/TgaImporter.cpp

26
src/Plugins/TgaImporter/Test/TgaImporterTest.cpp

@ -95,15 +95,26 @@ void TgaImporterTest::bits24() {
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0,
1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8
}; };
#ifndef MAGNUM_TARGET_GLES
const char* pixels = data + 18;
#else
const char pixels[] = {
3, 2, 1, 4, 3, 2, 5, 4, 3, 6, 5, 4, 7, 6, 5, 8, 7, 6
};
#endif
std::istringstream in(string(data, sizeof(data))); std::istringstream in(string(data, sizeof(data)));
TgaImporter importer; TgaImporter importer;
CORRADE_VERIFY(importer.open(in)); CORRADE_VERIFY(importer.open(in));
auto image = importer.image2D(0); auto image = importer.image2D(0);
#ifndef MAGNUM_TARGET_GLES
CORRADE_VERIFY(image->components() == AbstractImage::Components::BGR); CORRADE_VERIFY(image->components() == AbstractImage::Components::BGR);
#else
CORRADE_VERIFY(image->components() == AbstractImage::Components::RGB);
#endif
CORRADE_COMPARE(image->dimensions(), Math::Vector2<GLsizei>(2, 3)); CORRADE_COMPARE(image->dimensions(), Math::Vector2<GLsizei>(2, 3));
CORRADE_VERIFY(image->type() == TypeTraits<GLubyte>::imageType()); CORRADE_VERIFY(image->type() == TypeTraits<GLubyte>::imageType());
CORRADE_COMPARE(string(static_cast<const char*>(image->data()), 2*3*3), string(data + 18, 2*3*3)); CORRADE_COMPARE(string(static_cast<const char*>(image->data()), 2*3*3), string(pixels, 2*3*3));
} }
void TgaImporterTest::bits32() { void TgaImporterTest::bits32() {
@ -111,15 +122,26 @@ void TgaImporterTest::bits32() {
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 32, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 32, 0,
1, 2, 3, 1, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5, 6, 1, 5, 6, 7, 1, 6, 7, 8, 1 1, 2, 3, 1, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5, 6, 1, 5, 6, 7, 1, 6, 7, 8, 1
}; };
#ifndef MAGNUM_TARGET_GLES
const char* pixels = data + 18;
#else
const char pixels[] = {
3, 2, 1, 1, 4, 3, 2, 1, 5, 4, 3, 1, 6, 5, 4, 1, 7, 6, 5, 1, 8, 7, 6, 1
};
#endif
std::istringstream in(string(data, sizeof(data))); std::istringstream in(string(data, sizeof(data)));
TgaImporter importer; TgaImporter importer;
CORRADE_VERIFY(importer.open(in)); CORRADE_VERIFY(importer.open(in));
auto image = importer.image2D(0); auto image = importer.image2D(0);
#ifndef MAGNUM_TARGET_GLES
CORRADE_VERIFY(image->components() == AbstractImage::Components::BGRA); CORRADE_VERIFY(image->components() == AbstractImage::Components::BGRA);
#else
CORRADE_VERIFY(image->components() == AbstractImage::Components::RGBA);
#endif
CORRADE_COMPARE(image->dimensions(), Math::Vector2<GLsizei>(2, 3)); CORRADE_COMPARE(image->dimensions(), Math::Vector2<GLsizei>(2, 3));
CORRADE_VERIFY(image->type() == TypeTraits<GLubyte>::imageType()); CORRADE_VERIFY(image->type() == TypeTraits<GLubyte>::imageType());
CORRADE_COMPARE(string(static_cast<const char*>(image->data()), 2*3*3), string(data + 18, 2*3*3)); CORRADE_COMPARE(string(static_cast<const char*>(image->data()), 2*3*3), string(pixels, 2*3*3));
} }
}}}} }}}}

22
src/Plugins/TgaImporter/TgaImporter.cpp

@ -15,9 +15,11 @@
#include "TgaImporter.h" #include "TgaImporter.h"
#include <algorithm>
#include <Utility/Endianness.h> #include <Utility/Endianness.h>
#include "Math/Vector2.h" #include "Math/Vector2.h"
#include "Math/Swizzle.h"
#include "Trade/ImageData.h" #include "Trade/ImageData.h"
using namespace std; using namespace std;
@ -76,10 +78,18 @@ bool TgaImporter::open(istream& in, const string& name) {
AbstractImage::Components components; AbstractImage::Components components;
switch(header.bpp) { switch(header.bpp) {
case 24: case 24:
#ifndef MAGNUM_TARGET_GLES
components = AbstractImage::Components::BGR; components = AbstractImage::Components::BGR;
#else
components = AbstractImage::Components::RGB;
#endif
break; break;
case 32: case 32:
#ifndef MAGNUM_TARGET_GLES
components = AbstractImage::Components::BGRA; components = AbstractImage::Components::BGRA;
#else
components = AbstractImage::Components::RGBA;
#endif
break; break;
default: default:
Error() << "TgaImporter: unsupported bits-per-pixel:" << int(header.bpp); Error() << "TgaImporter: unsupported bits-per-pixel:" << int(header.bpp);
@ -92,6 +102,18 @@ bool TgaImporter::open(istream& in, const string& name) {
Math::Vector2<GLsizei> dimensions(header.width, header.height); Math::Vector2<GLsizei> dimensions(header.width, header.height);
#ifdef MAGNUM_TARGET_GLES
if(components == AbstractImage::Components::RGB) {
Math::Vector3<GLubyte>* data = reinterpret_cast<Math::Vector3<GLubyte>*>(buffer);
std::transform(data, data + dimensions.product(), data,
[](Math::Vector3<GLubyte> pixel) { return Math::swizzle<'b', 'g', 'r'>(pixel); });
} else /* RGBA */ {
Math::Vector4<GLubyte>* data = reinterpret_cast<Math::Vector4<GLubyte>*>(buffer);
std::transform(data, data + dimensions.product(), data,
[](Math::Vector4<GLubyte> pixel) { return Math::swizzle<'b', 'g', 'r', 'a'>(pixel); });
}
#endif
_image = new ImageData2D(name, dimensions, components, buffer); _image = new ImageData2D(name, dimensions, components, buffer);
return true; return true;
} }

Loading…
Cancel
Save