diff --git a/src/overlay.rs b/src/overlay.rs index d364a76..8561ab5 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -129,20 +129,16 @@ impl + AsRef<[u8]>, U: AsRef<[u8]>> Overlay> for Imag } } -impl BlendingOverlay> for Image<&mut [u8], 4> { +impl + AsRef<[u8]>, U: AsRef<[u8]>> + BlendingOverlay> for Image +where + [u8; A]: Blend, +{ #[inline] - unsafe fn overlay_blended(&mut self, with: &Image<&[u8], 4>) -> &mut Self { + unsafe fn overlay_blended(&mut self, with: &Image) -> &mut Self { debug_assert!(self.width() == with.width()); debug_assert!(self.height() == with.height()); - for (i, other_pixels) in with.chunked().enumerate() { - // SAFETY: caller assures us, all is well. - let own_pixels = unsafe { - &mut *(self - .buffer - .as_mut() - .get_unchecked_mut(i * 4..i * 4 + 4) - .as_mut_ptr() as *mut [u8; 4]) - }; + for (other_pixels, own_pixels) in with.chunked().zip(self.chunked_mut()) { own_pixels.blend(*other_pixels); } self @@ -259,28 +255,6 @@ impl + AsRef<[u8]>, U: AsRef<[u8]>> Overlay> for Imag } } -impl + AsRef<[u8]>, U: AsRef<[u8]>> BlendingOverlay> for Image { - #[inline] - unsafe fn overlay_blended(&mut self, with: &Image) -> &mut Self { - debug_assert!(self.width() == with.width()); - debug_assert!(self.height() == with.height()); - for (i, other_pixels) in with.chunked().enumerate() { - // SAFETY: caller assures us, all is well. - let [r, g, b] = unsafe { - &mut *(self - .buffer - .as_mut() - .get_unchecked_mut(i * 3..i * 3 + 3) - .as_mut_ptr() as *mut [u8; 3]) - }; - let mut us = [*r, *g, *b, 255]; - us.blend(*other_pixels); - (*r, *g, *b) = (us[0], us[1], us[2]); - } - self - } -} - impl ClonerOverlay<4, 3> for ImageCloner<'_, 3> { #[inline] #[must_use = "function does not modify the original image"] diff --git a/src/pixels/blending.rs b/src/pixels/blending.rs index 17ce141..a65861e 100644 --- a/src/pixels/blending.rs +++ b/src/pixels/blending.rs @@ -1,5 +1,5 @@ //! module for pixel blending ops -use super::{unfloat, Floatify, PMap, Trunc, Unfloatify}; +use super::{convert::PFrom, unfloat, Floatify, PMap, Trunc, Unfloatify}; use umath::FF32; /// Trait for blending pixels together. @@ -41,6 +41,14 @@ impl Blend<3> for [u8; 3] { } } +impl Blend<4> for [u8; 3] { + fn blend(&mut self, with: [u8; 4]) { + let mut us: [u8; 4] = PFrom::pfrom(*self); + us.blend(with); + *self = PFrom::pfrom(us); + } +} + impl Blend<2> for [u8; 2] { fn blend(&mut self, with: [u8; 2]) { let bg = self.float();