OpenVDB 13.2.0
Loading...
Searching...
No Matches
PointDataIO.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED
5#define OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED
6
7#include <openvdb/io/Compression.h> // for io::readCompressedValues(), etc
10
11
12namespace openvdb {
14namespace OPENVDB_VERSION_NAME {
15
16
17////////////////////////////////////////
18
19
20namespace io
21{
22
23/// @brief openvdb::io::readCompressedValues specialized on PointDataIndex32 arrays to
24/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit
25template<>
26inline void
27readCompressedValues( std::istream& is, PointDataIndex32* destBuf, Index destCount,
28 const util::NodeMask<3>& /*valueMask*/, bool /*fromHalf*/,
29 const PointDataIndex32* /*background*/)
30{
32
33 const bool seek = destBuf == nullptr;
34
35 const size_t destBytes = destCount*sizeof(PointDataIndex32);
36 const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
37 if (destBytes >= maximumBytes) {
38 OPENVDB_THROW(openvdb::IoError, "Cannot read more than " <<
39 maximumBytes << " bytes in voxel values.")
40 }
41
42 uint16_t bytes16;
43
45
46 if (seek && meta) {
47 // buffer size temporarily stored in the StreamMetadata pass
48 // to avoid having to perform an expensive disk read for 2-bytes
49 bytes16 = static_cast<uint16_t>(meta->pass());
50 // seek over size of the compressed buffer
51 is.seekg(sizeof(uint16_t), std::ios_base::cur);
52 }
53 else {
54 // otherwise read from disk
55 is.read(reinterpret_cast<char*>(&bytes16), sizeof(uint16_t));
56 }
57
58 if (bytes16 == std::numeric_limits<uint16_t>::max()) {
59 // read or seek uncompressed data
60 if (seek) {
61 is.seekg(destBytes, std::ios_base::cur);
62 }
63 else {
64 is.read(reinterpret_cast<char*>(destBuf), destBytes);
65 }
66 }
67 else {
68 // read or seek uncompressed data
69 if (seek) {
70 is.seekg(int(bytes16), std::ios_base::cur);
71 }
72 else {
73 // decompress into the destination buffer
74 std::unique_ptr<char[]> bloscBuffer(new char[int(bytes16)]);
75 is.read(bloscBuffer.get(), bytes16);
76 std::unique_ptr<char[]> buffer = bloscDecompress( bloscBuffer.get(),
77 destBytes,
78 /*resize=*/false);
79 std::memcpy(destBuf, buffer.get(), destBytes);
80 }
81 }
82}
83
84/// @brief openvdb::io::writeCompressedValues specialized on PointDataIndex32 arrays to
85/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit
86template<>
87inline void
88writeCompressedValues( std::ostream& os, const PointDataIndex32* srcBuf, Index srcCount,
89 const util::NodeMask<3>& /*valueMask*/,
90 const util::NodeMask<3>& /*childMask*/, bool /*toHalf*/,
91 const PointDataIndex32* /*background*/)
92{
94
95 const size_t srcBytes = srcCount*sizeof(PointDataIndex32);
96 const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
97 if (srcBytes >= maximumBytes) {
98 OPENVDB_THROW(openvdb::IoError, "Cannot write more than " <<
99 maximumBytes << " bytes in voxel values.")
100 }
101
102 const char* charBuffer = reinterpret_cast<const char*>(srcBuf);
103
104 size_t compressedBytes;
105 std::unique_ptr<char[]> buffer = bloscCompress( charBuffer, srcBytes,
106 compressedBytes, /*resize=*/false);
107
108 if (compressedBytes > 0) {
109 auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer
110 os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
111 os.write(reinterpret_cast<const char*>(buffer.get()), compressedBytes);
112 }
113 else {
114 auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed
115 os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
116 os.write(reinterpret_cast<const char*>(srcBuf), srcBytes);
117 }
118}
119
120template <typename T>
121inline void
122writeCompressedValuesSize(std::ostream& os, const T* srcBuf, Index srcCount)
123{
125
126 const size_t srcBytes = srcCount*sizeof(T);
127 const size_t maximumBytes = std::numeric_limits<uint16_t>::max();
128 if (srcBytes >= maximumBytes) {
129 OPENVDB_THROW(openvdb::IoError, "Cannot write more than " <<
130 maximumBytes << " bytes in voxel values.")
131 }
132
133 const char* charBuffer = reinterpret_cast<const char*>(srcBuf);
134
135 // calculate voxel buffer size after compression
136 size_t compressedBytes = bloscCompressedSize(charBuffer, srcBytes);
137
138 if (compressedBytes > 0) {
139 auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer
140 os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
141 }
142 else {
143 auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed
144 os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t));
145 }
146}
147
148} // namespace io
149
150
151} // namespace OPENVDB_VERSION_NAME
152} // namespace openvdb
153
154#endif // OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED
Convenience wrappers to using Blosc and reading and writing of Paged data.
Definition Exceptions.h:58
SharedPtr< StreamMetadata > Ptr
Definition io.h:33
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation.
Definition NodeMasks.h:309
size_t bloscCompressedSize(const char *buffer, const size_t uncompressedBytes)
Convenience wrapper to retrieve the compressed size of buffer when compressed.
void bloscCompress(char *compressedBuffer, size_t &compressedBytes, const size_t bufferBytes, const char *uncompressedBuffer, const size_t uncompressedBytes)
Compress into the supplied buffer.
void bloscDecompress(char *uncompressedBuffer, const size_t expectedBytes, const size_t bufferBytes, const char *compressedBuffer)
Decompress into the supplied buffer. Will throw if decompression fails or uncompressed buffer has ins...
void writeCompressedValues(std::ostream &os, const ValueT *srcBuf, Index srcCount, const MaskT &valueMask, const MaskT &childMask, bool toHalf, const ValueT *background=nullptr)
Definition Compression.h:626
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf, const ValueT *background=nullptr)
Definition Compression.h:454
SharedPtr< StreamMetadata > getStreamMetadataPtr(std::ios_base &)
Return a shared pointer to an object that stores metadata (file format, compression scheme,...
size_t writeCompressedValuesSize(ValueT *srcBuf, Index srcCount, const MaskT &valueMask, uint8_t maskMetadata, bool toHalf, uint32_t compress)
Definition Compression.h:571
Index32 Index
Definition Types.h:34
PointIndex< Index32, 1 > PointDataIndex32
Definition Types.h:162
Definition Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:286