From eb8ace0c54fa992cab861e568c62ce7484526d97 Mon Sep 17 00:00:00 2001 From: Daviw Williams Date: Thu, 21 Feb 2013 16:56:57 +0100 Subject: [PATCH] Replaced high level miniz interface with low-level version. --- .../PolyVoxCore/source/MinizCompressor.cpp | 70 ++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/library/PolyVoxCore/source/MinizCompressor.cpp b/library/PolyVoxCore/source/MinizCompressor.cpp index 2d749a54..0c3496d8 100644 --- a/library/PolyVoxCore/source/MinizCompressor.cpp +++ b/library/PolyVoxCore/source/MinizCompressor.cpp @@ -34,7 +34,7 @@ namespace PolyVox return static_cast(mz_compressBound(static_cast(uUncompressedInputSize))); } - uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) + /*uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) { mz_ulong ulDstLength = uDstLength; @@ -48,10 +48,50 @@ namespace PolyVox } // Return the number of bytes written to the output. + return ulDstLength; + }*/ + + uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) + { + int level = 9; + + // tdefl_compressor contains all the state needed by the low-level compressor so it's a pretty big struct (~300k). + // This example makes it a global vs. putting it on the stack, of course in real-world usage you'll probably malloc() or new it. + tdefl_compressor g_deflator; + + // The number of dictionary probes to use at each compression level (0-10). 0=implies fastest/minimal possible probing. + static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; + + // create tdefl() compatible flags (we have to compose the low-level flags ourselves, or use tdefl_create_comp_flags_from_zip_params() but that means MINIZ_NO_ZLIB_APIS can't be defined). + mz_uint comp_flags = TDEFL_WRITE_ZLIB_HEADER | s_tdefl_num_probes[MZ_MIN(10, level)] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); + if (!level) + comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; + + tdefl_status status = tdefl_init(&g_deflator, NULL, NULL, comp_flags); + assert(status == TDEFL_STATUS_OKAY); + if (status != TDEFL_STATUS_OKAY) + { + stringstream ss; + ss << "tdefl_init() failed with return code '" << status << "'"; + POLYVOX_THROW(std::runtime_error, ss.str()); + } + + size_t ulDstLength = uDstLength; + // Compress as much of the input as possible (or all of it) to the output buffer. + status = tdefl_compress(&g_deflator, pSrcData, &uSrcLength, pDstData, &ulDstLength, TDEFL_FINISH); + + assert(status == TDEFL_STATUS_DONE); + if (status != TDEFL_STATUS_DONE) + { + stringstream ss; + ss << "tdefl_compress() failed with return code '" << status << "'"; + POLYVOX_THROW(std::runtime_error, ss.str()); + } + return ulDstLength; } - uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) + /*uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) { mz_ulong ulDstLength = uDstLength; @@ -63,6 +103,32 @@ namespace PolyVox POLYVOX_THROW(std::runtime_error, ss.str()); } + return ulDstLength; + }*/ + + uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) + { + size_t ulDstLength = uDstLength; + + //int result = mz_uncompress((unsigned char*) pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength); + //assert(result == MZ_OK); + + //Check dest length is power of two! If it's a problem we could fall back on mz_uncompress at the expense of performance and bringing in more of the library. + + tinfl_decompressor inflator; + tinfl_init(&inflator); + + // Do the decompression. In some scenarios 'tinfl_decompress' would be called multiple times with the same dest buffer but + // different locations within it. In our scenario it's only called once so the start and the location are the same (both pDstData). + tinfl_status status = tinfl_decompress(&inflator, (const mz_uint8 *)pSrcData, &uSrcLength, (mz_uint8 *)pDstData, (mz_uint8 *)pDstData, &ulDstLength, TINFL_FLAG_PARSE_ZLIB_HEADER); + + if (status != TINFL_STATUS_DONE) + { + stringstream ss; + ss << "tinfl_decompress() failed with return code '" << status << "'"; + POLYVOX_THROW(std::runtime_error, ss.str()); + } + return ulDstLength; } }