dcm4rs 0.1.0

DICOM 数据模型与数据字典
Documentation
use crate::dataset::DataSet;
use crate::endian::Endian;
use crate::error::{Error, Result};
use crate::tag::Tag;
use crate::value::Value;
use crate::vr::Vr;

/// A DICOM data element: tag, VR and value.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Element {
    /// The tag.
    pub tag: Tag,
    /// The value representation.
    pub vr: Vr,
    /// The value.
    pub value: Value,
}

impl Element {
    /// Construct an element. VR/value consistency is not checked here; checking happens at read
    /// time.
    pub fn new(tag: Tag, vr: Vr, value: Value) -> Self {
        Self { tag, vr, value }
    }

    /// Whether this is a sequence element.
    pub fn is_sequence(&self) -> bool {
        self.vr.is_sequence()
    }

    /// Byte length of the value; sequences return `0`.
    pub fn byte_len(&self) -> usize {
        self.value.as_bytes().map_or(0, <[u8]>::len)
    }

    /// Whether the value is empty.
    pub fn is_empty(&self) -> bool {
        self.value.is_empty()
    }

    /// Raw bytes; an empty slice for non-byte values.
    pub fn raw_value(&self) -> &[u8] {
        self.value.as_bytes().unwrap_or(&[])
    }

    /// Sequence items.
    pub fn as_sequence(&self) -> Result<&[DataSet]> {
        if !self.vr.is_sequence() {
            return Err(Error::NotASequence(self.vr));
        }
        Ok(self.value.as_sequence().unwrap_or(&[]))
    }

    /// Mutable sequence items; an empty value is first promoted to an empty sequence.
    pub fn as_sequence_mut(&mut self) -> Result<&mut Vec<DataSet>> {
        if !self.vr.is_sequence() {
            return Err(Error::NotASequence(self.vr));
        }
        if !self.value.is_sequence() {
            self.value = Value::Sequence(Vec::new());
        }
        self.value
            .as_sequence_mut()
            .ok_or(Error::NotASequence(self.vr))
    }

    /// String values split on backslashes, with trailing padding removed; textual VRs only.
    ///
    /// Values are decoded as UTF-8 from the raw bytes, so this is correct only for the default
    /// character set or `ISO_IR 192` (UTF-8). Other character sets (`ISO_IR 100`, `GB18030`, ...)
    /// are decoded once charset support lands.
    pub fn string_values(&self) -> Result<Vec<&str>> {
        if !self.vr.is_string() {
            return Err(Error::WrongVr {
                actual: self.vr,
                expected: "textual VR",
            });
        }
        let bytes = self.raw_value();
        if bytes.is_empty() {
            return Ok(Vec::new());
        }
        if self.vr.splits_on_backslash() {
            bytes
                .split(|byte| *byte == b'\\')
                .map(|part| decode_text(part, self.vr))
                .collect()
        } else {
            Ok(vec![decode_text(bytes, self.vr)?])
        }
    }

    /// The first string value; an empty value returns an empty string.
    pub fn string(&self) -> Result<&str> {
        Ok(self.string_values()?.first().copied().unwrap_or(""))
    }

    /// Decode 16-bit integers for `US`/`SS`/`UN`.
    pub fn i16s(&self, endian: Endian) -> Result<Vec<i16>> {
        self.expect_vr(&[Vr::Us, Vr::Ss, Vr::Un], "US/SS")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 2, self.vr)?;
        Ok(bytes
            .chunks_exact(2)
            .map(|chunk| endian.i16([chunk[0], chunk[1]]))
            .collect())
    }

    /// Decode 16-bit unsigned integers for `US`/`SS`/`UN`.
    pub fn u16s(&self, endian: Endian) -> Result<Vec<u16>> {
        self.expect_vr(&[Vr::Us, Vr::Ss, Vr::Un], "US/SS")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 2, self.vr)?;
        Ok(bytes
            .chunks_exact(2)
            .map(|chunk| endian.u16([chunk[0], chunk[1]]))
            .collect())
    }

    /// Decode 32-bit integers for `UL`/`SL`/`UN`.
    pub fn i32s(&self, endian: Endian) -> Result<Vec<i32>> {
        self.expect_vr(&[Vr::Ul, Vr::Sl, Vr::Un], "UL/SL")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 4, self.vr)?;
        Ok(bytes
            .chunks_exact(4)
            .map(|chunk| endian.i32([chunk[0], chunk[1], chunk[2], chunk[3]]))
            .collect())
    }

    /// Decode 32-bit unsigned integers for `UL`/`SL`/`UN`.
    pub fn u32s(&self, endian: Endian) -> Result<Vec<u32>> {
        self.expect_vr(&[Vr::Ul, Vr::Sl, Vr::Un], "UL/SL")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 4, self.vr)?;
        Ok(bytes
            .chunks_exact(4)
            .map(|chunk| endian.u32([chunk[0], chunk[1], chunk[2], chunk[3]]))
            .collect())
    }

    /// Decode 64-bit integers for `UV`/`SV`/`UN`.
    pub fn i64s(&self, endian: Endian) -> Result<Vec<i64>> {
        self.expect_vr(&[Vr::Uv, Vr::Sv, Vr::Un], "UV/SV")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 8, self.vr)?;
        Ok(bytes
            .chunks_exact(8)
            .map(|chunk| endian.i64(chunk.try_into().expect("8-byte slice")))
            .collect())
    }

    /// Decode 64-bit unsigned integers for `UV`/`SV`/`UN`.
    pub fn u64s(&self, endian: Endian) -> Result<Vec<u64>> {
        self.expect_vr(&[Vr::Uv, Vr::Sv, Vr::Un], "UV/SV")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 8, self.vr)?;
        Ok(bytes
            .chunks_exact(8)
            .map(|chunk| endian.u64(chunk.try_into().expect("8-byte slice")))
            .collect())
    }

    /// Decode single-precision floats for `FL`/`UN`.
    pub fn f32s(&self, endian: Endian) -> Result<Vec<f32>> {
        self.expect_vr(&[Vr::Fl, Vr::Un], "FL")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 4, self.vr)?;
        Ok(bytes
            .chunks_exact(4)
            .map(|chunk| endian.f32([chunk[0], chunk[1], chunk[2], chunk[3]]))
            .collect())
    }

    /// Decode double-precision floats for `FD`/`UN`.
    pub fn f64s(&self, endian: Endian) -> Result<Vec<f64>> {
        self.expect_vr(&[Vr::Fd, Vr::Un], "FD")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 8, self.vr)?;
        Ok(bytes
            .chunks_exact(8)
            .map(|chunk| endian.f64(chunk.try_into().expect("8-byte slice")))
            .collect())
    }

    /// Decode attribute tags for `AT`/`UN`.
    pub fn tags(&self, endian: Endian) -> Result<Vec<Tag>> {
        self.expect_vr(&[Vr::At, Vr::Un], "AT")?;
        let bytes = self.raw_value();
        check_unit(bytes.len(), 4, self.vr)?;
        Ok(bytes
            .chunks_exact(4)
            .map(|chunk| {
                Tag::new(
                    endian.u16([chunk[0], chunk[1]]),
                    endian.u16([chunk[2], chunk[3]]),
                )
            })
            .collect())
    }

    /// Parse `DS` text into a float sequence; empty components are skipped as "no value".
    pub fn decimal_values(&self) -> Result<Vec<f64>> {
        if self.vr != Vr::Ds {
            return Err(Error::WrongVr {
                actual: self.vr,
                expected: "DS",
            });
        }
        let mut values = Vec::new();
        for part in self.string_values()? {
            let text = part.trim();
            if text.is_empty() {
                continue;
            }
            values.push(
                text.parse::<f64>()
                    .map_err(|_| Error::InvalidNumber(text.to_owned()))?,
            );
        }
        Ok(values)
    }

    /// Parse `IS` text into an integer sequence; empty components are skipped as "no value".
    pub fn integer_values(&self) -> Result<Vec<i64>> {
        if self.vr != Vr::Is {
            return Err(Error::WrongVr {
                actual: self.vr,
                expected: "IS",
            });
        }
        let mut values = Vec::new();
        for part in self.string_values()? {
            let text = part.trim();
            if text.is_empty() {
                continue;
            }
            values.push(
                text.parse::<i64>()
                    .map_err(|_| Error::InvalidNumber(text.to_owned()))?,
            );
        }
        Ok(values)
    }

    fn expect_vr(&self, allowed: &[Vr], expected: &'static str) -> Result<()> {
        if allowed.contains(&self.vr) {
            Ok(())
        } else {
            Err(Error::WrongVr {
                actual: self.vr,
                expected,
            })
        }
    }
}

fn check_unit(len: usize, unit: usize, vr: Vr) -> Result<()> {
    if len % unit == 0 {
        Ok(())
    } else {
        Err(Error::WrongLength { vr, len, unit })
    }
}

fn decode_text(part: &[u8], vr: Vr) -> Result<&str> {
    let mut end = part.len();
    while end > 0 && (part[end - 1] == b' ' || part[end - 1] == vr.padding()) {
        end -= 1;
    }
    std::str::from_utf8(&part[..end]).map_err(Error::InvalidUtf8)
}