From 8a7d5589c267b0aa06162effb095aba92e9d1add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 2 Nov 2018 17:16:32 -0700 Subject: [PATCH] texturec: Added --linear option. --- include/bimg/bimg.h | 6 +++++ src/image.cpp | 44 +++++++++++++++++++++++++++++++++++++ src/image_encode.cpp | 2 +- tools/texturec/texturec.cpp | 17 +++++++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/include/bimg/bimg.h b/include/bimg/bimg.h index 9893e94..337bce8 100644 --- a/include/bimg/bimg.h +++ b/include/bimg/bimg.h @@ -314,6 +314,9 @@ namespace bimg , const void* _src ); + /// + void imageRgba32fToLinear(ImageContainer* _imageContainer); + /// void imageRgba32fToGamma( void* _dst @@ -324,6 +327,9 @@ namespace bimg , const void* _src ); + /// + void imageRgba32fToGamma(ImageContainer* _imageContainer); + /// void imageRgba32fLinearDownsample2x2( void* _dst diff --git a/src/image.cpp b/src/image.cpp index 0eacfb9..05ed59c 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -560,6 +560,28 @@ namespace bimg } } + void imageRgba32fToLinear(ImageContainer* _imageContainer) + { + const uint16_t numSides = _imageContainer->m_numLayers * (_imageContainer->m_cubeMap ? 6 : 1); + + for (uint16_t side = 0; side < numSides; ++side) + { + bimg::ImageMip mip; + bimg::imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip); + + const uint32_t pitch = _imageContainer->m_width*16; + const uint32_t slice = _imageContainer->m_height*pitch; + + for (uint32_t zz = 0, depth = _imageContainer->m_depth; zz < depth; ++zz) + { + const uint32_t srcDataStep = uint32_t(bx::floor(zz * _imageContainer->m_depth / float(depth) ) ); + const uint8_t* srcData = &mip.m_data[srcDataStep*slice]; + + imageRgba32fToLinear(const_cast(srcData), mip.m_width, mip.m_height, 1, pitch, srcData); + } + } + } + void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src) { uint8_t* dst = ( uint8_t*)_dst; @@ -584,6 +606,28 @@ namespace bimg } } + void imageRgba32fToGamma(ImageContainer* _imageContainer) + { + const uint16_t numSides = _imageContainer->m_numLayers * (_imageContainer->m_cubeMap ? 6 : 1); + + for (uint16_t side = 0; side < numSides; ++side) + { + bimg::ImageMip mip; + bimg::imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip); + + const uint32_t pitch = _imageContainer->m_width*16; + const uint32_t slice = _imageContainer->m_height*pitch; + + for (uint32_t zz = 0, depth = _imageContainer->m_depth; zz < depth; ++zz) + { + const uint32_t srcDataStep = uint32_t(bx::floor(zz * _imageContainer->m_depth / float(depth) ) ); + const uint8_t* srcData = &mip.m_data[srcDataStep*slice]; + + imageRgba32fToGamma(const_cast(srcData), mip.m_width, mip.m_height, 1, pitch, srcData); + } + } + } + void imageRgba32fLinearDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src) { const uint32_t dstWidth = _width/2; diff --git a/src/image_encode.cpp b/src/image_encode.cpp index 879c3d3..13da73b 100644 --- a/src/image_encode.cpp +++ b/src/image_encode.cpp @@ -467,7 +467,7 @@ namespace bimg , 4, 3 , STBIR_FLAG_ALPHA_PREMULTIPLIED , STBIR_EDGE_CLAMP - , STBIR_FILTER_CUBICBSPLINE + , STBIR_FILTER_BOX , STBIR_COLORSPACE_LINEAR , NULL ); diff --git a/tools/texturec/texturec.cpp b/tools/texturec/texturec.cpp index f74671a..b9ea4a2 100644 --- a/tools/texturec/texturec.cpp +++ b/tools/texturec/texturec.cpp @@ -25,7 +25,7 @@ #include #define BIMG_TEXTUREC_VERSION_MAJOR 1 -#define BIMG_TEXTUREC_VERSION_MINOR 17 +#define BIMG_TEXTUREC_VERSION_MINOR 18 BX_ERROR_RESULT(TEXTRUREC_ERROR, BX_MAKEFOURCC('t', 'c', 0, 0) ); @@ -46,6 +46,7 @@ struct Options "\t radiance: %s\n" "\t equirect: %s\n" "\t strip: %s\n" + "\t linear: %s\n" , maxSize , mipSkip , edge @@ -58,6 +59,7 @@ struct Options , radiance ? "true" : "false" , equirect ? "true" : "false" , strip ? "true" : "false" + , linear ? "true" : "false" ); } @@ -75,6 +77,7 @@ struct Options bool pma = false; bool sdf = false; bool alphaTest = false; + bool linear = false; }; void imageRgba32fNormalize(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src) @@ -272,8 +275,18 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , false ); + if (!_options.linear) + { + bimg::imageRgba32fToLinear(src); + } + bimg::imageResizeRgba32fLinear(dst, src); + if (!_options.linear) + { + bimg::imageRgba32fToGamma(dst); + } + bimg::imageFree(src); bimg::imageFree(input); @@ -852,6 +865,7 @@ void help(const char* _error = NULL, bool _showHelp = true) " --ref Alpha reference value.\n" " --iqa Image Quality Assessment\n" " --pma Premultiply alpha into RGB channel.\n" + " --linear Input and output texture is linear color space (gamma correction won't be applied).\n" " --max Maximum width/height (image will be scaled down and\n" " aspect ratio will be preserved.\n" " --radiance Radiance cubemap filter. (Lighting model: Phong, PhongBrdf, Blinn, BlinnBrdf, GGX)\n" @@ -987,6 +1001,7 @@ int main(int _argc, const char* _argv[]) options.strip = cmdLine.hasArg("strip"); options.iqa = cmdLine.hasArg("iqa"); options.pma = cmdLine.hasArg("pma"); + options.linear = cmdLine.hasArg("linear"); if (options.equirect && options.strip)