fast image operations
Diffstat (limited to 'src/overlay.rs')
| -rw-r--r-- | src/overlay.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/overlay.rs b/src/overlay.rs index 8733f20..b21985d 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -2,7 +2,7 @@ // TODO Y/YA use crate::{cloner::ImageCloner, uninit}; -use super::{assert_unchecked, Image}; +use super::{Image, assert_unchecked}; use crate::pixels::Blend; use std::{mem::transmute, simd::prelude::*}; @@ -52,6 +52,14 @@ pub trait BlendingOverlay<W> { /// UB if a.width != b.width || a.height != b.height unsafe fn overlay_blended(&mut self, with: &W) -> &mut Self; } +/// Blending overlay at. +pub trait BlendingOverlayAt<W> { + /// See [BlendingOverlay::overlay_blended]. + /// # Safety + /// + /// UB if x, y is out of bounds + unsafe fn overlay_blended_at(&mut self, with: &W, x: u32, y: u32) -> &mut Self; +} /// [`Overlay`] but owned pub trait ClonerOverlay<const W: usize, const C: usize>: Sealed { @@ -136,6 +144,25 @@ where } } +impl<const A: usize, const B: usize, T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> + BlendingOverlayAt<Image<U, B>> for Image<T, A> +where + [u8; A]: Blend<B>, +{ + #[inline] + unsafe fn overlay_blended_at(&mut self, with: &Image<U, B>, x: u32, y: u32) -> &mut Self { + for j in 0..with.height() { + for i in 0..with.width() { + // SAFETY: i, j is in bounds. + let their_px = unsafe { &with.pixel(i, j) }; + let our_px = unsafe { self.pixel_mut(i + x, j + y) }; + our_px.blend(*their_px); + } + } + self + } +} + impl ClonerOverlay<4, 4> for ImageCloner<'_, 4> { #[inline] #[must_use = "function does not modify the original image"] |