Browse Source

TgaImporter, TgaImageConverter: don't bother with BGR(A).

Swizzle to RGB/RGBA on all platforms. Usability over (minor) performance
benefits. Otherwise we would be like the ugly mess called QImage (in
Qt4 at least).
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
6b927b97f5
  1. 8
      src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp
  2. 15
      src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp
  3. 3
      src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h
  4. 16
      src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp
  5. 19
      src/MagnumPlugins/TgaImporter/TgaImporter.cpp
  6. 13
      src/MagnumPlugins/TgaImporter/TgaImporter.h

8
src/MagnumPlugins/TgaImageConverter/Test/TgaImageConverterTest.cpp

@ -56,11 +56,7 @@ namespace {
5, 6, 7, 6, 7, 8
};
#ifndef MAGNUM_TARGET_GLES
const ImageReference2D original(ColorFormat::BGR, ColorType::UnsignedByte, {2, 3}, originalData);
#else
const ImageReference2D original(ColorFormat::RGB, ColorType::UnsignedByte, {2, 3}, originalData);
#endif
}
TgaImageConverterTest::TgaImageConverterTest() {
@ -101,11 +97,7 @@ void TgaImageConverterTest::data() {
CORRADE_VERIFY(converted);
CORRADE_COMPARE(converted->size(), Vector2i(2, 3));
#ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(converted->format(), ColorFormat::BGR);
#else
CORRADE_COMPARE(converted->format(), ColorFormat::RGB);
#endif
CORRADE_COMPARE(converted->type(), ColorType::UnsignedByte);
CORRADE_COMPARE((std::string{converted->data(), 2*3*3}),
(std::string{original.data(), 2*3*3}));

15
src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp

@ -25,6 +25,7 @@
#include "TgaImageConverter.h"
#include <algorithm>
#include <fstream>
#include <tuple>
#include <Corrade/Containers/Array.h>
@ -32,13 +33,9 @@
#include "Magnum/ColorFormat.h"
#include "Magnum/Image.h"
#include "MagnumPlugins/TgaImporter/TgaHeader.h"
#ifdef MAGNUM_TARGET_GLES
#include <algorithm>
#include "Magnum/Math/Swizzle.h"
#include "Magnum/Math/Vector4.h"
#endif
#include "MagnumPlugins/TgaImporter/TgaHeader.h"
namespace Magnum { namespace Trade {
@ -49,15 +46,9 @@ TgaImageConverter::TgaImageConverter(PluginManager::AbstractManager& manager, st
auto TgaImageConverter::doFeatures() const -> Features { return Feature::ConvertData; }
Containers::Array<char> TgaImageConverter::doExportToData(const ImageReference2D& image) const {
#ifndef MAGNUM_TARGET_GLES
if(image.format() != ColorFormat::BGR &&
image.format() != ColorFormat::BGRA &&
image.format() != ColorFormat::Red)
#else
if(image.format() != ColorFormat::RGB &&
image.format() != ColorFormat::RGBA &&
image.format() != ColorFormat::Red)
#endif
{
Error() << "Trade::TgaImageConverter::exportToData(): unsupported color format" << image.format();
return nullptr;
@ -82,7 +73,6 @@ Containers::Array<char> TgaImageConverter::doExportToData(const ImageReference2D
/* Fill data */
std::copy(image.data(), image.data()+pixelSize*image.size().product(), data.begin()+sizeof(TgaHeader));
#ifdef MAGNUM_TARGET_GLES
if(image.format() == ColorFormat::RGB) {
auto pixels = reinterpret_cast<Math::Vector3<UnsignedByte>*>(data.begin()+sizeof(TgaHeader));
std::transform(pixels, pixels + image.size().product(), pixels,
@ -92,7 +82,6 @@ Containers::Array<char> TgaImageConverter::doExportToData(const ImageReference2D
std::transform(pixels, pixels + image.size().product(), pixels,
[](Math::Vector4<UnsignedByte> pixel) { return Math::swizzle<'b', 'g', 'r', 'a'>(pixel); });
}
#endif
return data;
}

3
src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h

@ -51,8 +51,7 @@ namespace Magnum { namespace Trade {
/**
@brief TGA image converter plugin
Supports images with format @ref ColorFormat::BGR, @ref ColorFormat::BGRA
(@ref ColorFormat::RGB/@ref ColorFormat::RGBA on OpenGL ES) or
Supports images with format @ref ColorFormat::RGB, @ref ColorFormat::RGBA or
@ref ColorFormat::Red and type @ref ColorType::UnsignedByte.
This plugin is built if `WITH_TGAIMAGECONVERTER` is enabled when building

16
src/MagnumPlugins/TgaImporter/Test/TgaImporterTest.cpp

@ -132,24 +132,16 @@ void TgaImporterTest::colorBits24() {
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
CORRADE_VERIFY(importer.openData(data));
std::optional<Trade::ImageData2D> image = importer.image2D(0);
CORRADE_VERIFY(image);
#ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(image->format(), ColorFormat::BGR);
#else
CORRADE_COMPARE(image->format(), ColorFormat::RGB);
#endif
CORRADE_COMPARE(image->size(), Vector2i(2, 3));
CORRADE_COMPARE(image->type(), ColorType::UnsignedByte);
CORRADE_COMPARE((std::string{image->data(), 2*3*3}),
@ -164,24 +156,16 @@ void TgaImporterTest::colorBits32() {
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
CORRADE_VERIFY(importer.openData(data));
std::optional<Trade::ImageData2D> image = importer.image2D(0);
CORRADE_VERIFY(image);
#ifndef MAGNUM_TARGET_GLES
CORRADE_COMPARE(image->format(), ColorFormat::BGRA);
#else
CORRADE_COMPARE(image->format(), ColorFormat::RGBA);
#endif
CORRADE_COMPARE(image->size(), Vector2i(2, 3));
CORRADE_COMPARE(image->type(), ColorType::UnsignedByte);
CORRADE_COMPARE((std::string{image->data(), 2*3*3}),

19
src/MagnumPlugins/TgaImporter/TgaImporter.cpp

@ -25,20 +25,17 @@
#include "TgaImporter.h"
#include <algorithm>
#include <fstream>
#include <sstream>
#include <Corrade/Utility/Endianness.h>
#include <Corrade/Containers/Array.h>
#include "Magnum/ColorFormat.h"
#include "Magnum/Trade/ImageData.h"
#include "MagnumPlugins/TgaImporter/TgaHeader.h"
#ifdef MAGNUM_TARGET_GLES
#include <algorithm>
#include "Magnum/Math/Swizzle.h"
#include "Magnum/Math/Vector4.h"
#endif
#include "Magnum/Trade/ImageData.h"
#include "MagnumPlugins/TgaImporter/TgaHeader.h"
#ifdef MAGNUM_TARGET_GLES2
#include "Magnum/Context.h"
@ -104,18 +101,10 @@ std::optional<ImageData2D> TgaImporter::doImage2D(UnsignedInt) {
if(header.imageType == 2) {
switch(header.bpp) {
case 24:
#ifndef MAGNUM_TARGET_GLES
format = ColorFormat::BGR;
#else
format = ColorFormat::RGB;
#endif
break;
case 32:
#ifndef MAGNUM_TARGET_GLES
format = ColorFormat::BGRA;
#else
format = ColorFormat::RGBA;
#endif
break;
default:
Error() << "Trade::TgaImporter::image2D(): unsupported color bits-per-pixel:" << header.bpp;
@ -147,7 +136,6 @@ std::optional<ImageData2D> TgaImporter::doImage2D(UnsignedInt) {
Vector2i size(header.width, header.height);
#ifdef MAGNUM_TARGET_GLES
if(format == ColorFormat::RGB) {
auto pixels = reinterpret_cast<Math::Vector3<UnsignedByte>*>(data);
std::transform(pixels, pixels + size.product(), pixels,
@ -157,7 +145,6 @@ std::optional<ImageData2D> TgaImporter::doImage2D(UnsignedInt) {
std::transform(pixels, pixels + size.product(), pixels,
[](Math::Vector4<UnsignedByte> pixel) { return Math::swizzle<'b', 'g', 'r', 'a'>(pixel); });
}
#endif
return ImageData2D(format, ColorType::UnsignedByte, size, data);
}

13
src/MagnumPlugins/TgaImporter/TgaImporter.h

@ -62,14 +62,11 @@ of another plugin, you need to request `TgaImporter` component of `Magnum`
package in CMake and link to `${MAGNUM_TGAIMPORTER_LIBRARIES}`. See
@ref building, @ref cmake and @ref plugins for more information.
The images are imported with @ref ColorType::UnsignedByte and @ref ColorFormat::BGR,
@ref ColorFormat::BGRA or @ref ColorFormat::Red, respectively. Grayscale images
require extension @extension{ARB,texture_rg}.
In OpenGL ES BGR and BGRA images are converted to @ref ColorFormat::RGB
and @ref ColorFormat::RGBA. In OpenGL ES 2.0, if @es_extension{EXT,texture_rg}
is not supported, grayscale images use @ref ColorFormat::Luminance instead of
@ref ColorFormat::Red.
The images are imported with @ref ColorType::UnsignedByte and @ref ColorFormat::RGB,
@ref ColorFormat::RGBA or @ref ColorFormat::Red, respectively. Grayscale images
require extension @extension{ARB,texture_rg}. In OpenGL ES 2.0, if
@es_extension{EXT,texture_rg} is not supported, grayscale images use
@ref ColorFormat::Luminance instead of @ref ColorFormat::Red.
*/
class MAGNUM_TGAIMPORTER_EXPORT TgaImporter: public AbstractImporter {
public:

Loading…
Cancel
Save