diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-04-20 03:25:31 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-04-20 03:25:31 +0000 |
| commit | e9a41f56c0a8bc2fa256b24352598689425bdbe7 (patch) | |
| tree | 61db93b91162e839bd82dad06f9cdfb03cc8ae7e /Graphics | |
| parent | Cosmetic updates in GraphicsUtilities.h (diff) | |
| download | DiligentCore-e9a41f56c0a8bc2fa256b24352598689425bdbe7.tar.gz DiligentCore-e9a41f56c0a8bc2fa256b24352598689425bdbe7.zip | |
Added ColorConversion.h/cpp
Diffstat (limited to 'Graphics')
| -rw-r--r-- | Graphics/GraphicsAccessories/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | Graphics/GraphicsAccessories/interface/ColorConversion.h | 57 | ||||
| -rw-r--r-- | Graphics/GraphicsAccessories/src/ColorConversion.cpp | 88 | ||||
| -rw-r--r-- | Graphics/GraphicsTools/src/GraphicsUtilities.cpp | 18 |
4 files changed, 153 insertions, 14 deletions
diff --git a/Graphics/GraphicsAccessories/CMakeLists.txt b/Graphics/GraphicsAccessories/CMakeLists.txt index 65f10313..6d194893 100644 --- a/Graphics/GraphicsAccessories/CMakeLists.txt +++ b/Graphics/GraphicsAccessories/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required (VERSION 3.6) project(GraphicsAccessories CXX) set(INTERFACE + interface/ColorConversion.h interface/GraphicsAccessories.h interface/ResourceReleaseQueue.h interface/RingBuffer.h @@ -11,7 +12,8 @@ set(INTERFACE interface/VariableSizeGPUAllocationsManager.h ) -set(SOURCE +set(SOURCE + src/ColorConversion.cpp src/SRBMemoryAllocator.cpp src/GraphicsAccessories.cpp ) diff --git a/Graphics/GraphicsAccessories/interface/ColorConversion.h b/Graphics/GraphicsAccessories/interface/ColorConversion.h new file mode 100644 index 00000000..5403c930 --- /dev/null +++ b/Graphics/GraphicsAccessories/interface/ColorConversion.h @@ -0,0 +1,57 @@ +/* Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +#include <cmath> +#include "../../../Primitives/interface/BasicTypes.h" + +namespace Diligent +{ + +// https://en.wikipedia.org/wiki/SRGB +inline float LinearToSRGB(float x) +{ + return x <= 0.0031308 ? x * 12.92f : 1.055f * std::pow(x, 1.f / 2.4f) - 0.055f; +} + +inline float SRGBToLinear(float x) +{ + return x <= 0.04045f ? x / 12.92f : std::pow((x + 0.055f) / 1.055f, 2.4f); +} + +float LinearToSRGB(Uint8 x); +float SRGBToLinear(Uint8 x); + +inline float FastLinearToSRGB(float x) +{ + return x < 0.0031308f ? 12.92f * x : 1.13005f * sqrtf(std::abs(x - 0.00228f)) - 0.13448f * x + 0.005719f; +} + +inline float FastSRGBToLinear(float x) +{ + // http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html + return x * (x * (x * 0.305306011f + 0.682171111f) + 0.012522878f); +} + +} diff --git a/Graphics/GraphicsAccessories/src/ColorConversion.cpp b/Graphics/GraphicsAccessories/src/ColorConversion.cpp new file mode 100644 index 00000000..d917b8ab --- /dev/null +++ b/Graphics/GraphicsAccessories/src/ColorConversion.cpp @@ -0,0 +1,88 @@ +/* Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include <array> +#include <algorithm> +#include "ColorConversion.h" + +namespace Diligent +{ + +namespace +{ + +class LinearToSRGBMap +{ +public: + LinearToSRGBMap() noexcept + { + for (Uint32 i=0; i < m_ToSRBG.size(); ++i) + { + m_ToSRBG[i] = LinearToSRGB(static_cast<float>(i) / 255.f); + } + } + + float operator[](Uint8 x) const + { + return m_ToSRBG[x]; + } + +private: + std::array<float, 256> m_ToSRBG; +}; + +class SRGBToLinearMap +{ +public: + SRGBToLinearMap() noexcept + { + for (Uint32 i=0; i < m_ToLinear.size(); ++i) + { + m_ToLinear[i] = SRGBToLinear(static_cast<float>(i) / 255.f); + } + } + + float operator[](Uint8 x) const + { + return m_ToLinear[x]; + } + +private: + std::array<float, 256> m_ToLinear; +}; + +} // namespace + +float LinearToSRGB(Uint8 x) +{ + static const LinearToSRGBMap map; + return map[x]; +} + +float SRGBToLinear(Uint8 x) +{ + static const SRGBToLinearMap map; + return map[x]; +} + +} // namespace Diligent diff --git a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp index 29837ec3..f1f85aa6 100644 --- a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp +++ b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp @@ -22,11 +22,13 @@ */ #include "pch.h" +#include <algorithm> +#include <cmath> + #include "GraphicsUtilities.h" #include "DebugUtilities.h" #include "GraphicsAccessories.h" -#include <algorithm> -#include <cmath> +#include "ColorConversion.h" #define PI_F 3.1415926f @@ -64,16 +66,6 @@ void GenerateCheckerBoardPatternInternal(Uint32 Width, Uint32 Height, TEXTURE_FO } } -static float LinearToSRGB(float x) -{ - // This is exactly the sRGB curve - //return x < 0.0031308 ? 12.92 * x : 1.055 * pow(std::abs(x), 1.0 / 2.4) - 0.055; - - // This is cheaper but nearly equivalent - return x < 0.0031308f ? 12.92f * x : 1.13005f * sqrtf(std::abs(x - 0.00228f)) - 0.13448f * x + 0.005719f; -} - - void GenerateCheckerBoardPattern(Uint32 Width, Uint32 Height, TEXTURE_FORMAT Fmt, Uint32 HorzCells, Uint32 VertCells, Uint8 *pData, Uint32 StrideInBytes) { const auto& FmtAttribs = GetTextureFormatAttribs(Fmt); @@ -94,7 +86,7 @@ void GenerateCheckerBoardPattern(Uint32 Width, Uint32 Height, TEXTURE_FORMAT Fmt GenerateCheckerBoardPatternInternal(Width, Height, Fmt, HorzCells, VertCells, pData, StrideInBytes, [](Uint8 *pDstTexel, Uint32 NumComponents, float fVal) { - Uint8 uVal = static_cast<Uint8>( LinearToSRGB(fVal) * 255.f); + Uint8 uVal = static_cast<Uint8>( FastLinearToSRGB(fVal) * 255.f); for (Uint32 c = 0; c < NumComponents; ++c) pDstTexel[c] = uVal; }); |
