forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkX3DExporterFIWriterHelper.h
400 lines (345 loc) · 11.4 KB
/
vtkX3DExporterFIWriterHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*=========================================================================
Program: Visualization Toolkit
Module: vtkX3DExporterFIWriterHelper.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* @class vtkX3DExporterFIWriterHelper
*
*/
#ifndef vtkX3DExporterFIWriterHelper_h
#define vtkX3DExporterFIWriterHelper_h
//#include "vtkX3DExporterFIByteWriter.h"
#include "vtkZLibDataCompressor.h"
#include <cassert>
#define EXPONENT_MASK_32 0x7f800000
#define MANTISSA_MASK_32 0x007fffff
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
class vtkX3DExporterFIWriterHelper
{
public:
union float_to_unsigned_int_to_bytes
{
float f;
unsigned int ui;
unsigned char ub[4]; // unsigned bytes
};
template<typename T>
static inline void EncodeFloatFI(vtkX3DExporterFIByteWriter* writer, T* value, size_t size)
{
// We want to start at position 3
assert(writer->CurrentBytePos == 2);
// ITU C.19.3.4: If the alternative encoding-algorithm is present,
// then the two bits '11' (discriminant) are appended
writer->PutBits("11");
// ITU 10.8.1: This encoding algorithm has a vocabulary table index of 7,
writer->PutBits(7-1, 8);
std::string bytes;
char byte[4];
for (size_t i = 0; i < size; i++)
{
float_to_unsigned_int_to_bytes v;
v.f = value[i];
// Avoid -0
if (v.ui == 0x80000000)
{
v.f = 0;
}
byte[0] = v.ub[3];
byte[1] = v.ub[2];
byte[2] = v.ub[1];
byte[3] = v.ub[0];
bytes.append(byte, 4);
}
EncodeNonEmptyByteString5(writer, bytes);
}
template<typename T>
static inline void EncodeIntegerFI(vtkX3DExporterFIByteWriter* writer, T* value, size_t size)
{
// We want to start at position 3
assert(writer->CurrentBytePos == 2);
// ITU C.19.3.4: If the alternative encoding-algorithm is present,
// then the two bits '11' (discriminant) are appended
writer->PutBits("11");
// ITU 10.8.1: This encoding algorithm has a vocabulary table index of 4,
writer->PutBits(4-1, 8);
std::string bytes;
for(size_t i = 0; i < size; i++)
{
int v = value[i];
int f = ReverseBytes(&v);
char *p = reinterpret_cast <char*> (&f);
bytes.append(p, 4);
}
EncodeNonEmptyByteString5(writer, bytes);
}
static inline void EncodeCharacterString3(vtkX3DExporterFIByteWriter* writer, const std::string& value)
{
// We want to start at position 3
assert(writer->CurrentBytePos == 2);
// ITU C.19.3.1 If the alternative utf-8 is present, then the two bits '00'
// are appended to the bit stream.
writer->PutBits("00");
// ITU C.19.4: The component bytes is encoded as described in C.23.
EncodeNonEmptyByteString5(writer, value);
}
// ITU C.23: Encoding of the NonEmptyByteString starting
// on the fifth bit of an byte
static inline void EncodeNonEmptyByteString5(vtkX3DExporterFIByteWriter* writer, const std::string& value)
{
int length = static_cast<int>(value.length());
if (length <= 8)
{
writer->PutBit(0);
writer->PutBits(length - 1, 3);
}
else if (length <= 264)
{
writer->PutBits("1000");
writer->PutBits(length - 9, 8);
}
else
{
writer->PutBits("1100");
writer->PutBits(length - 265, 32);
}
writer->PutBytes(value.c_str(), length);
}
// ITU C.27: Encoding of integers in the range 1 to 2^20
// starting on the third bit of an byte
static inline void EncodeInteger3(vtkX3DExporterFIByteWriter* writer, unsigned int value)
{
// We want to start at position 3
assert(writer->CurrentBytePos == 2);
if (value <= 32) // ITU C.27.2
{
writer->PutBit(0);
writer->PutBits(value - 1, 5);
}
else if (value <= 2080) // ITU C.27.3
{
writer->PutBits("100");
writer->PutBits(value - 33, 11);
}
else if (value < 526368) // ITU C.27.4
{
writer->PutBits("101");
writer->PutBits(value - 2081, 19);
}
else // ITU C.27.5
{
writer->PutBits("1100000000");
writer->PutBits(value - 526369, 20);
}
}
// ITU C.25: Encoding of integers in the range 1 to 2^20
// starting on the second bit of an byte
static inline void EncodeInteger2(vtkX3DExporterFIByteWriter* writer, unsigned int value)
{
// We want to start at position 2
assert(writer->CurrentBytePos == 1);
if (value <= 64) // ITU C.25.2
{
writer->PutBits("0");
writer->PutBits(value - 1, 6);
}
else if (value <= 8256) // ITU C.25.3
{
writer->PutBits("10");
writer->PutBits(value - 65, 13);
}
else // ITU C.25.4
{
writer->PutBits("110");
writer->PutBits(value - 8257, 20);
}
}
static inline void EncodeLineFeed(vtkX3DExporterFIByteWriter* writer)
{
static bool firstTime = true;
writer->FillByte();
if (firstTime)
{
writer->PutBits("1001000000001010");
firstTime = false;
}
else
{
//cout << "Encode NOT the first time" << endl;
writer->PutBits("10100000");
}
}
private:
static int ReverseBytes(int* x) {
/* break x apart, then put it back together backwards */
int part1 = (*x) & 0xFF;
int part2 = ((*x) >> 8) & 0xFF;
int part3 = ((*x) >> 16) & 0xFF;
int part4 = ((*x) >> 24) & 0xFF;
return (part1 << 24) | ( part2 << 16) | (part3 << 8) | part4;
}
friend class X3DEncoderFunctions;
};
class X3DEncoderFunctions {
public:
template<typename T>
static inline void EncodeIntegerDeltaZ(vtkX3DExporterFIByteWriter* writer, T* value, size_t size, vtkZLibDataCompressor* compressor, bool image = false)
{
// We want to start at position 3
assert(writer->CurrentBytePos == 2);
// ITU C.19.3.4: If the alternative encoding-algorithm is present,
// then the two bits '11' (discriminant) are appended
writer->PutBits("11");
// ITU 10.8.1: This encoding algorithm has a vocabulary table index of 33
writer->PutBits(34-1, 8);
// compute delta
char span = 0;
size_t i = 0;
int f; unsigned char *p;
std::vector<unsigned char> deltas;
if (image)
{
span = 0;
for(i = 0; i < size; i++)
{
int v = 1 + (value[i]);
int *vp = reinterpret_cast<int*>(&v);
f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
p = reinterpret_cast <unsigned char*> (&f);
deltas.push_back(p[0]);
deltas.push_back(p[1]);
deltas.push_back(p[2]);
deltas.push_back(p[3]);
}
compressor->SetCompressionLevel(9);
}
else
{
for (i = 0; i < 20; i++)
{
if (value[i] == -1)
{
span = static_cast<char>(i) + 1;
break;
}
}
if (!span) span = 4;
for(i = 0; i < static_cast<size_t>(span); i++)
{
int v = 1 + value[i];
int *vp = reinterpret_cast<int*>(&v);
f = vtkX3DExporterFIWriterHelper::ReverseBytes(vp);
p = reinterpret_cast <unsigned char*> (&f);
deltas.push_back(p[0]);
deltas.push_back(p[1]);
deltas.push_back(p[2]);
deltas.push_back(p[3]);
}
for(i = span; i < size; i++)
{
int v = 1 + (value[i] - value[i-span]);
f = vtkX3DExporterFIWriterHelper::ReverseBytes(&v);
p = reinterpret_cast <unsigned char*> (&f);
deltas.push_back(p[0]);
deltas.push_back(p[1]);
deltas.push_back(p[2]);
deltas.push_back(p[3]);
}
}
size_t bufferSize = deltas.size() + static_cast<unsigned int>(ceil(deltas.size()*0.001)) + 12;
unsigned char* buffer = new unsigned char[bufferSize];
size_t newSize = compressor->Compress(&deltas[0],static_cast<unsigned long>(deltas.size()), buffer, static_cast<unsigned long>(bufferSize));
std::string bytes;
int size32 = static_cast<int>(size);
int size32_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&size32);
char *s = reinterpret_cast <char*> (&size32_reversed);
bytes.append(s, 4);
bytes.append(&span, 1);
for (i = 0; i < newSize; i++)
{
unsigned char c = buffer[i];
bytes += c;
}
delete [] buffer;
vtkX3DExporterFIWriterHelper::EncodeNonEmptyByteString5(writer, bytes);
if (image)
{
compressor->SetCompressionLevel(5);
}
}
static inline void EncodeQuantizedzlibFloatArray(vtkX3DExporterFIByteWriter* writer, const double* value, size_t size, vtkZLibDataCompressor* compressor)
{
// We want to start at position 3
assert(writer->CurrentBytePos == 2);
// ITU C.19.3.4: If the alternative encoding-algorithm is present,
// then the two bits '11' (discriminant) are appended
writer->PutBits("11");
// ITU 10.8.1: This encoding algorithm has a vocabulary table index of 33
writer->PutBits(34, 8);
unsigned char* bytes = new unsigned char[size*4];
unsigned char* bytepos = bytes;
std::string bytesCompressed;
size_t i;
const double* vd = value;
for (i = 0; i < size; i++)
{
union float_to_unsigned_int_to_bytes
{
float f;
unsigned int ui;
unsigned char ub[4]; // unsigned bytes
};
float_to_unsigned_int_to_bytes v;
v.f = (*vd) * 2.0;
// Avoid -0
if (v.ui == 0x80000000)
{
v.f = 0.0f;
}
//vtkGenericWarningMacro(<< "value: " << v << " bytes: " << (int)s[0] << " " << (int)s[1] << " " << (int)s[2] << " " << (int)s[3]);
*bytepos++ = v.ub[3];
*bytepos++ = v.ub[2];
*bytepos++ = v.ub[1];
*bytepos++ = v.ub[0];
vd++;
}
// Compress the data
size_t bufferSize = (size * 4) + static_cast<size_t>(ceil((size * 4)*0.001)) + 12;
unsigned char* buffer = new unsigned char[bufferSize];
size_t newSize = compressor->Compress(bytes,
static_cast<unsigned long>(size * 4), buffer,
static_cast<unsigned long>(bufferSize));
char *s;
// Put the number of bits for exponent
bytesCompressed += static_cast<char>(8);
// Put the number of bits for mantissa
bytesCompressed += static_cast<char>(23);
// Put the length
int length = static_cast<int>(size*4);
int length_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&length);
s = reinterpret_cast <char*> (&length_reversed);
bytesCompressed.append(s, 4);
// Put the number of floats
int numFloats = static_cast<int>(size);
int numFloats_reversed = vtkX3DExporterFIWriterHelper::ReverseBytes(&numFloats);
s = reinterpret_cast <char*> (&numFloats_reversed);
bytesCompressed.append(s, 4);
for (i = 0; i < newSize; i++)
{
unsigned char c = buffer[i];
bytesCompressed += c;
}
vtkX3DExporterFIWriterHelper::EncodeNonEmptyByteString5(writer, bytesCompressed);
delete [] buffer;
delete [] bytes;
}
};
#endif
// VTK-HeaderTest-Exclude: vtkX3DExporterFIWriterHelper.h