|
| 1 | +import { |
| 2 | + createDicomParser, |
| 3 | + DataElement, |
| 4 | +} from '@/src/core/streaming/dicom/dicomParser'; |
| 5 | + |
| 6 | +export const US_REGION_META_KEY = '__volview_us_region'; |
| 7 | + |
| 8 | +// DICOM unit codes for PhysicalUnitsXDirection / YDirection |
| 9 | +// 0x0003 = centimeters. See DICOM PS3.3 C.8.5.5.1.1. |
| 10 | +export const US_UNIT_CENTIMETERS = 3; |
| 11 | + |
| 12 | +export type UltrasoundRegion = { |
| 13 | + physicalDeltaX: number; |
| 14 | + physicalDeltaY: number; |
| 15 | + physicalUnitsXDirection: number; |
| 16 | + physicalUnitsYDirection: number; |
| 17 | +}; |
| 18 | + |
| 19 | +const SEQUENCE_OF_ULTRASOUND_REGIONS: [number, number] = [0x0018, 0x6011]; |
| 20 | +const PHYSICAL_DELTA_X: [number, number] = [0x0018, 0x602c]; |
| 21 | +const PHYSICAL_DELTA_Y: [number, number] = [0x0018, 0x602e]; |
| 22 | +const PHYSICAL_UNITS_X_DIRECTION: [number, number] = [0x0018, 0x6024]; |
| 23 | +const PHYSICAL_UNITS_Y_DIRECTION: [number, number] = [0x0018, 0x6026]; |
| 24 | + |
| 25 | +const isTag = (el: DataElement, [group, element]: [number, number]) => |
| 26 | + el.group === group && el.element === element; |
| 27 | + |
| 28 | +const readFloat64LE = (bytes: Uint8Array) => |
| 29 | + new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getFloat64( |
| 30 | + 0, |
| 31 | + true |
| 32 | + ); |
| 33 | + |
| 34 | +const readUint16LE = (bytes: Uint8Array) => |
| 35 | + new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint16( |
| 36 | + 0, |
| 37 | + true |
| 38 | + ); |
| 39 | + |
| 40 | +/** |
| 41 | + * Decodes the first item of a SequenceOfUltrasoundRegions element. |
| 42 | + * Returns null if required fields are missing. |
| 43 | + */ |
| 44 | +export function decodeUltrasoundRegion( |
| 45 | + sequenceData: DataElement['data'] |
| 46 | +): UltrasoundRegion | null { |
| 47 | + if (!Array.isArray(sequenceData) || sequenceData.length === 0) return null; |
| 48 | + const [firstItem] = sequenceData; |
| 49 | + |
| 50 | + const findBytes = (target: [number, number]) => { |
| 51 | + const el = firstItem.find((inner) => isTag(inner, target)); |
| 52 | + if (!el || !(el.data instanceof Uint8Array)) return null; |
| 53 | + return el.data; |
| 54 | + }; |
| 55 | + |
| 56 | + const deltaXBytes = findBytes(PHYSICAL_DELTA_X); |
| 57 | + const deltaYBytes = findBytes(PHYSICAL_DELTA_Y); |
| 58 | + const unitsXBytes = findBytes(PHYSICAL_UNITS_X_DIRECTION); |
| 59 | + const unitsYBytes = findBytes(PHYSICAL_UNITS_Y_DIRECTION); |
| 60 | + |
| 61 | + if (!deltaXBytes || !deltaYBytes || !unitsXBytes || !unitsYBytes) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + physicalDeltaX: readFloat64LE(deltaXBytes), |
| 67 | + physicalDeltaY: readFloat64LE(deltaYBytes), |
| 68 | + physicalUnitsXDirection: readUint16LE(unitsXBytes), |
| 69 | + physicalUnitsYDirection: readUint16LE(unitsYBytes), |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Parses a DICOM blob and returns the first ultrasound region, if present. |
| 75 | + */ |
| 76 | +export async function parseUltrasoundRegionFromBlob( |
| 77 | + blob: Blob |
| 78 | +): Promise<UltrasoundRegion | null> { |
| 79 | + let region: UltrasoundRegion | null = null; |
| 80 | + |
| 81 | + const parse = createDicomParser({ |
| 82 | + stopAtElement(group, element) { |
| 83 | + return group === 0x7fe0 && element === 0x0010; |
| 84 | + }, |
| 85 | + onDataElement(el) { |
| 86 | + if (region) return; |
| 87 | + if (isTag(el, SEQUENCE_OF_ULTRASOUND_REGIONS)) { |
| 88 | + region = decodeUltrasoundRegion(el.data); |
| 89 | + } |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + const stream = blob.stream(); |
| 94 | + const reader = stream.getReader(); |
| 95 | + try { |
| 96 | + while (!region) { |
| 97 | + const { value, done } = await reader.read(); |
| 98 | + if (done) break; |
| 99 | + const result = parse(value); |
| 100 | + if (result.done) break; |
| 101 | + } |
| 102 | + } catch { |
| 103 | + return null; |
| 104 | + } finally { |
| 105 | + reader.releaseLock(); |
| 106 | + } |
| 107 | + |
| 108 | + return region; |
| 109 | +} |
| 110 | + |
| 111 | +export function encodeUltrasoundRegionMeta( |
| 112 | + region: UltrasoundRegion |
| 113 | +): [string, string] { |
| 114 | + return [US_REGION_META_KEY, JSON.stringify(region)]; |
| 115 | +} |
| 116 | + |
| 117 | +export function getUltrasoundRegionFromMetadata( |
| 118 | + meta: ReadonlyArray<readonly [string, string]> | null | undefined |
| 119 | +): UltrasoundRegion | null { |
| 120 | + if (!meta) return null; |
| 121 | + const entry = meta.find(([key]) => key === US_REGION_META_KEY); |
| 122 | + if (!entry) return null; |
| 123 | + try { |
| 124 | + return JSON.parse(entry[1]) as UltrasoundRegion; |
| 125 | + } catch { |
| 126 | + return null; |
| 127 | + } |
| 128 | +} |
0 commit comments