Skip to main content

qualia_core_db/medical/dicom_codecs/padding/
float.rs

1use super::{PaddingError, PaddingMatch};
2use crate::medical::{
3    dicom_codecs::float_pixels::{FloatBits, FloatWidth},
4    dicom_dataset::{Part10Dataset, WorkBudget},
5};
6#[derive(Clone, Copy, Debug)]
7pub struct FloatPadding {
8    source: [u8; 32],
9    width: FloatWidth,
10    endpoints: Option<[FloatBits; 2]>,
11}
12impl FloatPadding {
13    pub fn from_dataset(
14        ds: &Part10Dataset<'_>,
15        width: FloatWidth,
16        w: &mut WorkBudget,
17    ) -> Result<Self, PaddingError> {
18        if ds.find(None, width.pixel_tag(), w)?.is_none() {
19            return Err(PaddingError::Missing(width.pixel_tag()));
20        }
21        for tag in [
22            0x7fe00010,
23            if width == FloatWidth::F32 {
24                0x7fe00009
25            } else {
26                0x7fe00008
27            },
28        ] {
29            if ds.find(None, tag, w)?.is_some() {
30                return Err(PaddingError::Invalid(tag));
31            }
32        }
33        let (value, limit, vr, other) = match width {
34            FloatWidth::F32 => (0x00280122, 0x00280124, *b"FL", [0x00280123, 0x00280125]),
35            FloatWidth::F64 => (0x00280123, 0x00280125, *b"FD", [0x00280122, 0x00280124]),
36        };
37        for tag in [0x00280120, 0x00280121, other[0], other[1]] {
38            if ds.find(None, tag, w)?.is_some() {
39                return Err(PaddingError::Invalid(tag));
40            }
41        }
42        let read = |tag, w: &mut WorkBudget| -> Result<Option<FloatBits>, PaddingError> {
43            ds.expected_value(None, tag, vr, w)?
44                .map(|b| match width {
45                    FloatWidth::F32 => Ok(FloatBits::F32(u32::from_le_bytes(
46                        b.try_into().map_err(|_| PaddingError::Invalid(tag))?,
47                    ))),
48                    FloatWidth::F64 => Ok(FloatBits::F64(u64::from_le_bytes(
49                        b.try_into().map_err(|_| PaddingError::Invalid(tag))?,
50                    ))),
51                })
52                .transpose()
53        };
54        let a = read(value, w)?;
55        let b = read(limit, w)?;
56        let endpoints = match (a, b) {
57            (None, None) => None,
58            (Some(a), Some(b)) => Some([a, b]),
59            (None, Some(_)) => return Err(PaddingError::Missing(value)),
60            (Some(_), None) => return Err(PaddingError::Missing(limit)),
61        };
62        Ok(Self {
63            source: ds.source_blake3(),
64            width,
65            endpoints,
66        })
67    }
68    pub fn source_blake3(&self) -> [u8; 32] {
69        self.source
70    }
71    pub fn endpoints(&self) -> Option<[FloatBits; 2]> {
72        self.endpoints
73    }
74    /// Ordered endpoints use inclusive numeric comparison (including infinities
75    /// and numerically equal signed zeros). NaN endpoints remain Unordered:
76    /// DICOM permits these bits but does not define NaN range ordering/payload
77    /// equality. No finite measurement is guessed from that declaration.
78    pub fn classify(&self, value: FloatBits) -> Result<PaddingMatch, PaddingError> {
79        if value.width() != self.width {
80            return Err(PaddingError::WidthMismatch);
81        }
82        let Some([a, b]) = self.endpoints else {
83            return Ok(PaddingMatch::NotPadding);
84        };
85        let (Some(a), Some(b)) = (a.ordered_f64(), b.ordered_f64()) else {
86            return Ok(PaddingMatch::Unordered);
87        };
88        let Some(value) = value.ordered_f64() else {
89            return Ok(PaddingMatch::NotPadding);
90        };
91        let (low, high) = if a <= b { (a, b) } else { (b, a) };
92        Ok(if value >= low && value <= high {
93            PaddingMatch::Padding
94        } else {
95            PaddingMatch::NotPadding
96        })
97    }
98}