dicom-synth 1.0.0 → 1.1.0
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 +348 -55
- package/bin/dicom-synth-generate.mjs +85 -0
- package/dist/esm/collection/writer.js +386 -0
- package/dist/esm/index.js +498 -177
- package/dist/esm/schema/types.js +0 -0
- package/dist/esm/schema/validate.js +140 -0
- package/dist/esm/syntheticFixtures/generator.js +140 -103
- package/dist/esm/syntheticFixtures/uid.js +40 -0
- package/dist/esm/syntheticFixtures/violations.js +115 -0
- package/dist/types/collection/writer.d.ts +19 -0
- package/dist/types/index.d.ts +5 -5
- package/dist/types/schema/types.d.ts +48 -0
- package/dist/types/schema/validate.d.ts +3 -0
- package/dist/types/syntheticFixtures/generator.d.ts +3 -27
- package/dist/types/syntheticFixtures/uid.d.ts +7 -0
- package/dist/types/syntheticFixtures/violations.d.ts +2 -0
- package/package.json +3 -3
- package/bin/dicom-synth-write.mjs +0 -9
- package/dist/esm/negativeCases/index.js +0 -17
- package/dist/types/negativeCases/index.d.ts +0 -11
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// src/schema/validate.ts
|
|
2
|
+
var TYPE_CAPABILITIES = {
|
|
3
|
+
"valid-ct": { tags: true, violations: true, transferSyntax: true },
|
|
4
|
+
"invalid-uid-ct": { tags: true, violations: true, transferSyntax: true },
|
|
5
|
+
"vendor-warnings-ct": { tags: true, violations: true, transferSyntax: true },
|
|
6
|
+
"large-ct": { tags: true, violations: true, transferSyntax: true },
|
|
7
|
+
"fake-signature": { tags: false, violations: false, transferSyntax: false },
|
|
8
|
+
"non-dicom": { tags: false, violations: false, transferSyntax: false },
|
|
9
|
+
dicomdir: { tags: false, violations: false, transferSyntax: false }
|
|
10
|
+
};
|
|
11
|
+
var VALID_TRANSFER_SYNTAXES = {
|
|
12
|
+
"explicit-vr-little-endian": true,
|
|
13
|
+
"implicit-vr-little-endian": true
|
|
14
|
+
};
|
|
15
|
+
var MAX_PIXEL_BYTES = 512 * 1024 * 1024;
|
|
16
|
+
var VALID_VIOLATIONS = {
|
|
17
|
+
"uid-too-long": true,
|
|
18
|
+
"non-conformant-uid": true,
|
|
19
|
+
"missing-meta-header": true,
|
|
20
|
+
"malformed-sq-delimiter": true,
|
|
21
|
+
"vr-max-length-exceeded": true,
|
|
22
|
+
"missing-type1-tag": true
|
|
23
|
+
};
|
|
24
|
+
var HEX_TAG_RE = /^[0-9a-fA-F]{8}$/;
|
|
25
|
+
var KEYWORD_RE = /^[A-Z][A-Za-z0-9]*$/;
|
|
26
|
+
function validateTagKey(key, path) {
|
|
27
|
+
if (!HEX_TAG_RE.test(key) && !KEYWORD_RE.test(key)) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
`${path}: invalid tag key "${key}" \u2014 must be a DICOM keyword name (e.g. "Modality") or an 8-hex-char tag (e.g. "00080060")`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function validateEntry(entry, path) {
|
|
34
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) {
|
|
35
|
+
throw new Error(`${path}: must be an object`);
|
|
36
|
+
}
|
|
37
|
+
const e = entry;
|
|
38
|
+
if (typeof e.type !== "string" || !Object.hasOwn(TYPE_CAPABILITIES, e.type)) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`${path}.type: must be one of ${Object.keys(TYPE_CAPABILITIES).join(", ")}; got ${JSON.stringify(e.type)}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
const type = e.type;
|
|
44
|
+
const caps = TYPE_CAPABILITIES[type];
|
|
45
|
+
if ("count" in e) {
|
|
46
|
+
if (typeof e.count !== "number" || !Number.isInteger(e.count) || e.count < 1) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`${path}.count: must be a positive integer; got ${JSON.stringify(e.count)}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if ("transferSyntax" in e) {
|
|
53
|
+
if (!caps.transferSyntax) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
`${path}.transferSyntax: not supported for type "${type}"`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
if (!Object.hasOwn(VALID_TRANSFER_SYNTAXES, e.transferSyntax)) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`${path}.transferSyntax: must be one of ${Object.keys(VALID_TRANSFER_SYNTAXES).join(", ")}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!caps.tags && "tags" in e)
|
|
65
|
+
throw new Error(`${path}.tags: not supported for type "${type}"`);
|
|
66
|
+
if (!caps.violations && "violations" in e)
|
|
67
|
+
throw new Error(`${path}.violations: not supported for type "${type}"`);
|
|
68
|
+
if ("tags" in e) {
|
|
69
|
+
if (typeof e.tags !== "object" || e.tags === null || Array.isArray(e.tags)) {
|
|
70
|
+
throw new Error(`${path}.tags: must be an object`);
|
|
71
|
+
}
|
|
72
|
+
for (const key of Object.keys(e.tags)) {
|
|
73
|
+
validateTagKey(key, `${path}.tags`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if ("violations" in e) {
|
|
77
|
+
if (!Array.isArray(e.violations)) {
|
|
78
|
+
throw new Error(`${path}.violations: must be an array`);
|
|
79
|
+
}
|
|
80
|
+
for (const [i, v] of e.violations.entries()) {
|
|
81
|
+
if (typeof v !== "string" || !Object.hasOwn(VALID_VIOLATIONS, v)) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
`${path}.violations[${i}]: must be one of ${Object.keys(VALID_VIOLATIONS).join(", ")}; got ${JSON.stringify(v)}`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (type === "large-ct") {
|
|
89
|
+
if (typeof e.rows !== "number" || !Number.isInteger(e.rows) || e.rows < 1) {
|
|
90
|
+
throw new Error(`${path}.rows: must be a positive integer`);
|
|
91
|
+
}
|
|
92
|
+
if (typeof e.columns !== "number" || !Number.isInteger(e.columns) || e.columns < 1) {
|
|
93
|
+
throw new Error(`${path}.columns: must be a positive integer`);
|
|
94
|
+
}
|
|
95
|
+
if ("frames" in e) {
|
|
96
|
+
if (typeof e.frames !== "number" || !Number.isInteger(e.frames) || e.frames < 1) {
|
|
97
|
+
throw new Error(`${path}.frames: must be a positive integer`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const rows = e.rows;
|
|
101
|
+
const columns = e.columns;
|
|
102
|
+
const frames = typeof e.frames === "number" ? e.frames : 1;
|
|
103
|
+
if (rows * columns * frames * 2 > MAX_PIXEL_BYTES) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`${path}: pixel data (${rows}\xD7${columns}\xD7${frames}\xD72 bytes) exceeds the 512 MB limit`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return e;
|
|
110
|
+
}
|
|
111
|
+
function validateDatasetSpec(raw) {
|
|
112
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
113
|
+
throw new Error("DatasetSpec: must be a JSON object");
|
|
114
|
+
}
|
|
115
|
+
const r = raw;
|
|
116
|
+
if (!Array.isArray(r.entries)) {
|
|
117
|
+
throw new Error("DatasetSpec.entries: must be an array");
|
|
118
|
+
}
|
|
119
|
+
if (r.entries.length === 0) {
|
|
120
|
+
throw new Error("DatasetSpec.entries: must not be empty");
|
|
121
|
+
}
|
|
122
|
+
const entries = r.entries.map(
|
|
123
|
+
(entry, i) => validateEntry(entry, `entries[${i}]`)
|
|
124
|
+
);
|
|
125
|
+
if ("seed" in r) {
|
|
126
|
+
if (typeof r.seed !== "number" || !Number.isFinite(r.seed)) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`DatasetSpec.seed: must be a finite number; got ${JSON.stringify(r.seed)}`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
entries,
|
|
134
|
+
...r.seed !== void 0 ? { seed: r.seed } : {}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export {
|
|
138
|
+
HEX_TAG_RE,
|
|
139
|
+
validateDatasetSpec
|
|
140
|
+
};
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// src/syntheticFixtures/generator.ts
|
|
2
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
|
-
|
|
5
1
|
// src/loadDcmjs.ts
|
|
6
2
|
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
7
3
|
function loadDcmjs() {
|
|
@@ -21,38 +17,85 @@ function loadDcmjs() {
|
|
|
21
17
|
}
|
|
22
18
|
var dcmjs = loadDcmjs();
|
|
23
19
|
|
|
20
|
+
// src/nonStandardDicom/dicomdir.ts
|
|
21
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
22
|
+
import { resolve } from "node:path";
|
|
23
|
+
var DICOMDIR_SOP_CLASS_UID = "1.2.840.10008.1.3.10";
|
|
24
|
+
function buildDicomdirBuffer() {
|
|
25
|
+
const uid = DICOMDIR_SOP_CLASS_UID;
|
|
26
|
+
const buf = Buffer.alloc(256, 0);
|
|
27
|
+
buf.write("DICM", 128, 4, "ascii");
|
|
28
|
+
let off = 132;
|
|
29
|
+
buf.writeUInt16LE(2, off);
|
|
30
|
+
off += 2;
|
|
31
|
+
buf.writeUInt16LE(2, off);
|
|
32
|
+
off += 2;
|
|
33
|
+
buf.write("UI", off, 2, "ascii");
|
|
34
|
+
off += 2;
|
|
35
|
+
buf.writeUInt16LE(uid.length, off);
|
|
36
|
+
off += 2;
|
|
37
|
+
buf.write(uid, off, uid.length, "ascii");
|
|
38
|
+
off += uid.length;
|
|
39
|
+
return buf.subarray(0, off);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/schema/validate.ts
|
|
43
|
+
var MAX_PIXEL_BYTES = 512 * 1024 * 1024;
|
|
44
|
+
var HEX_TAG_RE = /^[0-9a-fA-F]{8}$/;
|
|
45
|
+
|
|
24
46
|
// src/syntheticFixtures/generator.ts
|
|
25
47
|
var CT_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.2";
|
|
26
|
-
var
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"vendor-warnings": 2
|
|
48
|
+
var TRANSFER_SYNTAX_UID = {
|
|
49
|
+
"explicit-vr-little-endian": "1.2.840.10008.1.2.1",
|
|
50
|
+
"implicit-vr-little-endian": "1.2.840.10008.1.2"
|
|
30
51
|
};
|
|
31
|
-
function
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
52
|
+
function buildTagToKeyword() {
|
|
53
|
+
const map = /* @__PURE__ */ new Map();
|
|
54
|
+
const nm = dcmjs.data?.DicomMetaDictionary?.nameMap;
|
|
55
|
+
if (!nm) return map;
|
|
56
|
+
for (const [keyword, info] of Object.entries(nm)) {
|
|
57
|
+
if (info?.tag) {
|
|
58
|
+
const hex = info.tag.replace(/[(,)]/g, "").toUpperCase();
|
|
59
|
+
if (hex.length === 8) map.set(hex, keyword);
|
|
60
|
+
}
|
|
35
61
|
}
|
|
36
|
-
|
|
37
|
-
"1000000000000000000000000000000000001",
|
|
38
|
-
"1000000000000000000000000000000000002",
|
|
39
|
-
"1000000000000000000000000000000000003"
|
|
40
|
-
];
|
|
41
|
-
const roleSuffix = role === "study" ? 1 : role === "series" ? 2 : 3;
|
|
42
|
-
return `2.25.${uidRoots[i]}.${roleSuffix}`;
|
|
62
|
+
return map;
|
|
43
63
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
64
|
+
var tagToKeyword = buildTagToKeyword();
|
|
65
|
+
function applyTagOverrides(dataset, tags) {
|
|
66
|
+
if (!tags) return;
|
|
67
|
+
for (const [key, value] of Object.entries(tags)) {
|
|
68
|
+
if (HEX_TAG_RE.test(key)) {
|
|
69
|
+
const keyword = tagToKeyword.get(key.toUpperCase());
|
|
70
|
+
if (keyword) {
|
|
71
|
+
dataset[keyword] = value;
|
|
72
|
+
} else {
|
|
73
|
+
console.warn(`dicom-synth: unknown hex tag "${key}" \u2014 skipping`);
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
dataset[key] = value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function buildMeta(uid, transferSyntax = "explicit-vr-little-endian") {
|
|
81
|
+
return {
|
|
82
|
+
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
83
|
+
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
84
|
+
MediaStorageSOPInstanceUID: uid.sop,
|
|
85
|
+
TransferSyntaxUID: TRANSFER_SYNTAX_UID[transferSyntax],
|
|
86
|
+
ImplementationClassUID: "1.2.3.4",
|
|
87
|
+
ImplementationVersionName: "SYNTH"
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function buildBaseCtDataset(uid) {
|
|
91
|
+
return {
|
|
49
92
|
PatientName: "SYNTH^SUBJECT",
|
|
50
93
|
PatientID: "SYNTH_PID",
|
|
51
94
|
Modality: "CT",
|
|
52
95
|
SOPClassUID: CT_SOP_CLASS,
|
|
53
|
-
SOPInstanceUID:
|
|
54
|
-
SeriesInstanceUID:
|
|
55
|
-
StudyInstanceUID:
|
|
96
|
+
SOPInstanceUID: uid.sop,
|
|
97
|
+
SeriesInstanceUID: uid.series,
|
|
98
|
+
StudyInstanceUID: uid.study,
|
|
56
99
|
StudyDescription: "Synthetic study",
|
|
57
100
|
SeriesDescription: "Synthetic series",
|
|
58
101
|
Rows: 1,
|
|
@@ -63,93 +106,87 @@ function naturalDataset(variant) {
|
|
|
63
106
|
SamplesPerPixel: 1,
|
|
64
107
|
PhotometricInterpretation: "MONOCHROME2",
|
|
65
108
|
PixelRepresentation: 0,
|
|
66
|
-
PixelData: new Uint8Array([0, 0]).buffer
|
|
109
|
+
PixelData: new Uint8Array([0, 0]).buffer,
|
|
110
|
+
// Declares PixelData's VR explicitly — without it dcmjs logs
|
|
111
|
+
// "No value representation given for PixelData" and falls back to OW anyway.
|
|
112
|
+
_vrMap: { PixelData: "OW" }
|
|
67
113
|
};
|
|
68
|
-
if (variant === "vendor-warnings") {
|
|
69
|
-
dataset.Laterality = "";
|
|
70
|
-
dataset.PatientWeight = "0";
|
|
71
|
-
}
|
|
72
|
-
return dataset;
|
|
73
114
|
}
|
|
74
|
-
function
|
|
75
|
-
const meta = {
|
|
76
|
-
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
77
|
-
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
78
|
-
MediaStorageSOPInstanceUID: uidFor(variant, "sop"),
|
|
79
|
-
TransferSyntaxUID: "1.2.840.10008.1.2.1",
|
|
80
|
-
ImplementationClassUID: "1.2.3.4",
|
|
81
|
-
ImplementationVersionName: "SYNTH"
|
|
82
|
-
};
|
|
115
|
+
function serializeDict(meta, dataset) {
|
|
83
116
|
const dicomDict = new dcmjs.data.DicomDict(
|
|
84
|
-
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
117
|
+
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
118
|
+
meta
|
|
119
|
+
)
|
|
85
120
|
);
|
|
86
121
|
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
87
|
-
|
|
122
|
+
dataset
|
|
88
123
|
);
|
|
89
|
-
return dicomDict;
|
|
124
|
+
return Buffer.from(dicomDict.write({ allowInvalidVRLength: true }));
|
|
90
125
|
}
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
);
|
|
126
|
+
function buildValidCtBuffer(uid, tags, transferSyntax) {
|
|
127
|
+
const dataset = buildBaseCtDataset(uid);
|
|
128
|
+
applyTagOverrides(dataset, tags);
|
|
129
|
+
return serializeDict(buildMeta(uid, transferSyntax), dataset);
|
|
95
130
|
}
|
|
96
|
-
|
|
97
|
-
{
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
filename: "valid-uid-ct-0.dcm",
|
|
104
|
-
variant: "valid-uid",
|
|
105
|
-
description: "Minimal CT with numeric UIDs only (missing-module noise)"
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
filename: "vendor-warnings-ct-0.dcm",
|
|
109
|
-
variant: "vendor-warnings",
|
|
110
|
-
description: "Valid UIDs plus empty Laterality and zero PatientWeight"
|
|
111
|
-
}
|
|
112
|
-
];
|
|
113
|
-
function writeMinimalDicomFile(filePath, tags) {
|
|
114
|
-
const patientId = tags?.patientId ?? "TEST-PATIENT-001";
|
|
115
|
-
const patientName = tags?.patientName ?? "TEST^PATIENT";
|
|
116
|
-
const sopUID = tags?.sopInstanceUid ?? "1.2.3.4.5.6.7.8.9.0.1";
|
|
117
|
-
const dataset = {
|
|
118
|
-
PatientName: patientName,
|
|
119
|
-
PatientID: patientId,
|
|
120
|
-
Modality: "CT",
|
|
121
|
-
SOPClassUID: CT_SOP_CLASS,
|
|
122
|
-
SOPInstanceUID: sopUID,
|
|
123
|
-
SeriesInstanceUID: "1.2.3.4.5.6.7.8",
|
|
124
|
-
StudyInstanceUID: "1.2.3.4.5.6.7",
|
|
125
|
-
SeriesNumber: "1",
|
|
126
|
-
Rows: 1,
|
|
127
|
-
Columns: 1,
|
|
128
|
-
BitsAllocated: 16,
|
|
129
|
-
PixelRepresentation: 0,
|
|
130
|
-
PixelData: new Uint8Array([0, 0]).buffer
|
|
131
|
+
function buildInvalidUidCtBuffer(uid, tags, transferSyntax) {
|
|
132
|
+
const invalidUid = {
|
|
133
|
+
study: `2.25.invalid.study.${uid.study.slice(-6)}`,
|
|
134
|
+
series: `2.25.invalid.series.${uid.series.slice(-6)}`,
|
|
135
|
+
sop: `2.25.invalid.sop.${uid.sop.slice(-6)}`
|
|
131
136
|
};
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
const dataset = buildBaseCtDataset(invalidUid);
|
|
138
|
+
applyTagOverrides(dataset, tags);
|
|
139
|
+
return serializeDict(buildMeta(invalidUid, transferSyntax), dataset);
|
|
140
|
+
}
|
|
141
|
+
function buildVendorCtBuffer(uid, tags, transferSyntax) {
|
|
142
|
+
const dataset = buildBaseCtDataset(uid);
|
|
143
|
+
dataset.Laterality = "";
|
|
144
|
+
dataset.PatientWeight = "0";
|
|
145
|
+
applyTagOverrides(dataset, tags);
|
|
146
|
+
return serializeDict(buildMeta(uid, transferSyntax), dataset);
|
|
147
|
+
}
|
|
148
|
+
function buildLargeCtBuffer(uid, rows, columns, frames, tags, transferSyntax) {
|
|
149
|
+
const dataset = buildBaseCtDataset(uid);
|
|
150
|
+
dataset.Rows = rows;
|
|
151
|
+
dataset.Columns = columns;
|
|
152
|
+
dataset.NumberOfFrames = frames;
|
|
153
|
+
dataset.PixelData = new Uint8Array(rows * columns * frames * 2).buffer;
|
|
154
|
+
applyTagOverrides(dataset, tags);
|
|
155
|
+
return serializeDict(buildMeta(uid, transferSyntax), dataset);
|
|
156
|
+
}
|
|
157
|
+
function buildFakeSignatureBuffer() {
|
|
158
|
+
const buf = Buffer.alloc(200, 0);
|
|
159
|
+
buf.write("XXXX", 128, 4, "ascii");
|
|
160
|
+
return buf;
|
|
161
|
+
}
|
|
162
|
+
function buildNonDicomBuffer(content = "not dicom") {
|
|
163
|
+
return Buffer.from(content, "utf8");
|
|
137
164
|
}
|
|
138
|
-
function
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
165
|
+
function buildBufferForSpec(spec, uid) {
|
|
166
|
+
switch (spec.type) {
|
|
167
|
+
case "valid-ct":
|
|
168
|
+
return buildValidCtBuffer(uid, spec.tags, spec.transferSyntax);
|
|
169
|
+
case "invalid-uid-ct":
|
|
170
|
+
return buildInvalidUidCtBuffer(uid, spec.tags, spec.transferSyntax);
|
|
171
|
+
case "vendor-warnings-ct":
|
|
172
|
+
return buildVendorCtBuffer(uid, spec.tags, spec.transferSyntax);
|
|
173
|
+
case "large-ct":
|
|
174
|
+
return buildLargeCtBuffer(
|
|
175
|
+
uid,
|
|
176
|
+
spec.rows,
|
|
177
|
+
spec.columns,
|
|
178
|
+
spec.frames ?? 1,
|
|
179
|
+
spec.tags,
|
|
180
|
+
spec.transferSyntax
|
|
181
|
+
);
|
|
182
|
+
case "fake-signature":
|
|
183
|
+
return buildFakeSignatureBuffer();
|
|
184
|
+
case "non-dicom":
|
|
185
|
+
return buildNonDicomBuffer(spec.content);
|
|
186
|
+
case "dicomdir":
|
|
187
|
+
return buildDicomdirBuffer();
|
|
146
188
|
}
|
|
147
|
-
return written;
|
|
148
189
|
}
|
|
149
190
|
export {
|
|
150
|
-
|
|
151
|
-
buildDicomDict,
|
|
152
|
-
buildSyntheticCtBuffer,
|
|
153
|
-
writeMinimalDicomFile,
|
|
154
|
-
writeSyntheticFixturesToDir
|
|
191
|
+
buildBufferForSpec
|
|
155
192
|
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/syntheticFixtures/uid.ts
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
var ROLE_CODE = { study: 1, series: 2, sop: 3 };
|
|
4
|
+
function lcg(seed) {
|
|
5
|
+
let s = seed >>> 0;
|
|
6
|
+
return () => {
|
|
7
|
+
s = Math.imul(1664525, s) + 1013904223 >>> 0;
|
|
8
|
+
return s;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function formatUid(a, b, role) {
|
|
12
|
+
return `2.25.${a}.${b}.${ROLE_CODE[role]}`;
|
|
13
|
+
}
|
|
14
|
+
function randomUid(role) {
|
|
15
|
+
const buf = randomBytes(8);
|
|
16
|
+
return formatUid(buf.readUInt32BE(0), buf.readUInt32BE(4), role);
|
|
17
|
+
}
|
|
18
|
+
function seededUid(next, role) {
|
|
19
|
+
return formatUid(next(), next(), role);
|
|
20
|
+
}
|
|
21
|
+
function makeUidGenerator(seed) {
|
|
22
|
+
if (seed === void 0) {
|
|
23
|
+
return () => ({
|
|
24
|
+
study: randomUid("study"),
|
|
25
|
+
series: randomUid("series"),
|
|
26
|
+
sop: randomUid("sop")
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return (fileIndex) => {
|
|
30
|
+
const next = lcg(seed * 999983 + fileIndex * 1000003 >>> 0);
|
|
31
|
+
return {
|
|
32
|
+
study: seededUid(next, "study"),
|
|
33
|
+
series: seededUid(next, "series"),
|
|
34
|
+
sop: seededUid(next, "sop")
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
makeUidGenerator
|
|
40
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// src/loadDcmjs.ts
|
|
2
|
+
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
3
|
+
function loadDcmjs() {
|
|
4
|
+
if (dcmjsNamespace.data?.DicomDict) {
|
|
5
|
+
return dcmjsNamespace;
|
|
6
|
+
}
|
|
7
|
+
const d = dcmjsDefaultImport;
|
|
8
|
+
if (d?.data?.DicomDict) {
|
|
9
|
+
return d;
|
|
10
|
+
}
|
|
11
|
+
if (d?.default?.data?.DicomDict) {
|
|
12
|
+
return d.default;
|
|
13
|
+
}
|
|
14
|
+
throw new Error(
|
|
15
|
+
"dcmjs failed to load (missing data.DicomDict). Install dcmjs >= 0.29."
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
var dcmjs = loadDcmjs();
|
|
19
|
+
|
|
20
|
+
// src/syntheticFixtures/violations.ts
|
|
21
|
+
var dcmjsAny = dcmjs;
|
|
22
|
+
function parseBuffer(buffer) {
|
|
23
|
+
const ab = buffer.buffer.slice(
|
|
24
|
+
buffer.byteOffset,
|
|
25
|
+
buffer.byteOffset + buffer.byteLength
|
|
26
|
+
);
|
|
27
|
+
return dcmjsAny.data.DicomMessage.readFile(ab);
|
|
28
|
+
}
|
|
29
|
+
function reserialize(parsed) {
|
|
30
|
+
return Buffer.from(parsed.write({ allowInvalidVRLength: true }));
|
|
31
|
+
}
|
|
32
|
+
var OVERLONG_UID = `2.25.${"0".repeat(60)}`;
|
|
33
|
+
var VIOLATION_LEVEL = {
|
|
34
|
+
"uid-too-long": "tag",
|
|
35
|
+
"non-conformant-uid": "tag",
|
|
36
|
+
"vr-max-length-exceeded": "tag",
|
|
37
|
+
"missing-type1-tag": "tag",
|
|
38
|
+
"missing-meta-header": "byte",
|
|
39
|
+
"malformed-sq-delimiter": "byte"
|
|
40
|
+
};
|
|
41
|
+
function applyTagLevel(buffer, violations) {
|
|
42
|
+
if (violations.length === 0) return buffer;
|
|
43
|
+
let parsed;
|
|
44
|
+
try {
|
|
45
|
+
parsed = parseBuffer(buffer);
|
|
46
|
+
} catch {
|
|
47
|
+
console.warn(
|
|
48
|
+
"dicom-synth: could not parse buffer for tag-level violations \u2014 skipping"
|
|
49
|
+
);
|
|
50
|
+
return buffer;
|
|
51
|
+
}
|
|
52
|
+
const natural = dcmjsAny.data.DicomMetaDictionary.naturalizeDataset(
|
|
53
|
+
parsed.dict
|
|
54
|
+
);
|
|
55
|
+
for (const v of violations) {
|
|
56
|
+
switch (v) {
|
|
57
|
+
case "uid-too-long":
|
|
58
|
+
break;
|
|
59
|
+
case "non-conformant-uid":
|
|
60
|
+
natural.SOPInstanceUID = "2.25.01.234.567";
|
|
61
|
+
break;
|
|
62
|
+
case "vr-max-length-exceeded":
|
|
63
|
+
natural.StudyDescription = "X".repeat(65);
|
|
64
|
+
break;
|
|
65
|
+
case "missing-type1-tag":
|
|
66
|
+
delete natural.SOPClassUID;
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
throw new Error(
|
|
70
|
+
`dicom-synth: unhandled tag-level violation "${v}"`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
parsed.dict = dcmjsAny.data.DicomMetaDictionary.denaturalizeDataset(natural);
|
|
75
|
+
if (violations.includes("uid-too-long")) {
|
|
76
|
+
;
|
|
77
|
+
parsed.dict["00080018"] = {
|
|
78
|
+
vr: "UI",
|
|
79
|
+
Value: [OVERLONG_UID]
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return reserialize(parsed);
|
|
83
|
+
}
|
|
84
|
+
function applyByteLevel(buffer, violations) {
|
|
85
|
+
let result = buffer;
|
|
86
|
+
for (const v of violations) {
|
|
87
|
+
switch (v) {
|
|
88
|
+
case "missing-meta-header":
|
|
89
|
+
result = result.subarray(132);
|
|
90
|
+
break;
|
|
91
|
+
case "malformed-sq-delimiter": {
|
|
92
|
+
const delimiter = Buffer.alloc(8);
|
|
93
|
+
delimiter.writeUInt16LE(65534, 0);
|
|
94
|
+
delimiter.writeUInt16LE(57565, 2);
|
|
95
|
+
delimiter.writeUInt32LE(4, 4);
|
|
96
|
+
result = Buffer.concat([result, delimiter]);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
default:
|
|
100
|
+
throw new Error(
|
|
101
|
+
`dicom-synth: unhandled byte-level violation "${v}"`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
function applyViolations(buffer, violations) {
|
|
108
|
+
if (violations.length === 0) return buffer;
|
|
109
|
+
const tagViolations = violations.filter((v) => VIOLATION_LEVEL[v] === "tag");
|
|
110
|
+
const byteViolations = violations.filter((v) => VIOLATION_LEVEL[v] === "byte");
|
|
111
|
+
return applyByteLevel(applyTagLevel(buffer, tagViolations), byteViolations);
|
|
112
|
+
}
|
|
113
|
+
export {
|
|
114
|
+
applyViolations
|
|
115
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DatasetSpec, FileSpec } from '../schema/types.js';
|
|
2
|
+
export type GeneratedFile = {
|
|
3
|
+
filename: string;
|
|
4
|
+
buffer: Buffer;
|
|
5
|
+
type: FileSpec['type'];
|
|
6
|
+
index: number;
|
|
7
|
+
};
|
|
8
|
+
export type CollectionManifest = Array<{
|
|
9
|
+
path: string;
|
|
10
|
+
type: FileSpec['type'];
|
|
11
|
+
index: number;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function generateFile(spec: FileSpec, options?: {
|
|
14
|
+
index?: number;
|
|
15
|
+
seed?: number;
|
|
16
|
+
padWidth?: number;
|
|
17
|
+
}): Promise<GeneratedFile>;
|
|
18
|
+
export declare function generateCollectionFromSpec(spec: DatasetSpec): AsyncGenerator<GeneratedFile>;
|
|
19
|
+
export declare function writeCollectionFromSpec(spec: DatasetSpec, outDir: string): Promise<CollectionManifest>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
1
|
+
export { type CollectionManifest, type GeneratedFile, generateCollectionFromSpec, generateFile, writeCollectionFromSpec, } from './collection/writer.js';
|
|
2
|
+
export { defaultPublicCasesPath, loadCaseById, loadCasesFromJson, loadDefaultCases, type PublicCaseRecord, type PublicCaseSource, } from './public-fixtures/catalog.js';
|
|
3
|
+
export { caseCachePath, fetchPublicCaseToCache, verifySha256, } from './public-fixtures/fetch.js';
|
|
4
|
+
export type { DatasetSpec, DicomdirSpec, DicomTagOverrides, FakeSignatureSpec, FileSpec, InvalidUidCtSpec, LargeCtSpec, NonDicomSpec, TransferSyntax, ValidCtSpec, VendorCtSpec, ViolationClass, } from './schema/types.js';
|
|
5
|
+
export { validateDatasetSpec } from './schema/validate.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type TransferSyntax = 'explicit-vr-little-endian' | 'implicit-vr-little-endian';
|
|
2
|
+
export type DicomTagOverrides = {
|
|
3
|
+
PatientID?: string;
|
|
4
|
+
PatientName?: string;
|
|
5
|
+
StudyDescription?: string;
|
|
6
|
+
SeriesDescription?: string;
|
|
7
|
+
[tag: string]: unknown;
|
|
8
|
+
};
|
|
9
|
+
export type ViolationClass = 'uid-too-long' | 'non-conformant-uid' | 'missing-meta-header' | 'malformed-sq-delimiter' | 'vr-max-length-exceeded' | 'missing-type1-tag';
|
|
10
|
+
type BaseCtSpec = {
|
|
11
|
+
tags?: DicomTagOverrides;
|
|
12
|
+
transferSyntax?: TransferSyntax;
|
|
13
|
+
violations?: ViolationClass[];
|
|
14
|
+
};
|
|
15
|
+
export type ValidCtSpec = BaseCtSpec & {
|
|
16
|
+
type: 'valid-ct';
|
|
17
|
+
};
|
|
18
|
+
export type InvalidUidCtSpec = BaseCtSpec & {
|
|
19
|
+
type: 'invalid-uid-ct';
|
|
20
|
+
};
|
|
21
|
+
export type VendorCtSpec = BaseCtSpec & {
|
|
22
|
+
type: 'vendor-warnings-ct';
|
|
23
|
+
};
|
|
24
|
+
export type LargeCtSpec = BaseCtSpec & {
|
|
25
|
+
type: 'large-ct';
|
|
26
|
+
rows: number;
|
|
27
|
+
columns: number;
|
|
28
|
+
frames?: number;
|
|
29
|
+
};
|
|
30
|
+
export type FakeSignatureSpec = {
|
|
31
|
+
type: 'fake-signature';
|
|
32
|
+
};
|
|
33
|
+
export type NonDicomSpec = {
|
|
34
|
+
type: 'non-dicom';
|
|
35
|
+
content?: string;
|
|
36
|
+
};
|
|
37
|
+
export type DicomdirSpec = {
|
|
38
|
+
type: 'dicomdir';
|
|
39
|
+
};
|
|
40
|
+
export type FileSpec = ValidCtSpec | InvalidUidCtSpec | VendorCtSpec | LargeCtSpec | FakeSignatureSpec | NonDicomSpec | DicomdirSpec;
|
|
41
|
+
export type EntrySpec = FileSpec & {
|
|
42
|
+
count?: number;
|
|
43
|
+
};
|
|
44
|
+
export type DatasetSpec = {
|
|
45
|
+
entries: EntrySpec[];
|
|
46
|
+
seed?: number;
|
|
47
|
+
};
|
|
48
|
+
export {};
|