qualia_core_db/medical/dicom_codecs/padding/
integer.rs1use super::{map_sample, PaddingError, QuantitativeSample, SampleValidity};
2use crate::medical::{
3 dicom_codecs::{metadata::IntegerImageInfo, NativePixelView, StoredSample},
4 dicom_dataset::{Part10Dataset, WorkBudget},
5 dicom_frames::FrameRecord,
6};
7#[derive(Clone, Copy, Debug)]
8pub struct IntegerPadding {
9 source: [u8; 32],
10 endpoints: Option<[i64; 2]>,
11}
12impl IntegerPadding {
13 pub fn source_blake3(&self) -> [u8; 32] {
14 self.source
15 }
16 pub fn endpoints(&self) -> Option<[i64; 2]> {
17 self.endpoints
18 }
19 pub fn is_padding(&self, value: StoredSample) -> bool {
20 let value = match value {
21 StoredSample::Unsigned(n) => n as i64,
22 StoredSample::Signed(n) => n as i64,
23 };
24 self.endpoints
25 .is_some_and(|r| value >= r[0].min(r[1]) && value <= r[0].max(r[1]))
26 }
27 fn from_dataset(
28 ds: &Part10Dataset<'_>,
29 max_bytes: usize,
30 w: &mut WorkBudget,
31 ) -> Result<Self, PaddingError> {
32 let info = IntegerImageInfo::from_dataset(ds, max_bytes, w)?;
33 for tag in [0x00280122, 0x00280123, 0x00280124, 0x00280125] {
34 if ds.find(None, tag, w)?.is_some() {
35 return Err(PaddingError::Invalid(tag));
36 }
37 }
38 let read = |tag, w: &mut WorkBudget| -> Result<Option<i64>, PaddingError> {
39 ds.expected_value(None, tag, if info.signed { *b"SS" } else { *b"US" }, w)?
40 .map(|b| {
41 let a: [u8; 2] = b.try_into().map_err(|_| PaddingError::Invalid(tag))?;
42 Ok(if info.signed {
43 i16::from_le_bytes(a) as i64
44 } else {
45 u16::from_le_bytes(a) as i64
46 })
47 })
48 .transpose()
49 };
50 let a = read(0x00280120, w)?;
51 let b = read(0x00280121, w)?;
52 let endpoints = match (a, b) {
53 (None, None) => None,
54 (Some(a), None) => Some([a, a]),
55 (Some(a), Some(b)) => Some([a, b]),
56 (None, Some(_)) => return Err(PaddingError::Missing(0x00280120)),
57 };
58 if let Some([a, b]) = endpoints {
59 let (low, high) = if info.signed {
60 let n = 1i64
61 .checked_shl((info.bits_stored - 1) as u32)
62 .ok_or(PaddingError::Invalid(0x00280101))?;
63 (-n, n - 1)
64 } else {
65 (
66 0,
67 1i64.checked_shl(info.bits_stored as u32)
68 .ok_or(PaddingError::Invalid(0x00280101))?
69 - 1,
70 )
71 };
72 if a < low || a > high || b < low || b > high {
73 return Err(PaddingError::Invalid(0x00280120));
74 }
75 if (info.photometric == "MONOCHROME2" && a > b)
76 || (info.photometric == "MONOCHROME1" && a < b)
77 {
78 return Err(PaddingError::Invalid(0x00280121));
79 }
80 }
81 Ok(Self {
82 source: ds.source_blake3(),
83 endpoints,
84 })
85 }
86}
87#[derive(Clone, Copy, Debug, PartialEq, Eq)]
88pub struct IntegerSample {
89 pub stored: StoredSample,
90 pub validity: SampleValidity,
91}
92pub struct IntegerPaddingView<'v, 'a> {
95 pixels: &'v NativePixelView<'a>,
96 padding: IntegerPadding,
97}
98impl<'v, 'a> IntegerPaddingView<'v, 'a> {
99 pub fn bind(
100 ds: &Part10Dataset<'_>,
101 pixels: &'v NativePixelView<'a>,
102 w: &mut WorkBudget,
103 ) -> Result<Self, PaddingError> {
104 if ds.source_blake3() != pixels.source_blake3() {
105 return Err(PaddingError::SourceMismatch);
106 }
107 let padding = IntegerPadding::from_dataset(ds, pixels.stored_sample_bytes().len(), w)?;
108 Ok(Self { pixels, padding })
109 }
110 pub fn padding(&self) -> IntegerPadding {
111 self.padding
112 }
113 pub fn sample(&self, frame: u32, row: u16, column: u16) -> Result<IntegerSample, PaddingError> {
114 let stored = self.pixels.sample(frame, row, column)?;
115 let validity = if self.padding.is_padding(stored) {
116 SampleValidity::Padding
117 } else {
118 SampleValidity::Valid
119 };
120 Ok(IntegerSample { stored, validity })
121 }
122 pub fn quantitative_sample<'b>(
123 &self,
124 record: &FrameRecord<'b>,
125 row: u16,
126 column: u16,
127 real_world: bool,
128 ) -> Result<QuantitativeSample<'b>, PaddingError> {
129 if record.identity().source_blake3 != self.padding.source {
130 return Err(PaddingError::SourceMismatch);
131 }
132 let frame = record
133 .frame_number()
134 .checked_sub(1)
135 .ok_or(PaddingError::FrameMismatch)?;
136 let sample = self.sample(frame, row, column)?;
137 map_sample(
138 record,
139 self.padding.source,
140 frame,
141 Some(sample.stored.as_f64()),
142 sample.validity,
143 real_world,
144 )
145 }
146}