|
| 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 | + |
| 8 | +namespace openvdb { |
| 9 | +OPENVDB_USE_VERSION_NAMESPACE |
| 10 | +namespace OPENVDB_VERSION_NAME { |
| 11 | + |
| 12 | + |
| 13 | +//////////////////////////////////////// |
| 14 | + |
| 15 | + |
| 16 | +namespace io |
| 17 | +{ |
| 18 | + |
| 19 | +/// @brief openvdb::io::readCompressedValues specialized on PointDataIndex32 arrays to |
| 20 | +/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit |
| 21 | +template<> |
| 22 | +inline void |
| 23 | +readCompressedValues( std::istream& is, PointDataIndex32* destBuf, Index destCount, |
| 24 | + const util::NodeMask<3>& /*valueMask*/, bool /*fromHalf*/) |
| 25 | +{ |
| 26 | + using compression::bloscDecompress; |
| 27 | + |
| 28 | + const bool seek = destBuf == nullptr; |
| 29 | + |
| 30 | + const size_t destBytes = destCount*sizeof(PointDataIndex32); |
| 31 | + const size_t maximumBytes = std::numeric_limits<uint16_t>::max(); |
| 32 | + if (destBytes >= maximumBytes) { |
| 33 | + OPENVDB_THROW(openvdb::IoError, "Cannot read more than " << |
| 34 | + maximumBytes << " bytes in voxel values.") |
| 35 | + } |
| 36 | + |
| 37 | + uint16_t bytes16; |
| 38 | + |
| 39 | + const io::StreamMetadata::Ptr meta = io::getStreamMetadataPtr(is); |
| 40 | + |
| 41 | + if (seek && meta) { |
| 42 | + // buffer size temporarily stored in the StreamMetadata pass |
| 43 | + // to avoid having to perform an expensive disk read for 2-bytes |
| 44 | + bytes16 = static_cast<uint16_t>(meta->pass()); |
| 45 | + // seek over size of the compressed buffer |
| 46 | + is.seekg(sizeof(uint16_t), std::ios_base::cur); |
| 47 | + } |
| 48 | + else { |
| 49 | + // otherwise read from disk |
| 50 | + is.read(reinterpret_cast<char*>(&bytes16), sizeof(uint16_t)); |
| 51 | + } |
| 52 | + |
| 53 | + if (bytes16 == std::numeric_limits<uint16_t>::max()) { |
| 54 | + // read or seek uncompressed data |
| 55 | + if (seek) { |
| 56 | + is.seekg(destBytes, std::ios_base::cur); |
| 57 | + } |
| 58 | + else { |
| 59 | + is.read(reinterpret_cast<char*>(destBuf), destBytes); |
| 60 | + } |
| 61 | + } |
| 62 | + else { |
| 63 | + // read or seek uncompressed data |
| 64 | + if (seek) { |
| 65 | + is.seekg(int(bytes16), std::ios_base::cur); |
| 66 | + } |
| 67 | + else { |
| 68 | + // decompress into the destination buffer |
| 69 | + std::unique_ptr<char[]> bloscBuffer(new char[int(bytes16)]); |
| 70 | + is.read(bloscBuffer.get(), bytes16); |
| 71 | + std::unique_ptr<char[]> buffer = bloscDecompress( bloscBuffer.get(), |
| 72 | + destBytes, |
| 73 | + /*resize=*/false); |
| 74 | + std::memcpy(destBuf, buffer.get(), destBytes); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// @brief openvdb::io::writeCompressedValues specialized on PointDataIndex32 arrays to |
| 80 | +/// ignore the value mask, use a larger block size and use 16-bit size instead of 64-bit |
| 81 | +template<> |
| 82 | +inline void |
| 83 | +writeCompressedValues( std::ostream& os, const PointDataIndex32* srcBuf, Index srcCount, |
| 84 | + const util::NodeMask<3>& /*valueMask*/, |
| 85 | + const util::NodeMask<3>& /*childMask*/, bool /*toHalf*/) |
| 86 | +{ |
| 87 | + using compression::bloscCompress; |
| 88 | + |
| 89 | + const size_t srcBytes = srcCount*sizeof(PointDataIndex32); |
| 90 | + const size_t maximumBytes = std::numeric_limits<uint16_t>::max(); |
| 91 | + if (srcBytes >= maximumBytes) { |
| 92 | + OPENVDB_THROW(openvdb::IoError, "Cannot write more than " << |
| 93 | + maximumBytes << " bytes in voxel values.") |
| 94 | + } |
| 95 | + |
| 96 | + const char* charBuffer = reinterpret_cast<const char*>(srcBuf); |
| 97 | + |
| 98 | + size_t compressedBytes; |
| 99 | + std::unique_ptr<char[]> buffer = bloscCompress( charBuffer, srcBytes, |
| 100 | + compressedBytes, /*resize=*/false); |
| 101 | + |
| 102 | + if (compressedBytes > 0) { |
| 103 | + auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer |
| 104 | + os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t)); |
| 105 | + os.write(reinterpret_cast<const char*>(buffer.get()), compressedBytes); |
| 106 | + } |
| 107 | + else { |
| 108 | + auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed |
| 109 | + os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t)); |
| 110 | + os.write(reinterpret_cast<const char*>(srcBuf), srcBytes); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +template <typename T> |
| 115 | +inline void |
| 116 | +writeCompressedValuesSize(std::ostream& os, const T* srcBuf, Index srcCount) |
| 117 | +{ |
| 118 | + using compression::bloscCompressedSize; |
| 119 | + |
| 120 | + const size_t srcBytes = srcCount*sizeof(T); |
| 121 | + const size_t maximumBytes = std::numeric_limits<uint16_t>::max(); |
| 122 | + if (srcBytes >= maximumBytes) { |
| 123 | + OPENVDB_THROW(openvdb::IoError, "Cannot write more than " << |
| 124 | + maximumBytes << " bytes in voxel values.") |
| 125 | + } |
| 126 | + |
| 127 | + const char* charBuffer = reinterpret_cast<const char*>(srcBuf); |
| 128 | + |
| 129 | + // calculate voxel buffer size after compression |
| 130 | + size_t compressedBytes = bloscCompressedSize(charBuffer, srcBytes); |
| 131 | + |
| 132 | + if (compressedBytes > 0) { |
| 133 | + auto bytes16 = static_cast<uint16_t>(compressedBytes); // clamp to 16-bit unsigned integer |
| 134 | + os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t)); |
| 135 | + } |
| 136 | + else { |
| 137 | + auto bytes16 = static_cast<uint16_t>(maximumBytes); // max value indicates uncompressed |
| 138 | + os.write(reinterpret_cast<const char*>(&bytes16), sizeof(uint16_t)); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace io |
| 143 | + |
| 144 | + |
| 145 | +} // namespace OPENVDB_VERSION_NAME |
| 146 | +} // namespace openvdb |
| 147 | + |
| 148 | +#endif // OPENVDB_POINTS_POINT_DATA_IO_HAS_BEEN_INCLUDED |
0 commit comments