diff --git a/bingus/Cargo.toml b/bingus/Cargo.toml index 0383821..ba3a65c 100644 --- a/bingus/Cargo.toml +++ b/bingus/Cargo.toml @@ -12,3 +12,4 @@ keywords.workspace = true [dependencies] image = { version = "0.25", features = ["rayon"] } num = "0" +rayon = "1" diff --git a/bingus/src/img/image.rs b/bingus/src/img/image.rs index ceb852d..4496cd5 100644 --- a/bingus/src/img/image.rs +++ b/bingus/src/img/image.rs @@ -1,7 +1,8 @@ -use std::ops::{BitOr, Deref, Shl}; +use std::ops::{BitOr, Deref, DerefMut, Shl}; pub use image::*; use num::{traits::ToBytes, Zero}; +use rayon::iter::ParallelIterator; use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; @@ -54,13 +55,18 @@ where impl Bendable for ImageBuffer> where - Vec: Deref, - P::Subpixel: - ToBytes + Zero + Shl + BitOr, + Vec: Deref + DerefMut, + P::Subpixel: ToBytes + + Zero + + Shl + + BitOr + + Send + + Sync, + P: Pixel + Send + Sync, { type Unit = P; - fn apply Self::Unit>(mut self, f: F) -> Self { - self.pixels_mut().for_each(|p| *p = f(p)); + fn apply Self::Unit + Sync>(mut self, f: F) -> Self { + self.par_pixels_mut().for_each(|p| *p = f(p)); self } } @@ -69,8 +75,7 @@ where mod tests { #[cfg(test)] mod ser_de { - use super::super::{Dimensions, IntoDataBytes, TryFromDataBytes}; - use image::RgbImage; + use super::super::{Dimensions, IntoDataBytes, RgbImage, TryFromDataBytes}; #[test] fn empty() { @@ -86,5 +91,20 @@ mod tests { ) ) } + + #[test] + fn simple() { + let image = RgbImage::from_raw(3, 1, vec![1, 2, 3, 4, 5, 6, 7, 8, 9]).unwrap(); + assert_eq!( + Ok(image.clone()), + RgbImage::try_from_bytes( + image.into_bytes(), + Dimensions { + width: 3, + height: 1 + } + ) + ) + } } } diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index 2fa2d6a..238b5b8 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -18,7 +18,7 @@ pub trait Bendable: TryFromDataBytes + IntoDataBytes { ) -> Result::Error> { Self::try_from_bytes(b.into_bytes(), format) } - fn apply Self::Unit>(self, f: F) -> Self; + fn apply Self::Unit + Sync>(self, f: F) -> Self; } pub trait IntoDataBytes: Sized {