HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IMG_GammaTable.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: IMG_GammaTable.h ( IMG Library, C++)
7  *
8  * COMMENTS: Gamma table computer
9  */
10 
11 #ifndef __IMG_GammaTable__
12 #define __IMG_GammaTable__
13 
14 #include <SYS/SYS_Types.h>
15 #include "IMG_API.h"
16 #include "IMG_TextureFilter.h"
17 #include <limits>
18 
20 {
21 public:
22  static constexpr int AutoStride = 0;
23 
24  static const IMG_GammaTableDetail &get();
25 
26  template <typename T> static constexpr inline T
28  {
29  constexpr T zero = 0.0031308;
30  constexpr T scale = 12.92;
31  constexpr T a = 0.055;
32  constexpr T gamma = 2.4;
33  return x <= zero ? x*scale : (1+a)*SYSpow(x, 1/gamma) - a;
34  }
35  template <typename T> static constexpr inline T
37  {
38  constexpr T zero = 0.018;
39  constexpr T scale = 4.5;
40  constexpr T a = 0.099;
41  constexpr T gamma = 0.45;
42  return x <= zero ? x*scale : (1+a)*SYSpow(x, gamma) - a;
43  }
44  template <typename T> static constexpr inline T
45  linearToGamma(T x, T gamma)
46  {
47  return SYSsafepow(x, 1/gamma);
48  }
49  template <typename T> static constexpr inline T
51  {
52  constexpr T gamma = 2.2;
53  return linearToGamma(x, gamma);
54  }
55 
56  template <typename T> static constexpr inline T
58  {
59  constexpr T zero = 0.04045;
60  constexpr T scale = 12.92;
61  constexpr T a = 0.055;
62  constexpr T gamma = 2.4;
63  return x <= zero ? x*(1/scale) : SYSpow((x+a)*(1/(1+a)), gamma);
64  }
65  template <typename T> static constexpr inline T
67  {
68  constexpr T zero = 0.081;
69  constexpr T scale = 4.5;
70  constexpr T a = 0.099;
71  constexpr T gamma = 0.45;
72  return x <= zero ? x*(1/scale) : SYSpow((x+a)*(1/(1+a)), (1/gamma));
73  }
74  template <typename T> static constexpr inline T
75  gammaToLinear(T x, T gamma)
76  {
77  return SYSsafepow(x, gamma);
78  }
79  template <typename T> static constexpr inline T
81  {
82  constexpr T gamma = 2.2;
83  return gammaToLinear(x, gamma);
84  }
85 
86  SYS_FORCE_INLINE void
87  toFloat(float *result, const uint8 *src, int nchan, const float *lut) const
88  {
89  switch (nchan)
90  {
91  case 4:
92  result[3] = myLinear01[src[3]];
94  case 3:
95  result[2] = lut[src[2]];
97  case 2:
98  result[1] = lut[src[1]];
100  case 1:
101  result[0] = lut[src[0]];
102  break;
103  }
104  }
105  // Convert 8-bit to float value
106  float linear01(uint8 v) const { return myLinear01[v]; }
107 
108  SYS_FORCE_INLINE void
109  sRGBToFloat(float *result, const uint8 *src, int nchan) const
110  {
111  toFloat(result, src, nchan, myRGBtoLinear1);
112  }
113  SYS_FORCE_INLINE void
114  rec709ToFloat(float *result, const uint8 *src, int nchan) const
115  {
116  toFloat(result, src, nchan, myRec709toLinear1);
117  }
118  SYS_FORCE_INLINE void
119  gamma22ToFloat(float *result, const uint8 *src, int nchan) const
120  {
121  toFloat(result, src, nchan, myGamma22toLinear1);
122  }
123 
124 private:
126 
127  float myRGBtoLinear1[256]; // 0-1
128  float myRec709toLinear1[256]; // 0-1
129  float myGamma22toLinear1[256]; // 0-1
130  float myLinear01[256]; // 0-1
131 };
132 
133 namespace IMG_GammaTable
134 {
135  // IMG_GammaTable converts some well known color spaces to Linear Rec709
136  // (Linear sRGB).
137  static constexpr int AutoStride = IMG_GammaTableDetail::AutoStride;
138 
139  /// Simple conversion from integral types to float
140  template <typename T>
141  static SYS_FORCE_INLINE float toFloat(T data)
142  {
143  constexpr float scale = float(1.0 / std::numeric_limits<T>::max());
144  return float(data) * scale;
145  }
146  template <> float toFloat<uint8>(uint8 d)
147  {
149  }
150  template <> float toFloat<fpreal16>(fpreal16 d) { return d; }
151  template <> float toFloat<fpreal32>(fpreal32 d) { return d; }
152  template <> float toFloat<fpreal64>(fpreal64 d) { return d; }
153 
154  template <typename T> static SYS_FORCE_INLINE
155  void linearToFloat(float *result, const T *src, int nchan)
156  {
157  for (int i = 0; i < nchan; ++i)
158  result[i] = toFloat(src[i]);
159  }
160 
161  template <typename T> static SYS_FORCE_INLINE
162  void sRGBToFloat(float *dst, const T *src, int nchan)
163  {
164  switch (nchan)
165  {
166  case 4:
167  dst[3] = toFloat(src[3]);
169  case 3:
170  dst[2] = IMG_GammaTableDetail::srgbToLinear(toFloat(src[2]));
172  case 2:
173  dst[1] = IMG_GammaTableDetail::srgbToLinear(toFloat(src[1]));
175  case 1:
176  dst[0] = IMG_GammaTableDetail::srgbToLinear(toFloat(src[0]));
177  break;
178  }
179  }
180  template <typename T> static SYS_FORCE_INLINE
181  void rec709ToFloat(float *dst, const T *src, int nchan)
182  {
183  switch (nchan)
184  {
185  case 4:
186  dst[3] = toFloat(src[3]);
188  case 3:
189  dst[2] = IMG_GammaTableDetail::rec709ToLinear(toFloat(src[2]));
191  case 2:
192  dst[1] = IMG_GammaTableDetail::rec709ToLinear(toFloat(src[1]));
194  case 1:
195  dst[0] = IMG_GammaTableDetail::rec709ToLinear(toFloat(src[0]));
196  break;
197  }
198  }
199  template <typename T> static SYS_FORCE_INLINE
200  void gamma22ToFloat(float *dst, const T *src, int nchan)
201  {
202  switch (nchan)
203  {
204  case 4:
205  dst[3] = toFloat(src[3]);
207  case 3:
208  dst[2] = IMG_GammaTableDetail::gamma22ToLinear(toFloat(src[2]));
210  case 2:
211  dst[1] = IMG_GammaTableDetail::gamma22ToLinear(toFloat(src[1]));
213  case 1:
214  dst[0] = IMG_GammaTableDetail::gamma22ToLinear(toFloat(src[0]));
215  break;
216  }
217  }
218  // uint8 specializations
219  template <> SYS_FORCE_INLINE
220  void sRGBToFloat<uint8>(float *dst, const uint8 *src, int nchan)
221  {
222  IMG_GammaTableDetail::get().sRGBToFloat(dst, src, nchan);
223  }
224  template <> SYS_FORCE_INLINE
225  void rec709ToFloat<uint8>(float *dst, const uint8 *src, int nchan)
226  {
227  IMG_GammaTableDetail::get().rec709ToFloat(dst, src, nchan);
228  }
229  template <> SYS_FORCE_INLINE
230  void gamma22ToFloat<uint8>(float *dst, const uint8 *src, int nchan)
231  {
232  IMG_GammaTableDetail::get().gamma22ToFloat(dst, src, nchan);
233  }
234 
235  /// Convert from source data to linear float using built-in color manager
236  template <typename T> static SYS_FORCE_INLINE void
237  builtinConvert(float *result, const T *src, int n, IMG_ColorSpace cs)
238  {
239  switch (cs)
240  {
241  case IMG_COLORSPACE_AUTO: // non-uint8 defaults to linear
243  linearToFloat(result, src, n);
244  break;
245  case IMG_COLORSPACE_SRGB:
246  sRGBToFloat(result, src, n);
247  break;
249  gamma22ToFloat(result, src, n);
250  break;
252  rec709ToFloat(result, src, n);
253  break;
254  }
255  }
256  // Specialize for uint8
257  template <> void
259  {
260  switch (cs)
261  {
262  case IMG_COLORSPACE_AUTO: // uint8 defaults to sRGB
263  case IMG_COLORSPACE_SRGB:
264  IMG_GammaTableDetail::get().sRGBToFloat(result, src, n);
265  break;
267  linearToFloat(result, src, n);
268  break;
270  IMG_GammaTableDetail::get().gamma22ToFloat(result, src, n);
271  break;
273  IMG_GammaTableDetail::get().rec709ToFloat(result, src, n);
274  break;
275  }
276  }
277 
278  /// Convert from source data to linear float using the proper color manager
279  template <typename T> static SYS_FORCE_INLINE void
280  convert(float *result, const T *src, int n, IMG_ColorSpace cs)
281  {
282  switch (cs)
283  {
284  case IMG_COLORSPACE_AUTO: // non-uint8 defaults to linear
286  linearToFloat(result, src, n);
287  break;
288  case IMG_COLORSPACE_SRGB:
289  sRGBToFloat(result, src, n);
290  break;
292  gamma22ToFloat(result, src, n);
293  break;
295  rec709ToFloat(result, src, n);
296  break;
297  }
298  }
299 
300  // Specialize for uint8, using the proper color manager
301  template <> void
302  convert<uint8>(float *result, const uint8 *src, int n, IMG_ColorSpace cs)
303  {
304  switch (cs)
305  {
306  case IMG_COLORSPACE_AUTO: // uint8 defaults to sRGB
307  case IMG_COLORSPACE_SRGB:
308  IMG_GammaTableDetail::get().sRGBToFloat(result, src, n);
309  break;
311  linearToFloat(result, src, n);
312  break;
314  IMG_GammaTableDetail::get().gamma22ToFloat(result, src, n);
315  break;
317  IMG_GammaTableDetail::get().rec709ToFloat(result, src, n);
318  break;
319  }
320  }
321 }
322 
323 #endif
static const IMG_GammaTableDetail & get()
float toFloat< fpreal16 >(fpreal16 d)
SYS_FORCE_INLINE void gamma22ToFloat< uint8 >(float *dst, const uint8 *src, int nchan)
float toFloat< uint8 >(uint8 d)
float linear01(uint8 v) const
const GLdouble * v
Definition: glcorearb.h:837
static constexpr int AutoStride
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
**But if you need a result
Definition: thread.h:613
float fpreal32
Definition: SYS_Types.h:200
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
static constexpr T linearToGamma(T x, T gamma)
SYS_FORCE_INLINE void toFloat(float *result, const uint8 *src, int nchan, const float *lut) const
#define IMG_API
Definition: IMG_API.h:10
GA_API const UT_StringHolder scale
void convert< uint8 >(float *result, const uint8 *src, int n, IMG_ColorSpace cs)
GLdouble n
Definition: glcorearb.h:2008
#define SYS_FALLTHROUGH
Definition: SYS_Compiler.h:68
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE void rec709ToFloat(float *result, const uint8 *src, int nchan) const
float toFloat< fpreal64 >(fpreal64 d)
GLint GLenum GLint x
Definition: glcorearb.h:409
static constexpr T linearToSRGB(T x)
SYS_FORCE_INLINE void rec709ToFloat< uint8 >(float *dst, const uint8 *src, int nchan)
static constexpr T gammaToLinear(T x, T gamma)
GLenum GLenum dst
Definition: glcorearb.h:1793
SYS_FORCE_INLINE void gamma22ToFloat(float *result, const uint8 *src, int nchan) const
const stride_t AutoStride
Definition: imageio.h:56
SYS_FORCE_INLINE void sRGBToFloat(float *result, const uint8 *src, int nchan) const
static constexpr T linearToRec709(T x)
float toFloat< fpreal32 >(fpreal32 d)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
SYS_FORCE_INLINE void sRGBToFloat< uint8 >(float *dst, const uint8 *src, int nchan)
static constexpr T rec709ToLinear(T x)
IMG_ColorSpace
Modes for how to translate colors in images for texture lookups.
void builtinConvert< uint8 >(float *result, const uint8 *src, int n, IMG_ColorSpace cs)
static constexpr T gamma22ToLinear(T x)
ImageBuf OIIO_API zero(ROI roi, int nthreads=0)
static constexpr T srgbToLinear(T x)
static constexpr T linearToGamma22(T x)
Definition: format.h:895
GLenum src
Definition: glcorearb.h:1793