forked from UL-FRI-LGM/RenderCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMHDReader.js
283 lines (239 loc) · 10.6 KB
/
MHDReader.js
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
/**
* Created by Primoz on 8.6.2016.
*/
export class MHDReader {
/**
* Instantiates MHD reader with the loading callback
* @param onLoad {function} Callback used by the loading functions.
*/
constructor(onLoad) {
// Loading callback
this._onLoad = onLoad;
// Object that holds relevant MHD meta data
this._mhdMeta = {};
// MHD and RAW file names
this._mhdFile = "";
this._rawFile = "";
// Any non-critical data miss-matches will be logged here
this._warnings = [];
this._fileReader = new FileReader();
this._loadingInProgress = false;
}
_clearParams() {
// Clear meta
this._mhdMeta = {};
// Clear file names
this._mhdFile = "";
this._rawFile = "";
// Clear warnings
this._warnings = [];
}
/**
* Asynchronously loads the volume values and meta data from the specified HTML5 files. Data is returned via object
* global onLoad callback specified in the constructor. Besides the values and meta data the callback also contains
* warning and error messages produced during the loading.
*
* @param mhdFile {Blob} HTML5 file pointing at .mhd file containing volume meta data like dimensions, orientation, position and data format.
* @param rawFile {Blob} HTML5 file pointing at .raw file containing volume values
* @returns {boolean} True if the loading was starter. False if the loading could not be started because there is already loading in progress.
*/
fileLoad(mhdFile, rawFile) {
// Check if anything is beeing loaded
if (this._loadingInProgress) {
return false;
}
this._clearParams();
// Put up the loading flag
this._loadingInProgress = true;
// Save reference to files
this._mhdFile = mhdFile;
this._rawFile = rawFile;
var self = this;
var rawParser = function(event) {
var status = {code: 0, msg: ""};
var rez = {};
var data;
try {
var dataLength = self._mhdMeta.dimensions[0] * self._mhdMeta.dimensions[1] * self._mhdMeta.dimensions[2];
var requiredByteLength = 0;
// Build the correct typed array based on the volume element type
switch (self._mhdMeta.elementType) {
case "MET_CHAR":
data = new Int8Array(this.result);
requiredByteLength = dataLength;
break;
case "MET_UCHAR":
data = new Uint8Array(this.result);
requiredByteLength = dataLength;
break;
case "MET_SHORT":
data = new Int16Array(this.result);
requiredByteLength = dataLength * 2;
break;
case "MET_USHORT":
data = new Uint16Array(this.result);
requiredByteLength = dataLength * 2;
break;
case "MET_INT":
data = new Int32Array(this.result);
requiredByteLength = dataLength * 4;
break;
case "MET_UINT":
data = new Uint32Array(this.result);
requiredByteLength = dataLength * 4;
break;
case "MET_FLOAT":
data = new Float32Array(this.result);
requiredByteLength = dataLength * 4;
break;
case "MET_DOUBLE":
data = new Float64Array(this.result);
requiredByteLength = dataLength * 8;
break;
}
if (this.result.byteLength !== requiredByteLength) {
status.code = 3;
status.msg = "Data length does not match specified dimensions in MHD";
}
}
catch (err) {
status.code = 4;
status.msg = "Failed while parsing the RAW file!";
}
// Return the result and toggle the loading flag.
self._onLoad({meta: self._mhdMeta, data: data, status: status, warnings: self._warnings});
};
var mhdParser = function(event) {
var status = {code: 0, msg: ""};
try {
var mhdData = this.result;
// Parse MHD content
var nDims = parseInt(/NDims\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1]);
self._mhdMeta.dimensions = /DimSize\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1].match(/\S+/g).map(Number);
self._mhdMeta.elementSpacing = /ElementSpacing\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1].match(/\S+/g).map(Number);
self._mhdMeta.position = /Position\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1].match(/\S+/g).map(Number);
self._mhdMeta.byteOrderMsb = /ElementByteOrderMSB\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1].replace(/\s+/g, '').toLowerCase() === "false";
self._mhdMeta.elementType = /ElementType\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1];
var dataFileName = /ElementDataFile\s+=\s+(.*)[\r\n]/g.exec(mhdData)[1];
// Check if the file RAW name matches the one specified in the MHD file
if (self._rawFile.name !== dataFileName) {
self._warnings.push({code: 1, msg: "RAW name does not match the one specified in the MHD file."})
}
// Check if this is indeed 3 dimensional data
if (nDims !== 3) {
status.code = 1;
status.msg = "Invalid data dimensions (" + nDims + "), required 3.";
}
} catch (err) {
status.code = 2;
status.msg = "Failed while parsing the MHD file!";
}
// Check if there has been an error while parsing the meta data
if (status.code !== 0) {
self._onLoad({status: status});
self._loadingInProgress = false;
return;
}
// If everything is in order.. proceed to RAW file loading
this.onload = rawParser;
this.readAsArrayBuffer(self._rawFile);
};
// Initiate MHD file loading
this._fileReader.onload = mhdParser;
this._fileReader.readAsBinaryString(this._mhdFile);
return true;
}
_strToBuffer(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0; i < str.length; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
dataParse(mhdString, rawString) {
// Check if anything is beeing loaded
if (this._loadingInProgress) {
return false;
}
// Clear parameters
this._clearParams();
// Put up the loading flag
this._loadingInProgress = true;
var status = {code: 0, msg: ""};
try {
// Parse MHD content
var nDims = parseInt(/NDims\s+=\s+(.*)[\r\n]/g.exec(mhdString)[1]);
this._mhdMeta.dimensions = /DimSize\s+=\s+(.*)[\r\n]/g.exec(mhdString)[1].match(/\S+/g).map(Number);
this._mhdMeta.elementSpacing = /ElementSpacing\s+=\s+(.*)[\r\n]/g.exec(mhdString)[1].match(/\S+/g).map(Number);
this._mhdMeta.position = /Position\s+=\s+(.*)[\r\n]/g.exec(mhdString)[1].match(/\S+/g).map(Number);
this._mhdMeta.byteOrderMsb = /ElementByteOrderMSB\s+=\s+(.*)[\r\n]/g.exec(mhdString)[1].replace(/\s+/g, '').toLowerCase() === "false";
this._mhdMeta.elementType = /ElementType\s+=\s+(.*)[\r\n]/g.exec(mhdString)[1];
// Check if this is indeed 3 dimensional data
if (nDims !== 3) {
status.code = 1;
status.msg = "Invalid data dimensions (" + nDims + "), required 3.";
}
} catch (err) {
status.code = 2;
status.msg = "Failed to parse MHD file!";
}
// Check if there has been an error while parsing the meta data
if (status.code !== 0) {
this._onLoad({status: status});
this._loadingInProgress = false;
return true;
}
var data;
var rawBuffer = this._strToBuffer(rawString);
try {
var dataLength = this._mhdMeta.dimensions[0] * this._mhdMeta.dimensions[1] * this._mhdMeta.dimensions[2];
var requiredByteLength = 0;
// Build the correct typed array based on the volume element type
switch (this._mhdMeta.elementType) {
case "MET_CHAR":
data = new Int8Array(rawBuffer);
requiredByteLength = dataLength;
break;
case "MET_UCHAR":
data = new Uint8Array(rawBuffer);
requiredByteLength = dataLength;
break;
case "MET_SHORT":
data = new Int16Array(rawBuffer);
requiredByteLength = dataLength * 2;
break;
case "MET_USHORT":
data = new Uint16Array(rawBuffer);
requiredByteLength = dataLength * 2;
break;
case "MET_INT":
data = new Int32Array(rawBuffer);
requiredByteLength = dataLength * 4;
break;
case "MET_UINT":
data = new Uint32Array(rawBuffer);
requiredByteLength = dataLength * 4;
break;
case "MET_FLOAT":
data = new Float32Array(rawBuffer);
requiredByteLength = dataLength * 4;
break;
case "MET_DOUBLE":
data = new Float64Array(rawBuffer);
requiredByteLength = dataLength * 8;
break;
}
if (rawBuffer.byteLength !== requiredByteLength) {
status.code = 3;
status.msg = "Data length does not match specified dimensions in MHD";
}
}
catch (err) {
status.code = 4;
status.msg = "Failed while parsing the RAW file!";
}
// Return the result and toggle the loading flag.
this._onLoad({meta: this._mhdMeta, data: data, status: status, warnings: this._warnings});
}
};