-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVicarLoaderBase.js
321 lines (240 loc) · 8.1 KB
/
VicarLoaderBase.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
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
import { readUntil, parseLabels, readString, getFirstLabelInstance } from './utils.js';
/**
* @typedef {Object} VicarResult
* @param {Array<{ isLabelGroup: Boolean, name: String, value: any }>} labels
* The set of header labels in the file. This includes both the header
* and EOL extension labels if present.
*
* @param {TypedArray} data
* The data of the file in a typed array of the type defined by the
* `labels.FORMAT` field with the binary prefix data stripped out.
*
* The array is of length `width * height * depth`. If {@link #VicarResult#complex complex}
* is true then the length will be `width * height * depth * 2` to account
* for the imaginary and real components of the values.
*
* @param {Number} width
* The row stride of the image as defined by the `labels.N1` field.
*
* @param {Number} height
* The height of the image as defined by the `labels.N2` field.
*
* @param {Number} depth
* The depth of the image as defined by the `labels.N3` field.
*
* @param {Uint8Array} prefixData
* The binary prefix data for each row defined at a `Uint8Array`. The
* array is of length `width * prefixWidth`.
*
* @param {Number} prefixWidth
* The width of the binary prefix as defined by the `labels.NBB` field.
*
* @param {Boolean} complex
* Whether the values are complex or not as dependent on the `labels.FORMAT`
* field. This will be `true` if `labels.FORMAT` is `COMP` or `COMPLEX`.
*/
/** Class for loading and parsing Vicar files */
export class VicarLoaderBase {
constructor() {
/**
* @member {Object}
* @description Fetch options for loading the file.
* @default { credentials: 'same-origin' }
*/
this.fetchOptions = { credentials: 'same-origin' };
}
/**
* Loads and parses the Vicar file. The promise resolves with the returned
* data from the {@link #VicarLoaderBase#parse parse} function.
* @param {String} url
* @returns {Promise<VicarResult>}
*/
load( url ) {
return fetch( url, this.fetchOptions )
.then( res => {
if ( ! res.ok ) {
throw new Error( `VicarLoader: Failed to load file "${url}" with status ${res.status} : ${res.statusText}` );
}
return res.arrayBuffer();
} )
.then( buffer => this.parse( buffer ) );
}
/**
* Parses the contents of the given Vicar file and returns an object describing
* the telemetry.
* @param {Uint8Array | ArrayBuffer} buffer
* @returns {VicarResult}
*/
parse( buffer ) {
let byteBuffer;
if ( buffer instanceof Uint8Array ) {
byteBuffer = buffer;
buffer = byteBuffer.buffer;
} else {
byteBuffer = new Uint8Array( buffer );
}
const lblsizeStr = readUntil( byteBuffer, 0, c => /\s/.test( c ) );
const labelSize = parseInt( lblsizeStr.split( '=' )[ 1 ] );
if ( Number.isNaN( labelSize ) ) {
throw new Error( 'VicarLoader: Label size not provided.' );
}
const header = readString( byteBuffer, 0, labelSize );
const labels = parseLabels( header );
const LBLSIZE = getFirstLabelInstance( labels, 'LBLSIZE' );
const RECSIZE = getFirstLabelInstance( labels, 'RECSIZE' );
const ORG = getFirstLabelInstance( labels, 'ORG' );
const NS = getFirstLabelInstance( labels, 'NS' );
const NL = getFirstLabelInstance( labels, 'NL' );
const NB = getFirstLabelInstance( labels, 'NB' );
const FORMAT = getFirstLabelInstance( labels, 'FORMAT' );
const EOL = getFirstLabelInstance( labels, 'EOL', 0 );
const INTFMT = getFirstLabelInstance( labels, 'INTFMT', 'LOW' );
const REALFMT = getFirstLabelInstance( labels, 'REALFMT', 'VAX' );
const NLB = getFirstLabelInstance( labels, 'NLB', 0 );
const NBB = getFirstLabelInstance( labels, 'NBB', 0 );
// const DIM = getFirstLabelInstance(labels, 'DIM', 3);
// const TYPE = getFirstLabelInstance(labels, 'TYPE', 'IMAGE');
// const HOST = getFirstLabelInstance(labels, 'HOST', 'VAX-VMS');
// const N4 = getFirstLabelInstance(labels, 'N4', 0);
let N1, N2, N3;
switch ( ORG ) {
case 'BSQ':
N1 = getFirstLabelInstance( labels, 'N1', NS );
N2 = getFirstLabelInstance( labels, 'N2', NL );
N3 = getFirstLabelInstance( labels, 'N3', NB );
if ( N1 !== NS || N2 !== NL || N3 !== NB ) {
throw new Error(
`VicarLoader: N1, N2, N3 labels do not match NS, NL, NB in BSQ order: ${ N1 }, ${ N2 }, ${ N2 } != ${ NS }, ${ NL }, ${ NB }`
);
}
break;
case 'BIL':
N1 = getFirstLabelInstance( labels, 'N1', NS );
N2 = getFirstLabelInstance( labels, 'N2', NB );
N3 = getFirstLabelInstance( labels, 'N3', NL );
if ( N1 !== NS || N2 !== NB || N3 !== NL ) {
throw new Error(
`VicarLoader: N1, N2, N3 labels do not match NS, NB, NL in BSQ order: ${ N1 }, ${ N2 }, ${ N2 } != ${ NS }, ${ NB }, ${ NL }`
);
}
break;
case 'BIP':
N1 = getFirstLabelInstance( labels, 'N1', NB );
N2 = getFirstLabelInstance( labels, 'N2', NS );
N3 = getFirstLabelInstance( labels, 'N3', NL );
if ( N1 !== NS || N2 !== NB || N3 !== NL ) {
throw new Error(
`VicarLoader: N1, N2, N3 labels do not match NB, NS, NL in BSQ order: ${ N1 }, ${ N2 }, ${ N2 } != ${ NB }, ${ NS }, ${ NL }`
);
}
break;
}
const imageOffset = LBLSIZE;
const imageSize = RECSIZE * ( N2 * N3 + NLB );
if ( EOL === 1 ) {
const eolOffset = imageOffset + imageSize;
const eolLabelStr = readUntil( byteBuffer, eolOffset, c => /\s/.test( c ) );
const eolLabelSize = parseInt( eolLabelStr.split( '=' )[ 1 ] );
const eolHeader = readString( byteBuffer, eolOffset, eolOffset + eolLabelSize );
const eolLabels = parseLabels( eolHeader );
labels.push( ...eolLabels );
}
let cons;
let readFunc;
let littleEndian;
let complex = false;
switch ( FORMAT ) {
case 'BYTE':
cons = Uint8Array;
readFunc = 'getUint8';
littleEndian = INTFMT === 'LOW';
break;
case 'WORD':
case 'HALF':
cons = Int16Array;
readFunc = 'getInt16';
littleEndian = INTFMT === 'LOW';
break;
case 'LONG':
case 'FULL':
cons = Int32Array;
readFunc = 'getInt32';
littleEndian = INTFMT === 'LOW';
break;
case 'REAL':
cons = Float32Array;
readFunc = 'getFloat32';
littleEndian = REALFMT === 'RIEEE';
if ( labels.REALFMT === 'VAX' ) {
throw new Error( 'VicarLoader: VAX REALFMT not supported.' );
}
break;
case 'DOUB':
cons = Float64Array;
readFunc = 'getFloat64';
littleEndian = REALFMT === 'RIEEE';
if ( REALFMT === 'VAX' ) {
throw new Error( 'VicarLoader: VAX REALFMT not supported.' );
}
break;
case 'COMPLEX':
case 'COMP':
complex = true;
cons = Float32Array;
readFunc = 'getFloat32';
littleEndian = REALFMT === 'RIEEE';
if ( REALFMT === 'VAX' ) {
throw new Error( 'VicarLoader: VAX REALFMT not supported.' );
}
break;
}
const dataOffset = imageOffset + NLB * RECSIZE;
const dataSize = imageSize - NLB * RECSIZE;
const view = new DataView( buffer, byteBuffer.byteOffset + dataOffset, dataSize );
const data = new cons( N1 * N2 * N3 );
const prefixData = new Uint8Array( NBB * N2 * N3 );
const recsize = RECSIZE;
const pxlSize = cons.BYTES_PER_ELEMENT;
const nbb = NBB;
for ( let i3 = 0, l3 = N3; i3 < l3; i3 ++ ) {
for ( let i2 = 0, l2 = N2; i2 < l2; i2 ++ ) {
// row number
const row = i3 * l2 + i2;
// row start index in bytes
const rowStart = row * recsize;
// copy the row data
for ( let i1 = 0, l1 = N1; i1 < l1; i1 ++ ) {
const byteOffset = rowStart + nbb + i1 * pxlSize;
const index = row * l1 + i1;
data[ index ] = view[ readFunc ]( byteOffset, littleEndian );
if ( complex ) {
data[ index + 1 ] = view[ readFunc ]( byteOffset + 4, littleEndian );
}
}
// copy the prefix data
for ( let ib = 0; ib < nbb; ib ++ ) {
const byteOffset = rowStart + ib;
const index = row * nbb + ib;
prefixData[ index ] = view.getUint8( byteOffset );
}
}
}
const width = NS;
const prefixWidth = NBB;
const height = NL;
const depth = NB;
if ( NLB !== 0 ) {
console.warn( 'VicarLoader: NLB Data is present but is not being procesed.' );
}
return {
labels,
data,
width,
height,
depth,
prefixData,
prefixWidth,
complex,
};
}
}