feat: add local DICOM and canvas PDF previews
This commit is contained in:
1 parent
b569f68300
commit
dd29258828
18 files changed
+960
-6
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { storedValue, windowPixel } from "../dicom-pixels";
|
||||
|
||||
function fixture() {
|
||||
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);
|
||||
data.push(
|
||||
group & 255,
|
||||
group >> 8,
|
||||
element & 255,
|
||||
element >> 8,
|
||||
vr.charCodeAt(0),
|
||||
vr.charCodeAt(1)
|
||||
);
|
||||
if (["OB", "OW"].includes(vr))
|
||||
data.push(0, 0, bytes.length & 255, bytes.length >> 8, 0, 0);
|
||||
else data.push(bytes.length & 255, bytes.length >> 8);
|
||||
data.push(...bytes);
|
||||
};
|
||||
const text = (s: string) => Array.from(new TextEncoder().encode(s));
|
||||
add(2, 0x10, "UI", text("1.2.840.10008.1.2.1"));
|
||||
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, 0x101, "US", [12, 0]);
|
||||
add(0x28, 0x102, "US", [11, 0]);
|
||||
add(0x28, 0x103, "US", [1, 0]);
|
||||
add(0x28, 0x1050, "DS", text("0"));
|
||||
add(0x28, 0x1051, "DS", text("4096"));
|
||||
add(
|
||||
0x7fe0,
|
||||
0x10,
|
||||
"OW",
|
||||
[0, 8, 255, 15, 0, 0, 255, 7, 255, 7, 0, 0, 255, 15, 0, 8]
|
||||
);
|
||||
return new Uint8Array(data).buffer;
|
||||
}
|
||||
|
||||
describe("DICOM rendering", () => {
|
||||
it("preserves signed 12-bit CT values", () => {
|
||||
expect(storedValue(0x800, 12, true)).toBe(-2048);
|
||||
expect(storedValue(0xfff, 12, true)).toBe(-1);
|
||||
expect(storedValue(0xfff, 12, false)).toBe(4095);
|
||||
});
|
||||
it("applies DICOM window and MONOCHROME1 inversion", () => {
|
||||
expect(windowPixel(-2048, 0, 4096, false)).toBe(0);
|
||||
expect(windowPixel(2047, 0, 4096, false)).toBe(255);
|
||||
expect(windowPixel(-2048, 0, 4096, true)).toBe(255);
|
||||
expect(windowPixel(0, 0, 1, false)).toBe(255);
|
||||
});
|
||||
it("decodes a real two-frame DICOM and switches frames", async () => {
|
||||
const postMessage = vi.fn();
|
||||
const scope = {
|
||||
postMessage,
|
||||
onmessage: null as unknown as (e: { data: object }) => void,
|
||||
};
|
||||
vi.stubGlobal("self", scope);
|
||||
try {
|
||||
await import("../dicom.worker");
|
||||
scope.onmessage({ data: { id: 1, buffer: fixture() } });
|
||||
const first = postMessage.mock.calls[0][0];
|
||||
expect(first.error).toBeUndefined();
|
||||
expect(first.frames).toBe(2);
|
||||
expect(first.cols).toBe(2);
|
||||
expect(first.rgba[0]).toBe(0);
|
||||
expect(first.rgba[12]).toBe(255);
|
||||
scope.onmessage({ data: { id: 2, frame: 1 } });
|
||||
expect(postMessage.mock.calls[1][0].rgba[0]).toBe(255);
|
||||
} finally {
|
||||
vi.unstubAllGlobals();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
export function storedValue(
|
||||
raw: number,
|
||||
bits: number,
|
||||
signed: boolean
|
||||
): number {
|
||||
const range = 2 ** bits;
|
||||
const value = raw % range;
|
||||
return signed && value >= range / 2 ? value - range : value;
|
||||
}
|
||||
|
||||
export function windowPixel(
|
||||
value: number,
|
||||
center: number,
|
||||
width: number,
|
||||
invert: boolean
|
||||
): number {
|
||||
const normalized =
|
||||
width <= 1
|
||||
? value > center - 0.5
|
||||
? 255
|
||||
: 0
|
||||
: Math.max(
|
||||
0,
|
||||
Math.min(255, ((value - (center - 0.5)) / (width - 1) + 0.5) * 255)
|
||||
);
|
||||
return Math.round(invert ? 255 - normalized : normalized);
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { Series, type Image } from "daikon";
|
||||
import { storedValue, windowPixel } from "./dicom-pixels";
|
||||
|
||||
let image: Image;
|
||||
let raw: DataView;
|
||||
let rows = 0,
|
||||
cols = 0,
|
||||
frames = 1,
|
||||
samples = 1,
|
||||
bits = 16,
|
||||
stored = 16;
|
||||
let compressed = false;
|
||||
let defaultCenter = 0,
|
||||
defaultWidth = 1;
|
||||
let slope = 1,
|
||||
intercept = 0;
|
||||
const pixel = (index: number) =>
|
||||
storedValue(
|
||||
bits === 8
|
||||
? raw.getUint8(index)
|
||||
: raw.getUint16(index * 2, image.littleEndian),
|
||||
stored,
|
||||
image.getPixelRepresentation() === 1
|
||||
) *
|
||||
slope +
|
||||
intercept;
|
||||
|
||||
self.onmessage = (event: MessageEvent) => {
|
||||
const { id, buffer, frame = 0, center, width } = event.data;
|
||||
try {
|
||||
if (buffer) {
|
||||
const parsed = Series.parseImage(new DataView(buffer));
|
||||
if (!parsed?.hasPixelData())
|
||||
throw new Error("DICOM has no supported image data");
|
||||
image = parsed;
|
||||
cols = image.getCols();
|
||||
rows = image.getRows();
|
||||
frames = image.getNumberOfFrames() || 1;
|
||||
samples = image.getNumberOfSamplesPerPixel() || 1;
|
||||
bits = image.getBitsAllocated();
|
||||
stored = image.getBitsStored() || bits;
|
||||
compressed = image.isCompressed();
|
||||
const photo = image.getPhotometricInterpretation().trim();
|
||||
if (
|
||||
![8, 16].includes(bits) ||
|
||||
stored < 1 ||
|
||||
stored > bits ||
|
||||
![1, 3].includes(samples) ||
|
||||
![cols, rows, frames].every((v) => Number.isSafeInteger(v) && v > 0) ||
|
||||
cols * rows > 16_000_000 ||
|
||||
(cols * rows * frames * samples * bits) / 8 > 128 * 1024 * 1024 ||
|
||||
!(
|
||||
photo === "MONOCHROME1" ||
|
||||
photo === "MONOCHROME2" ||
|
||||
(samples === 3 && (photo === "RGB" || compressed))
|
||||
)
|
||||
) {
|
||||
throw new Error("Unsupported DICOM pixel format or image too large");
|
||||
}
|
||||
raw = new DataView(image.getRawData());
|
||||
if (raw.byteLength < (cols * rows * frames * samples * bits) / 8)
|
||||
throw new Error("Truncated DICOM pixel data");
|
||||
slope = image.getDataScaleSlope() ?? 1;
|
||||
intercept = image.getDataScaleIntercept() ?? 0;
|
||||
let min = samples === 1 ? Infinity : 0,
|
||||
max = samples === 1 ? -Infinity : 255;
|
||||
if (samples === 1)
|
||||
for (let i = 0; i < cols * rows; i++) {
|
||||
const v = pixel(i);
|
||||
min = Math.min(min, v);
|
||||
max = Math.max(max, v);
|
||||
}
|
||||
defaultWidth =
|
||||
image.getWindowWidth() > 0
|
||||
? image.getWindowWidth()
|
||||
: Math.max(1, max - min + 1);
|
||||
defaultCenter =
|
||||
image.getWindowWidth() > 0 ? image.getWindowCenter() : (min + max) / 2;
|
||||
}
|
||||
if (!raw || !Number.isInteger(frame) || frame < 0 || frame >= frames)
|
||||
throw new Error("Invalid DICOM frame");
|
||||
const rgba = new Uint8ClampedArray(cols * rows * 4);
|
||||
const invert =
|
||||
image.getPhotometricInterpretation().trim() === "MONOCHROME1";
|
||||
const count = cols * rows,
|
||||
base = frame * count * samples;
|
||||
for (let i = 0; i < count; i++) {
|
||||
if (samples === 1) {
|
||||
const v = windowPixel(
|
||||
pixel(base + i),
|
||||
center ?? defaultCenter,
|
||||
width ?? defaultWidth,
|
||||
invert
|
||||
);
|
||||
rgba[i * 4] = rgba[i * 4 + 1] = rgba[i * 4 + 2] = v;
|
||||
} else {
|
||||
if (bits !== 8) throw new Error("Unsupported DICOM color depth");
|
||||
const planar = !compressed && image.getPlanarConfig() === 1;
|
||||
for (let c = 0; c < 3; c++)
|
||||
rgba[i * 4 + c] = raw.getUint8(
|
||||
base + (planar ? c * count + i : i * 3 + c)
|
||||
);
|
||||
}
|
||||
rgba[i * 4 + 3] = 255;
|
||||
}
|
||||
self.postMessage(
|
||||
{
|
||||
id,
|
||||
rgba,
|
||||
cols,
|
||||
rows,
|
||||
frames,
|
||||
center: defaultCenter,
|
||||
width: defaultWidth,
|
||||
color: samples === 3,
|
||||
},
|
||||
{ transfer: [rgba.buffer] }
|
||||
);
|
||||
} catch (error) {
|
||||
self.postMessage({
|
||||
id,
|
||||
error: error instanceof Error ? error.message : "Cannot decode DICOM",
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user