102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
import { MAX_VOLUME_BYTES, type DicomSlice } from "./dicom-series";
|
|
|
|
export function dicomPreferences(
|
|
value?: Partial<Dicom3DSettings>
|
|
): Dicom3DSettings {
|
|
return {
|
|
memoryMiB:
|
|
Number.isInteger(value?.memoryMiB) &&
|
|
value!.memoryMiB! >= 128 &&
|
|
value!.memoryMiB! <= 1536
|
|
? value!.memoryMiB!
|
|
: 1536,
|
|
quality: ["auto", "original", "half", "quarter"].includes(
|
|
value?.quality || ""
|
|
)
|
|
? value!.quality!
|
|
: "auto",
|
|
opacity:
|
|
Number.isInteger(value?.opacity) &&
|
|
value!.opacity! >= 5 &&
|
|
value!.opacity! <= 100
|
|
? value!.opacity!
|
|
: 30,
|
|
};
|
|
}
|
|
|
|
export function volumeSampling(
|
|
rows: number,
|
|
cols: number,
|
|
count: number,
|
|
preferences?: Partial<Dicom3DSettings>
|
|
) {
|
|
const settings = dicomPreferences(preferences);
|
|
const limit = Math.min(MAX_VOLUME_BYTES, settings.memoryMiB * 1024 * 1024);
|
|
let factor =
|
|
settings.quality === "half" ? 2 : settings.quality === "quarter" ? 4 : 1;
|
|
const dimensions = () => ({
|
|
rows: Math.max(2, Math.ceil(rows / factor)),
|
|
cols: Math.max(2, Math.ceil(cols / factor)),
|
|
});
|
|
const bytes = () => dimensions().rows * dimensions().cols * count * 4;
|
|
if (settings.quality === "auto") {
|
|
factor = Math.max(
|
|
1,
|
|
Math.ceil(Math.sqrt((rows * cols * count * 4) / limit))
|
|
);
|
|
while (bytes() > limit && factor < Math.max(rows, cols)) factor++;
|
|
}
|
|
if (
|
|
rows < 2 ||
|
|
cols < 2 ||
|
|
![rows, cols, count].every((n) => Number.isSafeInteger(n) && n > 0)
|
|
)
|
|
throw new Error("DICOM_GEOMETRY");
|
|
if (bytes() > limit) throw new Error("DICOM_VOLUME_LIMIT");
|
|
return {
|
|
...dimensions(),
|
|
factor,
|
|
bytes: bytes(),
|
|
limit,
|
|
originalDimensions: [cols, rows, count] as [number, number, number],
|
|
};
|
|
}
|
|
|
|
// Bilinear resampling keeps the full field of view and its physical spacing.
|
|
// Every slice keeps its original patient position and orientation.
|
|
export function resampleSlice(
|
|
slice: DicomSlice,
|
|
target: ReturnType<typeof volumeSampling>
|
|
): DicomSlice {
|
|
if (target.rows === slice.rows && target.cols === slice.cols) return slice;
|
|
const dx = (slice.cols - 1) / (target.cols - 1),
|
|
dy = (slice.rows - 1) / (target.rows - 1);
|
|
const pixels = new Float32Array(target.rows * target.cols);
|
|
for (let y = 0; y < target.rows; y++) {
|
|
const sy = y * dy,
|
|
y0 = Math.floor(sy),
|
|
y1 = Math.min(slice.rows - 1, y0 + 1),
|
|
fy = sy - y0;
|
|
for (let x = 0; x < target.cols; x++) {
|
|
const sx = x * dx,
|
|
x0 = Math.floor(sx),
|
|
x1 = Math.min(slice.cols - 1, x0 + 1),
|
|
fx = sx - x0;
|
|
const a =
|
|
slice.pixels[y0 * slice.cols + x0] * (1 - fx) +
|
|
slice.pixels[y0 * slice.cols + x1] * fx;
|
|
const b =
|
|
slice.pixels[y1 * slice.cols + x0] * (1 - fx) +
|
|
slice.pixels[y1 * slice.cols + x1] * fx;
|
|
pixels[y * target.cols + x] = a * (1 - fy) + b * fy;
|
|
}
|
|
}
|
|
return {
|
|
...slice,
|
|
rows: target.rows,
|
|
cols: target.cols,
|
|
pixels,
|
|
spacing: [slice.spacing[0] * dy, slice.spacing[1] * dx],
|
|
};
|
|
}
|