add row and col iters (#3)

This commit is contained in:
bendn 2024-05-20 08:31:33 +07:00 committed by GitHub
parent 2c9b749230
commit 8649f3c2a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 67 additions and 3 deletions

View file

@ -552,6 +552,72 @@ impl<T, const CHANNELS: usize> Image<T, CHANNELS> {
// SAFETY: slice should always return a valid index // SAFETY: slice should always return a valid index
unsafe { self.buffer.as_mut().get_unchecked_mut(idx) } 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> { impl<T: AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {

View file

@ -1,7 +1,7 @@
use super::{float, unfloat}; use super::{float, unfloat};
use atools::prelude::*; use atools::prelude::*;
use umath::{generic_float::Constructors, FF32}; use umath::{generic_float::Constructors, FF32};
#[allow(dead_code)]
pub trait Wam { pub trait Wam {
/// this function weighs the sides and combines /// this function weighs the sides and combines
/// ///

View file

@ -1,6 +1,5 @@
use super::{b64, Basic}; use super::{b64, Basic};
use crate::{Image, WritePng}; use crate::{Image, WritePng};
use core::intrinsics::transmute_unchecked as transmute;
use std::fmt::{Debug, Display, Formatter, Result, Write}; use std::fmt::{Debug, Display, Formatter, Result, Write};
/// Outputs [Iterm2 Inline image protocol](https://iterm2.com/documentation-images.html) encoded data. /// Outputs [Iterm2 Inline image protocol](https://iterm2.com/documentation-images.html) encoded data.

View file

@ -1,6 +1,5 @@
use super::{b64, Basic}; use super::{b64, Basic};
use crate::Image; use crate::Image;
use core::intrinsics::transmute_unchecked as transmute;
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt::{Debug, Display, Formatter, Result, Write}; use std::fmt::{Debug, Display, Formatter, Result, Write};