| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
// |
| 7 |
// Color conversion functions and general color algorithms |
| 8 |
// |
| 9 |
|
| 10 |
#ifndef INCLUDED_IMATHCOLORALGO_H |
| 11 |
#define INCLUDED_IMATHCOLORALGO_H |
| 12 |
|
| 13 |
#include "ImathNamespace.h" |
| 14 |
#include "ImathExport.h" |
| 15 |
|
| 16 |
#include "ImathColor.h" |
| 17 |
#include "ImathMath.h" |
| 18 |
|
| 19 |
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER |
| 20 |
|
| 21 |
// |
| 22 |
// Non-templated helper routines for color conversion. |
| 23 |
// These routines eliminate type warnings under g++. |
| 24 |
// |
| 25 |
|
| 26 |
/// |
| 27 |
/// Convert 3-channel hsv to rgb. Non-templated helper routine. |
| 28 |
IMATH_EXPORT Vec3<double> hsv2rgb_d (const Vec3<double>& hsv) IMATH_NOEXCEPT; |
| 29 |
|
| 30 |
/// |
| 31 |
/// Convert 4-channel hsv to rgb (with alpha). Non-templated helper routine. |
| 32 |
IMATH_EXPORT Color4<double> hsv2rgb_d (const Color4<double>& hsv) IMATH_NOEXCEPT; |
| 33 |
|
| 34 |
/// |
| 35 |
/// Convert 3-channel rgb to hsv. Non-templated helper routine. |
| 36 |
IMATH_EXPORT Vec3<double> rgb2hsv_d (const Vec3<double>& rgb) IMATH_NOEXCEPT; |
| 37 |
|
| 38 |
/// |
| 39 |
/// Convert 4-channel rgb to hsv. Non-templated helper routine. |
| 40 |
IMATH_EXPORT Color4<double> rgb2hsv_d (const Color4<double>& rgb) IMATH_NOEXCEPT; |
| 41 |
|
| 42 |
/// |
| 43 |
/// Convert 3-channel hsv to rgb. |
| 44 |
/// |
| 45 |
|
| 46 |
template <class T> |
| 47 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T> |
| 48 |
hsv2rgb (const Vec3<T>& hsv) IMATH_NOEXCEPT |
| 49 |
{ |
| 50 |
if (std::numeric_limits<T>::is_integer) |
| 51 |
{ |
| 52 |
Vec3<double> v = Vec3<double> (hsv.x / double (std::numeric_limits<T>::max()), |
| 53 |
hsv.y / double (std::numeric_limits<T>::max()), |
| 54 |
hsv.z / double (std::numeric_limits<T>::max())); |
| 55 |
Vec3<double> c = hsv2rgb_d (v); |
| 56 |
return Vec3<T> ((T) (c.x * std::numeric_limits<T>::max()), |
| 57 |
(T) (c.y * std::numeric_limits<T>::max()), |
| 58 |
(T) (c.z * std::numeric_limits<T>::max())); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
Vec3<double> v = Vec3<double> (hsv.x, hsv.y, hsv.z); |
| 63 |
Vec3<double> c = hsv2rgb_d (v); |
| 64 |
return Vec3<T> ((T) c.x, (T) c.y, (T) c.z); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/// |
| 69 |
/// Convert 4-channel hsv to rgb (with alpha). |
| 70 |
/// |
| 71 |
|
| 72 |
template <class T> |
| 73 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Color4<T> |
| 74 |
hsv2rgb (const Color4<T>& hsv) IMATH_NOEXCEPT |
| 75 |
{ |
| 76 |
if (std::numeric_limits<T>::is_integer) |
| 77 |
{ |
| 78 |
Color4<double> v = Color4<double> (hsv.r / float (std::numeric_limits<T>::max()), |
| 79 |
hsv.g / float (std::numeric_limits<T>::max()), |
| 80 |
hsv.b / float (std::numeric_limits<T>::max()), |
| 81 |
hsv.a / float (std::numeric_limits<T>::max())); |
| 82 |
Color4<double> c = hsv2rgb_d (v); |
| 83 |
return Color4<T> ((T) (c.r * std::numeric_limits<T>::max()), |
| 84 |
(T) (c.g * std::numeric_limits<T>::max()), |
| 85 |
(T) (c.b * std::numeric_limits<T>::max()), |
| 86 |
(T) (c.a * std::numeric_limits<T>::max())); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
Color4<double> v = Color4<double> (hsv.r, hsv.g, hsv.b, hsv.a); |
| 91 |
Color4<double> c = hsv2rgb_d (v); |
| 92 |
return Color4<T> ((T) c.r, (T) c.g, (T) c.b, (T) c.a); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/// |
| 97 |
/// Convert 3-channel rgb to hsv. |
| 98 |
/// |
| 99 |
|
| 100 |
template <class T> |
| 101 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T> |
| 102 |
rgb2hsv (const Vec3<T>& rgb) IMATH_NOEXCEPT |
| 103 |
{ |
| 104 |
if (std::numeric_limits<T>::is_integer) |
| 105 |
{ |
| 106 |
Vec3<double> v = Vec3<double> (rgb.x / double (std::numeric_limits<T>::max()), |
| 107 |
rgb.y / double (std::numeric_limits<T>::max()), |
| 108 |
rgb.z / double (std::numeric_limits<T>::max())); |
| 109 |
Vec3<double> c = rgb2hsv_d (v); |
| 110 |
return Vec3<T> ((T) (c.x * std::numeric_limits<T>::max()), |
| 111 |
(T) (c.y * std::numeric_limits<T>::max()), |
| 112 |
(T) (c.z * std::numeric_limits<T>::max())); |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
Vec3<double> v = Vec3<double> (rgb.x, rgb.y, rgb.z); |
| 117 |
Vec3<double> c = rgb2hsv_d (v); |
| 118 |
return Vec3<T> ((T) c.x, (T) c.y, (T) c.z); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/// |
| 123 |
/// Convert 4-channel rgb to hsv (with alpha). |
| 124 |
/// |
| 125 |
|
| 126 |
template <class T> |
| 127 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Color4<T> |
| 128 |
rgb2hsv (const Color4<T>& rgb) IMATH_NOEXCEPT |
| 129 |
{ |
| 130 |
if (std::numeric_limits<T>::is_integer) |
| 131 |
{ |
| 132 |
Color4<double> v = Color4<double> (rgb.r / float (std::numeric_limits<T>::max()), |
| 133 |
rgb.g / float (std::numeric_limits<T>::max()), |
| 134 |
rgb.b / float (std::numeric_limits<T>::max()), |
| 135 |
rgb.a / float (std::numeric_limits<T>::max())); |
| 136 |
Color4<double> c = rgb2hsv_d (v); |
| 137 |
return Color4<T> ((T) (c.r * std::numeric_limits<T>::max()), |
| 138 |
(T) (c.g * std::numeric_limits<T>::max()), |
| 139 |
(T) (c.b * std::numeric_limits<T>::max()), |
| 140 |
(T) (c.a * std::numeric_limits<T>::max())); |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
Color4<double> v = Color4<double> (rgb.r, rgb.g, rgb.b, rgb.a); |
| 145 |
Color4<double> c = rgb2hsv_d (v); |
| 146 |
return Color4<T> ((T) c.r, (T) c.g, (T) c.b, (T) c.a); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/// |
| 151 |
/// Convert 3-channel rgb to PackedColor |
| 152 |
/// |
| 153 |
|
| 154 |
template <class T> |
| 155 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor |
| 156 |
rgb2packed (const Vec3<T>& c) IMATH_NOEXCEPT |
| 157 |
{ |
| 158 |
if (std::numeric_limits<T>::is_integer) |
| 159 |
{ |
| 160 |
float x = c.x / float (std::numeric_limits<T>::max()); |
| 161 |
float y = c.y / float (std::numeric_limits<T>::max()); |
| 162 |
float z = c.z / float (std::numeric_limits<T>::max()); |
| 163 |
return rgb2packed (V3f (x, y, z)); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
// clang-format off |
| 168 |
return ( (PackedColor) (c.x * 255) | |
| 169 |
(((PackedColor) (c.y * 255)) << 8) | |
| 170 |
(((PackedColor) (c.z * 255)) << 16) | 0xFF000000 ); |
| 171 |
// clang-format on |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/// |
| 176 |
/// Convert 4-channel rgb to PackedColor (with alpha) |
| 177 |
/// |
| 178 |
|
| 179 |
template <class T> |
| 180 |
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor |
| 181 |
rgb2packed (const Color4<T>& c) IMATH_NOEXCEPT |
| 182 |
{ |
| 183 |
if (std::numeric_limits<T>::is_integer) |
| 184 |
{ |
| 185 |
float r = c.r / float (std::numeric_limits<T>::max()); |
| 186 |
float g = c.g / float (std::numeric_limits<T>::max()); |
| 187 |
float b = c.b / float (std::numeric_limits<T>::max()); |
| 188 |
float a = c.a / float (std::numeric_limits<T>::max()); |
| 189 |
return rgb2packed (C4f (r, g, b, a)); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
// clang-format off |
| 194 |
return ( (PackedColor) (c.r * 255) | |
| 195 |
(((PackedColor) (c.g * 255)) << 8) | |
| 196 |
(((PackedColor) (c.b * 255)) << 16) | |
| 197 |
(((PackedColor) (c.a * 255)) << 24)); |
| 198 |
// clang-format on |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/// |
| 203 |
/// Convert PackedColor to 3-channel rgb. Return the result in the |
| 204 |
/// `out` parameter. |
| 205 |
/// |
| 206 |
|
| 207 |
template <class T> |
| 208 |
IMATH_HOSTDEVICE void |
| 209 |
packed2rgb (PackedColor packed, Vec3<T>& out) IMATH_NOEXCEPT |
| 210 |
{ |
| 211 |
if (std::numeric_limits<T>::is_integer) |
| 212 |
{ |
| 213 |
T f = std::numeric_limits<T>::max() / ((PackedColor) 0xFF); |
| 214 |
out.x = (packed & 0xFF) * f; |
| 215 |
out.y = ((packed & 0xFF00) >> 8) * f; |
| 216 |
out.z = ((packed & 0xFF0000) >> 16) * f; |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
T f = T (1) / T (255); |
| 221 |
out.x = (packed & 0xFF) * f; |
| 222 |
out.y = ((packed & 0xFF00) >> 8) * f; |
| 223 |
out.z = ((packed & 0xFF0000) >> 16) * f; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/// |
| 228 |
/// Convert PackedColor to 4-channel rgb (with alpha). Return the |
| 229 |
/// result in the `out` parameter. |
| 230 |
/// |
| 231 |
|
| 232 |
template <class T> |
| 233 |
IMATH_HOSTDEVICE void |
| 234 |
packed2rgb (PackedColor packed, Color4<T>& out) IMATH_NOEXCEPT |
| 235 |
{ |
| 236 |
if (std::numeric_limits<T>::is_integer) |
| 237 |
{ |
| 238 |
T f = std::numeric_limits<T>::max() / ((PackedColor) 0xFF); |
| 239 |
out.r = (packed & 0xFF) * f; |
| 240 |
out.g = ((packed & 0xFF00) >> 8) * f; |
| 241 |
out.b = ((packed & 0xFF0000) >> 16) * f; |
| 242 |
out.a = ((packed & 0xFF000000) >> 24) * f; |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
T f = T (1) / T (255); |
| 247 |
out.r = (packed & 0xFF) * f; |
| 248 |
out.g = ((packed & 0xFF00) >> 8) * f; |
| 249 |
out.b = ((packed & 0xFF0000) >> 16) * f; |
| 250 |
out.a = ((packed & 0xFF000000) >> 24) * f; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
IMATH_INTERNAL_NAMESPACE_HEADER_EXIT |
| 255 |
|
| 256 |
#endif // INCLUDED_IMATHCOLORALGO_H |