HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PXL_Raster.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_Raster.h (Pixel Library, C++)
7  *
8  * COMMENTS:
9  * Defines a more generic raster for texture maps & display.
10  *
11  */
12 #ifndef PXL_RASTER_H
13 #define PXL_RASTER_H
14 
15 #include <UT/UT_Rect.h>
16 #include "PXL_API.h"
17 #include "PXL_Common.h"
18 #include <UT/UT_SmallObject.h>
19 #include <UT/UT_StringHolder.h>
20 #include <SYS/fpreal16Limits.h>
21 #include <SYS/SYS_TypeTraits.h>
22 #include <iosfwd>
23 #include <limits>
24 
25 class UT_IStream;
26 
27 #define PXL_ROFF 16
28 
33 };
34 
36  : public UT_SmallObject<PXL_Raster,
37  UT_SMALLOBJECT_CLEANPAGES_OFF, 250,
38  UT_SMALLOBJECT_THREADSAFE_ON>
39 {
40 public:
41  PXL_Raster();
42  PXL_Raster(const PXL_Raster &);
43  PXL_Raster(const PXL_Raster &,
44  exint x1, exint y1, exint x2, exint y2);
46  exint xres = 0, exint yres = 0, int clear = 0,
47  int alloc = 1);
48  virtual ~PXL_Raster();
49 
50  virtual int64 getMemoryUsage(bool inclusive) const;
51 
52  /// Copy over all the information and data from the src. This allocates a
53  /// new raster (rather than doing a shallow copy).
54  virtual void copy(const PXL_Raster &src);
55 
56  /// copies only the raster properties (res, type, etc), not the data, nor
57  /// does this method allocate memory for a raster.
58  virtual void copyProperties(const PXL_Raster &src);
59 
60  // initializes the raster (destroys any previous image data). If you call
61  // any of these methods, call init() after to reallocate the raster.
62  void setPacking(PXL_Packing p);
63  void setFormat(PXL_DataFormat f);
64  void setRes(exint x, exint y);
65  void setBWPoints(float black, float white);
66 
67  /// Colorspace that this raster is in. Changing the color space does not
68  /// affect the image values stored in this raster, only how they are
69  /// interpreted. custom_gamma must be specified for PXL_CS_CUSTOM_GAMMA.
70  void setColorSpace(PXL_ColorSpace space,
71  fpreal custom_gamma = 0.0);
72  void setOCIOColorSpace(const UT_StringHolder &space);
73 
74  void init();
75 
76  // sets the raster data to r, optionally giving this class ownership of it.
77  // make sure it's the right packing type, res & format!
78  void setRaster(void *r, bool give_ownership = true,
79  bool use_offset = false);
80 
81  // steals ownership from this raster. Returns NULL if this raster doesn't
82  // own the image.
83  void * steal();
84 
85  // packing, format & xres/yres must be set for the raster to be valid.
86  int isValid() const { return myRaster ? 1 : 0; }
87  // accessers.
88  exint getXres() const { return myXres; }
89  exint getYres() const { return myYres; }
90  exint getNumPixels() const { return myXres * myYres; }
91  int64 getStride() const { return myStride; }
92  int64 getSize() const { return myRasterSize; }
93  PXL_DataFormat getFormat() const { return myFormat; }
94  PXL_Packing getPacking() const { return myPacking; }
95  void getBWPoints(float &black, float &white) const;
96 
97  /// Colorspace this raster requires for correct display
98  PXL_ColorSpace getColorSpace() const { return myColorSpace; }
99 
100  /// Gamma that must be applied to this raster to get accurate color. Only
101  /// valid for PXL_CS_LINEAR, PXL_CS_GAMMA2_2, and PXL_CS_CUSTOM_GAMMA.
102  fpreal getColorSpaceGamma() const { return myColorSpaceGamma; }
103  /// OCIO colorspace name, valid for PXL_CS_OCIO
104  const UT_StringHolder &getColorSpaceName() const { return myColorSpaceName;}
105 
106  // data window
107  void setDataWindow(const UT_DimRect &new_rect);
108  const UT_DimRect & getDataWindow() const
109  { return myDataWindow; }
110  bool hasDataWindow() const
111  { return !myDataWindow.isEmpty(); }
112 
113  // returns the min/max values. black & white specify the black & white
114  // of the raster, comp specifies an optional component to get the range
115  // from. If ignore_farthest is true, 'max' will contain the 2nd largest
116  // value. Good for ignoring the background.
117  void getRange(float &min, float &max,
118  float black = 0.0f, float white = 1.0f,
119  exint comp = -1,
120  bool ignore_farthest = false) const;
121 
122  // leave 2 pixels to draw grey single channels (R,G,B)
123  void * getPixels()
124  { return (unsigned char *)myRaster+myRasterOffset; }
125 
126  const void * getPixels() const
127  { return (unsigned char *)myRaster+myRasterOffset; }
128 
129  // this is for drawing single greyscale R,G,B images.
130  const void * getRawPixels() const
131  { return myRaster; }
132 
133  // x/y coords, + comp# for Non-interleaved data (0-2 for RGB, 0-3 RGBA).
134  void * getPixel(exint x, exint y, exint comp = 0);
135  const void * getPixel(exint x, exint y, exint comp = 0) const;
136 
137  void setPixelValue(exint x, exint y, const float *vals);
138  void getPixelValue(exint x, exint y, float *vals) const;
139 
140  // returns 1 if interleaved (RGBRGB), 0 otherwise.
141  bool isInterleaved() const;
142 
143  // returns 1 if this raster can have its components rendered separately.
144  int areComponentsRenderable() const;
145 
146  // if not interleaved, returbs the size of 1 channel's data.
147  int64 getNonInterleavedSize() const;
148 
149  // simple operations.
150 
151  // clears the raster to 0, unless color is not null; it assumes it points
152  // to 8/16/32 bit int or float single/triple/quad value, depending on
153  // packing type & data format.
154  void clear(void *color = 0);
155  void clearNormal(float *color,
156  unsigned ib = 0,
157  unsigned iw = 0);
158 
159 
160  // reads a scanline from the raster. THe buffer must be big enough to
161  // hold a single scanline at the data format and packing of the raster.
162  void readFromRow(exint y, void *data) const;
163 
164  // Writes a scanline to the raster. The data format and packing are
165  // assumed to match the raster's.
166  void writeToRow(exint y, const void *data);
167 
168  // instead of writing a scanline, this writes a column of data. The
169  // data format and packing are assumed to match the raster's, and
170  // have yres pixels. Useful for flopping images.
171  void writeToColumn(exint x, const void *data);
172 
173  // deletes the image memory. If full = 1, everything is reset - xres/yres
174  // to 0, packing and format to invalid.
175  virtual void reset(int full = 0);
176 
177  // Extracts a portion of the image 'from' into this raster, setting the
178  // res, data format and packing if needed.
179  void extract(const PXL_Raster &from,
180  exint x1, exint y1, exint x2, exint y2);
181 
182  // Inserts a portion of the image 'from' into this raster. The res,
183  // data format and packing must match.
184  void insert(const PXL_Raster &from,exint x, exint y);
185 
186  void applyLUT(const PXL_Raster *src, float *lptr[4],
187  exint comp, float lstart, float lend);
188 
189  // of the raster. The rectangle can go outside of the raster.
190  void streakEdges(exint x1,exint y1, exint x2,exint y2,
191  PXL_StreakComponents streak_comps = PXL_STREAK_ALL);
192  void convert8Bit(unsigned b, unsigned w);
193 
194  void adjustSaturation(float sat);
195 
196  bool load(UT_IStream &is);
197  int save(std::ostream &os, int binary = 1) const;
198 
199  int getProxyIndex() const { return myProxyIndex; }
200 
201  void setDataWritten(bool written = true)
202  { myWriteFlag = written; }
203  bool getDataWritten() const { return myWriteFlag; }
204 
205  void setTextureID(void *id) { myTextureID = id; }
206  void *getTextureID() const;
207  void clearTextureID();
208 
209  // dither the image in a subdued way for mplay's render over previous.
210  // If a crop region is provided, only pixels inside the crop will be
211  // dimmed.
212  void dither(const UT_DimRect &crop);
213  void dither();
214 
215  bool unpremultiply(PXL_Raster *other = nullptr);
216 
217  bool hasAlpha() const;
218 
219  bool clampAlpha();
220 
221  void addInterest(void *callerData,
222  void (*changeCB)(PXL_Raster *, void *));
223  void removeInterest();
224 
225 protected:
226  virtual void initialize();
227  bool allocRaster(int alloc_mem = 1);
228 
229  void applyLUT8(const PXL_Raster *src, float *lptr[4],
230  exint comp, float lscale, float lshift,
231  exint size);
232  void applyLUT16(const PXL_Raster *src, float *lptr[4],
233  exint comp, float lscale, float lshift,
234  exint size);
235 
236  virtual void attemptRealloc(int64 size);
237 
244  exint myXres, myYres;
247  float myBlack, myWhite;
248  void *myRaster;
249  bool myAmOwner;
252  void *myTextureID;
253  void (*myChangeCB)(PXL_Raster *, void *);
255 
256  UT_DimRect myDataWindow; // (0,0) is lower-left corner
257 };
258 
259 /// This is a wrapper for when the PXL_DataFormat and PXL_Packing are known
260 /// at compile-time, e.g. constructing this inside a branch of a switch
261 /// statement over all types.
262 template<typename T, int NCOMPONENTS, bool INTERLEAVED, bool READ_ONLY>
264 {
265 public:
268 
270  {
271  UT_ASSERT_COMPILETIME(NCOMPONENTS >= 1 && NCOMPONENTS <= 4);
272  UT_ASSERT_MSG(!std::numeric_limits<T>::is_integer || !std::numeric_limits<T>::is_signed, "Integer types must be unsigned.");
273 
274  setRaster(raster);
275  }
276 
278  {
280  }
281 
283  {
284  return myRaster;
285  }
286 
288  {
289  return myRaster->getXres();
290  }
292  {
293  return myRaster->getYres();
294  }
295 
297  {
298  myRaster = raster;
299  if (!raster)
300  return;
301 
302  UT_ASSERT(PXLpackingComponents(raster->getPacking()) == NCOMPONENTS);
303  UT_ASSERT(NCOMPONENTS==1 || raster->isInterleaved() == INTERLEAVED);
304  UT_ASSERT(sizeof(T) == PXLformatDepth(raster->getFormat()));
305  UT_ASSERT(std::numeric_limits<T>::is_integer == (
306  raster->getFormat() == PXL_INT8 ||
307  raster->getFormat() == PXL_INT16 ||
308  raster->getFormat() == PXL_INT32));
309  UT_ASSERT(raster->getRawPixels() != nullptr);
310  }
311 
312  DataType *getPixel(exint x, exint y, exint comp = 0) const
313  {
314  UT_ASSERT_P(myRaster);
315  if (!myRaster)
316  return nullptr;
317  const exint xres = myRaster->getXres();
318  const exint yres = myRaster->getYres();
319  if (x < 0 || y < 0 || x >= xres || y >= yres || !myRaster->getRawPixels())
320  return nullptr;
321 
322  const exint stride = myRaster->getStride();
323  int64 offset;
324  if (NCOMPONENTS==1)
325  offset = x*sizeof(T) + y * stride;
326  else if (INTERLEAVED)
327  offset = (comp+x*NCOMPONENTS)*sizeof(T) + y * stride;
328  else
329  offset = x *sizeof(T) + (y + comp*yres) * stride;
330 
331  return (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)myRaster->getPixels()) + offset);
332  }
333 
334  SYS_FORCE_INLINE DataType *getPixelFast(void *pixels, exint x, exint y/*, exint comp = 0*/) const
335  {
336  UT_ASSERT_P(myRaster);
337  const exint stride = myRaster->getStride();
338  int64 offset;
339  if (NCOMPONENTS==1)
340  offset = x*sizeof(T) + y * stride;
341  else if (INTERLEAVED)
342  offset = (/*comp+*/x*NCOMPONENTS)*sizeof(T) + y * stride;
343  else
344  offset = x *sizeof(T) + (y /*+ comp*myRaster->getYres()*/) * stride;
345  return (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)pixels) + offset);
346  }
347 
348  SYS_FORCE_INLINE void getPixelValueFast(const void *pixels, exint x, exint y, float *vals) const
349  {
350  // const even if READ_ONLY is false
351  const DataType *data = getPixelFast(pixels, x, y);
352  if (INTERLEAVED || NCOMPONENTS==1)
353  {
354  for (int i = 0; i < NCOMPONENTS; ++i)
355  {
356  if (std::numeric_limits<T>::is_integer)
357  vals[i] = float(data[i])/float(std::numeric_limits<T>::max());
358  else
359  vals[i] = float(data[i]);
360  }
361  }
362  else
363  {
364  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
365  for (int i = 0; i < NCOMPONENTS; ++i,
366  data = (const DataType *)(((const unsigned char *)data) + yres_stride))
367  {
368  if (std::numeric_limits<T>::is_integer)
369  vals[i] = float(*data)/float(std::numeric_limits<T>::max());
370  else
371  vals[i] = float(*data);
372  }
373  }
374  }
375 
377  {
378  UT_ASSERT_P(!READ_ONLY);
379 
380  DataType *data = getPixelFast(pixels, x, y);
381  if (INTERLEAVED || NCOMPONENTS==1)
382  {
383  for (int i = 0; i < NCOMPONENTS; ++i)
384  data[i] = T(0);
385  }
386  else
387  {
388  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
389  for (int i = 0; i < NCOMPONENTS; ++i,
390  data = (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)data) + yres_stride))
391  {
392  *data = T(0);
393  }
394  }
395  }
396 
398  {
399  if (std::numeric_limits<T>::is_integer)
400  return T(val * std::numeric_limits<T>::max());
401  else
402  return T(val);
403  }
404 
405  SYS_FORCE_INLINE void setPixelValueFast(void *pixels, exint x, exint y, const T *vals) const
406  {
407  UT_ASSERT_P(!READ_ONLY);
408 
409  DataType *data = getPixelFast(pixels, x, y);
410  if (INTERLEAVED || NCOMPONENTS==1)
411  {
412  for (int i = 0; i < NCOMPONENTS; ++i)
413  data[i] = vals[i];
414  }
415  else
416  {
417  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
418  for (int i = 0; i < NCOMPONENTS; ++i,
419  data = (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)data) + yres_stride))
420  {
421  *data = vals[i];
422  }
423  }
424  }
425 
426 private:
427  RasterType *myRaster;
428 };
429 
430 #endif
SYS_FORCE_INLINE DataType * getPixelFast(void *pixels, exint x, exint y) const
Definition: PXL_Raster.h:334
void * getPixels()
Definition: PXL_Raster.h:123
#define UT_ASSERT_COMPILETIME(expr)
Definition: UT_Assert.h:109
SYS_FORCE_INLINE void getPixelValueFast(const void *pixels, exint x, exint y, float *vals) const
Definition: PXL_Raster.h:348
int PXLpackingComponents(PXL_Packing p)
Definition: PXL_Common.h:92
fpreal myColorSpaceGamma
Definition: PXL_Raster.h:241
SYS_FORCE_INLINE exint getYres() const
Definition: PXL_Raster.h:291
void * myTextureID
Definition: PXL_Raster.h:252
exint getYres() const
Definition: PXL_Raster.h:89
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128
void setTextureID(void *id)
Definition: PXL_Raster.h:205
PXL_DataFormat myFormat
Definition: PXL_Raster.h:238
GLuint color
Definition: glcorearb.h:1260
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
exint myRasterOffset
Definition: PXL_Raster.h:243
const GLuint GLenum const void * binary
Definition: glcorearb.h:1923
UT_StringHolder myColorSpaceName
Definition: PXL_Raster.h:242
PXL_ColorSpace getColorSpace() const
Colorspace this raster requires for correct display.
Definition: PXL_Raster.h:98
const void * getRawPixels() const
Definition: PXL_Raster.h:130
fpreal getColorSpaceGamma() const
Definition: PXL_Raster.h:102
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
Definition: glcorearb.h:107
GLint y
Definition: glcorearb.h:102
bool myWriteFlag
Definition: PXL_Raster.h:251
bool getDataWritten() const
Definition: PXL_Raster.h:203
SYS_FORCE_INLINE PXL_RasterWrapper< T, NCOMPONENTS, INTERLEAVED, true > makeConstant() const
Definition: PXL_Raster.h:277
png_uint_32 i
Definition: png.h:2877
#define PXL_API
Definition: PXL_API.h:10
void * myRaster
Definition: PXL_Raster.h:248
GLsizeiptr size
Definition: glcorearb.h:663
PXL_DataFormat getFormat() const
Definition: PXL_Raster.h:93
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
int64 myStride
Definition: PXL_Raster.h:245
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
GLuint id
Definition: glcorearb.h:654
long long int64
Definition: SYS_Types.h:106
SYS_FORCE_INLINE exint getXres() const
Definition: PXL_Raster.h:287
GLfloat f
Definition: glcorearb.h:1925
PXL_ColorSpace myColorSpace
Definition: PXL_Raster.h:240
PXL_Packing getPacking() const
Definition: PXL_Raster.h:94
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
bool myAmOwner
Definition: PXL_Raster.h:249
int64 exint
Definition: SYS_Types.h:115
exint getNumPixels() const
Definition: PXL_Raster.h:90
SYS_FORCE_INLINE void setRaster(RasterType *raster)
Definition: PXL_Raster.h:296
int isValid() const
Definition: PXL_Raster.h:86
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLintptr offset
Definition: glcorearb.h:664
SYS_ConstType< T, READ_ONLY >::type DataType
Definition: PXL_Raster.h:267
float myWhite
Definition: PXL_Raster.h:247
PXL_Packing
Definition: PXL_Common.h:29
bool isInterleaved() const
GLboolean * data
Definition: glcorearb.h:130
int myProxyIndex
Definition: PXL_Raster.h:250
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:316
bool hasDataWindow() const
Definition: PXL_Raster.h:110
int64 myRasterSize
Definition: PXL_Raster.h:246
PXL_DataFormat
Definition: PXL_Common.h:19
static SYS_FORCE_INLINE T convertFromFloat(const float val)
Definition: PXL_Raster.h:397
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
const UT_DimRect & getDataWindow() const
Definition: PXL_Raster.h:108
const void * getPixels() const
Definition: PXL_Raster.h:126
SYS_FORCE_INLINE RasterType * getRaster() const
Definition: PXL_Raster.h:282
exint myYres
Definition: PXL_Raster.h:244
const UT_StringHolder & getColorSpaceName() const
OCIO colorspace name, valid for PXL_CS_OCIO.
Definition: PXL_Raster.h:104
int64 getStride() const
Definition: PXL_Raster.h:91
double fpreal
Definition: SYS_Types.h:269
int64 getSize() const
Definition: PXL_Raster.h:92
void * myCallerData
Definition: PXL_Raster.h:254
PXL_ColorSpace
Definition: PXL_Common.h:42
GLint GLenum GLint x
Definition: glcorearb.h:408
GLuint GLfloat * val
Definition: glcorearb.h:1607
int PXLformatDepth(PXL_DataFormat d)
Definition: PXL_Common.h:89
PXL_Packing myPacking
Definition: PXL_Raster.h:239
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
int getProxyIndex() const
Definition: PXL_Raster.h:199
void setDataWritten(bool written=true)
Definition: PXL_Raster.h:201
GLboolean r
Definition: glcorearb.h:1221
SYS_ConstType< PXL_Raster, READ_ONLY >::type RasterType
Definition: PXL_Raster.h:266
PXL_StreakComponents
Definition: PXL_Raster.h:29
SYS_FORCE_INLINE void zeroPixelValueFast(void *pixels, exint x, exint y) const
Definition: PXL_Raster.h:376
exint getXres() const
Definition: PXL_Raster.h:88
#define UT_ASSERT_MSG(ZZ, MM)
Definition: UT_Assert.h:105
DataType * getPixel(exint x, exint y, exint comp=0) const
Definition: PXL_Raster.h:312
SYS_FORCE_INLINE PXL_RasterWrapper(RasterType *raster=nullptr)
Definition: PXL_Raster.h:269
SYS_FORCE_INLINE void setPixelValueFast(void *pixels, exint x, exint y, const T *vals) const
Definition: PXL_Raster.h:405
UT_DimRect myDataWindow
Definition: PXL_Raster.h:256
GLenum src
Definition: glcorearb.h:1792