@dcmjs-org/dicomdir 0.0.0-stage → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -2
- package/package.json +16 -3
- package/src/dicomdir.js +386 -0
- package/src/directoryOffsets.js +104 -0
- package/src/index.js +17 -0
- package/test/dicomdir.test.js +285 -0
- package/test/mediaRoundTrip.long.test.js +303 -0
- package/test/standalone.test.js +80 -0
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dcmjs-org/dicomdir
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
DICOMDIR (Media Storage Directory, PS3.10 / PS3.3 Annex F) building and
|
|
4
|
+
writing — the index file at the root of patient CDs, DVDs, and USB media.
|
|
5
|
+
|
|
6
|
+
Give `writeDicomDir` a flat list of file-set entries (one per referenced
|
|
7
|
+
file) and it builds the PATIENT → STUDY → SERIES → leaf directory record
|
|
8
|
+
hierarchy, computes the real byte offsets that link the records
|
|
9
|
+
(`OffsetOfTheNextDirectoryRecord`,
|
|
10
|
+
`OffsetOfReferencedLowerLevelDirectoryEntity`, and the root first/last
|
|
11
|
+
record offsets), and serializes the complete Part 10 file in a single
|
|
12
|
+
pass — no patch-and-rewrite, no buffer scanning.
|
|
13
|
+
|
|
14
|
+
Grouping is automatic and unbounded: entries are bucketed by `PatientID`,
|
|
15
|
+
then `StudyInstanceUID`, then `SeriesInstanceUID`, so any mix of patients,
|
|
16
|
+
studies, and series on one piece of media comes out as the correct tree
|
|
17
|
+
with sibling records chained at every level. The media below carries two
|
|
18
|
+
patients — one with two studies — and the records land as
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
PATIENT DOE^JANE ─ STUDY 1.2.3.4 ─ SERIES MR ─ IMAGE IM000001
|
|
22
|
+
└ STUDY 1.2.9.9 ─ SERIES CT ─ IMAGE IM000002
|
|
23
|
+
PATIENT FOX^SAM ─ STUDY 5.6.7.8 ─ SERIES US ─ IMAGE IM000003
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
import { writeDicomDir } from "@dcmjs-org/dicomdir";
|
|
28
|
+
|
|
29
|
+
const mr = {
|
|
30
|
+
referencedFileID: ["DICOM", "IM000001"],
|
|
31
|
+
sopClassUid: "1.2.840.10008.5.1.4.1.1.4",
|
|
32
|
+
sopInstanceUid: "1.2.3.4.100",
|
|
33
|
+
transferSyntaxUid: "1.2.840.10008.1.2.1",
|
|
34
|
+
patient: { PatientID: "998877", PatientName: "DOE^JANE" },
|
|
35
|
+
study: { StudyInstanceUID: "1.2.3.4" },
|
|
36
|
+
series: { SeriesInstanceUID: "1.2.3.4.5", Modality: "MR" },
|
|
37
|
+
instance: { InstanceNumber: 1 }
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Same patient, second study.
|
|
41
|
+
const ct = {
|
|
42
|
+
...mr,
|
|
43
|
+
referencedFileID: ["DICOM", "IM000002"],
|
|
44
|
+
sopClassUid: "1.2.840.10008.5.1.4.1.1.2",
|
|
45
|
+
sopInstanceUid: "1.2.9.9.100",
|
|
46
|
+
study: { StudyInstanceUID: "1.2.9.9" },
|
|
47
|
+
series: { SeriesInstanceUID: "1.2.9.9.1", Modality: "CT" }
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Different patient entirely.
|
|
51
|
+
const us = {
|
|
52
|
+
...mr,
|
|
53
|
+
referencedFileID: ["DICOM", "IM000003"],
|
|
54
|
+
sopClassUid: "1.2.840.10008.5.1.4.1.1.6.1",
|
|
55
|
+
sopInstanceUid: "5.6.7.8.100",
|
|
56
|
+
patient: { PatientID: "112233", PatientName: "FOX^SAM" },
|
|
57
|
+
study: { StudyInstanceUID: "5.6.7.8" },
|
|
58
|
+
series: { SeriesInstanceUID: "5.6.7.8.1", Modality: "US" }
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const arrayBuffer = writeDicomDir([mr, ct, us], { fileSetID: "MY_CD" });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`buildDicomDirDataset` exposes the record tree before serialization (all
|
|
65
|
+
offsets zero) for inspection or customization.
|
|
66
|
+
|
|
67
|
+
The package covers building and writing. Reading a DICOMDIR needs no
|
|
68
|
+
dedicated API — it is an ordinary Part 10 file, so either engine returns
|
|
69
|
+
the records as `DirectoryRecordSequence` — but there is no convenience
|
|
70
|
+
walker or QIDO-style query surface over those records yet; that follow-up
|
|
71
|
+
is tracked in
|
|
72
|
+
[#611](https://github.com/dcmjs-org/dcmjs/issues/611).
|
|
73
|
+
|
|
74
|
+
Depends only on `@dcmjs-org/core`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcmjs-org/dicomdir",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
|
+
"description": "DICOMDIR (Media Storage Directory, PS3.10) building and writing: turns a flat file-set description into the PATIENT/STUDY/SERIES/leaf record hierarchy with real byte offsets, serialized in one pass",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/dcmjs-org/dcmjs.git",
|
|
10
|
+
"directory": "packages/dicomdir"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@dcmjs-org/core": "1.0.0-beta.2"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@dcmjs-org/legacy": "1.0.0-beta.2",
|
|
17
|
+
"@dcmjs-org/parser": "1.0.0-beta.2"
|
|
18
|
+
}
|
|
6
19
|
}
|
package/src/dicomdir.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// packages/dicomdir/src/dicomdir.js
|
|
2
|
+
//
|
|
3
|
+
// DICOMDIR (Media Storage Directory, PS3.10 / PS3.3 F) builder. Turns a flat
|
|
4
|
+
// file-set description into the PATIENT -> STUDY -> SERIES -> leaf record
|
|
5
|
+
// hierarchy and serializes it with real byte offsets in the three offset
|
|
6
|
+
// attributes (OffsetOfTheNextDirectoryRecord, OffsetOfReferencedLowerLevel-
|
|
7
|
+
// DirectoryEntity, and the root first/last record offsets).
|
|
8
|
+
//
|
|
9
|
+
// Offsets are computed by measure-then-write (see directoryOffsets.js): every
|
|
10
|
+
// offset attribute is VR UL — fixed four bytes — so a measurement pass with
|
|
11
|
+
// placeholder zeros produces the exact final layout, and the file is written
|
|
12
|
+
// once. No patch-and-rewrite, no buffer scanning.
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
DicomMetaDictionary,
|
|
16
|
+
WriteBufferStream,
|
|
17
|
+
writeDataSet,
|
|
18
|
+
writeTagObject,
|
|
19
|
+
EXPLICIT_LITTLE_ENDIAN,
|
|
20
|
+
TagHex
|
|
21
|
+
} from "@dcmjs-org/core";
|
|
22
|
+
import { computeDirectoryOffsets } from "./directoryOffsets.js";
|
|
23
|
+
|
|
24
|
+
const MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID = "1.2.840.10008.1.3.10";
|
|
25
|
+
|
|
26
|
+
// PS3.10 8.5: each File ID component is 1-8 characters from the ISO 9660
|
|
27
|
+
// level 1 repertoire (A-Z, 0-9, underscore), at most 8 components deep.
|
|
28
|
+
const FILE_ID_COMPONENT = /^[A-Z0-9_]{1,8}$/;
|
|
29
|
+
|
|
30
|
+
function assertEntry(entry, index) {
|
|
31
|
+
const where = `buildDicomDirDataset: entries[${index}]`;
|
|
32
|
+
if (!entry || typeof entry !== "object") {
|
|
33
|
+
throw new Error(`${where} is not an object`);
|
|
34
|
+
}
|
|
35
|
+
if (
|
|
36
|
+
!Array.isArray(entry.referencedFileID) ||
|
|
37
|
+
!entry.referencedFileID.length
|
|
38
|
+
) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`${where}.referencedFileID must be a non-empty array of path components`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
for (const required of [
|
|
44
|
+
"sopClassUid",
|
|
45
|
+
"sopInstanceUid",
|
|
46
|
+
"transferSyntaxUid"
|
|
47
|
+
]) {
|
|
48
|
+
if (!entry[required]) {
|
|
49
|
+
throw new Error(`${where}.${required} is required`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!entry.patient?.PatientID) {
|
|
53
|
+
throw new Error(`${where}.patient.PatientID is required`);
|
|
54
|
+
}
|
|
55
|
+
if (!entry.study?.StudyInstanceUID) {
|
|
56
|
+
throw new Error(`${where}.study.StudyInstanceUID is required`);
|
|
57
|
+
}
|
|
58
|
+
if (!entry.series?.SeriesInstanceUID) {
|
|
59
|
+
throw new Error(`${where}.series.SeriesInstanceUID is required`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function assertFileID(components, index, allowNonConforming) {
|
|
64
|
+
if (components.length > 8) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`buildDicomDirDataset: entries[${index}].referencedFileID has ` +
|
|
67
|
+
`${components.length} components (max 8)`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (allowNonConforming) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
for (const component of components) {
|
|
74
|
+
if (!FILE_ID_COMPONENT.test(component)) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`buildDicomDirDataset: entries[${index}].referencedFileID ` +
|
|
77
|
+
`component "${component}" is not ISO 9660 level 1 ` +
|
|
78
|
+
`conformant (A-Z 0-9 _, 1-8 chars) — rename the file or ` +
|
|
79
|
+
`pass allowNonConformingFileIDs: true`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The four keys every directory record starts with (PS3.3 F.3). */
|
|
86
|
+
function recordCommon(recordType) {
|
|
87
|
+
return {
|
|
88
|
+
OffsetOfTheNextDirectoryRecord: 0,
|
|
89
|
+
RecordInUseFlag: 0xffff,
|
|
90
|
+
OffsetOfReferencedLowerLevelDirectoryEntity: 0,
|
|
91
|
+
DirectoryRecordType: recordType
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Group the flat entries into the PATIENT -> STUDY -> SERIES -> leaf tree,
|
|
97
|
+
* preserving first-seen order at every level.
|
|
98
|
+
*/
|
|
99
|
+
function groupEntries(entries, options) {
|
|
100
|
+
const patients = new Map();
|
|
101
|
+
entries.forEach((entry, index) => {
|
|
102
|
+
assertEntry(entry, index);
|
|
103
|
+
assertFileID(
|
|
104
|
+
entry.referencedFileID,
|
|
105
|
+
index,
|
|
106
|
+
options.allowNonConformingFileIDs
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const patientKey = entry.patient.PatientID;
|
|
110
|
+
if (!patients.has(patientKey)) {
|
|
111
|
+
patients.set(patientKey, {
|
|
112
|
+
attributes: entry.patient,
|
|
113
|
+
studies: new Map()
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const patient = patients.get(patientKey);
|
|
117
|
+
|
|
118
|
+
const studyKey = entry.study.StudyInstanceUID;
|
|
119
|
+
if (!patient.studies.has(studyKey)) {
|
|
120
|
+
patient.studies.set(studyKey, {
|
|
121
|
+
attributes: entry.study,
|
|
122
|
+
series: new Map()
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const study = patient.studies.get(studyKey);
|
|
126
|
+
|
|
127
|
+
const seriesKey = entry.series.SeriesInstanceUID;
|
|
128
|
+
if (!study.series.has(seriesKey)) {
|
|
129
|
+
study.series.set(seriesKey, {
|
|
130
|
+
attributes: entry.series,
|
|
131
|
+
leaves: []
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
study.series.get(seriesKey).leaves.push(entry);
|
|
135
|
+
});
|
|
136
|
+
return patients;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Flatten the tree depth-first into naturalized record objects, tracking the
|
|
141
|
+
* flat index of each record's next sibling and first child so the offset
|
|
142
|
+
* pass can wire the chains. Depth-first order means every link points
|
|
143
|
+
* strictly forward in the file.
|
|
144
|
+
*
|
|
145
|
+
* @returns {{ records: Object[], links: Array<{next: number|null, child: number|null}> }}
|
|
146
|
+
*/
|
|
147
|
+
function flattenRecords(patients) {
|
|
148
|
+
const records = [];
|
|
149
|
+
const links = [];
|
|
150
|
+
|
|
151
|
+
const push = record => {
|
|
152
|
+
records.push(record);
|
|
153
|
+
links.push({ next: null, child: null });
|
|
154
|
+
return records.length - 1;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const patientList = [...patients.values()];
|
|
158
|
+
let previousPatientIndex = null;
|
|
159
|
+
|
|
160
|
+
for (const patient of patientList) {
|
|
161
|
+
const patientIndex = push({
|
|
162
|
+
...recordCommon("PATIENT"),
|
|
163
|
+
PatientName: patient.attributes.PatientName ?? "",
|
|
164
|
+
PatientID: patient.attributes.PatientID
|
|
165
|
+
});
|
|
166
|
+
if (previousPatientIndex !== null) {
|
|
167
|
+
links[previousPatientIndex].next = patientIndex;
|
|
168
|
+
}
|
|
169
|
+
previousPatientIndex = patientIndex;
|
|
170
|
+
|
|
171
|
+
const studyList = [...patient.studies.values()];
|
|
172
|
+
let previousStudyIndex = null;
|
|
173
|
+
for (const study of studyList) {
|
|
174
|
+
const studyIndex = push({
|
|
175
|
+
...recordCommon("STUDY"),
|
|
176
|
+
StudyDate: study.attributes.StudyDate ?? "",
|
|
177
|
+
StudyTime: study.attributes.StudyTime ?? "",
|
|
178
|
+
StudyDescription: study.attributes.StudyDescription ?? "",
|
|
179
|
+
AccessionNumber: study.attributes.AccessionNumber ?? "",
|
|
180
|
+
StudyInstanceUID: study.attributes.StudyInstanceUID,
|
|
181
|
+
StudyID: study.attributes.StudyID ?? ""
|
|
182
|
+
});
|
|
183
|
+
if (previousStudyIndex === null) {
|
|
184
|
+
links[patientIndex].child = studyIndex;
|
|
185
|
+
} else {
|
|
186
|
+
links[previousStudyIndex].next = studyIndex;
|
|
187
|
+
}
|
|
188
|
+
previousStudyIndex = studyIndex;
|
|
189
|
+
|
|
190
|
+
const seriesList = [...study.series.values()];
|
|
191
|
+
let previousSeriesIndex = null;
|
|
192
|
+
for (const series of seriesList) {
|
|
193
|
+
const seriesIndex = push({
|
|
194
|
+
...recordCommon("SERIES"),
|
|
195
|
+
Modality: series.attributes.Modality ?? "OT",
|
|
196
|
+
SeriesInstanceUID: series.attributes.SeriesInstanceUID,
|
|
197
|
+
SeriesNumber: series.attributes.SeriesNumber ?? 1
|
|
198
|
+
});
|
|
199
|
+
if (previousSeriesIndex === null) {
|
|
200
|
+
links[studyIndex].child = seriesIndex;
|
|
201
|
+
} else {
|
|
202
|
+
links[previousSeriesIndex].next = seriesIndex;
|
|
203
|
+
}
|
|
204
|
+
previousSeriesIndex = seriesIndex;
|
|
205
|
+
|
|
206
|
+
let previousLeafIndex = null;
|
|
207
|
+
for (const entry of series.leaves) {
|
|
208
|
+
const leafIndex = push({
|
|
209
|
+
...recordCommon(entry.recordType || "IMAGE"),
|
|
210
|
+
ReferencedFileID: [...entry.referencedFileID],
|
|
211
|
+
ReferencedSOPClassUIDInFile: entry.sopClassUid,
|
|
212
|
+
ReferencedSOPInstanceUIDInFile: entry.sopInstanceUid,
|
|
213
|
+
ReferencedTransferSyntaxUIDInFile:
|
|
214
|
+
entry.transferSyntaxUid,
|
|
215
|
+
InstanceNumber: entry.instance?.InstanceNumber ?? 1,
|
|
216
|
+
...entry.instance
|
|
217
|
+
});
|
|
218
|
+
if (previousLeafIndex === null) {
|
|
219
|
+
links[seriesIndex].child = leafIndex;
|
|
220
|
+
} else {
|
|
221
|
+
links[previousLeafIndex].next = leafIndex;
|
|
222
|
+
}
|
|
223
|
+
previousLeafIndex = leafIndex;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return { records, links };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Build the naturalized DICOMDIR dataset (all offsets zero) plus the link
|
|
234
|
+
* table the offset pass needs. Pure and inspectable — no serialization.
|
|
235
|
+
*
|
|
236
|
+
* @param {Array} entries - flat file-set entries; see module JSDoc/typedef
|
|
237
|
+
* @param {Object} [options]
|
|
238
|
+
* @param {string} [options.fileSetID=""]
|
|
239
|
+
* @param {boolean} [options.allowNonConformingFileIDs=false]
|
|
240
|
+
* @returns {{ dataset: Object, links: Array, rootIndices: number[] }}
|
|
241
|
+
*/
|
|
242
|
+
function buildDicomDirDataset(entries = [], options = {}) {
|
|
243
|
+
const patients = groupEntries(entries, options);
|
|
244
|
+
const { records, links } = flattenRecords(patients);
|
|
245
|
+
|
|
246
|
+
const rootIndices = [];
|
|
247
|
+
records.forEach((record, index) => {
|
|
248
|
+
if (record.DirectoryRecordType === "PATIENT") {
|
|
249
|
+
rootIndices.push(index);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const dataset = {
|
|
254
|
+
FileSetID: options.fileSetID ?? "",
|
|
255
|
+
OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity: 0,
|
|
256
|
+
OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity: 0,
|
|
257
|
+
FileSetConsistencyFlag: 0,
|
|
258
|
+
DirectoryRecordSequence: records
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
return { dataset, links, rootIndices };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Mint the DICOMDIR file meta group (SOP identity lives here, not the body). */
|
|
265
|
+
function buildDicomDirMeta(options = {}) {
|
|
266
|
+
const fileMetaInformationVersion = new Uint8Array(2);
|
|
267
|
+
fileMetaInformationVersion[1] = 1;
|
|
268
|
+
return DicomMetaDictionary.denaturalizeDataset({
|
|
269
|
+
FileMetaInformationVersion: fileMetaInformationVersion.buffer,
|
|
270
|
+
MediaStorageSOPClassUID: MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID,
|
|
271
|
+
MediaStorageSOPInstanceUID:
|
|
272
|
+
options.fileSetUID || DicomMetaDictionary.uid(),
|
|
273
|
+
TransferSyntaxUID: EXPLICIT_LITTLE_ENDIAN,
|
|
274
|
+
ImplementationClassUID:
|
|
275
|
+
"2.25.80302813137786398554742050926734630921603366648225212145404",
|
|
276
|
+
ImplementationVersionName: "dcmjs-0.0"
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Build and serialize a complete DICOMDIR: build the record tree,
|
|
282
|
+
* denaturalize it, measure the layout, wire the offset chains, write once.
|
|
283
|
+
*
|
|
284
|
+
* @param {Array} entries - flat file-set entries
|
|
285
|
+
* @param {Object} [options] - buildDicomDirDataset options plus
|
|
286
|
+
* {string} [options.fileSetUID] fixed MediaStorageSOPInstanceUID
|
|
287
|
+
* @returns {ArrayBuffer} the DICOMDIR file bytes
|
|
288
|
+
*/
|
|
289
|
+
/**
|
|
290
|
+
* The DICOM data dictionary gives the directory offset attributes the
|
|
291
|
+
* special VR "up" (byte offset); the dcmjs writer has no "up" representation
|
|
292
|
+
* and would fall back to UN. They are four-byte unsigned offsets — pin them
|
|
293
|
+
* to UL so they serialize (and measure) as the fixed-width values the
|
|
294
|
+
* offset algorithm depends on.
|
|
295
|
+
*/
|
|
296
|
+
function pinOffsetVRs(dict) {
|
|
297
|
+
for (const tag of ["00041200", "00041202"]) {
|
|
298
|
+
if (dict[tag]) {
|
|
299
|
+
dict[tag].vr = "UL";
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const items = dict["00041220"]?.Value || [];
|
|
303
|
+
for (const item of items) {
|
|
304
|
+
for (const tag of ["00041400", "00041420"]) {
|
|
305
|
+
if (item[tag]) {
|
|
306
|
+
item[tag].vr = "UL";
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Serialize { meta, dict } as a Part 10 file. This is the body of
|
|
314
|
+
* DicomDict.write (packages/legacy) minus the deflate branch — a DICOMDIR
|
|
315
|
+
* is always Explicit VR Little Endian, never deflated — rebuilt on core's
|
|
316
|
+
* writeDataSet/writeTagObject so this package does not depend on
|
|
317
|
+
* @dcmjs-org/legacy. DicomMessage.write and DicomMessage.writeTagObject
|
|
318
|
+
* are one-line delegates to those same core functions, so the bytes this
|
|
319
|
+
* produces are identical to DicomDict.write's, which is what keeps the
|
|
320
|
+
* measured directory offsets valid.
|
|
321
|
+
*/
|
|
322
|
+
function writePart10(meta, dict, writeOptions) {
|
|
323
|
+
const metaSyntax = EXPLICIT_LITTLE_ENDIAN;
|
|
324
|
+
const fileStream = new WriteBufferStream(4096, true);
|
|
325
|
+
fileStream.writeUint8Repeat(0, 128);
|
|
326
|
+
fileStream.writeAsciiString("DICM");
|
|
327
|
+
|
|
328
|
+
const metaStream = new WriteBufferStream(1024);
|
|
329
|
+
if (!meta[TagHex.TransferSyntaxUID]) {
|
|
330
|
+
meta[TagHex.TransferSyntaxUID] = {
|
|
331
|
+
vr: "UI",
|
|
332
|
+
Value: [EXPLICIT_LITTLE_ENDIAN]
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
writeDataSet(meta, metaStream, metaSyntax, writeOptions);
|
|
336
|
+
writeTagObject(
|
|
337
|
+
fileStream,
|
|
338
|
+
TagHex.FileMetaInformationGroupLength,
|
|
339
|
+
"UL",
|
|
340
|
+
metaStream.size,
|
|
341
|
+
metaSyntax,
|
|
342
|
+
writeOptions
|
|
343
|
+
);
|
|
344
|
+
fileStream.concat(metaStream);
|
|
345
|
+
|
|
346
|
+
const useSyntax = meta[TagHex.TransferSyntaxUID].Value[0];
|
|
347
|
+
writeDataSet(dict, fileStream, useSyntax, writeOptions);
|
|
348
|
+
return fileStream.getBuffer();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function writeDicomDir(entries = [], options = {}) {
|
|
352
|
+
const { dataset, links, rootIndices } = buildDicomDirDataset(
|
|
353
|
+
entries,
|
|
354
|
+
options
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
const meta = buildDicomDirMeta(options);
|
|
358
|
+
const dict = DicomMetaDictionary.denaturalizeDataset(dataset);
|
|
359
|
+
pinOffsetVRs(dict);
|
|
360
|
+
|
|
361
|
+
const writeOptions = { allowInvalidVRLength: false };
|
|
362
|
+
const recordOffsets = computeDirectoryOffsets({ meta, dict }, writeOptions);
|
|
363
|
+
|
|
364
|
+
const sequence = dict["00041220"];
|
|
365
|
+
const items = (sequence && sequence.Value) || [];
|
|
366
|
+
items.forEach((item, index) => {
|
|
367
|
+
const { next, child } = links[index];
|
|
368
|
+
item["00041400"].Value = [next === null ? 0 : recordOffsets[next]];
|
|
369
|
+
item["00041420"].Value = [child === null ? 0 : recordOffsets[child]];
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
const first = rootIndices.length ? recordOffsets[rootIndices[0]] : 0;
|
|
373
|
+
const last = rootIndices.length
|
|
374
|
+
? recordOffsets[rootIndices[rootIndices.length - 1]]
|
|
375
|
+
: 0;
|
|
376
|
+
dict["00041200"].Value = [first];
|
|
377
|
+
dict["00041202"].Value = [last];
|
|
378
|
+
|
|
379
|
+
return writePart10(meta, dict, writeOptions);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export {
|
|
383
|
+
buildDicomDirDataset,
|
|
384
|
+
writeDicomDir,
|
|
385
|
+
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
386
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// packages/dicomdir/src/directoryOffsets.js
|
|
2
|
+
//
|
|
3
|
+
// Byte-offset computation for DICOMDIR directory records.
|
|
4
|
+
//
|
|
5
|
+
// The load-bearing fact: every DICOMDIR offset attribute (0004,1200/1202/
|
|
6
|
+
// 1400/1420) is VR UL — a fixed four-byte value — so the values NEVER affect
|
|
7
|
+
// the byte layout. One measurement pass over the exact structures the final
|
|
8
|
+
// write will serialize (same writer, same options, placeholder zeros)
|
|
9
|
+
// therefore yields the exact offsets of the final file, and the file is
|
|
10
|
+
// written once with the real values filled in.
|
|
11
|
+
//
|
|
12
|
+
// Layout being measured (mirrors the Part 10 envelope in dicomdir.js):
|
|
13
|
+
// 128-byte preamble + "DICM"
|
|
14
|
+
// FileMetaInformationGroupLength element
|
|
15
|
+
// meta group (Explicit VR Little Endian)
|
|
16
|
+
// body tags below (0004,1220), sorted ascending
|
|
17
|
+
// (0004,1220) DirectoryRecordSequence: 12-byte SQ header (undefined
|
|
18
|
+
// length), then per item: 8-byte item header (FFFE,E000, undefined
|
|
19
|
+
// length) + item body + 8-byte item delimiter (FFFE,E00D) — see
|
|
20
|
+
// SequenceOfItems.writeBytes — and a trailing 8-byte sequence delimiter.
|
|
21
|
+
//
|
|
22
|
+
// A directory-record offset points at the FFFE,E000 item tag of that record,
|
|
23
|
+
// counted from byte 0 of the file (PS3.10 8.6).
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
WriteBufferStream,
|
|
27
|
+
writeDataSet,
|
|
28
|
+
writeTagObject,
|
|
29
|
+
EXPLICIT_LITTLE_ENDIAN,
|
|
30
|
+
TagHex
|
|
31
|
+
} from "@dcmjs-org/core";
|
|
32
|
+
|
|
33
|
+
const DIRECTORY_RECORD_SEQUENCE = "00041220";
|
|
34
|
+
|
|
35
|
+
const ITEM_HEADER_BYTES = 8; // FFFE,E000 + 4-byte undefined length
|
|
36
|
+
const ITEM_DELIMITER_BYTES = 8; // FFFE,E00D + 4-byte zero length
|
|
37
|
+
|
|
38
|
+
/** Bytes writeDataSet produces for one tag-keyed object. */
|
|
39
|
+
function measure(jsonObject, writeOptions) {
|
|
40
|
+
const stream = new WriteBufferStream(1024);
|
|
41
|
+
writeDataSet(jsonObject, stream, EXPLICIT_LITTLE_ENDIAN, writeOptions);
|
|
42
|
+
return stream.size;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Compute the absolute byte offset of every directory record item in the
|
|
47
|
+
* file the Part 10 envelope write will produce. Call with the offsets
|
|
48
|
+
* still zero; patch the returned values in, then write — the layout is
|
|
49
|
+
* guaranteed identical because offsets are fixed-width.
|
|
50
|
+
*
|
|
51
|
+
* @param {{ meta: Object, dict: Object }} dicomDict - denaturalized
|
|
52
|
+
* DICOMDIR ({ meta, dict }) whose dict contains (0004,1220)
|
|
53
|
+
* @param {Object} writeOptions - the SAME object later passed to write()
|
|
54
|
+
* @returns {number[]} byte offset of each record's item tag, in item order
|
|
55
|
+
*/
|
|
56
|
+
export function computeDirectoryOffsets(dicomDict, writeOptions) {
|
|
57
|
+
// Preamble + magic.
|
|
58
|
+
let cursor = 128 + 4;
|
|
59
|
+
|
|
60
|
+
// FileMetaInformationGroupLength + meta group. The group length element
|
|
61
|
+
// is measured with the real meta size as its value — UL again, so the
|
|
62
|
+
// element size is value-independent, but measuring keeps zero assumptions.
|
|
63
|
+
const metaSize = measure(dicomDict.meta, writeOptions);
|
|
64
|
+
const groupLengthStream = new WriteBufferStream(64);
|
|
65
|
+
writeTagObject(
|
|
66
|
+
groupLengthStream,
|
|
67
|
+
TagHex.FileMetaInformationGroupLength,
|
|
68
|
+
"UL",
|
|
69
|
+
metaSize,
|
|
70
|
+
EXPLICIT_LITTLE_ENDIAN,
|
|
71
|
+
writeOptions
|
|
72
|
+
);
|
|
73
|
+
cursor += groupLengthStream.size + metaSize;
|
|
74
|
+
|
|
75
|
+
// Body tags before the record sequence, in the writer's sorted order.
|
|
76
|
+
const bodyTags = Object.keys(dicomDict.dict).sort();
|
|
77
|
+
for (const tag of bodyTags) {
|
|
78
|
+
if (tag >= DIRECTORY_RECORD_SEQUENCE) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
cursor += measure({ [tag]: dicomDict.dict[tag] }, writeOptions);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// SQ element header: measure an empty sequence (header + 8-byte
|
|
85
|
+
// sequence delimiter) rather than assuming the 12-byte constant.
|
|
86
|
+
const sequence = dicomDict.dict[DIRECTORY_RECORD_SEQUENCE];
|
|
87
|
+
const emptySequenceSize = measure(
|
|
88
|
+
{ [DIRECTORY_RECORD_SEQUENCE]: { vr: "SQ", Value: [] } },
|
|
89
|
+
writeOptions
|
|
90
|
+
);
|
|
91
|
+
cursor += emptySequenceSize - ITEM_DELIMITER_BYTES;
|
|
92
|
+
|
|
93
|
+
const offsets = [];
|
|
94
|
+
const items = (sequence && sequence.Value) || [];
|
|
95
|
+
for (const item of items) {
|
|
96
|
+
offsets.push(cursor);
|
|
97
|
+
cursor +=
|
|
98
|
+
ITEM_HEADER_BYTES +
|
|
99
|
+
measure(item, writeOptions) +
|
|
100
|
+
ITEM_DELIMITER_BYTES;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return offsets;
|
|
104
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildDicomDirDataset,
|
|
3
|
+
writeDicomDir,
|
|
4
|
+
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
5
|
+
} from "./dicomdir.js";
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
buildDicomDirDataset,
|
|
9
|
+
writeDicomDir,
|
|
10
|
+
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
buildDicomDirDataset,
|
|
15
|
+
writeDicomDir,
|
|
16
|
+
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
17
|
+
};
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// packages/dicomdir/test/dicomdir.test.js
|
|
2
|
+
//
|
|
3
|
+
// DICOMDIR builder: record hierarchy, and — the critical property — real
|
|
4
|
+
// byte offsets. Ground truth is the written buffer itself: every nonzero
|
|
5
|
+
// offset value must land exactly on an FFFE,E000 item tag, and walking the
|
|
6
|
+
// next/lower chains must visit every record once in hierarchy order.
|
|
7
|
+
|
|
8
|
+
import { DicomMetaDictionary, validationLog } from "@dcmjs-org/core";
|
|
9
|
+
// The legacy eager reader is the readback oracle only — a devDependency of
|
|
10
|
+
// this package's tests, never a runtime import of its src/.
|
|
11
|
+
import { DicomMessage } from "@dcmjs-org/legacy";
|
|
12
|
+
import {
|
|
13
|
+
writeDicomDir,
|
|
14
|
+
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
15
|
+
} from "../src/index.js";
|
|
16
|
+
|
|
17
|
+
validationLog.setLevel(5);
|
|
18
|
+
|
|
19
|
+
const MR_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.4";
|
|
20
|
+
const ELE = "1.2.840.10008.1.2.1";
|
|
21
|
+
|
|
22
|
+
function entry(overrides = {}) {
|
|
23
|
+
return {
|
|
24
|
+
referencedFileID: ["DICOM", "IM000001"],
|
|
25
|
+
sopClassUid: MR_SOP_CLASS,
|
|
26
|
+
sopInstanceUid: "1.2.3.4.100",
|
|
27
|
+
transferSyntaxUid: ELE,
|
|
28
|
+
patient: { PatientID: "998877", PatientName: "DOE^JANE" },
|
|
29
|
+
study: { StudyInstanceUID: "1.2.3.4", StudyDescription: "HEAD^BRAIN" },
|
|
30
|
+
series: { SeriesInstanceUID: "1.2.3.4.5", Modality: "MR" },
|
|
31
|
+
instance: { InstanceNumber: 1 },
|
|
32
|
+
...overrides
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function readDicomDir(arrayBuffer) {
|
|
37
|
+
const dicomDict = DicomMessage.readFile(arrayBuffer);
|
|
38
|
+
return {
|
|
39
|
+
meta: DicomMetaDictionary.naturalizeDataset(dicomDict.meta),
|
|
40
|
+
dataset: DicomMetaDictionary.naturalizeDataset(dicomDict.dict)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function recordsOf(dataset) {
|
|
45
|
+
const sequence = dataset.DirectoryRecordSequence;
|
|
46
|
+
return Array.isArray(sequence) ? sequence : [sequence];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Assert the four bytes at `offset` are the FFFE,E000 item tag (LE). */
|
|
50
|
+
function expectItemTagAt(bytes, offset) {
|
|
51
|
+
expect(bytes[offset]).toBe(0xfe);
|
|
52
|
+
expect(bytes[offset + 1]).toBe(0xff);
|
|
53
|
+
expect(bytes[offset + 2]).toBe(0x00);
|
|
54
|
+
expect(bytes[offset + 3]).toBe(0xe0);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
describe("record hierarchy", () => {
|
|
58
|
+
test("one entry yields PATIENT/STUDY/SERIES/IMAGE", () => {
|
|
59
|
+
const { dataset } = readDicomDir(writeDicomDir([entry()]));
|
|
60
|
+
expect(recordsOf(dataset).map(r => r.DirectoryRecordType)).toEqual([
|
|
61
|
+
"PATIENT",
|
|
62
|
+
"STUDY",
|
|
63
|
+
"SERIES",
|
|
64
|
+
"IMAGE"
|
|
65
|
+
]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("two entries in one series share the branch", () => {
|
|
69
|
+
const { dataset } = readDicomDir(
|
|
70
|
+
writeDicomDir([
|
|
71
|
+
entry(),
|
|
72
|
+
entry({
|
|
73
|
+
referencedFileID: ["DICOM", "IM000002"],
|
|
74
|
+
sopInstanceUid: "1.2.3.4.101",
|
|
75
|
+
instance: { InstanceNumber: 2 }
|
|
76
|
+
})
|
|
77
|
+
])
|
|
78
|
+
);
|
|
79
|
+
expect(recordsOf(dataset).map(r => r.DirectoryRecordType)).toEqual([
|
|
80
|
+
"PATIENT",
|
|
81
|
+
"STUDY",
|
|
82
|
+
"SERIES",
|
|
83
|
+
"IMAGE",
|
|
84
|
+
"IMAGE"
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("two patients chain as siblings", () => {
|
|
89
|
+
const { dataset } = readDicomDir(
|
|
90
|
+
writeDicomDir([
|
|
91
|
+
entry(),
|
|
92
|
+
entry({
|
|
93
|
+
patient: { PatientID: "22446688", PatientName: "FOX^JANE" },
|
|
94
|
+
study: { StudyInstanceUID: "9.8.7" },
|
|
95
|
+
series: { SeriesInstanceUID: "9.8.7.6", Modality: "MR" },
|
|
96
|
+
sopInstanceUid: "9.8.7.6.5",
|
|
97
|
+
referencedFileID: ["DICOM", "IM000002"]
|
|
98
|
+
})
|
|
99
|
+
])
|
|
100
|
+
);
|
|
101
|
+
const types = recordsOf(dataset).map(r => r.DirectoryRecordType);
|
|
102
|
+
expect(types.filter(t => t === "PATIENT")).toHaveLength(2);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("leaf carries the file reference", () => {
|
|
106
|
+
const { dataset } = readDicomDir(writeDicomDir([entry()]));
|
|
107
|
+
const image = recordsOf(dataset).find(
|
|
108
|
+
r => r.DirectoryRecordType === "IMAGE"
|
|
109
|
+
);
|
|
110
|
+
expect(image.ReferencedFileID).toEqual(["DICOM", "IM000001"]);
|
|
111
|
+
expect(image.ReferencedSOPClassUIDInFile).toBe(MR_SOP_CLASS);
|
|
112
|
+
expect(image.ReferencedSOPInstanceUIDInFile).toBe("1.2.3.4.100");
|
|
113
|
+
expect(image.ReferencedTransferSyntaxUIDInFile).toBe(ELE);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("offsets are real byte positions", () => {
|
|
118
|
+
const entries = [
|
|
119
|
+
entry(),
|
|
120
|
+
entry({
|
|
121
|
+
referencedFileID: ["DICOM", "IM000002"],
|
|
122
|
+
sopInstanceUid: "1.2.3.4.101",
|
|
123
|
+
instance: { InstanceNumber: 2 }
|
|
124
|
+
}),
|
|
125
|
+
entry({
|
|
126
|
+
patient: { PatientID: "22446688", PatientName: "FOX^JANE" },
|
|
127
|
+
study: { StudyInstanceUID: "9.8.7" },
|
|
128
|
+
series: { SeriesInstanceUID: "9.8.7.6", Modality: "MR" },
|
|
129
|
+
sopInstanceUid: "9.8.7.6.5",
|
|
130
|
+
referencedFileID: ["DICOM", "IM000003"]
|
|
131
|
+
})
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const buffer = writeDicomDir(entries, { fileSetUID: "2.25.42" });
|
|
135
|
+
const bytes = new Uint8Array(buffer);
|
|
136
|
+
const { dataset } = readDicomDir(buffer);
|
|
137
|
+
const records = recordsOf(dataset);
|
|
138
|
+
|
|
139
|
+
test("every nonzero offset lands on an FFFE,E000 item tag", () => {
|
|
140
|
+
const offsets = [
|
|
141
|
+
dataset.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity,
|
|
142
|
+
dataset.OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity,
|
|
143
|
+
...records.flatMap(r => [
|
|
144
|
+
r.OffsetOfTheNextDirectoryRecord,
|
|
145
|
+
r.OffsetOfReferencedLowerLevelDirectoryEntity
|
|
146
|
+
])
|
|
147
|
+
].filter(offset => offset !== 0);
|
|
148
|
+
|
|
149
|
+
expect(offsets.length).toBeGreaterThan(0);
|
|
150
|
+
for (const offset of offsets) {
|
|
151
|
+
expectItemTagAt(bytes, offset);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("chain links + first root offset address every record exactly once", () => {
|
|
156
|
+
// Every record is referenced exactly once: the first root record by
|
|
157
|
+
// the root offset, every other record by exactly one next/lower
|
|
158
|
+
// link. The union must therefore have exactly one offset per record,
|
|
159
|
+
// each landing on an item tag. Records were flattened depth-first,
|
|
160
|
+
// so sorted offsets correspond to sequence order.
|
|
161
|
+
const referenced = records
|
|
162
|
+
.flatMap(r => [
|
|
163
|
+
r.OffsetOfTheNextDirectoryRecord,
|
|
164
|
+
r.OffsetOfReferencedLowerLevelDirectoryEntity
|
|
165
|
+
])
|
|
166
|
+
.filter(offset => offset !== 0);
|
|
167
|
+
const all = [
|
|
168
|
+
dataset.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity,
|
|
169
|
+
...referenced
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
expect(all).toHaveLength(records.length); // no double references
|
|
173
|
+
const unique = [...new Set(all)].sort((a, b) => a - b);
|
|
174
|
+
expect(unique).toHaveLength(records.length); // no shared targets
|
|
175
|
+
for (const offset of unique) {
|
|
176
|
+
expectItemTagAt(bytes, offset);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("offsets are strictly increasing in sequence order", () => {
|
|
181
|
+
const referenced = records
|
|
182
|
+
.flatMap(r => [
|
|
183
|
+
r.OffsetOfTheNextDirectoryRecord,
|
|
184
|
+
r.OffsetOfReferencedLowerLevelDirectoryEntity
|
|
185
|
+
])
|
|
186
|
+
.filter(o => o !== 0);
|
|
187
|
+
const all = [
|
|
188
|
+
dataset.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity,
|
|
189
|
+
...referenced
|
|
190
|
+
];
|
|
191
|
+
const sorted = [...new Set(all)].sort((a, b) => a - b);
|
|
192
|
+
expect(sorted[0]).toBe(
|
|
193
|
+
dataset.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("root first/last point at the two PATIENT records", () => {
|
|
198
|
+
const first =
|
|
199
|
+
dataset.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity;
|
|
200
|
+
const last =
|
|
201
|
+
dataset.OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity;
|
|
202
|
+
expect(first).not.toBe(0);
|
|
203
|
+
expect(last).not.toBe(0);
|
|
204
|
+
expect(last).toBeGreaterThan(first);
|
|
205
|
+
// First PATIENT's next-sibling link is the last PATIENT.
|
|
206
|
+
expect(records[0].OffsetOfTheNextDirectoryRecord).toBe(last);
|
|
207
|
+
// Last sibling terminates its chain.
|
|
208
|
+
const lastPatient = records.find(
|
|
209
|
+
(r, i) => r.DirectoryRecordType === "PATIENT" && i > 0
|
|
210
|
+
);
|
|
211
|
+
expect(lastPatient.OffsetOfTheNextDirectoryRecord).toBe(0);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("deterministic: same entries + fileSetUID -> identical bytes", () => {
|
|
215
|
+
const again = writeDicomDir(entries, { fileSetUID: "2.25.42" });
|
|
216
|
+
expect(new Uint8Array(again)).toEqual(bytes);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
describe("file meta and root attributes", () => {
|
|
221
|
+
const buffer = writeDicomDir([entry()], {
|
|
222
|
+
fileSetID: "FOX_CD",
|
|
223
|
+
fileSetUID: "2.25.7"
|
|
224
|
+
});
|
|
225
|
+
const { meta, dataset } = readDicomDir(buffer);
|
|
226
|
+
|
|
227
|
+
test("meta group carries the Media Storage Directory SOP class", () => {
|
|
228
|
+
expect(meta.MediaStorageSOPClassUID).toBe(
|
|
229
|
+
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
230
|
+
);
|
|
231
|
+
expect(meta.MediaStorageSOPInstanceUID).toBe("2.25.7");
|
|
232
|
+
expect(meta.TransferSyntaxUID).toBe(ELE);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("body has file-set attributes but no SOP Common", () => {
|
|
236
|
+
expect(dataset.FileSetID).toBe("FOX_CD");
|
|
237
|
+
expect(dataset.FileSetConsistencyFlag).toBe(0);
|
|
238
|
+
expect(dataset.SOPClassUID).toBeUndefined();
|
|
239
|
+
expect(dataset.SOPInstanceUID).toBeUndefined();
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe("validation", () => {
|
|
244
|
+
test("non-conformant file ID component throws with guidance", () => {
|
|
245
|
+
expect(() =>
|
|
246
|
+
writeDicomDir([
|
|
247
|
+
entry({ referencedFileID: ["series1", "img001.dcm"] })
|
|
248
|
+
])
|
|
249
|
+
).toThrow(/not ISO 9660 level 1 conformant/);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test("allowNonConformingFileIDs bypasses the check", () => {
|
|
253
|
+
const buffer = writeDicomDir(
|
|
254
|
+
[entry({ referencedFileID: ["series1", "img001.dcm"] })],
|
|
255
|
+
{ allowNonConformingFileIDs: true }
|
|
256
|
+
);
|
|
257
|
+
const { dataset } = readDicomDir(buffer);
|
|
258
|
+
const image = recordsOf(dataset).find(
|
|
259
|
+
r => r.DirectoryRecordType === "IMAGE"
|
|
260
|
+
);
|
|
261
|
+
expect(image.ReferencedFileID).toEqual(["series1", "img001.dcm"]);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("missing PatientID throws", () => {
|
|
265
|
+
expect(() =>
|
|
266
|
+
writeDicomDir([entry({ patient: { PatientName: "X" } })])
|
|
267
|
+
).toThrow(/PatientID is required/);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("missing StudyInstanceUID throws", () => {
|
|
271
|
+
expect(() =>
|
|
272
|
+
writeDicomDir([entry({ study: { StudyID: "1" } })])
|
|
273
|
+
).toThrow(/StudyInstanceUID is required/);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("empty file set writes with zero root offsets", () => {
|
|
277
|
+
const { dataset } = readDicomDir(writeDicomDir([]));
|
|
278
|
+
expect(
|
|
279
|
+
dataset.OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity
|
|
280
|
+
).toBe(0);
|
|
281
|
+
expect(
|
|
282
|
+
dataset.OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity
|
|
283
|
+
).toBe(0);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// packages/dicomdir/test/mediaRoundTrip.long.test.js
|
|
2
|
+
//
|
|
3
|
+
// Opt-in long test: a full media round trip at video scale. Skipped unless
|
|
4
|
+
// DCMJS_LONG_TESTS is set, because it writes a multi-gigabyte file to the
|
|
5
|
+
// temp directory:
|
|
6
|
+
//
|
|
7
|
+
// DCMJS_LONG_TESTS=1 pnpm jest packages/dicomdir
|
|
8
|
+
//
|
|
9
|
+
// What it pins: a DICOMDIR itself never contains pixel data — the records
|
|
10
|
+
// carry only paths and identity attributes, so writeDicomDir's cost does not
|
|
11
|
+
// depend on how big the referenced files are. The size-sensitive half of a
|
|
12
|
+
// media workflow is reading the referenced files back, and that must go
|
|
13
|
+
// through the bounded-memory streaming path. So this test builds the media
|
|
14
|
+
// a real burner would: a synthesized MPEG-4 (Sup 149/225-style) video
|
|
15
|
+
// instance far larger than any sane heap allocation, streamed to disk chunk
|
|
16
|
+
// by chunk, plus the DICOMDIR that indexes it. Then it reads the media back
|
|
17
|
+
// the way a workstation would: parse the DICOMDIR (ordinary Part 10, tiny),
|
|
18
|
+
// resolve ReferencedFileID to the file on disk, and stream-parse the huge
|
|
19
|
+
// file through fromPart10Stream with a listener that retains nothing —
|
|
20
|
+
// proving the indexed file is readable end-to-end at a size that would OOM
|
|
21
|
+
// any buffer-everything path. (The memory *bounds* of the streaming parser
|
|
22
|
+
// are pinned separately by the K5b tests in test/eventStream/.)
|
|
23
|
+
//
|
|
24
|
+
// Size defaults to 2 GiB of pixel fragments; override with
|
|
25
|
+
// DCMJS_LONG_TEST_VIDEO_BYTES (bytes, rounded down to whole fragments).
|
|
26
|
+
|
|
27
|
+
import fs from "fs";
|
|
28
|
+
import os from "os";
|
|
29
|
+
import path from "path";
|
|
30
|
+
import {
|
|
31
|
+
fromPart10,
|
|
32
|
+
fromPart10Stream,
|
|
33
|
+
NaturalizedListener,
|
|
34
|
+
EventStreamListener
|
|
35
|
+
} from "@dcmjs-org/parser";
|
|
36
|
+
import { writeDicomDir } from "../src/index.js";
|
|
37
|
+
|
|
38
|
+
const LONG = !!process.env.DCMJS_LONG_TESTS;
|
|
39
|
+
const longTest = LONG ? test : test.skip;
|
|
40
|
+
|
|
41
|
+
const MPEG4_TS = "1.2.840.10008.1.2.4.102"; // MPEG-4 AVC/H.264 HiP@4.1
|
|
42
|
+
const VIDEO_PHOTOGRAPHIC_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.77.4.1";
|
|
43
|
+
const SOP_INSTANCE_UID = "1.2.3.4.200";
|
|
44
|
+
|
|
45
|
+
const FRAGMENT_SIZE = 4 * 1024 * 1024; // 4 MiB per on-wire fragment
|
|
46
|
+
const DEFAULT_PIXEL_BYTES = 2 * 1024 * 1024 * 1024; // 2 GiB
|
|
47
|
+
const PIXEL_BYTES =
|
|
48
|
+
Number(process.env.DCMJS_LONG_TEST_VIDEO_BYTES) || DEFAULT_PIXEL_BYTES;
|
|
49
|
+
const FRAGMENT_COUNT = Math.max(1, Math.floor(PIXEL_BYTES / FRAGMENT_SIZE));
|
|
50
|
+
|
|
51
|
+
// --- Minimal explicit-VR-little-endian byte emission -----------------------
|
|
52
|
+
// Just enough to synthesize the video file's envelope; the dicomdir package
|
|
53
|
+
// writer is NOT used for this file — the point is to index a file produced
|
|
54
|
+
// independently, as burned media would be.
|
|
55
|
+
|
|
56
|
+
function asciiEven(s) {
|
|
57
|
+
const padded = s.length % 2 ? s + "\0" : s;
|
|
58
|
+
const bytes = new Uint8Array(padded.length);
|
|
59
|
+
for (let i = 0; i < padded.length; i++) {
|
|
60
|
+
bytes[i] = padded.charCodeAt(i);
|
|
61
|
+
}
|
|
62
|
+
return bytes;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Short-form element (UI/CS/US/IS...): 2-byte length. */
|
|
66
|
+
function shortElem(group, element, vr, valueBytes) {
|
|
67
|
+
const out = new Uint8Array(8 + valueBytes.length);
|
|
68
|
+
const dv = new DataView(out.buffer);
|
|
69
|
+
dv.setUint16(0, group, true);
|
|
70
|
+
dv.setUint16(2, element, true);
|
|
71
|
+
out[4] = vr.charCodeAt(0);
|
|
72
|
+
out[5] = vr.charCodeAt(1);
|
|
73
|
+
dv.setUint16(6, valueBytes.length, true);
|
|
74
|
+
out.set(valueBytes, 8);
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Long-form element (OB/OW...): 2 reserved bytes + 4-byte length. */
|
|
79
|
+
function longElemHeader(group, element, vr, length) {
|
|
80
|
+
const out = new Uint8Array(12);
|
|
81
|
+
const dv = new DataView(out.buffer);
|
|
82
|
+
dv.setUint16(0, group, true);
|
|
83
|
+
dv.setUint16(2, element, true);
|
|
84
|
+
out[4] = vr.charCodeAt(0);
|
|
85
|
+
out[5] = vr.charCodeAt(1);
|
|
86
|
+
dv.setUint32(8, length, true);
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function itemHeader(length) {
|
|
91
|
+
const out = new Uint8Array(8);
|
|
92
|
+
const dv = new DataView(out.buffer);
|
|
93
|
+
dv.setUint16(0, 0xfffe, true);
|
|
94
|
+
dv.setUint16(2, 0xe000, true);
|
|
95
|
+
dv.setUint32(4, length, true);
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function sequenceDelimiter() {
|
|
100
|
+
const out = new Uint8Array(8);
|
|
101
|
+
const dv = new DataView(out.buffer);
|
|
102
|
+
dv.setUint16(0, 0xfffe, true);
|
|
103
|
+
dv.setUint16(2, 0xe0dd, true);
|
|
104
|
+
dv.setUint32(4, 0, true);
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function us(n) {
|
|
109
|
+
const out = new Uint8Array(2);
|
|
110
|
+
new DataView(out.buffer).setUint16(0, n, true);
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Yield the complete Part 10 video file as chunks: preamble + DICM, FMI,
|
|
116
|
+
* a small dataset, then FRAGMENT_COUNT encapsulated pixel fragments of
|
|
117
|
+
* FRAGMENT_SIZE bytes each (fragment i filled with i & 0xff). Only one
|
|
118
|
+
* fragment's bytes exist at a time.
|
|
119
|
+
*/
|
|
120
|
+
function* videoFileChunks() {
|
|
121
|
+
const preamble = new Uint8Array(132);
|
|
122
|
+
preamble.set(asciiEven("DICM"), 128);
|
|
123
|
+
yield preamble;
|
|
124
|
+
|
|
125
|
+
const fmiElements = [
|
|
126
|
+
longElemHeader(0x0002, 0x0001, "OB", 2),
|
|
127
|
+
new Uint8Array([0, 1]),
|
|
128
|
+
shortElem(
|
|
129
|
+
0x0002,
|
|
130
|
+
0x0002,
|
|
131
|
+
"UI",
|
|
132
|
+
asciiEven(VIDEO_PHOTOGRAPHIC_SOP_CLASS)
|
|
133
|
+
),
|
|
134
|
+
shortElem(0x0002, 0x0003, "UI", asciiEven(SOP_INSTANCE_UID)),
|
|
135
|
+
shortElem(0x0002, 0x0010, "UI", asciiEven(MPEG4_TS))
|
|
136
|
+
];
|
|
137
|
+
const fmiLength = fmiElements.reduce((n, b) => n + b.length, 0);
|
|
138
|
+
const groupLength = new Uint8Array(4);
|
|
139
|
+
new DataView(groupLength.buffer).setUint32(0, fmiLength, true);
|
|
140
|
+
yield shortElem(0x0002, 0x0000, "UL", groupLength);
|
|
141
|
+
yield* fmiElements;
|
|
142
|
+
|
|
143
|
+
yield shortElem(
|
|
144
|
+
0x0008,
|
|
145
|
+
0x0016,
|
|
146
|
+
"UI",
|
|
147
|
+
asciiEven(VIDEO_PHOTOGRAPHIC_SOP_CLASS)
|
|
148
|
+
);
|
|
149
|
+
yield shortElem(0x0008, 0x0018, "UI", asciiEven(SOP_INSTANCE_UID));
|
|
150
|
+
yield shortElem(0x0008, 0x0060, "CS", asciiEven("XC"));
|
|
151
|
+
yield shortElem(0x0028, 0x0008, "IS", asciiEven(String(FRAGMENT_COUNT)));
|
|
152
|
+
yield shortElem(0x0028, 0x0010, "US", us(512));
|
|
153
|
+
yield shortElem(0x0028, 0x0011, "US", us(512));
|
|
154
|
+
|
|
155
|
+
// (7FE0,0010) OB undefined length, empty Basic Offset Table, fragments.
|
|
156
|
+
yield longElemHeader(0x7fe0, 0x0010, "OB", 0xffffffff);
|
|
157
|
+
yield itemHeader(0);
|
|
158
|
+
for (let i = 0; i < FRAGMENT_COUNT; i++) {
|
|
159
|
+
yield itemHeader(FRAGMENT_SIZE);
|
|
160
|
+
const fragment = new Uint8Array(FRAGMENT_SIZE);
|
|
161
|
+
fragment.fill(i & 0xff);
|
|
162
|
+
yield fragment;
|
|
163
|
+
}
|
|
164
|
+
yield sequenceDelimiter();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Stream the chunks to disk without accumulating them in memory. */
|
|
168
|
+
async function writeVideoFile(filePath) {
|
|
169
|
+
const stream = fs.createWriteStream(filePath);
|
|
170
|
+
let size = 0;
|
|
171
|
+
for (const chunk of videoFileChunks()) {
|
|
172
|
+
size += chunk.length;
|
|
173
|
+
if (!stream.write(chunk)) {
|
|
174
|
+
await new Promise(resolve => stream.once("drain", resolve));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
await new Promise((resolve, reject) => {
|
|
178
|
+
stream.end(() => resolve());
|
|
179
|
+
stream.on("error", reject);
|
|
180
|
+
});
|
|
181
|
+
return size;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A listener that retains nothing: it counts the pixel-data fragment bytes
|
|
186
|
+
* and remembers the identity values it sees. Binary events are scoped to
|
|
187
|
+
* (7FE0,0010) because FileMetaInformationVersion is also OB and emits its
|
|
188
|
+
* own (non-encapsulated) startBinary. If the huge file's pixel data were
|
|
189
|
+
* ever materialized instead of streamed, jest's heap would not survive it.
|
|
190
|
+
*/
|
|
191
|
+
class CountingListener extends EventStreamListener {
|
|
192
|
+
constructor() {
|
|
193
|
+
super();
|
|
194
|
+
this.pixelBytes = 0;
|
|
195
|
+
this.pixelEncapsulated = null;
|
|
196
|
+
this.uiValues = [];
|
|
197
|
+
this._inPixelData = false;
|
|
198
|
+
}
|
|
199
|
+
_baseStartElement(tag) {
|
|
200
|
+
this._inPixelData = tag === "7FE00010";
|
|
201
|
+
}
|
|
202
|
+
_baseStartBinary(info = {}) {
|
|
203
|
+
if (this._inPixelData) {
|
|
204
|
+
this.pixelEncapsulated = !!info.encapsulated;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
_baseBinaryFragment(buf) {
|
|
208
|
+
if (this._inPixelData) {
|
|
209
|
+
this.pixelBytes += buf.byteLength ?? buf.length ?? 0;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
_baseValue(value) {
|
|
213
|
+
if (typeof value === "string" && /^[0-9.]+$/.test(value)) {
|
|
214
|
+
this.uiValues.push(value);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
describe("dicomdir — long media round trip at video scale", () => {
|
|
220
|
+
let mediaRoot;
|
|
221
|
+
|
|
222
|
+
beforeAll(() => {
|
|
223
|
+
if (!LONG) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
mediaRoot = fs.mkdtempSync(path.join(os.tmpdir(), "dcmjs-media-"));
|
|
227
|
+
fs.mkdirSync(path.join(mediaRoot, "VIDEO"));
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
afterAll(() => {
|
|
231
|
+
if (mediaRoot) {
|
|
232
|
+
fs.rmSync(mediaRoot, { recursive: true, force: true });
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
longTest(
|
|
237
|
+
"indexes a multi-GiB video file and reads the media back streaming",
|
|
238
|
+
async () => {
|
|
239
|
+
// --- Burn the media: huge video file + the DICOMDIR indexing it.
|
|
240
|
+
const videoPath = path.join(mediaRoot, "VIDEO", "MOV00001");
|
|
241
|
+
const videoSize = await writeVideoFile(videoPath);
|
|
242
|
+
expect(videoSize).toBeGreaterThan(FRAGMENT_COUNT * FRAGMENT_SIZE);
|
|
243
|
+
|
|
244
|
+
const dicomdirBytes = writeDicomDir(
|
|
245
|
+
[
|
|
246
|
+
{
|
|
247
|
+
referencedFileID: ["VIDEO", "MOV00001"],
|
|
248
|
+
sopClassUid: VIDEO_PHOTOGRAPHIC_SOP_CLASS,
|
|
249
|
+
sopInstanceUid: SOP_INSTANCE_UID,
|
|
250
|
+
transferSyntaxUid: MPEG4_TS,
|
|
251
|
+
patient: {
|
|
252
|
+
PatientID: "998877",
|
|
253
|
+
PatientName: "DOE^JANE"
|
|
254
|
+
},
|
|
255
|
+
study: { StudyInstanceUID: "1.2.3.4" },
|
|
256
|
+
series: {
|
|
257
|
+
SeriesInstanceUID: "1.2.3.4.5",
|
|
258
|
+
Modality: "XC"
|
|
259
|
+
},
|
|
260
|
+
instance: { InstanceNumber: 1 }
|
|
261
|
+
}
|
|
262
|
+
],
|
|
263
|
+
{ fileSetID: "VIDEO_CD" }
|
|
264
|
+
);
|
|
265
|
+
fs.writeFileSync(
|
|
266
|
+
path.join(mediaRoot, "DICOMDIR"),
|
|
267
|
+
new Uint8Array(dicomdirBytes)
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
// The DICOMDIR indexes the file; it does not contain it. Its size
|
|
271
|
+
// must be unrelated to the gigabytes it references.
|
|
272
|
+
expect(dicomdirBytes.byteLength).toBeLessThan(4096);
|
|
273
|
+
|
|
274
|
+
// --- Read the media back: DICOMDIR first, like a workstation.
|
|
275
|
+
const dirListener = new NaturalizedListener();
|
|
276
|
+
const dirBuffer = fs.readFileSync(path.join(mediaRoot, "DICOMDIR"));
|
|
277
|
+
await fromPart10(
|
|
278
|
+
dirBuffer.buffer.slice(
|
|
279
|
+
dirBuffer.byteOffset,
|
|
280
|
+
dirBuffer.byteOffset + dirBuffer.byteLength
|
|
281
|
+
),
|
|
282
|
+
dirListener
|
|
283
|
+
);
|
|
284
|
+
const records = dirListener.result.DirectoryRecordSequence;
|
|
285
|
+
const image = records.find(r => r.DirectoryRecordType === "IMAGE");
|
|
286
|
+
expect(image.ReferencedFileID).toEqual(["VIDEO", "MOV00001"]);
|
|
287
|
+
expect(image.ReferencedSOPInstanceUIDInFile).toBe(SOP_INSTANCE_UID);
|
|
288
|
+
expect(image.ReferencedTransferSyntaxUIDInFile).toBe(MPEG4_TS);
|
|
289
|
+
|
|
290
|
+
// --- Resolve the record to the file on disk and stream-parse it.
|
|
291
|
+
const resolved = path.join(mediaRoot, ...image.ReferencedFileID);
|
|
292
|
+
expect(fs.statSync(resolved).size).toBe(videoSize);
|
|
293
|
+
|
|
294
|
+
const counter = new CountingListener();
|
|
295
|
+
await fromPart10Stream(fs.createReadStream(resolved), counter);
|
|
296
|
+
|
|
297
|
+
expect(counter.pixelEncapsulated).toBe(true);
|
|
298
|
+
expect(counter.pixelBytes).toBe(FRAGMENT_COUNT * FRAGMENT_SIZE);
|
|
299
|
+
expect(counter.uiValues).toContain(SOP_INSTANCE_UID);
|
|
300
|
+
},
|
|
301
|
+
20 * 60 * 1000
|
|
302
|
+
);
|
|
303
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// packages/dicomdir/test/standalone.test.js
|
|
2
|
+
//
|
|
3
|
+
// The package exists to build and write DICOMDIRs; what 1.0 additionally
|
|
4
|
+
// promises is that it does so without the legacy engines. This file pins
|
|
5
|
+
// that second property: it imports nothing from @dcmjs-org/legacy — jest
|
|
6
|
+
// gives each test file its own module registry, so no eager DicomMessage
|
|
7
|
+
// is ever wired into core's late-binding seam here. Writing a DICOMDIR serializes a
|
|
8
|
+
// DirectoryRecordSequence, which exercises core's sequence writer with no
|
|
9
|
+
// injected engine; before SequenceOfItems.writeBytes called back into
|
|
10
|
+
// writeDataSet directly, this write threw.
|
|
11
|
+
|
|
12
|
+
import { writeDicomDir } from "../src/index.js";
|
|
13
|
+
|
|
14
|
+
const MR_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.4";
|
|
15
|
+
const ELE = "1.2.840.10008.1.2.1";
|
|
16
|
+
|
|
17
|
+
function countItemTags(bytes) {
|
|
18
|
+
// FFFE,E000 little-endian with the undefined-length marker the item
|
|
19
|
+
// headers carry — anchoring on all eight bytes keeps value bytes from
|
|
20
|
+
// matching by accident.
|
|
21
|
+
let count = 0;
|
|
22
|
+
for (let i = 0; i + 8 <= bytes.length; i++) {
|
|
23
|
+
if (
|
|
24
|
+
bytes[i] === 0xfe &&
|
|
25
|
+
bytes[i + 1] === 0xff &&
|
|
26
|
+
bytes[i + 2] === 0x00 &&
|
|
27
|
+
bytes[i + 3] === 0xe0 &&
|
|
28
|
+
bytes[i + 4] === 0xff &&
|
|
29
|
+
bytes[i + 5] === 0xff &&
|
|
30
|
+
bytes[i + 6] === 0xff &&
|
|
31
|
+
bytes[i + 7] === 0xff
|
|
32
|
+
) {
|
|
33
|
+
count++;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return count;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("writes a complete DICOMDIR with no eager engine loaded", () => {
|
|
40
|
+
const arrayBuffer = writeDicomDir(
|
|
41
|
+
[
|
|
42
|
+
{
|
|
43
|
+
referencedFileID: ["DICOM", "IM000001"],
|
|
44
|
+
sopClassUid: MR_SOP_CLASS,
|
|
45
|
+
sopInstanceUid: "1.2.3.4.100",
|
|
46
|
+
transferSyntaxUid: ELE,
|
|
47
|
+
patient: { PatientID: "998877", PatientName: "DOE^JANE" },
|
|
48
|
+
study: { StudyInstanceUID: "1.2.3.4" },
|
|
49
|
+
series: { SeriesInstanceUID: "1.2.3.4.5", Modality: "MR" },
|
|
50
|
+
instance: { InstanceNumber: 1 }
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
{ fileSetID: "MY_CD", fileSetUID: "2.25.99" }
|
|
54
|
+
);
|
|
55
|
+
const bytes = new Uint8Array(arrayBuffer);
|
|
56
|
+
|
|
57
|
+
// Part 10 magic after the 128-byte preamble.
|
|
58
|
+
expect(String.fromCharCode(...bytes.slice(128, 132))).toBe("DICM");
|
|
59
|
+
// One entry yields PATIENT/STUDY/SERIES/IMAGE — four record items.
|
|
60
|
+
expect(countItemTags(bytes)).toBe(4);
|
|
61
|
+
// Deterministic under a fixed fileSetUID, same as the main suite.
|
|
62
|
+
const again = new Uint8Array(
|
|
63
|
+
writeDicomDir(
|
|
64
|
+
[
|
|
65
|
+
{
|
|
66
|
+
referencedFileID: ["DICOM", "IM000001"],
|
|
67
|
+
sopClassUid: MR_SOP_CLASS,
|
|
68
|
+
sopInstanceUid: "1.2.3.4.100",
|
|
69
|
+
transferSyntaxUid: ELE,
|
|
70
|
+
patient: { PatientID: "998877", PatientName: "DOE^JANE" },
|
|
71
|
+
study: { StudyInstanceUID: "1.2.3.4" },
|
|
72
|
+
series: { SeriesInstanceUID: "1.2.3.4.5", Modality: "MR" },
|
|
73
|
+
instance: { InstanceNumber: 1 }
|
|
74
|
+
}
|
|
75
|
+
],
|
|
76
|
+
{ fileSetID: "MY_CD", fileSetUID: "2.25.99" }
|
|
77
|
+
)
|
|
78
|
+
);
|
|
79
|
+
expect(again).toEqual(bytes);
|
|
80
|
+
});
|