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 
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(const void *color = 0);
155  void clearNormal(const 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  void freeRaster();
229 
230  void applyLUT8(const PXL_Raster *src, float *lptr[4],
231  exint comp, float lscale, float lshift,
232  exint size);
233  void applyLUT16(const PXL_Raster *src, float *lptr[4],
234  exint comp, float lscale, float lshift,
235  exint size);
236 
237  virtual void attemptRealloc(int64 size);
238 
245  exint myXres, myYres;
248  float myBlack, myWhite;
249  void *myRaster;
250  bool myAmOwner;
253  void *myTextureID;
254  void (*myChangeCB)(PXL_Raster *, void *);
256 
257  UT_DimRect myDataWindow; // (0,0) is lower-left corner
258 };
259 
260 /// This is a wrapper for when the PXL_DataFormat and PXL_Packing are known
261 /// at compile-time, e.g. constructing this inside a branch of a switch
262 /// statement over all types.
263 template<typename T, int NCOMPONENTS, bool INTERLEAVED, bool READ_ONLY>
265 {
266 public:
269 
271  {
272  UT_ASSERT_COMPILETIME(NCOMPONENTS >= 1 && NCOMPONENTS <= 4);
273  UT_ASSERT_MSG(!std::numeric_limits<T>::is_integer || !std::numeric_limits<T>::is_signed, "Integer types must be unsigned.");
274 
275  setRaster(raster);
276  }
277 
279  {
281  }
282 
284  {
285  return myRaster;
286  }
287 
289  {
290  return myRaster->getXres();
291  }
293  {
294  return myRaster->getYres();
295  }
296 
298  {
299  myRaster = raster;
300  if (!raster)
301  return;
302 
303  UT_ASSERT(PXLpackingComponents(raster->getPacking()) == NCOMPONENTS);
304  UT_ASSERT(NCOMPONENTS==1 || raster->isInterleaved() == INTERLEAVED);
305  UT_ASSERT(sizeof(T) == PXLformatDepth(raster->getFormat()));
306  UT_ASSERT(std::numeric_limits<T>::is_integer == (
307  raster->getFormat() == PXL_INT8 ||
308  raster->getFormat() == PXL_INT16 ||
309  raster->getFormat() == PXL_INT32));
310  UT_ASSERT(raster->getRawPixels() != nullptr);
311  }
312 
313  DataType *getPixel(exint x, exint y, exint comp = 0) const
314  {
315  UT_ASSERT_P(myRaster);
316  if (!myRaster)
317  return nullptr;
318  const exint xres = myRaster->getXres();
319  const exint yres = myRaster->getYres();
320  if (x < 0 || y < 0 || x >= xres || y >= yres || !myRaster->getRawPixels())
321  return nullptr;
322 
323  const exint stride = myRaster->getStride();
324  int64 offset;
325  if (NCOMPONENTS==1)
326  offset = x*sizeof(T) + y * stride;
327  else if (INTERLEAVED)
328  offset = (comp+x*NCOMPONENTS)*sizeof(T) + y * stride;
329  else
330  offset = x *sizeof(T) + (y + comp*yres) * stride;
331 
332  return (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)myRaster->getPixels()) + offset);
333  }
334 
335  SYS_FORCE_INLINE DataType *getPixelFast(void *pixels, exint x, exint y/*, exint comp = 0*/) const
336  {
337  UT_ASSERT_P(myRaster);
338  const exint stride = myRaster->getStride();
339  int64 offset;
340  if (NCOMPONENTS==1)
341  offset = x*sizeof(T) + y * stride;
342  else if (INTERLEAVED)
343  offset = (/*comp+*/x*NCOMPONENTS)*sizeof(T) + y * stride;
344  else
345  offset = x *sizeof(T) + (y /*+ comp*myRaster->getYres()*/) * stride;
346  return (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)pixels) + offset);
347  }
348 
349  SYS_FORCE_INLINE void getPixelValueFast(const void *pixels, exint x, exint y, float *vals) const
350  {
351  // const even if READ_ONLY is false
352  const DataType *data = getPixelFast(pixels, x, y);
353  if (INTERLEAVED || NCOMPONENTS==1)
354  {
355  for (int i = 0; i < NCOMPONENTS; ++i)
356  {
357  if (std::numeric_limits<T>::is_integer)
358  vals[i] = float(data[i])/float(std::numeric_limits<T>::max());
359  else
360  vals[i] = float(data[i]);
361  }
362  }
363  else
364  {
365  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
366  for (int i = 0; i < NCOMPONENTS; ++i,
367  data = (const DataType *)(((const unsigned char *)data) + yres_stride))
368  {
369  if (std::numeric_limits<T>::is_integer)
370  vals[i] = float(*data)/float(std::numeric_limits<T>::max());
371  else
372  vals[i] = float(*data);
373  }
374  }
375  }
376 
378  {
379  UT_ASSERT_P(!READ_ONLY);
380 
381  DataType *data = getPixelFast(pixels, x, y);
382  if (INTERLEAVED || NCOMPONENTS==1)
383  {
384  for (int i = 0; i < NCOMPONENTS; ++i)
385  data[i] = T(0);
386  }
387  else
388  {
389  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
390  for (int i = 0; i < NCOMPONENTS; ++i,
391  data = (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)data) + yres_stride))
392  {
393  *data = T(0);
394  }
395  }
396  }
397 
399  {
400  if (std::numeric_limits<T>::is_integer)
401  return T(val * std::numeric_limits<T>::max());
402  else
403  return T(val);
404  }
405 
406  SYS_FORCE_INLINE void setPixelValueFast(void *pixels, exint x, exint y, const T *vals) const
407  {
408  UT_ASSERT_P(!READ_ONLY);
409 
410  DataType *data = getPixelFast(pixels, x, y);
411  if (INTERLEAVED || NCOMPONENTS==1)
412  {
413  for (int i = 0; i < NCOMPONENTS; ++i)
414  data[i] = vals[i];
415  }
416  else
417  {
418  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
419  for (int i = 0; i < NCOMPONENTS; ++i,
420  data = (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)data) + yres_stride))
421  {
422  *data = vals[i];
423  }
424  }
425  }
426 
427 private:
428  RasterType *myRaster;
429 };
430 
431 #endif
SYS_FORCE_INLINE DataType * getPixelFast(void *pixels, exint x, exint y) const
Definition: PXL_Raster.h:335
void * getPixels()
Definition: PXL_Raster.h:123
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:349
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
int PXLpackingComponents(PXL_Packing p)
Definition: PXL_Common.h:92
fpreal myColorSpaceGamma
Definition: PXL_Raster.h:242
UT_ASSERT_COMPILETIME(BRAY_EVENT_MAXFLAGS<=32)
SYS_FORCE_INLINE exint getYres() const
Definition: PXL_Raster.h:292
void * myTextureID
Definition: PXL_Raster.h:253
GLuint id
Definition: glew.h:1679
GLsizeiptr size
Definition: glew.h:1681
GLenum src
Definition: glew.h:2410
exint getYres() const
Definition: PXL_Raster.h:89
void setTextureID(void *id)
Definition: PXL_Raster.h:205
GLuint const GLfloat * val
Definition: glew.h:2794
PXL_DataFormat myFormat
Definition: PXL_Raster.h:239
exint myRasterOffset
Definition: PXL_Raster.h:244
UT_StringHolder myColorSpaceName
Definition: PXL_Raster.h:243
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
GLuint GLfloat GLfloat GLfloat x1
Definition: glew.h:12681
fpreal getColorSpaceGamma() const
Definition: PXL_Raster.h:102
bool myWriteFlag
Definition: PXL_Raster.h:252
GLboolean reset
Definition: glew.h:4959
bool getDataWritten() const
Definition: PXL_Raster.h:203
SYS_FORCE_INLINE PXL_RasterWrapper< T, NCOMPONENTS, INTERLEAVED, true > makeConstant() const
Definition: PXL_Raster.h:278
#define PXL_API
Definition: PXL_API.h:10
void * myRaster
Definition: PXL_Raster.h:249
PXL_DataFormat getFormat() const
Definition: PXL_Raster.h:93
int64 myStride
Definition: PXL_Raster.h:246
#define UT_ASSERT_MSG(ZZ,...)
Definition: UT_Assert.h:138
long long int64
Definition: SYS_Types.h:111
SYS_FORCE_INLINE exint getXres() const
Definition: PXL_Raster.h:288
PXL_ColorSpace myColorSpace
Definition: PXL_Raster.h:241
PXL_Packing getPacking() const
Definition: PXL_Raster.h:94
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:250
int64 exint
Definition: SYS_Types.h:120
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
exint getNumPixels() const
Definition: PXL_Raster.h:90
#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:297
int isValid() const
Definition: PXL_Raster.h:86
#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:268
float myWhite
Definition: PXL_Raster.h:248
PXL_Packing
Definition: PXL_Common.h:29
bool isInterleaved() const
int myProxyIndex
Definition: PXL_Raster.h:251
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:318
bool hasDataWindow() const
Definition: PXL_Raster.h:110
int64 myRasterSize
Definition: PXL_Raster.h:247
PXL_DataFormat
Definition: PXL_Common.h:19
GLuint color
Definition: glew.h:7902
static SYS_FORCE_INLINE T convertFromFloat(const float val)
Definition: PXL_Raster.h:398
const UT_DimRect & getDataWindow() const
Definition: PXL_Raster.h:108
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const void * pixels
Definition: glew.h:1254
const void * getPixels() const
Definition: PXL_Raster.h:126
SYS_FORCE_INLINE RasterType * getRaster() const
Definition: PXL_Raster.h:283
GLsizei stride
Definition: glew.h:1523
exint myYres
Definition: PXL_Raster.h:245
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
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
GLfloat GLfloat p
Definition: glew.h:16321
double fpreal
Definition: SYS_Types.h:276
int64 getSize() const
Definition: PXL_Raster.h:92
void * myCallerData
Definition: PXL_Raster.h:255
PXL_ColorSpace
Definition: PXL_Common.h:42
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:89
PXL_Packing myPacking
Definition: PXL_Raster.h:240
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
int getProxyIndex() const
Definition: PXL_Raster.h:199
void setDataWritten(bool written=true)
Definition: PXL_Raster.h:201
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
ImageBuf OIIO_API crop(const ImageBuf &src, ROI roi={}, int nthreads=0)
SYS_ConstType< PXL_Raster, READ_ONLY >::type RasterType
Definition: PXL_Raster.h:267
PXL_StreakComponents
Definition: PXL_Raster.h:29
SYS_FORCE_INLINE void zeroPixelValueFast(void *pixels, exint x, exint y) const
Definition: PXL_Raster.h:377
exint getXres() const
Definition: PXL_Raster.h:88
DataType * getPixel(exint x, exint y, exint comp=0) const
Definition: PXL_Raster.h:313
SYS_FORCE_INLINE PXL_RasterWrapper(RasterType *raster=nullptr)
Definition: PXL_Raster.h:270
SYS_FORCE_INLINE void setPixelValueFast(void *pixels, exint x, exint y, const T *vals) const
Definition: PXL_Raster.h:406
UT_DimRect myDataWindow
Definition: PXL_Raster.h:257
GLintptr offset
Definition: glew.h:1682