8#ifndef vtkX3DExporterFIWriterHelper_h
9#define vtkX3DExporterFIWriterHelper_h
15#define EXPONENT_MASK_32 0x7f800000
16#define MANTISSA_MASK_32 0x007fffff
19#define max(a, b) (((a) > (b)) ? (a) : (b))
21VTK_ABI_NAMESPACE_BEGIN
33 static inline void EncodeFloatFI(vtkX3DExporterFIByteWriter* writer, T* value,
size_t size)
36 assert(writer->CurrentBytePos == 2);
40 writer->PutBits(
"11");
42 writer->PutBits(7 - 1, 8);
46 for (
size_t i = 0; i < size; i++)
52 if (v.
ui == 0x80000000)
62 bytes.append(
byte, 4);
68 static inline void EncodeIntegerFI(vtkX3DExporterFIByteWriter* writer, T* value,
size_t size)
71 assert(writer->CurrentBytePos == 2);
75 writer->PutBits(
"11");
77 writer->PutBits(4 - 1, 8);
79 for (
size_t i = 0; i < size; i++)
82 int f = ReverseBytes(&v);
83 char* p =
reinterpret_cast<char*
>(&f);
90 vtkX3DExporterFIByteWriter* writer,
const std::string& value)
93 assert(writer->CurrentBytePos == 2);
97 writer->PutBits(
"00");
105 vtkX3DExporterFIByteWriter* writer,
const std::string& value)
107 int length =
static_cast<int>(value.length());
110 writer->PutBit(
false);
111 writer->PutBits(length - 1, 3);
113 else if (length <= 264)
115 writer->PutBits(
"1000");
116 writer->PutBits(length - 9, 8);
120 writer->PutBits(
"1100");
121 writer->PutBits(length - 265, 32);
123 writer->PutBytes(value.c_str(), length);
128 static inline void EncodeInteger3(vtkX3DExporterFIByteWriter* writer,
unsigned int value)
131 assert(writer->CurrentBytePos == 2);
135 writer->PutBit(
false);
136 writer->PutBits(value - 1, 5);
138 else if (value <= 2080)
140 writer->PutBits(
"100");
141 writer->PutBits(value - 33, 11);
143 else if (value < 526368)
145 writer->PutBits(
"101");
146 writer->PutBits(value - 2081, 19);
150 writer->PutBits(
"1100000000");
151 writer->PutBits(value - 526369, 20);
157 static inline void EncodeInteger2(vtkX3DExporterFIByteWriter* writer,
unsigned int value)
160 assert(writer->CurrentBytePos == 1);
164 writer->PutBits(
"0");
165 writer->PutBits(value - 1, 6);
167 else if (value <= 8256)
169 writer->PutBits(
"10");
170 writer->PutBits(value - 65, 13);
174 writer->PutBits(
"110");
175 writer->PutBits(value - 8257, 20);
181 static bool firstTime =
true;
185 writer->PutBits(
"1001000000001010");
191 writer->PutBits(
"10100000");
196 static int ReverseBytes(
int* x)
199 int part1 = (*x) & 0xFF;
200 int part2 = ((*x) >> 8) & 0xFF;
201 int part3 = ((*x) >> 16) & 0xFF;
202 int part4 = ((*x) >> 24) & 0xFF;
203 return (part1 << 24) | (part2 << 16) | (part3 << 8) | part4;
213 template <
typename T>
218 assert(writer->CurrentBytePos == 2);
222 writer->PutBits(
"11");
224 writer->PutBits(34 - 1, 8);
231 std::vector<unsigned char> deltas;
236 for (i = 0; i < size; i++)
238 int v = 1 + (value[i]);
239 int* vp =
reinterpret_cast<int*
>(&v);
240 f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
241 p =
reinterpret_cast<unsigned char*
>(&f);
242 deltas.push_back(p[0]);
243 deltas.push_back(p[1]);
244 deltas.push_back(p[2]);
245 deltas.push_back(p[3]);
251 for (i = 0; i < 20; i++)
255 span =
static_cast<char>(i) + 1;
262 for (i = 0; i < static_cast<size_t>(span); i++)
264 int v = 1 + value[i];
265 int* vp =
reinterpret_cast<int*
>(&v);
266 f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
268 p =
reinterpret_cast<unsigned char*
>(&f);
269 deltas.push_back(p[0]);
270 deltas.push_back(p[1]);
271 deltas.push_back(p[2]);
272 deltas.push_back(p[3]);
274 for (i = span; i < size; i++)
276 int v = 1 + (value[i] - value[i - span]);
277 f = vtkX3DExporterFIWriterHelper::ReverseBytes(&v);
279 p =
reinterpret_cast<unsigned char*
>(&f);
280 deltas.push_back(p[0]);
281 deltas.push_back(p[1]);
282 deltas.push_back(p[2]);
283 deltas.push_back(p[3]);
287 size_t bufferSize = deltas.size() +
static_cast<unsigned int>(ceil(deltas.size() * 0.001)) + 12;
288 unsigned char* buffer =
new unsigned char[bufferSize];
289 size_t newSize = compressor->
Compress(deltas.data(),
static_cast<unsigned long>(deltas.size()),
290 buffer,
static_cast<unsigned long>(bufferSize));
293 int size32 =
static_cast<int>(size);
294 int size32_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&size32);
295 char* s =
reinterpret_cast<char*
>(&size32_reversed);
297 bytes.append(&span, 1);
299 for (i = 0; i < newSize; i++)
301 unsigned char c = buffer[i];
317 assert(writer->CurrentBytePos == 2);
321 writer->PutBits(
"11");
323 writer->PutBits(34, 8);
325 unsigned char* bytes =
new unsigned char[size * 4];
326 unsigned char* bytepos = bytes;
327 std::string bytesCompressed;
330 const double* vd = value;
331 for (i = 0; i < size; i++)
333 union float_to_unsigned_int_to_bytes
339 float_to_unsigned_int_to_bytes v;
343 if (v.ui == 0x80000000)
349 *bytepos++ = v.ub[3];
350 *bytepos++ = v.ub[2];
351 *bytepos++ = v.ub[1];
352 *bytepos++ = v.ub[0];
357 size_t bufferSize = (size * 4) +
static_cast<size_t>(ceil((size * 4) * 0.001)) + 12;
358 unsigned char* buffer =
new unsigned char[bufferSize];
359 size_t newSize = compressor->
Compress(
360 bytes,
static_cast<unsigned long>(size * 4), buffer,
static_cast<unsigned long>(bufferSize));
364 bytesCompressed +=
static_cast<char>(8);
366 bytesCompressed +=
static_cast<char>(23);
368 int length =
static_cast<int>(size * 4);
369 int length_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&length);
370 s =
reinterpret_cast<char*
>(&length_reversed);
371 bytesCompressed.append(s, 4);
374 int numFloats =
static_cast<int>(size);
375 int numFloats_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&numFloats);
376 s =
reinterpret_cast<char*
>(&numFloats_reversed);
377 bytesCompressed.append(s, 4);
379 for (i = 0; i < newSize; i++)
381 unsigned char c = buffer[i];
382 bytesCompressed += c;
static void EncodeQuantizedzlibFloatArray(vtkX3DExporterFIByteWriter *writer, const double *value, size_t size, vtkZLibDataCompressor *compressor)
static void EncodeIntegerDeltaZ(vtkX3DExporterFIByteWriter *writer, T *value, size_t size, vtkZLibDataCompressor *compressor, bool image=false)
size_t Compress(unsigned char const *uncompressedData, size_t uncompressedSize, unsigned char *compressedData, size_t compressionSpace)
Compress the given input data buffer into the given output buffer.
static void EncodeInteger2(vtkX3DExporterFIByteWriter *writer, unsigned int value)
static void EncodeInteger3(vtkX3DExporterFIByteWriter *writer, unsigned int value)
static void EncodeCharacterString3(vtkX3DExporterFIByteWriter *writer, const std::string &value)
static void EncodeLineFeed(vtkX3DExporterFIByteWriter *writer)
static void EncodeIntegerFI(vtkX3DExporterFIByteWriter *writer, T *value, size_t size)
static void EncodeFloatFI(vtkX3DExporterFIByteWriter *writer, T *value, size_t size)
static void EncodeNonEmptyByteString5(vtkX3DExporterFIByteWriter *writer, const std::string &value)
Data compression using zlib.
void SetCompressionLevel(int compressionLevel) override
Get/Set the compression level.