vsg: include/vsg/utils/CoordinateSpace.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
CoordinateSpace.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2024 Chris Djali
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Export.h>
16#include <vsg/maths/color.h>
17
18namespace vsg
19{
20
21 enum class CoordinateSpace
22 {
23 NO_PREFERENCE = 0,
24 LINEAR = (1 << 0),
25 sRGB = (1 << 1)
26 };
27 VSG_type_name(vsg::CoordinateSpace);
28
29 template<typename T>
30 constexpr T linear_to_sRGB(T c)
31 {
32 constexpr T cutoff = static_cast<T>(0.04045 / 12.92);
33 constexpr T linearFactor = static_cast<T>(12.92);
34 constexpr T nonlinearFactor = static_cast<T>(1.055);
35 constexpr T exponent = static_cast<T>(1.0 / 2.4);
36 if (c <= cutoff)
37 return c * linearFactor;
38 else
39 return std::pow(c, exponent) * nonlinearFactor - static_cast<T>(0.055);
40 }
41
42 template<typename T>
43 constexpr T sRGB_to_linear(T c)
44 {
45 constexpr T cutoff = static_cast<T>(0.04045);
46 constexpr T linearFactor = static_cast<T>(1.0 / 12.92);
47 constexpr T nonlinearFactor = static_cast<T>(1.0 / 1.055);
48 constexpr T exponent = static_cast<T>(2.4);
49 if (c <= cutoff)
50 return c * linearFactor;
51 else
52 return std::pow((c + static_cast<T>(0.055)) * nonlinearFactor, exponent);
53 }
54
55 template<typename T>
56 constexpr t_vec3<T> linear_to_sRGB(const t_vec3<T>& src)
57 {
58 return t_vec3<T>(linear_to_sRGB(src.r), linear_to_sRGB(src.g), linear_to_sRGB(src.b));
59 }
60
61 template<typename T>
62 constexpr t_vec4<T> linear_to_sRGB(const t_vec4<T>& src)
63 {
64 return t_vec4<T>(linear_to_sRGB(src.r), linear_to_sRGB(src.g), linear_to_sRGB(src.b), src.a);
65 }
66
67 template<typename T>
68 constexpr t_vec3<T> linear_to_sRGB(T r, T g, T b)
69 {
70 return t_vec3<T>(linear_to_sRGB(r), linear_to_sRGB(g), linear_to_sRGB(b));
71 }
72
73 template<typename T>
74 constexpr t_vec3<T> linear_to_sRGB(T r, T g, T b, T a)
75 {
76 return t_vec4<T>(linear_to_sRGB(r), linear_to_sRGB(g), linear_to_sRGB(b), a);
77 }
78
79 template<typename T>
80 constexpr t_vec3<T> sRGB_to_linear(const t_vec3<T>& src)
81 {
82 return t_vec3<T>(sRGB_to_linear(src.r), sRGB_to_linear(src.g), sRGB_to_linear(src.b));
83 }
84
85 template<typename T>
86 constexpr t_vec4<T> sRGB_to_linear(const t_vec4<T>& src)
87 {
88 return t_vec4<T>(sRGB_to_linear(src.r), sRGB_to_linear(src.g), sRGB_to_linear(src.b), src.a);
89 }
90
91 template<typename T>
92 constexpr t_vec3<T> sRGB_to_linear(T r, T g, T b)
93 {
94 return t_vec3<T>(sRGB_to_linear(r), sRGB_to_linear(g), sRGB_to_linear(b));
95 }
96
97 template<typename T>
98 constexpr t_vec4<T> sRGB_to_linear(T r, T g, T b, T a)
99 {
100 return t_vec4<T>(sRGB_to_linear(r), sRGB_to_linear(g), sRGB_to_linear(b), a);
101 }
102
103 template<typename T>
104 void convert(T& data, CoordinateSpace source, CoordinateSpace target)
105 {
106 if (source == CoordinateSpace::sRGB && target == CoordinateSpace::LINEAR)
107 data = sRGB_to_linear(data);
108 else if (source == CoordinateSpace::LINEAR && target == CoordinateSpace::sRGB)
109 data = linear_to_sRGB(data);
110 }
111
112 template<typename T>
113 void convert(size_t num, T* data, CoordinateSpace source, CoordinateSpace target)
114 {
115 if (source == CoordinateSpace::sRGB && target == CoordinateSpace::LINEAR)
116 for (size_t i = 0; i < num; ++i) data[i] = sRGB_to_linear(data[i]);
117 else if (source == CoordinateSpace::LINEAR && target == CoordinateSpace::sRGB)
118 for (size_t i = 0; i < num; ++i) data[i] = linear_to_sRGB(data[i]);
119 }
120
121 extern VSG_DECLSPEC VkFormat uNorm_to_sRGB(VkFormat format);
122 extern VSG_DECLSPEC VkFormat sRGB_to_uNorm(VkFormat format);
123
124} // namespace vsg
vsg::t_vec3
t_vec3 template class that represents a 3D vector
Definition vec3.h:34
vsg::t_vec4
t_vec4 template class that represents a 4D vector
Definition vec4.h:35