4 #ifndef OPENVDB_IO_COMPRESSION_HAS_BEEN_INCLUDED 5 #define OPENVDB_IO_COMPRESSION_HAS_BEEN_INCLUDED 80 template <
typename ValueT,
typename MaskT>
84 static inline bool eq(
const ValueT& a,
const ValueT& b) {
89 const MaskT& valueMask,
const MaskT& childMask,
90 const ValueT* srcBuf,
const ValueT& background)
93 inactiveVal[0] = inactiveVal[1] = background;
94 int numUniqueInactiveVals = 0;
95 for (
typename MaskT::OffIterator it = valueMask.beginOff();
96 numUniqueInactiveVals < 3 && it; ++it)
101 if (childMask.isOn(idx))
continue;
103 const ValueT& val = srcBuf[idx];
104 const bool unique = !(
105 (numUniqueInactiveVals > 0 && MaskCompress::eq(val, inactiveVal[0])) ||
106 (numUniqueInactiveVals > 1 && MaskCompress::eq(val, inactiveVal[1]))
109 if (numUniqueInactiveVals < 2) inactiveVal[numUniqueInactiveVals] = val;
110 ++numUniqueInactiveVals;
116 if (numUniqueInactiveVals == 1) {
117 if (!MaskCompress::eq(inactiveVal[0], background)) {
118 if (MaskCompress::eq(inactiveVal[0],
math::negative(background))) {
124 }
else if (numUniqueInactiveVals == 2) {
126 if (!MaskCompress::eq(inactiveVal[0], background) && !MaskCompress::eq(inactiveVal[1], background)) {
131 }
else if (MaskCompress::eq(inactiveVal[1], background)) {
132 if (MaskCompress::eq(inactiveVal[0],
math::negative(background))) {
143 }
else if (MaskCompress::eq(inactiveVal[0], background)) {
144 if (MaskCompress::eq(inactiveVal[1],
math::negative(background))) {
149 std::swap(inactiveVal[0], inactiveVal[1]);
154 std::swap(inactiveVal[0], inactiveVal[1]);
158 }
else if (numUniqueInactiveVals > 2) {
164 ValueT inactiveVal[2];
175 enum { isReal =
false };
180 enum { isReal =
true };
185 enum { isReal =
true };
191 enum { isReal =
true };
196 enum { isReal =
true };
202 enum { isReal =
true };
207 enum { isReal =
true };
251 const bool seek = data ==
nullptr;
257 if (metadata && seek && hasCompression) {
258 size_t compressedSize = metadata->getCompressedSize(metadataOffset);
259 is.seekg(compressedSize, std::ios_base::cur);
265 is.seekg(
sizeof(T) * count, std::ios_base::cur);
267 is.read(reinterpret_cast<char*>(data),
sizeof(T) * count);
274 readData<std::string>(std::istream& is, std::string* data,
Index count, uint32_t ,
277 for (
Index i = 0; i < count; ++i) {
283 std::string buffer(len+1,
' ');
284 is.read(&buffer[0], len+1);
285 if (data !=
nullptr) data[i].assign(buffer, 0, len);
297 static inline void read(std::istream& is, T* data,
Index count, uint32_t compression,
298 DelayedLoadMetadata* metadata =
nullptr,
size_t metadataOffset =
size_t(0)) {
299 readData(is, data, count, compression, metadata, metadataOffset);
306 static inline void read(std::istream& is, T* data,
Index count, uint32_t compression,
307 DelayedLoadMetadata* metadata =
nullptr,
size_t metadataOffset =
size_t(0)) {
308 if (count < 1)
return;
309 if (data ==
nullptr) {
311 readData<HalfT>(is,
nullptr, count, compression, metadata, metadataOffset);
313 std::vector<HalfT> halfData(count);
314 readData<HalfT>(is,
reinterpret_cast<HalfT*
>(&halfData[0]), count, compression,
315 metadata, metadataOffset);
317 std::copy(halfData.begin(), halfData.end(), data);
330 return zipToStreamSize(reinterpret_cast<const char*>(data),
sizeof(T) * count);
332 return sizeof(T) * count;
340 writeDataSize<std::string>(
const std::string* data,
Index count,
344 for (
Index i = 0; i < count; ++i) {
345 const size_t len = data[i].size();
346 size +=
sizeof(size_t) + (len+1);
367 bloscToStream(os, reinterpret_cast<const char*>(data),
sizeof(T), count);
369 zipToStream(os, reinterpret_cast<const char*>(data),
sizeof(T) * count);
371 os.write(reinterpret_cast<const char*>(data),
sizeof(T) * count);
378 writeData<std::string>(std::ostream& os,
const std::string* data,
Index count,
381 for (
Index i = 0; i < count; ++i) {
382 const size_t len = data[i].size();
384 os.write(data[i].c_str(), len+1);
397 static inline size_t writeSize(
const T* data,
Index count, uint32_t compression) {
400 static inline void write(std::ostream& os,
const T* data,
Index count, uint32_t compression) {
408 static inline size_t writeSize(
const T* data,
Index count, uint32_t compression) {
409 if (count < 1)
return size_t(0);
411 std::vector<HalfT> halfData(count);
412 for (
Index i = 0; i < count; ++i) halfData[i] = RealToHalf<T>::convert(data[i]);
413 return writeDataSize<HalfT>(
reinterpret_cast<const HalfT*
>(&halfData[0]), count, compression);
415 static inline void write(std::ostream& os,
const T* data,
Index count, uint32_t compression) {
416 if (count < 1)
return;
418 std::vector<HalfT> halfData(count);
419 for (
Index i = 0; i < count; ++i) halfData[i] = RealToHalf<T>::convert(data[i]);
420 writeData<HalfT>(os,
reinterpret_cast<const HalfT*
>(&halfData[0]), count, compression);
428 static inline size_t writeSize(
const double* data,
Index count, uint32_t compression)
430 if (count < 1)
return size_t(0);
432 std::vector<HalfT> halfData(count);
433 for (
Index i = 0; i < count; ++i) halfData[i] = RealToHalf<double>::convert(data[i]);
434 return writeDataSize<HalfT>(
reinterpret_cast<const HalfT*
>(&halfData[0]), count, compression);
436 static inline void write(std::ostream& os,
const double* data,
Index count,
437 uint32_t compression)
439 if (count < 1)
return;
441 std::vector<HalfT> halfData(count);
442 for (
Index i = 0; i < count; ++i) halfData[i] = RealToHalf<double>::convert(data[i]);
443 writeData<HalfT>(os,
reinterpret_cast<const HalfT*
>(&halfData[0]), count, compression);
464 template<
typename ValueT,
typename MaskT>
467 const MaskT& valueMask,
bool fromHalf)
474 const bool seek = (destBuf ==
nullptr);
480 uint64_t leafIndex(0);
481 if (seek && meta && meta->delayedLoadMeta()) {
483 meta->gridMetadata().getMetadata<DelayedLoadMetadata>(
"file_delayed_load");
484 leafIndex = meta->leaf();
491 if (seek && !maskCompressed) {
492 is.seekg(1, std::ios_base::cur);
493 }
else if (seek && delayLoadMeta) {
494 metadata = delayLoadMeta->getMask(leafIndex);
495 is.seekg(1, std::ios_base::cur);
497 is.read(reinterpret_cast<char*>(&metadata), 1);
501 ValueT background = zeroVal<ValueT>();
503 background = *
static_cast<const ValueT*
>(bgPtr);
505 ValueT inactiveVal1 = background;
506 ValueT inactiveVal0 =
515 is.seekg(
sizeof(ValueT), std::ios_base::cur);
517 is.read(reinterpret_cast<char*>(&inactiveVal0),
sizeof(ValueT));
522 is.seekg(
sizeof(ValueT), std::ios_base::cur);
524 is.read(reinterpret_cast<char*>(&inactiveVal1),
sizeof(ValueT));
537 is.seekg(selectionMask.memUsage(), std::ios_base::cur);
539 selectionMask.load(is);
543 ValueT* tempBuf = destBuf;
544 std::unique_ptr<ValueT[]> scopedTempBuf;
546 Index tempCount = destCount;
551 tempCount = valueMask.countOn();
552 if (!seek && tempCount != destCount) {
555 scopedTempBuf.reset(
new ValueT[tempCount]);
556 tempBuf = scopedTempBuf.get();
563 is, (seek ?
nullptr : tempBuf), tempCount, compression, delayLoadMeta.get(), leafIndex);
566 is, (seek ?
nullptr : tempBuf), tempCount, compression, delayLoadMeta.get(), leafIndex);
572 if (!seek && maskCompressed && tempCount != destCount) {
576 for (
Index destIdx = 0, tempIdx = 0; destIdx < MaskT::SIZE; ++destIdx) {
577 if (valueMask.isOn(destIdx)) {
579 destBuf[destIdx] = tempBuf[tempIdx];
583 destBuf[destIdx] = (selectionMask.isOn(destIdx) ? inactiveVal1 : inactiveVal0);
590 template<
typename ValueT,
typename MaskT>
593 const MaskT& valueMask, uint8_t maskMetadata,
bool toHalf, uint32_t compress)
595 using NonConstValueT =
typename std::remove_const<ValueT>::type;
599 Index tempCount = srcCount;
600 ValueT* tempBuf = srcBuf;
601 std::unique_ptr<NonConstValueT[]> scopedTempBuf;
607 Index64 onVoxels = valueMask.countOn();
610 scopedTempBuf.reset(
new NonConstValueT[onVoxels]);
611 NonConstValueT* localTempBuf = scopedTempBuf.get();
614 for (
typename MaskT::OnIterator it = valueMask.beginOn(); it; ++it, ++tempCount) {
615 localTempBuf[tempCount] = srcBuf[it.pos()];
618 tempBuf = scopedTempBuf.get();
625 tempBuf, tempCount, compress);
627 return writeDataSize<NonConstValueT>(tempBuf, tempCount, compress);
644 template<
typename ValueT,
typename MaskT>
647 const MaskT& valueMask,
const MaskT& childMask,
bool toHalf)
653 Index tempCount = srcCount;
654 ValueT* tempBuf = srcBuf;
655 std::unique_ptr<ValueT[]> scopedTempBuf;
660 os.write(reinterpret_cast<const char*>(&metadata), 1);
668 const ValueT zero = zeroVal<ValueT>();
669 ValueT background = zero;
671 background = *
static_cast<const ValueT*
>(bgPtr);
675 metadata = maskCompressData.
metadata;
677 os.write(reinterpret_cast<const char*>(&metadata), 1);
685 os.write(reinterpret_cast<const char*>(&maskCompressData.
inactiveVal[0]),
sizeof(ValueT));
688 os.write(reinterpret_cast<const char*>(&maskCompressData.
inactiveVal[1]),
sizeof(ValueT));
693 os.write(reinterpret_cast<const char*>(&truncatedVal),
sizeof(ValueT));
697 os.write(reinterpret_cast<const char*>(&truncatedVal),
sizeof(ValueT));
709 scopedTempBuf.reset(
new ValueT[srcCount]);
710 tempBuf = scopedTempBuf.get();
718 for (
typename MaskT::OnIterator it = valueMask.beginOn(); it; ++it, ++tempCount) {
719 tempBuf[tempCount] = srcBuf[it.pos()];
726 for (
Index srcIdx = 0; srcIdx < srcCount; ++srcIdx) {
727 if (valueMask.isOn(srcIdx)) {
728 tempBuf[tempCount] = srcBuf[srcIdx];
732 selectionMask.setOn(srcIdx);
739 selectionMask.save(os);
748 writeData(os, tempBuf, tempCount, compress);
756 #endif // OPENVDB_IO_COMPRESSION_HAS_BEEN_INCLUDED static void write(std::ostream &os, const T *data, Index count, uint32_t compression)
Definition: Compression.h:400
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
uint64_t Index64
Definition: Types.h:53
static size_t writeSize(const T *data, Index count, uint32_t compression)
Definition: Compression.h:408
Definition: Compression.h:73
internal::half half
Definition: Types.h:29
static void write(std::ostream &os, const T *data, Index count, uint32_t compression)
Definition: Compression.h:415
math::Vec2< math::half > Vec2H
Definition: Types.h:66
static HalfT convert(const Vec3d &val)
Definition: Compression.h:210
Definition: Compression.h:70
Definition: Compression.h:55
Index32 Index
Definition: Types.h:54
Vec3< double > Vec3d
Definition: Vec3.h:665
static HalfT convert(const Vec3s &val)
Definition: Compression.h:204
MaskCompress(const MaskT &valueMask, const MaskT &childMask, const ValueT *srcBuf, const ValueT &background)
Definition: Compression.h:88
RealToHalf and its specializations define a mapping from floating-point data types to analogous half ...
Definition: Compression.h:174
OPENVDB_API std::string compressionToString(uint32_t flags)
Return a string describing the given compression flags.
T HalfT
Definition: Compression.h:176
T negative(const T &val)
Return the unary negation of the given value.
Definition: Math.h:128
Definition: Compression.h:74
Vec3< float > Vec3s
Definition: Vec3.h:664
Vec2< float > Vec2s
Definition: Vec2.h:532
static HalfT convert(float val)
Definition: Compression.h:182
Definition: Compression.h:293
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
Definition: Compression.h:466
Definition: Compression.h:393
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
Definition: Compression.h:72
T truncateRealToHalf(const T &val)
Return the given value truncated to 16-bit float precision.
Definition: Compression.h:217
size_t writeCompressedValuesSize(ValueT *srcBuf, Index srcCount, const MaskT &valueMask, uint8_t maskMetadata, bool toHalf, uint32_t compress)
Definition: Compression.h:592
typename RealToHalf< T >::HalfT HalfT
Definition: Compression.h:407
OPENVDB_API size_t bloscToStreamSize(const char *data, size_t valSize, size_t numVals)
static size_t writeSize(const T *data, Index count, uint32_t compression)
Definition: Compression.h:397
static HalfT convert(const Vec2s &val)
Definition: Compression.h:193
Definition: Compression.h:57
static void read(std::istream &is, T *data, Index count, uint32_t compression, DelayedLoadMetadata *metadata=nullptr, size_t metadataOffset=size_t(0))
Definition: Compression.h:306
static bool eq(const ValueT &a, const ValueT &b)
Definition: Compression.h:84
Definition: Exceptions.h:13
size_t writeDataSize(const T *data, Index count, uint32_t compression)
Definition: Compression.h:325
OPENVDB_API void bloscToStream(std::ostream &, const char *data, size_t valSize, size_t numVals)
static HalfT convert(const Vec2d &val)
Definition: Compression.h:199
ValueT inactiveVal[2]
Definition: Compression.h:164
Definition: Compression.h:81
OPENVDB_API SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme...
static void read(std::istream &is, T *data, Index count, uint32_t compression, DelayedLoadMetadata *metadata=nullptr, size_t metadataOffset=size_t(0))
Definition: Compression.h:297
uint32_t Index32
Definition: Types.h:52
Definition: Compression.h:54
int8_t metadata
Definition: Compression.h:163
OPENVDB_API const void * getGridBackgroundValuePtr(std::ios_base &)
Return a pointer to the background value of the grid currently being read from or written to the give...
Definition: Compression.h:71
void writeData(std::ostream &os, const T *data, Index count, uint32_t compression)
Definition: Compression.h:364
Vec2< double > Vec2d
Definition: Vec2.h:533
Definition: version.h.in:262
static HalfT convert(const T &val)
Definition: Compression.h:177
void readData(std::istream &is, T *data, Index count, uint32_t compression, DelayedLoadMetadata *metadata=nullptr, size_t metadataOffset=size_t(0))
Read data from a stream.
Definition: Compression.h:248
OPENVDB_API void unzipFromStream(std::istream &, char *data, size_t numBytes)
OPENVDB_API size_t zipToStreamSize(const char *data, size_t numBytes)
Definition: Compression.h:76
OPENVDB_API void zipToStream(std::ostream &, const char *data, size_t numBytes)
OPENVDB_API uint32_t getFormatVersion(std::ios_base &)
Return the file format version number associated with the given input stream.
math::Vec3< math::half > Vec3H
Definition: Types.h:75
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
static HalfT convert(double val)
Definition: Compression.h:188
typename RealToHalf< T >::HalfT HalfT
Definition: Compression.h:305
Definition: Compression.h:75
OPENVDB_API uint32_t getDataCompression(std::ios_base &)
Return a bitwise OR of compression option flags (COMPRESS_ZIP, COMPRESS_ACTIVE_MASK, etc.) specifying whether and how input data is compressed or output data should be compressed.
Definition: Compression.h:56
OPENVDB_API void bloscFromStream(std::istream &, char *data, size_t numBytes)
void writeCompressedValues(std::ostream &os, ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf)
Definition: Compression.h:646
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218