HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PXL_Lookup.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: PXL_Lookup.h ( PXL Library, C++)
7  *
8  * COMMENTS:
9  * Simple lookup table class.
10  */
11 #ifndef PXL_LOOKUP_H
12 #define PXL_LOOKUP_H
13 
14 #include "PXL_API.h"
15 #include "PXL_Common.h"
16 #include "PXL_OCIO.h"
17 #include <UT/UT_UniquePtr.h>
18 #include <SYS/SYS_Types.h>
19 #include <stdio.h>
20 
21 class PXL_Raster;
22 class UT_FileLUT;
23 
25 {
26  PXL_LOOKUP_R = 0x001, // 1 table for Red only.
27  PXL_LOOKUP_G = 0x002,
28  PXL_LOOKUP_B = 0x004,
29  PXL_LOOKUP_A = 0x008, // 1 table for Alpha only.
30 
31  PXL_LOOKUP_RGB = 0x017, // 3 tables, 1 each for Red, Green & Blue.
32  PXL_LOOKUP_RGBA = 0x01F, // 4 tables, 1 each for R, G, B and alpha.
33 
34  PXL_LOOKUP_COLOR = 0x027, // 1 table for RGB.
35  PXL_LOOKUP_ALL = 0x02F, // 1 table for all channels.
36 
37  PXL_LOOKUP_3D = 0x040, // 3D LUT for color only.
38  PXL_LOOKUP_3D_1D = 0x060, // 3D LUT with 1D pre-pass, color only.
39  PXL_LOOKUP_3D_HDR = 0x140 // 3D LUT with log sampling
40 };
41 
42 // Masks for PXL_LookupType
43 #define PXL_LOOKUP_1D 0x3F
44 #define PXL_SEPARATE_CHANNELS 0x10
45 #define PXL_SINGLE_CHANNEL 0x20
46 #define PXL_ILLEGAL_MATCH 0x30
47 #define PXL_LOG_SAMPLE 0x100
48 
49 
50 class CL_Clip;
51 
52 
54 {
55 public:
56  PXL_Lookup();
57  ~PXL_Lookup();
58 
59  static struct BuildGammaTag {} BuildGamma;
60  static struct BuildSRGBTag {} BuildSRGB;
61  static struct BuildRec709Tag {} BuildRec709;
62  static struct BuildOCIOTag {} BuildOCIO;
63 
64  enum Dir
65  {
67  GAMMA_TO_LINEAR
68  };
69 
70  PXL_Lookup(BuildGammaTag,
71  fpreal gamma,
72  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
73  int lutsize = 1024);
74 
75  PXL_Lookup(BuildSRGBTag,
76  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
77  int lutsize = 1024);
78 
79  PXL_Lookup(BuildRec709Tag,
80  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
81  int lutsize = 1024);
82 
83  PXL_Lookup(BuildOCIOTag,
84  const char *src_color_space,
85  const char *dest_color_space,
86  int quality = 0);
87 
88  PXL_Lookup(const PXL_Lookup &) = delete;
89  PXL_Lookup & operator=(const PXL_Lookup &) = delete;
90 
91  int64 getMemoryUsage(bool inclusive) const;
92 
93  void reset();
94 
95  // common LUT applications
96 
97  // single color modification
98  void evalLUT(float color[3]) const;
99 
100  // set a 1D LUT.
101  void setLUT(const CL_Clip &clip);
102 
103  // set the 3D LUT.
104  void set3DLUT(int size, float *lut);
105 
106  // applies the LUT to a raster (along with gamma). black/white are the
107  // BW points of the image in the raster. 'comp' is used to convert the
108  // raster to show only 1 component (ie, r->rgb).
109  void applyLUT(PXL_Raster &r, float gamma = 1.0f,
110  float black=0.0f, float white=1.0f,
111  int comp = -1) const;
112 
113  // returns the value for sampling at integer LUT index 'i'. If the
114  // value is passed to evalLUT(), it will return the sample at table index i.
115  // For 3D LUTs, this needs to be called once for each axis.
116  fpreal indexToValue(int i) const;
117 
118  // --------------------------------------------------------------------
119  // accessers
120 
121  bool isValid() const;
122 
123  // 1D accessers
124  PXL_LookupType getType() const;
125  bool getLogSampling() const;
126  float getLogBias() const;
127  int getNumChannels() const;
128  int getTableSize() const;
129  float getBlackPoint() const;
130  float getWhitePoint() const;
131  void getDomain(float &s, float &e) const;
132  void getRange(float &s, float &e) const;
133  float getPreShift() const { return myPreShift; }
134 
135  bool hasSpecificFormat(PXL_DataFormat &format) const;
136  bool hasLUTfor(PXL_LookupType type) const;
137 
138  void setType(PXL_LookupType type);
139  void setLogSampling(bool log);
140  void setLogBias(float bias);
141  void setSpecificFormat(PXL_DataFormat format);
142  void setNumChannels(int num);
143  void setTableSize(int size);
144  void setBlackWhitePoints(float b, float w);
145  void setDomain(float s,float e);
146  void setRange(float s, float e);
147  void invert(int newsize = 0);
148 
149  bool isHalfPrec() const { return myHalfPrecFlag; }
150 
151  // 3D accessers
152  bool is3DLUT() const { return myCubeSize != 0; }
153  int get3DSize() const { return myCubeSize; }
154 
155  // --------------------------------------------------------------------
156  // 1D LUT evaluation routines
157  fpreal *getRawLUT(PXL_LookupType type);
158  const fpreal *getRawLUT(PXL_LookupType type) const;
159 
160  bool evalLUT(PXL_LookupType,
161  int nsamples,
162  float *data,
163  unsigned b = 0,
164  unsigned w = 0) const;
165 
166  bool evalLUT(PXL_LookupType,
167  int nsamples,
168  float *data,
169  float black, float white,
170  float from = 0.0f, float to = 1.0f) const;
171  float evalLUT(PXL_LookupType, float pos) const;
172 
173  void eval1DLUT(float color[3]) const;
174  void get1DLUT(float *lut[4], int size, float b, float w,
175  float sc, float sh, float gamma) const;
176 
177  void apply1DLUT(PXL_Raster &raster,
178  float gamma = 1.0f,
179  float black = 0.0f,
180  float white = 1.0f,
181  int comp = -1) const;
182  // ---------------------------------------------------------------------
183  // 3D LUT evaluation routines
184  void eval3DLUT(float color[3]) const;
185 
186  // makes a copy of the 3D LUT. 'lut' must be of size get3DSize()^3 * 3 if
187  // type is a 3D linear or log LUT. If 3D with prelut is used, prelut must
188  // non-NULL and of size get3DSize()^2.
189  void copy3DLUT(float *lut, float *prelut = 0) const;
190  void copy3DLUT(fpreal16 *lut, fpreal16 *prelut = 0) const;
191 
192  // clamps the 3D LUT to 0,1. Used for display LUTs.
193  void clamp3DLUT();
194 
195  void apply3DLUT(PXL_Raster &r, float gamma = 1.0f,
196  float black = 0.0f, float white = 1.0f,
197  int component = -1) const;
198 
199 
200  // File I/O.
201  bool load(const char *filename, bool header_only = false);
202  bool save(const char *filename, bool half_prec=false) const;
203 
204  // these load and save methods do not close fp.
205  bool load(FILE *fp, bool header_only);
206  bool loadBinary(FILE *fp, bool header_only);
207  bool save(FILE *fp) const;
208  bool saveBinary(FILE *fp, bool half_prec = false) const;
209 
210 
211  bool buildGamma(fpreal gamma,
212  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
213  int lutsize = 1024);
214 
215  bool buildSRGB(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
216  int lutsize = 1024);
217 
218  bool buildREC709(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
219  int lutsize = 1024);
220 
221  /// Transform from an OpenColorIO colorspace to a View, specified by a
222  /// display/view pair.
223  /// Always use this method to build a LUT to or from a view; do not attempt
224  /// to roll your own using colorspaces and view transforms as it is not
225  /// straighforward.
226  bool buildOpenColorIOView(const char *src_color_space,
227  const char *display_name,
228  const char *view_name,
229  const char *baked_display= nullptr,
230  const char *baked_view = nullptr,
231  bool inverse = false,
232  int quality = 0);
233 
234  /// Transform from one colorspace to another.
235  /// @c quality can be 0 or 1 right now.
236  bool buildOpenColorIO(const char *src_color_space,
237  const char *dest_color_space,
238  const char *looks = nullptr,
239  int quality = 0);
240 
241  // creates a cineon LUT for our Cineon lib. This allocates a new LUT,
242  // which should be freed with delete. Filename is only for comparing
243  // UT_FileLUTs, it is optional.
245  getCineonLUT(bool forward,
246  const char *filename = 0) const;
247 
248  // Merge 2 LUTs into one LUT (this LUT). This has the same effect as running
249  // a pixel through 'first_lut', then 'second_lut', with some possible
250  // precision loss if one is a large 1D LUT and the other is a 3D LUT.
251  bool mergeLUTs(const PXL_Lookup &first_lut,
252  const PXL_Lookup &second_lut);
253 
254  void setTextureID(void *id) { myTextureID = id; }
255  void *getTextureID() const;
256  void clearTextureID();
257 
258  void setPreLutID(void *id) { myPreLutID = id; }
259  void *getPreLutID() const;
260 
261 private:
262  int getIndexFor(PXL_LookupType type) const;
263 
264  bool correctLUT();
265 
266  inline void lerpPreLUT(float &color, float prelut_scale,
267  const fpreal *prelut, int prelut_last) const;
268 
269  void save1D(FILE *fp, bool bin, bool half = false) const;
270  void save3D(FILE *fp, bool bin, bool half = false) const;
271 
272  bool fetchAllocationVars(const PXL_OCIO::ColorSpace *scs,
273  const PXL_OCIO::ColorSpace *dcs,
274  bool &uniform,
275  fpreal &minv,
276  fpreal &maxv,
277  fpreal &offset);
278  void prep1DLUT(float *data, int size,
279  bool uniform, fpreal minv, fpreal maxv);
280  void prep3DLUT(bool uniform, fpreal minv, fpreal maxv,
281  fpreal offset, int quality);
282 
283  UT_UniquePtr<CL_Clip> myTable;
284  PXL_LookupType myType;
285  PXL_DataFormat myFormat;
286 
287  // 3D LUT
288  UT_UniquePtr<float[]> myCubeLUT;
289  int myCubeSize;
290 
291  float myBlack;
292  float myWhite;
293  float myDomStart;
294  float myDomEnd;
295  float myRangeStart;
296  float myRangeEnd;
297  float myLogBias;
298  float myPreShift;
299  void *myTextureID;
300  void *myPreLutID;
301  bool myHalfPrecFlag;
302 };
303 
304 #endif
GT_API const UT_StringHolder filename
int get3DSize() const
Definition: PXL_Lookup.h:153
imath_half_bits_t half
if we're in a C-only context, alias the half bits type to half
Definition: half.h:266
GLboolean invert
Definition: glcorearb.h:549
float getPreShift() const
Definition: PXL_Lookup.h:133
GLdouble s
Definition: glad.h:3009
void setPreLutID(void *id)
Definition: PXL_Lookup.h:258
bool is3DLUT() const
Definition: PXL_Lookup.h:152
bool isHalfPrec() const
Definition: PXL_Lookup.h:149
#define PXL_API
Definition: PXL_API.h:10
PXL_LookupType
Definition: PXL_Lookup.h:24
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLfloat f
Definition: glcorearb.h:1926
GLintptr offset
Definition: glcorearb.h:665
GLboolean reset
Definition: glad.h:5138
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
HUSD_API const char * raster()
long long int64
Definition: SYS_Types.h:116
GLuint id
Definition: glcorearb.h:655
PXL_DataFormat
Definition: PXL_Common.h:20
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
void setTextureID(void *id)
Definition: PXL_Lookup.h:254
GLsizeiptr size
Definition: glcorearb.h:664
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:277
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
class OCIOEXPORT ColorSpace
GLboolean r
Definition: glcorearb.h:1222
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7688
type
Definition: core.h:1059
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE IMATH_CONSTEXPR14 T clip(const T &p, const Box< T > &box) IMATH_NOEXCEPT
Definition: ImathBoxAlgo.h:29
Definition: format.h:895