From 34671130fbd7bc626a3238fc25ce79928e8bc317 Mon Sep 17 00:00:00 2001 From: Daviw Williams Date: Mon, 25 Feb 2013 16:33:39 +0100 Subject: [PATCH] Tidying up Miniz compression code. --- .../include/PolyVoxCore/MinizCompressor.h | 4 +-- .../PolyVoxCore/source/MinizCompressor.cpp | 32 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/MinizCompressor.h b/library/PolyVoxCore/include/PolyVoxCore/MinizCompressor.h index 80a3717a..f6a5ed5c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MinizCompressor.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MinizCompressor.h @@ -16,12 +16,12 @@ namespace PolyVox uint32_t decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); private: - int m_iCompressionLevel; + unsigned int m_uCompressionFlags; // tdefl_compressor contains all the state needed by the low-level compressor so it's a pretty big struct (~300k). // We're storing it by void* because miniz does not supply a header and we don't want to include the .c file from // here as it will cause linker problems. - void* g_deflator; + void* m_pDeflator; }; } diff --git a/library/PolyVoxCore/source/MinizCompressor.cpp b/library/PolyVoxCore/source/MinizCompressor.cpp index 19b86c9c..7e7032a9 100644 --- a/library/PolyVoxCore/source/MinizCompressor.cpp +++ b/library/PolyVoxCore/source/MinizCompressor.cpp @@ -23,16 +23,25 @@ namespace PolyVox { // Compression levels: 0-9 are the standard zlib-style levels, 10 is best possible compression (not zlib compatible, and may be very slow) MinizCompressor::MinizCompressor(int iCompressionLevel) - :m_iCompressionLevel(iCompressionLevel) - ,g_deflator(0) + :m_pDeflator(0) { tdefl_compressor* pDeflator = new tdefl_compressor; - g_deflator = reinterpret_cast(pDeflator); + m_pDeflator = reinterpret_cast(pDeflator); + + // 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). + m_uCompressionFlags = TDEFL_WRITE_ZLIB_HEADER | s_tdefl_num_probes[MZ_MIN(10, iCompressionLevel)] | ((iCompressionLevel <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); + if (!iCompressionLevel) + { + m_uCompressionFlags |= TDEFL_FORCE_ALL_RAW_BLOCKS; + } } MinizCompressor::~MinizCompressor() { - tdefl_compressor* pDeflator = reinterpret_cast(g_deflator); + tdefl_compressor* pDeflator = reinterpret_cast(m_pDeflator); delete pDeflator; } @@ -68,19 +77,10 @@ namespace PolyVox // but it's implemented using the lower level API which does not conflict with zlib or perform any memory allocations. uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) { - tdefl_compressor* pDeflator = reinterpret_cast(g_deflator); + tdefl_compressor* pDeflator = reinterpret_cast(m_pDeflator); - // 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, m_iCompressionLevel)] | ((m_iCompressionLevel <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); - if (!m_iCompressionLevel) - { - comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; - } - - tdefl_status status = tdefl_init(pDeflator, NULL, NULL, comp_flags); + // 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) { stringstream ss;