feat: fit DICOM previews and show searchable file metadata

This commit is contained in:
SantaSpeen committed 2026-10-22 11:07:27 +03:00
1 parent 7a1d18dfc6
commit a69fdf2e09
17 files changed
+471 -221

No files matched your search

@@ -33,6 +33,22 @@ describe("DICOM rendering", () => {
expect(first.rgba[12]).toBe(255);
scope.onmessage({ data: { id: 2, frame: 1 } });
expect(postMessage.mock.calls[1][0].rgba[0]).toBe(255);
scope.onmessage({
data: { id: 3, mode: "metadata", buffer: fixture(32) },
});
const metadata = postMessage.mock.calls[2][0];
expect(metadata.error).toBeUndefined();
expect(metadata.rgba).toBeUndefined();
expect(metadata.metadata).toContainEqual({
tag: "(0010,0010)",
name: "Patient Name",
value: "Example Patient",
});
expect(
metadata.metadata.some(
(tag: { tag: string }) => tag.tag === "(7FE0,0010)"
)
).toBe(false);
} finally {
vi.unstubAllGlobals();
}
@@ -1,4 +1,4 @@
export function fixture() {
export function fixture(bits = 16) {
const data: number[] = [...new Uint8Array(128), 68, 73, 67, 77];
const add = (group: number, element: number, vr: string, bytes: number[]) => {
if (bytes.length % 2) bytes.push(vr === "UI" ? 0 : 32);
@@ -17,12 +17,16 @@ export function fixture() {
};
const text = (s: string) => Array.from(new TextEncoder().encode(s));
add(2, 0x10, "UI", text("1.2.840.10008.1.2.1"));
add(8, 0x20, "DA", text("20261022"));
add(8, 0x60, "CS", text("CT"));
add(0x10, 0x10, "PN", text("Example^Patient"));
add(0x10, 0x20, "LO", text("TEST-123"));
add(0x28, 2, "US", [1, 0]);
add(0x28, 4, "CS", text("MONOCHROME2"));
add(0x28, 8, "IS", text("2"));
add(0x28, 0x10, "US", [2, 0]);
add(0x28, 0x11, "US", [2, 0]);
add(0x28, 0x100, "US", [16, 0]);
add(0x28, 0x100, "US", [bits, 0]);
add(0x28, 0x101, "US", [12, 0]);
add(0x28, 0x102, "US", [11, 0]);
add(0x28, 0x103, "US", [1, 0]);
+28
View File
@@ -0,0 +1,28 @@
export async function readDicomFile(source: string, signal: AbortSignal) {
const response = await fetch(source, { credentials: "same-origin", signal });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const limit = 64 * 1024 * 1024;
if (Number(response.headers.get("Content-Length")) > limit)
throw new Error("DICOM_TOO_LARGE");
const reader = response.body?.getReader();
if (!reader) throw new Error("Empty response");
const chunks: Uint8Array[] = [];
let length = 0;
while (true) {
const chunk = await reader.read();
if (chunk.done) break;
length += chunk.value.length;
if (length > limit) {
await reader.cancel();
throw new Error("DICOM_TOO_LARGE");
}
chunks.push(chunk.value);
}
const data = new Uint8Array(length);
let offset = 0;
for (const chunk of chunks) {
data.set(chunk, offset);
offset += chunk.length;
}
return data.buffer;
}
+42
View File
@@ -0,0 +1,42 @@
import { Dictionary, type Image } from "daikon";
export interface DicomMetadata {
tag: string;
name: string;
value: string;
}
export function dicomMetadata(image: Image): DicomMetadata[] {
const hex = (n: number) => n.toString(16).toUpperCase().padStart(4, "0");
return Object.values({ ...image.tagsFlat, ...image.tags })
.filter(
(tag) =>
tag &&
!tag.sublist &&
tag.value != null &&
!["OB", "OW", "OF", "OD", "OL", "OV", "UN", "SQ"].includes(tag.vr) &&
!(tag.group === 0x7fe0 && tag.element === 0x10)
)
.sort((a, b) => a.group - b.group || a.element - b.element)
.slice(0, 512)
.flatMap((tag) => {
const values = Array.isArray(tag.value)
? tag.value.slice(0, 64)
: [tag.value];
const value = values
.filter((v) => typeof v === "string" || typeof v === "number")
.map((v) => String(v).replace(/\0/g, "").trim())
.join(" \\ ")
.slice(0, 2048);
if (!value) return [];
return [
{
tag: `(${hex(tag.group)},${hex(tag.element)})`,
name: (
Dictionary.getDescription(tag.group, tag.element) || "DICOM"
).replace(/([a-z])([A-Z])/g, "$1 $2"),
value,
},
];
});
}
+7 -1
View File
@@ -1,5 +1,6 @@
import { Series, type Image } from "daikon";
import { storedValue, windowPixel } from "./dicom-pixels";
import { dicomMetadata } from "./dicom-metadata";
let image: Image;
let raw: DataView;
@@ -26,10 +27,15 @@ const pixel = (index: number) =>
intercept;
self.onmessage = (event: MessageEvent) => {
const { id, buffer, frame = 0, center, width } = event.data;
const { id, buffer, mode, frame = 0, center, width } = event.data;
try {
if (buffer) {
const parsed = Series.parseImage(new DataView(buffer));
if (mode === "metadata") {
if (!parsed) throw new Error("Invalid DICOM file");
self.postMessage({ id, metadata: dicomMetadata(parsed) });
return;
}
if (!parsed?.hasPixelData())
throw new Error("DICOM has no supported image data");
image = parsed;