Skip to content

Commit

Permalink
Added optional BGRA support for DDS loader
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Jun 17, 2015
1 parent 0d5ad6d commit c7bfbc5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
6 changes: 6 additions & 0 deletions gliml.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -157,6 +161,7 @@ class context {
bool dxtEnabled;
bool pvrtcEnabled;
bool etc2Enabled;
bool bgraEnabled;
int errorCode;
GLenum target;
bool isCompressed;
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion gliml.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
inline context::context() :
dxtEnabled(false),
pvrtcEnabled(false),
etc2Enabled(false) {
etc2Enabled(false),
bgraEnabled(false) {
this->clear();
}

Expand All @@ -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) {
Expand Down
79 changes: 50 additions & 29 deletions gliml_dds.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down

0 comments on commit c7bfbc5

Please sign in to comment.