Skip to content

Commit 5ed6896

Browse files
committed
Merge pull request #127 from dneg/compressionpaddingbug
1 parent 3d6dfa0 commit 5ed6896

File tree

2 files changed

+125
-66
lines changed

2 files changed

+125
-66
lines changed

openvdb/points/StreamCompression.cc

Lines changed: 81 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,25 @@ namespace compression {
4646
#ifdef OPENVDB_USE_BLOSC
4747

4848

49-
bool bloscCanCompress()
49+
bool
50+
bloscCanCompress()
5051
{
5152
return true;
5253
}
5354

5455

55-
size_t bloscUncompressedSize(const char* buffer)
56+
size_t
57+
bloscUncompressedSize(const char* buffer)
5658
{
5759
size_t bytes, _1, _2;
5860
blosc_cbuffer_sizes(buffer, &bytes, &_1, &_2);
5961
return bytes;
6062
}
6163

6264

63-
void bloscCompress( char* compressedBuffer, size_t& compressedBytes, const size_t bufferBytes,
64-
const char* uncompressedBuffer, const size_t uncompressedBytes)
65+
void
66+
bloscCompress(char* compressedBuffer, size_t& compressedBytes, const size_t bufferBytes,
67+
const char* uncompressedBuffer, const size_t uncompressedBytes)
6568
{
6669
if (bufferBytes > BLOSC_MAX_BUFFERSIZE) {
6770
OPENVDB_LOG_DEBUG("Blosc compress failed due to exceeding maximum buffer size.");
@@ -84,7 +87,8 @@ void bloscCompress( char* compressedBuffer, size_t& compressedBytes, const size_
8487
}
8588

8689
if (uncompressedBytes < BLOSC_PAD_BYTES && bufferBytes < BLOSC_PAD_BYTES + BLOSC_MAX_OVERHEAD) {
87-
OPENVDB_LOG_DEBUG("Blosc compress failed due to insufficient space in compressed buffer for padding.");
90+
OPENVDB_LOG_DEBUG(
91+
"Blosc compress failed due to insufficient space in compressed buffer for padding.");
8892
compressedBytes = 0;
8993
compressedBuffer = nullptr;
9094
return;
@@ -120,7 +124,8 @@ void bloscCompress( char* compressedBuffer, size_t& compressedBytes, const size_
120124

121125
if (_compressedBytes <= 0) {
122126
std::ostringstream ostr;
123-
ostr << "Blosc failed to compress " << uncompressedBytes << " byte" << (uncompressedBytes == 1 ? "" : "s");
127+
ostr << "Blosc failed to compress " << uncompressedBytes << " byte"
128+
<< (uncompressedBytes == 1 ? "" : "s");
124129
if (_compressedBytes < 0) ostr << " (internal error " << _compressedBytes << ")";
125130
OPENVDB_LOG_DEBUG(ostr.str());
126131
compressedBytes = 0;
@@ -140,13 +145,17 @@ void bloscCompress( char* compressedBuffer, size_t& compressedBytes, const size_
140145
}
141146

142147

143-
std::unique_ptr<char[]> bloscCompress(const char* buffer, const size_t uncompressedBytes, size_t& compressedBytes, const bool resize)
148+
std::unique_ptr<char[]>
149+
bloscCompress(const char* buffer, const size_t uncompressedBytes, size_t& compressedBytes,
150+
const bool resize)
144151
{
145-
size_t tempBytes = uncompressedBytes + BLOSC_MAX_OVERHEAD;
152+
size_t tempBytes = uncompressedBytes;
146153
// increase temporary buffer for padding if necessary
147154
if (tempBytes >= BLOSC_MINIMUM_BYTES && tempBytes < BLOSC_PAD_BYTES) {
148-
tempBytes += BLOSC_PAD_BYTES + BLOSC_MAX_OVERHEAD;
155+
tempBytes += BLOSC_PAD_BYTES;
149156
}
157+
// increase by Blosc max overhead
158+
tempBytes += BLOSC_MAX_OVERHEAD;
150159
const bool outOfRange = tempBytes > BLOSC_MAX_BUFFERSIZE;
151160
std::unique_ptr<char[]> outBuffer(outOfRange ? new char[1] : new char[tempBytes]);
152161

@@ -169,23 +178,28 @@ std::unique_ptr<char[]> bloscCompress(const char* buffer, const size_t uncompres
169178
}
170179

171180

172-
size_t bloscCompressedSize( const char* buffer, const size_t uncompressedBytes)
181+
size_t
182+
bloscCompressedSize( const char* buffer, const size_t uncompressedBytes)
173183
{
174184
size_t compressedBytes;
175185
bloscCompress(buffer, uncompressedBytes, compressedBytes, /*resize=*/false);
176186
return compressedBytes;
177187
}
178188

179189

180-
void bloscDecompress(char* uncompressedBuffer, const size_t expectedBytes, const size_t bufferBytes, const char* compressedBuffer)
190+
void
191+
bloscDecompress(char* uncompressedBuffer, const size_t expectedBytes,
192+
const size_t bufferBytes, const char* compressedBuffer)
181193
{
182194
size_t uncompressedBytes = bloscUncompressedSize(compressedBuffer);
183195

184196
if (bufferBytes > BLOSC_MAX_BUFFERSIZE) {
185-
OPENVDB_THROW(RuntimeError, "Blosc decompress failed due to exceeding maximum buffer size.");
197+
OPENVDB_THROW(RuntimeError,
198+
"Blosc decompress failed due to exceeding maximum buffer size.");
186199
}
187200
if (bufferBytes < uncompressedBytes + BLOSC_MAX_OVERHEAD) {
188-
OPENVDB_THROW(RuntimeError, "Blosc decompress failed due to insufficient space in uncompressed buffer.");
201+
OPENVDB_THROW(RuntimeError,
202+
"Blosc decompress failed due to insufficient space in uncompressed buffer.");
189203
}
190204

191205
uncompressedBytes = blosc_decompress_ctx( /*src=*/compressedBuffer,
@@ -208,7 +222,8 @@ void bloscDecompress(char* uncompressedBuffer, const size_t expectedBytes, const
208222
}
209223

210224

211-
std::unique_ptr<char[]> bloscDecompress(const char* buffer, const size_t expectedBytes, const bool resize)
225+
std::unique_ptr<char[]>
226+
bloscDecompress(const char* buffer, const size_t expectedBytes, const bool resize)
212227
{
213228
size_t uncompressedBytes = bloscUncompressedSize(buffer);
214229
size_t tempBytes = uncompressedBytes + BLOSC_MAX_OVERHEAD;
@@ -234,49 +249,55 @@ std::unique_ptr<char[]> bloscDecompress(const char* buffer, const size_t expecte
234249
#else
235250

236251

237-
bool bloscCanCompress()
252+
bool
253+
bloscCanCompress()
238254
{
239255
OPENVDB_LOG_DEBUG("Can't compress array data without the blosc library.");
240256
return false;
241257
}
242258

243259

244-
size_t bloscUncompressedSize(const char*)
260+
size_t
261+
bloscUncompressedSize(const char*)
245262
{
246263
OPENVDB_THROW(RuntimeError, "Can't extract compressed data without the blosc library.");
247264
}
248265

249266

250-
void bloscCompress( char*, size_t& compressedBytes, const size_t,
251-
const char*, const size_t)
267+
void
268+
bloscCompress(char*, size_t& compressedBytes, const size_t, const char*, const size_t)
252269
{
253270
OPENVDB_LOG_DEBUG("Can't compress array data without the blosc library.");
254271
compressedBytes = 0;
255272
}
256273

257274

258-
std::unique_ptr<char[]> bloscCompress(const char*, const size_t, size_t& compressedBytes, const bool)
275+
std::unique_ptr<char[]>
276+
bloscCompress(const char*, const size_t, size_t& compressedBytes, const bool)
259277
{
260278
OPENVDB_LOG_DEBUG("Can't compress array data without the blosc library.");
261279
compressedBytes = 0;
262280
return nullptr;
263281
}
264282

265283

266-
size_t bloscCompressedSize(const char*, const size_t)
284+
size_t
285+
bloscCompressedSize(const char*, const size_t)
267286
{
268287
OPENVDB_LOG_DEBUG("Can't compress array data without the blosc library.");
269288
return 0;
270289
}
271290

272291

273-
void bloscDecompress(char*, const size_t, const size_t, const char*)
292+
void
293+
bloscDecompress(char*, const size_t, const size_t, const char*)
274294
{
275295
OPENVDB_THROW(RuntimeError, "Can't extract compressed data without the blosc library.");
276296
}
277297

278298

279-
std::unique_ptr<char[]> bloscDecompress(const char*, const size_t, const bool)
299+
std::unique_ptr<char[]>
300+
bloscDecompress(const char*, const size_t, const bool)
280301
{
281302
OPENVDB_THROW(RuntimeError, "Can't extract compressed data without the blosc library.");
282303
}
@@ -288,28 +309,32 @@ std::unique_ptr<char[]> bloscDecompress(const char*, const size_t, const bool)
288309
////////////////////////////////////////
289310

290311

291-
void Page::load() const
312+
void
313+
Page::load() const
292314
{
293315
this->doLoad();
294316
}
295317

296318

297-
long Page::uncompressedBytes() const
319+
long
320+
Page::uncompressedBytes() const
298321
{
299322
assert(mInfo);
300323
return mInfo->uncompressedBytes;
301324
}
302325

303326

304-
const char* Page::buffer(const int index) const
327+
const char*
328+
Page::buffer(const int index) const
305329
{
306330
if (this->isOutOfCore()) this->load();
307331

308332
return mData.get() + index;
309333
}
310334

311335

312-
void Page::readHeader(std::istream& is)
336+
void
337+
Page::readHeader(std::istream& is)
313338
{
314339
assert(mInfo);
315340

@@ -330,7 +355,8 @@ void Page::readHeader(std::istream& is)
330355
}
331356

332357

333-
void Page::readBuffers(std::istream&is, bool delayed)
358+
void
359+
Page::readBuffers(std::istream&is, bool delayed)
334360
{
335361
assert(mInfo);
336362

@@ -345,7 +371,8 @@ void Page::readBuffers(std::istream&is, bool delayed)
345371
std::streamoff filepos = is.tellg();
346372

347373
// seek over the page
348-
is.seekg((isCompressed ? mInfo->compressedBytes : -mInfo->compressedBytes), std::ios_base::cur);
374+
is.seekg((isCompressed ? mInfo->compressedBytes : -mInfo->compressedBytes),
375+
std::ios_base::cur);
349376

350377
mInfo->mappedFile = mappedFile;
351378
mInfo->meta = meta;
@@ -354,7 +381,8 @@ void Page::readBuffers(std::istream&is, bool delayed)
354381
assert(mInfo->mappedFile);
355382
}
356383
else {
357-
std::unique_ptr<char[]> buffer(new char[(isCompressed ? mInfo->compressedBytes : -mInfo->compressedBytes)]);
384+
std::unique_ptr<char[]> buffer(new char[
385+
(isCompressed ? mInfo->compressedBytes : -mInfo->compressedBytes)]);
358386
is.read(buffer.get(), (isCompressed ? mInfo->compressedBytes : -mInfo->compressedBytes));
359387

360388
if (mInfo->compressedBytes > 0) {
@@ -367,20 +395,23 @@ void Page::readBuffers(std::istream&is, bool delayed)
367395
}
368396

369397

370-
bool Page::isOutOfCore() const
398+
bool
399+
Page::isOutOfCore() const
371400
{
372401
return bool(mInfo);
373402
}
374403

375404

376-
void Page::copy(const std::unique_ptr<char[]>& temp, int pageSize)
405+
void
406+
Page::copy(const std::unique_ptr<char[]>& temp, int pageSize)
377407
{
378408
mData.reset(new char[pageSize]);
379409
std::memcpy(mData.get(), temp.get(), pageSize);
380410
}
381411

382412

383-
void Page::decompress(const std::unique_ptr<char[]>& temp)
413+
void
414+
Page::decompress(const std::unique_ptr<char[]>& temp)
384415
{
385416
size_t uncompressedBytes = bloscUncompressedSize(temp.get());
386417
size_t tempBytes = uncompressedBytes;
@@ -393,7 +424,8 @@ void Page::decompress(const std::unique_ptr<char[]>& temp)
393424
}
394425

395426

396-
void Page::doLoad() const
427+
void
428+
Page::doLoad() const
397429
{
398430
if (!this->isOutOfCore()) return;
399431

@@ -442,14 +474,16 @@ PageHandle::PageHandle( const Page::Ptr& page, const int index, const int size)
442474
}
443475

444476

445-
Page& PageHandle::page()
477+
Page&
478+
PageHandle::page()
446479
{
447480
assert(mPage);
448481
return *mPage;
449482
}
450483

451484

452-
std::unique_ptr<char[]> PageHandle::read()
485+
std::unique_ptr<char[]>
486+
PageHandle::read()
453487
{
454488
assert(mIndex >= 0);
455489
assert(mSize > 0);
@@ -468,7 +502,8 @@ PagedInputStream::PagedInputStream(std::istream& is)
468502
}
469503

470504

471-
PageHandle::Ptr PagedInputStream::createHandle(std::streamsize n)
505+
PageHandle::Ptr
506+
PagedInputStream::createHandle(std::streamsize n)
472507
{
473508
assert(mByteIndex <= mUncompressedBytes);
474509

@@ -488,7 +523,8 @@ PageHandle::Ptr PagedInputStream::createHandle(std::streamsize n)
488523
}
489524

490525

491-
void PagedInputStream::read(PageHandle::Ptr& pageHandle, std::streamsize n, bool delayed)
526+
void
527+
PagedInputStream::read(PageHandle::Ptr& pageHandle, std::streamsize n, bool delayed)
492528
{
493529
assert(mByteIndex <= mUncompressedBytes);
494530

@@ -524,7 +560,8 @@ PagedOutputStream::PagedOutputStream(std::ostream& os)
524560
}
525561

526562

527-
PagedOutputStream& PagedOutputStream::write(const char* str, std::streamsize n)
563+
PagedOutputStream&
564+
PagedOutputStream::write(const char* str, std::streamsize n)
528565
{
529566
if (n > PageSize) {
530567
this->flush();
@@ -546,14 +583,16 @@ PagedOutputStream& PagedOutputStream::write(const char* str, std::streamsize n)
546583
}
547584

548585

549-
void PagedOutputStream::flush()
586+
void
587+
PagedOutputStream::flush()
550588
{
551589
this->compressAndWrite(mData.get(), mBytes);
552590
mBytes = 0;
553591
}
554592

555593

556-
void PagedOutputStream::compressAndWrite(const char* buffer, size_t size)
594+
void
595+
PagedOutputStream::compressAndWrite(const char* buffer, size_t size)
557596
{
558597
if (size == 0) return;
559598

@@ -598,7 +637,8 @@ void PagedOutputStream::compressAndWrite(const char* buffer, size_t size)
598637
}
599638

600639

601-
void PagedOutputStream::resize(size_t size)
640+
void
641+
PagedOutputStream::resize(size_t size)
602642
{
603643
// grow the capacity if not sufficient space
604644
size_t requiredSize = size;

0 commit comments

Comments
 (0)