fast image operations
Diffstat (limited to 'src/dyn/mod.rs')
-rw-r--r--src/dyn/mod.rs41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/dyn/mod.rs b/src/dyn/mod.rs
index ccf19f9..2688160 100644
--- a/src/dyn/mod.rs
+++ b/src/dyn/mod.rs
@@ -1,4 +1,4 @@
-use crate::{pixels::convert::PFrom, Image};
+use crate::{Image, pixels::convert::PFrom};
mod affine;
mod convert;
#[cfg(feature = "scale")]
@@ -20,6 +20,30 @@ pub enum DynImage<T> {
impl Copy for DynImage<&[u8]> {}
+impl<T> const From<Image<T, 1>> for DynImage<T> {
+ fn from(x: Image<T, 1>) -> Self {
+ Self::Y(x)
+ }
+}
+
+impl<T> const From<Image<T, 2>> for DynImage<T> {
+ fn from(x: Image<T, 2>) -> Self {
+ Self::Ya(x)
+ }
+}
+
+impl<T> const From<Image<T, 3>> for DynImage<T> {
+ fn from(x: Image<T, 3>) -> Self {
+ Self::Rgb(x)
+ }
+}
+
+impl<T> const From<Image<T, 4>> for DynImage<T> {
+ fn from(x: Image<T, 4>) -> Self {
+ Self::Rgba(x)
+ }
+}
+
macro_rules! e {
($dyn:expr => |$image: pat_param| $do:expr) => {
match $dyn {
@@ -38,7 +62,7 @@ macro_rules! e {
}
};
}
-use e;
+pub(crate) use e;
#[cfg(feature = "term")]
impl<T: AsRef<[u8]>> std::fmt::Display for crate::term::Display<DynImage<T>> {
@@ -72,6 +96,12 @@ impl<T> DynImage<T> {
e!(self, |i| i.height())
}
+ #[doc(hidden)]
+ pub unsafe fn mapped<U, F: FnOnce(T) -> U>(self, f: F) -> DynImage<U> {
+ // SAFETY: we dont change anything, why check
+ unsafe { e!(self => |i| i.mapped(f)) }
+ }
+
/// Get the image buffer.
pub const fn buffer(&self) -> &T {
e!(self, |i| i.buffer())
@@ -83,6 +113,13 @@ impl<T> DynImage<T> {
}
}
+impl DynImage<&[u8]> {
+ /// Copy this ref image
+ pub fn copy(&self) -> Self {
+ e!(self => |i| i.copy())
+ }
+}
+
impl<T: AsRef<[u8]>> DynImage<T> {
/// Reference this image.
pub fn as_ref(&self) -> DynImage<&[u8]> {