From c7bfbc55fc7cde9c00f48376d24937bc4e840bc1 Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Wed, 17 Jun 2015 12:10:55 +0200 Subject: [PATCH] Added optional BGRA support for DDS loader --- gliml.h | 6 ++++ gliml.inl | 9 +++++- gliml_dds.inl | 79 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 64 insertions(+), 30 deletions(-) diff --git a/gliml.h b/gliml.h index d0a687a..9eb0772 100644 --- a/gliml.h +++ b/gliml.h @@ -45,6 +45,8 @@ #define GLIML_GL_RGBA 0x1908 #define GLIML_GL_LUMINANCE 0x1909 #define GLIML_GL_LUMINANCE_ALPHA 0x190A +#define GLIML_GL_BGRA 0x80E1 +#define GLIML_GL_BGR 0x80E0 #define GLIML_GL_UNSIGNED_BYTE 0x1401 #define GLIML_GL_UNSIGNED_SHORT_4_4_4_4 0x8033 @@ -99,6 +101,8 @@ class context { void enable_pvrtc(bool b); /// enable or disable ETC2 support void enable_etc2(bool b); + /// enable BGRA support + void enable_bgra(bool b); #ifndef GLIML_NO_DDS /// load DDS image data into context @@ -157,6 +161,7 @@ class context { bool dxtEnabled; bool pvrtcEnabled; bool etc2Enabled; + bool bgraEnabled; int errorCode; GLenum target; bool isCompressed; @@ -188,6 +193,7 @@ class context { #define GLIML_ERROR_PVRTC_NOT_ENABLED (6) #define GLIML_ERROR_ETC2_NOT_ENABLED (7) #define GLIML_ERROR_ENDIAN_MISMATCH (8) +#define GLIML_ERROR_BGRA_NOT_ENABLED (9) #include "gliml.inl" #ifndef GLIML_NO_DDS diff --git a/gliml.inl b/gliml.inl index 3125a49..cd419f0 100644 --- a/gliml.inl +++ b/gliml.inl @@ -6,7 +6,8 @@ inline context::context() : dxtEnabled(false), pvrtcEnabled(false), -etc2Enabled(false) { +etc2Enabled(false), +bgraEnabled(false) { this->clear(); } @@ -33,6 +34,12 @@ context::enable_etc2(bool b) { this->etc2Enabled = b; } +//------------------------------------------------------------------------------ +inline void +context::enable_bgra(bool b) { + this->bgraEnabled = b; +} + //------------------------------------------------------------------------------ inline bool context::load(const void* data, unsigned int size) { diff --git a/gliml_dds.inl b/gliml_dds.inl index ae22b29..b1cba23 100644 --- a/gliml_dds.inl +++ b/gliml_dds.inl @@ -53,8 +53,7 @@ context::load_dds(const void* data, unsigned int byteSize) { if (!this->dxtEnabled) { this->errorCode = GLIML_ERROR_DXT_NOT_ENABLED; return false; - } - + } this->isCompressed = true; switch (hdr->ddspf.dwFourCC) { case GLIML_FOURCC_DXT1: @@ -76,87 +75,109 @@ context::load_dds(const void* data, unsigned int byteSize) { } else if ((hdr->ddspf.dwFlags & (GLIML_DDSF_RGBA|GLIML_DDSF_RGB)) && (hdr->ddspf.dwRGBBitCount == 32)) { // 32-bit RGBA, check byte order - this->format = GLIML_GL_RGBA; - this->type = GLIML_GL_UNSIGNED_BYTE; bytesPerElement = 4; + this->type = GLIML_GL_UNSIGNED_BYTE; + this->internalFormat = GLIML_GL_RGBA; if (hdr->ddspf.dwRBitMask == 0x00FF0000) { // Direct3D style BGRA - // FIXME! - this->internalFormat = GLIML_GL_RGBA; + if (this->bgraEnabled) { + this->format = GLIML_GL_BGRA; + } + else { + this->errorCode = GLIML_ERROR_BGRA_NOT_ENABLED; + return false; + } } else { // OpenGLES style RGBA - this->internalFormat = GLIML_GL_RGBA; + this->format = GLIML_GL_RGBA; } } else if ((hdr->ddspf.dwFlags & GLIML_DDSF_RGB) && (hdr->ddspf.dwRGBBitCount == 24)) { // 24-bit RGB, check byte order - this->format = GLIML_GL_RGB; - this->type = GLIML_GL_UNSIGNED_BYTE; bytesPerElement = 3; + this->type = GLIML_GL_UNSIGNED_BYTE; + this->internalFormat = GLIML_GL_RGB; if (hdr->ddspf.dwRBitMask == 0x00FF0000) { // Direct3D style BGR - // FIXME! - this->internalFormat = GLIML_GL_RGB; + if (this->bgraEnabled) { + this->format = GLIML_GL_BGR; + } + else { + this->errorCode = GLIML_ERROR_BGRA_NOT_ENABLED; + return false; + } } else { // OpenGLES style RGB - this->internalFormat = GLIML_GL_RGB; + this->format = GLIML_GL_RGB; } } else if (hdr->ddspf.dwRGBBitCount == 16) { - bytesPerElement = 2; // 16-bit RGB(A) + bytesPerElement = 2; if (hdr->ddspf.dwABitMask == 0) { // RGB565 or BGR565 - this->format = GLIML_GL_RGB; this->type = GLIML_GL_UNSIGNED_SHORT_5_6_5; + this->internalFormat = GLIML_GL_RGB; if (hdr->ddspf.dwRBitMask == (0x1F << 11)) { // Direct3D style - // FIXME! - this->internalFormat = GLIML_GL_RGB; + if (this->bgraEnabled) { + this->format = GLIML_GL_BGR; + } + else { + this->errorCode = GLIML_ERROR_BGRA_NOT_ENABLED; + return false; + } } else { // OpenGLES style - this->internalFormat = GLIML_GL_RGB; + this->format = GLIML_GL_RGB; } } else { // RGBA4 or BGRA4 or RGBA5551 or BGRA5551 - this->format = GLIML_GL_RGBA; + this->internalFormat = GLIML_GL_RGBA; if (hdr->ddspf.dwRBitMask == 0x00F0) { // Direct3D style bgra4 - // FIXME! - this->type = GLIML_GL_UNSIGNED_SHORT_4_4_4_4; - this->internalFormat = GLIML_GL_RGBA; + if (this->bgraEnabled) { + this->type = GLIML_GL_UNSIGNED_SHORT_4_4_4_4; + this->format = GLIML_GL_BGRA; + } + else { + this->errorCode = GLIML_ERROR_BGRA_NOT_ENABLED; + return false; + } } else if (hdr->ddspf.dwRBitMask == 0xF000) { // OpenGLES style rgba4 this->type = GLIML_GL_UNSIGNED_SHORT_4_4_4_4; - this->internalFormat = GLIML_GL_RGBA; + this->format = GLIML_GL_RGBA; } else if (hdr->ddspf.dwRBitMask == (0x1F << 1)) { // Direc3D style bgra5551 - // FIXME! - this->type = GLIML_GL_UNSIGNED_SHORT_5_5_5_1; - this->internalFormat = GLIML_GL_RGBA; + if (this->bgraEnabled) { + this->type = GLIML_GL_UNSIGNED_SHORT_5_5_5_1; + this->format = GLIML_GL_BGRA; + } + else { + this->errorCode = GLIML_ERROR_BGRA_NOT_ENABLED; + return false; + } } else { // OpenGLES style rgba5551 this->type = GLIML_GL_UNSIGNED_SHORT_5_5_5_1; - this->internalFormat = GLIML_GL_RGBA; + this->format = GLIML_GL_RGBA; } } } -/* else if (hdr->ddspf.dwRGBBitCount == 8) { - // FIXME! this->format = GLIML_GL_LUMINANCE; this->internalFormat = GLIML_GL_LUMINANCE; this->type = GLIML_GL_UNSIGNED_BYTE; bytesPerElement = 1; } -*/ // for each face... int faceIndex;