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. If pad4 is true, insert a
189  // 4th component.
190  void copy3DLUT(float *lut,
191  float *prelut = nullptr,
192  bool pad4 = false) const;
193  void copy3DLUT(fpreal16 *lut,
194  fpreal16 *prelut = nullptr,
195  bool pad4 = false) const;
196 
197  // clamps the 3D LUT to 0,1. Used for display LUTs.
198  void clamp3DLUT();
199 
200  void apply3DLUT(PXL_Raster &r, float gamma = 1.0f,
201  float black = 0.0f, float white = 1.0f,
202  int component = -1) const;
203 
204 
205  // File I/O.
206  bool load(const char *filename, bool header_only = false);
207  bool save(const char *filename, bool half_prec=false) const;
208 
209  // these load and save methods do not close fp.
210  bool load(FILE *fp, bool header_only);
211  bool loadBinary(FILE *fp, bool header_only);
212  bool save(FILE *fp) const;
213  bool saveBinary(FILE *fp, bool half_prec = false) const;
214 
215 
216  bool buildGamma(fpreal gamma,
217  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
218  int lutsize = 1024);
219 
220  bool buildSRGB(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
221  int lutsize = 1024);
222 
223  bool buildREC709(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
224  int lutsize = 1024);
225 
226  /// Transform from an OpenColorIO colorspace to a View, specified by a
227  /// display/view pair.
228  /// Always use this method to build a LUT to or from a view; do not attempt
229  /// to roll your own using colorspaces and view transforms as it is not
230  /// straighforward.
231  bool buildOpenColorIOView(const char *src_color_space,
232  const char *display_name,
233  const char *view_name,
234  const char *baked_display= nullptr,
235  const char *baked_view = nullptr,
236  bool inverse = false,
237  int quality = 0);
238 
239  /// Transform from one colorspace to another.
240  /// @c quality can be 0 or 1 right now.
241  bool buildOpenColorIO(const char *src_color_space,
242  const char *dest_color_space,
243  const char *looks = nullptr,
244  int quality = 0);
245 
246  // creates a cineon LUT for our Cineon lib. This allocates a new LUT,
247  // which should be freed with delete. Filename is only for comparing
248  // UT_FileLUTs, it is optional.
250  getCineonLUT(bool forward,
251  const char *filename = 0) const;
252 
253  // Merge 2 LUTs into one LUT (this LUT). This has the same effect as running
254  // a pixel through 'first_lut', then 'second_lut', with some possible
255  // precision loss if one is a large 1D LUT and the other is a 3D LUT.
256  bool mergeLUTs(const PXL_Lookup &first_lut,
257  const PXL_Lookup &second_lut);
258 
259  void setTextureID(void *id) { myTextureID = id; }
260  void *getTextureID() const;
261  void clearTextureID();
262 
263  void setPreLutID(void *id) { myPreLutID = id; }
264  void *getPreLutID() const;
265 
266  exint getDirtySerial() const { return myDirtySerial; }
267 
268 private:
269  int getIndexFor(PXL_LookupType type) const;
270 
271  bool correctLUT();
272 
273  inline void lerpPreLUT(float &color, float prelut_scale,
274  const fpreal *prelut, int prelut_last) const;
275 
276  void save1D(FILE *fp, bool bin, bool half = false) const;
277  void save3D(FILE *fp, bool bin, bool half = false) const;
278 
279  bool fetchAllocationVars(const PXL_OCIO::ColorSpace *scs,
280  const PXL_OCIO::ColorSpace *dcs,
281  bool &uniform,
282  fpreal &minv,
283  fpreal &maxv,
284  fpreal &offset);
285  void prep1DLUT(float *data, int size,
286  bool uniform, fpreal minv, fpreal maxv);
287  void prep3DLUT(bool uniform, fpreal minv, fpreal maxv,
288  fpreal offset, int quality);
289 
290  UT_UniquePtr<CL_Clip> myTable;
291  PXL_LookupType myType;
292  PXL_DataFormat myFormat;
293 
294  // 3D LUT
295  UT_UniquePtr<float[]> myCubeLUT;
296  int myCubeSize;
297 
298  float myBlack;
299  float myWhite;
300  float myDomStart;
301  float myDomEnd;
302  float myRangeStart;
303  float myRangeEnd;
304  float myLogBias;
305  float myPreShift;
306  void *myTextureID;
307  void *myPreLutID;
308  bool myHalfPrecFlag;
309  exint myDirtySerial = 0;
310 };
311 
312 #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
int64 exint
Definition: SYS_Types.h:125
GLdouble s
Definition: glad.h:3009
void setPreLutID(void *id)
Definition: PXL_Lookup.h:263
bool is3DLUT() const
Definition: PXL_Lookup.h:152
exint getDirtySerial() const
Definition: PXL_Lookup.h:266
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:259
GLsizeiptr size
Definition: glcorearb.h:664
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:277
LeafData & operator=(const LeafData &)=delete
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