use crate::sys::{PF_LayerDef, PF_Pixel};
use super::{Depth8, Depth16, Depth32, Pixel, PixelDepth, PixelDepthKind};
use core::ops::{Index, IndexMut};
use std::ptr::null_mut;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LayerError {
DimensionMismatch {
expected: usize,
actual: usize,
},
TooLarge {
width: u32,
height: u32,
},
}
impl core::fmt::Display for LayerError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::DimensionMismatch { expected, actual } => write!(
f,
"Pixel data length ({}) does not match layer dimensions ({}).",
actual, expected
),
Self::TooLarge { width, height } => write!(f, "Layer dimensions {width}x{height} are too large."),
}
}
}
impl std::error::Error for LayerError {}
fn pixel_count<D: PixelDepth>(width: u32, height: u32) -> Option<usize> {
let row_bytes = (width as usize).checked_mul(core::mem::size_of::<Pixel<D>>())?;
i32::try_from(row_bytes).ok()?;
i32::try_from(height).ok()?;
(width as usize).checked_mul(height as usize)
}
fn pixel_count_or_panic<D: PixelDepth>(width: u32, height: u32) -> usize {
pixel_count::<D>(width, height).unwrap_or_else(|| panic!("{}", LayerError::TooLarge { width, height }))
}
#[derive(Clone)]
pub struct Layer<D: PixelDepth> {
width: u32,
height: u32,
pixels: Vec<Pixel<D>>,
}
impl<D> Layer<D>
where
D: PixelDepth,
{
pub fn new(width: u32, height: u32, pixels: Vec<Pixel<D>>) -> Result<Self, LayerError> {
let expected = pixel_count::<D>(width, height).ok_or(LayerError::TooLarge { width, height })?;
if pixels.len() != expected {
return Err(LayerError::DimensionMismatch {
expected,
actual: pixels.len(),
});
}
Ok(Self { width, height, pixels })
}
pub fn blank(width: u32, height: u32) -> Self {
let pixel_count = pixel_count_or_panic::<D>(width, height);
let pixels = vec![<Pixel<D>>::blank(); pixel_count];
Self { width, height, pixels }
}
pub fn black(width: u32, height: u32) -> Self {
let pixel_count = pixel_count_or_panic::<D>(width, height);
let pixels = vec![<Pixel<D>>::black(); pixel_count];
Self { width, height, pixels }
}
pub fn from_fn(width: u32, height: u32, mut f: impl FnMut(u32, u32) -> Pixel<D>) -> Self {
let mut pixels = Vec::with_capacity(pixel_count_or_panic::<D>(width, height));
for y in 0..height {
for x in 0..width {
pixels.push(f(x, y));
}
}
Self { width, height, pixels }
}
pub fn convert<E: PixelDepth>(&self) -> Layer<E> {
Layer {
width: self.width,
height: self.height,
pixels: self.pixels.iter().map(|p| Pixel::<E>::from_unit(p.to_unit())).collect(),
}
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn len(&self) -> usize {
self.pixels.len()
}
pub fn is_empty(&self) -> bool {
self.pixels.is_empty()
}
pub fn pixels(&self) -> &[Pixel<D>] {
&self.pixels
}
pub fn pixels_mut(&mut self) -> &mut [Pixel<D>] {
&mut self.pixels
}
pub fn get_linear(&self, index: usize) -> Option<&Pixel<D>> {
self.pixels.get(index)
}
pub fn get_linear_mut(&mut self, index: usize) -> Option<&mut Pixel<D>> {
self.pixels.get_mut(index)
}
pub fn get(&self, x: u32, y: u32) -> Option<&Pixel<D>> {
self.index_of(x, y).and_then(|idx| self.pixels.get(idx))
}
pub fn get_mut(&mut self, x: u32, y: u32) -> Option<&mut Pixel<D>> {
self.index_of(x, y).and_then(|idx| self.pixels.get_mut(idx))
}
fn index_of(&self, x: u32, y: u32) -> Option<usize> {
(x < self.width && y < self.height).then(|| y as usize * self.width as usize + x as usize)
}
pub fn iter(&self) -> core::slice::Iter<'_, Pixel<D>> {
self.pixels.iter()
}
pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Pixel<D>> {
self.pixels.iter_mut()
}
pub fn as_sys(&mut self) -> PF_LayerDef {
let data = self.pixels.as_mut_ptr() as *mut PF_Pixel;
PF_LayerDef {
reserved0: null_mut(),
reserved1: null_mut(),
world_flags: if D::KIND == PixelDepthKind::U16 {
crate::sys::PF_WorldFlag_DEEP as crate::sys::PF_WorldFlags
} else {
0
},
width: self.width as i32,
height: self.height as i32,
extent_hint: crate::sys::PF_UnionableRect {
left: 0,
top: 0,
right: self.width as i32,
bottom: self.height as i32,
},
platform_ref: null_mut(),
reserved_long1: 0,
reserved_long4: null_mut(),
pix_aspect_ratio: crate::sys::PF_RationalScale { num: 1, den: 1 },
reserved_long2: null_mut(),
origin_x: 0,
origin_y: 0,
reserved_long3: 0,
dephault: 0,
data,
rowbytes: (self.width as usize * std::mem::size_of::<Pixel<D>>()) as i32,
}
}
}
impl Layer<Depth8> {
pub fn from_rgba8(pixels: Vec<u8>, width: u32, height: u32) -> Result<Self, LayerError> {
let expected = pixel_count::<Depth8>(width, height)
.and_then(|count| count.checked_mul(4))
.ok_or(LayerError::TooLarge { width, height })?;
if pixels.len() != expected {
return Err(LayerError::DimensionMismatch {
expected,
actual: pixels.len(),
});
}
let converted = pixels
.as_chunks::<4>()
.0
.iter()
.map(|&rgba| Pixel::from_rgba(rgba))
.collect();
Layer::new(width, height, converted)
}
pub fn write_rgba8(&self, buffer: &mut [u8]) -> Result<(), LayerError> {
let required = self.pixels.len() * 4;
if buffer.len() != required {
return Err(LayerError::DimensionMismatch {
expected: required,
actual: buffer.len(),
});
}
for (chunk, pixel) in buffer.as_chunks_mut::<4>().0.iter_mut().zip(self.pixels.iter()) {
*chunk = pixel.to_rgba();
}
Ok(())
}
}
#[derive(Clone)]
pub enum AnyLayer {
U8(Layer<Depth8>),
U16(Layer<Depth16>),
F32(Layer<Depth32>),
}
macro_rules! with_layer {
($any:expr, $layer:ident => $body:expr) => {
match $any {
AnyLayer::U8($layer) => $body,
AnyLayer::U16($layer) => $body,
AnyLayer::F32($layer) => $body,
}
};
}
impl AnyLayer {
pub fn new<D: PixelDepth>(layer: Layer<D>) -> Self {
D::wrap(layer)
}
pub fn filled(kind: PixelDepthKind, width: u32, height: u32, rgba: [f32; 4]) -> Self {
fn fill<D: PixelDepth>(width: u32, height: u32, rgba: [f32; 4]) -> AnyLayer {
let count = pixel_count_or_panic::<D>(width, height);
AnyLayer::new(Layer::<D> {
width,
height,
pixels: vec![Pixel::<D>::from_unit(rgba); count],
})
}
match kind {
PixelDepthKind::U8 => fill::<Depth8>(width, height, rgba),
PixelDepthKind::U16 => fill::<Depth16>(width, height, rgba),
PixelDepthKind::F32 => fill::<Depth32>(width, height, rgba),
}
}
pub fn kind(&self) -> PixelDepthKind {
match self {
Self::U8(_) => PixelDepthKind::U8,
Self::U16(_) => PixelDepthKind::U16,
Self::F32(_) => PixelDepthKind::F32,
}
}
pub fn width(&self) -> u32 {
with_layer!(self, l => l.width())
}
pub fn len(&self) -> usize {
with_layer!(self, l => l.len())
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn height(&self) -> u32 {
with_layer!(self, l => l.height())
}
pub fn get<D: PixelDepth>(&self) -> Option<&Layer<D>> {
D::unwrap_ref(self)
}
pub fn get_mut<D: PixelDepth>(&mut self) -> Option<&mut Layer<D>> {
D::unwrap_mut(self)
}
pub fn as_sys(&mut self) -> PF_LayerDef {
with_layer!(self, l => l.as_sys())
}
pub fn data_ptr(&self) -> *const u8 {
with_layer!(self, l => l.pixels().as_ptr() as *const u8)
}
pub fn data_mut_ptr(&mut self) -> *mut u8 {
with_layer!(self, l => l.pixels_mut().as_mut_ptr() as *mut u8)
}
pub fn unit_rgba(&self, index: usize) -> Option<[f32; 4]> {
with_layer!(self, l => l.get_linear(index).map(Pixel::to_unit))
}
pub fn to_unit_rgba(&self) -> Vec<[f32; 4]> {
with_layer!(self, l => l.iter().map(Pixel::to_unit).collect())
}
pub fn set_from_unit_rgba(&mut self, values: impl IntoIterator<Item = [f32; 4]>) {
fn fill<D: PixelDepth>(l: &mut Layer<D>, values: impl IntoIterator<Item = [f32; 4]>) {
for (pixel, rgba) in l.iter_mut().zip(values) {
*pixel = Pixel::from_unit(rgba);
}
}
with_layer!(self, l => fill(l, values))
}
pub fn convert(&self, kind: PixelDepthKind) -> AnyLayer {
fn to<E: PixelDepth>(layer: &AnyLayer) -> AnyLayer {
AnyLayer::new(with_layer!(layer, l => l.convert::<E>()))
}
match kind {
PixelDepthKind::U8 => to::<Depth8>(self),
PixelDepthKind::U16 => to::<Depth16>(self),
PixelDepthKind::F32 => to::<Depth32>(self),
}
}
pub fn write_rgba8(&self, buffer: &mut [u8]) -> Result<(), LayerError> {
if let Self::U8(layer) = self {
return layer.write_rgba8(buffer);
}
let required = self.len() * 4;
if buffer.len() != required {
return Err(LayerError::DimensionMismatch {
expected: required,
actual: buffer.len(),
});
}
fn write<D: PixelDepth>(l: &Layer<D>, buffer: &mut [u8]) {
for (chunk, pixel) in buffer.as_chunks_mut::<4>().0.iter_mut().zip(l.iter()) {
*chunk = pixel.to_unit().map(Depth8::from_unit);
}
}
with_layer!(self, l => write(l, buffer));
Ok(())
}
}
impl<D: PixelDepth> From<Layer<D>> for AnyLayer {
fn from(layer: Layer<D>) -> Self {
Self::new(layer)
}
}
impl core::fmt::Debug for AnyLayer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("AnyLayer")
.field("kind", &self.kind())
.field("width", &self.width())
.field("height", &self.height())
.finish_non_exhaustive()
}
}
impl<D: PixelDepth> core::fmt::Debug for Layer<D> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Layer")
.field("width", &self.width)
.field("height", &self.height)
.field("pixels", &self.pixels)
.finish()
}
}
impl<D: PixelDepth> Index<(u32, u32)> for Layer<D> {
type Output = Pixel<D>;
fn index(&self, index: (u32, u32)) -> &Self::Output {
let (x, y) = index;
assert!(x < self.width, "X coordinate out of bounds.");
assert!(y < self.height, "Y coordinate out of bounds.");
&self.pixels[y as usize * self.width as usize + x as usize]
}
}
impl<D: PixelDepth> IndexMut<(u32, u32)> for Layer<D> {
fn index_mut(&mut self, index: (u32, u32)) -> &mut Self::Output {
let (x, y) = index;
assert!(x < self.width, "X coordinate out of bounds.");
assert!(y < self.height, "Y coordinate out of bounds.");
&mut self.pixels[y as usize * self.width as usize + x as usize]
}
}