From 44dadd87b29899920afb23bf5472338f330a6ea1 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 26 Aug 2023 23:49:04 -0600 Subject: [PATCH] imglib: add -tex_saturation_boost [n], defaults to 0.0, 1.0 emulates Q2 tools --- common/imglib.cc | 33 +++++++++++++++++++++++++++++++++ common/settings.cc | 4 +++- include/common/settings.hh | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/common/imglib.cc b/common/imglib.cc index dc6484fe..69070181 100644 --- a/common/imglib.cc +++ b/common/imglib.cc @@ -537,6 +537,29 @@ if (texture) { tex.meta.averageColor = img::calculate_average(tex.pixels); */ +static qvec3b increase_saturation(const qvec3b &color) +{ + qvec3f color_float = qvec3f(color); + color_float /= 255.0f; + + // square it to boost saturation + color_float *= color_float; + + // multiply by 2, then scale back to avoid clipping if needed + color_float *= 2.0f; + + float max_comp = qv::max(color_float); + if (max_comp > 1.0f) { + color_float /= max_comp; + } + + qvec3b color_int; + for (int i = 0; i < 3; ++i) { + color_int[i] = static_cast(std::clamp(color_float[i] * 255.0f, 0.0f, 255.0f)); + } + return color_int; +} + // Load the specified texture from the BSP static void AddTextureName( const std::string_view &textureName, const mbsp_t *bsp, const settings::common_settings &options) @@ -569,6 +592,11 @@ static void AddTextureName( tex.averageColor = *tex.meta.color_override; } else { tex.averageColor = img::calculate_average(tex.pixels); + + if (options.tex_saturation_boost.value() > 0.0f) { + tex.averageColor = + mix(tex.averageColor, increase_saturation(tex.averageColor), options.tex_saturation_boost.value()); + } } if (tex.meta.width && tex.meta.height) { @@ -639,6 +667,11 @@ static void ConvertTextures(const mbsp_t *bsp, const settings::common_settings & tex.averageColor = *tex.meta.color_override; } else { tex.averageColor = img::calculate_average(tex.pixels); + + if (options.tex_saturation_boost.value() > 0.0f) { + tex.averageColor = + mix(tex.averageColor, increase_saturation(tex.averageColor), options.tex_saturation_boost.value()); + } } if (tex.meta.width && tex.meta.height) { diff --git a/common/settings.cc b/common/settings.cc index f1840c9d..d34d8c5e 100644 --- a/common/settings.cc +++ b/common/settings.cc @@ -740,7 +740,9 @@ common_settings::common_settings() "additional paths or archives to add to the search path, mostly for loose files"}, q2rtx{this, "q2rtx", false, &game_group, "adjust settings to best support Q2RTX"}, defaultpaths{this, "defaultpaths", true, &game_group, - "whether the compiler should attempt to automatically derive game/base paths for games that support it"} + "whether the compiler should attempt to automatically derive game/base paths for games that support it"}, + tex_saturation_boost{this, "tex_saturation_boost", 0.0f, 0.0f, 1.0f, &game_group, + "increase texture saturation to match original Q2 tools"} { } diff --git a/include/common/settings.hh b/include/common/settings.hh index 243f728f..f5ddf1ea 100644 --- a/include/common/settings.hh +++ b/include/common/settings.hh @@ -601,6 +601,7 @@ public: setting_set paths; setting_bool q2rtx; setting_invertible_bool defaultpaths; + setting_scalar tex_saturation_boost; common_settings();