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
package/dist/esm/index.js
CHANGED
|
@@ -1,20 +1,30 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import { mkdirSync
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
// src/collection/writer.ts
|
|
2
|
+
import { mkdirSync as mkdirSync2 } from "node:fs";
|
|
3
|
+
import { writeFile } from "node:fs/promises";
|
|
4
|
+
import { resolve as resolve2 } from "node:path";
|
|
5
|
+
|
|
6
|
+
// src/loadDcmjs.ts
|
|
7
|
+
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
8
|
+
function loadDcmjs() {
|
|
9
|
+
if (dcmjsNamespace.data?.DicomDict) {
|
|
10
|
+
return dcmjsNamespace;
|
|
11
|
+
}
|
|
12
|
+
const d = dcmjsDefaultImport;
|
|
13
|
+
if (d?.data?.DicomDict) {
|
|
14
|
+
return d;
|
|
15
|
+
}
|
|
16
|
+
if (d?.default?.data?.DicomDict) {
|
|
17
|
+
return d.default;
|
|
18
|
+
}
|
|
19
|
+
throw new Error(
|
|
20
|
+
"dcmjs failed to load (missing data.DicomDict). Install dcmjs >= 0.29."
|
|
21
|
+
);
|
|
13
22
|
}
|
|
23
|
+
var dcmjs = loadDcmjs();
|
|
14
24
|
|
|
15
25
|
// src/nonStandardDicom/dicomdir.ts
|
|
16
|
-
import { mkdirSync
|
|
17
|
-
import { resolve
|
|
26
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
27
|
+
import { resolve } from "node:path";
|
|
18
28
|
var DICOMDIR_SOP_CLASS_UID = "1.2.840.10008.1.3.10";
|
|
19
29
|
function buildDicomdirBuffer() {
|
|
20
30
|
const uid = DICOMDIR_SOP_CLASS_UID;
|
|
@@ -33,9 +43,474 @@ function buildDicomdirBuffer() {
|
|
|
33
43
|
off += uid.length;
|
|
34
44
|
return buf.subarray(0, off);
|
|
35
45
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
|
|
47
|
+
// src/schema/validate.ts
|
|
48
|
+
var TYPE_CAPABILITIES = {
|
|
49
|
+
"valid-ct": { tags: true, violations: true, transferSyntax: true },
|
|
50
|
+
"invalid-uid-ct": { tags: true, violations: true, transferSyntax: true },
|
|
51
|
+
"vendor-warnings-ct": { tags: true, violations: true, transferSyntax: true },
|
|
52
|
+
"large-ct": { tags: true, violations: true, transferSyntax: true },
|
|
53
|
+
"fake-signature": { tags: false, violations: false, transferSyntax: false },
|
|
54
|
+
"non-dicom": { tags: false, violations: false, transferSyntax: false },
|
|
55
|
+
dicomdir: { tags: false, violations: false, transferSyntax: false }
|
|
56
|
+
};
|
|
57
|
+
var VALID_TRANSFER_SYNTAXES = {
|
|
58
|
+
"explicit-vr-little-endian": true,
|
|
59
|
+
"implicit-vr-little-endian": true
|
|
60
|
+
};
|
|
61
|
+
var MAX_PIXEL_BYTES = 512 * 1024 * 1024;
|
|
62
|
+
var VALID_VIOLATIONS = {
|
|
63
|
+
"uid-too-long": true,
|
|
64
|
+
"non-conformant-uid": true,
|
|
65
|
+
"missing-meta-header": true,
|
|
66
|
+
"malformed-sq-delimiter": true,
|
|
67
|
+
"vr-max-length-exceeded": true,
|
|
68
|
+
"missing-type1-tag": true
|
|
69
|
+
};
|
|
70
|
+
var HEX_TAG_RE = /^[0-9a-fA-F]{8}$/;
|
|
71
|
+
var KEYWORD_RE = /^[A-Z][A-Za-z0-9]*$/;
|
|
72
|
+
function validateTagKey(key, path) {
|
|
73
|
+
if (!HEX_TAG_RE.test(key) && !KEYWORD_RE.test(key)) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`${path}: invalid tag key "${key}" \u2014 must be a DICOM keyword name (e.g. "Modality") or an 8-hex-char tag (e.g. "00080060")`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function validateEntry(entry, path) {
|
|
80
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) {
|
|
81
|
+
throw new Error(`${path}: must be an object`);
|
|
82
|
+
}
|
|
83
|
+
const e = entry;
|
|
84
|
+
if (typeof e.type !== "string" || !Object.hasOwn(TYPE_CAPABILITIES, e.type)) {
|
|
85
|
+
throw new Error(
|
|
86
|
+
`${path}.type: must be one of ${Object.keys(TYPE_CAPABILITIES).join(", ")}; got ${JSON.stringify(e.type)}`
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const type = e.type;
|
|
90
|
+
const caps = TYPE_CAPABILITIES[type];
|
|
91
|
+
if ("count" in e) {
|
|
92
|
+
if (typeof e.count !== "number" || !Number.isInteger(e.count) || e.count < 1) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`${path}.count: must be a positive integer; got ${JSON.stringify(e.count)}`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if ("transferSyntax" in e) {
|
|
99
|
+
if (!caps.transferSyntax) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`${path}.transferSyntax: not supported for type "${type}"`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
if (!Object.hasOwn(VALID_TRANSFER_SYNTAXES, e.transferSyntax)) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`${path}.transferSyntax: must be one of ${Object.keys(VALID_TRANSFER_SYNTAXES).join(", ")}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (!caps.tags && "tags" in e)
|
|
111
|
+
throw new Error(`${path}.tags: not supported for type "${type}"`);
|
|
112
|
+
if (!caps.violations && "violations" in e)
|
|
113
|
+
throw new Error(`${path}.violations: not supported for type "${type}"`);
|
|
114
|
+
if ("tags" in e) {
|
|
115
|
+
if (typeof e.tags !== "object" || e.tags === null || Array.isArray(e.tags)) {
|
|
116
|
+
throw new Error(`${path}.tags: must be an object`);
|
|
117
|
+
}
|
|
118
|
+
for (const key of Object.keys(e.tags)) {
|
|
119
|
+
validateTagKey(key, `${path}.tags`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if ("violations" in e) {
|
|
123
|
+
if (!Array.isArray(e.violations)) {
|
|
124
|
+
throw new Error(`${path}.violations: must be an array`);
|
|
125
|
+
}
|
|
126
|
+
for (const [i, v] of e.violations.entries()) {
|
|
127
|
+
if (typeof v !== "string" || !Object.hasOwn(VALID_VIOLATIONS, v)) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`${path}.violations[${i}]: must be one of ${Object.keys(VALID_VIOLATIONS).join(", ")}; got ${JSON.stringify(v)}`
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (type === "large-ct") {
|
|
135
|
+
if (typeof e.rows !== "number" || !Number.isInteger(e.rows) || e.rows < 1) {
|
|
136
|
+
throw new Error(`${path}.rows: must be a positive integer`);
|
|
137
|
+
}
|
|
138
|
+
if (typeof e.columns !== "number" || !Number.isInteger(e.columns) || e.columns < 1) {
|
|
139
|
+
throw new Error(`${path}.columns: must be a positive integer`);
|
|
140
|
+
}
|
|
141
|
+
if ("frames" in e) {
|
|
142
|
+
if (typeof e.frames !== "number" || !Number.isInteger(e.frames) || e.frames < 1) {
|
|
143
|
+
throw new Error(`${path}.frames: must be a positive integer`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const rows = e.rows;
|
|
147
|
+
const columns = e.columns;
|
|
148
|
+
const frames = typeof e.frames === "number" ? e.frames : 1;
|
|
149
|
+
if (rows * columns * frames * 2 > MAX_PIXEL_BYTES) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`${path}: pixel data (${rows}\xD7${columns}\xD7${frames}\xD72 bytes) exceeds the 512 MB limit`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return e;
|
|
156
|
+
}
|
|
157
|
+
function validateDatasetSpec(raw) {
|
|
158
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
159
|
+
throw new Error("DatasetSpec: must be a JSON object");
|
|
160
|
+
}
|
|
161
|
+
const r = raw;
|
|
162
|
+
if (!Array.isArray(r.entries)) {
|
|
163
|
+
throw new Error("DatasetSpec.entries: must be an array");
|
|
164
|
+
}
|
|
165
|
+
if (r.entries.length === 0) {
|
|
166
|
+
throw new Error("DatasetSpec.entries: must not be empty");
|
|
167
|
+
}
|
|
168
|
+
const entries = r.entries.map(
|
|
169
|
+
(entry, i) => validateEntry(entry, `entries[${i}]`)
|
|
170
|
+
);
|
|
171
|
+
if ("seed" in r) {
|
|
172
|
+
if (typeof r.seed !== "number" || !Number.isFinite(r.seed)) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`DatasetSpec.seed: must be a finite number; got ${JSON.stringify(r.seed)}`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
entries,
|
|
180
|
+
...r.seed !== void 0 ? { seed: r.seed } : {}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/syntheticFixtures/generator.ts
|
|
185
|
+
var CT_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.2";
|
|
186
|
+
var TRANSFER_SYNTAX_UID = {
|
|
187
|
+
"explicit-vr-little-endian": "1.2.840.10008.1.2.1",
|
|
188
|
+
"implicit-vr-little-endian": "1.2.840.10008.1.2"
|
|
189
|
+
};
|
|
190
|
+
function buildTagToKeyword() {
|
|
191
|
+
const map = /* @__PURE__ */ new Map();
|
|
192
|
+
const nm = dcmjs.data?.DicomMetaDictionary?.nameMap;
|
|
193
|
+
if (!nm) return map;
|
|
194
|
+
for (const [keyword, info] of Object.entries(nm)) {
|
|
195
|
+
if (info?.tag) {
|
|
196
|
+
const hex = info.tag.replace(/[(,)]/g, "").toUpperCase();
|
|
197
|
+
if (hex.length === 8) map.set(hex, keyword);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return map;
|
|
201
|
+
}
|
|
202
|
+
var tagToKeyword = buildTagToKeyword();
|
|
203
|
+
function applyTagOverrides(dataset, tags) {
|
|
204
|
+
if (!tags) return;
|
|
205
|
+
for (const [key, value] of Object.entries(tags)) {
|
|
206
|
+
if (HEX_TAG_RE.test(key)) {
|
|
207
|
+
const keyword = tagToKeyword.get(key.toUpperCase());
|
|
208
|
+
if (keyword) {
|
|
209
|
+
dataset[keyword] = value;
|
|
210
|
+
} else {
|
|
211
|
+
console.warn(`dicom-synth: unknown hex tag "${key}" \u2014 skipping`);
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
dataset[key] = value;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function buildMeta(uid, transferSyntax = "explicit-vr-little-endian") {
|
|
219
|
+
return {
|
|
220
|
+
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
221
|
+
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
222
|
+
MediaStorageSOPInstanceUID: uid.sop,
|
|
223
|
+
TransferSyntaxUID: TRANSFER_SYNTAX_UID[transferSyntax],
|
|
224
|
+
ImplementationClassUID: "1.2.3.4",
|
|
225
|
+
ImplementationVersionName: "SYNTH"
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function buildBaseCtDataset(uid) {
|
|
229
|
+
return {
|
|
230
|
+
PatientName: "SYNTH^SUBJECT",
|
|
231
|
+
PatientID: "SYNTH_PID",
|
|
232
|
+
Modality: "CT",
|
|
233
|
+
SOPClassUID: CT_SOP_CLASS,
|
|
234
|
+
SOPInstanceUID: uid.sop,
|
|
235
|
+
SeriesInstanceUID: uid.series,
|
|
236
|
+
StudyInstanceUID: uid.study,
|
|
237
|
+
StudyDescription: "Synthetic study",
|
|
238
|
+
SeriesDescription: "Synthetic series",
|
|
239
|
+
Rows: 1,
|
|
240
|
+
Columns: 1,
|
|
241
|
+
BitsAllocated: 16,
|
|
242
|
+
BitsStored: 16,
|
|
243
|
+
HighBit: 15,
|
|
244
|
+
SamplesPerPixel: 1,
|
|
245
|
+
PhotometricInterpretation: "MONOCHROME2",
|
|
246
|
+
PixelRepresentation: 0,
|
|
247
|
+
PixelData: new Uint8Array([0, 0]).buffer,
|
|
248
|
+
// Declares PixelData's VR explicitly — without it dcmjs logs
|
|
249
|
+
// "No value representation given for PixelData" and falls back to OW anyway.
|
|
250
|
+
_vrMap: { PixelData: "OW" }
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
function serializeDict(meta, dataset) {
|
|
254
|
+
const dicomDict = new dcmjs.data.DicomDict(
|
|
255
|
+
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
256
|
+
meta
|
|
257
|
+
)
|
|
258
|
+
);
|
|
259
|
+
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
260
|
+
dataset
|
|
261
|
+
);
|
|
262
|
+
return Buffer.from(dicomDict.write({ allowInvalidVRLength: true }));
|
|
263
|
+
}
|
|
264
|
+
function buildValidCtBuffer(uid, tags, transferSyntax) {
|
|
265
|
+
const dataset = buildBaseCtDataset(uid);
|
|
266
|
+
applyTagOverrides(dataset, tags);
|
|
267
|
+
return serializeDict(buildMeta(uid, transferSyntax), dataset);
|
|
268
|
+
}
|
|
269
|
+
function buildInvalidUidCtBuffer(uid, tags, transferSyntax) {
|
|
270
|
+
const invalidUid = {
|
|
271
|
+
study: `2.25.invalid.study.${uid.study.slice(-6)}`,
|
|
272
|
+
series: `2.25.invalid.series.${uid.series.slice(-6)}`,
|
|
273
|
+
sop: `2.25.invalid.sop.${uid.sop.slice(-6)}`
|
|
274
|
+
};
|
|
275
|
+
const dataset = buildBaseCtDataset(invalidUid);
|
|
276
|
+
applyTagOverrides(dataset, tags);
|
|
277
|
+
return serializeDict(buildMeta(invalidUid, transferSyntax), dataset);
|
|
278
|
+
}
|
|
279
|
+
function buildVendorCtBuffer(uid, tags, transferSyntax) {
|
|
280
|
+
const dataset = buildBaseCtDataset(uid);
|
|
281
|
+
dataset.Laterality = "";
|
|
282
|
+
dataset.PatientWeight = "0";
|
|
283
|
+
applyTagOverrides(dataset, tags);
|
|
284
|
+
return serializeDict(buildMeta(uid, transferSyntax), dataset);
|
|
285
|
+
}
|
|
286
|
+
function buildLargeCtBuffer(uid, rows, columns, frames, tags, transferSyntax) {
|
|
287
|
+
const dataset = buildBaseCtDataset(uid);
|
|
288
|
+
dataset.Rows = rows;
|
|
289
|
+
dataset.Columns = columns;
|
|
290
|
+
dataset.NumberOfFrames = frames;
|
|
291
|
+
dataset.PixelData = new Uint8Array(rows * columns * frames * 2).buffer;
|
|
292
|
+
applyTagOverrides(dataset, tags);
|
|
293
|
+
return serializeDict(buildMeta(uid, transferSyntax), dataset);
|
|
294
|
+
}
|
|
295
|
+
function buildFakeSignatureBuffer() {
|
|
296
|
+
const buf = Buffer.alloc(200, 0);
|
|
297
|
+
buf.write("XXXX", 128, 4, "ascii");
|
|
298
|
+
return buf;
|
|
299
|
+
}
|
|
300
|
+
function buildNonDicomBuffer(content = "not dicom") {
|
|
301
|
+
return Buffer.from(content, "utf8");
|
|
302
|
+
}
|
|
303
|
+
function buildBufferForSpec(spec, uid) {
|
|
304
|
+
switch (spec.type) {
|
|
305
|
+
case "valid-ct":
|
|
306
|
+
return buildValidCtBuffer(uid, spec.tags, spec.transferSyntax);
|
|
307
|
+
case "invalid-uid-ct":
|
|
308
|
+
return buildInvalidUidCtBuffer(uid, spec.tags, spec.transferSyntax);
|
|
309
|
+
case "vendor-warnings-ct":
|
|
310
|
+
return buildVendorCtBuffer(uid, spec.tags, spec.transferSyntax);
|
|
311
|
+
case "large-ct":
|
|
312
|
+
return buildLargeCtBuffer(
|
|
313
|
+
uid,
|
|
314
|
+
spec.rows,
|
|
315
|
+
spec.columns,
|
|
316
|
+
spec.frames ?? 1,
|
|
317
|
+
spec.tags,
|
|
318
|
+
spec.transferSyntax
|
|
319
|
+
);
|
|
320
|
+
case "fake-signature":
|
|
321
|
+
return buildFakeSignatureBuffer();
|
|
322
|
+
case "non-dicom":
|
|
323
|
+
return buildNonDicomBuffer(spec.content);
|
|
324
|
+
case "dicomdir":
|
|
325
|
+
return buildDicomdirBuffer();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// src/syntheticFixtures/uid.ts
|
|
330
|
+
import { randomBytes } from "node:crypto";
|
|
331
|
+
var ROLE_CODE = { study: 1, series: 2, sop: 3 };
|
|
332
|
+
function lcg(seed) {
|
|
333
|
+
let s = seed >>> 0;
|
|
334
|
+
return () => {
|
|
335
|
+
s = Math.imul(1664525, s) + 1013904223 >>> 0;
|
|
336
|
+
return s;
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function formatUid(a, b, role) {
|
|
340
|
+
return `2.25.${a}.${b}.${ROLE_CODE[role]}`;
|
|
341
|
+
}
|
|
342
|
+
function randomUid(role) {
|
|
343
|
+
const buf = randomBytes(8);
|
|
344
|
+
return formatUid(buf.readUInt32BE(0), buf.readUInt32BE(4), role);
|
|
345
|
+
}
|
|
346
|
+
function seededUid(next, role) {
|
|
347
|
+
return formatUid(next(), next(), role);
|
|
348
|
+
}
|
|
349
|
+
function makeUidGenerator(seed) {
|
|
350
|
+
if (seed === void 0) {
|
|
351
|
+
return () => ({
|
|
352
|
+
study: randomUid("study"),
|
|
353
|
+
series: randomUid("series"),
|
|
354
|
+
sop: randomUid("sop")
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
return (fileIndex) => {
|
|
358
|
+
const next = lcg(seed * 999983 + fileIndex * 1000003 >>> 0);
|
|
359
|
+
return {
|
|
360
|
+
study: seededUid(next, "study"),
|
|
361
|
+
series: seededUid(next, "series"),
|
|
362
|
+
sop: seededUid(next, "sop")
|
|
363
|
+
};
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/syntheticFixtures/violations.ts
|
|
368
|
+
var dcmjsAny = dcmjs;
|
|
369
|
+
function parseBuffer(buffer) {
|
|
370
|
+
const ab = buffer.buffer.slice(
|
|
371
|
+
buffer.byteOffset,
|
|
372
|
+
buffer.byteOffset + buffer.byteLength
|
|
373
|
+
);
|
|
374
|
+
return dcmjsAny.data.DicomMessage.readFile(ab);
|
|
375
|
+
}
|
|
376
|
+
function reserialize(parsed) {
|
|
377
|
+
return Buffer.from(parsed.write({ allowInvalidVRLength: true }));
|
|
378
|
+
}
|
|
379
|
+
var OVERLONG_UID = `2.25.${"0".repeat(60)}`;
|
|
380
|
+
var VIOLATION_LEVEL = {
|
|
381
|
+
"uid-too-long": "tag",
|
|
382
|
+
"non-conformant-uid": "tag",
|
|
383
|
+
"vr-max-length-exceeded": "tag",
|
|
384
|
+
"missing-type1-tag": "tag",
|
|
385
|
+
"missing-meta-header": "byte",
|
|
386
|
+
"malformed-sq-delimiter": "byte"
|
|
387
|
+
};
|
|
388
|
+
function applyTagLevel(buffer, violations) {
|
|
389
|
+
if (violations.length === 0) return buffer;
|
|
390
|
+
let parsed;
|
|
391
|
+
try {
|
|
392
|
+
parsed = parseBuffer(buffer);
|
|
393
|
+
} catch {
|
|
394
|
+
console.warn(
|
|
395
|
+
"dicom-synth: could not parse buffer for tag-level violations \u2014 skipping"
|
|
396
|
+
);
|
|
397
|
+
return buffer;
|
|
398
|
+
}
|
|
399
|
+
const natural = dcmjsAny.data.DicomMetaDictionary.naturalizeDataset(
|
|
400
|
+
parsed.dict
|
|
401
|
+
);
|
|
402
|
+
for (const v of violations) {
|
|
403
|
+
switch (v) {
|
|
404
|
+
case "uid-too-long":
|
|
405
|
+
break;
|
|
406
|
+
case "non-conformant-uid":
|
|
407
|
+
natural.SOPInstanceUID = "2.25.01.234.567";
|
|
408
|
+
break;
|
|
409
|
+
case "vr-max-length-exceeded":
|
|
410
|
+
natural.StudyDescription = "X".repeat(65);
|
|
411
|
+
break;
|
|
412
|
+
case "missing-type1-tag":
|
|
413
|
+
delete natural.SOPClassUID;
|
|
414
|
+
break;
|
|
415
|
+
default:
|
|
416
|
+
throw new Error(
|
|
417
|
+
`dicom-synth: unhandled tag-level violation "${v}"`
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
parsed.dict = dcmjsAny.data.DicomMetaDictionary.denaturalizeDataset(natural);
|
|
422
|
+
if (violations.includes("uid-too-long")) {
|
|
423
|
+
;
|
|
424
|
+
parsed.dict["00080018"] = {
|
|
425
|
+
vr: "UI",
|
|
426
|
+
Value: [OVERLONG_UID]
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return reserialize(parsed);
|
|
430
|
+
}
|
|
431
|
+
function applyByteLevel(buffer, violations) {
|
|
432
|
+
let result = buffer;
|
|
433
|
+
for (const v of violations) {
|
|
434
|
+
switch (v) {
|
|
435
|
+
case "missing-meta-header":
|
|
436
|
+
result = result.subarray(132);
|
|
437
|
+
break;
|
|
438
|
+
case "malformed-sq-delimiter": {
|
|
439
|
+
const delimiter = Buffer.alloc(8);
|
|
440
|
+
delimiter.writeUInt16LE(65534, 0);
|
|
441
|
+
delimiter.writeUInt16LE(57565, 2);
|
|
442
|
+
delimiter.writeUInt32LE(4, 4);
|
|
443
|
+
result = Buffer.concat([result, delimiter]);
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
default:
|
|
447
|
+
throw new Error(
|
|
448
|
+
`dicom-synth: unhandled byte-level violation "${v}"`
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
function applyViolations(buffer, violations) {
|
|
455
|
+
if (violations.length === 0) return buffer;
|
|
456
|
+
const tagViolations = violations.filter((v) => VIOLATION_LEVEL[v] === "tag");
|
|
457
|
+
const byteViolations = violations.filter((v) => VIOLATION_LEVEL[v] === "byte");
|
|
458
|
+
return applyByteLevel(applyTagLevel(buffer, tagViolations), byteViolations);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// src/collection/writer.ts
|
|
462
|
+
var NO_EXTENSION_TYPES = /* @__PURE__ */ new Set([
|
|
463
|
+
"fake-signature",
|
|
464
|
+
"non-dicom"
|
|
465
|
+
]);
|
|
466
|
+
function filename(type, index, padWidth) {
|
|
467
|
+
const padded = String(index).padStart(padWidth, "0");
|
|
468
|
+
const base = `${type}-${padded}`;
|
|
469
|
+
return NO_EXTENSION_TYPES.has(type) ? base : `${base}.dcm`;
|
|
470
|
+
}
|
|
471
|
+
async function generateFile(spec, options) {
|
|
472
|
+
const index = options?.index ?? 0;
|
|
473
|
+
const padWidth = options?.padWidth ?? 3;
|
|
474
|
+
const uidGen = makeUidGenerator(options?.seed);
|
|
475
|
+
const uid = uidGen(index);
|
|
476
|
+
let buffer = buildBufferForSpec(spec, uid);
|
|
477
|
+
const violations = "violations" in spec ? spec.violations : void 0;
|
|
478
|
+
if (violations?.length) {
|
|
479
|
+
buffer = applyViolations(buffer, violations);
|
|
480
|
+
}
|
|
481
|
+
return {
|
|
482
|
+
filename: filename(spec.type, index, padWidth),
|
|
483
|
+
buffer,
|
|
484
|
+
type: spec.type,
|
|
485
|
+
index
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
async function* generateCollectionFromSpec(spec) {
|
|
489
|
+
const totalFiles = spec.entries.reduce((sum, e) => sum + (e.count ?? 1), 0);
|
|
490
|
+
const padWidth = String(totalFiles).length;
|
|
491
|
+
let globalIndex = 0;
|
|
492
|
+
for (const entry of spec.entries) {
|
|
493
|
+
const { count = 1, ...fileSpec } = entry;
|
|
494
|
+
for (let i = 0; i < count; i++) {
|
|
495
|
+
yield generateFile(fileSpec, {
|
|
496
|
+
index: globalIndex,
|
|
497
|
+
seed: spec.seed,
|
|
498
|
+
padWidth
|
|
499
|
+
});
|
|
500
|
+
globalIndex++;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
async function writeCollectionFromSpec(spec, outDir) {
|
|
505
|
+
const root = resolve2(outDir);
|
|
506
|
+
mkdirSync2(root, { recursive: true });
|
|
507
|
+
const manifest = [];
|
|
508
|
+
for await (const file of generateCollectionFromSpec(spec)) {
|
|
509
|
+
const filePath = resolve2(root, file.filename);
|
|
510
|
+
await writeFile(filePath, file.buffer);
|
|
511
|
+
manifest.push({ path: filePath, type: file.type, index: file.index });
|
|
512
|
+
}
|
|
513
|
+
return manifest;
|
|
39
514
|
}
|
|
40
515
|
|
|
41
516
|
// src/public-fixtures/catalog.ts
|
|
@@ -71,7 +546,7 @@ function loadCaseById(casesJsonPath, id) {
|
|
|
71
546
|
|
|
72
547
|
// src/public-fixtures/fetch.ts
|
|
73
548
|
import { createHash } from "node:crypto";
|
|
74
|
-
import { existsSync, mkdirSync as mkdirSync3, readFileSync as readFileSync2, writeFileSync as
|
|
549
|
+
import { existsSync, mkdirSync as mkdirSync3, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
75
550
|
import { homedir } from "node:os";
|
|
76
551
|
import { join as join2 } from "node:path";
|
|
77
552
|
var DEFAULT_CACHE_ROOT = join2(homedir(), ".cache", "dicom-synth-testcases");
|
|
@@ -107,173 +582,19 @@ async function fetchPublicCaseToCache(record, cacheRoot = DEFAULT_CACHE_ROOT) {
|
|
|
107
582
|
}
|
|
108
583
|
const buf = Buffer.from(await res.arrayBuffer());
|
|
109
584
|
verifySha256(buf, record.sha256);
|
|
110
|
-
|
|
585
|
+
writeFileSync2(dest, buf);
|
|
111
586
|
return dest;
|
|
112
587
|
}
|
|
113
|
-
|
|
114
|
-
// src/syntheticFixtures/generator.ts
|
|
115
|
-
import { mkdirSync as mkdirSync4, writeFileSync as writeFileSync4 } from "node:fs";
|
|
116
|
-
import { resolve as resolve3 } from "node:path";
|
|
117
|
-
|
|
118
|
-
// src/loadDcmjs.ts
|
|
119
|
-
import dcmjsDefaultImport, * as dcmjsNamespace from "dcmjs";
|
|
120
|
-
function loadDcmjs() {
|
|
121
|
-
if (dcmjsNamespace.data?.DicomDict) {
|
|
122
|
-
return dcmjsNamespace;
|
|
123
|
-
}
|
|
124
|
-
const d = dcmjsDefaultImport;
|
|
125
|
-
if (d?.data?.DicomDict) {
|
|
126
|
-
return d;
|
|
127
|
-
}
|
|
128
|
-
if (d?.default?.data?.DicomDict) {
|
|
129
|
-
return d.default;
|
|
130
|
-
}
|
|
131
|
-
throw new Error(
|
|
132
|
-
"dcmjs failed to load (missing data.DicomDict). Install dcmjs >= 0.29."
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
var dcmjs = loadDcmjs();
|
|
136
|
-
|
|
137
|
-
// src/syntheticFixtures/generator.ts
|
|
138
|
-
var CT_SOP_CLASS = "1.2.840.10008.5.1.4.1.1.2";
|
|
139
|
-
var VARIANT_INDEX = {
|
|
140
|
-
"minimal-invalid-uid": 0,
|
|
141
|
-
"valid-uid": 1,
|
|
142
|
-
"vendor-warnings": 2
|
|
143
|
-
};
|
|
144
|
-
function uidFor(variant, role) {
|
|
145
|
-
const i = VARIANT_INDEX[variant];
|
|
146
|
-
if (variant === "minimal-invalid-uid") {
|
|
147
|
-
return `2.25.100000000000000000000000000000.${role}.${i}`;
|
|
148
|
-
}
|
|
149
|
-
const uidRoots = [
|
|
150
|
-
"1000000000000000000000000000000000001",
|
|
151
|
-
"1000000000000000000000000000000000002",
|
|
152
|
-
"1000000000000000000000000000000000003"
|
|
153
|
-
];
|
|
154
|
-
const roleSuffix = role === "study" ? 1 : role === "series" ? 2 : 3;
|
|
155
|
-
return `2.25.${uidRoots[i]}.${roleSuffix}`;
|
|
156
|
-
}
|
|
157
|
-
function naturalDataset(variant) {
|
|
158
|
-
const studyUID = uidFor(variant, "study");
|
|
159
|
-
const seriesUID = uidFor(variant, "series");
|
|
160
|
-
const sopUID = uidFor(variant, "sop");
|
|
161
|
-
const dataset = {
|
|
162
|
-
PatientName: "SYNTH^SUBJECT",
|
|
163
|
-
PatientID: "SYNTH_PID",
|
|
164
|
-
Modality: "CT",
|
|
165
|
-
SOPClassUID: CT_SOP_CLASS,
|
|
166
|
-
SOPInstanceUID: sopUID,
|
|
167
|
-
SeriesInstanceUID: seriesUID,
|
|
168
|
-
StudyInstanceUID: studyUID,
|
|
169
|
-
StudyDescription: "Synthetic study",
|
|
170
|
-
SeriesDescription: "Synthetic series",
|
|
171
|
-
Rows: 1,
|
|
172
|
-
Columns: 1,
|
|
173
|
-
BitsAllocated: 16,
|
|
174
|
-
BitsStored: 16,
|
|
175
|
-
HighBit: 15,
|
|
176
|
-
SamplesPerPixel: 1,
|
|
177
|
-
PhotometricInterpretation: "MONOCHROME2",
|
|
178
|
-
PixelRepresentation: 0,
|
|
179
|
-
PixelData: new Uint8Array([0, 0]).buffer
|
|
180
|
-
};
|
|
181
|
-
if (variant === "vendor-warnings") {
|
|
182
|
-
dataset.Laterality = "";
|
|
183
|
-
dataset.PatientWeight = "0";
|
|
184
|
-
}
|
|
185
|
-
return dataset;
|
|
186
|
-
}
|
|
187
|
-
function buildDicomDict(variant) {
|
|
188
|
-
const meta = {
|
|
189
|
-
FileMetaInformationVersion: new Uint8Array([0, 1]).buffer,
|
|
190
|
-
MediaStorageSOPClassUID: CT_SOP_CLASS,
|
|
191
|
-
MediaStorageSOPInstanceUID: uidFor(variant, "sop"),
|
|
192
|
-
TransferSyntaxUID: "1.2.840.10008.1.2.1",
|
|
193
|
-
ImplementationClassUID: "1.2.3.4",
|
|
194
|
-
ImplementationVersionName: "SYNTH"
|
|
195
|
-
};
|
|
196
|
-
const dicomDict = new dcmjs.data.DicomDict(
|
|
197
|
-
dcmjs.data.DicomMetaDictionary.denaturalizeDataset(meta)
|
|
198
|
-
);
|
|
199
|
-
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(
|
|
200
|
-
naturalDataset(variant)
|
|
201
|
-
);
|
|
202
|
-
return dicomDict;
|
|
203
|
-
}
|
|
204
|
-
function buildSyntheticCtBuffer(variant) {
|
|
205
|
-
return Buffer.from(
|
|
206
|
-
buildDicomDict(variant).write({ allowInvalidVRLength: true })
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
var SYNTHETIC_FIXTURES = [
|
|
210
|
-
{
|
|
211
|
-
filename: "minimal-ct-0.dcm",
|
|
212
|
-
variant: "minimal-invalid-uid",
|
|
213
|
-
description: "Minimal CT with text in UIDs (many dciodvfy errors)"
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
filename: "valid-uid-ct-0.dcm",
|
|
217
|
-
variant: "valid-uid",
|
|
218
|
-
description: "Minimal CT with numeric UIDs only (missing-module noise)"
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
filename: "vendor-warnings-ct-0.dcm",
|
|
222
|
-
variant: "vendor-warnings",
|
|
223
|
-
description: "Valid UIDs plus empty Laterality and zero PatientWeight"
|
|
224
|
-
}
|
|
225
|
-
];
|
|
226
|
-
function writeMinimalDicomFile(filePath, tags) {
|
|
227
|
-
const patientId = tags?.patientId ?? "TEST-PATIENT-001";
|
|
228
|
-
const patientName = tags?.patientName ?? "TEST^PATIENT";
|
|
229
|
-
const sopUID = tags?.sopInstanceUid ?? "1.2.3.4.5.6.7.8.9.0.1";
|
|
230
|
-
const dataset = {
|
|
231
|
-
PatientName: patientName,
|
|
232
|
-
PatientID: patientId,
|
|
233
|
-
Modality: "CT",
|
|
234
|
-
SOPClassUID: CT_SOP_CLASS,
|
|
235
|
-
SOPInstanceUID: sopUID,
|
|
236
|
-
SeriesInstanceUID: "1.2.3.4.5.6.7.8",
|
|
237
|
-
StudyInstanceUID: "1.2.3.4.5.6.7",
|
|
238
|
-
SeriesNumber: "1",
|
|
239
|
-
Rows: 1,
|
|
240
|
-
Columns: 1,
|
|
241
|
-
BitsAllocated: 16,
|
|
242
|
-
PixelRepresentation: 0,
|
|
243
|
-
PixelData: new Uint8Array([0, 0]).buffer
|
|
244
|
-
};
|
|
245
|
-
const dicomDict = new dcmjs.data.DicomDict({});
|
|
246
|
-
dicomDict.dict = dcmjs.data.DicomMetaDictionary.denaturalizeDataset(dataset);
|
|
247
|
-
const buffer = Buffer.from(dicomDict.write({ allowInvalidVRLength: true }));
|
|
248
|
-
mkdirSync4(resolve3(filePath, ".."), { recursive: true });
|
|
249
|
-
writeFileSync4(filePath, buffer);
|
|
250
|
-
}
|
|
251
|
-
function writeSyntheticFixturesToDir(outDir) {
|
|
252
|
-
const root = resolve3(outDir);
|
|
253
|
-
mkdirSync4(root, { recursive: true });
|
|
254
|
-
const written = [];
|
|
255
|
-
for (const { filename, variant } of SYNTHETIC_FIXTURES) {
|
|
256
|
-
const path = resolve3(root, filename);
|
|
257
|
-
writeFileSync4(path, buildSyntheticCtBuffer(variant));
|
|
258
|
-
written.push(path);
|
|
259
|
-
}
|
|
260
|
-
return written;
|
|
261
|
-
}
|
|
262
588
|
export {
|
|
263
|
-
SYNTHETIC_FIXTURES,
|
|
264
|
-
buildDicomDict,
|
|
265
|
-
buildDicomdirBuffer,
|
|
266
|
-
buildSyntheticCtBuffer,
|
|
267
589
|
caseCachePath,
|
|
268
590
|
defaultPublicCasesPath,
|
|
269
591
|
fetchPublicCaseToCache,
|
|
592
|
+
generateCollectionFromSpec,
|
|
593
|
+
generateFile,
|
|
270
594
|
loadCaseById,
|
|
271
595
|
loadCasesFromJson,
|
|
272
596
|
loadDefaultCases,
|
|
597
|
+
validateDatasetSpec,
|
|
273
598
|
verifySha256,
|
|
274
|
-
|
|
275
|
-
writeFakeDicomSignatureFile,
|
|
276
|
-
writeMinimalDicomFile,
|
|
277
|
-
writeNonDicomFile,
|
|
278
|
-
writeSyntheticFixturesToDir
|
|
599
|
+
writeCollectionFromSpec
|
|
279
600
|
};
|
|
File without changes
|