use super::{
GeometryOverflow, InsufficientPlane, InsufficientStride, WidthOverflow, ZeroDimension,
};
use derive_more::{IsVariant, TryUnwrap, Unwrap};
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum X2Rgb10FrameError {
#[error(transparent)]
ZeroDimension(ZeroDimension),
#[error(transparent)]
InsufficientStride(InsufficientStride),
#[error(transparent)]
InsufficientPlane(InsufficientPlane),
#[error(transparent)]
GeometryOverflow(GeometryOverflow),
#[error(transparent)]
WidthOverflow(WidthOverflow),
}
#[derive(Debug, Clone, Copy)]
pub struct X2Rgb10Frame<'a, const BE: bool = false> {
x2rgb10: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
pub type X2Rgb10LeFrame<'a> = X2Rgb10Frame<'a, false>;
pub type X2Rgb10BeFrame<'a> = X2Rgb10Frame<'a, true>;
impl<'a, const BE: bool> X2Rgb10Frame<'a, BE> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
x2rgb10: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, X2Rgb10FrameError> {
if width == 0 || height == 0 {
return Err(X2Rgb10FrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(4) {
Some(v) => v,
None => return Err(X2Rgb10FrameError::WidthOverflow(WidthOverflow::new(width))),
};
if stride < min_stride {
return Err(X2Rgb10FrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as usize).checked_mul(height as usize) {
Some(v) => v,
None => {
return Err(X2Rgb10FrameError::GeometryOverflow(GeometryOverflow::new(
stride, height,
)));
}
};
if x2rgb10.len() < plane_min {
return Err(X2Rgb10FrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, x2rgb10.len()),
));
}
Ok(Self {
x2rgb10,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(x2rgb10: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(x2rgb10, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid X2Rgb10Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn x2rgb10(&self) -> &'a [u8] {
self.x2rgb10
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn width(&self) -> u32 {
self.width
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn height(&self) -> u32 {
self.height
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn stride(&self) -> u32 {
self.stride
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn is_be(&self) -> bool {
BE
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IsVariant, TryUnwrap, Unwrap, Error)]
#[non_exhaustive]
#[unwrap(ref, ref_mut)]
#[try_unwrap(ref, ref_mut)]
pub enum X2Bgr10FrameError {
#[error(transparent)]
ZeroDimension(ZeroDimension),
#[error(transparent)]
InsufficientStride(InsufficientStride),
#[error(transparent)]
InsufficientPlane(InsufficientPlane),
#[error(transparent)]
GeometryOverflow(GeometryOverflow),
#[error(transparent)]
WidthOverflow(WidthOverflow),
}
#[derive(Debug, Clone, Copy)]
pub struct X2Bgr10Frame<'a, const BE: bool = false> {
x2bgr10: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
pub type X2Bgr10LeFrame<'a> = X2Bgr10Frame<'a, false>;
pub type X2Bgr10BeFrame<'a> = X2Bgr10Frame<'a, true>;
impl<'a, const BE: bool> X2Bgr10Frame<'a, BE> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
x2bgr10: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, X2Bgr10FrameError> {
if width == 0 || height == 0 {
return Err(X2Bgr10FrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(4) {
Some(v) => v,
None => return Err(X2Bgr10FrameError::WidthOverflow(WidthOverflow::new(width))),
};
if stride < min_stride {
return Err(X2Bgr10FrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as usize).checked_mul(height as usize) {
Some(v) => v,
None => {
return Err(X2Bgr10FrameError::GeometryOverflow(GeometryOverflow::new(
stride, height,
)));
}
};
if x2bgr10.len() < plane_min {
return Err(X2Bgr10FrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, x2bgr10.len()),
));
}
Ok(Self {
x2bgr10,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(x2bgr10: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(x2bgr10, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid X2Bgr10Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn x2bgr10(&self) -> &'a [u8] {
self.x2bgr10
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn width(&self) -> u32 {
self.width
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn height(&self) -> u32 {
self.height
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn stride(&self) -> u32 {
self.stride
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn is_be(&self) -> bool {
BE
}
}