forked from cornerstonejs/dicomParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicomParser.js
2682 lines (2371 loc) · 95.2 KB
/
dicomParser.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
/*! dicom-parser - v1.7.4 - 2016-08-18 | (c) 2014 Chris Hafey | https://github.com/chafey/dicomParser */
(function (root, factory) {
// node.js
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
}
else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else {
// Browser globals
if(typeof cornerstone === 'undefined'){
dicomParser = {};
// meteor
if (typeof Package !== 'undefined') {
root.dicomParser = dicomParser;
}
}
dicomParser = factory();
}
}(this, function () {
/**
* Parses a DICOM P10 byte array and returns a DataSet object with the parsed elements. If the options
* argument is supplied and it contains the untilTag property, parsing will stop once that
* tag is encoutered. This can be used to parse partial byte streams.
*
* @param byteArray the byte array
* @param options object to control parsing behavior (optional)
* @returns {DataSet}
* @throws error if an error occurs while parsing. The exception object will contain a property dataSet with the
* elements successfully parsed before the error.
*/
var dicomParser = (function(dicomParser) {
if(dicomParser === undefined)
{
dicomParser = {};
}
dicomParser.parseDicom = function(byteArray, options) {
if(byteArray === undefined)
{
throw "dicomParser.parseDicom: missing required parameter 'byteArray'";
}
function readTransferSyntax(metaHeaderDataSet) {
if(metaHeaderDataSet.elements.x00020010 === undefined) {
throw 'dicomParser.parseDicom: missing required meta header attribute 0002,0010';
}
var transferSyntaxElement = metaHeaderDataSet.elements.x00020010;
return dicomParser.readFixedString(byteArray, transferSyntaxElement.dataOffset, transferSyntaxElement.length);
}
function isExplicit(transferSyntax) {
if(transferSyntax === '1.2.840.10008.1.2') // implicit little endian
{
return false;
}
// all other transfer syntaxes should be explicit
return true;
}
function getDataSetByteStream(transferSyntax, position) {
if(transferSyntax === '1.2.840.10008.1.2.1.99')
{
// if an infalter callback is registered, use it
if (options && options.inflater) {
var fullByteArrayCallback = options.inflater(byteArray, position);
return new dicomParser.ByteStream(dicomParser.littleEndianByteArrayParser, fullByteArrayCallback, 0);
}
// if running on node, use the zlib library to inflate
// http://stackoverflow.com/questions/4224606/how-to-check-whether-a-script-is-running-under-node-js
else if (typeof module !== 'undefined' && this.module !== module) {
// inflate it
var zlib = require('zlib');
var deflatedBuffer = dicomParser.sharedCopy(byteArray, position, byteArray.length - position);
var inflatedBuffer = zlib.inflateRawSync(deflatedBuffer);
// create a single byte array with the full header bytes and the inflated bytes
var fullByteArrayBuffer = dicomParser.alloc(byteArray, inflatedBuffer.length + position);
byteArray.copy(fullByteArrayBuffer, 0, 0, position);
inflatedBuffer.copy(fullByteArrayBuffer, position);
return new dicomParser.ByteStream(dicomParser.littleEndianByteArrayParser, fullByteArrayBuffer, 0);
}
// if pako is defined - use it. This is the web browser path
// https://github.com/nodeca/pako
else if(typeof pako !== "undefined") {
// inflate it
var deflated = byteArray.slice(position);
var inflated = pako.inflateRaw(deflated);
// create a single byte array with the full header bytes and the inflated bytes
var fullByteArray = dicomParser.alloc(byteArray, inflated.length + position);
fullByteArray.set(byteArray.slice(0, position), 0);
fullByteArray.set(inflated, position);
return new dicomParser.ByteStream(dicomParser.littleEndianByteArrayParser, fullByteArray, 0);
}
// throw exception since no inflater is available
else {
throw 'dicomParser.parseDicom: no inflater available to handle deflate transfer syntax';
}
}
if(transferSyntax === '1.2.840.10008.1.2.2') // explicit big endian
{
return new dicomParser.ByteStream(dicomParser.bigEndianByteArrayParser, byteArray, position);
}
else
{
// all other transfer syntaxes are little endian; only the pixel encoding differs
// make a new stream so the metaheader warnings don't come along for the ride
return new dicomParser.ByteStream(dicomParser.littleEndianByteArrayParser, byteArray, position);
}
}
function mergeDataSets(metaHeaderDataSet, instanceDataSet)
{
for (var propertyName in metaHeaderDataSet.elements)
{
if(metaHeaderDataSet.elements.hasOwnProperty(propertyName))
{
instanceDataSet.elements[propertyName] = metaHeaderDataSet.elements[propertyName];
}
}
if (metaHeaderDataSet.warnings !== undefined) {
instanceDataSet.warnings = metaHeaderDataSet.warnings.concat(instanceDataSet.warnings);
}
return instanceDataSet;
}
function readDataSet(metaHeaderDataSet)
{
var transferSyntax = readTransferSyntax(metaHeaderDataSet);
var explicit = isExplicit(transferSyntax);
var dataSetByteStream = getDataSetByteStream(transferSyntax, metaHeaderDataSet.position);
var elements = {};
var dataSet = new dicomParser.DataSet(dataSetByteStream.byteArrayParser, dataSetByteStream.byteArray, elements);
dataSet.warnings = dataSetByteStream.warnings;
try{
if(explicit) {
dicomParser.parseDicomDataSetExplicit(dataSet, dataSetByteStream, dataSetByteStream.byteArray.length, options);
}
else
{
dicomParser.parseDicomDataSetImplicit(dataSet, dataSetByteStream, dataSetByteStream.byteArray.length, options);
}
}
catch(e) {
var ex = {
exception: e,
dataSet: dataSet
};
throw ex;
}
return dataSet;
}
// main function here
function parseTheByteStream() {
var metaHeaderDataSet = dicomParser.readPart10Header(byteArray, options);
var dataSet = readDataSet(metaHeaderDataSet);
return mergeDataSets(metaHeaderDataSet, dataSet);
}
// This is where we actually start parsing
return parseTheByteStream();
};
return dicomParser;
})(dicomParser);
/**
* Utility function for creating a basic offset table for JPEG transfer syntaxes
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
// Each JPEG image has an end of image marker 0xFFD9
function isEndOfImageMarker(dataSet, position) {
return (dataSet.byteArray[position] === 0xFF &&
dataSet.byteArray[position + 1] === 0xD9);
}
function isFragmentEndOfImage(dataSet, pixelDataElement, fragmentIndex) {
var fragment = pixelDataElement.fragments[fragmentIndex];
// Need to check the last two bytes and the last three bytes for marker since odd length
// fragments are zero padded
if(isEndOfImageMarker(dataSet, fragment.position + fragment.length - 2) ||
isEndOfImageMarker(dataSet, fragment.position + fragment.length - 3)) {
return true;
}
return false;
}
function findLastImageFrameFragmentIndex(dataSet, pixelDataElement, startFragment) {
for(var fragmentIndex=startFragment; fragmentIndex < pixelDataElement.fragments.length; fragmentIndex++) {
if(isFragmentEndOfImage(dataSet, pixelDataElement, fragmentIndex)) {
return fragmentIndex;
}
}
}
/**
* Creates a basic offset table by scanning fragments for JPEG start of image and end Of Image markers
* @param {object} dataSet - the parsed dicom dataset
* @param {object} pixelDataElement - the pixel data element
* @param [fragments] - optional array of objects describing each fragment (offset, position, length)
* @returns {Array} basic offset table (array of offsets to beginning of each frame)
*/
dicomParser.createJPEGBasicOffsetTable = function(dataSet, pixelDataElement, fragments) {
// Validate parameters
if(dataSet === undefined) {
throw 'dicomParser.createJPEGBasicOffsetTable: missing required parameter dataSet';
}
if(pixelDataElement === undefined) {
throw 'dicomParser.createJPEGBasicOffsetTable: missing required parameter pixelDataElement';
}
if(pixelDataElement.tag !== 'x7fe00010') {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'pixelDataElement' refers to non pixel data tag (expected tag = x7fe00010'";
}
if(pixelDataElement.encapsulatedPixelData !== true) {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'pixelDataElement' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.hadUndefinedLength !== true) {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'pixelDataElement' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.basicOffsetTable === undefined) {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'pixelDataElement' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.fragments === undefined) {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'pixelDataElement' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.fragments.length <= 0) {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'pixelDataElement' refers to pixel data element that does not have encapsulated pixel data";
}
if(fragments && fragments.length <=0) {
throw "dicomParser.createJPEGBasicOffsetTable: parameter 'fragments' must not be zero length";
}
// Default values
fragments = fragments || pixelDataElement.fragments;
var basicOffsetTable = [];
var startFragmentIndex = 0;
while(true) {
// Add the offset for the start fragment
basicOffsetTable.push(pixelDataElement.fragments[startFragmentIndex].offset);
var endFragmentIndex = findLastImageFrameFragmentIndex(dataSet, pixelDataElement, startFragmentIndex);
if(endFragmentIndex === undefined || endFragmentIndex === pixelDataElement.fragments.length -1) {
return basicOffsetTable;
}
startFragmentIndex = endFragmentIndex + 1;
}
};
return dicomParser;
}(dicomParser));
var dicomParser = (function (dicomParser) {
"use strict";
if (dicomParser === undefined) {
dicomParser = {};
}
/**
* converts an explicit dataSet to a javascript object
* @param dataSet
* @param options
*/
dicomParser.explicitDataSetToJS = function (dataSet, options) {
if(dataSet === undefined) {
throw 'dicomParser.explicitDataSetToJS: missing required parameter dataSet';
}
options = options || {
omitPrivateAttibutes: true, // true if private elements should be omitted
maxElementLength : 128 // maximum element length to try and convert to string format
};
var result = {
};
for(var tag in dataSet.elements) {
var element = dataSet.elements[tag];
// skip this element if it a private element and our options specify that we should
if(options.omitPrivateAttibutes === true && dicomParser.isPrivateTag(tag))
{
continue;
}
if(element.items) {
// handle sequences
var sequenceItems = [];
for(var i=0; i < element.items.length; i++) {
sequenceItems.push(dicomParser.explicitDataSetToJS(element.items[i].dataSet, options));
}
result[tag] = sequenceItems;
} else {
var asString;
asString = undefined;
if(element.length < options.maxElementLength) {
asString = dicomParser.explicitElementToString(dataSet, element);
}
if(asString !== undefined) {
result[tag] = asString;
} else {
result[tag] = {
dataOffset: element.dataOffset,
length : element.length
};
}
}
}
return result;
};
return dicomParser;
}(dicomParser));
var dicomParser = (function (dicomParser) {
"use strict";
if (dicomParser === undefined) {
dicomParser = {};
}
/**
* Converts an explicit VR element to a string or undefined if it is not possible to convert.
* Throws an error if an implicit element is supplied
* @param dataSet
* @param element
* @returns {*}
*/
dicomParser.explicitElementToString = function(dataSet, element)
{
if(dataSet === undefined || element === undefined) {
throw 'dicomParser.explicitElementToString: missing required parameters';
}
if(element.vr === undefined) {
throw 'dicomParser.explicitElementToString: cannot convert implicit element to string';
}
var vr = element.vr;
var tag = element.tag;
var textResult;
function multiElementToString(numItems, func) {
var result = "";
for(var i=0; i < numItems; i++) {
if(i !== 0) {
result += '/';
}
result += func.call(dataSet, tag, i).toString();
}
return result;
}
if(dicomParser.isStringVr(vr) === true)
{
textResult = dataSet.string(tag);
}
else if (vr == 'AT') {
var num = dataSet.uint32(tag);
if(num === undefined) {
return undefined;
}
if (num < 0)
{
num = 0xFFFFFFFF + num + 1;
}
return 'x' + num.toString(16).toUpperCase();
}
else if (vr == 'US')
{
textResult = multiElementToString(element.length / 2, dataSet.uint16);
}
else if(vr === 'SS')
{
textResult = multiElementToString(element.length / 2, dataSet.int16);
}
else if (vr == 'UL')
{
textResult = multiElementToString(element.length / 4, dataSet.uint32);
}
else if(vr === 'SL')
{
textResult = multiElementToString(element.length / 4, dataSet.int32);
}
else if(vr == 'FD')
{
textResult = multiElementToString(element.length / 8, dataSet.double);
}
else if(vr == 'FL')
{
textResult = multiElementToString(element.length / 4, dataSet.float);
}
return textResult;
};
return dicomParser;
}(dicomParser));
/**
* Utility functions for dealing with DICOM
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
// algorithm based on http://stackoverflow.com/questions/1433030/validate-number-of-days-in-a-given-month
function daysInMonth(m, y) { // m is 0 indexed: 0-11
switch (m) {
case 2 :
return (y % 4 == 0 && y % 100) || y % 400 == 0 ? 29 : 28;
case 9 : case 4 : case 6 : case 11 :
return 30;
default :
return 31
}
}
function isValidDate(d, m, y) {
// make year is a number
if(isNaN(y)) {
return false;
}
return m > 0 && m <= 12 && d > 0 && d <= daysInMonth(m, y);
}
/**
* Parses a DA formatted string into a Javascript object
* @param {string} date a string in the DA VR format
* @param {boolean} [validate] - true if an exception should be thrown if the date is invalid
* @returns {*} Javascript object with properties year, month and day or undefined if not present or not 8 bytes long
*/
dicomParser.parseDA = function(date, validate)
{
if(date && date.length === 8)
{
var yyyy = parseInt(date.substring(0, 4), 10);
var mm = parseInt(date.substring(4, 6), 10);
var dd = parseInt(date.substring(6, 8), 10);
if(validate) {
if (isValidDate(dd, mm, yyyy) !== true) {
throw "invalid DA '" + date + "'";
}
}
return {
year: yyyy,
month: mm,
day: dd
};
}
if(validate) {
throw "invalid DA '" + date + "'";
}
return undefined;
};
return dicomParser;
}(dicomParser));
/**
* Utility functions for dealing with DICOM
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
/**
* Parses a TM formatted string into a javascript object with properties for hours, minutes, seconds and fractionalSeconds
* @param {string} time - a string in the TM VR format
* @param {boolean} [validate] - true if an exception should be thrown if the date is invalid
* @returns {*} javascript object with properties for hours, minutes, seconds and fractionalSeconds or undefined if no element or data. Missing fields are set to undefined
*/
dicomParser.parseTM = function(time, validate) {
if (time.length >= 2) // must at least have HH
{
// 0123456789
// HHMMSS.FFFFFF
var hh = parseInt(time.substring(0, 2), 10);
var mm = time.length >= 4 ? parseInt(time.substring(2, 4), 10) : undefined;
var ss = time.length >= 6 ? parseInt(time.substring(4, 6), 10) : undefined;
var ffffff = time.length >= 8 ? parseInt(time.substring(7, 13), 10) : undefined;
if(validate) {
if((isNaN(hh)) ||
(mm !== undefined && isNaN(mm)) ||
(ss !== undefined && isNaN(ss)) ||
(ffffff !== undefined && isNaN(ffffff)) ||
(hh < 0 || hh > 23) ||
(mm && (mm <0 || mm > 59)) ||
(ss && (ss <0 || ss > 59)) ||
(ffffff && (ffffff <0 || ffffff > 999999)))
{
throw "invalid TM '" + time + "'";
}
}
return {
hours: hh,
minutes: mm,
seconds: ss,
fractionalSeconds: ffffff
};
}
if(validate) {
throw "invalid TM '" + time + "'";
}
return undefined;
};
return dicomParser;
}(dicomParser));
/**
* Utility functions for dealing with DICOM
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
var stringVrs = {
AE: true,
AS: true,
AT: false,
CS: true,
DA: true,
DS: true,
DT: true,
FL: false,
FD: false,
IS: true,
LO: true,
LT: true,
OB: false,
OD: false,
OF: false,
OW: false,
PN: true,
SH: true,
SL: false,
SQ: false,
SS: false,
ST: true,
TM: true,
UI: true,
UL: false,
UN: undefined, // dunno
UR: true,
US: false,
UT: true
};
/**
* Tests to see if vr is a string or not.
* @param vr
* @returns true if string, false it not string, undefined if unknown vr or UN type
*/
dicomParser.isStringVr = function(vr)
{
return stringVrs[vr];
};
/**
* Tests to see if a given tag in the format xggggeeee is a private tag or not
* @param tag
* @returns {boolean}
*/
dicomParser.isPrivateTag = function(tag)
{
var lastGroupDigit = parseInt(tag[4]);
var groupIsOdd = (lastGroupDigit % 2) === 1;
return groupIsOdd;
};
/**
* Parses a PN formatted string into a javascript object with properties for givenName, familyName, middleName, prefix and suffix
* @param personName a string in the PN VR format
* @param index
* @returns {*} javascript object with properties for givenName, familyName, middleName, prefix and suffix or undefined if no element or data
*/
dicomParser.parsePN = function(personName) {
if(personName === undefined) {
return undefined;
}
var stringValues = personName.split('^');
return {
familyName: stringValues[0],
givenName: stringValues[1],
middleName: stringValues[2],
prefix: stringValues[3],
suffix: stringValues[4]
};
};
return dicomParser;
}(dicomParser));
/**
* Functionality for extracting encapsulated pixel data
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
var deprecatedNoticeLogged = false;
/**
* Returns the pixel data for the specified frame in an encapsulated pixel data element. If no basic offset
* table is present, it assumes that all fragments are for one frame. Note that this assumption/logic is not
* valid for multi-frame instances so this function has been deprecated and will eventually be removed. Code
* should be updated to use readEncapsulatedPixelDataFromFragments() or readEncapsulatedImageFrame()
*
* @deprecated since version 1.6 - use readEncapsulatedPixelDataFromFragments() or readEncapsulatedImageFrame()
* @param dataSet - the dataSet containing the encapsulated pixel data
* @param pixelDataElement - the pixel data element (x7fe00010) to extract the frame from
* @param frame - the zero based frame index
* @returns {object} with the encapsulated pixel data
*/
dicomParser.readEncapsulatedPixelData = function(dataSet, pixelDataElement, frame)
{
if(!deprecatedNoticeLogged) {
deprecatedNoticeLogged = true;
if(console && console.log) {
console.log("WARNING: dicomParser.readEncapsulatedPixelData() has been deprecated");
}
}
if(dataSet === undefined) {
throw "dicomParser.readEncapsulatedPixelData: missing required parameter 'dataSet'";
}
if(pixelDataElement === undefined) {
throw "dicomParser.readEncapsulatedPixelData: missing required parameter 'element'";
}
if(frame === undefined) {
throw "dicomParser.readEncapsulatedPixelData: missing required parameter 'frame'";
}
if(pixelDataElement.tag !== 'x7fe00010') {
throw "dicomParser.readEncapsulatedPixelData: parameter 'element' refers to non pixel data tag (expected tag = x7fe00010'";
}
if(pixelDataElement.encapsulatedPixelData !== true) {
throw "dicomParser.readEncapsulatedPixelData: parameter 'element' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.hadUndefinedLength !== true) {
throw "dicomParser.readEncapsulatedPixelData: parameter 'element' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.basicOffsetTable === undefined) {
throw "dicomParser.readEncapsulatedPixelData: parameter 'element' refers to pixel data element that does not have encapsulated pixel data";
}
if(pixelDataElement.fragments === undefined) {
throw "dicomParser.readEncapsulatedPixelData: parameter 'element' refers to pixel data element that does not have encapsulated pixel data";
}
if(frame < 0) {
throw "dicomParser.readEncapsulatedPixelData: parameter 'frame' must be >= 0";
}
// If the basic offset table is not empty, we can extract the frame
if(pixelDataElement.basicOffsetTable.length !== 0)
{
return dicomParser.readEncapsulatedImageFrame(dataSet, pixelDataElement, frame);
}
else
{
// No basic offset table, assume all fragments are for one frame - NOTE that this is NOT a valid
// assumption but is the original behavior so we are keeping it for now
return dicomParser.readEncapsulatedPixelDataFromFragments(dataSet, pixelDataElement, 0, pixelDataElement.fragments.length);
}
};
return dicomParser;
}(dicomParser));
/**
*
* Internal helper function to allocate new byteArray buffers
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
/**
* Creates a new byteArray of the same type (Uint8Array or Buffer) of the specified length.
* @param byteArray the underlying byteArray (either Uint8Array or Buffer)
* @param length number of bytes of the Byte Array
* @returns {object} Uint8Array or Buffer depending on the type of byteArray
*/
dicomParser.alloc = function(byteArray, length) {
if (typeof Buffer !== 'undefined' && byteArray instanceof Buffer) {
return Buffer.alloc(length);
}
else if(byteArray instanceof Uint8Array) {
return new Uint8Array(length);
} else {
throw 'dicomParser.alloc: unknown type for byteArray';
}
};
return dicomParser;
}(dicomParser));
/**
* Internal helper functions for parsing different types from a big-endian byte array
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
dicomParser.bigEndianByteArrayParser = {
/**
*
* Parses an unsigned int 16 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed unsigned int 16
* @throws error if buffer overread would occur
* @access private
*/
readUint16: function (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readUint16: position cannot be less than 0';
}
if (position + 2 > byteArray.length) {
throw 'bigEndianByteArrayParser.readUint16: attempt to read past end of buffer';
}
return (byteArray[position] << 8) + byteArray[position + 1];
},
/**
*
* Parses a signed int 16 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed signed int 16
* @throws error if buffer overread would occur
* @access private
*/
readInt16: function (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readInt16: position cannot be less than 0';
}
if (position + 2 > byteArray.length) {
throw 'bigEndianByteArrayParser.readInt16: attempt to read past end of buffer';
}
var int16 = (byteArray[position] << 8) + byteArray[position + 1];
// fix sign
if (int16 & 0x8000) {
int16 = int16 - 0xFFFF - 1;
}
return int16;
},
/**
* Parses an unsigned int 32 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed unsigned int 32
* @throws error if buffer overread would occur
* @access private
*/
readUint32: function (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readUint32: position cannot be less than 0';
}
if (position + 4 > byteArray.length) {
throw 'bigEndianByteArrayParser.readUint32: attempt to read past end of buffer';
}
var uint32 = (256 * (256 * (256 * byteArray[position] +
byteArray[position + 1]) +
byteArray[position + 2]) +
byteArray[position + 3]);
return uint32;
},
/**
* Parses a signed int 32 from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed signed int 32
* @throws error if buffer overread would occur
* @access private
*/
readInt32: function (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readInt32: position cannot be less than 0';
}
if (position + 4 > byteArray.length) {
throw 'bigEndianByteArrayParser.readInt32: attempt to read past end of buffer';
}
var int32 = ((byteArray[position] << 24) +
(byteArray[position + 1] << 16) +
(byteArray[position + 2] << 8) +
byteArray[position + 3]);
return int32;
},
/**
* Parses 32-bit float from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed 32-bit float
* @throws error if buffer overread would occur
* @access private
*/
readFloat: function (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readFloat: position cannot be less than 0';
}
if (position + 4 > byteArray.length) {
throw 'bigEndianByteArrayParser.readFloat: attempt to read past end of buffer';
}
// I am sure there is a better way than this but this should be safe
var byteArrayForParsingFloat = new Uint8Array(4);
byteArrayForParsingFloat[3] = byteArray[position];
byteArrayForParsingFloat[2] = byteArray[position + 1];
byteArrayForParsingFloat[1] = byteArray[position + 2];
byteArrayForParsingFloat[0] = byteArray[position + 3];
var floatArray = new Float32Array(byteArrayForParsingFloat.buffer);
return floatArray[0];
},
/**
* Parses 64-bit float from a big-endian byte array
*
* @param byteArray the byte array to read from
* @param position the position in the byte array to read from
* @returns {*} the parsed 64-bit float
* @throws error if buffer overread would occur
* @access private
*/
readDouble: function (byteArray, position) {
if (position < 0) {
throw 'bigEndianByteArrayParser.readDouble: position cannot be less than 0';
}
if (position + 8 > byteArray.length) {
throw 'bigEndianByteArrayParser.readDouble: attempt to read past end of buffer';
}
// I am sure there is a better way than this but this should be safe
var byteArrayForParsingFloat = new Uint8Array(8);
byteArrayForParsingFloat[7] = byteArray[position];
byteArrayForParsingFloat[6] = byteArray[position + 1];
byteArrayForParsingFloat[5] = byteArray[position + 2];
byteArrayForParsingFloat[4] = byteArray[position + 3];
byteArrayForParsingFloat[3] = byteArray[position + 4];
byteArrayForParsingFloat[2] = byteArray[position + 5];
byteArrayForParsingFloat[1] = byteArray[position + 6];
byteArrayForParsingFloat[0] = byteArray[position + 7];
var floatArray = new Float64Array(byteArrayForParsingFloat.buffer);
return floatArray[0];
}
};
return dicomParser;
}(dicomParser));
/**
* Internal helper functions common to parsing byte arrays of any type
*/
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}
/**
* Reads a string of 8-bit characters from an array of bytes and advances
* the position by length bytes. A null terminator will end the string
* but will not effect advancement of the position. Trailing and leading
* spaces are preserved (not trimmed)
* @param byteArray the byteArray to read from
* @param position the position in the byte array to read from
* @param length the maximum number of bytes to parse
* @returns {string} the parsed string
* @throws error if buffer overread would occur
* @access private
*/
dicomParser.readFixedString = function(byteArray, position, length)
{
if(length < 0)
{
throw 'dicomParser.readFixedString - length cannot be less than 0';
}
if(position + length > byteArray.length) {
throw 'dicomParser.readFixedString: attempt to read past end of buffer';
}
var result = "";
var byte;
for(var i=0; i < length; i++)
{
byte = byteArray[position + i];
if(byte === 0) {
position += length;
return result;
}
result += String.fromCharCode(byte);
}
return result;
};
return dicomParser;
}(dicomParser));
/**
*
* Internal helper class to assist with parsing. Supports reading from a byte
* stream contained in a Uint8Array. Example usage:
*
* var byteArray = new Uint8Array(32);
* var byteStream = new dicomParser.ByteStream(dicomParser.littleEndianByteArrayParser, byteArray);
*
* */
var dicomParser = (function (dicomParser)
{
"use strict";
if(dicomParser === undefined)
{
dicomParser = {};
}