use super::*;
use core::ops::{Deref, DerefMut};
use image::{flat::SampleLayout, ImageBuffer, Luma, Pixel, Primitive};
use ndarray::{Array2, Array3, ArrayView2, ArrayView3, ArrayViewMut2, ArrayViewMut3, ShapeBuilder};
extern crate alloc;
use alloc::vec::Vec;
impl<A> IntoNdarray2 for ImageBuffer<Luma<A>, Vec<A>>
where
A: Primitive + 'static,
{
type Out = Array2<A>;
fn into_ndarray2(self) -> Self::Out {
let SampleLayout {
height,
height_stride,
width,
width_stride,
..
} = self.sample_layout();
let shape = (height as usize, width as usize);
let strides = (height_stride, width_stride);
Array2::from_shape_vec(shape.strides(strides), self.into_raw()).unwrap()
}
}
impl<A, Container> AsNdarray2 for ImageBuffer<Luma<A>, Container>
where
A: Primitive + 'static,
Container: Deref<Target = [A]>,
{
type Out<'a> = ArrayView2<'a, A>
where
Container: 'a;
fn as_ndarray2(&self) -> Self::Out<'_> {
let SampleLayout {
height,
height_stride,
width,
width_stride,
..
} = self.sample_layout();
let shape = (height as usize, width as usize);
let strides = (height_stride, width_stride);
ArrayView2::from_shape(shape.strides(strides), self).unwrap()
}
}
impl<A, Container> AsNdarray2Mut for ImageBuffer<Luma<A>, Container>
where
A: Primitive + 'static,
Container: DerefMut<Target = [A]>,
{
type Out<'a> = ArrayViewMut2<'a, A>
where
Container: 'a;
fn as_ndarray2_mut(&mut self) -> Self::Out<'_> {
let SampleLayout {
height,
height_stride,
width,
width_stride,
..
} = self.sample_layout();
let shape = (height as usize, width as usize);
let strides = (height_stride, width_stride);
ArrayViewMut2::from_shape(shape.strides(strides), self).unwrap()
}
}
impl<P> IntoNdarray3 for ImageBuffer<P, Vec<P::Subpixel>>
where
P: Pixel + 'static,
{
type Out = Array3<P::Subpixel>;
fn into_ndarray3(self) -> Self::Out {
let SampleLayout {
channels,
channel_stride,
height,
height_stride,
width,
width_stride,
} = self.sample_layout();
let shape = (channels as usize, height as usize, width as usize);
let strides = (channel_stride, height_stride, width_stride);
Array3::from_shape_vec(shape.strides(strides), self.into_raw()).unwrap()
}
}
impl<P> AsNdarray3 for ImageBuffer<P, Vec<P::Subpixel>>
where
P: Pixel + 'static,
{
type Out<'a> = ArrayView3<'a, P::Subpixel>;
fn as_ndarray3(&self) -> Self::Out<'_> {
let SampleLayout {
channels,
channel_stride,
height,
height_stride,
width,
width_stride,
} = self.sample_layout();
let shape = (channels as usize, height as usize, width as usize);
let strides = (channel_stride, height_stride, width_stride);
ArrayView3::from_shape(shape.strides(strides), self).unwrap()
}
}
impl<P> AsNdarray3Mut for ImageBuffer<P, Vec<P::Subpixel>>
where
P: Pixel + 'static,
{
type Out<'a> = ArrayViewMut3<'a, P::Subpixel>;
fn as_ndarray3_mut(&mut self) -> Self::Out<'_> {
let SampleLayout {
channels,
channel_stride,
height,
height_stride,
width,
width_stride,
} = self.sample_layout();
let shape = (channels as usize, height as usize, width as usize);
let strides = (channel_stride, height_stride, width_stride);
ArrayViewMut3::from_shape(shape.strides(strides), self).unwrap()
}
}