dcmnorm-transcode 0.20.0

DICOM transfer-syntax registry and pixel codecs for dcmnorm, forked from dicom-transfer-syntax-registry 0.9.1. See README.md.
Documentation
//! Support for encapsulated uncompressed via pixel data adapter.

use dcmnorm_core::{
    ops::{AttributeAction, AttributeOp},
    PrimitiveValue, Tag,
};
use dcmnorm_encoding::{
    adapters::{
        decode_error, encode_error, DecodeResult, EncodeOptions, EncodeResult, PixelDataObject,
        PixelDataReader, PixelDataWriter,
    },
    snafu::OptionExt,
};

/// Adapter for [Encapsulated Uncompressed Explicit VR Little Endian][1]
/// [1]: https://dicom.nema.org/medical/dicom/2023c/output/chtml/part05/sect_A.4.11.html
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UncompressedAdapter;

impl PixelDataReader for UncompressedAdapter {
    fn decode(&self, src: &dyn PixelDataObject, dst: &mut Vec<u8>) -> DecodeResult<()> {
        // just flatten all fragments into the output vector
        let pixeldata = src
            .raw_pixel_data()
            .context(decode_error::MissingAttributeSnafu { name: "Pixel Data" })?;

        for fragment in pixeldata.fragments {
            dst.extend_from_slice(&fragment);
        }

        Ok(())
    }

    fn decode_frame(
        &self,
        src: &dyn PixelDataObject,
        frame: u32,
        dst: &mut Vec<u8>,
    ) -> DecodeResult<()> {
        // `fragment()` is zero-copy (`Cow::Borrowed`) - unlike `raw_pixel_data()`, which clones
        // every fragment just to let a per-frame caller index into one of them. That matters
        // here: this is called once per frame for a multi-frame series (see
        // `decode_pixel_data_parallel_frames` in dcmnorm's own `io.rs`), so going through
        // `raw_pixel_data()` would clone the *entire* pixel buffer on every single frame call -
        // O(frames^2) total bytes copied for an N-frame series where O(N) would do.
        let fragment = src
            .fragment(frame as usize)
            .context(decode_error::FrameRangeOutOfBoundsSnafu)?;

        dst.extend_from_slice(&fragment);

        Ok(())
    }
}

impl PixelDataWriter for UncompressedAdapter {
    fn encode_frame(
        &self,
        src: &dyn PixelDataObject,
        frame: u32,
        _options: EncodeOptions,
        dst: &mut Vec<u8>,
    ) -> EncodeResult<Vec<AttributeOp>> {
        let cols = src
            .cols()
            .context(encode_error::MissingAttributeSnafu { name: "Columns" })?;
        let rows = src
            .rows()
            .context(encode_error::MissingAttributeSnafu { name: "Rows" })?;
        let samples_per_pixel =
            src.samples_per_pixel()
                .context(encode_error::MissingAttributeSnafu {
                    name: "SamplesPerPixel",
                })?;
        let bits_allocated = src
            .bits_allocated()
            .context(encode_error::MissingAttributeSnafu {
                name: "BitsAllocated",
            })?;

        let bytes_per_sample = (bits_allocated / 8) as usize;
        let frame_size =
            cols as usize * rows as usize * samples_per_pixel as usize * bytes_per_sample;

        // identify frame data using the frame index. Zero-copy - see decode_frame's doc comment
        // above for why this matters (called once per frame for a multi-frame series).
        let pixeldata_uncompressed = src
            .fragment(0)
            .context(encode_error::MissingAttributeSnafu { name: "Pixel Data" })?;

        let len_before = pixeldata_uncompressed.len();

        let frame_data = pixeldata_uncompressed
            .get(frame_size * frame as usize..frame_size * (frame as usize + 1))
            .whatever_context("Frame index out of bounds")?;

        // Copy the data to the output
        dst.extend_from_slice(frame_data);

        // provide attribute changes
        Ok(vec![
            // Encapsulated Pixel Data Value Total Length
            AttributeOp::new(
                Tag(0x7FE0, 0x0003),
                AttributeAction::Set(PrimitiveValue::from(len_before as u64)),
            ),
        ])
    }
}