HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathColorAlgo.h
Go to the documentation of this file.
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.
29 
30 ///
31 /// Convert 4-channel hsv to rgb (with alpha). Non-templated helper routine.
33 
34 ///
35 /// Convert 3-channel rgb to hsv. Non-templated helper routine.
37 
38 ///
39 /// Convert 4-channel rgb to hsv. Non-templated helper routine.
41 
42 ///
43 /// Convert 3-channel hsv to rgb.
44 ///
45 
46 template <class T>
47 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3<T>
49 {
51  {
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()),
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>
75 {
77  {
82  Color4<double> c = hsv2rgb_d (v);
83  return Color4<T> ((T) (c.r * 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>
103 {
105  {
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>
129 {
131  {
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
157 {
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
182 {
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
210 {
212  {
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
235 {
237  {
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
T z
Definition: ImathVec.h:310
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:72
Definition: ImathVec.h:32
const GLdouble * v
Definition: glcorearb.h:837
bool_constant< is_integral< T >::value &&!std::is_same< T, bool >::value &&!std::is_same< T, char >::value &&!std::is_same< T, wchar_t >::value > is_integer
Definition: format.h:2010
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLboolean GLboolean g
Definition: glcorearb.h:1222
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
unsigned int PackedColor
Packed 32-bit integer.
Definition: ImathColor.h:304
GLint y
Definition: glcorearb.h:103
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:102
IMATH_HOSTDEVICE void packed2rgb(PackedColor packed, Vec3< T > &out) IMATH_NOEXCEPT
GLfloat f
Definition: glcorearb.h:1926
T x
Definition: ImathVec.h:310
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_EXPORT Vec3< double > hsv2rgb_d(const Vec3< double > &hsv) IMATH_NOEXCEPT
Convert 3-channel hsv to rgb. Non-templated helper routine.
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR_14 uint8_t packed(VULKAN_HPP_NAMESPACE::Format format)
IMATH_EXPORT Vec3< double > rgb2hsv_d(const Vec3< double > &rgb) IMATH_NOEXCEPT
Convert 3-channel rgb to hsv. Non-templated helper routine.
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
#define IMATH_EXPORT
Definition: ImathExport.h:47
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3< T > rgb2hsv(const Vec3< T > &rgb) IMATH_NOEXCEPT
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 PackedColor rgb2packed(const Vec3< T > &c) IMATH_NOEXCEPT
T y
Definition: ImathVec.h:310
GLboolean r
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 Vec3< T > hsv2rgb(const Vec3< T > &hsv) IMATH_NOEXCEPT