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