Skip to content

Commit

Permalink
Switch {map, apply}2_without_alpha Pixel functions to trait defaults
Browse files Browse the repository at this point in the history
- This makes their implementations parallel with {map,
  apply}_without_alpha implementations
- Per PR feedback in image-rs#2239
  • Loading branch information
jnickg committed Jun 11, 2024
1 parent b7b4d9f commit 42b9fe0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
13 changes: 0 additions & 13 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,6 @@ impl<T: $($bound+)*> Pixel for $ident<T> {
}
}

fn map2_without_alpha<F>(&self, other: &Self, f: F) -> $ident<T> where F: FnMut(T, T) -> T {
let mut this = (*self).clone();
this.apply2_without_alpha(other, f);
this
}

fn apply2_without_alpha<F>(&mut self, other: &$ident<T>, mut f: F) where F: FnMut(T, T) -> T {
const ALPHA: usize = $channels - $alphas;
for (a, &b) in self.0[..ALPHA].iter_mut().zip(other.0[..ALPHA].iter()) {
*a = f(*a, b)
}
}

fn invert(&mut self) {
Invert::invert(self)
}
Expand Down
12 changes: 10 additions & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,21 @@ pub trait Pixel: Copy + Clone {
/// of this pixel and ```other``` pairwise.
fn map2_without_alpha<F>(&self, other: &Self, f: F) -> Self
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel
{
let mut this = *self;
this.apply2_with_alpha(other, f, |x, _| x);
this
}

/// Apply the function ```f``` to each channel except the alpha channel,
/// of this pixel and ```other``` pairwise. Works in place.
fn apply2_without_alpha<F>(&mut self, other: &Self, f: F)
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel
{
self.apply2_with_alpha(other, f, |x, _| x);
}

/// Invert this pixel
fn invert(&mut self);
Expand Down

0 comments on commit 42b9fe0

Please sign in to comment.