149 lines
6.1 KiB
C++
149 lines
6.1 KiB
C++
#include "PolyVoxCore/MinizCompressor.h"
|
|
|
|
// Diable things we don't need, and in particular the zlib compatible names which
|
|
// would cause conflicts if a user application is using both PolyVox and zlib.
|
|
#define MINIZ_NO_STDIO
|
|
#define MINIZ_NO_ARCHIVE_APIS
|
|
#define MINIZ_NO_TIME
|
|
#define MINIZ_NO_ZLIB_APIS
|
|
#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
|
|
#define MINIZ_NO_MALLOC
|
|
|
|
#include "PolyVoxCore/Impl/ErrorHandling.h"
|
|
// For some unknown reason the miniz library is supplied only as a
|
|
// single .c file without a header. Apparently the only way to use
|
|
// it is then to #include it directly which is what the examples do.
|
|
#include "PolyVoxCore/Impl/miniz.c"
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
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)
|
|
{
|
|
tdefl_compressor* pDeflator = new tdefl_compressor;
|
|
g_deflator = reinterpret_cast<void*>(pDeflator);
|
|
}
|
|
|
|
MinizCompressor::~MinizCompressor()
|
|
{
|
|
tdefl_compressor* pDeflator = reinterpret_cast<tdefl_compressor*>(g_deflator);
|
|
delete pDeflator;
|
|
}
|
|
|
|
uint32_t MinizCompressor::getMaxCompressedSize(uint32_t uUncompressedInputSize)
|
|
{
|
|
// The contents of this function are copied from miniz's 'mz_deflateBound()'
|
|
// (which we can't use because it is part of the zlib-style higher level API).
|
|
unsigned long source_len = uUncompressedInputSize;
|
|
|
|
// This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.)
|
|
return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
|
|
}
|
|
|
|
// The commented out function is left here for reference and debugging purposes.
|
|
/*uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
|
|
{
|
|
mz_ulong ulDstLength = uDstLength;
|
|
|
|
// Do the compression
|
|
int result = mz_compress((unsigned char*)pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength);
|
|
if(result != MZ_OK)
|
|
{
|
|
stringstream ss;
|
|
ss << "mz_compress() failed with return code '" << result << "'";
|
|
POLYVOX_THROW(std::runtime_error, ss.str());
|
|
}
|
|
|
|
// Return the number of bytes written to the output.
|
|
return ulDstLength;
|
|
}*/
|
|
|
|
// The behaviour of this function should be the same as the commented out version above (except that it requires the destination to be a power of two),
|
|
// 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<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, 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);
|
|
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(pDeflator, pSrcData, &uSrcLength, pDstData, &ulDstLength, TDEFL_FINISH);
|
|
|
|
if (status != TDEFL_STATUS_DONE)
|
|
{
|
|
stringstream ss;
|
|
ss << "tdefl_compress() failed with return code '" << status << "'";
|
|
POLYVOX_THROW(std::runtime_error, ss.str());
|
|
}
|
|
|
|
return ulDstLength;
|
|
}
|
|
|
|
// The commented out function is left here for reference and debugging purposes.
|
|
/*uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
|
|
{
|
|
mz_ulong ulDstLength = uDstLength;
|
|
|
|
int result = mz_uncompress((unsigned char*) pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength);
|
|
if(result != MZ_OK)
|
|
{
|
|
stringstream ss;
|
|
ss << "mz_uncompress() failed with return code '" << result << "'";
|
|
POLYVOX_THROW(std::runtime_error, ss.str());
|
|
}
|
|
|
|
return ulDstLength;
|
|
}*/
|
|
|
|
// The behaviour of this function should be the same as the commented out version above (except that it requires the destination to be a power of two),
|
|
// but it's implemented using the lower level API which does not conflict with zlib or perform any memory allocations.
|
|
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;
|
|
}
|
|
}
|