From 9b5edc85ae029eff3820f8fe7c0b697578295e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Sun, 10 Nov 2019 20:26:04 -0800 Subject: [PATCH] WIP: Added ability to enable/disable decoders. --- src/bimg_p.h | 2 + src/config.h | 31 +++++++++ src/image.cpp | 178 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 152 insertions(+), 59 deletions(-) create mode 100644 src/config.h diff --git a/src/bimg_p.h b/src/bimg_p.h index 557bcaa..4d68fcd 100644 --- a/src/bimg_p.h +++ b/src/bimg_p.h @@ -14,6 +14,8 @@ #include #include +#include "config.h" + #define BIMG_CHUNK_MAGIC_TEX BX_MAKEFOURCC('T', 'E', 'X', 0x0) #define BIMG_CHUNK_MAGIC_GNF BX_MAKEFOURCC('G', 'N', 'F', ' ') diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..f814021 --- /dev/null +++ b/src/config.h @@ -0,0 +1,31 @@ +/* + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#ifndef BIMG_CONFIG_H_HEADER_GUARD +#define BIMG_CONFIG_H_HEADER_GUARD + +#include + +#ifndef BIMG_DECODE_BC1 +# define BIMG_DECODE_BC1 1 +#endif // BIMG_DECODE_BC1 + +#ifndef BIMG_DECODE_BC2 +# define BIMG_DECODE_BC2 1 +#endif // BIMG_DECODE_BC2 + +#ifndef BIMG_DECODE_BC3 +# define BIMG_DECODE_BC3 1 +#endif // BIMG_DECODE_BC3 + +#ifndef BIMG_DECODE_BC4 +# define BIMG_DECODE_BC4 1 +#endif // BIMG_DECODE_BC4 + +#ifndef BIMG_DECODE_BC5 +# define BIMG_DECODE_BC5 1 +#endif // BIMG_DECODE_BC5 + +#endif // BIMG_CONFIG_H_HEADER_GUARD diff --git a/src/image.cpp b/src/image.cpp index 9ef2e3a..8ac47a3 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1343,6 +1343,11 @@ namespace bimg void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) ) + { + return; + } + uint8_t colors[4*3]; uint32_t c0 = _src[0] | (_src[1] << 8); @@ -1374,6 +1379,11 @@ namespace bimg void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) ) + { + return; + } + uint8_t colors[4*4]; uint32_t c0 = _src[0] | (_src[1] << 8); @@ -1425,6 +1435,11 @@ namespace bimg void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC2) ) + { + return; + } + for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4) { uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf; @@ -1434,6 +1449,11 @@ namespace bimg void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8]) { + if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) ) + { + return; + } + uint8_t alpha[8]; alpha[0] = _src[0]; alpha[1] = _src[1]; @@ -4255,103 +4275,143 @@ namespace bimg switch (_srcFormat) { case TextureFormat::BC1: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC1) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt1(temp, src); - src += 8; + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt1(temp, src); + src += 8; - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC2: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC2) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt23A(temp+3, src); - src += 8; - decodeBlockDxt(temp, src); - src += 8; + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt23A(temp+3, src); + src += 8; + decodeBlockDxt(temp, src); + src += 8; - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC3: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC3) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt45A(temp+3, src); - src += 8; - decodeBlockDxt(temp, src); - src += 8; + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt45A(temp+3, src); + src += 8; + decodeBlockDxt(temp, src); + src += 8; - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC4: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC4) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt45A(temp, src); - src += 8; + for (uint32_t xx = 0; xx < width; ++xx) + { + decodeBlockDxt45A(temp, src); + src += 8; - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC5: - for (uint32_t yy = 0; yy < height; ++yy) + if (BX_ENABLED(BIMG_DECODE_BC5) ) { - for (uint32_t xx = 0; xx < width; ++xx) + for (uint32_t yy = 0; yy < height; ++yy) { - decodeBlockDxt45A(temp+2, src); - src += 8; - decodeBlockDxt45A(temp+1, src); - src += 8; - - for (uint32_t ii = 0; ii < 16; ++ii) + for (uint32_t xx = 0; xx < width; ++xx) { - float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f; - float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f; - float nz = bx::sqrt(1.0f - nx*nx - ny*ny); - temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f); - temp[ii*4+3] = 0; - } + decodeBlockDxt45A(temp+2, src); + src += 8; + decodeBlockDxt45A(temp+1, src); + src += 8; - uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; - bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); - bx::memCopy(&block[1*_dstPitch], &temp[16], 16); - bx::memCopy(&block[2*_dstPitch], &temp[32], 16); - bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + for (uint32_t ii = 0; ii < 16; ++ii) + { + float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f; + float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f; + float nz = bx::sqrt(1.0f - nx*nx - ny*ny); + temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f); + temp[ii*4+3] = 0; + } + + uint8_t* block = &dst[yy*_dstPitch*4 + xx*16]; + bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16); + bx::memCopy(&block[1*_dstPitch], &temp[16], 16); + bx::memCopy(&block[2*_dstPitch], &temp[32], 16); + bx::memCopy(&block[3*_dstPitch], &temp[48], 16); + } } } + else + { + BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5)."); + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) ); + } break; case TextureFormat::BC6H: