Skip to content

Commit 24d8365

Browse files
committed
add row and col iters (no ICE)
1 parent 2c9b749 commit 24d8365

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

src/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,72 @@ impl<T, const CHANNELS: usize> Image<T, CHANNELS> {
552552
// SAFETY: slice should always return a valid index
553553
unsafe { self.buffer.as_mut().get_unchecked_mut(idx) }
554554
}
555+
556+
/// iterator over columns
557+
/// returned iterator returns a iterator for each column
558+
///
559+
/// ```text
560+
/// ┌ ┐┌ ┐┌ ┐
561+
/// │1││2││3│
562+
/// │4││5││6│
563+
/// │7││8││9│
564+
/// └ ┘└ ┘└ ┘
565+
/// ```
566+
///
567+
/// ```
568+
/// # use fimg::Image;
569+
/// let img: Image<&[u8],1> = Image::build(2, 3).buf(&[
570+
/// 1, 5,
571+
/// 2, 4,
572+
/// 7, 9
573+
/// ]);
574+
/// assert_eq!(
575+
/// img.cols().map(|x| x.collect::<Vec<_>>()).collect::<Vec<_>>(),
576+
/// [[[1], [2], [7]], [[5], [4], [9]]]
577+
/// );
578+
/// ```
579+
#[must_use = "iterators are lazy and do nothing unless consumed"]
580+
pub fn cols<U: Copy>(
581+
&self,
582+
) -> impl DoubleEndedIterator
583+
+ ExactSizeIterator<
584+
Item = impl ExactSizeIterator + DoubleEndedIterator<Item = [U; CHANNELS]> + '_,
585+
>
586+
where
587+
T: AsRef<[U]>,
588+
{
589+
(0..self.width()).map(move |x| (0..self.height()).map(move |y| unsafe { self.pixel(x, y) }))
590+
}
591+
592+
/// iterator over rows
593+
/// returns a iterator over each row
594+
/// ```text
595+
/// [ 1 2 3 ]
596+
/// [ 4 5 6 ]
597+
/// [ 7 8 9 ]
598+
/// ```
599+
///
600+
/// ```
601+
/// # use fimg::Image;
602+
/// let img: Image<&[u8],1> = Image::build(2, 3).buf(&[
603+
/// 1, 5,
604+
/// 2, 4,
605+
/// 7, 9
606+
/// ]);
607+
/// assert_eq!(
608+
/// img.rows().collect::<Vec<_>>(),
609+
/// [[[1], [5]], [[2], [4]], [[7], [9]]]
610+
/// );
611+
/// ```
612+
#[must_use = "iterators are lazy and do nothing unless consumed"]
613+
pub fn rows<'a, U: 'a>(
614+
&'a self,
615+
) -> impl ExactSizeIterator + DoubleEndedIterator<Item = &'a [[U; CHANNELS]]>
616+
where
617+
T: AsRef<[U]>,
618+
{
619+
self.flatten().chunks_exact(self.width() as usize)
620+
}
555621
}
556622

557623
impl<T: AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {

src/pixels/wam.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{float, unfloat};
22
use atools::prelude::*;
33
use umath::{generic_float::Constructors, FF32};
4-
4+
#[allow(dead_code)]
55
pub trait Wam {
66
/// this function weighs the sides and combines
77
///

src/term/iterm2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{b64, Basic};
22
use crate::{Image, WritePng};
3-
use core::intrinsics::transmute_unchecked as transmute;
43
use std::fmt::{Debug, Display, Formatter, Result, Write};
54

65
/// Outputs [Iterm2 Inline image protocol](https://iterm2.com/documentation-images.html) encoded data.

src/term/kitty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::{b64, Basic};
22
use crate::Image;
3-
use core::intrinsics::transmute_unchecked as transmute;
43
use std::borrow::Cow;
54
use std::fmt::{Debug, Display, Formatter, Result, Write};
65

0 commit comments

Comments
 (0)