feat: Added ndarray to image
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
type Result<T, E = ndarray::ShapeError> = core::result::Result<T, E>;
|
||||
fn shape_error() -> ndarray::ShapeError {
|
||||
ndarray::ShapeError::from_kind(ndarray::ErrorKind::IncompatibleShape)
|
||||
}
|
||||
|
||||
mod rgb8 {
|
||||
use super::Result;
|
||||
pub(super) fn image_as_ndarray(image: &image::RgbImage) -> Result<ndarray::ArrayView3<u8>> {
|
||||
@@ -11,6 +15,18 @@ mod rgb8 {
|
||||
let data = image.into_raw();
|
||||
ndarray::Array3::from_shape_vec((height as usize, width as usize, 3), data)
|
||||
}
|
||||
pub(super) fn ndarray_to_image(array: &ndarray::ArrayView3<u8>) -> Result<image::RgbImage> {
|
||||
let (height, width, channels) = array.dim();
|
||||
let data = array.as_slice().ok_or_else(super::shape_error)?;
|
||||
if channels != 3 {
|
||||
return Err(super::shape_error());
|
||||
}
|
||||
Ok(
|
||||
image::RgbImage::from_raw(width as u32, height as u32, data.to_vec()).ok_or(
|
||||
ndarray::ShapeError::from_kind(ndarray::ErrorKind::IncompatibleShape),
|
||||
)?,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mod rgba8 {
|
||||
@@ -25,6 +41,18 @@ mod rgba8 {
|
||||
let data = image.into_raw();
|
||||
ndarray::Array3::from_shape_vec((height as usize, width as usize, 4), data)
|
||||
}
|
||||
pub(super) fn ndarray_to_image(array: &ndarray::ArrayView3<u8>) -> Result<image::RgbaImage> {
|
||||
let (height, width, channels) = array.dim();
|
||||
let data = array.as_slice().ok_or_else(super::shape_error)?;
|
||||
if channels != 4 {
|
||||
return Err(super::shape_error());
|
||||
}
|
||||
Ok(
|
||||
image::RgbaImage::from_raw(width as u32, height as u32, data.to_vec()).ok_or(
|
||||
ndarray::ShapeError::from_kind(ndarray::ErrorKind::IncompatibleShape),
|
||||
)?,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mod gray8 {
|
||||
@@ -39,6 +67,15 @@ mod gray8 {
|
||||
let data = image.into_raw();
|
||||
ndarray::Array2::from_shape_vec((height as usize, width as usize), data)
|
||||
}
|
||||
pub(super) fn ndarray_to_image(array: &ndarray::ArrayView2<u8>) -> Result<image::GrayImage> {
|
||||
let (height, width) = array.dim();
|
||||
let data = array.as_slice().ok_or_else(super::shape_error)?;
|
||||
Ok(
|
||||
image::GrayImage::from_raw(width as u32, height as u32, data.to_vec()).ok_or(
|
||||
ndarray::ShapeError::from_kind(ndarray::ErrorKind::IncompatibleShape),
|
||||
)?,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mod gray_alpha8 {
|
||||
@@ -55,6 +92,20 @@ mod gray_alpha8 {
|
||||
let data = image.into_raw();
|
||||
ndarray::Array3::from_shape_vec((height as usize, width as usize, 2), data)
|
||||
}
|
||||
pub(super) fn ndarray_to_image(
|
||||
array: &ndarray::ArrayView3<u8>,
|
||||
) -> Result<image::GrayAlphaImage> {
|
||||
let (height, width, channels) = array.dim();
|
||||
let data = array.as_slice().ok_or_else(super::shape_error)?;
|
||||
if channels != 2 {
|
||||
return Err(super::shape_error());
|
||||
}
|
||||
Ok(
|
||||
image::GrayAlphaImage::from_raw(width as u32, height as u32, data.to_vec()).ok_or(
|
||||
ndarray::ShapeError::from_kind(ndarray::ErrorKind::IncompatibleShape),
|
||||
)?,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
mod dynamic_image {
|
||||
@@ -95,6 +146,34 @@ pub trait ImageToNdarray {
|
||||
fn into_ndarray(self) -> Result<Self::OwnedOutput>;
|
||||
}
|
||||
|
||||
pub trait NdarrayToImage<ImageOutput> {
|
||||
fn to_image(&self) -> Result<ImageOutput>;
|
||||
}
|
||||
|
||||
impl NdarrayToImage<image::RgbImage> for ndarray::ArrayView3<'_, u8> {
|
||||
fn to_image(&self) -> Result<image::RgbImage> {
|
||||
rgb8::ndarray_to_image(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl NdarrayToImage<image::RgbaImage> for ndarray::ArrayView3<'_, u8> {
|
||||
fn to_image(&self) -> Result<image::RgbaImage> {
|
||||
rgba8::ndarray_to_image(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl NdarrayToImage<image::GrayImage> for ndarray::ArrayView2<'_, u8> {
|
||||
fn to_image(&self) -> Result<image::GrayImage> {
|
||||
gray8::ndarray_to_image(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl NdarrayToImage<image::GrayAlphaImage> for ndarray::ArrayView3<'_, u8> {
|
||||
fn to_image(&self) -> Result<image::GrayAlphaImage> {
|
||||
gray_alpha8::ndarray_to_image(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ImageToNdarray for image::RgbImage {
|
||||
type OwnedOutput = ndarray::Array3<u8>;
|
||||
type RefOutput<'a> = ndarray::ArrayView3<'a, u8>;
|
||||
|
||||
Reference in New Issue
Block a user