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_Vector2.h>
19 #include <UT/UT_Vector3.h>
20 #include <UT/UT_Vector4.h>
21 #include <algorithm>
22 
23 class PXL_Raster;
24 class UT_JSONWriter;
25 class UT_JSONParser;
26 
27 /// PXL_Raster crazily stores 2 channel rasters as 3 channel rasters. All
28 /// assumptions about 2 channel rasters are incorrect. So, we have to use our
29 /// own class here.
30 /// When we sweep PXL_Raster to have proper semantics, we might be able to
31 /// switch back.
33 {
34 public:
35  /// Construct a raster. It's possible to have a reference to external
36  /// raster data instead of allocating a raster.
38  : myRaster(nullptr)
39  {
40  setRes(0, 0, PACK_RGB, PXL_FLOAT32);
41  }
42  BRAY_Raster(exint xres, exint yres,
43  PXL_Packing pack, PXL_DataFormat fmt,
44  const void *raster = nullptr)
45  : myRaster(reinterpret_cast<const char *>(raster))
46  {
47  setRes(xres, yres, pack, fmt);
48  if (!myRaster)
49  allocate();
50  }
52  {
53  freeRaster();
54  }
55  /// Set up a PXL_Raster to reference the data in this raster. If you need
56  /// to write to the PXL_Raster, @c for_write should be true. This will
57  /// assert that @c isWritable() is also true.
58  void setupReference(PXL_Raster &rp, bool for_write) const;
59 
60  /// Clear storage
61  void freeRaster();
62  void resize(exint xres, exint yres,
63  PXL_Packing pack, PXL_DataFormat fmt)
64  {
65  if (myXres == xres && myYres == yres
66  && myPacking == pack && myDataFormat == fmt)
67  {
68  return;
69  }
70  freeRaster();
71  setRes(xres, yres, pack, fmt);
72  allocate();
73  }
74  void createFrom(const BRAY_Raster &rp)
75  {
76  resize(rp.xres(), rp.yres(), rp.packing(), rp.dataFormat());
77  if (isValid())
78  copyDataFrom(rp);
79  }
81  {
82  UT_ASSERT(!rp.xres() && !rp.yres() && !xres() && !yres());
83  myNChannels = rp.myNChannels;
84  myBPChan = rp.myBPChan;
85  myBPPixel = rp.myBPPixel;
86  myBPScan = rp.myBPScan;
87  myDataFormat = rp.myDataFormat;
88  myPacking = rp.myPacking;
89  }
90  void copyDataFrom(const BRAY_Raster &rp)
91  {
92  if (&rp != this)
93  {
94  UT_ASSERT((!rp.isValid() && !isValid())
95  || (myBuffer && imageBytes() == rp.imageBytes()));
96  memcpy(myBuffer.get(), rp.raster(), rp.imageBytes());
97  }
98  }
99  void copyDataFrom(const PXL_Raster &rp);
100  void zeroPixels()
101  {
102  UT_ASSERT(myBuffer.get() || !imageBytes());
103  std::fill(myBuffer.get(), myBuffer.get()+imageBytes(), 0);
104  }
105 
106  /// Read access to the raster
107  const void *raster() const { return myRaster; }
108  /// Read access to the raster as a given type
109  template <typename T>
110  const T *rasterAs() const
111  {
113  return reinterpret_cast<const T *>(myRaster);
114  }
115  /// Write access to the raster
116  void *writeRaster()
117  {
118  UT_ASSERT(myBuffer);
119  return myBuffer.get();
120  }
121  /// Write access to the raster as a given type
122  template <typename T>
124  {
125  UT_ASSERT(myBuffer);
127  return reinterpret_cast<T *>(myBuffer.get());
128  }
129 
130  /// Read access to a given scanline
131  const void *scanline(exint y) const { return myRaster+byteOffset(y); }
132 
133  /// Read access to a given pixel
134  const void *pixel(exint x, exint y) const
135  { return myRaster + byteOffset(x, y); }
136  /// Read access to a given pixel as a given type
137  template <typename T>
138  const T *pixelAs(exint x, exint y) const
139  {
141  return reinterpret_cast<const T *>(pixel(x, y));
142  }
143 
144  /// Write access to a given pixel
145  void *writePixel(exint x, exint y) const
146  {
147  UT_ASSERT(myBuffer.get());
148  return myBuffer.get() + byteOffset(x, y);
149  }
150  /// Write access to a given pixel as a given type
151  template <typename T>
153  {
155  return reinterpret_cast<T *>(writePixel(x, y));
156  }
157 
158 #define VECTOR_ACCESS(VEC, NAME) \
159  const VEC *rasterAs##NAME() const { \
160  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
161  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
162  return reinterpret_cast<const VEC *>(myRaster); \
163  } \
164  VEC *writeRasterAs##NAME() const { \
165  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
166  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
167  UT_ASSERT(myBuffer.get()); \
168  return reinterpret_cast<VEC *>(myBuffer.get()); \
169  } \
170  const VEC *pixelAs##NAME(exint x, exint y) const { \
171  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
172  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
173  return reinterpret_cast<const VEC *>(pixel(x, y)); \
174  } \
175  VEC *writePixelAs##NAME(exint x, exint y) const { \
176  UT_ASSERT_P(VEC::tuple_size == nchannels()); \
177  UT_ASSERT_P(PXL_DataFormatResolver<VEC::value_type>::format == myDataFormat); \
178  UT_ASSERT(myBuffer.get()); \
179  return reinterpret_cast<VEC *>(writePixel(x, y)); \
180  } \
181  /* end macro */
188  VECTOR_ACCESS(UT_Vector2H, Vec2h)
189  VECTOR_ACCESS(UT_Vector3H, Vec3h)
190  VECTOR_ACCESS(UT_Vector4H, Vec4h)
191 #undef VECTOR_ACCESS
192 
193  bool isValid() const { return myRaster && myXres > 0 && myYres > 0; }
194  bool isWritable() const { return myBuffer.get() != nullptr; }
195 
196  PXL_Packing packing() const { return myPacking; }
197  PXL_DataFormat dataFormat() const { return myDataFormat; }
198  exint npixels() const { return myXres*myYres; }
199  exint nchannels() const { return myNChannels; }
200  exint xres() const { return myXres; }
201  exint yres() const { return myYres; }
202  exint pixelBytes() const { return myBPPixel; }
203  exint scanBytes() const { return myBPScan; }
204  exint imageBytes() const { return myBPScan*myYres; }
205 
207  {
208  UT_ASSERT(!myRaster && myXres == 0 && myYres == 0);
209  setRes(myXres, myYres, pack, fmt);
210  }
211 
212  void dump() const;
213  void dump(UT_JSONWriter &w) const;
214 
215  bool checkpoint(UT_JSONWriter &w) const;
216  bool loadCheckpoint(UT_JSONParser &p);
217 
218 private:
219  void allocate();
220  exint byteOffset(exint y) const
221  {
222  return myBPScan * y;
223  }
224  exint byteOffset(exint x, exint y) const
225  {
226  return myBPPixel * (y*myXres + x);
227  }
228  void setRes(exint xres, exint yres,
229  PXL_Packing pack, PXL_DataFormat fmt)
230  {
231  myXres = xres;
232  myYres = yres;
233  myDataFormat = fmt;
234  myPacking = pack;
235  // NOTE: We cannot call PXLpackingComponents() since it returns 3 for
236  // PACK_DUAL.
237  switch (pack)
238  {
239  case PACK_SINGLE:
240  myNChannels = 1;
241  break;
242  case PACK_DUAL:
243  myNChannels = 2;
244  break;
245  case PACK_RGB:
246  myNChannels = 3;
247  break;
248  case PACK_RGBA:
249  myNChannels = 4;
250  break;
251  default:
252  UT_ASSERT(0 && "Invalid usage");
253  myNChannels = 0;
254  break;
255  }
256  myBPChan = PXLformatDepth(fmt);
257  myBPPixel = myBPChan * myNChannels;
258  myBPScan = myBPPixel * myXres;
259  }
260  UT_UniquePtr<char[]> myBuffer;
261  const char *myRaster; // Raster
262  exint myXres;
263  exint myYres;
264  exint myNChannels; // Number of channels
265  exint myBPChan; // Bytes per channel
266  exint myBPPixel; // Bytes per pixel
267  exint myBPScan; // Bytes per scanline
268  PXL_DataFormat myDataFormat;
269  PXL_Packing myPacking;
270 };
271 
272 #endif
273 
BRAY_Raster(exint xres, exint yres, PXL_Packing pack, PXL_DataFormat fmt, const void *raster=nullptr)
Definition: BRAY_Raster.h:42
void resize(exint xres, exint yres, PXL_Packing pack, PXL_DataFormat fmt)
Definition: BRAY_Raster.h:62
void copyDataFrom(const BRAY_Raster &rp)
Definition: BRAY_Raster.h:90
T * writeRasterAs()
Write access to the raster as a given type.
Definition: BRAY_Raster.h:123
math::Vec4< float > Vec4f
Definition: Types.h:67
exint pixelBytes() const
Definition: BRAY_Raster.h:202
void copyPropertiesFrom(const BRAY_Raster &rp)
Definition: BRAY_Raster.h:80
exint scanBytes() const
Definition: BRAY_Raster.h:203
ImageBuf OIIO_API fill(cspan< float > values, ROI roi, int nthreads=0)
void * writePixel(exint x, exint y) const
Write access to a given pixel.
Definition: BRAY_Raster.h:145
int64 exint
Definition: SYS_Types.h:125
const T * rasterAs() const
Read access to the raster as a given type.
Definition: BRAY_Raster.h:110
PXL_DataFormat dataFormat() const
Definition: BRAY_Raster.h:197
JSON reader class which handles parsing of JSON or bJSON files.
Definition: UT_JSONParser.h:76
#define VECTOR_ACCESS(VEC, NAME)
Definition: BRAY_Raster.h:158
Class which writes ASCII or binary JSON streams.
Definition: UT_JSONWriter.h:34
PXL_Packing packing() const
Definition: BRAY_Raster.h:196
4D Vector class.
Definition: UT_Vector4.h:166
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
void createFrom(const BRAY_Raster &rp)
Definition: BRAY_Raster.h:74
bool isValid() const
Definition: BRAY_Raster.h:193
exint xres() const
Definition: BRAY_Raster.h:200
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
void zeroPixels()
Definition: BRAY_Raster.h:100
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1890
void * writeRaster()
Write access to the raster.
Definition: BRAY_Raster.h:116
PXL_Packing
Definition: PXL_Common.h:30
ImageBuf OIIO_API resize(const ImageBuf &src, string_view filtername="", float filterwidth=0.0f, ROI roi={}, int nthreads=0)
PXL_DataFormat
Definition: PXL_Common.h:20
exint npixels() const
Definition: BRAY_Raster.h:198
T * writePixelAs(exint x, exint y) const
Write access to a given pixel as a given type.
Definition: BRAY_Raster.h:152
const T * pixelAs(exint x, exint y) const
Read access to a given pixel as a given type.
Definition: BRAY_Raster.h:138
exint nchannels() const
Definition: BRAY_Raster.h:199
GLfloat GLfloat p
Definition: glew.h:16321
Allocator::value_type * allocate(Allocator &alloc, std::size_t n)
Definition: format.h:282
const void * raster() const
Read access to the raster.
Definition: BRAY_Raster.h:107
exint imageBytes() const
Definition: BRAY_Raster.h:204
exint yres() const
Definition: BRAY_Raster.h:201
const void * pixel(exint x, exint y) const
Read access to a given pixel.
Definition: BRAY_Raster.h:134
bool isWritable() const
Definition: BRAY_Raster.h:194
#define BRAY_API
Definition: BRAY_API.h:12
int PXLformatDepth(PXL_DataFormat d)
Definition: PXL_Common.h:115
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
#define const
Definition: zconf.h:214
const void * scanline(exint y) const
Read access to a given scanline.
Definition: BRAY_Raster.h:131
math::Vec2< float > Vec2f
Definition: Types.h:43
void setStorage(PXL_Packing pack, PXL_DataFormat fmt)
Definition: BRAY_Raster.h:206
math::Vec3< float > Vec3f
Definition: Types.h:52