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 LegacyRgbFrameError {
#[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 Rgb565Frame<'a> {
rgb565: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
impl<'a> Rgb565Frame<'a> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
rgb565: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, LegacyRgbFrameError> {
if width == 0 || height == 0 {
return Err(LegacyRgbFrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(2) {
Some(v) => v,
None => {
return Err(LegacyRgbFrameError::WidthOverflow(WidthOverflow::new(
width,
)));
}
};
if stride < min_stride {
return Err(LegacyRgbFrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as u64).checked_mul(height as u64) {
Some(v) if v <= usize::MAX as u64 => v as usize,
_ => {
return Err(LegacyRgbFrameError::GeometryOverflow(
GeometryOverflow::new(stride, height),
));
}
};
if rgb565.len() < plane_min {
return Err(LegacyRgbFrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, rgb565.len()),
));
}
Ok(Self {
rgb565,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(rgb565: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(rgb565, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid Rgb565Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn rgb565(&self) -> &'a [u8] {
self.rgb565
}
#[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
}
}
#[derive(Debug, Clone, Copy)]
pub struct Bgr565Frame<'a> {
bgr565: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
impl<'a> Bgr565Frame<'a> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
bgr565: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, LegacyRgbFrameError> {
if width == 0 || height == 0 {
return Err(LegacyRgbFrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(2) {
Some(v) => v,
None => {
return Err(LegacyRgbFrameError::WidthOverflow(WidthOverflow::new(
width,
)));
}
};
if stride < min_stride {
return Err(LegacyRgbFrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as u64).checked_mul(height as u64) {
Some(v) if v <= usize::MAX as u64 => v as usize,
_ => {
return Err(LegacyRgbFrameError::GeometryOverflow(
GeometryOverflow::new(stride, height),
));
}
};
if bgr565.len() < plane_min {
return Err(LegacyRgbFrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, bgr565.len()),
));
}
Ok(Self {
bgr565,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(bgr565: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(bgr565, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid Bgr565Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn bgr565(&self) -> &'a [u8] {
self.bgr565
}
#[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
}
}
#[derive(Debug, Clone, Copy)]
pub struct Rgb555Frame<'a> {
rgb555: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
impl<'a> Rgb555Frame<'a> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
rgb555: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, LegacyRgbFrameError> {
if width == 0 || height == 0 {
return Err(LegacyRgbFrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(2) {
Some(v) => v,
None => {
return Err(LegacyRgbFrameError::WidthOverflow(WidthOverflow::new(
width,
)));
}
};
if stride < min_stride {
return Err(LegacyRgbFrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as u64).checked_mul(height as u64) {
Some(v) if v <= usize::MAX as u64 => v as usize,
_ => {
return Err(LegacyRgbFrameError::GeometryOverflow(
GeometryOverflow::new(stride, height),
));
}
};
if rgb555.len() < plane_min {
return Err(LegacyRgbFrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, rgb555.len()),
));
}
Ok(Self {
rgb555,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(rgb555: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(rgb555, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid Rgb555Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn rgb555(&self) -> &'a [u8] {
self.rgb555
}
#[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
}
}
#[derive(Debug, Clone, Copy)]
pub struct Bgr555Frame<'a> {
bgr555: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
impl<'a> Bgr555Frame<'a> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
bgr555: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, LegacyRgbFrameError> {
if width == 0 || height == 0 {
return Err(LegacyRgbFrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(2) {
Some(v) => v,
None => {
return Err(LegacyRgbFrameError::WidthOverflow(WidthOverflow::new(
width,
)));
}
};
if stride < min_stride {
return Err(LegacyRgbFrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as u64).checked_mul(height as u64) {
Some(v) if v <= usize::MAX as u64 => v as usize,
_ => {
return Err(LegacyRgbFrameError::GeometryOverflow(
GeometryOverflow::new(stride, height),
));
}
};
if bgr555.len() < plane_min {
return Err(LegacyRgbFrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, bgr555.len()),
));
}
Ok(Self {
bgr555,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(bgr555: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(bgr555, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid Bgr555Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn bgr555(&self) -> &'a [u8] {
self.bgr555
}
#[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
}
}
#[derive(Debug, Clone, Copy)]
pub struct Rgb444Frame<'a> {
rgb444: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
impl<'a> Rgb444Frame<'a> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
rgb444: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, LegacyRgbFrameError> {
if width == 0 || height == 0 {
return Err(LegacyRgbFrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(2) {
Some(v) => v,
None => {
return Err(LegacyRgbFrameError::WidthOverflow(WidthOverflow::new(
width,
)));
}
};
if stride < min_stride {
return Err(LegacyRgbFrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as u64).checked_mul(height as u64) {
Some(v) if v <= usize::MAX as u64 => v as usize,
_ => {
return Err(LegacyRgbFrameError::GeometryOverflow(
GeometryOverflow::new(stride, height),
));
}
};
if rgb444.len() < plane_min {
return Err(LegacyRgbFrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, rgb444.len()),
));
}
Ok(Self {
rgb444,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(rgb444: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(rgb444, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid Rgb444Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn rgb444(&self) -> &'a [u8] {
self.rgb444
}
#[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
}
}
#[derive(Debug, Clone, Copy)]
pub struct Bgr444Frame<'a> {
bgr444: &'a [u8],
width: u32,
height: u32,
stride: u32,
}
impl<'a> Bgr444Frame<'a> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn try_new(
bgr444: &'a [u8],
width: u32,
height: u32,
stride: u32,
) -> Result<Self, LegacyRgbFrameError> {
if width == 0 || height == 0 {
return Err(LegacyRgbFrameError::ZeroDimension(ZeroDimension::new(
width, height,
)));
}
let min_stride = match width.checked_mul(2) {
Some(v) => v,
None => {
return Err(LegacyRgbFrameError::WidthOverflow(WidthOverflow::new(
width,
)));
}
};
if stride < min_stride {
return Err(LegacyRgbFrameError::InsufficientStride(
InsufficientStride::new(stride, min_stride),
));
}
let plane_min = match (stride as u64).checked_mul(height as u64) {
Some(v) if v <= usize::MAX as u64 => v as usize,
_ => {
return Err(LegacyRgbFrameError::GeometryOverflow(
GeometryOverflow::new(stride, height),
));
}
};
if bgr444.len() < plane_min {
return Err(LegacyRgbFrameError::InsufficientPlane(
InsufficientPlane::new(plane_min, bgr444.len()),
));
}
Ok(Self {
bgr444,
width,
height,
stride,
})
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn new(bgr444: &'a [u8], width: u32, height: u32, stride: u32) -> Self {
match Self::try_new(bgr444, width, height, stride) {
Ok(frame) => frame,
Err(_) => panic!("invalid Bgr444Frame dimensions or plane length"),
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn bgr444(&self) -> &'a [u8] {
self.bgr444
}
#[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
}
}