This commit is contained in:
bendn 2024-12-14 09:51:02 +07:00
parent dff5b4e933
commit 3e3ca7b2ee
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6

View file

@ -536,20 +536,46 @@ impl<T, const CHANNELS: usize> Image<T, CHANNELS> {
unsafe { *ptr }
}
/// Returns a [`PixelEntry`]
pub fn replace<U: Copy>(
&mut self,
x: u32,
y: u32,
f: impl FnOnce([U; CHANNELS]) -> [U; CHANNELS],
) -> Option<[U; CHANNELS]>
where
T: AsRef<[U]> + AsMut<[U]>,
{
if x < self.width() && y < self.height() {
let x = unsafe { self.pixel_mut(x, y) };
let v = *x;
*x = f(v);
Some(v)
} else {
None
}
}
/// Return a mutable reference to a pixel at (x, y).
/// # Safety
///
/// - UB if x, y is out of bounds
/// - UB if buffer is too small
#[inline]
pub unsafe fn pixel_mut<U: Copy>(&mut self, x: u32, y: u32) -> &mut [U]
pub unsafe fn pixel_mut<U: Copy>(&mut self, x: u32, y: u32) -> &mut [U; CHANNELS]
where
T: AsMut<[U]> + AsRef<[U]>,
{
// SAFETY: we have been told x, y is in bounds.
let idx = self.slice(x, y);
// SAFETY: slice should always return a valid index
unsafe { self.buffer.as_mut().get_unchecked_mut(idx) }
unsafe {
self.buffer
.as_mut()
.get_unchecked_mut(idx)
.try_into()
.unwrap_unchecked()
}
}
/// iterator over columns