HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BRAY_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: BRAY_Raster.h (BRAY Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __BRAY_Raster__
12 #define __BRAY_Raster__
13 
14 #include "BRAY_API.h"
15 #include <PXL/PXL_Common.h>
16 #include <UT/UT_Assert.h>
17 #include <UT/UT_UniquePtr.h>
18 #include <UT/UT_Rect.h>
19 #include <UT/UT_Vector2.h>
20 #include <UT/UT_Vector3.h>
21 #include <UT/UT_Vector4.h>
22 #include <SYS/SYS_TypeDecorate.h>
23 #include <algorithm>
24 
25 class PXL_Raster;
26 class UT_JSONWriter;
27 class UT_JSONParser;
28 
29 /// Simple raster container.
31 {
32 public:
33  /// Construct a raster. It's possible to have a reference to external
34  /// raster data instead of allocating a raster.
36  : myRaster(nullptr)
37  {
38  setRes(0, 0, PACK_RGB, PXL_FLOAT32);
39  }
40  BRAY_Raster(exint xres, exint yres,
41  PXL_Packing pack, PXL_DataFormat fmt,
42  const void *raster = nullptr)
43  : myRaster(reinterpret_cast<const char *>(raster))
44  {
45  setRes(xres, yres, pack, fmt);
46  if (!myRaster)
47  allocate();
48  }
50  {
51  freeRaster();
52  }
53  /// Set up a PXL_Raster to reference the data in this raster. If you need
54  /// to write to the PXL_Raster, @c for_write should be true. This will
55  /// assert that @c isWritable() is also true.
56  void setupReference(PXL_Raster &rp, bool for_write) const;
57 
58  static int64 memoryUsed();
59  static int64 peakMemoryUsed();
60 
61  /// Clear storage
62  void freeRaster();
63  void resize(exint xres, exint yres,
64  PXL_Packing pack, PXL_DataFormat fmt)
65  {
66  if (myXres == xres && myYres == yres
67  && myPacking == pack && myDataFormat == fmt)
68  {
69  return;
70  }
71  freeRaster();
72  setRes(xres, yres, pack, fmt);
73  allocate();
74  }
75  void createFrom(const BRAY_Raster &rp)
76  {
77  resize(rp.xres(), rp.yres(), rp.packing(), rp.dataFormat());
78  if (isValid())
79  copyDataFrom(rp);
80  }
82  {
83  UT_ASSERT(!rp.xres() && !rp.yres() && !xres() && !yres());
84  myNChannels = rp.myNChannels;
85  myBPChan = rp.myBPChan;
86  myBPPixel = rp.myBPPixel;
87  myBPScan = rp.myBPScan;
88  myDataFormat = rp.myDataFormat;
89  myPacking = rp.myPacking;
90  }
91  void copyDataFrom(const BRAY_Raster &rp)
92  {
93  if (&rp != this)
94  {
95  UT_ASSERT((!rp.isValid() && !isValid())
96  || (myBuffer && imageBytes() == rp.imageBytes()));
97  memcpy(myBuffer.get(), rp.raster(), rp.imageBytes());
98  }
99  }
100  void copyDataFrom(const PXL_Raster &rp);
101  void zeroPixels()
102  {
103  UT_ASSERT(myBuffer.get() || !imageBytes());
104 
105  //std::fill(myBuffer.get(), myBuffer.get()+imageBytes(), 0);
106  memset(myBuffer.get(), 0, imageBytes()); // should be faster than fill
107  }
108 
109  void clearToFloat(float val)
110  {
111  if (val == 0.f)
112  {
113  zeroPixels();
114  return;
115  }
116 
117 #define CLEAR_TO_FLOAT(FORMAT_T) \
118  { \
119  using T = PXL_TypeResolver< FORMAT_T >::T; \
120  T* buffer = writeRasterAs<T>(); \
121  T tval = (T)val; \
122  std::fill( \
123  buffer, \
124  buffer + (myXres * myYres * myNChannels), \
125  tval); \
126  } \
127  /* end macro */
128 
129  switch(myDataFormat)
130  {
131  case PXL_INT32: CLEAR_TO_FLOAT(PXL_INT32); break;
132  case PXL_FLOAT32: CLEAR_TO_FLOAT(PXL_FLOAT32); break;
133  default:
134  UT_ASSERT(0); // other types TODO, probably require PXL_Pixel
135  break;
136  }
137 
138 #undef CLEAR_TO_FLOAT
139  }
140 
141  /// Read access to the raster
142  const void *raster() const { return myRaster; }
143  /// Read access to the raster as a given type
144  template <typename T>
145  const T *rasterAs() const
146  {
148  return reinterpret_cast<const T *>(myRaster);
149  }
150  /// Write access to the raster
151  void *writeRaster()
152  {
153  UT_ASSERT(myBuffer);
154  return myBuffer.get();
155  }
156  /// Write access to the raster as a given type
157  template <typename T>
159  {
160  UT_ASSERT(myBuffer);
162  return reinterpret_cast<T *>(myBuffer.get());
163  }
164 
165  /// Read access to a given scanline
166  const void *scanline(exint y) const { return myRaster+byteOffset(y); }
167 
168  /// Read access to a given pixel
169  const void *pixel(exint x, exint y) const
170  { return myRaster + byteOffset(x, y); }
171  /// Read access to a given pixel as a given type
172  template <typename T>
173  const T *pixelAs(exint x, exint y) const
174  {
176  return reinterpret_cast<const T *>(pixel(x, y));
177  }
178 
179  /// Write access to a given pixel
180  void *writePixel(exint x, exint y) const
181  {
182  UT_ASSERT(myBuffer.get());
183  return myBuffer.get() + byteOffset(x, y);
184  }
185  /// Write access to a given pixel as a given type
186  template <typename T>
188  {
190  return reinterpret_cast<T *>(writePixel(x, y));
191  }
192 
193 #define VECTOR_ACCESS(VEC, NAME) \
194  const VEC *rasterAs##NAME() const { \
195  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
196  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
197  return reinterpret_cast<const VEC *>(myRaster); \
198  } \
199  VEC *writeRasterAs##NAME() const { \
200  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
201  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
202  UT_ASSERT(myBuffer.get()); \
203  return reinterpret_cast<VEC *>(myBuffer.get()); \
204  } \
205  const VEC *pixelAs##NAME(exint x, exint y) const { \
206  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
207  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
208  return reinterpret_cast<const VEC *>(pixel(x, y)); \
209  } \
210  VEC *writePixelAs##NAME(exint x, exint y) const { \
211  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
212  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
213  UT_ASSERT(myBuffer.get()); \
214  return reinterpret_cast<VEC *>(writePixel(x, y)); \
215  } \
216  /* end macro */
223  VECTOR_ACCESS(UT_Vector2H, Vec2h)
224  VECTOR_ACCESS(UT_Vector3H, Vec3h)
225  VECTOR_ACCESS(UT_Vector4H, Vec4h)
226 #undef VECTOR_ACCESS
227 
228  bool isValid() const { return myRaster && myXres > 0 && myYres > 0; }
229  bool isWritable() const { return myBuffer.get() != nullptr; }
230 
231  PXL_Packing packing() const { return myPacking; }
232  PXL_DataFormat dataFormat() const { return myDataFormat; }
233  exint npixels() const { return myXres*myYres; }
234  exint nchannels() const { return myNChannels; }
235  exint xres() const { return myXres; }
236  exint yres() const { return myYres; }
237  exint pixelBytes() const { return myBPPixel; }
238  exint scanBytes() const { return myBPScan; }
239  exint imageBytes() const { return myBPScan*myYres; }
240 
242  {
243  UT_ASSERT(!myRaster && myXres == 0 && myYres == 0);
244  setRes(myXres, myYres, pack, fmt);
245  }
246 
247  void dump() const;
248  void dump(UT_JSONWriter &w) const;
249 
250  bool checkpoint(UT_JSONWriter &w) const;
251  bool loadCheckpoint(UT_JSONParser &p);
252 
253  /// Find the window where non-zero data exists
254  UT_InclusiveRect findDataWindow() const;
255 
257  {
258  return myYres*myBPScan;
259  }
260 
261 private:
262  void allocate();
263  exint byteOffset(exint y) const
264  {
265  return myBPScan * y;
266  }
267  exint byteOffset(exint x, exint y) const
268  {
269  return myBPPixel * (y*myXres + x);
270  }
271  void setRes(exint xres, exint yres,
272  PXL_Packing pack, PXL_DataFormat fmt)
273  {
274  UT_ASSERT(pack != PACK_DUAL);
275  myXres = xres;
276  myYres = yres;
277  myDataFormat = fmt;
278  myPacking = pack;
279  myNChannels = PXLpackingComponents(pack);
280  myBPChan = PXLformatDepth(fmt);
281  myBPPixel = myBPChan * myNChannels;
282  myBPScan = myBPPixel * myXres;
283  }
284  UT_UniquePtr<char[]> myBuffer;
285  const char *myRaster; // Raster
286  exint myXres;
287  exint myYres;
288  exint myNChannels; // Number of channels
289  exint myBPChan; // Bytes per channel
290  exint myBPPixel; // Bytes per pixel
291  exint myBPScan; // Bytes per scanline
292  PXL_DataFormat myDataFormat;
293  PXL_Packing myPacking;
294 };
295 
297 
298 #endif
299 
BRAY_Raster(exint xres, exint yres, PXL_Packing pack, PXL_DataFormat fmt, const void *raster=nullptr)
Definition: BRAY_Raster.h:40
void resize(exint xres, exint yres, PXL_Packing pack, PXL_DataFormat fmt)
Definition: BRAY_Raster.h:63
int PXLpackingComponents(PXL_Packing p)
Definition: PXL_Common.h:126
void copyDataFrom(const BRAY_Raster &rp)
Definition: BRAY_Raster.h:91
T * writeRasterAs()
Write access to the raster as a given type.
Definition: BRAY_Raster.h:158
math::Vec4< float > Vec4f
Definition: Types.h:89
Simple raster container.
Definition: BRAY_Raster.h:30
exint pixelBytes() const
Definition: BRAY_Raster.h:237
Definition: ImathVec.h:32
void copyPropertiesFrom(const BRAY_Raster &rp)
Definition: BRAY_Raster.h:81
exint scanBytes() const
Definition: BRAY_Raster.h:238
void * writePixel(exint x, exint y) const
Write access to a given pixel.
Definition: BRAY_Raster.h:180
int64 getMemoryUsage() const
Definition: BRAY_Raster.h:256
#define CLEAR_TO_FLOAT(FORMAT_T)
int64 exint
Definition: SYS_Types.h:125
const T * rasterAs() const
Read access to the raster as a given type.
Definition: BRAY_Raster.h:145
PXL_DataFormat dataFormat() const
Definition: BRAY_Raster.h:232
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:87
#define VECTOR_ACCESS(VEC, NAME)
Definition: BRAY_Raster.h:193
GLint y
Definition: glcorearb.h:103
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:37
PXL_Packing packing() const
Definition: BRAY_Raster.h:231
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
void createFrom(const BRAY_Raster &rp)
Definition: BRAY_Raster.h:75
bool isValid() const
Definition: BRAY_Raster.h:228
GLfloat f
Definition: glcorearb.h:1926
exint xres() const
Definition: BRAY_Raster.h:235
void zeroPixels()
Definition: BRAY_Raster.h:101
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
void * writeRaster()
Write access to the raster.
Definition: BRAY_Raster.h:151
PXL_Packing
Definition: PXL_Common.h:32
HUSD_API const char * raster()
long long int64
Definition: SYS_Types.h:116
PXL_DataFormat
Definition: PXL_Common.h:20
SYS_DECLARE_LEGACY_TR(GU_Detail)
exint npixels() const
Definition: BRAY_Raster.h:233
T * writePixelAs(exint x, exint y) const
Write access to a given pixel as a given type.
Definition: BRAY_Raster.h:187
GLint GLenum GLint x
Definition: glcorearb.h:409
const T * pixelAs(exint x, exint y) const
Read access to a given pixel as a given type.
Definition: BRAY_Raster.h:173
exint nchannels() const
Definition: BRAY_Raster.h:234
const void * raster() const
Read access to the raster.
Definition: BRAY_Raster.h:142
exint imageBytes() const
Definition: BRAY_Raster.h:239
ImageBuf OIIO_API resize(const ImageBuf &src, string_view filtername="", float filterwidth=0.0f, ROI roi={}, int nthreads=0)
exint yres() const
Definition: BRAY_Raster.h:236
Base Integer Rectangle class.
Definition: ImathVec.h:31
const void * pixel(exint x, exint y) const
Read access to a given pixel.
Definition: BRAY_Raster.h:169
bool isWritable() const
Definition: BRAY_Raster.h:229
void clearToFloat(float val)
Definition: BRAY_Raster.h:109
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define BRAY_API
Definition: BRAY_API.h:12
int PXLformatDepth(PXL_DataFormat d)
Definition: PXL_Common.h:123
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
#define const
Definition: zconf.h:214
const void * scanline(exint y) const
Read access to a given scanline.
Definition: BRAY_Raster.h:166
Definition: ImathVec.h:33
math::Vec2< float > Vec2f
Definition: Types.h:65
void setStorage(PXL_Packing pack, PXL_DataFormat fmt)
Definition: BRAY_Raster.h:241
math::Vec3< float > Vec3f
Definition: Types.h:74