-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathdata.test.js
1641 lines (1464 loc) · 56.2 KB
/
data.test.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
import "regenerator-runtime/runtime.js";
import { jest } from "@jest/globals";
import fs from "fs";
import fsPromises from "fs/promises";
import path from "path";
import { WriteBufferStream } from "../src/BufferStream";
import dcmjs from "../src/index.js";
import { getTestDataset, getZippedTestDataset } from "./testUtils.js";
import { log } from "./../src/log.js";
import { promisify } from "util";
import arrayItem from "./arrayItem.json";
import minimalDataset from "./mocks/minimal_fields_dataset.json";
import datasetWithNullNumberVRs from "./mocks/null_number_vrs_dataset.json";
import sampleDicomSR from "./sample-sr.json";
import { rawTags } from "./rawTags";
import {
EXPLICIT_LITTLE_ENDIAN,
IMPLICIT_LITTLE_ENDIAN,
PADDING_SPACE
} from "./../src/constants/dicom.js";
import { ValueRepresentation } from "../src/ValueRepresentation";
const { DicomMetaDictionary, DicomDict, DicomMessage, ReadBufferStream } =
dcmjs.data;
const fileMetaInformationVersionArray = new Uint8Array(2);
fileMetaInformationVersionArray[1] = 1;
// The asset downloads in this file might take some time on a slower connection
jest.setTimeout(60000);
const metadata = {
"00020001": {
Value: [fileMetaInformationVersionArray.buffer],
vr: "OB"
},
"00020012": {
Value: ["1.2.840.113819.7.1.1997.1.0"],
vr: "UI"
},
"00020002": {
Value: ["1.2.840.10008.5.1.4.1.1.4"],
vr: "UI"
},
"00020003": {
Value: [DicomMetaDictionary.uid()],
vr: "UI"
},
"00020010": {
Value: ["1.2.840.10008.1.2"],
vr: "UI"
}
};
const sequenceMetadata = {
"00080081": { vr: "ST", Value: [null] },
"00081032": {
vr: "SQ",
Value: [
{
"00080100": {
vr: "SH",
Value: ["IMG1332"]
},
"00080102": {
vr: "SH",
Value: ["L"]
},
"00080104": {
vr: "LO",
Value: ["MRI SHOULDER WITHOUT IV CONTRAST LEFT"]
}
}
]
},
52009229: {
vr: "SQ",
Value: [
{
"00289110": {
vr: "SQ",
Value: [
{
"00180088": {
vr: "DS",
Value: [0.12]
}
}
]
}
}
]
}
};
function makeOverlayBitmap({ width, height }) {
const topBottom = new Array(width).fill(1, 0, width);
const middle = new Array(width).fill(0, 0, width);
const bitmap = [];
middle[0] = 1;
middle[width - 1] = 1;
bitmap.push(topBottom);
for (let i = 0; i < height - 2; i++) {
bitmap.push(middle);
}
bitmap.push(topBottom);
return bitmap.flat();
}
it("test_array_items", () => {
const dicomJSON = JSON.stringify(arrayItem);
const datasets = JSON.parse(dicomJSON);
const natural0 = DicomMetaDictionary.naturalizeDataset(datasets[0]);
// Shouldn't throw an exception
const natural0b = DicomMetaDictionary.naturalizeDataset(datasets[0]);
// And should be identical to the previous version
expect(natural0b).toEqual(natural0);
});
it("test_json_1", () => {
//
// multiple results example
// from http://dicom.nema.org/medical/dicom/current/output/html/part18.html#chapter_F
//
const dicomJSON = `
[
{
"0020000D": {
"vr": "UI",
"Value": [ "1.2.392.200036.9116.2.2.2.1762893313.1029997326.945873" ]
}
},
{
"0020000D" : {
"vr": "UI",
"Value": [ "1.2.392.200036.9116.2.2.2.2162893313.1029997326.945876" ]
}
}
]
`;
const datasets = JSON.parse(dicomJSON);
const firstUID = datasets[0]["0020000D"].Value[0];
const secondUID = datasets[1]["0020000D"].Value[0];
//
// make a natural version of the first study and confirm it has correct value
//
const naturalDICOM = DicomMetaDictionary.naturalizeDataset(datasets[0]);
expect(naturalDICOM.StudyInstanceUID).toEqual(firstUID);
//
// make a natural version of a dataset with sequence tags and confirm it has correct values
//
const naturalSequence =
DicomMetaDictionary.naturalizeDataset(sequenceMetadata);
// The match object needs to be done on the actual element, not the proxied value
expect(naturalSequence.ProcedureCodeSequence[0]).toMatchObject({
CodeValue: "IMG1332"
});
// tests that single element sequences have been converted
// from arrays to values.
// See discussion here for more details: https://github.com/dcmjs-org/dcmjs/commit/74571a4bd6c793af2a679a31cec7e197f93e28cc
const spacing =
naturalSequence.SharedFunctionalGroupsSequence.PixelMeasuresSequence
.SpacingBetweenSlices;
expect(spacing).toEqual(0.12);
expect(
Array.isArray(naturalSequence.SharedFunctionalGroupsSequence)
).toEqual(true);
expect(naturalSequence.ProcedureCodeSequence[0]).toMatchObject({
CodingSchemeDesignator: "L",
CodeMeaning: "MRI SHOULDER WITHOUT IV CONTRAST LEFT"
});
// expect original data to remain unnaturalized
expect(sequenceMetadata["00081032"].Value[0]).toHaveProperty("00080100");
expect(sequenceMetadata["00081032"].Value[0]).toHaveProperty("00080102");
expect(sequenceMetadata["00081032"].Value[0]).toHaveProperty("00080104");
//
// convert to part10 and back
//
const dicomDict = new DicomDict(metadata);
dicomDict.dict = datasets[1];
const part10Buffer = dicomDict.write();
const dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer);
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);
expect(dataset.StudyInstanceUID).toEqual(secondUID);
});
it("test_multiframe_1", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/MRHead/MRHead.zip";
const unzipPath = await getZippedTestDataset(
url,
"MRHead.zip",
"test_multiframe_1"
);
const mrHeadPath = path.join(unzipPath, "MRHead");
const fileNames = await fsPromises.readdir(mrHeadPath);
const datasets = [];
fileNames.forEach(fileName => {
const arrayBuffer = fs.readFileSync(
path.join(mrHeadPath, fileName)
).buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
datasets.push(dataset);
});
const multiframe =
dcmjs.normalizers.Normalizer.normalizeToDataset(datasets);
const spacing =
multiframe.SharedFunctionalGroupsSequence.PixelMeasuresSequence
.SpacingBetweenSlices;
const roundedSpacing = Math.round(100 * spacing) / 100;
expect(multiframe.NumberOfFrames).toEqual(130);
expect(roundedSpacing).toEqual(1.3);
});
it("test_oneslice_seg", async () => {
const ctPelvisURL =
"https://github.com/dcmjs-org/data/releases/download/CTPelvis/CTPelvis.zip";
const segURL =
"https://github.com/dcmjs-org/data/releases/download/CTPelvis/Lesion1_onesliceSEG.dcm";
const unzipPath = await getZippedTestDataset(
ctPelvisURL,
"CTPelvis.zip",
"test_oneslice_seg"
);
const segFileName = "Lesion1_onesliceSEG.dcm";
const ctPelvisPath = path.join(
unzipPath,
"Series-1.2.840.113704.1.111.1916.1223562191.15"
);
const fileNames = await fsPromises.readdir(ctPelvisPath);
const datasets = [];
fileNames.forEach(fileName => {
const arrayBuffer = fs.readFileSync(
path.join(ctPelvisPath, fileName)
).buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
datasets.push(dataset);
});
let multiframe = dcmjs.normalizers.Normalizer.normalizeToDataset(datasets);
const spacing =
multiframe.SharedFunctionalGroupsSequence.PixelMeasuresSequence
.SpacingBetweenSlices;
const roundedSpacing = Math.round(100 * spacing) / 100;
expect(multiframe.NumberOfFrames).toEqual(60);
expect(roundedSpacing).toEqual(5);
var segFilePath = await getTestDataset(segURL, segFileName);
const arrayBuffer = fs.readFileSync(segFilePath).buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
multiframe = dcmjs.normalizers.Normalizer.normalizeToDataset([dataset]);
expect(dataset.NumberOfFrames).toEqual(1);
expect(multiframe.NumberOfFrames).toEqual(1);
});
it("test_normalizer_smaller", () => {
const naturalizedTags =
dcmjs.data.DicomMetaDictionary.naturalizeDataset(rawTags);
const rawTagsLen = JSON.stringify(rawTags).length;
const naturalizedTagsLen = JSON.stringify(naturalizedTags).length;
expect(naturalizedTagsLen).toBeLessThan(rawTagsLen);
});
it("test_multiframe_us", () => {
const file = fs.readFileSync("test/cine-test.dcm");
const dicomData = dcmjs.data.DicomMessage.readFile(file.buffer, {
// ignoreErrors: true,
});
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);
// eslint-disable-next-line no-underscore-dangle
dataset._meta = dcmjs.data.DicomMetaDictionary.namifyDataset(
dicomData.meta
);
expect(dataset.NumberOfFrames).toEqual(8);
});
it("test_fragment_multiframe", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/encapsulation/encapsulation-fragment-multiframe.dcm";
const dcmPath = await getTestDataset(
url,
"encapsulation-fragment-multiframe.dcm"
);
const file = fs.readFileSync(dcmPath);
const dicomData = dcmjs.data.DicomMessage.readFile(file.buffer, {
// ignoreErrors: true,
});
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);
// eslint-disable-next-line no-underscore-dangle
dataset._meta = dcmjs.data.DicomMetaDictionary.namifyDataset(
dicomData.meta
);
expect(dataset.NumberOfFrames).toEqual(2);
});
it("test_null_number_vrs", () => {
const dicomDict = new DicomDict({
TransferSynxtaxUID: "1.2.840.10008.1.2.1"
});
dicomDict.dict = DicomMetaDictionary.denaturalizeDataset(
datasetWithNullNumberVRs
);
const part10Buffer = dicomDict.write();
const dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer);
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);
expect(dataset.ImageAndFluoroscopyAreaDoseProduct).toEqual(null);
expect(dataset.InstanceNumber).toEqual(null);
});
it("test_exponential_notation", () => {
const file = fs.readFileSync("test/sample-dicom.dcm");
const data = dcmjs.data.DicomMessage.readFile(file.buffer, {
// ignoreErrors: true,
});
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(data.dict);
dataset.ImagePositionPatient[2] = 7.1945578383e-5;
const buffer = data.write();
const copy = dcmjs.data.DicomMessage.readFile(buffer);
const datasetCopy = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
copy.dict
);
expect(dataset.ImagePositionPatient).toEqual(
datasetCopy.ImagePositionPatient
);
});
it("test_output_equality", () => {
const file = fs.readFileSync("test/cine-test.dcm");
const dicomData1 = dcmjs.data.DicomMessage.readFile(file.buffer, {
// ignoreErrors: true,
});
const buffer = dicomData1.write();
const dicomData2 = dcmjs.data.DicomMessage.readFile(buffer, {
// ignoreErrors: true,
});
check_equality(dicomData1.meta, dicomData2.meta);
check_equality(dicomData1.dict, dicomData2.dict);
function check_equality(dict1, dict2) {
Object.keys(dict1).forEach(key => {
const elem1 = dict1[key];
const elem2 = dict2[key];
expect(JSON.stringify(elem1)).toEqual(JSON.stringify(elem2));
});
}
});
it("test_performance", async () => {
const file = fs.readFileSync("test/cine-test.dcm");
let buffer = file.buffer;
let json;
const start = Date.now();
for (let i = 0; i < 100; ++i) {
let old = json;
json = DicomMessage.readFile(buffer);
buffer = json.write();
if (i > 0) {
check_equality(old.meta, json.meta);
check_equality(old.dict, json.dict);
}
}
function check_equality(dict1, dict2) {
Object.keys(dict1).forEach(key => {
const elem1 = dict1[key];
const elem2 = dict2[key];
expect(JSON.stringify(elem1)).toEqual(JSON.stringify(elem2));
});
}
console.log(`Finished. Total Time elapsed: ${Date.now() - start} ms`);
});
it("test_invalid_vr_length", () => {
const file = fs.readFileSync("test/invalid-vr-length-test.dcm");
const dicomDict = dcmjs.data.DicomMessage.readFile(file.buffer);
expect(() =>
writeToBuffer(dicomDict, { allowInvalidVRLength: false })
).toThrow();
expect(() =>
writeToBuffer(dicomDict, { allowInvalidVRLength: true })
).not.toThrow();
function writeToBuffer(dicomDict, options) {
return dicomDict.write(options);
}
});
it("test_encapsulation", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/encapsulation/encapsulation.dcm";
const dcmPath = await getTestDataset(url, "encapsulation.dcm");
// given
const arrayBuffer = fs.readFileSync(dcmPath).buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
dicomDict.upsertTag("60000010", "US", 30); // Overlay Rows
dicomDict.upsertTag("60000011", "US", 30); // Overlay Columns
dicomDict.upsertTag("60000040", "CS", "G"); // Overlay Type
dicomDict.upsertTag("60000045", "LO", "AUTOMATED"); // Overlay Subtype
dicomDict.upsertTag("60000050", "SS", [1 + 50, 1 + 50]); // Overlay Origin
let overlay = dcmjs.data.BitArray.pack(
makeOverlayBitmap({ width: 30, height: 30 })
);
if (overlay.length % 2 !== 0) {
const newOverlay = new Uint8Array(overlay.length + 1);
newOverlay.set(overlay);
newOverlay.set([0], overlay.length);
overlay = newOverlay;
}
dicomDict.upsertTag("60003000", "OB", [overlay.buffer]);
// when
const lengths = [];
const stream = new ReadBufferStream(
dicomDict.write({ fragmentMultiframe: false })
),
useSyntax = EXPLICIT_LITTLE_ENDIAN;
stream.reset();
stream.increment(128);
if (stream.readAsciiString(4) !== "DICM") {
throw new Error("Invalid a dicom file");
}
const el = DicomMessage._readTag(stream, useSyntax),
metaLength = el.values[0]; //read header buffer
const metaStream = stream.more(metaLength);
const metaHeader = DicomMessage._read(metaStream, useSyntax); //get the syntax
let mainSyntax = metaHeader["00020010"].Value[0];
mainSyntax = DicomMessage._normalizeSyntax(mainSyntax);
while (!stream.end()) {
const group = new Uint16Array(stream.buffer, stream.offset, 1)[0]
.toString(16)
.padStart(4, "0");
const element = new Uint16Array(stream.buffer, stream.offset + 2, 1)[0]
.toString(16)
.padStart(4, "0");
if (group.concat(element) === "60003000") {
// Overlay Data
const length = Buffer.from(
new Uint8Array(stream.buffer, stream.offset + 8, 4)
).readUInt32LE(0);
lengths.push(length);
}
if (group.concat(element) === "7fe00010") {
// Pixel Data
const length = Buffer.from(
new Uint8Array(stream.buffer, stream.offset + 8, 4)
).readUInt32LE(0);
lengths.push(length);
}
DicomMessage._readTag(stream, mainSyntax);
}
// then
expect(lengths[0]).not.toEqual(0xffffffff);
expect(lengths[1]).toEqual(0xffffffff);
});
it("test_custom_dictionary", () => {
const customDictionary = DicomMetaDictionary.dictionary;
customDictionary["(0013,1010)"] = {
tag: "(0013,1010)",
vr: "LO",
name: "TrialName",
vm: "1",
version: "Custom"
};
const dicomMetaDictionary = new DicomMetaDictionary(customDictionary);
const dicomDict = new DicomDict(metadata);
minimalDataset["TrialName"] = "Test Trial";
dicomDict.dict = dicomMetaDictionary.denaturalizeDataset(minimalDataset);
const part10Buffer = dicomDict.write();
const dicomData = DicomMessage.readFile(part10Buffer);
const dataset = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(dataset.TrialName).toEqual("Test Trial");
//check that all other fields were preserved, 15 original + 1 for _vr and +1 for "TrialName"
expect(Object.keys(dataset).length).toEqual(17);
});
it("Reads DICOM with multiplicity", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/multiplicity/multiplicity.dcm";
const dcmPath = await getTestDataset(url, "multiplicity.dcm");
const file = await promisify(fs.readFile)(dcmPath);
const dicomDict = DicomMessage.readFile(file.buffer);
expect(dicomDict.dict["00101020"].Value).toEqual([1, 2]);
expect(dicomDict.dict["0018100B"].Value).toEqual(["1.2", "3.4"]);
});
it("Reads DICOM with PersonName multiplicity", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/multiplicity2/multiplicity.2.dcm";
const dcmPath = await getTestDataset(url, "multiplicity.2.dcm");
const file = await promisify(fs.readFile)(dcmPath);
const dicomDict = DicomMessage.readFile(file.buffer);
expect(dicomDict.dict["00081070"].Value).toEqual([
{ Alphabetic: "Doe^John" },
{ Alphabetic: "Doe^Jane" }
]);
});
it("Reads binary data into an ArrayBuffer", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/binary-tag/binary-tag.dcm";
const dcmPath = await getTestDataset(url, "binary-tag.dcm");
const file = await promisify(fs.readFile)(dcmPath);
const dicomDict = DicomMessage.readFile(file.buffer);
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomDict.dict
);
expect(dataset.PixelData).toBeInstanceOf(Array);
expect(dataset.PixelData[0]).toBeInstanceOf(ArrayBuffer);
expect([...new Uint8Array(dataset.PixelData[0])]).toEqual([2, 3, 4, 5, 6]);
});
it("Reads a multiframe DICOM which has trailing padding", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/binary-parsing-stressors/multiframe-ultrasound.dcm";
const dcmPath = await getTestDataset(url, "multiframe-ultrasound.dcm");
const dicomDict = DicomMessage.readFile(fs.readFileSync(dcmPath).buffer);
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomDict.dict
);
expect(dataset.PixelData.length).toEqual(29);
expect(dataset.PixelData[0]).toBeInstanceOf(ArrayBuffer);
expect(dataset.PixelData[0].byteLength).toEqual(104976);
expect(dataset.PixelData[1].byteLength).toEqual(104920);
expect(dataset.PixelData[27].byteLength).toEqual(103168);
expect(dataset.PixelData[28].byteLength).toEqual(103194);
});
it("Reads a multiframe DICOM with large private tags before and after the image data", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/binary-parsing-stressors/large-private-tags.dcm";
const dcmPath = await getTestDataset(url, "large-private-tags.dcm");
const dicomDict = DicomMessage.readFile(fs.readFileSync(dcmPath).buffer);
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomDict.dict
);
expect(dataset.PixelData).toBeInstanceOf(Array);
expect(dataset.PixelData.length).toEqual(130);
expect(dataset.PixelData[0]).toBeInstanceOf(ArrayBuffer);
expect(dataset.PixelData[0].byteLength).toEqual(61518);
expect(dataset.PixelData[1].byteLength).toEqual(61482);
expect(dataset.PixelData[128].byteLength).toEqual(62144);
expect(dataset.PixelData[129].byteLength).toEqual(62148);
});
it("Writes encapsulated OB data which has an odd length with a padding byte in its last fragment", async () => {
const pixelData = [1, 2, 3];
const dataset = DicomMetaDictionary.denaturalizeDataset({
PixelData: [new Uint8Array(pixelData).buffer],
_vrMap: { PixelData: "OB" }
});
const stream = new WriteBufferStream(1024);
const bytesWritten = DicomMessage.write(
dataset,
stream,
"1.2.840.10008.1.2.4.50" // JPEG baseline (an encapsulated format)
);
expect(bytesWritten).toEqual(44);
expect([...new Uint32Array(stream.view.buffer, 0, 11)]).toEqual([
0x00107fe0, // PixelData tag's group & element
0x0000424f, // VR type "OB"
0xffffffff, // Value length (0xffffffff here indicates an undefined length)
0xe000fffe, // SequenceItemTag for the BOT (basic offset table)
0x00000004, // Size in bytes of the BOT
0x00000000, // First (and only) offset in the BOT
0xe000fffe, // SequenceItemTag
0x00000004, // SequenceItemTag's length in bytes
0x00030201, // The actual data for this fragment (specified above), with padding
0xe0ddfffe, // SequenceDelimiterTag
0x00000000 // SequenceDelimiterTag value (always zero)
]);
});
it("test_deflated", async () => {
const url =
"https://github.com/dcmjs-org/data/releases/download/deflate-transfer-syntax/deflate_tests.zip";
const unzipPath = await getZippedTestDataset(
url,
"deflate_tests.zip",
"deflate_tests"
);
const deflatedPath = path.join(unzipPath, "deflate_tests");
const expected = [
{
file: "image_dfl",
tags: { Modality: "OT", Rows: 512, Columns: 512 }
},
{
file: "report_dfl",
tags: {
Modality: "SR",
VerificationFlag: "UNVERIFIED",
ContentDate: "20001110"
}
},
{
file: "wave_dfl",
tags: {
Modality: "ECG",
SynchronizationTrigger: "NO TRIGGER",
ContentDate: "19991223"
}
}
];
expected.forEach(e => {
const buffer = fs.readFileSync(path.join(deflatedPath, e.file));
const dicomDict = DicomMessage.readFile(
buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
)
);
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
Object.keys(e.tags).forEach(t => {
expect(dataset[t]).toEqual(e.tags[t]);
});
});
});
describe("With a SpecificCharacterSet tag", () => {
it("Reads a long string in the '' character set", async () => {
expect(readEncodedLongString("", [0x68, 0x69])).toEqual("hi");
});
it("Reads a long string in the ISO_IR 6 (default) character set", async () => {
expect(readEncodedLongString("ISO_IR 6", [0x68, 0x69])).toEqual("hi");
});
it("Reads a long string in the ISO_IR 13 (shift-jis) character set", async () => {
expect(readEncodedLongString("ISO_IR 13", [0x83, 0x8b])).toEqual("ル");
});
it("Reads a long string in the ISO_IR 166 (tis-620) character set", async () => {
expect(readEncodedLongString("ISO_IR 166", [0xb9, 0xf7])).toEqual("น๗");
});
it("Reads a long string in the ISO_IR 192 (utf-8) character set", async () => {
expect(readEncodedLongString("ISO_IR 192", [0xed, 0x95, 0x9c])).toEqual(
"한"
);
});
it("Throws an exception on an unsupported character set", async () => {
log.setLevel(5);
expect(() => readEncodedLongString("nope", [])).toThrow(
new Error("Unsupported character set: nope")
);
});
it("Doesn't throw an exception on an unsupported character set when ignoring errors", async () => {
log.setLevel(5);
expect(
readEncodedLongString("nope", [0x68, 0x69], { ignoreErrors: true })
).toEqual("hi");
});
it("Throws an exception on multiple character sets", async () => {
expect(() =>
readEncodedLongString("ISO_IR 13\\ISO_IR 166", [])
).toThrow(
/Using multiple character sets is not supported: ISO_IR 13,ISO_IR 166/
);
});
it("Doesn't throw an exception on multiple character sets when ignoring errors", async () => {
expect(
readEncodedLongString("ISO_IR 13\\ISO_IR 166", [0x68, 0x69], {
ignoreErrors: true
})
).toEqual("hi");
});
function readEncodedLongString(
specificCharacterSet,
encodedBytes,
readOptions = { ignoreErrors: false }
) {
// Pad to even lengths with spaces if needed
if (specificCharacterSet.length & 1) {
specificCharacterSet += " ";
}
if (encodedBytes.length & 1) {
encodedBytes.push(PADDING_SPACE);
}
// Manually construct the binary representation for the following two tags:
// - Tag #1: SpecificCharacterSet specifying the character set
// - Tag #2: InstitutionName which is a long string tag that will have its value
// set to the encoded bytes
const stream = new WriteBufferStream(
16 + specificCharacterSet.length + encodedBytes.length
);
stream.isLittleEndian = true;
// Write SpecificCharacterSet tag
stream.writeUint32(0x00050008);
stream.writeUint32(specificCharacterSet.length);
stream.writeAsciiString(specificCharacterSet);
// Write InstitutionName tag
stream.writeUint32(0x00800008);
stream.writeUint32(encodedBytes.length);
for (const encodedByte of encodedBytes) {
stream.writeUint8(encodedByte);
}
// Read the stream back to get the value of the InstitutionName tag
const readResult = DicomMessage._read(
new ReadBufferStream(stream.buffer),
IMPLICIT_LITTLE_ENDIAN,
readOptions
);
// Return the resulting UTF-8 string value for InstitutionName
return readResult["00080080"].Value[0];
}
});
it("Reads and writes numbers with NaN and Infinity values of tags with type FD (double float)", () => {
const dicomDict = new DicomDict({
TransferSynxtaxUID: EXPLICIT_LITTLE_ENDIAN
});
dicomDict.dict = DicomMetaDictionary.denaturalizeDataset({
LongitudinalTemporalOffsetFromEvent: NaN,
SequenceOfUltrasoundRegions: [{ PhysicalDeltaX: Infinity }]
});
const part10Buffer = dicomDict.write();
const dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer);
const dataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);
expect(dataset.LongitudinalTemporalOffsetFromEvent).toEqual(NaN);
expect(dataset.SequenceOfUltrasoundRegions[0].PhysicalDeltaX).toEqual(
Infinity
);
});
it("Tests that reading fails on a DICOM without a meta length tag", () => {
const rawFile = fs.readFileSync("test/no-meta-length-test.dcm");
let arrayBuffer = rawFile.buffer;
if (
rawFile.byteOffset !== 0 ||
rawFile.byteLength !== arrayBuffer.byteLength
) {
arrayBuffer = arrayBuffer.slice(
rawFile.byteOffset,
rawFile.byteOffset + rawFile.byteLength
);
}
expect(() => {
dcmjs.data.DicomMessage.readFile(arrayBuffer, {
ignoreErrors: false,
untilTag: "0020000E",
includeUntilTagValue: true
});
}).toThrow(
"Invalid DICOM file, meta length tag is malformed or not present."
);
});
describe("The same DICOM file loaded from both DCM and JSON", () => {
let dicomData;
let jsonData;
beforeEach(() => {
const file = fs.readFileSync("test/sample-sr.dcm");
dicomData = dcmjs.data.DicomMessage.readFile(file.buffer, {
// ignoreErrors: true,
});
jsonData = JSON.parse(JSON.stringify(sampleDicomSR));
});
describe("naturalized datasets", () => {
let dcmDataset;
let jsonDataset;
beforeEach(() => {
dcmDataset = dcmjs.data.DicomMetaDictionary.naturalizeDataset(
dicomData.dict
);
jsonDataset =
dcmjs.data.DicomMetaDictionary.naturalizeDataset(jsonData);
});
it("Compares denaturalized PersonName values and accessors", () => {
const jsonDenaturalized =
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(jsonDataset);
const dcmDenaturalized =
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(dcmDataset);
// These check to ensure when new denaturalized tags are created we're adding
// accessors to them, as well as the value accessors.
// This is specific to PN VRs.
expect(jsonDataset.OperatorsName.__hasValueAccessors).toBe(true);
expect(dcmDataset.OperatorsName.__hasValueAccessors).toBe(true);
expect(
jsonDenaturalized["00081070"].Value.__hasValueAccessors
).toBe(true);
expect(dcmDenaturalized["00081070"].Value.__hasValueAccessors).toBe(
true
);
expect(jsonDataset.__hasTagAccessors).toBe(true);
expect(dcmDataset.__hasTagAccessors).toBe(true);
expect(jsonDenaturalized["00081070"].__hasTagAccessors).toBe(true);
expect(dcmDenaturalized["00081070"].__hasTagAccessors).toBe(true);
expect(jsonDenaturalized["00081070"]).not.toBe(
jsonDataset.OperatorsName
);
expect(dcmDenaturalized["00081070"]).not.toBe(
dcmDataset.OperatorsName
);
});
it("Compares dcm rebuilt from json with original", () => {
const dicomDict = new dcmjs.data.DicomDict(dicomData.meta);
dicomDict.dict =
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(jsonDataset);
const buffer = dicomDict.write();
const rebuiltData = dcmjs.data.DicomMessage.readFile(buffer);
expect(JSON.stringify(rebuiltData)).toEqual(
JSON.stringify(dicomData)
);
});
it("Adds a new PN tag", () => {
jsonDataset.PerformingPhysicianName = { Alphabetic: "Doe^John" };
expect(String(jsonDataset.PerformingPhysicianName)).toEqual(
"Doe^John"
);
expect(JSON.stringify(jsonDataset.PerformingPhysicianName)).toEqual(
'[{"Alphabetic":"Doe^John"}]'
);
});
// Multiplicity
describe("multiplicity", () => {
it("Compares naturalized values", () => {
expect(JSON.stringify(jsonDataset.OtherPatientNames)).toEqual(
JSON.stringify(dcmDataset.OtherPatientNames)
);
expect(jsonDataset.OtherPatientNames.toString()).toEqual(
dcmDataset.OtherPatientNames.toString()
);
});
it("Checks dicom output string", () => {
expect(String(jsonDataset.OtherPatientNames)).toEqual(
"Doe^John=Johnny=Jonny\\Doe^Jane=Janie=Jayne"
);
expect(String(dcmDataset.OtherPatientNames)).toEqual(
"Doe^John=Johnny=Jonny\\Doe^Jane=Janie=Jayne"
);
});
it("Adds additional names", () => {
jsonDataset.OtherPatientNames.push("Test==Name");
expect(JSON.stringify(jsonDataset.OtherPatientNames)).toContain(
`,{"Alphabetic":"Test","Phonetic":"Name"}]`
);
jsonDataset.OtherPatientNames.push({ Alphabetic: "Test2" });
expect(JSON.stringify(jsonDataset.OtherPatientNames)).toContain(
`,{"Alphabetic":"Test2"}]`
);
dcmDataset.OtherPatientNames.push("Test==Name");
expect(JSON.stringify(dcmDataset.OtherPatientNames)).toContain(
`,{"Alphabetic":"Test","Phonetic":"Name"}]`
);
dcmDataset.OtherPatientNames.push({
Alphabetic: "Test2"
});
expect(JSON.stringify(dcmDataset.OtherPatientNames)).toContain(
`,{"Alphabetic":"Test2"}]`
);
});
});
// OperatorName is three-component name
describe("multiple-component name", () => {
it("Compares denaturalized values", () => {
const jsonDenaturalized =
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
jsonDataset
);
const dcmDenaturalized =
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
dcmDataset
);
expect(jsonDenaturalized["00081070"].Value).toEqual([
{
Alphabetic: "Operator^John^^Mr.^Sr.",
Ideographic: "John Operator",
Phonetic: "O-per-a-tor"
}
]);
expect(jsonDenaturalized["00081070"].Value).toEqual(
dcmDenaturalized["00081070"].Value
);
expect(jsonDenaturalized["00081070"].Value).toEqual(
jsonDataset.OperatorsName
);
expect(String(jsonDenaturalized["00081070"].Value)).toEqual(
String(jsonDataset.OperatorsName)
);
expect(
JSON.stringify(jsonDenaturalized["00081070"].Value)