forked from scijs/nrrd-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrrd.js
1254 lines (1198 loc) · 45.1 KB
/
nrrd.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var pako = require('pako');
var assert = require('assert');
var lineSeparatorRE = /[ \f\t\v]*\r?\n/;
var NRRDMagicRE = /^NRRD\d{4}$/;
var lineRE = /^([^:]*)(:[ =])(.*)$/;
var dataFileListRE = /^LIST(?: (\d+))?$/;
// The minimal object this accepts is formed like this:
// {data: SomeTypedArray, sizes: [...]}
// On the other hand, if data is not given it must have a form like this:
// {buffer: SomeArrayBuffer, type: ..., endian: ..., sizes: [...]}
// Of course, if 'type' is an 8-bit type, endian is not needed, and if 'type' equals 'block', 'blockSize' should be set instead of 'endian'. In this case, no interpretation of buffer is done (at all, it is written serialized directly to the buffer).
// TODO: For now this only supports serializing "inline" files, or files for which you have already prepared the data.
module.exports.serialize = function (nrrdOrg) {
var i, buffer, arr, totalLen = 1, nrrd = {}, prop, nativeType, nativeSize, bufferData, arrData, line, lines = [], header;
// Copy nrrdOrg to nrrd to allow modifications without altering the original
for(prop in nrrdOrg) {
nrrd[prop] = nrrdOrg[prop];
}
// For saving files we allow inferring certain information if it is not explicitly given.
// Also we normalize some fields to make our own lives easier.
if (nrrd.sizes===undefined) { // 'sizes' should ALWAYS be given
throw new Error("Sizes missing from NRRD file!");
} else if (nrrd.dimension===undefined) {
nrrd.dimension = nrrd.sizes.length;
}
if (nrrd.data instanceof Int8Array) {
nativeType = "int8";
} else if (nrrd.data instanceof Uint8Array) {
nativeType = "uint8";
} else if (nrrd.data instanceof Int16Array) {
nativeType = "int16";
} else if (nrrd.data instanceof Uint16Array) {
nativeType = "uint16";
} else if (nrrd.data instanceof Int32Array) {
nativeType = "int32";
} else if (nrrd.data instanceof Uint32Array) {
nativeType = "uint32";
//} else if (nrrd.data instanceof Int64Array) {
// nativeType = "int64";
//} else if (nrrd.data instanceof Uint64Array) {
// nativeType = "uint64";
} else if (nrrd.data instanceof Float32Array) {
nativeType = "float";
} else if (nrrd.data instanceof Float64Array) {
nativeType = "double";
}
if (nrrd.type===undefined && nativeType!==undefined) {
nrrd.type = nativeType;
} else if (nrrd.type===undefined) {
throw new Error("Type of data is not given and cannot be inferred!");
} else if ((typeof nrrd.type) == "string" || nrrd.type instanceof String) {
nrrd.type = parseNRRDType(nrrd.type);
}
if (nrrd.encoding===undefined) {
nrrd.encoding = "raw";
} else if ((typeof nrrd.encoding) == "string" || nrrd.encoding instanceof String) {
nrrd.encoding = parseNRRDEncoding(nrrd.encoding);
}
if (nrrd.data && nrrd.type != 'block' && nrrd.type != 'int8' && nrrd.type != 'uint8' && nrrd.encoding != 'ascii') {
nrrd.endian = systemEndianness;
} else if (nrrd.type == 'block' || nrrd.type == 'int8' || nrrd.type == 'uint8' || nrrd.encoding == 'ascii') {
nrrd.endian = undefined;
} else if ((typeof nrrd.endian) == "string" || nrrd.endian instanceof String) {
nrrd.endian = parseNRRDEndian(nrrd.endian);
}
// Try to infer spatial dimension
var spaceDimension = undefined;
if (nrrd.spaceDimension!==undefined) {
spaceDimension = nrrd.spaceDimension;
} else if (nrrd.space!==undefined) {
switch(nrrd.space) {
case "right-anterior-superior":
case "RAS":
spaceDimension = 3;
break;
case "left-anterior-superior":
case "LAS":
spaceDimension = 3;
break;
case "left-posterior-superior":
case "LPS":
spaceDimension = 3;
break;
case "right-anterior-superior-time":
case "RAST":
spaceDimension = 4;
break;
case "left-anterior-superior-time":
case "LAST":
spaceDimension = 4;
break;
case "left-posterior-superior-time":
case "LPST":
spaceDimension = 4;
break;
case "scanner-xyz":
spaceDimension = 3;
break;
case "scanner-xyz-time":
spaceDimension = 4;
break;
case "3D-right-handed":
spaceDimension = 3;
break;
case "3D-left-handed":
spaceDimension = 3;
break;
case "3D-right-handed-time":
spaceDimension = 4;
break;
case "3D-left-handed-time":
spaceDimension = 4;
break;
default:
console.warn("Unrecognized space: " + nrrd.space);
}
}
// Now check that we have a valid nrrd structure.
checkNRRD(nrrd);
// Determine number of elements and check that we have enough data (if possible)
for(i=0; i<nrrd.sizes.length; i++) {
if (nrrd.sizes[i]<=0) throw new Error("Sizes should be a list of positive (>0) integers!");
totalLen *= nrrd.sizes[i];
}
if (nrrd.data) {
if (nrrd.data.length < totalLen) {
throw new Error("Missing data to serialize!");
}
} else if (nrrd.buffer) {
if (nrrd.encoding == "raw") {
if (nrrd.type=="block" && nrrd.blockSize!==undefined) {
nativeSize = nrrd.blockSize;
} else {
nativeSize = getNRRDTypeSize(nrrd.type);
}
if (nrrd.buffer.byteLength < totalLen*nativeSize) {
throw new Error("Missing data to serialize!");
}
}
} else if (nrrd.dataFile) {
// Okay, if you have your data ready, we'll just write a header.
} else {
throw new Error("Will not serialize an empty NRRD file!");
}
// Make sure we have the correct buffer in bufferData.
if (nrrd.data) {
switch(nrrd.encoding) {
case 'raw':
if (nrrd.type == nativeType && nrrd.endian == systemEndianness) {
bufferData = nrrd.data.buffer.slice(nrrd.data.byteOffset, nrrd.data.byteOffset+nrrd.data.byteLength);
} else if (nrrd.endian == systemEndianness) {
bufferData = castTypedArray(nrrd.data, nrrd.type);
bufferData = bufferData.buffer.slice(bufferData.byteOffset, bufferData.byteOffset+bufferData.byteLength);
} else {
bufferData = serializeToBuffer(nrrd.data, nrrd.type, nrrd.endian);
}
break;
case 'ascii':
if (nrrd.type == nativeType) {
bufferData = serializeToTextBuffer(nrrd.data);
} else {
bufferData = serializeToTextBuffer(castTypedArray(nrrd.data, nrrd.type));
}
break;
default:
throw new Error("Unsupported NRRD encoding: " + nrrd.encoding);
}
} else if (nrrd.buffer) {
bufferData = nrrd.buffer;
}
// Start header
lines.push("NRRD0005"); // TODO: Adjust version based on features that are actually used and/or the version specified by the user (if any).
lines.push("# Generated by nrrd-js");
// Put in dimension and space dimension (the NRRD spec requires that these are present before any lists whose length depends on them)
var firstProps = ['dimension', 'spaceDimension', 'space'];
for(i=0; i<firstProps.length; i++) {
prop = firstProps[i];
if (nrrd[prop] === undefined) continue; // Skip things we explicitly set to undefined.
line = serializeField(prop, nrrd[prop], nrrd.dimension, spaceDimension);
if (line!==undefined) lines.push(line);
}
// Put in field specifications
for(prop in nrrd) {
if (nrrd[prop] === undefined) continue; // Skip things we explicitly set to undefined.
if (firstProps.indexOf(prop)>=0) continue; // Skip the fields we already output.
line = serializeField(prop, nrrd[prop], nrrd.dimension, spaceDimension);
if (line!==undefined) lines.push(line);
}
// Put in keys (if any)
if (nrrd.keys) for(prop in nrrd.keys) {
if (prop.indexOf(":=")>=0) throw new Error("The combination ':=' is not allowed in an NRRD key!");
lines.push(prop + ":=" + escapeValue(nrrd[prop]));
}
// Put in data file list (if any)
if (nrrd.dataFile && nrrd.dataFile.length) {
lines.push("data file: LIST");
Array.prototype.push.apply(lines, nrrd.dataFile);
} else if (nrrd.dataFile && nrrd.dataFile.files && 'subdim' in nrrd.dataFile) {
lines.push("data file: LIST " + nrrd.dataFile.subdim);
Array.prototype.push.apply(lines, nrrd.dataFile.files);
}
// Put in empty line and inline data (if we have inline data) and convert lines to buffer
if (bufferData && !('dataFile' in nrrd)) {
lines.push("");
lines.push(""); // We actually need an extra blank line to make sure the previous is terminated.
header = lines.join("\n");
buffer = new ArrayBuffer(header.length + bufferData.byteLength);
arr = new Uint8Array(buffer);
for(i=0; i<header.length; i++) {
arr[i] = header.charCodeAt(i);
}
arrData = new Uint8Array(bufferData);
arr.set(arrData, header.length);
} else {
lines.push(""); // Blank line to at least terminate the last line.
header = lines.join("\n");
buffer = new ArrayBuffer(header.length);
arr = new Uint8Array(buffer);
for(i=0; i<header.length; i++) {
arr[i] = header.charCodeAt(i);
}
}
return buffer;
};
// This expects an ArrayBuffer.
module.exports.parse = function (buffer) {
var i, header, dataStart, ret = {data: undefined/* parsed data */, buffer: undefined/* raw buffer holding data */, keys: {}, version: undefined},
lines, match, match2,
buf8 = new Uint8Array(buffer);
// A work-around for incompatibilities between Node's Buffer and ArrayBuffer.
if (buf8.buffer !== buffer) buffer = buf8.buffer;
// First find the separation between the header and the data (if there is one)
// Note that we need to deal with with LF and CRLF as possible line endings.
// Luckily this means the line always ends with LF, so we only need to consider
// LFLF and LFCRLF as patterns for the separating empty line.
i=2; // It is safe to start at position 2 (in fact, we could start even later), as the file HAS to start with a magic word.
while(i<buf8.length) {
if (buf8[i] == 10) { // We hit an LF
if (buf8[i-1] == 10 || (buf8[i-1] == 13 && buf8[i-2] == 10)) { // Safe because we start at position 2 and never move backwards
dataStart = i+1;
break;
} else {
i++; // Move forward just once
}
} else if (buf8[i] == 13) { // We hit a CR
i++; // Move forward just once
} else {
i += 2; // Move forward two places,
}
}
// Now split up the header and data
if (dataStart === undefined) {
header = String.fromCharCode.apply(null, buf8);
} else {
header = String.fromCharCode.apply(null, buf8.subarray(0,dataStart));
ret.buffer = buffer.slice(dataStart);
}
// Split header into lines, remove comments (and blank lines) and check magic.
// All remaining lines except the first should be field specifications or key/value pairs.
// TODO: This explicitly removes any whitespace at the end of lines, however, I am not sure that this is actually desired behaviour for all kinds of lines.
lines = header.split(lineSeparatorRE);
lines = lines.filter(function (l) { return l.length>0 && l[0] != '#'; }); // Remove comment lines
if (!NRRDMagicRE.test(lines[0])) {
throw new Error("File is not an NRRD file!");
}
ret.version = parseInt(lines[0].substring(4, 8), 10);
if (ret.version>5) {
console.warn("Reading an unsupported version of the NRRD format; things may go haywire.");
}
// Parse lines
for(i=1; i<lines.length; i++) {
match = lineRE.exec(lines[i]);
if (!match) {
console.warn("Unrecognized line in NRRD header: " + lines[i]);
continue;
}
if (match[2] == ': ') { // Field specification
match[1] = mapNRRDToJavascript(match[1]);
if ( match[1] == 'dataFile' &&
(match2 = dataFileListRE.exec(match[3]))) {
// This should be the last field specification,
// and the rest of the lines should contain file names.
if (match2.length == 2 && match2[1]) { // subdim specification
ret[match[1]] = {
files: lines.slice(i+1),
subdim: parseNRRDInteger(match2[1])
};
} else {
ret[match[1]] = lines.slice(i+1);
}
lines.length = i;
} else {
ret[match[1]] = parseField(match[1], match[3]);
}
} else if (match[2] == ':=') { // Key/value pair
ret.keys[match[1]] = unescapeValue(match[3]);
} else {
throw new Error("Logic error in NRRD parser."); // This should never happen (unless the NRRD syntax is extended and the regexp is updated, but this section is not, or some other programmer error).
}
}
// Make sure the file satisfies the requirements of the NRRD format
checkNRRD(ret);
// "Parse" data
if ('dataFile' in ret) {
console.warn("No support for external data yet!");
} else {
switch(ret.encoding) {
case 'raw':
ret.data = parseNRRDRawData(ret.buffer, ret.type, ret.sizes, {
endian: ret.endian, blockSize: ret.blockSize
});
break;
case 'ascii':
ret.data = parseNRRDTextData(ret.buffer, ret.type, ret.sizes);
break;
case 'gzip':
ret.encoding = 'raw';
ret.buffer = pako.ungzip(ret.buffer).buffer;
ret.data = parseNRRDRawData(ret.buffer, ret.type, ret.sizes, {
endian: ret.endian, blockSize: ret.blockSize
});
break;
default:
console.warn("Unsupported NRRD encoding: " + ret.encoding);
}
}
return ret;
};
function escapeValue(val) {
return val.replace('\\', '\\\\').replace('\n', '\\n');
}
function unescapeValue(val) {
return val.split('\\\\').map(
function(s) { return s.replace('\\n', '\n'); }
).join('\\');
}
// Serializes NRRD fields
function serializeField(prop, value, dimension, spaceDimension) {
var line;
var propNRRD = mapJavascriptToNRRD(prop);
switch(prop) {
// nrrd-js stuff: skip
case 'data':
case 'buffer':
case 'keys':
case 'version':
break;
// Literal (uninterpreted) fields
case 'content':
case 'number':
case 'sampleUnits':
case 'space':
line = propNRRD + ": " + value;
break;
// Integers (no infinity or whatever, just a plain integer, so the default serialization is good enough)
case 'blockSize':
case 'lineSkip':
case 'byteSkip':
case 'dimension':
case 'spaceDimension':
assert((typeof value) == "number" || value instanceof Number, "Field " + prop + " should at least contain a number!");
line = propNRRD + ": " + value;
break;
// Floats (default serialization is good enough, as NaN contains nan, ignoring case, and similarly for Infinity inf)
case 'min':
case 'max':
case 'oldMin':
case 'oldMax':
assert((typeof value) == "number" || value instanceof Number, "Field " + prop + " should contain a number!");
line = propNRRD + ": " + value;
break;
// Vectors
case 'spaceOrigin':
assert(value.length === spaceDimension, "Field " + prop + " should be a list with length equal to the space dimension!");
value.forEach(function (val) { assert((typeof val) == "number" || val instanceof Number, "Field " + prop + " should be a list of numbers!"); });
line = propNRRD + ": (" + value.join(",") + ")";
break;
// Lists of strings
case 'labels':
case 'units':
case 'spaceUnits':
assert(value.length !== undefined && value.length == dimension, "Field " + prop + " should be a list with length equal to the dimension!");
value.forEach(function (val) { assert((typeof val) == "string" || val instanceof String, "Field " + prop + " should be a list of numbers!"); });
line = propNRRD + ": " + value.map(serializeNRRDQuotedString).join(" ");
break;
// Lists of integers
case 'sizes':
assert(value.length !== undefined && value.length == dimension, "Field " + prop + " should be a list with length equal to the dimension!");
value.forEach(function (val) { assert((typeof val) == "number" || val instanceof Number, "Field " + prop + " should be a list of numbers!"); });
line = propNRRD + ": " + value.join(" ");
break;
// Lists of floats
case 'spacings':
case 'thicknesses':
case 'axisMins':
case 'axisMaxs':
assert(value.length !== undefined && value.length == dimension, "Field " + prop + " should be a list with length equal to the dimension!");
value.forEach(function (val) { assert((typeof val) == "number" || val instanceof Number, "Field " + prop + " should be a list of numbers!"); });
line = propNRRD + ": " + value.join(" ");
break;
// Lists of vectors (dimension sized)
case 'spaceDirections':
assert(value.length !== undefined && value.length === dimension, "Field " + prop + " should be a list with length equal to the dimension!");
value.forEach(function (vec) {
assert(vec === null || (vec.length !== undefined && vec.length === spaceDimension), "The elements of field " + prop + " should be lists with length equal to the space dimension!");
if (vec !== null) vec.forEach(function (val) { assert((typeof val) == "number" || val instanceof Number, "The elements of field " + prop + " should be lists of numbers!"); });
});
line = propNRRD + ": " + value.map(function(vec) { return vec === null ? "none" : ("(" + vec.join(",") + ")"); }).join(" ");
break;
// Lists of vectors (space dimension sized)
case 'measurementFrame':
assert(value.length !== undefined && value.length === spaceDimension, "Field " + prop + " should be a list with length equal to the space dimension!");
value.forEach(function (vec) {
assert(vec === null || (vec.length !== undefined && vec.length === spaceDimension), "The elements of field " + prop + " should be lists with length equal to the space dimension!");
if (vec !== null) vec.forEach(function (val) { assert((typeof val) == "number" || val instanceof Number, "The elements of field " + prop + " should be lists of numbers!"); });
});
line = propNRRD + ": " + value.map(function(vec) { return vec === null ? "none" : ("(" + vec.join(",") + ")"); }).join(" ");
break;
// One-of-a-kind fields
case 'type':
assert((typeof value) == "string" || value instanceof String, "Field " + prop + " should contain a string!");
line = propNRRD + ": " + value;
break;
case 'encoding':
assert((typeof value) == "string" || value instanceof String, "Field " + prop + " should contain a string!");
line = propNRRD + ": " + value;
break;
case 'endian':
assert((typeof value) == "string" || value instanceof String, "Field " + prop + " should contain a string!");
line = propNRRD + ": " + value;
break;
case 'dataFile':
if (value.length || (value.files && 'subdim' in value)) {
// List of data files: skip for now
} else {
line = propNRRD + ": " + serializeNRRDDataFile(value);
}
break;
case 'centers':
assert(value.length !== undefined && value.length == dimension, "Field " + prop + " should be a list with length equal to the dimension!");
line = propNRRD + ": " + value.map(serializeNRRDOptional).join(" ");
break;
case 'kinds':
assert(value.length !== undefined && value.length == dimension, "Field " + prop + " should be a list with length equal to the dimension!");
line = propNRRD + ": " + value.map(serializeNRRDOptional).join(" ");
break;
// Something unknown
default:
console.warn("Unrecognized NRRD field: " + prop + ", skipping.");
}
return line;
}
// Parses and normalizes NRRD fields, assumes the field names are already lower case.
function parseField(identifier, descriptor) {
switch(identifier) {
// Literal (uninterpreted) fields
case 'content':
case 'number':
case 'sampleUnits':
break;
// Integers
case 'dimension':
case 'blockSize':
case 'lineSkip':
case 'byteSkip':
case 'spaceDimension':
descriptor = parseNRRDInteger(descriptor);
break;
// Floats
case 'min':
case 'max':
case 'oldMin':
case 'oldMax':
descriptor = parseNRRDFloat(descriptor);
break;
// Vectors
case 'spaceOrigin':
descriptor = parseNRRDVector(descriptor);
break;
// Lists of strings
case 'labels':
case 'units':
case 'spaceUnits':
descriptor = parseNRRDWhitespaceSeparatedList(descriptor, parseNRRDQuotedString);
break;
// Lists of integers
case 'sizes':
descriptor = parseNRRDWhitespaceSeparatedList(descriptor, parseNRRDInteger);
break;
// Lists of floats
case 'spacings':
case 'thicknesses':
case 'axisMins':
case 'axisMaxs':
descriptor = parseNRRDWhitespaceSeparatedList(descriptor, parseNRRDFloat);
break;
// Lists of vectors
case 'spaceDirections':
case 'measurementFrame':
descriptor = parseNRRDWhitespaceSeparatedList(descriptor, parseNRRDVector);
break;
// One-of-a-kind fields
case 'type':
descriptor = parseNRRDType(descriptor);
break;
case 'encoding':
descriptor = parseNRRDEncoding(descriptor);
break;
case 'endian':
descriptor = parseNRRDEndian(descriptor);
break;
case 'dataFile':
descriptor = parseNRRDDataFile(descriptor);
break;
case 'centers':
descriptor = parseNRRDWhitespaceSeparatedList(descriptor, parseNRRDCenter);
break;
case 'kinds':
descriptor = parseNRRDWhitespaceSeparatedList(descriptor, parseNRRDKind);
break;
case 'space':
descriptor = parseNRRDSpace(descriptor);
break;
// Something unknown
default:
console.warn("Unrecognized NRRD field: " + identifier);
}
return descriptor;
}
// This only includes names whose lower case form is different from the Javascript form.
var mapNRRDToJavascriptStatic = {
'block size': 'blockSize',
'blocksize': 'blockSize',
'old min': 'oldMin',
'oldmin': 'oldMin',
'old max': 'oldMax',
'oldmax': 'oldMax',
'data file': 'dataFile',
'datafile': 'dataFile',
'line skip': 'lineSkip',
'lineskip': 'lineSkip',
'byte skip': 'byteSkip',
'byteskip': 'byteSkip',
'sample units': 'sampleUnits',
'sampleunits': 'sampleUnits',
'axis mins': 'axisMins',
'axis maxs': 'axisMaxs',
'centers': 'centers', // Not different, just included so it is clear why centerings maps to centers
'centerings': 'centers',
'space dimension': 'spaceDimension',
'space units': 'spaceUnits',
'space origin': 'spaceOrigin',
'space directions': 'spaceDirections',
'measurement frame': 'measurementFrame'
};
var mapJavascriptToNRRDStatic = function() {
var id, m = {};
for(id in mapNRRDToJavascriptStatic) {
m[mapNRRDToJavascriptStatic[id]] = id;
}
return m;
}();
function mapNRRDToJavascript(id) {
// In any case, use the lower case version of the id
id = id.toLowerCase();
// Filter out any fields for which we have an explicit Javascript name
if (id in mapNRRDToJavascriptStatic) return mapNRRDToJavascriptStatic[id];
// Otherwise, just return the (lower case) id
return id;
}
function mapJavascriptToNRRD(id) {
// Filter out any fields for which we have an explicit NRRD name
if (id in mapJavascriptToNRRDStatic) return mapJavascriptToNRRDStatic[id];
// Otherwise, just return the id
return id;
}
function parseNRRDInteger(str) {
var val = parseInt(str, 10);
if (Number.isNaN(val)) throw new Error("Malformed NRRD integer: " + str);
return val;
}
function parseNRRDFloat(str) {
str = str.toLowerCase();
if (str.indexOf('nan')>=0) return NaN;
if (str.indexOf('-inf')>=0) return -Infinity;
if (str.indexOf('inf')>=0) return Infinity;
var val = parseFloat(str);
if (Number.isNaN(val)) throw new Error("Malformed NRRD float: " + str);
return val;
}
function parseNRRDVector(str) {
if (str == "none") return null;
if (str.length<2 || str[0]!=="(" || str[str.length-1]!==")") throw new Error("Malformed NRRD vector: " + str);
return str.slice(1, -1).split(",").map(parseNRRDFloat);
}
function parseNRRDQuotedString(str) {
if (length<2 || str[0]!='"' || str[str.length-1]!='"') {
throw new Error("Invalid NRRD quoted string: " + str);
}
return str.slice(1, -1).replace('\\"', '"');
}
function serializeNRRDQuotedString(str) {
return '"' + str.replace('"', '\\"') + '"';
}
var whitespaceListSeparator = /[ \t]+/; // Note that this excludes other types of whitespace on purpose!
function parseNRRDWhitespaceSeparatedList(str, parseElement) {
return str.split(whitespaceListSeparator).map(parseElement);
}
function parseNRRDType(descriptor) {
switch(descriptor.toLowerCase()) {
case "signed char":
case "int8":
case "int8_t":
return 'int8';
case "uchar":
case "unsigned char":
case "uint8":
case "uint8_t":
return 'uint8';
case "short":
case "short int":
case "signed short":
case "signed short int":
case "int16":
case "int16_t":
return 'int16';
case "ushort":
case "unsigned short":
case "unsigned short int":
case "uint16":
case "uint16_t":
return 'uint16';
case "int":
case "signed int":
case "int32":
case "int32_t":
return 'int32';
case "uint":
case "unsigned int":
case "uint32":
case "uint32_t":
return 'uint32';
case "longlong":
case "long long":
case "long long int":
case "signed long long":
case "signed long long int":
case "int64":
case "int64_t":
return 'int64';
case "ulonglong":
case "unsigned long long":
case "unsigned long long int":
case "uint64":
case "uint64_t":
return 'uint64';
case "float":
return 'float';
case "double":
return 'double';
case "block":
return 'block';
default:
console.warn("Unrecognized NRRD type: " + descriptor);
return descriptor;
}
}
function parseNRRDEncoding(encoding) {
switch(encoding.toLowerCase()) {
case "raw":
return "raw";
case "txt":
case "text":
case "ascii":
return "ascii";
case "hex":
return "hex";
case "gz":
case "gzip":
return "gzip";
case "bz2":
case "bzip2":
return "bzip2";
default:
console.warn("Unrecognized NRRD encoding: " + encoding);
return encoding;
}
}
function parseNRRDSpace(space) {
switch(space.toLowerCase()) {
case "right-anterior-superior":
case "ras":
return "right-anterior-superior";
case "left-anterior-superior":
case "las":
return "left-anterior-superior";
case "left-posterior-superior":
case "lps":
return "left-posterior-superior";
case "right-anterior-superior-time":
case "rast":
return "right-anterior-superior-time";
case "left-anterior-superior-time":
case "last":
return "left-anterior-superior-time";
case "left-posterior-superior-time":
case "lpst":
return "left-posterior-superior-time";
case "scanner-xyz":
return "scanner-xyz";
case "scanner-xyz-time":
return "scanner-xyz-time";
case "3d-right-handed":
return "3D-right-handed";
case "3d-left-handed":
return "3D-left-handed";
case "3d-right-handed-time":
return "3D-right-handed-time";
case "3d-left-handed-time":
return "3D-left-handed-time";
default:
console.warn("Unrecognized space: " + space);
return space;
}
}
function parseNRRDEndian(endian) {
switch(endian.toLowerCase()) {
case 'little':
return 'little';
case 'big':
return 'big';
default:
console.warn("Unrecognized NRRD endianness: " + endian);
return endian;
}
}
// Note that this function will never encounter the LIST data file specification format, as this is handled elsewhere.
var dataFileFormatRE = / (-?\d+) (-?\d+) (-?\d+)(?: (\d+))?$/;
function parseNRRDDataFile(dataFile) {
var match = dataFileFormatRE.exec(dataFile);
if (match) { // We have a format specification
if (match.length == 5 && match[4]) { // subdim specification
return {
format: dataFile.substring(0, match.index),
min: parseNRRDInteger(match[1]),
max: parseNRRDInteger(match[2]),
step: parseNRRDInteger(match[3]),
subdim: parseNRRDInteger(match[4])
};
} else {
return {
format: dataFile.substring(0, match.index),
min: parseNRRDInteger(match[1]),
max: parseNRRDInteger(match[2]),
step: parseNRRDInteger(match[3])
};
}
} else { // Just a file
return dataFile;
}
}
function serializeNRRDDataFile(dataFile) {
if ((typeof dataFile) == "string" || dataFile instanceof String) {
return dataFile;
} else if ('format' in dataFile && 'min' in dataFile && 'max' in dataFile && 'step' in dataFile) {
if ('subdim' in dataFile) {
return dataFile.format + " " + dataFile.min + " " + dataFile.max + " " + dataFile.step + " " + dataFile.subdim;
} else {
return dataFile.format + " " + dataFile.min + " " + dataFile.max + " " + dataFile.step;
}
} else {
throw new Error("Unrecognized data file format!");
}
}
function parseNRRDCenter(center) {
switch(center.toLowerCase()) {
case "cell":
return "cell";
case "node":
return "node";
case "???":
case "none":
return null;
default:
console.warn("Unrecognized NRRD center: " + center);
return center;
}
}
var NRRDKinds = {
"domain": "domain",
"space": "space",
"time": "time",
"list": "list",
"point": "point",
"vector": "vector",
"covariant-vector": "covariant-vector",
"normal": "normal",
"stub": "stub",
"scalar": "scalar",
"complex": "complex",
"2-vector": "2-vector",
"3-color": "3-color",
"rgb-color": "RGB-color",
"hsv-color": "HSV-color",
"xyz-color": "XYZ-color",
"4-color": "4-color",
"rgba-color": "RGBA-color",
"3-vector": "3-vector",
"3-gradient": "3-gradient",
"3-normal": "3-normal",
"4-vector": "4-vector",
"quaternion": "quaternion",
"2d-symmetric-matrix": "2D-symmetric-matrix",
"2d-masked-symmetric-matrix": "2D-masked-symmetric-matrix",
"2d-matrix": "2D-matrix",
"2d-masked-matrix": "2D-masked-matrix",
"3d-symmetric-matrix": "3D-symmetric-matrix",
"3d-masked-symmetric-matrix": "3D-masked-symmetric-matrix",
"3d-matrix": "3D-matrix",
"3d-masked-matrix": "3D-masked-matrix",
"???": null,
"none": null
};
function parseNRRDKind(kind) {
var kindLC = kind.toLowerCase();
if (kindLC in NRRDKinds) return NRRDKinds[kindLC];
console.warn("Unrecognized NRRD kind: " + kind);
return kind;
}
function serializeNRRDOptional(a) {
return a===null ? "???" : a;
}
var systemEndianness = (function() {
var buf = new ArrayBuffer(4),
intArr = new Uint32Array(buf),
byteArr = new Uint8Array(buf);
intArr[0] = 0x01020304;
if (byteArr[0]==1 && byteArr[1]==2 && byteArr[2]==3 && byteArr[3]==4) {
return 'big';
} else if (byteArr[0]==4 && byteArr[1]==3 && byteArr[2]==2 && byteArr[3]==1) {
return 'little';
}
console.warn("Unrecognized system endianness!");
return undefined;
})();
function parseNRRDRawData(buffer, type, sizes, options) {
var i, arr, view, totalLen = 1, endianFlag;
for(i=0; i<sizes.length; i++) {
if (sizes[i]<=0) throw new Error("Sizes should be a list of positive (>0) integers!");
totalLen *= sizes[i];
}
if (type == 'block') {
// Don't do anything special, just return the slice containing all blocks.
return buffer.slice(0,totalLen*options.blockSize);
} else if (type == 'int8' || type == 'uint8' || options.endian == systemEndianness) {
switch(type) {
case "int8":
checkSize(1);
return new Int8Array(buffer.slice(0,totalLen));
case "uint8":
checkSize(1);
return new Uint8Array(buffer.slice(0,totalLen));
case "int16":
checkSize(2);
return new Int16Array(buffer.slice(0,totalLen*2));
case "uint16":
checkSize(2);
return new Uint16Array(buffer.slice(0,totalLen*2));
case "int32":
checkSize(4);
return new Int32Array(buffer.slice(0,totalLen*4));
case "uint32":
checkSize(4);
return new Uint32Array(buffer.slice(0,totalLen*4));
//case "int64":
// checkSize(8);
// return new Int64Array(buffer.slice(0,totalLen*8));
//case "uint64":
// checkSize(8);
// return new Uint64Array(buffer.slice(0,totalLen*8));
case "float":
checkSize(4);
return new Float32Array(buffer.slice(0,totalLen*4));
case "double":
checkSize(8);
return new Float64Array(buffer.slice(0,totalLen*8));
default:
console.warn("Unsupported NRRD type: " + type + ", returning raw buffer.");
return undefined;
}
} else {
switch(options.endian) {
case 'big':
endianFlag = false;
break;
case 'little':
endianFlag = true;
break;
default:
console.warn("Unsupported endianness in NRRD file: " + options.endian);
return undefined;
}
view = new DataView(buffer);
switch(type) {
case "int8": // Note that here we do not need to check the size of the buffer, as the DataView.get methods should throw an exception if we read beyond the buffer.
arr = new Int8Array(totalLen);
for(i=0; i<totalLen; i++) {
arr[i] = view.getInt8(i);
}
return arr;
case "uint8":
arr = new Uint8Array(totalLen);
for(i=0; i<totalLen; i++) {
arr[i] = view.getUint8(i);
}
return arr;
case "int16":
arr = new Int16Array(totalLen);
for(i=0; i<totalLen; i++) {
arr[i] = view.getInt16(i*2);
}
return arr;
case "uint16":
arr = new Uint16Array(totalLen);
for(i=0; i<totalLen; i++) {
arr[i] = view.getUint16(i*2);
}
return arr;
case "int32":
arr = new Int32Array(totalLen);
for(i=0; i<totalLen; i++) {
arr[i] = view.getInt32(i*4);
}
return arr;
case "uint32":
arr = new Uint32Array(totalLen);
for(i=0; i<totalLen; i++) {
arr[i] = view.getUint32(i*4);
}
return arr;
//case "int64":
// arr = new Int64Array(totalLen);
// for(i=0; i<totalLen; i++) {
// arr[i] = view.getInt64(i*8);
// }
// return arr;
//case "uint64":
// arr = new Uint64Array(totalLen);
// for(i=0; i<totalLen; i++) {
// arr[i] = view.getUint64(i*8);
// }
// return arr;
case "float":