use dicom_core::{ops::AttributeOp, value::C};
use snafu::Snafu;
use std::borrow::Cow;
#[derive(Debug, Snafu)]
#[non_exhaustive]
#[snafu(visibility(pub), module)]
pub enum DecodeError {
#[snafu(whatever, display("{}", message))]
Custom {
message: String,
#[snafu(source(from(Box<dyn std::error::Error + Send + Sync + 'static>, Some)))]
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
},
NotEncapsulated,
FrameRangeOutOfBounds,
#[snafu(display("Missing required attribute `{}`", name))]
MissingAttribute { name: &'static str },
}
#[derive(Debug, Snafu)]
#[non_exhaustive]
#[snafu(visibility(pub), module)]
pub enum EncodeError {
#[snafu(whatever, display("{}", message))]
Custom {
message: String,
#[snafu(source(from(Box<dyn std::error::Error + Send + Sync + 'static>, Some)))]
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
},
NotNative,
FrameRangeOutOfBounds,
#[snafu(display("Missing required attribute `{}`", name))]
MissingAttribute { name: &'static str },
}
pub type DecodeResult<T, E = DecodeError> = Result<T, E>;
pub type EncodeResult<T, E = EncodeError> = Result<T, E>;
#[derive(Debug)]
pub struct RawPixelData {
pub fragments: C<Vec<u8>>,
pub offset_table: C<u32>,
}
pub trait PixelDataObject {
fn transfer_syntax_uid(&self) -> &str;
fn rows(&self) -> Option<u16>;
fn cols(&self) -> Option<u16>;
fn samples_per_pixel(&self) -> Option<u16>;
fn bits_allocated(&self) -> Option<u16>;
fn bits_stored(&self) -> Option<u16>;
fn photometric_interpretation(&self) -> Option<&str>;
fn number_of_frames(&self) -> Option<u32>;
fn number_of_fragments(&self) -> Option<u32>;
fn fragment(&self, fragment: usize) -> Option<Cow<'_, [u8]>>;
fn offset_table(&self) -> Option<Cow<'_, [u32]>>;
fn raw_pixel_data(&self) -> Option<RawPixelData>;
}
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct EncodeOptions {
pub quality: Option<u8>,
pub effort: Option<u8>,
}
impl EncodeOptions {
pub fn new() -> Self {
Self::default()
}
}
pub trait PixelDataReader {
fn decode(&self, src: &dyn PixelDataObject, dst: &mut Vec<u8>) -> DecodeResult<()> {
let frames = src.number_of_frames().unwrap_or(1);
for frame in 0..frames {
self.decode_frame(src, frame, dst)?;
}
Ok(())
}
fn decode_frame(
&self,
src: &dyn PixelDataObject,
frame: u32,
dst: &mut Vec<u8>,
) -> DecodeResult<()>;
}
pub trait PixelDataWriter {
fn encode(
&self,
src: &dyn PixelDataObject,
options: EncodeOptions,
dst: &mut Vec<Vec<u8>>,
offset_table: &mut Vec<u32>,
) -> EncodeResult<Vec<AttributeOp>> {
let frames = src.number_of_frames().unwrap_or(1);
let mut out = Vec::new();
for frame in 0..frames {
let mut frame_data = Vec::new();
out = self.encode_frame(src, frame, options.clone(), &mut frame_data)?;
offset_table.push(frame_data.len() as u32 + 8 * (frame + 1));
dst.push(frame_data);
}
Ok(out)
}
fn encode_frame(
&self,
src: &dyn PixelDataObject,
frame: u32,
options: EncodeOptions,
dst: &mut Vec<u8>,
) -> EncodeResult<Vec<AttributeOp>>;
}
pub type DynPixelDataReader = Box<dyn PixelDataReader + Send + Sync + 'static>;
pub type DynPixelDataWriter = Box<dyn PixelDataWriter + Send + Sync + 'static>;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum NeverPixelAdapter {}
impl PixelDataReader for NeverPixelAdapter {
fn decode(&self, _src: &dyn PixelDataObject, _dst: &mut Vec<u8>) -> DecodeResult<()> {
unreachable!()
}
fn decode_frame(
&self,
_src: &dyn PixelDataObject,
_frame: u32,
_dst: &mut Vec<u8>,
) -> DecodeResult<()> {
unreachable!()
}
}
impl PixelDataWriter for NeverPixelAdapter {
fn encode(
&self,
_src: &dyn PixelDataObject,
_options: EncodeOptions,
_dst: &mut Vec<Vec<u8>>,
_offset_table: &mut Vec<u32>,
) -> EncodeResult<Vec<AttributeOp>> {
unreachable!()
}
fn encode_frame(
&self,
_src: &dyn PixelDataObject,
_frame: u32,
_options: EncodeOptions,
_dst: &mut Vec<u8>,
) -> EncodeResult<Vec<AttributeOp>> {
unreachable!()
}
}
impl PixelDataReader for crate::transfer_syntax::NeverAdapter {
fn decode(&self, _src: &dyn PixelDataObject, _dst: &mut Vec<u8>) -> DecodeResult<()> {
unreachable!()
}
fn decode_frame(
&self,
_src: &dyn PixelDataObject,
_frame: u32,
_dst: &mut Vec<u8>,
) -> DecodeResult<()> {
unreachable!()
}
}
impl PixelDataWriter for crate::transfer_syntax::NeverAdapter {
fn encode(
&self,
_src: &dyn PixelDataObject,
_options: EncodeOptions,
_dst: &mut Vec<Vec<u8>>,
_offset_table: &mut Vec<u32>,
) -> EncodeResult<Vec<AttributeOp>> {
unreachable!()
}
fn encode_frame(
&self,
_src: &dyn PixelDataObject,
_frame: u32,
_options: EncodeOptions,
_dst: &mut Vec<u8>,
) -> EncodeResult<Vec<AttributeOp>> {
unreachable!()
}
}