fast image operations
-rw-r--r--src/lib.rs66
-rw-r--r--src/pixels/wam.rs2
-rw-r--r--src/term/iterm2.rs1
-rw-r--r--src/term/kitty.rs1
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<T, const CHANNELS: usize> Image<T, CHANNELS> {
// 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::<Vec<_>>()).collect::<Vec<_>>(),
+ /// [[[1], [2], [7]], [[5], [4], [9]]]
+ /// );
+ /// ```
+ #[must_use = "iterators are lazy and do nothing unless consumed"]
+ pub fn cols<U: Copy>(
+ &self,
+ ) -> impl DoubleEndedIterator
+ + ExactSizeIterator<
+ Item = impl ExactSizeIterator + DoubleEndedIterator<Item = [U; CHANNELS]> + '_,
+ >
+ 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::<Vec<_>>(),
+ /// [[[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<Item = &'a [[U; CHANNELS]]>
+ where
+ T: AsRef<[U]>,
+ {
+ self.flatten().chunks_exact(self.width() as usize)
+ }
}
impl<T: AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
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};