fix for rayon, new test

This commit is contained in:
Breval Ferrari 2025-01-17 12:32:43 -05:00
parent 9748483e74
commit 0d5532ab08
No known key found for this signature in database
GPG key ID: CEAB625B75A836B2
3 changed files with 30 additions and 9 deletions

View file

@ -12,3 +12,4 @@ keywords.workspace = true
[dependencies]
image = { version = "0.25", features = ["rayon"] }
num = "0"
rayon = "1"

View file

@ -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<P: Pixel> Bendable for ImageBuffer<P, Vec<P::Subpixel>>
where
Vec<P::Subpixel>: Deref<Target = [P::Subpixel]>,
P::Subpixel:
ToBytes + Zero + Shl<u8, Output = P::Subpixel> + BitOr<P::Subpixel, Output = P::Subpixel>,
Vec<P::Subpixel>: Deref<Target = [P::Subpixel]> + DerefMut,
P::Subpixel: ToBytes
+ Zero
+ Shl<u8, Output = P::Subpixel>
+ BitOr<P::Subpixel, Output = P::Subpixel>
+ Send
+ Sync,
P: Pixel + Send + Sync,
{
type Unit = P;
fn apply<F: Fn(&Self::Unit) -> Self::Unit>(mut self, f: F) -> Self {
self.pixels_mut().for_each(|p| *p = f(p));
fn apply<F: Fn(&Self::Unit) -> 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
}
)
)
}
}
}

View file

@ -18,7 +18,7 @@ pub trait Bendable: TryFromDataBytes + IntoDataBytes {
) -> Result<Self, <Self as TryFromDataBytes>::Error> {
Self::try_from_bytes(b.into_bytes(), format)
}
fn apply<F: Fn(&Self::Unit) -> Self::Unit>(self, f: F) -> Self;
fn apply<F: Fn(&Self::Unit) -> Self::Unit + Sync>(self, f: F) -> Self;
}
pub trait IntoDataBytes: Sized {