fix: widen file information and decode DICOM text encodings
This commit is contained in:
1 parent
a69fdf2e09
commit
b87f8cb961
13 files changed
+279
-33
No files matched your search
@@ -49,6 +49,61 @@ describe("DICOM rendering", () => {
|
||||
(tag: { tag: string }) => tag.tag === "(7FE0,0010)"
|
||||
)
|
||||
).toBe(false);
|
||||
const checkName = (
|
||||
id: number,
|
||||
buffer: ArrayBuffer,
|
||||
encoding: string,
|
||||
expected: string
|
||||
) => {
|
||||
scope.onmessage({ data: { id, mode: "metadata", buffer, encoding } });
|
||||
const result = postMessage.mock.lastCall![0];
|
||||
expect(result.error).toBeUndefined();
|
||||
expect(
|
||||
result.metadata.find(
|
||||
(entry: { tag: string }) => entry.tag === "(0010,0010)"
|
||||
).value
|
||||
).toBe(expected);
|
||||
return result;
|
||||
};
|
||||
expect(
|
||||
checkName(
|
||||
4,
|
||||
fixture(16, {
|
||||
charset: "ISO_IR 192",
|
||||
patientBytes: [0xe3, 0xcd, 0xe3, 0xcf],
|
||||
}),
|
||||
"auto",
|
||||
"محمد"
|
||||
).encoding
|
||||
).toBe("windows-1256");
|
||||
expect(
|
||||
checkName(
|
||||
5,
|
||||
fixture(16, {
|
||||
charset: "WINDOWS-1256",
|
||||
patientBytes: Array.from(new TextEncoder().encode("Иван^Петров")),
|
||||
}),
|
||||
"auto",
|
||||
"Иван Петров"
|
||||
).encoding
|
||||
).toBe("utf-8");
|
||||
checkName(
|
||||
6,
|
||||
fixture(16, {
|
||||
charset: "ISO_IR 192",
|
||||
patientBytes: [0xc8, 0xe2, 0xe0, 0xed],
|
||||
}),
|
||||
"windows-1251",
|
||||
"Иван"
|
||||
);
|
||||
scope.onmessage({
|
||||
data: { id: 7, mode: "metadata", encoding: "windows-1251" },
|
||||
});
|
||||
expect(
|
||||
postMessage.mock.lastCall![0].metadata.find(
|
||||
(entry: { tag: string }) => entry.tag === "(0010,0010)"
|
||||
).value
|
||||
).toBe("Иван");
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
export function fixture(bits = 16) {
|
||||
export function fixture(
|
||||
bits = 16,
|
||||
options: { charset?: string; patientBytes?: number[] } = {}
|
||||
) {
|
||||
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,9 +20,10 @@ export function fixture(bits = 16) {
|
||||
};
|
||||
const text = (s: string) => Array.from(new TextEncoder().encode(s));
|
||||
add(2, 0x10, "UI", text("1.2.840.10008.1.2.1"));
|
||||
if (options.charset) add(8, 5, "CS", text(options.charset));
|
||||
add(8, 0x20, "DA", text("20261022"));
|
||||
add(8, 0x60, "CS", text("CT"));
|
||||
add(0x10, 0x10, "PN", text("Example^Patient"));
|
||||
add(0x10, 0x10, "PN", options.patientBytes || text("Example^Patient"));
|
||||
add(0x10, 0x20, "LO", text("TEST-123"));
|
||||
add(0x28, 2, "US", [1, 0]);
|
||||
add(0x28, 4, "CS", text("MONOCHROME2"));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Dictionary, type Image } from "daikon";
|
||||
import { Dictionary, type Image, type Tag } from "daikon";
|
||||
|
||||
export interface DicomMetadata {
|
||||
tag: string;
|
||||
@@ -6,7 +6,85 @@ export interface DicomMetadata {
|
||||
value: string;
|
||||
}
|
||||
|
||||
export function dicomMetadata(image: Image): DicomMetadata[] {
|
||||
const textVRs = ["PN", "LO", "SH", "ST", "LT", "UC", "UT"];
|
||||
const encodings: Record<string, string> = {
|
||||
ISO_IR192: "utf-8",
|
||||
UTF8: "utf-8",
|
||||
ISO_IR100: "windows-1252",
|
||||
ISO_IR101: "iso-8859-2",
|
||||
ISO_IR109: "iso-8859-3",
|
||||
ISO_IR110: "iso-8859-4",
|
||||
ISO_IR144: "iso-8859-5",
|
||||
ISO_IR127: "iso-8859-6",
|
||||
ISO_IR126: "iso-8859-7",
|
||||
ISO_IR138: "iso-8859-8",
|
||||
ISO_IR148: "iso-8859-9",
|
||||
ISO_IR166: "windows-874",
|
||||
GB18030: "gb18030",
|
||||
GBK: "gbk",
|
||||
ISO_IR13: "shift_jis",
|
||||
WINDOWS1256: "windows-1256",
|
||||
CP1256: "windows-1256",
|
||||
WINDOWS1251: "windows-1251",
|
||||
CP1251: "windows-1251",
|
||||
};
|
||||
function rawText(tag: Tag, buffer: ArrayBuffer) {
|
||||
if (
|
||||
tag.offsetValue < 0 ||
|
||||
tag.offsetEnd < tag.offsetValue ||
|
||||
tag.offsetEnd > buffer.byteLength
|
||||
)
|
||||
return new Uint8Array();
|
||||
return new Uint8Array(
|
||||
buffer,
|
||||
tag.offsetValue,
|
||||
Math.min(tag.offsetEnd - tag.offsetValue, 65536)
|
||||
);
|
||||
}
|
||||
export function dicomTextEncoding(
|
||||
image: Image,
|
||||
buffer: ArrayBuffer,
|
||||
preferred = "auto"
|
||||
) {
|
||||
if (preferred !== "auto") {
|
||||
new TextDecoder(preferred);
|
||||
return preferred;
|
||||
}
|
||||
const tags = Object.values(image.tags);
|
||||
const charset = tags.find((tag) => tag.group === 8 && tag.element === 5);
|
||||
const declared = String(
|
||||
Array.isArray(charset?.value) ? charset.value[0] : charset?.value || ""
|
||||
)
|
||||
.replace(/[\s-]/g, "")
|
||||
.toUpperCase();
|
||||
let nonAscii = false,
|
||||
validUtf8 = true;
|
||||
for (const tag of tags.filter(
|
||||
(tag) => textVRs.includes(tag.vr) && !tag.sublist
|
||||
)) {
|
||||
const bytes = rawText(tag, buffer);
|
||||
if (!bytes.some((byte) => byte > 127)) continue;
|
||||
nonAscii = true;
|
||||
try {
|
||||
new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
||||
} catch {
|
||||
validUtf8 = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nonAscii && validUtf8) return "utf-8";
|
||||
const known = encodings[declared];
|
||||
// Broken exporters can declare UTF-8 while writing legacy Arabic bytes.
|
||||
// Single-byte encodings are ambiguous; allow an explicit override in the UI.
|
||||
if (known && !(known === "utf-8" && !validUtf8)) return known;
|
||||
return nonAscii ? "windows-1256" : "utf-8";
|
||||
}
|
||||
|
||||
export function dicomMetadata(
|
||||
image: Image,
|
||||
buffer?: ArrayBuffer,
|
||||
encoding = "utf-8"
|
||||
): DicomMetadata[] {
|
||||
const hex = (n: number) => n.toString(16).toUpperCase().padStart(4, "0");
|
||||
return Object.values({ ...image.tagsFlat, ...image.tags })
|
||||
.filter(
|
||||
@@ -20,9 +98,14 @@ export function dicomMetadata(image: Image): DicomMetadata[] {
|
||||
.sort((a, b) => a.group - b.group || a.element - b.element)
|
||||
.slice(0, 512)
|
||||
.flatMap((tag) => {
|
||||
const values = Array.isArray(tag.value)
|
||||
let values = Array.isArray(tag.value)
|
||||
? tag.value.slice(0, 64)
|
||||
: [tag.value];
|
||||
if (buffer && textVRs.includes(tag.vr)) {
|
||||
let text = new TextDecoder(encoding).decode(rawText(tag, buffer));
|
||||
if (tag.vr === "PN") text = text.replace(/\^/g, " ");
|
||||
values = text.split("\\").slice(0, 64);
|
||||
}
|
||||
const value = values
|
||||
.filter((v) => typeof v === "string" || typeof v === "number")
|
||||
.map((v) => String(v).replace(/\0/g, "").trim())
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Series, type Image } from "daikon";
|
||||
import { storedValue, windowPixel } from "./dicom-pixels";
|
||||
import { dicomMetadata } from "./dicom-metadata";
|
||||
import { dicomMetadata, dicomTextEncoding } from "./dicom-metadata";
|
||||
|
||||
let image: Image;
|
||||
let metadataImage: Image | null = null,
|
||||
metadataBuffer: ArrayBuffer | undefined;
|
||||
let raw: DataView;
|
||||
let rows = 0,
|
||||
cols = 0,
|
||||
@@ -27,15 +29,41 @@ const pixel = (index: number) =>
|
||||
intercept;
|
||||
|
||||
self.onmessage = (event: MessageEvent) => {
|
||||
const { id, buffer, mode, frame = 0, center, width } = event.data;
|
||||
const {
|
||||
id,
|
||||
buffer,
|
||||
mode,
|
||||
encoding = "auto",
|
||||
frame = 0,
|
||||
center,
|
||||
width,
|
||||
} = event.data;
|
||||
try {
|
||||
if (mode === "metadata") {
|
||||
if (buffer) {
|
||||
metadataImage = Series.parseImage(new DataView(buffer));
|
||||
metadataBuffer = metadataImage?.inflated || buffer;
|
||||
}
|
||||
if (!metadataImage || !metadataBuffer)
|
||||
throw new Error("Invalid DICOM file");
|
||||
const selectedEncoding = dicomTextEncoding(
|
||||
metadataImage,
|
||||
metadataBuffer,
|
||||
encoding
|
||||
);
|
||||
self.postMessage({
|
||||
id,
|
||||
metadata: dicomMetadata(
|
||||
metadataImage,
|
||||
metadataBuffer,
|
||||
selectedEncoding
|
||||
),
|
||||
encoding: selectedEncoding,
|
||||
});
|
||||
return;
|
||||
}
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user