From 796e80e6453d6971abdd47e6e431ddb565a3c770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 16 May 2017 17:25:11 -0700 Subject: [PATCH] texturec: Added quality option. --- include/bimg/encode.h | 18 ++++++++++++++++-- src/image_encode.cpp | 26 ++++++++++++++++++-------- tools/texturec/texturec.cpp | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/include/bimg/encode.h b/include/bimg/encode.h index 6ac9eec..4e540d0 100644 --- a/include/bimg/encode.h +++ b/include/bimg/encode.h @@ -10,6 +10,18 @@ namespace bimg { + struct Quality + { + enum Enum + { + Default, + Highest, + Fastest, + + Count + }; + }; + /// bool imageEncodeFromRgba8( void* _dst @@ -17,6 +29,7 @@ namespace bimg , uint32_t _width , uint32_t _height , TextureFormat::Enum _format + , Quality::Enum _quality ); /// @@ -27,7 +40,8 @@ namespace bimg , uint32_t _width , uint32_t _height , TextureFormat::Enum _format - ); + , Quality::Enum _quality + ); /// void imageRgba32f11to01( @@ -36,7 +50,7 @@ namespace bimg , uint32_t _height , uint32_t _pitch , const void* _src - ); + ); /// void imageMakeDist( diff --git a/src/image_encode.cpp b/src/image_encode.cpp index 936ed1d..a5dc5ae 100644 --- a/src/image_encode.cpp +++ b/src/image_encode.cpp @@ -3,6 +3,7 @@ * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ +#include #include "bimg_p.h" #include @@ -26,7 +27,15 @@ extern "C" { namespace bimg { - bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format) + static uint32_t s_squishQuality[] = + { + squish::kColourClusterFit, // Default + squish::kColourIterativeClusterFit, // Highest + squish::kColourRangeFit, // Fastest + }; + BX_STATIC_ASSERT(Quality::Count == BX_COUNTOF(s_squishQuality) ); + + bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, Quality::Enum _quality) { switch (_format) { @@ -36,11 +45,12 @@ namespace bimg case TextureFormat::BC4: case TextureFormat::BC5: squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst - , _format == TextureFormat::BC2 ? squish::kDxt3 - : _format == TextureFormat::BC3 ? squish::kDxt5 - : _format == TextureFormat::BC4 ? squish::kBc4 - : _format == TextureFormat::BC5 ? squish::kBc5 - : squish::kDxt1 + , s_squishQuality[_quality] + | (_format == TextureFormat::BC2 ? squish::kDxt3 + : _format == TextureFormat::BC3 ? squish::kDxt5 + : _format == TextureFormat::BC4 ? squish::kBc4 + : _format == TextureFormat::BC5 ? squish::kBc5 + : squish::kDxt1) ); return true; @@ -121,7 +131,7 @@ namespace bimg return imageConvert(_dst, _format, _src, TextureFormat::RGBA8, _width, _height); } - bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format) + bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, Quality::Enum _quality) { const uint8_t* src = (const uint8_t*)_src; @@ -163,7 +173,7 @@ namespace bimg } } - imageEncodeFromRgba8(_dst, temp, _width, _height, _format); + imageEncodeFromRgba8(_dst, temp, _width, _height, _format, _quality); BX_FREE(_allocator, temp); } return true; diff --git a/tools/texturec/texturec.cpp b/tools/texturec/texturec.cpp index 8fafa27..6d885a0 100644 --- a/tools/texturec/texturec.cpp +++ b/tools/texturec/texturec.cpp @@ -23,7 +23,7 @@ #include #define BIMG_TEXTUREC_VERSION_MAJOR 1 -#define BIMG_TEXTUREC_VERSION_MINOR 2 +#define BIMG_TEXTUREC_VERSION_MINOR 3 struct Options { @@ -31,6 +31,7 @@ struct Options : maxSize(UINT32_MAX) , edge(0.0f) , format(bimg::TextureFormat::Count) + , quality(bimg::Quality::Default) , mips(false) , normalMap(false) , iqa(false) @@ -61,6 +62,7 @@ struct Options uint32_t maxSize; float edge; bimg::TextureFormat::Enum format; + bimg::Quality::Enum quality; bool mips; bool normalMap; bool iqa; @@ -230,6 +232,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_width , dstMip.m_height , outputFormat + , _options.quality ); for (uint8_t lod = 1; lod < numMips; ++lod) @@ -257,6 +260,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_width , dstMip.m_height , outputFormat + , _options.quality ); } @@ -294,6 +298,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_width , dstMip.m_height , outputFormat + , _options.quality ); if (1 < numMips) @@ -330,6 +335,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_width , dstMip.m_height , outputFormat + , _options.quality ); if (!succeeded) @@ -374,7 +380,13 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip); dstData = const_cast(dstMip.m_data); - bimg::imageEncodeFromRgba8(dstData, rgba, dstMip.m_width, dstMip.m_height, outputFormat); + bimg::imageEncodeFromRgba8(dstData + , rgba + , dstMip.m_width + , dstMip.m_height + , outputFormat + , _options.quality + ); for (uint8_t lod = 1; lod < numMips; ++lod) { @@ -393,6 +405,7 @@ bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData , dstMip.m_width , dstMip.m_height , outputFormat + , _options.quality ); } @@ -470,6 +483,7 @@ void help(const char* _error = NULL) " -f Input file path.\n" " -o Output file path (file will be written in KTX format).\n" " -t Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n" + " -q Encoding quality (default, fastest, highest).\n" " -m, --mips Generate mip-maps.\n" " -n, --normalmap Input texture is normal map.\n" " --sdf Compute SDF texture.\n" @@ -558,6 +572,20 @@ int main(int _argc, const char* _argv[]) } } + const char* quality = cmdLine.findOption('q'); + if (NULL != quality) + { + switch (bx::toLower(quality[0]) ) + { + case 'h': options.quality = bimg::Quality::Highest; break; + case 'f': options.quality = bimg::Quality::Fastest; break; + case 'd': options.quality = bimg::Quality::Default; break; + default: + help("Invalid quality specified."); + return EXIT_FAILURE; + } + } + bx::CrtFileReader reader; if (!bx::open(&reader, inputFileName) ) {