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
32 static inline void EncodeFloatFI(vtkX3DExporterFIByteWriter* writer, T* value,
size_t size)
35 assert(writer->CurrentBytePos == 2);
39 writer->PutBits(
"11");
41 writer->PutBits(7 - 1, 8);
45 for (
size_t i = 0; i < size; i++)
51 if (v.
ui == 0x80000000)
61 bytes.append(
byte, 4);
67 static inline void EncodeIntegerFI(vtkX3DExporterFIByteWriter* writer, T* value,
size_t size)
70 assert(writer->CurrentBytePos == 2);
74 writer->PutBits(
"11");
76 writer->PutBits(4 - 1, 8);
78 for (
size_t i = 0; i < size; i++)
81 int f = ReverseBytes(&v);
82 char* p =
reinterpret_cast<char*
>(&f);
89 vtkX3DExporterFIByteWriter* writer,
const std::string& value)
92 assert(writer->CurrentBytePos == 2);
96 writer->PutBits(
"00");
104 vtkX3DExporterFIByteWriter* writer,
const std::string& value)
106 int length =
static_cast<int>(value.length());
109 writer->PutBit(
false);
110 writer->PutBits(length - 1, 3);
112 else if (length <= 264)
114 writer->PutBits(
"1000");
115 writer->PutBits(length - 9, 8);
119 writer->PutBits(
"1100");
120 writer->PutBits(length - 265, 32);
122 writer->PutBytes(value.c_str(), length);
127 static inline void EncodeInteger3(vtkX3DExporterFIByteWriter* writer,
unsigned int value)
130 assert(writer->CurrentBytePos == 2);
134 writer->PutBit(
false);
135 writer->PutBits(value - 1, 5);
137 else if (value <= 2080)
139 writer->PutBits(
"100");
140 writer->PutBits(value - 33, 11);
142 else if (value < 526368)
144 writer->PutBits(
"101");
145 writer->PutBits(value - 2081, 19);
149 writer->PutBits(
"1100000000");
150 writer->PutBits(value - 526369, 20);
156 static inline void EncodeInteger2(vtkX3DExporterFIByteWriter* writer,
unsigned int value)
159 assert(writer->CurrentBytePos == 1);
163 writer->PutBits(
"0");
164 writer->PutBits(value - 1, 6);
166 else if (value <= 8256)
168 writer->PutBits(
"10");
169 writer->PutBits(value - 65, 13);
173 writer->PutBits(
"110");
174 writer->PutBits(value - 8257, 20);
180 static bool firstTime =
true;
184 writer->PutBits(
"1001000000001010");
190 writer->PutBits(
"10100000");
195 static int ReverseBytes(
int* x)
198 int part1 = (*x) & 0xFF;
199 int part2 = ((*x) >> 8) & 0xFF;
200 int part3 = ((*x) >> 16) & 0xFF;
201 int part4 = ((*x) >> 24) & 0xFF;
202 return (part1 << 24) | (part2 << 16) | (part3 << 8) | part4;
212 template <
typename T>
217 assert(writer->CurrentBytePos == 2);
221 writer->PutBits(
"11");
223 writer->PutBits(34 - 1, 8);
230 std::vector<unsigned char> deltas;
235 for (i = 0; i < size; i++)
237 int v = 1 + (value[i]);
238 int* vp =
reinterpret_cast<int*
>(&v);
239 f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
240 p =
reinterpret_cast<unsigned char*
>(&f);
241 deltas.push_back(p[0]);
242 deltas.push_back(p[1]);
243 deltas.push_back(p[2]);
244 deltas.push_back(p[3]);
250 for (i = 0; i < 20; i++)
254 span =
static_cast<char>(i) + 1;
261 for (i = 0; i < static_cast<size_t>(span); i++)
263 int v = 1 + value[i];
264 int* vp =
reinterpret_cast<int*
>(&v);
265 f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
267 p =
reinterpret_cast<unsigned char*
>(&f);
268 deltas.push_back(p[0]);
269 deltas.push_back(p[1]);
270 deltas.push_back(p[2]);
271 deltas.push_back(p[3]);
273 for (i = span; i < size; i++)
275 int v = 1 + (value[i] - value[i - span]);
276 f = vtkX3DExporterFIWriterHelper::ReverseBytes(&v);
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]);
286 size_t bufferSize = deltas.size() +
static_cast<unsigned int>(ceil(deltas.size() * 0.001)) + 12;
287 unsigned char* buffer =
new unsigned char[bufferSize];
288 size_t newSize = compressor->
Compress(deltas.data(),
static_cast<unsigned long>(deltas.size()),
289 buffer,
static_cast<unsigned long>(bufferSize));
292 int size32 =
static_cast<int>(size);
293 int size32_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&size32);
294 char* s =
reinterpret_cast<char*
>(&size32_reversed);
296 bytes.append(&span, 1);
298 for (i = 0; i < newSize; i++)
300 unsigned char c = buffer[i];
316 assert(writer->CurrentBytePos == 2);
320 writer->PutBits(
"11");
322 writer->PutBits(34, 8);
324 unsigned char* bytes =
new unsigned char[size * 4];
325 unsigned char* bytepos = bytes;
326 std::string bytesCompressed;
329 const double* vd = value;
330 for (i = 0; i < size; i++)
332 union float_to_unsigned_int_to_bytes {
337 float_to_unsigned_int_to_bytes v;
341 if (v.ui == 0x80000000)
347 *bytepos++ = v.ub[3];
348 *bytepos++ = v.ub[2];
349 *bytepos++ = v.ub[1];
350 *bytepos++ = v.ub[0];
355 size_t bufferSize = (size * 4) +
static_cast<size_t>(ceil((size * 4) * 0.001)) + 12;
356 unsigned char* buffer =
new unsigned char[bufferSize];
357 size_t newSize = compressor->
Compress(
358 bytes,
static_cast<unsigned long>(size * 4), buffer,
static_cast<unsigned long>(bufferSize));
362 bytesCompressed +=
static_cast<char>(8);
364 bytesCompressed +=
static_cast<char>(23);
366 int length =
static_cast<int>(size * 4);
367 int length_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&length);
368 s =
reinterpret_cast<char*
>(&length_reversed);
369 bytesCompressed.append(s, 4);
372 int numFloats =
static_cast<int>(size);
373 int numFloats_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&numFloats);
374 s =
reinterpret_cast<char*
>(&numFloats_reversed);
375 bytesCompressed.append(s, 4);
377 for (i = 0; i < newSize; i++)
379 unsigned char c = buffer[i];
380 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.