PolyVox  0.3.0-dev
Open source voxel management library
MinizCompressor.cpp
Go to the documentation of this file.
2 
4 
5 // Diable things we don't need, and in particular the zlib compatible names which
6 // would cause conflicts if a user application is using both PolyVox and zlib.
7 #define MINIZ_NO_STDIO
8 #define MINIZ_NO_ARCHIVE_APIS
9 #define MINIZ_NO_TIME
10 #define MINIZ_NO_ZLIB_APIS
11 #define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
12 #define MINIZ_NO_MALLOC
13 
15 
16 // The miniz library is supplied only as a single .c file without a header. The examples just include the .c file
17 // directly which is also what we do here. Actually is is possible to define 'MINIZ_HEADER_FILE_ONLY' to treat
18 // the .c file as a header, but this seems messy in terms of our project and CMake as we keep the headers and source
19 // files in seperate folders. We could create our own header for miniz (based on the stuff between the MINIZ_HEADER_FILE_ONLY
20 // directives) but the other problem is that we are using #pragma GCC system_header to supress warnings which would
21 // then be in the .c part of the code. If we ever update GCC on the CDash machine so that it properly supports '#pragma
22 // GCC diagnosic ignored' (or so that it doesn't warn in the first place) then we can reconsider spliting miniz.c in two.
23 #include "PolyVoxCore/Impl/miniz.c"
24 
25 
26 #include <sstream>
27 
28 using namespace std;
29 
30 namespace PolyVox
31 {
37  MinizCompressor::MinizCompressor(int iCompressionLevel)
38  :m_pDeflator(0)
39  {
40  // Create and store the deflator.
41  tdefl_compressor* pDeflator = new tdefl_compressor;
42  m_pDeflator = reinterpret_cast<void*>(pDeflator);
43 
44  // The number of dictionary probes to use at each compression level (0-10). 0=implies fastest/minimal possible probing.
45  // The discontinuity is unsettling but may be explained by the 'iCompressionLevel <= 3' check later?
46  static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
47 
48  // 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).
49  m_uCompressionFlags = TDEFL_WRITE_ZLIB_HEADER | s_tdefl_num_probes[MZ_MIN(10, iCompressionLevel)] | ((iCompressionLevel <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
50  if (!iCompressionLevel)
51  {
52  m_uCompressionFlags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
53  }
54  }
55 
57  {
58  // Delete the deflator
59  tdefl_compressor* pDeflator = reinterpret_cast<tdefl_compressor*>(m_pDeflator);
60  delete pDeflator;
61  }
62 
64  {
65  // The contents of this function are copied from miniz's 'mz_deflateBound()'
66  // (which we can't use because it is part of the zlib-style higher level API).
67  unsigned long source_len = uUncompressedInputSize;
68 
69  // 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.)
70  return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
71  }
72 
73  uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
74  {
75  //Get the deflator
76  tdefl_compressor* pDeflator = reinterpret_cast<tdefl_compressor*>(m_pDeflator);
77 
78  // It seems we have to reinitialise the deflator for each fresh dataset (it's probably intended for streaming, which we're not doing here)
79  tdefl_status status = tdefl_init(pDeflator, NULL, NULL, m_uCompressionFlags);
80  if (status != TDEFL_STATUS_OKAY)
81  {
82  stringstream ss;
83  ss << "tdefl_init() failed with return code '" << status << "'";
84  POLYVOX_THROW(std::runtime_error, ss.str());
85  }
86 
87  // Change the type to avoid compiler warnings
88  size_t ulSrcLength = uSrcLength;
89  size_t ulDstLength = uDstLength;
90 
91  // Compress as much of the input as possible (or all of it) to the output buffer.
92  status = tdefl_compress(pDeflator, pSrcData, &ulSrcLength, pDstData, &ulDstLength, TDEFL_FINISH);
93 
94  //Check whther the compression was successful.
95  if (status != TDEFL_STATUS_DONE)
96  {
97  stringstream ss;
98  ss << "tdefl_compress() failed with return code '" << status << "'";
99  POLYVOX_THROW(std::runtime_error, ss.str());
100  }
101 
102  // The compression modifies 'ulDstLength' to hold the new length.
103  return ulDstLength;
104  }
105 
106  uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
107  {
108  // I don't know exactly why this limitation exists but it's an implementation detail of miniz. It shouldn't matter for our purposes
109  // as our detination is a Block and those are always a power of two. If you need to use this class for other purposes then you'll
110  // probably have to scale up your dst buffer to the nearest appropriate size. Alternatively you can use the mz_uncompress function,
111  // but that means enabling parts of the miniz API which are #defined out at the top of this file.
112  POLYVOX_ASSERT(isPowerOf2(uDstLength), "Miniz decompressor requires the destination buffer to have a size which is a power of two.");
113  if(isPowerOf2(uDstLength) == false)
114  {
115  POLYVOX_THROW(std::invalid_argument, "Miniz decompressor requires the destination buffer to have a size which is a power of two.");
116  }
117 
118  // Change the type to avoid compiler warnings
119  size_t ulSrcLength = uSrcLength;
120  size_t ulDstLength = uDstLength;
121 
122  // Create and initialise the decompressor (I believe this is much small than the compressor).
123  tinfl_decompressor inflator;
124  tinfl_init(&inflator);
125 
126  // Do the decompression. In some scenarios 'tinfl_decompress' would be called multiple times with the same dest buffer but
127  // different locations within it. In our scenario it's only called once so the start and the location are the same (both pDstData).
128  tinfl_status status = tinfl_decompress(&inflator, (const mz_uint8 *)pSrcData, &ulSrcLength, (mz_uint8 *)pDstData, (mz_uint8 *)pDstData, &ulDstLength, TINFL_FLAG_PARSE_ZLIB_HEADER);
129 
130  //Check whther the decompression was successful.
131  if (status != TINFL_STATUS_DONE)
132  {
133  stringstream ss;
134  ss << "tinfl_decompress() failed with return code '" << status << "'";
135  POLYVOX_THROW(std::runtime_error, ss.str());
136  }
137 
138  // The decompression modifies 'ulDstLength' to hold the new length.
139  return ulDstLength;
140  }
141 }