use pixel_formats::r8g8b8a8_Srgb;
#[inline]
#[must_use]
pub const fn xy_width_to_index(x: u32, y: u32, width: u32) -> usize {
y.wrapping_mul(width).wrapping_add(x) as usize
}
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BorrowedBitmap<'a, P = r8g8b8a8_Srgb> {
pub width: u32,
pub height: u32,
pub pixels: &'a mut [P],
}
impl<'a, P> BorrowedBitmap<'a, 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) {
let num_pixels = self.width.wrapping_mul(self.height) as usize;
if let Some(mut data) = self.pixels.get_mut(..num_pixels) {
let mut temp_height = self.height;
while temp_height > 1 {
let (low, mid) = data.split_at_mut(self.width as usize);
let (mid, high) = mid.split_at_mut(mid.len() - self.width as usize);
low.swap_with_slice(high);
data = mid;
temp_height -= 2;
}
}
}
}