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 
211  // dither the image in a subdued way for mplay's render over previous.
212  // If a crop region is provided, only pixels inside the crop will be
213  // dimmed.
214  void dither(const UT_DimRect &crop);
215  void dither();
216 
217  bool unpremultiply(PXL_Raster *other = nullptr);
218 
219  bool hasAlpha() const;
220 
221  bool clampAlpha();
222 
223  void addInterest(void *callerData,
224  void (*changeCB)(PXL_Raster *, void *));
225  void removeInterest();
226 
227 protected:
228  virtual void initialize();
229  bool allocRaster(int alloc_mem = 1);
230  void freeRaster();
231 
232  void applyLUT8(const PXL_Raster *src, float *lptr[4],
233  exint comp, float lscale, float lshift,
234  exint size);
235  void applyLUT16(const PXL_Raster *src, float *lptr[4],
236  exint comp, float lscale, float lshift,
237  exint size);
238 
239  virtual void attemptRealloc(int64 size);
240 
248  exint myXres, myYres;
251  float myBlack, myWhite;
252  void *myRaster;
253  bool myAmOwner;
256  void *myTextureID;
257  void (*myChangeCB)(PXL_Raster *, void *);
259 
260  UT_DimRect myDataWindow; // (0,0) is lower-left corner
261 };
262 
263 /// This is a wrapper for when the PXL_DataFormat and PXL_Packing are known
264 /// at compile-time, e.g. constructing this inside a branch of a switch
265 /// statement over all types.
266 template<typename T, int NCOMPONENTS, bool INTERLEAVED, bool READ_ONLY>
268 {
269 public:
272 
274  {
275  UT_ASSERT_COMPILETIME(NCOMPONENTS >= 1 && NCOMPONENTS <= 4);
276  UT_ASSERT_MSG(!std::numeric_limits<T>::is_integer || !std::numeric_limits<T>::is_signed, "Integer types must be unsigned.");
277 
278  setRaster(raster);
279  }
280 
282  {
284  }
285 
287  {
288  return myRaster;
289  }
290 
292  {
293  return myRaster->getXres();
294  }
296  {
297  return myRaster->getYres();
298  }
299 
301  {
302  myRaster = raster;
303  if (!raster)
304  return;
305 
306  UT_ASSERT(PXLpackingComponents(raster->getPacking()) == NCOMPONENTS);
307  UT_ASSERT(NCOMPONENTS==1 || raster->isInterleaved() == INTERLEAVED);
308  UT_ASSERT(sizeof(T) == PXLformatDepth(raster->getFormat()));
309  UT_ASSERT(std::numeric_limits<T>::is_integer == (
310  raster->getFormat() == PXL_INT8 ||
311  raster->getFormat() == PXL_INT16 ||
312  raster->getFormat() == PXL_INT32));
313  UT_ASSERT(raster->getRawPixels() != nullptr);
314  }
315 
316  DataType *getPixel(exint x, exint y, exint comp = 0) const
317  {
318  UT_ASSERT_P(myRaster);
319  if (!myRaster)
320  return nullptr;
321  const exint xres = myRaster->getXres();
322  const exint yres = myRaster->getYres();
323  if (x < 0 || y < 0 || x >= xres || y >= yres || !myRaster->getRawPixels())
324  return nullptr;
325 
326  const exint stride = myRaster->getStride();
327  int64 offset;
328  if (NCOMPONENTS==1)
329  offset = x*sizeof(T) + y * stride;
330  else if (INTERLEAVED)
331  offset = (comp+x*NCOMPONENTS)*sizeof(T) + y * stride;
332  else
333  offset = x *sizeof(T) + (y + comp*yres) * stride;
334 
335  return (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)myRaster->getPixels()) + offset);
336  }
337 
338  SYS_FORCE_INLINE DataType *getPixelFast(void *pixels, exint x, exint y/*, exint comp = 0*/) const
339  {
340  UT_ASSERT_P(myRaster);
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*myRaster->getYres()*/) * stride;
349  return (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)pixels) + offset);
350  }
351 
352  SYS_FORCE_INLINE void getPixelValueFast(const void *pixels, exint x, exint y, float *vals) const
353  {
354  // const even if READ_ONLY is false
355  const DataType *data = getPixelFast(pixels, x, y);
356  if (INTERLEAVED || NCOMPONENTS==1)
357  {
358  for (int i = 0; i < NCOMPONENTS; ++i)
359  {
360  if (std::numeric_limits<T>::is_integer)
361  vals[i] = float(data[i])/float(std::numeric_limits<T>::max());
362  else
363  vals[i] = float(data[i]);
364  }
365  }
366  else
367  {
368  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
369  for (int i = 0; i < NCOMPONENTS; ++i,
370  data = (const DataType *)(((const unsigned char *)data) + yres_stride))
371  {
372  if (std::numeric_limits<T>::is_integer)
373  vals[i] = float(*data)/float(std::numeric_limits<T>::max());
374  else
375  vals[i] = float(*data);
376  }
377  }
378  }
379 
381  {
382  UT_ASSERT_P(!READ_ONLY);
383 
384  DataType *data = getPixelFast(pixels, x, y);
385  if (INTERLEAVED || NCOMPONENTS==1)
386  {
387  for (int i = 0; i < NCOMPONENTS; ++i)
388  data[i] = T(0);
389  }
390  else
391  {
392  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
393  for (int i = 0; i < NCOMPONENTS; ++i,
394  data = (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)data) + yres_stride))
395  {
396  *data = T(0);
397  }
398  }
399  }
400 
402  {
403  if (std::numeric_limits<T>::is_integer)
404  return T(val * std::numeric_limits<T>::max());
405  else
406  return T(val);
407  }
408 
409  SYS_FORCE_INLINE void setPixelValueFast(void *pixels, exint x, exint y, const T *vals) const
410  {
411  UT_ASSERT_P(!READ_ONLY);
412 
413  DataType *data = getPixelFast(pixels, x, y);
414  if (INTERLEAVED || NCOMPONENTS==1)
415  {
416  for (int i = 0; i < NCOMPONENTS; ++i)
417  data[i] = vals[i];
418  }
419  else
420  {
421  const exint yres_stride = myRaster->getYres()*myRaster->getStride();
422  for (int i = 0; i < NCOMPONENTS; ++i,
423  data = (DataType *)(((typename SYS_ConstType<unsigned char,READ_ONLY>::type *)data) + yres_stride))
424  {
425  *data = vals[i];
426  }
427  }
428  }
429 
430 private:
431  RasterType *myRaster;
432 };
433 
434 #endif
SYS_FORCE_INLINE DataType * getPixelFast(void *pixels, exint x, exint y) const
Definition: PXL_Raster.h:338
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:352
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:244
UT_ASSERT_COMPILETIME(BRAY_EVENT_MAXFLAGS<=32)
SYS_FORCE_INLINE exint getYres() const
Definition: PXL_Raster.h:295
void * myTextureID
Definition: PXL_Raster.h:256
GLuint id
Definition: glew.h:1679
GLsizeiptr size
Definition: glew.h:1681
GLenum src
Definition: glew.h:2410
UT_StringHolder myColorSpaceLooks
Definition: PXL_Raster.h:246
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:241
exint myRasterOffset
Definition: PXL_Raster.h:247
UT_StringHolder myColorSpaceName
Definition: PXL_Raster.h:245
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
fpreal getColorSpaceGamma() const
Definition: PXL_Raster.h:103
bool myWriteFlag
Definition: PXL_Raster.h:255
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:281
#define PXL_API
Definition: PXL_API.h:10
void * myRaster
Definition: PXL_Raster.h:252
PXL_DataFormat getFormat() const
Definition: PXL_Raster.h:94
int64 myStride
Definition: PXL_Raster.h:249
#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:291
PXL_ColorSpace myColorSpace
Definition: PXL_Raster.h:243
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:253
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: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:300
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:271
float myWhite
Definition: PXL_Raster.h:251
PXL_Packing
Definition: PXL_Common.h:29
bool isInterleaved() const
int myProxyIndex
Definition: PXL_Raster.h:254
OPENVDB_API void initialize()
Global registration of basic types.
Definition: logging.h:318
bool hasDataWindow() const
Definition: PXL_Raster.h:112
int64 myRasterSize
Definition: PXL_Raster.h:250
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:401
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:286
GLsizei stride
Definition: glew.h:1523
exint myYres
Definition: PXL_Raster.h:248
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
double fpreal
Definition: SYS_Types.h:276
const UT_StringHolder & getColorSpaceLooks() const
Definition: PXL_Raster.h:106
int64 getSize() const
Definition: PXL_Raster.h:93
void * myCallerData
Definition: PXL_Raster.h:258
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:242
#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
ImageBuf OIIO_API crop(const ImageBuf &src, ROI roi={}, int nthreads=0)
SYS_ConstType< PXL_Raster, READ_ONLY >::type RasterType
Definition: PXL_Raster.h:270
PXL_StreakComponents
Definition: PXL_Raster.h:29
SYS_FORCE_INLINE void zeroPixelValueFast(void *pixels, exint x, exint y) const
Definition: PXL_Raster.h:380
exint getXres() const
Definition: PXL_Raster.h:89
DataType * getPixel(exint x, exint y, exint comp=0) const
Definition: PXL_Raster.h:316
SYS_FORCE_INLINE PXL_RasterWrapper(RasterType *raster=nullptr)
Definition: PXL_Raster.h:273
SYS_FORCE_INLINE void setPixelValueFast(void *pixels, exint x, exint y, const T *vals) const
Definition: PXL_Raster.h:409
UT_DimRect myDataWindow
Definition: PXL_Raster.h:260
GLintptr offset
Definition: glew.h:1682