31 #ifndef OPENVDB_IO_COMPRESSION_HAS_BEEN_INCLUDED
32 #define OPENVDB_IO_COMPRESSION_HAS_BEEN_INCLUDED
34 #include <openvdb/Types.h>
35 #include <openvdb/math/Math.h>
36 #include <boost/scoped_array.hpp>
100 enum { isReal =
false };
140 is.read(reinterpret_cast<char*>(data),
sizeof(T) * count);
147 readData<std::string>(std::istream& is, std::string* data,
Index count,
bool )
149 for (
Index i = 0; i < count; ++i) {
155 std::string buffer(len+1,
' ');
156 is.read(&buffer[0], len+1 );
157 data[i].assign(buffer, 0, len);
165 template<
bool IsReal,
typename T>
struct HalfReader;
168 struct HalfReader<false, T> {
169 static inline void read(std::istream& is, T* data,
Index count,
bool compressed) {
170 readData(is, data, count, compressed);
175 struct HalfReader<true, T> {
177 static inline void read(std::istream& is, T* data,
Index count,
bool compressed) {
178 if (count < 1)
return;
179 std::vector<HalfT> halfData(count);
180 readData<HalfT>(is,
reinterpret_cast<HalfT*
>(&halfData[0]), count, compressed);
182 std::copy(halfData.begin(), halfData.end(), data);
199 zipToStream(os, reinterpret_cast<const char*>(data),
sizeof(T) * count);
201 os.write(reinterpret_cast<const char*>(data),
sizeof(T) * count);
209 writeData<std::string>(std::ostream& os,
const std::string* data,
Index count,
bool )
211 for (
Index i = 0; i < count; ++i) {
212 const size_t len = data[i].size();
214 os.write(data[i].c_str(), len+1);
223 template<
bool IsReal,
typename T>
struct HalfWriter;
226 struct HalfWriter<false, T> {
227 static inline void write(std::ostream& os,
const T* data,
Index count,
bool compress) {
233 struct HalfWriter<true, T> {
235 static inline void write(std::ostream& os,
const T* data,
Index count,
bool compress) {
236 if (count < 1)
return;
238 std::vector<HalfT> halfData(count);
239 std::copy(data, data + count, halfData.begin());
240 writeData<HalfT>(os,
reinterpret_cast<const HalfT*
>(&halfData[0]), count, compress);
246 struct HalfWriter<true, double> {
247 typedef RealToHalf<double>::HalfT HalfT;
248 static inline void write(std::ostream& os,
const double* data,
Index count,
bool compress) {
249 if (count < 1)
return;
251 std::vector<HalfT> halfData(count);
252 for (
Index i = 0; i < count; ++i) halfData[i] =
float(data[i]);
253 writeData<HalfT>(os,
reinterpret_cast<const HalfT*
>(&halfData[0]), count, compress);
274 template<
typename ValueT,
typename MaskT>
277 const MaskT& valueMask,
bool fromHalf)
289 is.read(reinterpret_cast<char*>(&metadata), 1);
292 ValueT background = zeroVal<ValueT>();
294 background = *
static_cast<const ValueT*
>(bgPtr);
296 ValueT inactiveVal1 = background;
297 ValueT inactiveVal0 =
305 is.read(reinterpret_cast<char*>(&inactiveVal0),
sizeof(ValueT));
308 is.read(reinterpret_cast<char*>(&inactiveVal1),
sizeof(ValueT));
319 selectionMask.load(is);
322 ValueT* tempBuf = destBuf;
323 boost::scoped_array<ValueT> scopedTempBuf;
325 Index tempCount = destCount;
327 tempCount = valueMask.countOn();
328 if (tempCount != destCount) {
331 scopedTempBuf.reset(
new ValueT[tempCount]);
332 tempBuf = scopedTempBuf.get();
338 HalfReader<RealToHalf<ValueT>::isReal, ValueT>::read(is, tempBuf, tempCount, zipped);
340 readData<ValueT>(is, tempBuf, tempCount, zipped);
346 if (maskCompressed && tempCount != destCount) {
350 for (
Index destIdx = 0, tempIdx = 0; destIdx < MaskT::SIZE; ++destIdx) {
351 if (valueMask.isOn(destIdx)) {
353 destBuf[destIdx] = tempBuf[tempIdx];
357 destBuf[destIdx] = (selectionMask.isOn(destIdx) ? inactiveVal1 : inactiveVal0);
376 template<
typename ValueT,
typename MaskT>
379 const MaskT& valueMask,
const MaskT& childMask,
bool toHalf)
383 static inline bool eq(
const ValueT& a,
const ValueT& b) {
394 Index tempCount = srcCount;
395 ValueT* tempBuf = srcBuf;
396 boost::scoped_array<ValueT> scopedTempBuf;
401 os.write(reinterpret_cast<const char*>(&metadata), 1);
409 const ValueT zero = zeroVal<ValueT>();
410 ValueT background = zero;
412 background = *
static_cast<const ValueT*
>(bgPtr);
416 ValueT inactiveVal[2] = { background, background };
417 int numUniqueInactiveVals = 0;
418 for (
typename MaskT::OffIterator it = valueMask.beginOff();
419 numUniqueInactiveVals < 3 && it; ++it)
424 if (childMask.isOn(idx))
continue;
426 const ValueT& val = srcBuf[idx];
427 const bool unique = !(
428 (numUniqueInactiveVals > 0 && Local::eq(val, inactiveVal[0])) ||
429 (numUniqueInactiveVals > 1 && Local::eq(val, inactiveVal[1]))
432 if (numUniqueInactiveVals < 2) inactiveVal[numUniqueInactiveVals] = val;
433 ++numUniqueInactiveVals;
439 if (numUniqueInactiveVals == 1) {
440 if (!Local::eq(inactiveVal[0], background)) {
441 if (Local::eq(inactiveVal[0],
negative(background))) {
447 }
else if (numUniqueInactiveVals == 2) {
449 if (!Local::eq(inactiveVal[0], background) && !Local::eq(inactiveVal[1], background)) {
454 }
else if (Local::eq(inactiveVal[1], background)) {
455 if (Local::eq(inactiveVal[0],
negative(background))) {
466 }
else if (Local::eq(inactiveVal[0], background)) {
467 if (Local::eq(inactiveVal[1],
negative(background))) {
472 std::swap(inactiveVal[0], inactiveVal[1]);
477 std::swap(inactiveVal[0], inactiveVal[1]);
483 os.write(reinterpret_cast<const char*>(&metadata), 1);
491 os.write(reinterpret_cast<const char*>(&inactiveVal[0]),
sizeof(ValueT));
494 os.write(reinterpret_cast<const char*>(&inactiveVal[1]),
sizeof(ValueT));
499 os.write(reinterpret_cast<const char*>(&truncatedVal),
sizeof(ValueT));
503 os.write(reinterpret_cast<const char*>(&truncatedVal),
sizeof(ValueT));
515 scopedTempBuf.reset(
new ValueT[srcCount]);
516 tempBuf = scopedTempBuf.get();
524 for (
typename MaskT::OnIterator it = valueMask.beginOn(); it; ++it, ++tempCount) {
525 tempBuf[tempCount] = srcBuf[it.pos()];
532 for (
Index srcIdx = 0; srcIdx < srcCount; ++srcIdx) {
533 if (valueMask.isOn(srcIdx)) {
534 tempBuf[tempCount] = srcBuf[srcIdx];
537 if (Local::eq(srcBuf[srcIdx], inactiveVal[1])) {
538 selectionMask.setOn(srcIdx);
542 assert(tempCount == valueMask.countOn());
545 selectionMask.save(os);
552 HalfWriter<RealToHalf<ValueT>::isReal, ValueT>::write(os, tempBuf, tempCount, zip);
562 #endif // OPENVDB_IO_COMPRESSION_HAS_BEEN_INCLUDED