From 39a8c56545d961d18b1e79bebecf80c3f15a4919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 29 Nov 2017 16:53:00 -0800 Subject: [PATCH] Fixed SDF. --- include/bimg/bimg.h | 12 +++++++++ src/image.cpp | 25 ++++++++++++++++++ tools/texturec/texturec.cpp | 52 +++++++++++++++++++++++++++++++++++-- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/include/bimg/bimg.h b/include/bimg/bimg.h index 6831fd1..d4b1302 100644 --- a/include/bimg/bimg.h +++ b/include/bimg/bimg.h @@ -533,6 +533,18 @@ namespace bimg , bx::Error* _err ); + /// + void imageDecodeToR8( + bx::AllocatorI* _allocator + , void* _dst + , const void* _src + , uint32_t _width + , uint32_t _height + , uint32_t _depth + , uint32_t _dstPitch + , TextureFormat::Enum _srcFormat + ); + /// void imageDecodeToBgra8( void* _dst diff --git a/src/image.cpp b/src/image.cpp index d76e824..9b34985 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -2775,6 +2775,31 @@ namespace bimg return imageParse(_imageContainer, &reader, _err); } + void imageDecodeToR8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _dstPitch, TextureFormat::Enum _srcFormat) + { + const uint8_t* src = (const uint8_t*)_src; + uint8_t* dst = (uint8_t*)_dst; + + const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel; + const uint32_t srcPitch = _width*srcBpp/8; + + for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch) + { + if (isCompressed(_srcFormat)) + { + uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8); + void* temp = BX_ALLOC(_allocator, size); + imageDecodeToRgba8(temp, _src, _width, _height, _width*4, _srcFormat); + imageConvert(dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4); + BX_FREE(_allocator, temp); + } + else + { + imageConvert(dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch); + } + } + } + void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat) { const uint8_t* src = (const uint8_t*)_src; diff --git a/tools/texturec/texturec.cpp b/tools/texturec/texturec.cpp index d120391..8b6d27d 100644 --- a/tools/texturec/texturec.cpp +++ b/tools/texturec/texturec.cpp @@ -118,6 +118,11 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData outputFormat = _options.format; } + if (_options.sdf) + { + outputFormat = bimg::TextureFormat::R8; + } + const bimg::ImageBlockInfo& inputBlockInfo = bimg::getBlockInfo(inputFormat); const bimg::ImageBlockInfo& outputBlockInfo = bimg::getBlockInfo(outputFormat); const uint32_t blockWidth = outputBlockInfo.blockWidth; @@ -410,6 +415,43 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData BX_FREE(_allocator, rgbaDst); } + else if (_options.sdf) + { + uint32_t size = bimg::imageGetSize( + NULL + , uint16_t(dstMip.m_width) + , uint16_t(dstMip.m_height) + , uint16_t(dstMip.m_depth) + , false + , false + , 1 + , bimg::TextureFormat::R8 + ); + temp = BX_ALLOC(_allocator, size); + uint8_t* rgba = (uint8_t*)temp; + + bimg::imageDecodeToR8(_allocator + , rgba + , mip.m_data + , mip.m_width + , mip.m_height + , mip.m_depth + , mip.m_width + , mip.m_format + ); + + bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip); + dstData = const_cast(dstMip.m_data); + + bimg::imageMakeDist(_allocator + , dstData + , mip.m_width + , mip.m_height + , mip.m_width + , _options.edge + , rgba + ); + } else { uint32_t size = bimg::imageGetSize( @@ -666,7 +708,10 @@ int main(int _argc, const char* _argv[]) if (NULL != edgeOpt) { options.sdf = true; - options.edge = (float)atof(edgeOpt); + if (!bx::fromString(&options.edge, edgeOpt) ) + { + options.edge = 255.0f; + } } else { @@ -674,7 +719,10 @@ int main(int _argc, const char* _argv[]) if (NULL != alphaRef) { options.alphaTest = true; - options.edge = (float)atof(alphaRef); + if (!bx::fromString(&options.edge, alphaRef)) + { + options.edge = 0.5f; + } } }