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", }); } };