fast image operations
window
| -rw-r--r-- | src/lib.rs | 1 | ||||
| -rw-r--r-- | src/overlay.rs | 23 | ||||
| -rw-r--r-- | src/term.rs | 9 |
3 files changed, 30 insertions, 3 deletions
@@ -121,6 +121,7 @@ pub use cloner::ImageCloner; pub use r#dyn::DynImage; pub use overlay::{ BlendingOverlay, BlendingOverlayAt, ClonerOverlay, ClonerOverlayAt, Overlay, OverlayAt, + OverlayAtClipping, }; trait CopyWithinUnchecked { diff --git a/src/overlay.rs b/src/overlay.rs index f57b216..da70713 100644 --- a/src/overlay.rs +++ b/src/overlay.rs @@ -15,6 +15,12 @@ pub trait OverlayAt<W> { unsafe fn overlay_at(&mut self, with: &W, x: u32, y: u32) -> &mut Self; } +/// Useful for debugging, sometimes. +#[doc(hidden)] +pub trait OverlayAtClipping<W> { + fn clipping_overlay_at(&mut self, with: &W, x: u32, y: u32) -> &mut Self; +} + /// Sealant module mod sealed { /// Seals the cloner traits @@ -378,6 +384,23 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> OverlayAt<Image<U, 2>> for Im } } +impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> OverlayAtClipping<Image<U, 3>> for Image<T, 3> { + #[inline] + #[cfg_attr(debug_assertions, track_caller)] + fn clipping_overlay_at(&mut self, with: &Image<U, 3>, x: u32, y: u32) -> &mut Self { + for j in 0..with.height() { + for i in 0..with.width() { + // SAFETY: i, j is in bounds. + if let Some(their_px) = with.get_pixel(i, j) + && let Some(our_px) = self.get_pixel_mut(i + x, j + y) + { + our_px.copy_from_slice(&their_px); + } + } + } + self + } +} impl<T: AsMut<[u8]> + AsRef<[u8]>, U: AsRef<[u8]>> OverlayAt<Image<U, 3>> for Image<T, 4> { #[inline] #[cfg_attr(debug_assertions, track_caller)] diff --git a/src/term.rs b/src/term.rs index c211340..aeae9fb 100644 --- a/src/term.rs +++ b/src/term.rs @@ -112,7 +112,7 @@ where } #[cfg(unix)] - // https://github.com/benjajaja/ratatui-image/blob/master/src/picker.rs#L226 + // https://github.com/benjajaja/ratatui-image/blob/eeb2a1b26fbe360259f213d6d5eb5449c8ae1d6e/src/picker.rs#L226 fn guess_harder(&self, to: &mut impl Write) -> Option<Result> { // contains a kitty gfx and sixel query, the `\x1b[c` is for sixels let buf = query(r"_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\[c")?; @@ -129,7 +129,10 @@ where } } } - +#[cfg(not(unix))] +fn query(device_query_code: &'static str) -> Option<String> { + None +} #[cfg(unix)] // https://github.com/benjajaja/ratatui-image/blob/master/src/picker.rs#L226 fn query(device_query_code: &'static str) -> Option<String> { @@ -164,7 +167,7 @@ fn query(device_query_code: &'static str) -> Option<String> { let mut buf = Vec::new(); let mut tmp = [0; 1 << 5]; loop { - let mut x = std::mem::zeroed::<libc::fd_set>(); + let mut x: libc::fd_set = std::mem::zeroed::<libc::fd_set>(); libc::FD_SET(0, &mut x); match libc::select( 1, |