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 
87  PXL_Lookup(const PXL_Lookup &) = delete;
88  PXL_Lookup & operator=(const PXL_Lookup &) = delete;
89 
90  int64 getMemoryUsage(bool inclusive) const;
91 
92  void reset();
93 
94  // common LUT applications
95 
96  // single color modification
97  void evalLUT(float color[3]) const;
98 
99  // set a 1D LUT.
100  void setLUT(const CL_Clip &clip);
101 
102  // set the 3D LUT.
103  void set3DLUT(int size, float *lut);
104 
105  // applies the LUT to a raster (along with gamma). black/white are the
106  // BW points of the image in the raster. 'comp' is used to convert the
107  // raster to show only 1 component (ie, r->rgb).
108  void applyLUT(PXL_Raster &r, float gamma = 1.0f,
109  float black=0.0f, float white=1.0f,
110  int comp = -1) const;
111 
112  // returns the value for sampling at integer LUT index 'i'. If the
113  // value is passed to evalLUT(), it will return the sample at table index i.
114  // For 3D LUTs, this needs to be called once for each axis.
115  fpreal indexToValue(int i) const;
116 
117  // --------------------------------------------------------------------
118  // accessers
119 
120  bool isValid() const;
121 
122  // 1D accessers
123  PXL_LookupType getType() const;
124  bool getLogSampling() const;
125  float getLogBias() const;
126  int getNumChannels() const;
127  int getTableSize() const;
128  float getBlackPoint() const;
129  float getWhitePoint() const;
130  void getDomain(float &s, float &e) const;
131  void getRange(float &s, float &e) const;
132  float getPreShift() const { return myPreShift; }
133 
134  bool hasSpecificFormat(PXL_DataFormat &format) const;
135  bool hasLUTfor(PXL_LookupType type) const;
136 
137  void setType(PXL_LookupType type);
138  void setLogSampling(bool log);
139  void setLogBias(float bias);
140  void setSpecificFormat(PXL_DataFormat format);
141  void setNumChannels(int num);
142  void setTableSize(int size);
143  void setBlackWhitePoints(float b, float w);
144  void setDomain(float s,float e);
145  void setRange(float s, float e);
146  void invert(int newsize = 0);
147 
148  bool isHalfPrec() const { return myHalfPrecFlag; }
149 
150  // 3D accessers
151  bool is3DLUT() const { return myCubeSize != 0; }
152  int get3DSize() const { return myCubeSize; }
153 
154  // --------------------------------------------------------------------
155  // 1D LUT evaluation routines
156  fpreal *getRawLUT(PXL_LookupType type);
157  const fpreal *getRawLUT(PXL_LookupType type) const;
158 
159  bool evalLUT(PXL_LookupType,
160  int nsamples,
161  float *data,
162  unsigned b = 0,
163  unsigned w = 0) const;
164 
165  bool evalLUT(PXL_LookupType,
166  int nsamples,
167  float *data,
168  float black, float white,
169  float from = 0.0f, float to = 1.0f) const;
170  float evalLUT(PXL_LookupType, float pos) const;
171 
172  void eval1DLUT(float color[3]) const;
173  void get1DLUT(float *lut[4], int size, float b, float w,
174  float sc, float sh, float gamma) const;
175 
176  void apply1DLUT(PXL_Raster &raster,
177  float gamma = 1.0f,
178  float black = 0.0f,
179  float white = 1.0f,
180  int comp = -1) const;
181  // ---------------------------------------------------------------------
182  // 3D LUT evaluation routines
183  void eval3DLUT(float color[3]) const;
184 
185  // makes a copy of the 3D LUT. 'lut' must be of size get3DSize()^3 * 3 if
186  // type is a 3D linear or log LUT. If 3D with prelut is used, prelut must
187  // non-NULL and of size get3DSize()^2.
188  void copy3DLUT(float *lut, float *prelut = 0) const;
189  void copy3DLUT(fpreal16 *lut, fpreal16 *prelut = 0) const;
190 
191  // clamps the 3D LUT to 0,1. Used for display LUTs.
192  void clamp3DLUT();
193 
194  void apply3DLUT(PXL_Raster &r, float gamma = 1.0f,
195  float black = 0.0f, float white = 1.0f,
196  int component = -1) const;
197 
198 
199  // File I/O.
200  bool load(const char *filename, bool header_only = false);
201  bool save(const char *filename, bool half_prec=false) const;
202 
203  // these load and save methods do not close fp.
204  bool load(FILE *fp, bool header_only);
205  bool loadBinary(FILE *fp, bool header_only);
206  bool save(FILE *fp) const;
207  bool saveBinary(FILE *fp, bool half_prec = false) const;
208 
209 
210  bool buildGamma(fpreal gamma,
211  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
212  int lutsize = 1024);
213 
214  bool buildSRGB(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
215  int lutsize = 1024);
216 
217  bool buildREC709(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
218  int lutsize = 1024);
219 
220  /// Transform from an OpenColorIO colorspace to a View, specified by a
221  /// display/view pair.
222  /// Always use this method to build a LUT to or from a view; do not attempt
223  /// to roll your own using colorspaces and view transforms as it is not
224  /// straighforward.
225  bool buildOpenColorIOView(const char *src_color_space,
226  const char *display_name,
227  const char *view_name,
228  const char *baked_display= nullptr,
229  const char *baked_view = nullptr,
230  bool inverse = false);
231 
232  /// Transform from one colorspace to another.
233  bool buildOpenColorIO(const char *src_color_space,
234  const char *dest_color_space);
235 
236  // creates a cineon LUT for our Cineon lib. This allocates a new LUT,
237  // which should be freed with delete. Filename is only for comparing
238  // UT_FileLUTs, it is optional.
240  getCineonLUT(bool forward,
241  const char *filename = 0) const;
242 
243  // Merge 2 LUTs into one LUT (this LUT). This has the same effect as running
244  // a pixel through 'first_lut', then 'second_lut', with some possible
245  // precision loss if one is a large 1D LUT and the other is a 3D LUT.
246  bool mergeLUTs(const PXL_Lookup &first_lut,
247  const PXL_Lookup &second_lut);
248 
249  void setTextureID(void *id) { myTextureID = id; }
250  void *getTextureID() const;
251  void clearTextureID();
252 
253  void setPreLutID(void *id) { myPreLutID = id; }
254  void *getPreLutID() const;
255 
256 private:
257  int getIndexFor(PXL_LookupType type) const;
258 
259  bool correctLUT();
260 
261  inline void lerpPreLUT(float &color, float prelut_scale,
262  const fpreal *prelut, int prelut_last) const;
263 
264  void save1D(FILE *fp, bool bin, bool half = false) const;
265  void save3D(FILE *fp, bool bin, bool half = false) const;
266 
267  bool fetchAllocationVars(const PXL_OCIO::ColorSpace *scs,
268  const PXL_OCIO::ColorSpace *dcs,
269  bool &uniform,
270  fpreal &minv,
271  fpreal &maxv,
272  fpreal &offset);
273  void prep1DLUT(float *data, int size,
274  bool uniform, fpreal minv, fpreal maxv);
275  void prep3DLUT(bool uniform, fpreal minv, fpreal maxv,
276  fpreal offset);
277 
278  UT_UniquePtr<CL_Clip> myTable;
279  PXL_LookupType myType;
280  PXL_DataFormat myFormat;
281 
282  // 3D LUT
283  UT_UniquePtr<float[]> myCubeLUT;
284  int myCubeSize;
285 
286  float myBlack;
287  float myWhite;
288  float myDomStart;
289  float myDomEnd;
290  float myRangeStart;
291  float myRangeEnd;
292  float myLogBias;
293  float myPreShift;
294  void *myTextureID;
295  void *myPreLutID;
296  bool myHalfPrecFlag;
297 };
298 
299 #endif
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GT_API const UT_StringHolder filename
int get3DSize() const
Definition: PXL_Lookup.h:152
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
GLuint color
Definition: glcorearb.h:1261
float getPreShift() const
Definition: PXL_Lookup.h:132
void setPreLutID(void *id)
Definition: PXL_Lookup.h:253
bool is3DLUT() const
Definition: PXL_Lookup.h:151
bool isHalfPrec() const
Definition: PXL_Lookup.h:148
#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
GLsizeiptr size
Definition: glcorearb.h:664
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
GLfloat bias
Definition: glew.h:10316
GLuint num
Definition: glew.h:2695
HUSD_API const char * raster()
long long int64
Definition: SYS_Types.h:116
GLuint id
Definition: glcorearb.h:655
GLboolean reset
Definition: glew.h:4989
PXL_DataFormat
Definition: PXL_Common.h:20
void setTextureID(void *id)
Definition: PXL_Lookup.h:249
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
fpreal64 fpreal
Definition: SYS_Types.h:277
GLfloat f
Definition: glcorearb.h:1926
GLintptr offset
Definition: glcorearb.h:665
class OCIOEXPORT ColorSpace
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7688
type
Definition: core.h:1059
GLboolean r
Definition: glcorearb.h:1222
GLdouble s
Definition: glew.h:1395
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