use crate::borrowed_bitmap::*;
use pixel_formats::r8g8b8a8_Srgb;
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
pub struct Bitmap<P = r8g8b8a8_Srgb> {
pub width: u32,
pub height: u32,
pub pixels: alloc::vec::Vec<P>,
}
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
impl<P> Bitmap<P> {
#[inline]
#[must_use]
pub fn get_mut(&mut self, x: u32, y: u32) -> Option<&mut P> {
if x < self.width && y < self.height {
let i = xy_width_to_index(x, y, self.width);
self.pixels.get_mut(i)
} else {
None
}
}
#[inline]
pub fn vertical_flip(&mut self) {
BorrowedBitmap { width: self.width, height: self.height, pixels: &mut self.pixels }
.vertical_flip()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
pub struct Palmap<I = u8, P = r8g8b8a8_Srgb> {
pub width: u32,
pub height: u32,
pub indexes: alloc::vec::Vec<I>,
pub palette: alloc::vec::Vec<P>,
}
#[cfg(feature = "alloc")]
#[cfg_attr(docs_rs, doc(cfg(feature = "alloc")))]
impl<I, P> Palmap<I, P> {
#[inline]
#[must_use]
pub fn get_mut(&mut self, x: u32, y: u32) -> Option<&mut I> {
if x < self.width && y < self.height {
let i = xy_width_to_index(x, y, self.width);
Some(&mut self.indexes[i])
} else {
None
}
}
#[inline]
pub fn vertical_flip(&mut self) {
BorrowedBitmap { width: self.width, height: self.height, pixels: &mut self.indexes }
.vertical_flip()
}
}
impl<I, PxIn, PxOut> From<&Palmap<I, PxIn>> for Bitmap<PxOut>
where
usize: From<I>,
I: Clone,
PxOut: From<PxIn>,
PxIn: Clone + Default,
{
#[inline]
fn from(palmap: &Palmap<I, PxIn>) -> Self {
let pixels: alloc::vec::Vec<PxOut> = palmap
.indexes
.iter()
.cloned()
.map(|i| PxOut::from(palmap.palette.get(usize::from(i)).cloned().unwrap_or_default()))
.collect();
Bitmap { width: palmap.width, height: palmap.height, pixels }
}
}