diff options
Diffstat (limited to '3rdparty/bimg/src/image.cpp')
-rw-r--r-- | 3rdparty/bimg/src/image.cpp | 137 |
1 files changed, 75 insertions, 62 deletions
diff --git a/3rdparty/bimg/src/image.cpp b/3rdparty/bimg/src/image.cpp index 6bf0d5245da..9ef2e3a6656 100644 --- a/3rdparty/bimg/src/image.cpp +++ b/3rdparty/bimg/src/image.cpp @@ -1,14 +1,14 @@ /* - * Copyright 2011-2018 Branimir Karadzic. All rights reserved. + * Copyright 2011-2019 Branimir Karadzic. All rights reserved. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause */ #include "bimg_p.h" #include <bx/hash.h> -#if BIMG_CONFIG_ASTC_DECODE -# include "../3rdparty/astc/astc_lib.h" -#endif // BIMG_CONFIG_ASTC_DECODE +#include <astc-codec/astc-codec.h> + +#include <bx/debug.h> namespace bimg { @@ -252,6 +252,11 @@ namespace bimg const char* getName(TextureFormat::Enum _format) { + if (_format >= TextureFormat::Count) + { + return "Unknown?!"; + } + return s_textureFormatName[_format]; } @@ -928,7 +933,7 @@ namespace bimg xyz[1] += rgba1[5]; xyz[2] += rgba1[6]; - bx::store(dst, bx::normalize(bx::load(xyz) ) ); + bx::store(dst, bx::normalize(bx::load<bx::Vec3>(xyz) ) ); } } } @@ -1014,7 +1019,7 @@ namespace bimg for (uint32_t zz = 0; zz < _depth; ++zz, src += _srcPitch*_height, dst += _dstPitch*_height) { - bx::memCopy(dst, src, pitch, _height, _srcPitch, _dstPitch); + bx::memCopy(dst, _dstPitch, src, _srcPitch, pitch, _height); } } @@ -1060,7 +1065,7 @@ namespace bimg { NULL, NULL }, // ASTC10x5 { NULL, NULL }, // Unknown { NULL, NULL }, // R1 - { bx::packR8, bx::unpackR8 }, // A8 + { bx::packA8, bx::unpackA8 }, // A8 { bx::packR8, bx::unpackR8 }, // R8 { bx::packR8I, bx::unpackR8I }, // R8I { bx::packR8U, bx::unpackR8U }, // R8U @@ -1154,16 +1159,14 @@ namespace bimg } } - void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch) + void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch) { const uint8_t* src = (uint8_t*)_src; uint8_t* dst = (uint8_t*)_dst; - const uint32_t dstPitch = _width * _dstBpp / 8; - for (uint32_t zz = 0; zz < _depth; ++zz) { - for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += dstPitch) + for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _dstPitch) { for (uint32_t xx = 0; xx < _width; ++xx) { @@ -1175,7 +1178,7 @@ namespace bimg } } - bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch) + bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch) { UnpackFn unpack = s_packUnpack[_srcFormat].unpack; PackFn pack = s_packUnpack[_dstFormat].pack; @@ -1205,7 +1208,7 @@ namespace bimg const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel; const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel; - imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch); + imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch, _dstPitch); return true; } @@ -1220,7 +1223,10 @@ namespace bimg return true; } - return imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8); + const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel; + const uint32_t dstPitch = _width * dstBpp / 8; + + return imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8, dstPitch); } ImageContainer* imageConvert(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, const ImageContainer& _input, bool _convertMips) @@ -4226,12 +4232,12 @@ namespace bimg uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8); void* temp = BX_ALLOC(_allocator, size); imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat); - imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4); + imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4, _dstPitch); BX_FREE(_allocator, temp); } else { - imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch); + imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch); } } } @@ -4360,7 +4366,7 @@ namespace bimg , false ); imageDecodeToRgba32f(_allocator, rgba32f->m_data, _src, _width, _height, 1, _width*16, _srcFormat); - imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16); + imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16, _dstPitch); imageFree(rgba32f); } break; @@ -4523,24 +4529,8 @@ namespace bimg case TextureFormat::ASTC8x5: case TextureFormat::ASTC8x6: case TextureFormat::ASTC10x5: -#if BIMG_CONFIG_ASTC_DECODE - astc_decompress - ( - (const uint8_t*) _src, - s_imageBlockInfo[_srcFormat].blockWidth, - s_imageBlockInfo[_srcFormat].blockHeight, - ASTC_DECODE_LDR_LINEAR, - - _width, - _height, - (uint8_t*) _dst, - ASTC_BGRA, - _dstPitch - ); -#else - BX_WARN(false, "ASTC decoder is not implemented."); - imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) ); -#endif + imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat); + imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, _dstPitch); break; case TextureFormat::RGBA8: @@ -4554,7 +4544,7 @@ namespace bimg { const uint32_t srcPitch = _width * 4; const uint32_t size = bx::uint32_min(srcPitch, _dstPitch); - bx::memCopy(_dst, _src, size, _height, srcPitch, _dstPitch); + bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height); } break; @@ -4562,7 +4552,7 @@ namespace bimg { const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel; const uint32_t srcPitch = _width * srcBpp / 8; - if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch) ) + if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch) ) { // Failed to convert, just make ugly red-yellow checkerboard texture. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) ); @@ -4580,7 +4570,7 @@ namespace bimg { const uint32_t srcPitch = _width * 4; const uint32_t size = bx::uint32_min(srcPitch, _dstPitch); - bx::memCopy(_dst, _src, size, _height, srcPitch, _dstPitch); + bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height); } break; @@ -4591,6 +4581,32 @@ namespace bimg } break; + case TextureFormat::ASTC4x4: + case TextureFormat::ASTC5x5: + case TextureFormat::ASTC6x6: + case TextureFormat::ASTC8x5: + case TextureFormat::ASTC8x6: + case TextureFormat::ASTC10x5: + if (!astc_codec::ASTCDecompressToRGBA( + (const uint8_t*)_src + , imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, _srcFormat) + , _width + , _height + , TextureFormat::ASTC4x4 == _srcFormat ? astc_codec::FootprintType::k4x4 + : TextureFormat::ASTC5x5 == _srcFormat ? astc_codec::FootprintType::k5x5 + : TextureFormat::ASTC6x6 == _srcFormat ? astc_codec::FootprintType::k6x6 + : TextureFormat::ASTC8x5 == _srcFormat ? astc_codec::FootprintType::k8x5 + : TextureFormat::ASTC8x6 == _srcFormat ? astc_codec::FootprintType::k8x6 + : astc_codec::FootprintType::k10x5 + , (uint8_t*)_dst + , _width*_height*4 + , _dstPitch + ) ) + { + imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) ); + } + break; + default: { const uint32_t srcPitch = _width * 4; @@ -4753,7 +4769,7 @@ namespace bimg } else { - imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch); + imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch); } break; } @@ -4802,12 +4818,7 @@ namespace bimg depth = bx::max<uint32_t>(1, depth); const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize; - if (mipSize != width*height*depth*bpp/8) - { - BX_TRACE("x"); - } - - const uint32_t size = mipSize*numSides; + const uint32_t size = mipSize*numSides; uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE); BX_CHECK(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize); BX_UNUSED(size, imageSize); @@ -5267,23 +5278,25 @@ namespace bimg } } - if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat) - { - for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii) - { - if (s_translateDdsFourccFormat[ii].m_textureFormat == _format) - { - fourccFormat = s_translateDdsFourccFormat[ii].m_format; - break; - } - } - } - - if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat && UINT32_MAX == fourccFormat) - { - BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported."); - return 0; - } + if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat) + { + for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii) + { + if (s_translateDdsFourccFormat[ii].m_textureFormat == _format) + { + fourccFormat = s_translateDdsFourccFormat[ii].m_format; + break; + } + } + } + + if (UINT32_MAX == ddspf + && UINT32_MAX == dxgiFormat + && UINT32_MAX == fourccFormat) + { + BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported."); + return 0; + } const uint32_t bpp = getBitsPerPixel(_format); |