From 691c6c7f670e6fb49bee8ebac06fc3854543394b Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Wed, 14 Aug 2013 23:19:12 -0700 Subject: [PATCH] Added utility function imageSwizzleBGRA8 to convert RGBA8 image to BGRA8. --- include/bgfx.h | 3 +++ src/image.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++ src/renderer_gl.cpp | 24 +++-------------- 3 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 src/image.cpp diff --git a/include/bgfx.h b/include/bgfx.h index 3afff85e..ae5e87a9 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -511,6 +511,9 @@ namespace bgfx /// void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num = 1); + /// Swizzle RGBA8 image to BGRA8. + void imageSwizzleBGRA8(uint8_t* _rgbaData, uint32_t _width, uint32_t _height); + /// Returns renderer backend API type. RendererType::Enum getRendererType(); diff --git a/src/image.cpp b/src/image.cpp new file mode 100644 index 00000000..721364e2 --- /dev/null +++ b/src/image.cpp @@ -0,0 +1,63 @@ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include + +namespace bgfx +{ + static void imageSwizzleBGRA8Ref(uint8_t* _rgbaData, uint32_t _width, uint32_t _height) + { + uint32_t dstpitch = _width*4; + for (uint32_t yy = 0; yy < _height; ++yy) + { + uint8_t* dst = &_rgbaData[yy*dstpitch]; + + for (uint32_t xx = 0; xx < _width; ++xx) + { + uint8_t tmp = dst[0]; + dst[0] = dst[2]; + dst[2] = tmp; + dst += 4; + } + } + } + + void imageSwizzleBGRA8(uint8_t* _rgbaData, uint32_t _width, uint32_t _height) + { + if (0 != (_width&0xf) + || _width < 16) + { + imageSwizzleBGRA8Ref(_rgbaData, _width, _height); + return; + } + + uint32_t dstpitch = _width*4; + uint32_t num = dstpitch/16; + + using namespace bx; + + const float4_t mf0f0 = float4_isplat(0xff00ff00); + const float4_t m0f0f = float4_isplat(0x00ff00ff); + + for (uint32_t yy = 0; yy < _height; ++yy) + { + uint8_t* ptr = &_rgbaData[yy*dstpitch]; + + for (uint32_t xx = 0; xx < num; ++xx) + { + const float4_t tabgr = float4_ld(ptr); + const float4_t t00ab = float4_srl(tabgr, 16); + const float4_t tgr00 = float4_sll(tabgr, 16); + const float4_t tgrab = float4_or(t00ab, tgr00); + const float4_t ta0g0 = float4_and(tabgr, mf0f0); + const float4_t t0g0b = float4_and(tgrab, m0f0f); + const float4_t targb = float4_or(ta0g0, t0g0b); + float4_st(ptr, targb); + ptr += 16; + } + } + } + +} // namespace bgfx diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index f27bdb96..87cbab6e 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -9,6 +9,7 @@ # include "renderer_gl.h" # include # include +# include namespace bgfx { @@ -375,23 +376,6 @@ namespace bgfx typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height); - static void rgbaToBgra(uint8_t* _data, uint32_t _width, uint32_t _height) - { - uint32_t dstpitch = _width*4; - for (uint32_t yy = 0; yy < _height; ++yy) - { - uint8_t* dst = &_data[yy*dstpitch]; - - for (uint32_t xx = 0; xx < _width; ++xx) - { - uint8_t tmp = dst[0]; - dst[0] = dst[2]; - dst[2] = tmp; - dst += 4; - } - } - } - static const char* getGLString(GLenum _name) { const char* str = (const char*)glGetString(_name); @@ -775,7 +759,7 @@ namespace bgfx if (GL_RGBA == fmt) { - rgbaToBgra(data, width, height); + imageSwizzleBGRA8(data, width, height); } g_callback->screenShot(_filePath @@ -1472,7 +1456,7 @@ namespace bgfx if (swizzle) { - rgbaToBgra(bits, width, height); + imageSwizzleBGRA8(bits, width, height); } texImage(target+side @@ -1621,7 +1605,7 @@ namespace bgfx if (NULL != data && swizzle) { - rgbaToBgra(data, width, height); + imageSwizzleBGRA8(data, width, height); } texImage(target+side