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 Rgb96FrameError {
#[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 Rgb96Frame<'a, const BE: bool = false> {
rgb96: &'a [u32],
width: u32,
height: u32,
stride: u32,
}
pub type Rgb96LeFrame<'a> = Rgb96Frame<'a, false>;
pub type Rgb96BeFrame<'a> = Rgb96Frame<'a, true>;
impl<'a, const BE: bool> Rgb96Frame<'a, BE> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
rgb96: &'a [u32],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, Rgb96FrameError> {
if width == 0 || height == 0 {
return Err(Rgb96FrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(3) {
Some(v) => v,
None => return Err(Rgb96FrameError::WidthOverflow(WidthOverflow::new(width))),
};
if stride < min_stride {
return Err(Rgb96FrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as usize).checked_mul(height as usize) {
Some(v) => v,
None => {
return Err(Rgb96FrameError::GeometryOverflow(GeometryOverflow::new(
stride, height,
)));
}
};
if rgb96.len() < plane_min {
return Err(Rgb96FrameError::InsufficientPlane(InsufficientPlane::new(
plane_min,
rgb96.len(),
)));
}
Ok(Self {
rgb96,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(rgb96: &'a [u32], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(rgb96, width, height, stride) {
Ok(f) => f,
Err(_) => panic!("invalid Rgb96Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn rgb96(&self) -> &'a [u32] {
self.rgb96
}
#[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 Rgba128FrameError {
#[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 Rgba128Frame<'a, const BE: bool = false> {
rgba128: &'a [u32],
width: u32,
height: u32,
stride: u32,
}
pub type Rgba128LeFrame<'a> = Rgba128Frame<'a, false>;
pub type Rgba128BeFrame<'a> = Rgba128Frame<'a, true>;
impl<'a, const BE: bool> Rgba128Frame<'a, BE> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
rgba128: &'a [u32],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, Rgba128FrameError> {
if width == 0 || height == 0 {
return Err(Rgba128FrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(4) {
Some(v) => v,
None => return Err(Rgba128FrameError::WidthOverflow(WidthOverflow::new(width))),
};
if stride < min_stride {
return Err(Rgba128FrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as usize).checked_mul(height as usize) {
Some(v) => v,
None => {
return Err(Rgba128FrameError::GeometryOverflow(GeometryOverflow::new(
stride, height,
)));
}
};
if rgba128.len() < plane_min {
return Err(Rgba128FrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, rgba128.len()),
));
}
Ok(Self {
rgba128,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(rgba128: &'a [u32], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(rgba128, width, height, stride) {
Ok(f) => f,
Err(_) => panic!("invalid Rgba128Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn rgba128(&self) -> &'a [u32] {
self.rgba128
}
#[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
}
}