From 8649f3c2a2b669be6073dc060be2f971ca3d16bf Mon Sep 17 00:00:00 2001 From: bendn Date: Mon, 20 May 2024 08:31:33 +0700 Subject: [PATCH] add row and col iters (#3) --- src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ src/pixels/wam.rs | 2 +- src/term/iterm2.rs | 1 - src/term/kitty.rs | 1 - 4 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 33a27f7..388c82b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -552,6 +552,72 @@ impl Image { // SAFETY: slice should always return a valid index unsafe { self.buffer.as_mut().get_unchecked_mut(idx) } } + + /// iterator over columns + /// returned iterator returns a iterator for each column + /// + /// ```text + /// ┌ ┐┌ ┐┌ ┐ + /// │1││2││3│ + /// │4││5││6│ + /// │7││8││9│ + /// └ ┘└ ┘└ ┘ + /// ``` + /// + /// ``` + /// # use fimg::Image; + /// let img: Image<&[u8],1> = Image::build(2, 3).buf(&[ + /// 1, 5, + /// 2, 4, + /// 7, 9 + /// ]); + /// assert_eq!( + /// img.cols().map(|x| x.collect::>()).collect::>(), + /// [[[1], [2], [7]], [[5], [4], [9]]] + /// ); + /// ``` + #[must_use = "iterators are lazy and do nothing unless consumed"] + pub fn cols( + &self, + ) -> impl DoubleEndedIterator + + ExactSizeIterator< + Item = impl ExactSizeIterator + DoubleEndedIterator + '_, + > + where + T: AsRef<[U]>, + { + (0..self.width()).map(move |x| (0..self.height()).map(move |y| unsafe { self.pixel(x, y) })) + } + + /// iterator over rows + /// returns a iterator over each row + /// ```text + /// [ 1 2 3 ] + /// [ 4 5 6 ] + /// [ 7 8 9 ] + /// ``` + /// + /// ``` + /// # use fimg::Image; + /// let img: Image<&[u8],1> = Image::build(2, 3).buf(&[ + /// 1, 5, + /// 2, 4, + /// 7, 9 + /// ]); + /// assert_eq!( + /// img.rows().collect::>(), + /// [[[1], [5]], [[2], [4]], [[7], [9]]] + /// ); + /// ``` + #[must_use = "iterators are lazy and do nothing unless consumed"] + pub fn rows<'a, U: 'a>( + &'a self, + ) -> impl ExactSizeIterator + DoubleEndedIterator + where + T: AsRef<[U]>, + { + self.flatten().chunks_exact(self.width() as usize) + } } impl, const CHANNELS: usize> Image { diff --git a/src/pixels/wam.rs b/src/pixels/wam.rs index 1da4c4d..ccf7d9f 100644 --- a/src/pixels/wam.rs +++ b/src/pixels/wam.rs @@ -1,7 +1,7 @@ use super::{float, unfloat}; use atools::prelude::*; use umath::{generic_float::Constructors, FF32}; - +#[allow(dead_code)] pub trait Wam { /// this function weighs the sides and combines /// diff --git a/src/term/iterm2.rs b/src/term/iterm2.rs index 8ec5f42..d9831d9 100644 --- a/src/term/iterm2.rs +++ b/src/term/iterm2.rs @@ -1,6 +1,5 @@ use super::{b64, Basic}; use crate::{Image, WritePng}; -use core::intrinsics::transmute_unchecked as transmute; use std::fmt::{Debug, Display, Formatter, Result, Write}; /// Outputs [Iterm2 Inline image protocol](https://iterm2.com/documentation-images.html) encoded data. diff --git a/src/term/kitty.rs b/src/term/kitty.rs index dd13cb7..cb692b4 100644 --- a/src/term/kitty.rs +++ b/src/term/kitty.rs @@ -1,6 +1,5 @@ use super::{b64, Basic}; use crate::Image; -use core::intrinsics::transmute_unchecked as transmute; use std::borrow::Cow; use std::fmt::{Debug, Display, Formatter, Result, Write};