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