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 
16 class PXL_Plane;
17 class PXL_Raster;
18 class UT_FileLUT;
19 
20 #include <stdio.h>
21 #include <SYS/SYS_Types.h>
22 #include "PXL_Common.h"
23 #include <CL/CL_Clip.h>
24 
26 {
27  PXL_LOOKUP_R = 0x001, // 1 table for Red only.
28  PXL_LOOKUP_G = 0x002,
29  PXL_LOOKUP_B = 0x004,
30  PXL_LOOKUP_A = 0x008, // 1 table for Alpha only.
31 
32  PXL_LOOKUP_RGB = 0x017, // 3 tables, 1 each for Red, Green & Blue.
33  PXL_LOOKUP_RGBA = 0x01F, // 4 tables, 1 each for R, G, B and alpha.
34 
35  PXL_LOOKUP_COLOR = 0x027, // 1 table for RGB.
36  PXL_LOOKUP_ALL = 0x02F, // 1 table for all channels.
37 
38  PXL_LOOKUP_3D = 0x040, // 3D LUT for color only.
39  PXL_LOOKUP_3D_1D = 0x060, // 3D LUT with 1D pre-pass, color only.
40  PXL_LOOKUP_3D_HDR = 0x140 // 3D LUT with log sampling
41 };
42 
43 // Masks for PXL_LookupType
44 #define PXL_LOOKUP_1D 0x3F
45 #define PXL_SEPARATE_CHANNELS 0x10
46 #define PXL_SINGLE_CHANNEL 0x20
47 #define PXL_ILLEGAL_MATCH 0x30
48 #define PXL_LOG_SAMPLE 0x100
49 
50 
52 {
53 public:
54  PXL_Lookup();
55  ~PXL_Lookup();
56 
57  int64 getMemoryUsage(bool inclusive) const;
58 
59  void reset();
60 
61  // common LUT applications
62 
63  // single color modification
64  void evalLUT(float color[3]) const;
65 
66  // set a 1D LUT.
67  void setLUT(const CL_Clip &clip);
68 
69  // set the 3D LUT.
70  void set3DLUT(int size, float *lut);
71 
72  // applies the LUT to a raster (along with gamma). black/white are the
73  // BW points of the image in the raster. 'comp' is used to convert the
74  // raster to show only 1 component (ie, r->rgb).
75  void applyLUT(PXL_Raster &r, float gamma = 1.0f,
76  float black=0.0f, float white=1.0f,
77  int comp = -1) const;
78 
79  // returns the value for sampling at integer LUT index 'i'. If the
80  // value is passed to evalLUT(), it will return the sample at table index i.
81  // For 3D LUTs, this needs to be called once for each axis.
82  fpreal indexToValue(int i) const;
83 
84  // --------------------------------------------------------------------
85  // accessers
86 
87  bool isValid() const;
88 
89  // 1D accessers
90  PXL_LookupType getType() const;
91  bool getLogSampling() const;
92  float getLogBias() const;
93  int getNumChannels() const;
94  int getTableSize() const;
95  float getBlackPoint() const;
96  float getWhitePoint() const;
97  void getDomain(float &s, float &e) const;
98  void getRange(float &s, float &e) const;
99  float getPreShift() const { return myPreShift; }
100 
101  bool hasSpecificFormat(PXL_DataFormat &format) const;
102  bool hasLUTfor(PXL_LookupType type) const;
103 
104  void setType(PXL_LookupType type);
105  void setLogSampling(bool log);
106  void setLogBias(float bias);
107  void setSpecificFormat(PXL_DataFormat format);
108  void setNumChannels(int num);
109  void setTableSize(int size);
110  void setBlackWhitePoints(float b, float w);
111  void setDomain(float s,float e);
112  void setRange(float s, float e);
113  void invert(int newsize = 0);
114 
115  bool isHalfPrec() const { return myHalfPrecFlag; }
116 
117  // 3D accessers
118  bool is3DLUT() const { return myCubeSize != 0; }
119  int get3DSize() const { return myCubeSize; }
120 
121  // --------------------------------------------------------------------
122  // 1D LUT evaluation routines
123  fpreal *getRawLUT(PXL_LookupType type);
124  const fpreal *getRawLUT(PXL_LookupType type) const;
125 
126  bool evalLUT(PXL_LookupType,
127  int nsamples,
128  float *data,
129  unsigned b = 0,
130  unsigned w = 0) const;
131 
132  bool evalLUT(PXL_LookupType,
133  int nsamples,
134  float *data,
135  float black, float white,
136  float from = 0.0f, float to = 1.0f) const;
137  float evalLUT(PXL_LookupType, float pos) const;
138 
139  void eval1DLUT(float color[3]) const;
140  void get1DLUT(float *lut[4], int size, float b, float w,
141  float sc, float sh, float gamma) const;
142 
143  void apply1DLUT(PXL_Raster &raster,
144  float gamma = 1.0f,
145  float black = 0.0f,
146  float white = 1.0f,
147  int comp = -1) const;
148  // ---------------------------------------------------------------------
149  // 3D LUT evaluation routines
150  void eval3DLUT(float color[3]) const;
151 
152  // makes a copy of the 3D LUT. 'lut' must be of size get3DSize()^3 * 3 if
153  // type is a 3D linear or log LUT. If 3D with prelut is used, prelut must
154  // non-NULL and of size get3DSize()^2.
155  void copy3DLUT(float *lut, float *prelut = 0) const;
156  void copy3DLUT(fpreal16 *lut, fpreal16 *prelut = 0) const;
157 
158  // clamps the 3D LUT to 0,1. Used for display LUTs.
159  void clamp3DLUT();
160 
161  void apply3DLUT(PXL_Raster &r, float gamma = 1.0f,
162  float black = 0.0f, float white = 1.0f,
163  int component = -1) const;
164 
165 
166  // File I/O.
167  bool load(const char *filename, bool header_only = false);
168  bool save(const char *filename, bool half_prec=false) const;
169 
170  // these load and save methods do not close fp.
171  bool load(FILE *fp, bool header_only);
172  bool loadBinary(FILE *fp, bool header_only);
173  bool save(FILE *fp) const;
174  bool saveBinary(FILE *fp, bool half_prec = false) const;
175 
176 
177  // create builtin lut types
178  enum Dir
179  {
181  GAMMA_TO_LINEAR
182  };
183 
184  bool buildGamma(fpreal gamma,
185  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
186  int lutsize = 1024);
187 
188  bool buildSRGB(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
189  int lutsize = 1024);
190 
191  bool buildREC709(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
192  int lutsize = 1024);
193 
194  bool buildOpenColorIO(const char *src_color_space,
195  const char *dest_color_space,
196  const char * looks = NULL);
197 
198  // creates a cineon LUT for our Cineon lib. This allocates a new LUT,
199  // which should be freed with delete. Filename is only for comparing
200  // UT_FileLUTs, it is optional.
201  UT_FileLUT *getCineonLUT(bool forward,
202  const char *filename = 0) const;
203 
204  // Merge 2 LUTs into one LUT (this LUT). This has the same effect as running
205  // a pixel through 'first_lut', then 'second_lut', with some possible
206  // precision loss if one is a large 1D LUT and the other is a 3D LUT.
207  bool mergeLUTs(const PXL_Lookup &first_lut,
208  const PXL_Lookup &second_lut);
209 
210  void setTextureID(void *id) { myTextureID = id; }
211  void *getTextureID() const;
212  void clearTextureID();
213 
214  void setPreLutID(void *id) { myPreLutID = id; }
215  void *getPreLutID() const;
216 
217 private:
218  int getIndexFor(PXL_LookupType type) const;
219 
220  bool correctLUT();
221 
222  inline void lerpPreLUT(float &color, float prelut_scale,
223  const fpreal *prelut, int prelut_last) const;
224 
225  void save1D(FILE *fp, bool bin, bool half = false) const;
226  void save3D(FILE *fp, bool bin, bool half = false) const;
227 
228  CL_Clip *myTable;
229  PXL_LookupType myType;
230  PXL_DataFormat myFormat;
231 
232  // 3D LUT
233  float *myCubeLUT;
234  int myCubeSize;
235 
236  float myBlack;
237  float myWhite;
238  float myDomStart;
239  float myDomEnd;
240  float myRangeStart;
241  float myRangeEnd;
242  float myLogBias;
243  float myPreShift;
244  void *myTextureID;
245  void *myPreLutID;
246  bool myHalfPrecFlag;
247 };
248 
249 #endif
GLdouble s
Definition: glew.h:1390
GLboolean invert
Definition: glew.h:1422
GLuint id
Definition: glew.h:1679
GT_API const UT_StringHolder filename
int get3DSize() const
Definition: PXL_Lookup.h:119
GLsizeiptr size
Definition: glew.h:1681
float getPreShift() const
Definition: PXL_Lookup.h:99
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
void setPreLutID(void *id)
Definition: PXL_Lookup.h:214
bool is3DLUT() const
Definition: PXL_Lookup.h:118
GLboolean reset
Definition: glew.h:4959
bool isHalfPrec() const
Definition: PXL_Lookup.h:115
#define PXL_API
Definition: PXL_API.h:10
PXL_LookupType
Definition: PXL_Lookup.h:25
GLclampf f
Definition: glew.h:3499
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
long long int64
Definition: SYS_Types.h:116
GLfloat bias
Definition: glew.h:10274
PXL_DataFormat
Definition: PXL_Common.h:20
GLuint color
Definition: glew.h:7902
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
void setTextureID(void *id)
Definition: PXL_Lookup.h:210
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:89
T log(const T &v)
Definition: simd.h:7432
GLuint num
Definition: glew.h:2690
fpreal64 fpreal
Definition: SYS_Types.h:277
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
Definition: half.h:91