-
Notifications
You must be signed in to change notification settings - Fork 4
/
MatfileReader.cpp
443 lines (422 loc) · 13.8 KB
/
MatfileReader.cpp
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <cassert>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
#include <zlib.h>
#include "MatfileReader.hpp"
// endian_swap utility
inline void endianSwap(uint16_t& x) {
x = (x>>8) |
(x<<8);
}
inline void endianSwap(uint32_t& x) {
x = (x>>24) |
((x<<8) & 0x00FF0000) |
((x>>8) & 0x0000FF00) |
(x<<24);
}
inline void endianSwap(uint64_t& x) {
x = (x>>56) |
((x<<40) & 0x00FF000000000000) |
((x<<24) & 0x0000FF0000000000) |
((x<<8) & 0x000000FF00000000) |
((x>>8) & 0x00000000FF000000) |
((x>>24) & 0x0000000000FF0000) |
((x>>40) & 0x000000000000FF00) |
(x<<56);
}
//=========================================
pair<EDataType, EMatrixClass> classify(char* data, bool endianSwap) {
uint32_t dataType;
bool isSmallDataElement = (data[2] & 0xFF) || (data[3] & 0xFF);
if (isSmallDataElement)
dataType = *(reinterpret_cast<uint16_t*>(data));
else
dataType = *(reinterpret_cast<uint32_t*>(data));
if (static_cast<EDataType>(dataType) != miMATRIX)
return make_pair(static_cast<EDataType>(dataType), mxINVALID);
// miMATRIX must not be small element
assert(!isSmallDataElement);
ArrayFlags* af = new ArrayFlags(data+8, endianSwap);
EMatrixClass matrixClass;
matrixClass = af->klass();
return make_pair(static_cast<EDataType>(dataType), matrixClass);
}
DataElement* parse(char* data, bool endianSwap) {
DataElement* de = NULL;
// dispatch
pair<EDataType, EMatrixClass> kind = classify(data, endianSwap);
EDataType dataType = kind.first;
EMatrixClass matrixClass = kind.second;
switch (dataType) {
case miINT8:
de = new FlatDataElement<int8_t>(data, endianSwap);
break;
case miUINT8:
de = new FlatDataElement<uint8_t>(data, endianSwap);
break;
case miINT16:
de = new FlatDataElement<int16_t>(data, endianSwap);
break;
case miUINT16:
de = new FlatDataElement<uint16_t>(data, endianSwap);
break;
case miINT32:
de = new FlatDataElement<int32_t>(data, endianSwap);
break;
case miUINT32:
de = new FlatDataElement<uint32_t>(data, endianSwap);
break;
case miINT64:
de = new FlatDataElement<int64_t>(data, endianSwap);
break;
case miUINT64:
de = new FlatDataElement<uint64_t>(data, endianSwap);
break;
case miSINGLE:
de = new FlatDataElement<float>(data, endianSwap);
break;
case miDOUBLE:
de = new FlatDataElement<double>(data, endianSwap);
break;
case miCOMPRESSED:
de = new CompressedDataElement(data, endianSwap);
break;
case miUTF8:
de = new UnicodeDataElement<miUTF8>(data, endianSwap);
break;
case miUTF16:
de = new UnicodeDataElement<miUTF16>(data, endianSwap);
break;
case miUTF32:
de = new UnicodeDataElement<miUTF32>(data, endianSwap);
break;
case miMATRIX:
switch (matrixClass) {
case mxCELL_CLASS:
de = new Cell(data, endianSwap);
break;
case mxSTRUCT_CLASS:
de = new Struct(data, endianSwap);
break;
case mxOBJECT_CLASS:
de = new Object(data, endianSwap);
break;
case mxCHAR_CLASS:
de = new NumericArray<char>(data, endianSwap);
break;
case mxSPARSE_CLASS:
de = new SparseArray<double>(data, endianSwap);
break;
case mxDOUBLE_CLASS:
de = new NumericArray<double>(data, endianSwap);
break;
case mxSINGLE_CLASS:
de = new NumericArray<float>(data, endianSwap);
break;
case mxINT8_CLASS:
de = new NumericArray<int8_t>(data, endianSwap);
break;
case mxUINT8_CLASS:
de = new NumericArray<uint8_t>(data, endianSwap);
break;
case mxINT16_CLASS:
de = new NumericArray<int8_t>(data, endianSwap);
break;
case mxUINT16_CLASS:
de = new NumericArray<uint8_t>(data, endianSwap);
break;
case mxINT32_CLASS:
de = new NumericArray<int8_t>(data, endianSwap);
break;
case mxUINT32_CLASS:
de = new NumericArray<uint8_t>(data, endianSwap);
break;
case mxINT64_CLASS:
de = new NumericArray<int8_t>(data, endianSwap);
break;
case mxUINT64_CLASS:
de = new NumericArray<uint8_t>(data, endianSwap);
break;
default:
cerr << "invalid EMatrixClass!\n";
exit(1);
}
break;
default:
cerr << "invalid EDataType!\n";
exit(1);
}
if (!de) {
cerr << "failed to parse!\n";
exit(1);
}
return de;
}
//===========================================================
void DataElement::parseTag(char* tag) {
_isSmallDataElement = (tag[2] & 0xFF) || (tag[3] & 0xFF);
if (_isSmallDataElement) {
_dataType = static_cast<EDataType>(*(reinterpret_cast<uint16_t*>(tag)));
_numberOfBytes = *(reinterpret_cast<uint16_t*>(tag+2));
} else {
_dataType = static_cast<EDataType>(*(reinterpret_cast<uint32_t*>(tag)));
_numberOfBytes = *(reinterpret_cast<uint32_t*>(tag+4));
}
}
template <typename T>
FlatDataElement<T>::FlatDataElement(char* data, bool endianSwap) : DataElement(endianSwap), _data() {
parseTag(data);
if (_isSmallDataElement)
data += 4;
else
data += 8;
T* p = reinterpret_cast<T*>(data);
int size = _numberOfBytes / sizeof(T);
_data.reserve(size);
_data.insert(_data.begin(), p, p+size);
}
FieldNames::FieldNames(char* data, bool endianSwap, int32_t fieldNameLength) :
FlatDataElement<int8_t>(data, endianSwap), _fieldNameLength(fieldNameLength), _fieldNames() {
string name;
for (vector<int8_t>::iterator iter = _data.begin();
iter + _fieldNameLength <= _data.end();
iter += _fieldNameLength) {
name.assign(iter, iter+_fieldNameLength);
_fieldNames.push_back(name);
}
}
CompressedDataElement::CompressedDataElement(char* data, bool endianSwap) :
_decompressedData(NULL), DataElement(endianSwap) {
parseTag(data);
if (_isSmallDataElement)
data += 4; // not likely to happen
else
data += 8;
unsigned long size = _numberOfBytes * 2; // 4x as a hint
unsigned long rsize = 0;
int res = Z_BUF_ERROR;
while (res != Z_OK) {
if (res == Z_BUF_ERROR) {
if (_decompressedData) {
delete[] _decompressedData;
}
size *= 2;
_decompressedData = new char[size];
if (!_decompressedData) {
cerr << "FlatDataElement::parseData new failed!\n";
exit(1);
}
rsize = size;
res = uncompress(reinterpret_cast<unsigned char*>(_decompressedData), &size,
reinterpret_cast<unsigned char*>(data), _numberOfBytes);
} else {
cerr << "FlatDataElement::parseData uncompress failed!\n";
exit(1);
}
}
_decompressedSize = static_cast<uint32_t>(rsize);
}
DataElement* CompressedDataElement::reparse() {
DataElement* res = parse(_decompressedData, _endianSwap);
return res;
}
template <typename T>
NumericArray<T>::NumericArray(char* data, bool endianSwap) :
MatrixDataElement(endianSwap), _real(NULL), _imag(NULL) {
char* start = data;
parseTag(data);
if (_isSmallDataElement)
data += 4; // not likely to happen
else
data += 8;
parseFlags(data);
data += _flags->totalBytes();
parseDimensionsArray(data);
data += _dimensionsArray->totalBytes();
parseArrayName(data);
data += _name->totalBytes();
parseReal(data);
data += _real->totalBytes();
if (data < start + _numberOfBytes + 8) {
parseImag(data);
data += _imag->totalBytes();
}
assert(data == start+_numberOfBytes+8);
}
template <typename T>
SparseArray<T>::SparseArray(char* data, bool endianSwap) :
MatrixDataElement(endianSwap), _rows(NULL), _cols(NULL), _real(NULL), _imag(NULL) {
char* start = data;
parseTag(data);
if (_isSmallDataElement)
data += 4; // not likely to happen
else
data += 8;
parseFlags(data);
data += _flags->totalBytes();
parseDimensionsArray(data);
data += _dimensionsArray->totalBytes();
parseArrayName(data);
data += _name->totalBytes();
parseRows(data);
data += _rows->totalBytes();
parseCols(data);
data += _cols->totalBytes();
parseReal(data);
data += _real->totalBytes();
if (data < start + _numberOfBytes + 8) {
parseImag(data);
data += _imag->totalBytes();
}
assert(data == start+_numberOfBytes+8);
}
Cell::Cell(char* data, bool endianSwap) : MatrixDataElement(endianSwap), _cells() {
char* start = data;
parseTag(data);
if (_isSmallDataElement)
data += 4; // not likely to happen
else
data += 8;
parseFlags(data);
data += _flags->totalBytes();
parseDimensionsArray(data);
data += _dimensionsArray->totalBytes();
parseArrayName(data);
data += _name->totalBytes();
MatrixDataElement* cell = NULL;
while(data < start + _numberOfBytes + 8) {
// create matrix and add to _cells
cell = dynamic_cast<MatrixDataElement*>(parse(data, endianSwap));
assert(cell);
_cells.push_back(cell);
data += cell->totalBytes();
}
assert(data == start+_numberOfBytes+8);
}
Struct::Struct(char* data, bool endianSwap) :
MatrixDataElement(endianSwap), _fieldNameLength(NULL), _fieldNames(NULL), _fields() {
char* start = data;
parseTag(data);
if (_isSmallDataElement)
data += 4; // not likely to happen
else
data += 8;
parseFlags(data);
data += _flags->totalBytes();
parseDimensionsArray(data);
data += _dimensionsArray->totalBytes();
parseArrayName(data);
data += _name->totalBytes();
parseFieldNameLength(data);
data += _fieldNameLength->totalBytes();
parseFieldNames(data);
data += _fieldNames->totalBytes();
MatrixDataElement* field = NULL;
while(data < start + _numberOfBytes + 8) {
// create matrix and add to _fields
field = dynamic_cast<MatrixDataElement*>(parse(data, endianSwap));
assert(field);
_fields.push_back(field);
data += field->totalBytes();
}
assert(data == start+_numberOfBytes+8);
}
Object::Object(char* data, bool endianSwap) : _className(NULL), Struct(endianSwap) {
char* start = data;
parseTag(data);
if (_isSmallDataElement)
data += 4; // not likely to happen
else
data += 8;
parseFlags(data);
data += _flags->totalBytes();
parseDimensionsArray(data);
data += _dimensionsArray->totalBytes();
parseArrayName(data);
data += _name->totalBytes();
parseClassName(data);
data += _className->totalBytes();
parseFieldNameLength(data);
data += _fieldNameLength->totalBytes();
parseFieldNames(data);
data += _fieldNames->totalBytes();
MatrixDataElement* field = NULL;
while(data < start + _numberOfBytes + 8) {
// create matrix and add to _fields
field = dynamic_cast<MatrixDataElement*>(parse(data, endianSwap));
assert(field);
_fields.push_back(field);
data += field->totalBytes();
}
assert(data == start+_numberOfBytes+8);
}
MatfileReader::MatfileReader(string matfile)
: _matfile(matfile), _inputStream(), _descriptiveText(), _subsysDataOffset(0),
_version(0), _endianIndicator(), _endianSwap(false) {
_inputStream.open(_matfile, ios_base::in | ios_base::binary);
if(!_inputStream.is_open()) {
cerr << "open " << _matfile << " error!\n";
exit(1);
}
}
MatfileReader::~MatfileReader() {
for (int i = 0; i < _dataElements.size(); ++i)
delete _dataElements[i];
}
void MatfileReader::parseHeader() {
gotoHeader();
_inputStream.read(_header, 128);
_descriptiveText.assign(_header, 116);
memcpy(&_subsysDataOffset, _header+116, 8);
memcpy(&_version, _header+124, 2);
_endianIndicator.assign(_header+126, 2);
if (_endianIndicator == "IM")
_endianSwap = true;
else
_endianSwap = false;
}
void MatfileReader::parseDataElement() {
if (_inputStream.eof())
return;
uint32_t dataType;
uint32_t numberOfBytes;
bool isSmallDataElement;
char tag[8];
_inputStream.read(reinterpret_cast<char*>(&tag[0]), 4);
isSmallDataElement = tag[0] && tag[1]; // test the lower two bytes
if (isSmallDataElement) {
dataType = *(reinterpret_cast<uint16_t*>(&tag[2]));
numberOfBytes = *(reinterpret_cast<uint16_t*>(&tag[0]));
} else {
dataType = *(reinterpret_cast<uint32_t*>(&tag[0]));
_inputStream.read(reinterpret_cast<char*>(&tag[4]), 4);
numberOfBytes = *(reinterpret_cast<uint32_t*>(&tag[4]));
}
char* data = NULL;
DataElement* de = NULL;
if (isSmallDataElement)
de = parse(tag, _endianSwap);
else {
data = new char[numberOfBytes+8];
if (!data) {
cerr << "MatfileReader::parseDataElement new failed!\n";
exit(1);
}
memcpy(data, tag, 8);
_inputStream.read(reinterpret_cast<char*>(data+8), numberOfBytes);
de = parse(data, _endianSwap);
}
assert(de);
_dataElements.push_back(de);
// padding
if (numberOfBytes % 8)
_inputStream.ignore(8 - numberOfBytes % 8);
}
void MatfileReader::parseAllDataElements() {
gotoData();
while (!_inputStream.eof())
parseDataElement();
}