From 513c3a90b09f5d9b3828d4180a3ce8ce07c5e9a7 Mon Sep 17 00:00:00 2001 From: Daviw Williams Date: Tue, 1 Oct 2013 16:33:39 +0200 Subject: [PATCH] Rearranging some miniz code to simplify it a bit. --- .../PolyVoxCore/MinizBlockCompressor.inl | 41 ++++++++----------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/MinizBlockCompressor.inl b/library/PolyVoxCore/include/PolyVoxCore/MinizBlockCompressor.inl index b204e2c2..49ae7b74 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MinizBlockCompressor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/MinizBlockCompressor.inl @@ -35,8 +35,7 @@ namespace PolyVox :m_pDeflator(0) { // Create and store the deflator. - tdefl_compressor* pDeflator = new tdefl_compressor; - m_pDeflator = /*reinterpret_cast*/(pDeflator); + m_pDeflator = new tdefl_compressor; // The number of dictionary probes to use at each compression level (0-10). 0=implies fastest/minimal possible probing. // The discontinuity is unsettling but may be explained by the 'iCompressionLevel <= 3' check later? @@ -54,19 +53,27 @@ namespace PolyVox MinizBlockCompressor::~MinizBlockCompressor() { // Delete the deflator - tdefl_compressor* pDeflator = /*reinterpret_cast*/(m_pDeflator); - delete pDeflator; + delete m_pDeflator; } template void MinizBlockCompressor::compress(UncompressedBlock* pSrcBlock, CompressedBlock* pDstBlock) { + // It seems we have to reinitialise the deflator for each fresh dataset (it's probably intended for streaming, which we're not doing here) + tdefl_status status = tdefl_init(m_pDeflator, NULL, NULL, m_uCompressionFlags); + if (status != TDEFL_STATUS_OKAY) + { + std::stringstream ss; + ss << "tdefl_init() failed with return code '" << status << "'"; + POLYVOX_THROW(std::runtime_error, ss.str()); + } + void* pSrcData = reinterpret_cast(pSrcBlock->getData()); - uint32_t uSrcLength = pSrcBlock->getDataSizeInBytes(); + size_t uSrcLength = pSrcBlock->getDataSizeInBytes(); uint8_t tempBuffer[10000]; uint8_t* pDstData = reinterpret_cast( tempBuffer ); - uint32_t uDstLength = 10000; + size_t uDstLength = 10000; uint32_t uCompressedLength = 0; @@ -138,26 +145,10 @@ namespace PolyVox } template - uint32_t MinizBlockCompressor::compress(const void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) + uint32_t MinizBlockCompressor::compress(const void* pSrcData, size_t uSrcLength, void* pDstData, size_t uDstLength) { - //Get the deflator - tdefl_compressor* pDeflator = reinterpret_cast(m_pDeflator); - - // It seems we have to reinitialise the deflator for each fresh dataset (it's probably intended for streaming, which we're not doing here) - tdefl_status status = tdefl_init(pDeflator, NULL, NULL, m_uCompressionFlags); - if (status != TDEFL_STATUS_OKAY) - { - std::stringstream ss; - ss << "tdefl_init() failed with return code '" << status << "'"; - POLYVOX_THROW(std::runtime_error, ss.str()); - } - - // Change the type to avoid compiler warnings - size_t ulSrcLength = uSrcLength; - size_t ulDstLength = uDstLength; - // Compress as much of the input as possible (or all of it) to the output buffer. - status = tdefl_compress(pDeflator, pSrcData, &ulSrcLength, pDstData, &ulDstLength, TDEFL_FINISH); + tdefl_status status = tdefl_compress(m_pDeflator, pSrcData, &uSrcLength, pDstData, &uDstLength, TDEFL_FINISH); //Check whther the compression was successful. if (status != TDEFL_STATUS_DONE) @@ -168,7 +159,7 @@ namespace PolyVox } // The compression modifies 'ulDstLength' to hold the new length. - return ulDstLength; + return uDstLength; } template