From 8a54ba346278d6fa7f19165ab8e0afd67245e36a Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sun, 11 Sep 2016 02:50:08 +0200 Subject: [PATCH] Added GTC_color_encoding --- glm/gtc/color_encoding.hpp | 50 +++++++++++++++++++++++++ glm/gtc/color_encoding.inl | 65 +++++++++++++++++++++++++++++++++ glm/gtc/color_space.hpp | 10 ++--- readme.md | 1 + test/gtc/CMakeLists.txt | 1 + test/gtc/gtc_color_encoding.cpp | 51 ++++++++++++++++++++++++++ 6 files changed, 173 insertions(+), 5 deletions(-) create mode 100644 glm/gtc/color_encoding.hpp create mode 100644 glm/gtc/color_encoding.inl create mode 100644 test/gtc/gtc_color_encoding.cpp diff --git a/glm/gtc/color_encoding.hpp b/glm/gtc/color_encoding.hpp new file mode 100644 index 00000000..0ff5fdcb --- /dev/null +++ b/glm/gtc/color_encoding.hpp @@ -0,0 +1,50 @@ +/// @ref gtc_color_encoding +/// @file glm/gtc/color_encoding.hpp +/// +/// @see core (dependence) +/// @see gtc_color_encoding (dependence) +/// +/// @defgroup gtc_color_encoding GLM_GTC_color_encoding +/// @ingroup gtc +/// +/// @brief Allow to perform bit operations on integer values +/// +/// need to be included to use these functionalities. + +#pragma once + +// Dependencies +#include "../detail/setup.hpp" +#include "../detail/precision.hpp" +#include "../vec3.hpp" +#include + +#if GLM_MESSAGES == GLM_MESSAGES_ENABLED && !defined(GLM_EXT_INCLUDED) +# pragma message("GLM: GLM_GTC_color_encoding extension included") +#endif + +namespace glm +{ + /// @addtogroup gtc_color_encoding + /// @{ + + /// Convert a linear sRGB color to D65 YUV. + template + GLM_FUNC_DECL tvec3 convertLinearSRGBToD65XYZ(tvec3 const& ColorLinearSRGB); + + /// Convert a D65 YUV color to linear sRGB. + template + GLM_FUNC_DECL tvec3 convertD65XYZToLinearSRGB(tvec3 const& ColorD65XYZ); + + /// Convert a D50 YUV color to D65 YUV. + template + GLM_FUNC_DECL tvec3 convertD50XYZToD65XYZ(tvec3 const& ColorD50XYZ); + + /// Convert a D65 YUV color to D50 YUV. + template + GLM_FUNC_DECL tvec3 convertD65XYZToD50XYZ(tvec3 const& ColorD65XYZ); + + /// @} +} //namespace glm + +#include "color_encoding.inl" diff --git a/glm/gtc/color_encoding.inl b/glm/gtc/color_encoding.inl new file mode 100644 index 00000000..8d59c429 --- /dev/null +++ b/glm/gtc/color_encoding.inl @@ -0,0 +1,65 @@ +/// @ref gtc_color_encoding +/// @file glm/gtc/color_encoding.inl + +namespace glm +{ + template + GLM_FUNC_QUALIFIER tvec3 convertLinearSRGBToD65XYZ(tvec3 const& ColorLinearSRGB) + { + static const tvec3 M(0.490f, 0.17697f, 0.2f); + static const tvec3 N(0.31f, 0.8124f, 0.01063f); + static const tvec3 O(0.490f, 0.01f, 0.99f); + + return (M * ColorLinearSRGB + N * ColorLinearSRGB + O * ColorLinearSRGB) * static_cast(5.650675255693055f); + } + + template + GLM_FUNC_QUALIFIER tvec3 convertD65XYZToLinearSRGB(tvec3 const& ColorD65XYZ) + { + static const tvec3 M(0.41847f, -0.091169f, 0.0009209f); + static const tvec3 N(-0.15866f, 0.25243f, 0.015708f); + static const tvec3 O(0.0009209f, -0.0025498f, 0.1786f); + + return M * ColorD65XYZ + N * ColorD65XYZ + O * ColorD65XYZ; + } + + template + GLM_FUNC_QUALIFIER tvec3 convertLinearSRGBToD50XYZ(tvec3 const& ColorLinearSRGB) + { + static const tvec3 M(0.436030342570117f, 0.222438466210245f, 0.013897440074263f); + static const tvec3 N(0.385101860087134f, 0.716942745571917f, 0.097076381494207f); + static const tvec3 O(0.143067806654203f, 0.060618777416563f, 0.713926257896652f); + + return M * ColorLinearSRGB + N * ColorLinearSRGB + O * ColorLinearSRGB; + } + + template + GLM_FUNC_QUALIFIER tvec3 convertD50XYZToLinearSRGB(tvec3 const& ColorD50XYZ) + { + static const tvec3 M(); + static const tvec3 N(); + static const tvec3 O(); + + return M * ColorD65XYZ + N * ColorD65XYZ + O * ColorD65XYZ; + } + + template + GLM_FUNC_QUALIFIER tvec3 convertD65XYZToD50XYZ(tvec3 const& ColorD65XYZ) + { + static const tvec3 M(+1.047844353856414f, +0.029549007606644f, -0.009250984365223f); + static const tvec3 N(+0.022898981050086f, +0.990508028941971f, +0.015072338237051f); + static const tvec3 O(-0.050206647741605f, -0.017074711360960f, +0.751717835079977f); + + return M * ColorD65XYZ + N * ColorD65XYZ + O * ColorD65XYZ; + } + + template + GLM_FUNC_QUALIFIER tvec3 convertD50XYZToD65XYZ(tvec3 const& ColorD50XYZ) + { + static const tvec3 M(); + static const tvec3 N(); + static const tvec3 O(); + + return M * ColorD50XYZ + N * ColorD50XYZ + O * ColorD50XYZ; + } +}//namespace glm diff --git a/glm/gtc/color_space.hpp b/glm/gtc/color_space.hpp index 08ece8f8..9ae9692f 100644 --- a/glm/gtc/color_space.hpp +++ b/glm/gtc/color_space.hpp @@ -9,7 +9,7 @@ /// /// @brief Allow to perform bit operations on integer values /// -/// need to be included to use these functionalities. +/// need to be included to use these functionalities. #pragma once @@ -31,22 +31,22 @@ namespace glm /// @{ /// Convert a linear color to sRGB color using a standard gamma correction. - /// IEC 61966-2-1:1999 specification https://www.w3.org/Graphics/Color/srgb + /// IEC 61966-2-1:1999 / Rec. 709 specification https://www.w3.org/Graphics/Color/srgb template class vecType> GLM_FUNC_DECL vecType convertLinearToSRGB(vecType const & ColorLinear); /// Convert a linear color to sRGB color using a custom gamma correction. - /// IEC 61966-2-1:1999 specification https://www.w3.org/Graphics/Color/srgb + /// IEC 61966-2-1:1999 / Rec. 709 specification https://www.w3.org/Graphics/Color/srgb template class vecType> GLM_FUNC_DECL vecType convertLinearToSRGB(vecType const & ColorLinear, T Gamma); /// Convert a sRGB color to linear color using a standard gamma correction. - /// IEC 61966-2-1:1999 specification https://www.w3.org/Graphics/Color/srgb + /// IEC 61966-2-1:1999 / Rec. 709 specification https://www.w3.org/Graphics/Color/srgb template class vecType> GLM_FUNC_DECL vecType convertSRGBToLinear(vecType const & ColorSRGB); /// Convert a sRGB color to linear color using a custom gamma correction. - // IEC 61966-2-1:1999 specification https://www.w3.org/Graphics/Color/srgb + // IEC 61966-2-1:1999 / Rec. 709 specification https://www.w3.org/Graphics/Color/srgb template class vecType> GLM_FUNC_DECL vecType convertSRGBToLinear(vecType const & ColorSRGB, T Gamma); diff --git a/readme.md b/readme.md index d43d5340..3393ea13 100644 --- a/readme.md +++ b/readme.md @@ -53,6 +53,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate) #### [GLM 0.9.9.0](https://github.com/g-truc/glm/releases/latest) - 2017-XX-XX ##### Features: +- Added GLM_GTC_color_encoding extension ##### Improvements: diff --git a/test/gtc/CMakeLists.txt b/test/gtc/CMakeLists.txt index ab26247a..56b2f13a 100644 --- a/test/gtc/CMakeLists.txt +++ b/test/gtc/CMakeLists.txt @@ -1,4 +1,5 @@ glmCreateTestGTC(gtc_bitfield) +glmCreateTestGTC(gtc_color_encoding) glmCreateTestGTC(gtc_color_space) glmCreateTestGTC(gtc_constants) glmCreateTestGTC(gtc_epsilon) diff --git a/test/gtc/gtc_color_encoding.cpp b/test/gtc/gtc_color_encoding.cpp new file mode 100644 index 00000000..a60b88ca --- /dev/null +++ b/test/gtc/gtc_color_encoding.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +namespace srgb +{ + int test() + { + int Error(0); + + glm::vec3 const ColorSourceRGB(1.0, 0.5, 0.0); + + { + glm::vec3 const ColorSRGB = glm::convertLinearSRGBToD65XYZ(ColorSourceRGB); + glm::vec3 const ColorRGB = glm::convertD65XYZToLinearSRGB(ColorSRGB); + Error += glm::all(glm::epsilonEqual(ColorSourceRGB, ColorRGB, 0.00001f)) ? 0 : 1; + } + + { + glm::vec3 const ColorSRGB = glm::convertLinearToSRGB(ColorSourceRGB, 2.8f); + glm::vec3 const ColorRGB = glm::convertSRGBToLinear(ColorSRGB, 2.8f); + Error += glm::all(glm::epsilonEqual(ColorSourceRGB, ColorRGB, 0.00001f)) ? 0 : 1; + } + + glm::vec4 const ColorSourceRGBA(1.0, 0.5, 0.0, 1.0); + + { + glm::vec4 const ColorSRGB = glm::convertLinearToSRGB(ColorSourceRGBA); + glm::vec4 const ColorRGB = glm::convertSRGBToLinear(ColorSRGB); + Error += glm::all(glm::epsilonEqual(ColorSourceRGBA, ColorRGB, 0.00001f)) ? 0 : 1; + } + + { + glm::vec4 const ColorSRGB = glm::convertLinearToSRGB(ColorSourceRGBA, 2.8f); + glm::vec4 const ColorRGB = glm::convertSRGBToLinear(ColorSRGB, 2.8f); + Error += glm::all(glm::epsilonEqual(ColorSourceRGBA, ColorRGB, 0.00001f)) ? 0 : 1; + } + + return Error; + } +}//namespace srgb + +int main() +{ + int Error(0); + + Error += srgb::test(); + + return Error; +}