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 
100  bool hasSpecificFormat(PXL_DataFormat &format) const;
101  bool hasLUTfor(PXL_LookupType type) const;
102 
103  void setType(PXL_LookupType type);
104  void setLogSampling(bool log);
105  void setLogBias(float bias);
106  void setSpecificFormat(PXL_DataFormat format);
107  void setNumChannels(int num);
108  void setTableSize(int size);
109  void setBlackWhitePoints(float b, float w);
110  void setDomain(float s,float e);
111  void setRange(float s, float e);
112  void invert(int newsize = 0);
113 
114  bool isHalfPrec() const { return myHalfPrecFlag; }
115 
116  // 3D accessers
117  bool is3DLUT() const { return myCubeSize != 0; }
118  int get3DSize() const { return myCubeSize; }
119 
120  // --------------------------------------------------------------------
121  // 1D LUT evaluation routines
122  fpreal *getRawLUT(PXL_LookupType type);
123  const fpreal *getRawLUT(PXL_LookupType type) const;
124 
125  bool evalLUT(PXL_LookupType,
126  int nsamples,
127  float *data,
128  unsigned b = 0,
129  unsigned w = 0) const;
130 
131  bool evalLUT(PXL_LookupType,
132  int nsamples,
133  float *data,
134  float black, float white,
135  float from = 0.0f, float to = 1.0f) const;
136  float evalLUT(PXL_LookupType, float pos) const;
137 
138  void eval1DLUT(float color[3]) const;
139  void get1DLUT(float *lut[4], int size, float b, float w,
140  float sc, float sh, float gamma) const;
141 
142  void apply1DLUT(PXL_Raster &raster,
143  float gamma = 1.0f,
144  float black = 0.0f,
145  float white = 1.0f,
146  int comp = -1) const;
147  // ---------------------------------------------------------------------
148  // 3D LUT evaluation routines
149  void eval3DLUT(float color[3]) const;
150 
151  // makes a copy of the 3D LUT. 'lut' must be of size get3DSize()^3 * 3 if
152  // type is a 3D linear or log LUT. If 3D with prelut is used, prelut must
153  // non-NULL and of size get3DSize()^2.
154  void copy3DLUT(float *lut, float *prelut = 0) const;
155  void copy3DLUT(fpreal16 *lut, fpreal16 *prelut = 0) const;
156 
157  // clamps the 3D LUT to 0,1. Used for display LUTs.
158  void clamp3DLUT();
159 
160  void apply3DLUT(PXL_Raster &r, float gamma = 1.0f,
161  float black = 0.0f, float white = 1.0f,
162  int component = -1) const;
163 
164 
165  // File I/O.
166  bool load(const char *filename, bool header_only = false);
167  bool save(const char *filename, bool half_prec=false) const;
168 
169  // these load and save methods do not close fp.
170  bool load(FILE *fp, bool header_only);
171  bool loadBinary(FILE *fp, bool header_only);
172  bool save(FILE *fp) const;
173  bool saveBinary(FILE *fp, bool half_prec = false) const;
174 
175 
176  // create builtin lut types
177  enum Dir
178  {
180  GAMMA_TO_LINEAR
181  };
182 
183  bool buildGamma(fpreal gamma,
184  PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
185  int lutsize = 1024);
186 
187  bool buildSRGB(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
188  int lutsize = 1024);
189 
190  bool buildREC709(PXL_Lookup::Dir dir = LINEAR_TO_GAMMA,
191  int lutsize = 1024);
192 
193  bool buildOpenColorIO(const char *src_color_space,
194  const char *dest_color_space,
195  const char * looks = NULL);
196 
197  // creates a cineon LUT for our Cineon lib. This allocates a new LUT,
198  // which should be freed with delete. Filename is only for comparing
199  // UT_FileLUTs, it is optional.
200  UT_FileLUT *getCineonLUT(bool forward,
201  const char *filename = 0) const;
202 
203  // Merge 2 LUTs into one LUT (this LUT). This has the same effect as running
204  // a pixel through 'first_lut', then 'second_lut', with some possible
205  // precision loss if one is a large 1D LUT and the other is a 3D LUT.
206  bool mergeLUTs(const PXL_Lookup &first_lut,
207  const PXL_Lookup &second_lut);
208 
209  void setTextureID(void *id) { myTextureID = id; }
210  void *getTextureID() const;
211  void clearTextureID();
212 
213  void setPreLutID(void *id) { myPreLutID = id; }
214  void *getPreLutID() const;
215 
216 private:
217  int getIndexFor(PXL_LookupType type) const;
218 
219  bool correctLUT();
220 
221  inline void lerpPreLUT(float &color, float prelut_scale,
222  const fpreal *prelut, int prelut_last) const;
223 
224  void save1D(FILE *fp, bool bin, bool half = false) const;
225  void save3D(FILE *fp, bool bin, bool half = false) const;
226 
227  CL_Clip *myTable;
228  PXL_LookupType myType;
229  PXL_DataFormat myFormat;
230 
231  // 3D LUT
232  float *myCubeLUT;
233  int myCubeSize;
234 
235  float myBlack;
236  float myWhite;
237  float myDomStart;
238  float myDomEnd;
239  float myRangeStart;
240  float myRangeEnd;
241  float myLogBias;
242  void *myTextureID;
243  void *myPreLutID;
244  bool myHalfPrecFlag;
245 };
246 
247 #endif
GT_API const UT_StringHolder filename
int get3DSize() const
Definition: PXL_Lookup.h:118
GLboolean invert
Definition: glcorearb.h:548
GLuint color
Definition: glcorearb.h:1260
void setPreLutID(void *id)
Definition: PXL_Lookup.h:213
bool is3DLUT() const
Definition: PXL_Lookup.h:117
png_uint_32 i
Definition: png.h:2877
bool isHalfPrec() const
Definition: PXL_Lookup.h:114
#define PXL_API
Definition: PXL_API.h:10
GLsizeiptr size
Definition: glcorearb.h:663
PXL_LookupType
Definition: PXL_Lookup.h:25
GLuint id
Definition: glcorearb.h:654
SYS_API double log(double x)
Definition: SYS_FPUMath.h:87
long long int64
Definition: SYS_Types.h:107
png_FILE_p fp
Definition: png.h:2028
GLfloat f
Definition: glcorearb.h:1925
GLboolean * data
Definition: glcorearb.h:130
PXL_DataFormat
Definition: PXL_Common.h:19
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
void setTextureID(void *id)
Definition: PXL_Lookup.h:209
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
double fpreal
Definition: SYS_Types.h:270
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER T clip(const T &p, const Box< T > &box)
Definition: ImathBoxAlgo.h:89
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
png_infop png_uint_32 int num
Definition: png.h:2158
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
GLboolean r
Definition: glcorearb.h:1221
Definition: half.h:91