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 <UT/UT_Color.h>
16 #include "IMG_API.h"
17 #include "IMG_TextureFilter.h"
18 #include <limits>
19 
21 {
22 public:
23  static constexpr int AutoStride = 0;
24 
25  static const IMG_GammaTableDetail &get();
26 
27  SYS_FORCE_INLINE void
28  toFloat(float *result, const uint8 *src, int nchan, const float *lut) const
29  {
30  switch (nchan)
31  {
32  case 4:
33  result[3] = myLinear01[src[3]];
35  case 3:
36  result[2] = lut[src[2]];
38  case 2:
39  result[1] = lut[src[1]];
41  case 1:
42  result[0] = lut[src[0]];
43  break;
44  }
45  }
46  // Convert 8-bit to float value
47  float linear01(uint8 v) const { return myLinear01[v]; }
48 
49  SYS_FORCE_INLINE void
50  sRGBToFloat(float *result, const uint8 *src, int nchan) const
51  {
52  toFloat(result, src, nchan, myRGBtoLinear1);
53  }
54  SYS_FORCE_INLINE void
55  rec709ToFloat(float *result, const uint8 *src, int nchan) const
56  {
57  toFloat(result, src, nchan, myRec709toLinear1);
58  }
59  SYS_FORCE_INLINE void
60  gamma22ToFloat(float *result, const uint8 *src, int nchan) const
61  {
62  toFloat(result, src, nchan, myGamma22toLinear1);
63  }
64 
65 private:
67 
68  float myRGBtoLinear1[256]; // 0-1
69  float myRec709toLinear1[256]; // 0-1
70  float myGamma22toLinear1[256]; // 0-1
71  float myLinear01[256]; // 0-1
72 };
73 
74 namespace IMG_GammaTable
75 {
76  // IMG_GammaTable converts some well known color spaces to Linear Rec709
77  // (Linear sRGB).
78  static constexpr int AutoStride = IMG_GammaTableDetail::AutoStride;
79 
80  /// Simple conversion from integral types to float
81  template <typename T>
82  static SYS_FORCE_INLINE float toFloat(T data)
83  {
84  constexpr float scale = float(1.0 / std::numeric_limits<T>::max());
85  return float(data) * scale;
86  }
87  template <> float toFloat<uint8>(uint8 d)
88  {
90  }
91  template <> float toFloat<fpreal16>(fpreal16 d) { return d; }
92  template <> float toFloat<fpreal32>(fpreal32 d) { return d; }
93  template <> float toFloat<fpreal64>(fpreal64 d) { return d; }
94 
95  template <typename T> static SYS_FORCE_INLINE
96  void linearToFloat(float *result, const T *src, int nchan)
97  {
98  for (int i = 0; i < nchan; ++i)
99  result[i] = toFloat(src[i]);
100  }
101 
102  template <typename T> static SYS_FORCE_INLINE
103  void sRGBToFloat(float *dst, const T *src, int nchan)
104  {
105  switch (nchan)
106  {
107  case 4:
108  dst[3] = toFloat(src[3]);
110  case 3:
111  dst[2] = UT::srgbToLinear(toFloat(src[2]));
113  case 2:
114  dst[1] = UT::srgbToLinear(toFloat(src[1]));
116  case 1:
117  dst[0] = UT::srgbToLinear(toFloat(src[0]));
118  break;
119  }
120  }
121  template <typename T> static SYS_FORCE_INLINE
122  void rec709ToFloat(float *dst, const T *src, int nchan)
123  {
124  switch (nchan)
125  {
126  case 4:
127  dst[3] = toFloat(src[3]);
129  case 3:
130  dst[2] = UT::rec709ToLinear(toFloat(src[2]));
132  case 2:
133  dst[1] = UT::rec709ToLinear(toFloat(src[1]));
135  case 1:
136  dst[0] = UT::rec709ToLinear(toFloat(src[0]));
137  break;
138  }
139  }
140  template <typename T> static SYS_FORCE_INLINE
141  void gamma22ToFloat(float *dst, const T *src, int nchan)
142  {
143  switch (nchan)
144  {
145  case 4:
146  dst[3] = toFloat(src[3]);
148  case 3:
149  dst[2] = UT::gamma22ToLinear(toFloat(src[2]));
151  case 2:
152  dst[1] = UT::gamma22ToLinear(toFloat(src[1]));
154  case 1:
155  dst[0] = UT::gamma22ToLinear(toFloat(src[0]));
156  break;
157  }
158  }
159  // uint8 specializations
160  template <> SYS_FORCE_INLINE
161  void sRGBToFloat<uint8>(float *dst, const uint8 *src, int nchan)
162  {
163  IMG_GammaTableDetail::get().sRGBToFloat(dst, src, nchan);
164  }
165  template <> SYS_FORCE_INLINE
166  void rec709ToFloat<uint8>(float *dst, const uint8 *src, int nchan)
167  {
168  IMG_GammaTableDetail::get().rec709ToFloat(dst, src, nchan);
169  }
170  template <> SYS_FORCE_INLINE
171  void gamma22ToFloat<uint8>(float *dst, const uint8 *src, int nchan)
172  {
173  IMG_GammaTableDetail::get().gamma22ToFloat(dst, src, nchan);
174  }
175 
176  /// Convert from source data to linear float using built-in color manager
177  template <typename T> static SYS_FORCE_INLINE void
178  builtinConvert(float *result, const T *src, int n, IMG_ColorSpace cs)
179  {
180  switch (cs)
181  {
182  case IMG_COLORSPACE_AUTO: // non-uint8 defaults to linear
184  linearToFloat(result, src, n);
185  break;
186  case IMG_COLORSPACE_SRGB:
187  sRGBToFloat(result, src, n);
188  break;
190  gamma22ToFloat(result, src, n);
191  break;
193  rec709ToFloat(result, src, n);
194  break;
195  }
196  }
197  // Specialize for uint8
198  template <> void
200  {
201  switch (cs)
202  {
203  case IMG_COLORSPACE_AUTO: // uint8 defaults to sRGB
204  case IMG_COLORSPACE_SRGB:
205  IMG_GammaTableDetail::get().sRGBToFloat(result, src, n);
206  break;
208  linearToFloat(result, src, n);
209  break;
211  IMG_GammaTableDetail::get().gamma22ToFloat(result, src, n);
212  break;
214  IMG_GammaTableDetail::get().rec709ToFloat(result, src, n);
215  break;
216  }
217  }
218 
219  /// Convert from source data to linear float using the proper color manager
220  template <typename T> static SYS_FORCE_INLINE void
221  convert(float *result, const T *src, int n, IMG_ColorSpace cs)
222  {
223  switch (cs)
224  {
225  case IMG_COLORSPACE_AUTO: // non-uint8 defaults to linear
227  linearToFloat(result, src, n);
228  break;
229  case IMG_COLORSPACE_SRGB:
230  sRGBToFloat(result, src, n);
231  break;
233  gamma22ToFloat(result, src, n);
234  break;
236  rec709ToFloat(result, src, n);
237  break;
238  }
239  }
240 
241  // Specialize for uint8, using the proper color manager
242  template <> void
243  convert<uint8>(float *result, const uint8 *src, int n, IMG_ColorSpace cs)
244  {
245  switch (cs)
246  {
247  case IMG_COLORSPACE_AUTO: // uint8 defaults to sRGB
248  case IMG_COLORSPACE_SRGB:
249  IMG_GammaTableDetail::get().sRGBToFloat(result, src, n);
250  break;
252  linearToFloat(result, src, n);
253  break;
255  IMG_GammaTableDetail::get().gamma22ToFloat(result, src, n);
256  break;
258  IMG_GammaTableDetail::get().rec709ToFloat(result, src, n);
259  break;
260  }
261  }
262 }
263 
264 #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
**But if you need a result
Definition: thread.h:622
float fpreal32
Definition: SYS_Types.h:200
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
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:61
#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)
SYS_FORCE_INLINE void rec709ToFloat< uint8 >(float *dst, const uint8 *src, int nchan)
GLenum GLenum dst
Definition: glcorearb.h:1793
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
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
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)
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)
Definition: format.h:1821
GLenum src
Definition: glcorearb.h:1793