NDEVR
API Documentation
Compressor.h
1#pragma once
2#include <NDEVR/Buffer.h>
3#include <NDEVR/Dictionary.h>
4#include <NDEVR/String.h>
5#include <NDEVR/Pointer.h>
6#include <NDEVR/TypeInfo.h>
7#include <NDEVR/PasswordString.h>
8#define NDEVR_SUPPORTS_ZFP 0
9namespace NDEVR
10{
11 class File;
12 struct Module;
35
47
67
73 class NDEVR_BASE_API Compressor
74 {
75 public:
76#ifndef Q_OS_WASM
83 static sint04 ZLibCompressFile(File src, File output, const PasswordString& password = PasswordString());
90 static sint04 ZLibDecompressFile(File src, File output_dir, const PasswordString& password = PasswordString());
94#endif
100 static sint04 ZLibCompress(uint01* compressed, const uint01* uncompressed, size_t& compressed_size, size_t decompressed_size);
104 static sint04 ZLibDecompress(const uint01* compressed, uint01* uncompressed, size_t compressed_size, size_t& decompressed_size);
123 static char* GetString(BinaryCompressionObject& data, const Bounds<1, uint04>& bounds);
130 static char* GetString(BinaryCompressionObject& data, uint04 index);
146 template<class t_type>
148 {
149 if (compression_mode == e_best_compression || compression_mode == e_default_compression)
150 {
152 compression_mode = e_string_reference;
153#if NDEVR_SUPPORTS_ZFP
155 {
157 {
158 case 0:
159 case 1:
160 if (sizeof(t_type) == 4)
161 compression_mode = e_floating_point_compression_1_fltp04;
162 else if (sizeof(t_type) == 8)
163 compression_mode = e_floating_point_compression_1_fltp08;
164 break;
165 case 2:
166 if (sizeof(t_type) / 2 == 4)
167 compression_mode = e_floating_point_compression_2_fltp04;
168 else if (sizeof(t_type) / 2 == 8)
169 compression_mode = e_floating_point_compression_2_fltp08;
170 break;
171 case 3:
172 if (sizeof(t_type) / 3 == 4)
173 compression_mode = e_floating_point_compression_3_fltp04;
174 else if (sizeof(t_type) / 3 == 8)
175 compression_mode = e_floating_point_compression_3_fltp08;
176 break;
177 case 4:
178 if (sizeof(t_type) / 4 == 4)
179 compression_mode = e_floating_point_compression_4_fltp04;
180 else if (sizeof(t_type) / 4 == 8)
181 compression_mode = e_floating_point_compression_4_fltp08;
182 break;
183 }
184 }
185#endif
186 }
187 return compression_mode;
188 }
189
199 template<class t_type, class t_memory_manager>
200 static typename std::enable_if<!ObjectInfo<t_type>::Buffer>::type Compress(BinaryCompressionObject& object, Buffer<uint01>& compression_data, const Buffer<t_type, t_memory_manager>& data)
201 {
202 object.object_type = GetTypeInfo<t_type>();
203 object.buffer_size = data.size();
204 if (object.buffer_size != 0)
205 {
206 object.compression_mode = Compressor::PickCompressionMode<t_type>(object.compression_mode);
207 object.uncompressed_data = (uint01*)data.ptr();
208 object.uncompressed_size = data.memSize();
209 switch (object.compression_mode)
210 {
212 compression_data.setSize(sizeof(Bounds<1, uint04>));
213 object.compressed_size = sizeof(Bounds<1, uint04>);
214 break;
216 compression_data.clear();
217 object.compressed_size = 0;
218 break;
219 default:
220 compression_data.setSize(cast<uint04>(data.memSize()));
221 object.compressed_size = data.memSize();
222 break;
223 }
224 object.compressed_data = compression_data.begin();
225 CompressData(object);
226 if(object.compression_mode != e_string_reference)
227 compression_data.setSize(cast<typename t_memory_manager::index_type>(object.compressed_size));
228
229 }
230 else
231 {
232 compression_data.clear();
233 }
234 }
235
243 template<class t_memory_manager>
245 {
246 Buffer<uint04> locations;
247 locations.setSize(data.size());
248 for (uint04 i = 0; i < data.size(); i++)
249 {
250 CompressedStringInfo info = AddString(object, data[i]);
251 locations[i] = info.index;
252 }
253 Compress(object, compression_data, locations);
254 }
255
266 template<class t_type, class t_memory_manager>
267 static typename std::enable_if<ObjectInfo<t_type>::Buffer>::type Compress(BinaryCompressionObject& object, Buffer<uint01>& compression_data, const Buffer<t_type, t_memory_manager>& data)
268 {
269 Buffer<uint01> temp_compress;
270 Buffer<uint04> sizes;
271 sizes.setSize(data.size());
272 Buffer<decltype(t_type::Type())> combined_data;
273 for (uint04 i = 0; i < data.size(); i++)
274 {
275 sizes[i] = data[i].size();
276 combined_data.addAll(data[i]);
277 }
278 compression_data.clear();
280 *sub_obj = object;
281 object.compression_link = sub_obj;
282
283 Compress(*sub_obj, temp_compress, sizes);
284 compression_data += temp_compress;
285
286 Compress(object, temp_compress, combined_data);
287 compression_data += temp_compress;
288 sub_obj->compressed_data = compression_data.begin();
289 object.compressed_data = compression_data.begin(cast<typename t_memory_manager::index_type>(sub_obj->compressed_size));
290 }
291 };
292}
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:54
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A series of static methods designed to compress objects and reduce their memory consumption or file s...
Definition Compressor.h:74
static CompressionMode PickCompressionMode(CompressionMode compression_mode=e_default_compression)
Selects the most appropriate compression mode for the given type.
Definition Compressor.h:147
static CompressedStringInfo AddString(BinaryCompressionObject &data, const String &string)
Adds a string to the BinaryCompressionObject, returning the Compressed string info.
static sint04 ZLibDecompress(const uint01 *compressed, uint01 *uncompressed, size_t compressed_size, size_t &decompressed_size)
A simple, raw call into zlibs uncompress function.
static std::enable_if< ObjectInfo< t_type >::Buffer >::type Compress(BinaryCompressionObject &object, Buffer< uint01 > &compression_data, const Buffer< t_type, t_memory_manager > &data)
Compresses a buffer of buffer-type elements (nested buffers) by flattening sub-buffer sizes and conte...
Definition Compressor.h:267
static Module ZLibModule()
Returns module information for the version of zlib used in this implementation.
static sint04 ZLibCompress(uint01 *compressed, const uint01 *uncompressed, size_t &compressed_size, size_t decompressed_size)
A simple, raw call into zlibs compress function, except if the compressed size is larger than the unc...
static sint04 ZLibCompressFile(File src, File output, const PasswordString &password=PasswordString())
Compresses a file or folder and writes it to an output.
static std::enable_if<!ObjectInfo< t_type >::Buffer >::type Compress(BinaryCompressionObject &object, Buffer< uint01 > &compression_data, const Buffer< t_type, t_memory_manager > &data)
Compresses a buffer of non-buffer (flat) elements into the given compression object.
Definition Compressor.h:200
static void DecompressData(BinaryCompressionObject &data)
Decompresses the BinaryCompressionObject.
static void CompressString(BinaryCompressionObject &object, Buffer< uint01 > &compression_data, const Buffer< String, t_memory_manager > &data)
Compresses a buffer of strings by adding each to the shared string table and then compressing the res...
Definition Compressor.h:244
static void CompressData(BinaryCompressionObject &data)
Compresses the BinaryCompressionObject.
static char * GetString(BinaryCompressionObject &data, uint04 index)
Retrieves a raw string pointer from the compression object by reference index.
static sint04 ZLibDecompressFile(File src, File output_dir, const PasswordString &password=PasswordString())
Decompresses a file or folder and writes it to an output directory.
static uint04 GetStringLength(BinaryCompressionObject &data, uint04 index)
Returns the length of a stored string identified by its reference index.
static char * GetString(BinaryCompressionObject &data, const Bounds< 1, uint04 > &bounds)
Retrieves a raw string pointer from the compression object using byte offset bounds.
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Provides a modifiable pointer that has shared ownership of a dynamically allocated object.
Definition Pointer.hpp:356
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
When passwords are used inside the NDEVR engine, we don't want them logged or visible.
The core String class for the NDEVR API.
Definition String.h:95
Stores information about a type, relevant for certain templated functions.
Definition TypeInfo.h:43
The primary namespace for the NDEVR SDK.
constexpr std::enable_if<!ObjectInfo< t_type >::Buffer, TypeInfo >::type GetTypeInfo()
Constructs a TypeInfo for a non-buffer type at compile time using ObjectInfo traits.
Definition TypeInfo.h:125
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
CompressionMode
Forward declaration of the Module struct for module metadata.
Definition Compressor.h:17
@ e_default_compression
Uses a sensible default compression strategy.
Definition Compressor.h:19
@ e_floating_point_compression_2_fltp08
2D double-precision floating-point compression.
Definition Compressor.h:29
@ e_best_compression
Optimizes for smallest output at the cost of speed.
Definition Compressor.h:23
@ e_floating_point_compression_1_fltp08
1D double-precision floating-point compression.
Definition Compressor.h:28
@ e_string_compression
Compression mode tailored for string data.
Definition Compressor.h:21
@ e_floating_point_compression_3_fltp08
3D double-precision floating-point compression.
Definition Compressor.h:30
@ e_floating_point_compression_4_fltp04
4D single-precision floating-point compression.
Definition Compressor.h:27
@ e_floating_point_compression
Generic floating-point compression.
Definition Compressor.h:22
@ e_no_compression
No compression is applied; data is stored raw.
Definition Compressor.h:18
@ e_floating_point_compression_1_fltp04
1D single-precision floating-point compression.
Definition Compressor.h:24
@ e_best_speed
Optimizes for fastest compression at the cost of ratio.
Definition Compressor.h:20
@ e_string_reference
Stores strings by reference index into a shared table.
Definition Compressor.h:32
@ e_floating_point_compression_2_fltp04
2D single-precision floating-point compression.
Definition Compressor.h:25
@ e_floating_point_compression_3_fltp04
3D single-precision floating-point compression.
Definition Compressor.h:26
@ e_floating_point_compression_4_fltp08
4D double-precision floating-point compression.
Definition Compressor.h:31
@ e_compression_count
Total number of compression modes; always last.
Definition Compressor.h:33
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
A container for storing compressed data, typically used for File IO operations.
Definition Compressor.h:53
uint08 uncompressed_size
Size in bytes of the uncompressed data.
Definition Compressor.h:56
DynamicPointer< BinaryCompressionObject > compression_link
Linked sub-object used when compressing nested buffers.
Definition Compressor.h:64
uint08 buffer_size
Number of logical elements in the buffer.
Definition Compressor.h:59
TypeInfo object_type
Type information describing the elements being compressed.
Definition Compressor.h:60
CompressionMode compression_mode
The compression algorithm to apply.
Definition Compressor.h:54
uint08 version_number
Version number for serialization compatibility.
Definition Compressor.h:65
Dictionary< String, uint04 > * m_string_reference
Lookup table mapping strings to reference indices.
Definition Compressor.h:61
uint01 * compressed_data
Pointer to the compressed data buffer.
Definition Compressor.h:57
Buffer< Bounds< 1, uint04 > > * m_string_size_info
Per-string byte offset bounds within the combined buffer.
Definition Compressor.h:62
String * m_strings
Pointer to the combined string storage buffer.
Definition Compressor.h:63
uint01 * uncompressed_data
Pointer to the raw uncompressed data buffer.
Definition Compressor.h:55
uint08 compressed_size
Size in bytes of the compressed data.
Definition Compressor.h:58
Contains information for referencing compressed strings.
Definition Compressor.h:43
Bounds< 1, uint04 > bounds
The byte offset bounds within the combined string buffer.
Definition Compressor.h:45
uint04 index
The index into the shared string reference table.
Definition Compressor.h:44
Base class for extensions, typically added as external DLL's that can modify or enhance the behavior ...
Information about the object.
Definition ObjectInfo.h:55