20#ifndef vtkX3DExporterFIWriterHelper_h
21#define vtkX3DExporterFIWriterHelper_h
27#define EXPONENT_MASK_32 0x7f800000
28#define MANTISSA_MASK_32 0x007fffff
31#define max(a, b) (((a) > (b)) ? (a) : (b))
43 static inline void EncodeFloatFI(vtkX3DExporterFIByteWriter* writer, T* value,
size_t size)
46 assert(writer->CurrentBytePos == 2);
50 writer->PutBits(
"11");
52 writer->PutBits(7 - 1, 8);
56 for (
size_t i = 0; i < size; i++)
62 if (v.
ui == 0x80000000)
72 bytes.append(
byte, 4);
78 static inline void EncodeIntegerFI(vtkX3DExporterFIByteWriter* writer, T* value,
size_t size)
81 assert(writer->CurrentBytePos == 2);
85 writer->PutBits(
"11");
87 writer->PutBits(4 - 1, 8);
89 for (
size_t i = 0; i < size; i++)
92 int f = ReverseBytes(&v);
93 char* p =
reinterpret_cast<char*
>(&f);
100 vtkX3DExporterFIByteWriter* writer,
const std::string& value)
103 assert(writer->CurrentBytePos == 2);
107 writer->PutBits(
"00");
115 vtkX3DExporterFIByteWriter* writer,
const std::string& value)
117 int length =
static_cast<int>(value.length());
120 writer->PutBit(
false);
121 writer->PutBits(length - 1, 3);
123 else if (length <= 264)
125 writer->PutBits(
"1000");
126 writer->PutBits(length - 9, 8);
130 writer->PutBits(
"1100");
131 writer->PutBits(length - 265, 32);
133 writer->PutBytes(value.c_str(), length);
138 static inline void EncodeInteger3(vtkX3DExporterFIByteWriter* writer,
unsigned int value)
141 assert(writer->CurrentBytePos == 2);
145 writer->PutBit(
false);
146 writer->PutBits(value - 1, 5);
148 else if (value <= 2080)
150 writer->PutBits(
"100");
151 writer->PutBits(value - 33, 11);
153 else if (value < 526368)
155 writer->PutBits(
"101");
156 writer->PutBits(value - 2081, 19);
160 writer->PutBits(
"1100000000");
161 writer->PutBits(value - 526369, 20);
167 static inline void EncodeInteger2(vtkX3DExporterFIByteWriter* writer,
unsigned int value)
170 assert(writer->CurrentBytePos == 1);
174 writer->PutBits(
"0");
175 writer->PutBits(value - 1, 6);
177 else if (value <= 8256)
179 writer->PutBits(
"10");
180 writer->PutBits(value - 65, 13);
184 writer->PutBits(
"110");
185 writer->PutBits(value - 8257, 20);
191 static bool firstTime =
true;
195 writer->PutBits(
"1001000000001010");
201 writer->PutBits(
"10100000");
206 static int ReverseBytes(
int* x)
209 int part1 = (*x) & 0xFF;
210 int part2 = ((*x) >> 8) & 0xFF;
211 int part3 = ((*x) >> 16) & 0xFF;
212 int part4 = ((*x) >> 24) & 0xFF;
213 return (part1 << 24) | (part2 << 16) | (part3 << 8) | part4;
223 template <
typename T>
228 assert(writer->CurrentBytePos == 2);
232 writer->PutBits(
"11");
234 writer->PutBits(34 - 1, 8);
241 std::vector<unsigned char> deltas;
246 for (i = 0; i < size; i++)
248 int v = 1 + (value[i]);
249 int* vp =
reinterpret_cast<int*
>(&v);
250 f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
251 p =
reinterpret_cast<unsigned char*
>(&f);
252 deltas.push_back(p[0]);
253 deltas.push_back(p[1]);
254 deltas.push_back(p[2]);
255 deltas.push_back(p[3]);
261 for (i = 0; i < 20; i++)
265 span =
static_cast<char>(i) + 1;
272 for (i = 0; i < static_cast<size_t>(span); i++)
274 int v = 1 + value[i];
275 int* vp =
reinterpret_cast<int*
>(&v);
276 f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
278 p =
reinterpret_cast<unsigned char*
>(&f);
279 deltas.push_back(p[0]);
280 deltas.push_back(p[1]);
281 deltas.push_back(p[2]);
282 deltas.push_back(p[3]);
284 for (i = span; i < size; i++)
286 int v = 1 + (value[i] - value[i - span]);
287 f = vtkX3DExporterFIWriterHelper::ReverseBytes(&v);
289 p =
reinterpret_cast<unsigned char*
>(&f);
290 deltas.push_back(p[0]);
291 deltas.push_back(p[1]);
292 deltas.push_back(p[2]);
293 deltas.push_back(p[3]);
297 size_t bufferSize = deltas.size() +
static_cast<unsigned int>(ceil(deltas.size() * 0.001)) + 12;
298 unsigned char* buffer =
new unsigned char[bufferSize];
299 size_t newSize = compressor->
Compress(&deltas[0],
static_cast<unsigned long>(deltas.size()),
300 buffer,
static_cast<unsigned long>(bufferSize));
303 int size32 =
static_cast<int>(size);
304 int size32_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&size32);
305 char* s =
reinterpret_cast<char*
>(&size32_reversed);
307 bytes.append(&span, 1);
309 for (i = 0; i < newSize; i++)
311 unsigned char c = buffer[i];
327 assert(writer->CurrentBytePos == 2);
331 writer->PutBits(
"11");
333 writer->PutBits(34, 8);
335 unsigned char* bytes =
new unsigned char[size * 4];
336 unsigned char* bytepos = bytes;
337 std::string bytesCompressed;
340 const double* vd = value;
341 for (i = 0; i < size; i++)
343 union float_to_unsigned_int_to_bytes {
348 float_to_unsigned_int_to_bytes v;
352 if (v.ui == 0x80000000)
358 *bytepos++ = v.ub[3];
359 *bytepos++ = v.ub[2];
360 *bytepos++ = v.ub[1];
361 *bytepos++ = v.ub[0];
366 size_t bufferSize = (size * 4) +
static_cast<size_t>(ceil((size * 4) * 0.001)) + 12;
367 unsigned char* buffer =
new unsigned char[bufferSize];
368 size_t newSize = compressor->
Compress(
369 bytes,
static_cast<unsigned long>(size * 4), buffer,
static_cast<unsigned long>(bufferSize));
373 bytesCompressed +=
static_cast<char>(8);
375 bytesCompressed +=
static_cast<char>(23);
377 int length =
static_cast<int>(size * 4);
378 int length_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&length);
379 s =
reinterpret_cast<char*
>(&length_reversed);
380 bytesCompressed.append(s, 4);
383 int numFloats =
static_cast<int>(size);
384 int numFloats_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&numFloats);
385 s =
reinterpret_cast<char*
>(&numFloats_reversed);
386 bytesCompressed.append(s, 4);
388 for (i = 0; i < newSize; i++)
390 unsigned char c = buffer[i];
391 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.