@dcmjs-org/dicomdir 1.0.0-beta.3 → 1.0.0-beta.5
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/License.txt +22 -0
- package/dist/index.cjs +487 -0
- package/dist/index.cjs.map +1 -0
- package/{src/dicomdir.js → dist/index.mjs} +104 -10
- package/dist/index.mjs.map +1 -0
- package/package.json +21 -6
- package/src/directoryOffsets.js +0 -104
- package/src/index.js +0 -17
- package/test/dicomdir.test.js +0 -285
- package/test/mediaRoundTrip.long.test.js +0 -303
- package/test/standalone.test.js +0 -80
|
@@ -1,3 +1,103 @@
|
|
|
1
|
+
import { WriteBufferStream, writeTagObject, TagHex, EXPLICIT_LITTLE_ENDIAN, writeDataSet, DicomMetaDictionary } from '@dcmjs-org/core';
|
|
2
|
+
|
|
3
|
+
// packages/dicomdir/src/directoryOffsets.js
|
|
4
|
+
//
|
|
5
|
+
// Byte-offset computation for DICOMDIR directory records.
|
|
6
|
+
//
|
|
7
|
+
// The load-bearing fact: every DICOMDIR offset attribute (0004,1200/1202/
|
|
8
|
+
// 1400/1420) is VR UL — a fixed four-byte value — so the values NEVER affect
|
|
9
|
+
// the byte layout. One measurement pass over the exact structures the final
|
|
10
|
+
// write will serialize (same writer, same options, placeholder zeros)
|
|
11
|
+
// therefore yields the exact offsets of the final file, and the file is
|
|
12
|
+
// written once with the real values filled in.
|
|
13
|
+
//
|
|
14
|
+
// Layout being measured (mirrors the Part 10 envelope in dicomdir.js):
|
|
15
|
+
// 128-byte preamble + "DICM"
|
|
16
|
+
// FileMetaInformationGroupLength element
|
|
17
|
+
// meta group (Explicit VR Little Endian)
|
|
18
|
+
// body tags below (0004,1220), sorted ascending
|
|
19
|
+
// (0004,1220) DirectoryRecordSequence: 12-byte SQ header (undefined
|
|
20
|
+
// length), then per item: 8-byte item header (FFFE,E000, undefined
|
|
21
|
+
// length) + item body + 8-byte item delimiter (FFFE,E00D) — see
|
|
22
|
+
// SequenceOfItems.writeBytes — and a trailing 8-byte sequence delimiter.
|
|
23
|
+
//
|
|
24
|
+
// A directory-record offset points at the FFFE,E000 item tag of that record,
|
|
25
|
+
// counted from byte 0 of the file (PS3.10 8.6).
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const DIRECTORY_RECORD_SEQUENCE = "00041220";
|
|
29
|
+
|
|
30
|
+
const ITEM_HEADER_BYTES = 8; // FFFE,E000 + 4-byte undefined length
|
|
31
|
+
const ITEM_DELIMITER_BYTES = 8; // FFFE,E00D + 4-byte zero length
|
|
32
|
+
|
|
33
|
+
/** Bytes writeDataSet produces for one tag-keyed object. */
|
|
34
|
+
function measure(jsonObject, writeOptions) {
|
|
35
|
+
const stream = new WriteBufferStream(1024);
|
|
36
|
+
writeDataSet(jsonObject, stream, EXPLICIT_LITTLE_ENDIAN, writeOptions);
|
|
37
|
+
return stream.size;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Compute the absolute byte offset of every directory record item in the
|
|
42
|
+
* file the Part 10 envelope write will produce. Call with the offsets
|
|
43
|
+
* still zero; patch the returned values in, then write — the layout is
|
|
44
|
+
* guaranteed identical because offsets are fixed-width.
|
|
45
|
+
*
|
|
46
|
+
* @param {{ meta: Object, dict: Object }} dicomDict - denaturalized
|
|
47
|
+
* DICOMDIR ({ meta, dict }) whose dict contains (0004,1220)
|
|
48
|
+
* @param {Object} writeOptions - the SAME object later passed to write()
|
|
49
|
+
* @returns {number[]} byte offset of each record's item tag, in item order
|
|
50
|
+
*/
|
|
51
|
+
function computeDirectoryOffsets(dicomDict, writeOptions) {
|
|
52
|
+
// Preamble + magic.
|
|
53
|
+
let cursor = 128 + 4;
|
|
54
|
+
|
|
55
|
+
// FileMetaInformationGroupLength + meta group. The group length element
|
|
56
|
+
// is measured with the real meta size as its value — UL again, so the
|
|
57
|
+
// element size is value-independent, but measuring keeps zero assumptions.
|
|
58
|
+
const metaSize = measure(dicomDict.meta, writeOptions);
|
|
59
|
+
const groupLengthStream = new WriteBufferStream(64);
|
|
60
|
+
writeTagObject(
|
|
61
|
+
groupLengthStream,
|
|
62
|
+
TagHex.FileMetaInformationGroupLength,
|
|
63
|
+
"UL",
|
|
64
|
+
metaSize,
|
|
65
|
+
EXPLICIT_LITTLE_ENDIAN,
|
|
66
|
+
writeOptions
|
|
67
|
+
);
|
|
68
|
+
cursor += groupLengthStream.size + metaSize;
|
|
69
|
+
|
|
70
|
+
// Body tags before the record sequence, in the writer's sorted order.
|
|
71
|
+
const bodyTags = Object.keys(dicomDict.dict).sort();
|
|
72
|
+
for (const tag of bodyTags) {
|
|
73
|
+
if (tag >= DIRECTORY_RECORD_SEQUENCE) {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
cursor += measure({ [tag]: dicomDict.dict[tag] }, writeOptions);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// SQ element header: measure an empty sequence (header + 8-byte
|
|
80
|
+
// sequence delimiter) rather than assuming the 12-byte constant.
|
|
81
|
+
const sequence = dicomDict.dict[DIRECTORY_RECORD_SEQUENCE];
|
|
82
|
+
const emptySequenceSize = measure(
|
|
83
|
+
{ [DIRECTORY_RECORD_SEQUENCE]: { vr: "SQ", Value: [] } },
|
|
84
|
+
writeOptions
|
|
85
|
+
);
|
|
86
|
+
cursor += emptySequenceSize - ITEM_DELIMITER_BYTES;
|
|
87
|
+
|
|
88
|
+
const offsets = [];
|
|
89
|
+
const items = (sequence && sequence.Value) || [];
|
|
90
|
+
for (const item of items) {
|
|
91
|
+
offsets.push(cursor);
|
|
92
|
+
cursor +=
|
|
93
|
+
ITEM_HEADER_BYTES +
|
|
94
|
+
measure(item, writeOptions) +
|
|
95
|
+
ITEM_DELIMITER_BYTES;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return offsets;
|
|
99
|
+
}
|
|
100
|
+
|
|
1
101
|
// packages/dicomdir/src/dicomdir.js
|
|
2
102
|
//
|
|
3
103
|
// DICOMDIR (Media Storage Directory, PS3.10 / PS3.3 F) builder. Turns a flat
|
|
@@ -11,15 +111,6 @@
|
|
|
11
111
|
// placeholder zeros produces the exact final layout, and the file is written
|
|
12
112
|
// once. No patch-and-rewrite, no buffer scanning.
|
|
13
113
|
|
|
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
114
|
|
|
24
115
|
const MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID = "1.2.840.10008.1.3.10";
|
|
25
116
|
|
|
@@ -379,8 +470,11 @@ function writeDicomDir(entries = [], options = {}) {
|
|
|
379
470
|
return writePart10(meta, dict, writeOptions);
|
|
380
471
|
}
|
|
381
472
|
|
|
382
|
-
|
|
473
|
+
var index = {
|
|
383
474
|
buildDicomDirDataset,
|
|
384
475
|
writeDicomDir,
|
|
385
476
|
MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID
|
|
386
477
|
};
|
|
478
|
+
|
|
479
|
+
export { MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID, buildDicomDirDataset, index as default, writeDicomDir };
|
|
480
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/directoryOffsets.js","../src/dicomdir.js","../src/index.js"],"sourcesContent":["// packages/dicomdir/src/directoryOffsets.js\n//\n// Byte-offset computation for DICOMDIR directory records.\n//\n// The load-bearing fact: every DICOMDIR offset attribute (0004,1200/1202/\n// 1400/1420) is VR UL — a fixed four-byte value — so the values NEVER affect\n// the byte layout. One measurement pass over the exact structures the final\n// write will serialize (same writer, same options, placeholder zeros)\n// therefore yields the exact offsets of the final file, and the file is\n// written once with the real values filled in.\n//\n// Layout being measured (mirrors the Part 10 envelope in dicomdir.js):\n// 128-byte preamble + \"DICM\"\n// FileMetaInformationGroupLength element\n// meta group (Explicit VR Little Endian)\n// body tags below (0004,1220), sorted ascending\n// (0004,1220) DirectoryRecordSequence: 12-byte SQ header (undefined\n// length), then per item: 8-byte item header (FFFE,E000, undefined\n// length) + item body + 8-byte item delimiter (FFFE,E00D) — see\n// SequenceOfItems.writeBytes — and a trailing 8-byte sequence delimiter.\n//\n// A directory-record offset points at the FFFE,E000 item tag of that record,\n// counted from byte 0 of the file (PS3.10 8.6).\n\nimport {\n WriteBufferStream,\n writeDataSet,\n writeTagObject,\n EXPLICIT_LITTLE_ENDIAN,\n TagHex\n} from \"@dcmjs-org/core\";\n\nconst DIRECTORY_RECORD_SEQUENCE = \"00041220\";\n\nconst ITEM_HEADER_BYTES = 8; // FFFE,E000 + 4-byte undefined length\nconst ITEM_DELIMITER_BYTES = 8; // FFFE,E00D + 4-byte zero length\n\n/** Bytes writeDataSet produces for one tag-keyed object. */\nfunction measure(jsonObject, writeOptions) {\n const stream = new WriteBufferStream(1024);\n writeDataSet(jsonObject, stream, EXPLICIT_LITTLE_ENDIAN, writeOptions);\n return stream.size;\n}\n\n/**\n * Compute the absolute byte offset of every directory record item in the\n * file the Part 10 envelope write will produce. Call with the offsets\n * still zero; patch the returned values in, then write — the layout is\n * guaranteed identical because offsets are fixed-width.\n *\n * @param {{ meta: Object, dict: Object }} dicomDict - denaturalized\n * DICOMDIR ({ meta, dict }) whose dict contains (0004,1220)\n * @param {Object} writeOptions - the SAME object later passed to write()\n * @returns {number[]} byte offset of each record's item tag, in item order\n */\nexport function computeDirectoryOffsets(dicomDict, writeOptions) {\n // Preamble + magic.\n let cursor = 128 + 4;\n\n // FileMetaInformationGroupLength + meta group. The group length element\n // is measured with the real meta size as its value — UL again, so the\n // element size is value-independent, but measuring keeps zero assumptions.\n const metaSize = measure(dicomDict.meta, writeOptions);\n const groupLengthStream = new WriteBufferStream(64);\n writeTagObject(\n groupLengthStream,\n TagHex.FileMetaInformationGroupLength,\n \"UL\",\n metaSize,\n EXPLICIT_LITTLE_ENDIAN,\n writeOptions\n );\n cursor += groupLengthStream.size + metaSize;\n\n // Body tags before the record sequence, in the writer's sorted order.\n const bodyTags = Object.keys(dicomDict.dict).sort();\n for (const tag of bodyTags) {\n if (tag >= DIRECTORY_RECORD_SEQUENCE) {\n break;\n }\n cursor += measure({ [tag]: dicomDict.dict[tag] }, writeOptions);\n }\n\n // SQ element header: measure an empty sequence (header + 8-byte\n // sequence delimiter) rather than assuming the 12-byte constant.\n const sequence = dicomDict.dict[DIRECTORY_RECORD_SEQUENCE];\n const emptySequenceSize = measure(\n { [DIRECTORY_RECORD_SEQUENCE]: { vr: \"SQ\", Value: [] } },\n writeOptions\n );\n cursor += emptySequenceSize - ITEM_DELIMITER_BYTES;\n\n const offsets = [];\n const items = (sequence && sequence.Value) || [];\n for (const item of items) {\n offsets.push(cursor);\n cursor +=\n ITEM_HEADER_BYTES +\n measure(item, writeOptions) +\n ITEM_DELIMITER_BYTES;\n }\n\n return offsets;\n}\n","// packages/dicomdir/src/dicomdir.js\n//\n// DICOMDIR (Media Storage Directory, PS3.10 / PS3.3 F) builder. Turns a flat\n// file-set description into the PATIENT -> STUDY -> SERIES -> leaf record\n// hierarchy and serializes it with real byte offsets in the three offset\n// attributes (OffsetOfTheNextDirectoryRecord, OffsetOfReferencedLowerLevel-\n// DirectoryEntity, and the root first/last record offsets).\n//\n// Offsets are computed by measure-then-write (see directoryOffsets.js): every\n// offset attribute is VR UL — fixed four bytes — so a measurement pass with\n// placeholder zeros produces the exact final layout, and the file is written\n// once. No patch-and-rewrite, no buffer scanning.\n\nimport {\n DicomMetaDictionary,\n WriteBufferStream,\n writeDataSet,\n writeTagObject,\n EXPLICIT_LITTLE_ENDIAN,\n TagHex\n} from \"@dcmjs-org/core\";\nimport { computeDirectoryOffsets } from \"./directoryOffsets.js\";\n\nconst MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID = \"1.2.840.10008.1.3.10\";\n\n// PS3.10 8.5: each File ID component is 1-8 characters from the ISO 9660\n// level 1 repertoire (A-Z, 0-9, underscore), at most 8 components deep.\nconst FILE_ID_COMPONENT = /^[A-Z0-9_]{1,8}$/;\n\nfunction assertEntry(entry, index) {\n const where = `buildDicomDirDataset: entries[${index}]`;\n if (!entry || typeof entry !== \"object\") {\n throw new Error(`${where} is not an object`);\n }\n if (\n !Array.isArray(entry.referencedFileID) ||\n !entry.referencedFileID.length\n ) {\n throw new Error(\n `${where}.referencedFileID must be a non-empty array of path components`\n );\n }\n for (const required of [\n \"sopClassUid\",\n \"sopInstanceUid\",\n \"transferSyntaxUid\"\n ]) {\n if (!entry[required]) {\n throw new Error(`${where}.${required} is required`);\n }\n }\n if (!entry.patient?.PatientID) {\n throw new Error(`${where}.patient.PatientID is required`);\n }\n if (!entry.study?.StudyInstanceUID) {\n throw new Error(`${where}.study.StudyInstanceUID is required`);\n }\n if (!entry.series?.SeriesInstanceUID) {\n throw new Error(`${where}.series.SeriesInstanceUID is required`);\n }\n}\n\nfunction assertFileID(components, index, allowNonConforming) {\n if (components.length > 8) {\n throw new Error(\n `buildDicomDirDataset: entries[${index}].referencedFileID has ` +\n `${components.length} components (max 8)`\n );\n }\n if (allowNonConforming) {\n return;\n }\n for (const component of components) {\n if (!FILE_ID_COMPONENT.test(component)) {\n throw new Error(\n `buildDicomDirDataset: entries[${index}].referencedFileID ` +\n `component \"${component}\" is not ISO 9660 level 1 ` +\n `conformant (A-Z 0-9 _, 1-8 chars) — rename the file or ` +\n `pass allowNonConformingFileIDs: true`\n );\n }\n }\n}\n\n/** The four keys every directory record starts with (PS3.3 F.3). */\nfunction recordCommon(recordType) {\n return {\n OffsetOfTheNextDirectoryRecord: 0,\n RecordInUseFlag: 0xffff,\n OffsetOfReferencedLowerLevelDirectoryEntity: 0,\n DirectoryRecordType: recordType\n };\n}\n\n/**\n * Group the flat entries into the PATIENT -> STUDY -> SERIES -> leaf tree,\n * preserving first-seen order at every level.\n */\nfunction groupEntries(entries, options) {\n const patients = new Map();\n entries.forEach((entry, index) => {\n assertEntry(entry, index);\n assertFileID(\n entry.referencedFileID,\n index,\n options.allowNonConformingFileIDs\n );\n\n const patientKey = entry.patient.PatientID;\n if (!patients.has(patientKey)) {\n patients.set(patientKey, {\n attributes: entry.patient,\n studies: new Map()\n });\n }\n const patient = patients.get(patientKey);\n\n const studyKey = entry.study.StudyInstanceUID;\n if (!patient.studies.has(studyKey)) {\n patient.studies.set(studyKey, {\n attributes: entry.study,\n series: new Map()\n });\n }\n const study = patient.studies.get(studyKey);\n\n const seriesKey = entry.series.SeriesInstanceUID;\n if (!study.series.has(seriesKey)) {\n study.series.set(seriesKey, {\n attributes: entry.series,\n leaves: []\n });\n }\n study.series.get(seriesKey).leaves.push(entry);\n });\n return patients;\n}\n\n/**\n * Flatten the tree depth-first into naturalized record objects, tracking the\n * flat index of each record's next sibling and first child so the offset\n * pass can wire the chains. Depth-first order means every link points\n * strictly forward in the file.\n *\n * @returns {{ records: Object[], links: Array<{next: number|null, child: number|null}> }}\n */\nfunction flattenRecords(patients) {\n const records = [];\n const links = [];\n\n const push = record => {\n records.push(record);\n links.push({ next: null, child: null });\n return records.length - 1;\n };\n\n const patientList = [...patients.values()];\n let previousPatientIndex = null;\n\n for (const patient of patientList) {\n const patientIndex = push({\n ...recordCommon(\"PATIENT\"),\n PatientName: patient.attributes.PatientName ?? \"\",\n PatientID: patient.attributes.PatientID\n });\n if (previousPatientIndex !== null) {\n links[previousPatientIndex].next = patientIndex;\n }\n previousPatientIndex = patientIndex;\n\n const studyList = [...patient.studies.values()];\n let previousStudyIndex = null;\n for (const study of studyList) {\n const studyIndex = push({\n ...recordCommon(\"STUDY\"),\n StudyDate: study.attributes.StudyDate ?? \"\",\n StudyTime: study.attributes.StudyTime ?? \"\",\n StudyDescription: study.attributes.StudyDescription ?? \"\",\n AccessionNumber: study.attributes.AccessionNumber ?? \"\",\n StudyInstanceUID: study.attributes.StudyInstanceUID,\n StudyID: study.attributes.StudyID ?? \"\"\n });\n if (previousStudyIndex === null) {\n links[patientIndex].child = studyIndex;\n } else {\n links[previousStudyIndex].next = studyIndex;\n }\n previousStudyIndex = studyIndex;\n\n const seriesList = [...study.series.values()];\n let previousSeriesIndex = null;\n for (const series of seriesList) {\n const seriesIndex = push({\n ...recordCommon(\"SERIES\"),\n Modality: series.attributes.Modality ?? \"OT\",\n SeriesInstanceUID: series.attributes.SeriesInstanceUID,\n SeriesNumber: series.attributes.SeriesNumber ?? 1\n });\n if (previousSeriesIndex === null) {\n links[studyIndex].child = seriesIndex;\n } else {\n links[previousSeriesIndex].next = seriesIndex;\n }\n previousSeriesIndex = seriesIndex;\n\n let previousLeafIndex = null;\n for (const entry of series.leaves) {\n const leafIndex = push({\n ...recordCommon(entry.recordType || \"IMAGE\"),\n ReferencedFileID: [...entry.referencedFileID],\n ReferencedSOPClassUIDInFile: entry.sopClassUid,\n ReferencedSOPInstanceUIDInFile: entry.sopInstanceUid,\n ReferencedTransferSyntaxUIDInFile:\n entry.transferSyntaxUid,\n InstanceNumber: entry.instance?.InstanceNumber ?? 1,\n ...entry.instance\n });\n if (previousLeafIndex === null) {\n links[seriesIndex].child = leafIndex;\n } else {\n links[previousLeafIndex].next = leafIndex;\n }\n previousLeafIndex = leafIndex;\n }\n }\n }\n }\n\n return { records, links };\n}\n\n/**\n * Build the naturalized DICOMDIR dataset (all offsets zero) plus the link\n * table the offset pass needs. Pure and inspectable — no serialization.\n *\n * @param {Array} entries - flat file-set entries; see module JSDoc/typedef\n * @param {Object} [options]\n * @param {string} [options.fileSetID=\"\"]\n * @param {boolean} [options.allowNonConformingFileIDs=false]\n * @returns {{ dataset: Object, links: Array, rootIndices: number[] }}\n */\nfunction buildDicomDirDataset(entries = [], options = {}) {\n const patients = groupEntries(entries, options);\n const { records, links } = flattenRecords(patients);\n\n const rootIndices = [];\n records.forEach((record, index) => {\n if (record.DirectoryRecordType === \"PATIENT\") {\n rootIndices.push(index);\n }\n });\n\n const dataset = {\n FileSetID: options.fileSetID ?? \"\",\n OffsetOfTheFirstDirectoryRecordOfTheRootDirectoryEntity: 0,\n OffsetOfTheLastDirectoryRecordOfTheRootDirectoryEntity: 0,\n FileSetConsistencyFlag: 0,\n DirectoryRecordSequence: records\n };\n\n return { dataset, links, rootIndices };\n}\n\n/** Mint the DICOMDIR file meta group (SOP identity lives here, not the body). */\nfunction buildDicomDirMeta(options = {}) {\n const fileMetaInformationVersion = new Uint8Array(2);\n fileMetaInformationVersion[1] = 1;\n return DicomMetaDictionary.denaturalizeDataset({\n FileMetaInformationVersion: fileMetaInformationVersion.buffer,\n MediaStorageSOPClassUID: MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID,\n MediaStorageSOPInstanceUID:\n options.fileSetUID || DicomMetaDictionary.uid(),\n TransferSyntaxUID: EXPLICIT_LITTLE_ENDIAN,\n ImplementationClassUID:\n \"2.25.80302813137786398554742050926734630921603366648225212145404\",\n ImplementationVersionName: \"dcmjs-0.0\"\n });\n}\n\n/**\n * Build and serialize a complete DICOMDIR: build the record tree,\n * denaturalize it, measure the layout, wire the offset chains, write once.\n *\n * @param {Array} entries - flat file-set entries\n * @param {Object} [options] - buildDicomDirDataset options plus\n * {string} [options.fileSetUID] fixed MediaStorageSOPInstanceUID\n * @returns {ArrayBuffer} the DICOMDIR file bytes\n */\n/**\n * The DICOM data dictionary gives the directory offset attributes the\n * special VR \"up\" (byte offset); the dcmjs writer has no \"up\" representation\n * and would fall back to UN. They are four-byte unsigned offsets — pin them\n * to UL so they serialize (and measure) as the fixed-width values the\n * offset algorithm depends on.\n */\nfunction pinOffsetVRs(dict) {\n for (const tag of [\"00041200\", \"00041202\"]) {\n if (dict[tag]) {\n dict[tag].vr = \"UL\";\n }\n }\n const items = dict[\"00041220\"]?.Value || [];\n for (const item of items) {\n for (const tag of [\"00041400\", \"00041420\"]) {\n if (item[tag]) {\n item[tag].vr = \"UL\";\n }\n }\n }\n}\n\n/**\n * Serialize { meta, dict } as a Part 10 file. This is the body of\n * DicomDict.write (packages/legacy) minus the deflate branch — a DICOMDIR\n * is always Explicit VR Little Endian, never deflated — rebuilt on core's\n * writeDataSet/writeTagObject so this package does not depend on\n * @dcmjs-org/legacy. DicomMessage.write and DicomMessage.writeTagObject\n * are one-line delegates to those same core functions, so the bytes this\n * produces are identical to DicomDict.write's, which is what keeps the\n * measured directory offsets valid.\n */\nfunction writePart10(meta, dict, writeOptions) {\n const metaSyntax = EXPLICIT_LITTLE_ENDIAN;\n const fileStream = new WriteBufferStream(4096, true);\n fileStream.writeUint8Repeat(0, 128);\n fileStream.writeAsciiString(\"DICM\");\n\n const metaStream = new WriteBufferStream(1024);\n if (!meta[TagHex.TransferSyntaxUID]) {\n meta[TagHex.TransferSyntaxUID] = {\n vr: \"UI\",\n Value: [EXPLICIT_LITTLE_ENDIAN]\n };\n }\n writeDataSet(meta, metaStream, metaSyntax, writeOptions);\n writeTagObject(\n fileStream,\n TagHex.FileMetaInformationGroupLength,\n \"UL\",\n metaStream.size,\n metaSyntax,\n writeOptions\n );\n fileStream.concat(metaStream);\n\n const useSyntax = meta[TagHex.TransferSyntaxUID].Value[0];\n writeDataSet(dict, fileStream, useSyntax, writeOptions);\n return fileStream.getBuffer();\n}\n\nfunction writeDicomDir(entries = [], options = {}) {\n const { dataset, links, rootIndices } = buildDicomDirDataset(\n entries,\n options\n );\n\n const meta = buildDicomDirMeta(options);\n const dict = DicomMetaDictionary.denaturalizeDataset(dataset);\n pinOffsetVRs(dict);\n\n const writeOptions = { allowInvalidVRLength: false };\n const recordOffsets = computeDirectoryOffsets({ meta, dict }, writeOptions);\n\n const sequence = dict[\"00041220\"];\n const items = (sequence && sequence.Value) || [];\n items.forEach((item, index) => {\n const { next, child } = links[index];\n item[\"00041400\"].Value = [next === null ? 0 : recordOffsets[next]];\n item[\"00041420\"].Value = [child === null ? 0 : recordOffsets[child]];\n });\n\n const first = rootIndices.length ? recordOffsets[rootIndices[0]] : 0;\n const last = rootIndices.length\n ? recordOffsets[rootIndices[rootIndices.length - 1]]\n : 0;\n dict[\"00041200\"].Value = [first];\n dict[\"00041202\"].Value = [last];\n\n return writePart10(meta, dict, writeOptions);\n}\n\nexport {\n buildDicomDirDataset,\n writeDicomDir,\n MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID\n};\n","import {\n buildDicomDirDataset,\n writeDicomDir,\n MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID\n} from \"./dicomdir.js\";\n\nexport {\n buildDicomDirDataset,\n writeDicomDir,\n MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID\n};\n\nexport default {\n buildDicomDirDataset,\n writeDicomDir,\n MEDIA_STORAGE_DIRECTORY_SOP_CLASS_UID\n};\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAUA,MAAM,yBAAyB,GAAG,UAAU;;AAE5C,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAC5B,MAAM,oBAAoB,GAAG,CAAC,CAAC;;AAE/B;AACA,SAAS,OAAO,CAAC,UAAU,EAAE,YAAY,EAAE;AAC3C,IAAI,MAAM,MAAM,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC;AAC9C,IAAI,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,sBAAsB,EAAE,YAAY,CAAC;AAC1E,IAAI,OAAO,MAAM,CAAC,IAAI;AACtB;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,uBAAuB,CAAC,SAAS,EAAE,YAAY,EAAE;AACjE;AACA,IAAI,IAAI,MAAM,GAAG,GAAG,GAAG,CAAC;;AAExB;AACA;AACA;AACA,IAAI,MAAM,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC,IAAI,EAAE,YAAY,CAAC;AAC1D,IAAI,MAAM,iBAAiB,GAAG,I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@@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcmjs-org/dicomdir",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.5",
|
|
4
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": "./
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.mjs",
|
|
9
|
+
"require": "./dist/index.cjs"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
6
17
|
"license": "MIT",
|
|
7
18
|
"repository": {
|
|
8
19
|
"type": "git",
|
|
@@ -10,10 +21,14 @@
|
|
|
10
21
|
"directory": "packages/dicomdir"
|
|
11
22
|
},
|
|
12
23
|
"dependencies": {
|
|
13
|
-
"@dcmjs-org/core": "1.0.0-beta.
|
|
24
|
+
"@dcmjs-org/core": "1.0.0-beta.5"
|
|
14
25
|
},
|
|
15
26
|
"devDependencies": {
|
|
16
|
-
"@dcmjs-org/
|
|
17
|
-
"@dcmjs-org/
|
|
18
|
-
}
|
|
27
|
+
"@dcmjs-org/legacy": "1.0.0-beta.5",
|
|
28
|
+
"@dcmjs-org/parser": "1.0.0-beta.5"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "rollup -c ../../rollup.packages.config.mjs"
|
|
32
|
+
},
|
|
33
|
+
"module": "./dist/index.mjs"
|
|
19
34
|
}
|
package/src/directoryOffsets.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
};
|
package/test/dicomdir.test.js
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
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
|
-
});
|