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