From c27f8e76191778c43bdf0f8ea3a85adc3c0abd7e Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 29 Nov 2020 10:26:05 -0800 Subject: Minor update to SRGBAverage helper function --- Graphics/GraphicsTools/src/GraphicsUtilities.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Graphics/GraphicsTools') diff --git a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp index 52116d12..93e188fd 100644 --- a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp +++ b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp @@ -136,7 +136,8 @@ void GenerateCheckerBoardPattern(Uint32 Width, Uint32 Height, TEXTURE_FORMAT Fmt template ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelType c3) { - static constexpr float MinVal = static_cast(std::numeric_limits::min()); + static_assert(std::numeric_limits::is_integer && !std::numeric_limits::is_signed, "Unsigned integers are expected"); + static constexpr float MaxVal = static_cast(std::numeric_limits::max()); static constexpr float MaxValInv = 1.f / MaxVal; @@ -148,7 +149,7 @@ ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelT float fLinearAverage = (FastSRGBToLinear(fc0) + FastSRGBToLinear(fc1) + FastSRGBToLinear(fc2) + FastSRGBToLinear(fc3)) * 0.25f; float fSRGBAverage = FastLinearToSRGB(fLinearAverage) * MaxVal; - fSRGBAverage = std::max(fSRGBAverage, MinVal); + // Clamping by MaxVal is essential because fast SRGB math is imprecise fSRGBAverage = std::min(fSRGBAverage, MaxVal); return static_cast(fSRGBAverage); -- cgit v1.2.3