Skip to content

Commit ebcd84b

Browse files
committed
Rename modification to apply_alpha_channel
Also adds a test suite of the method for different failure mechanisms, pixel types and size mismatches.
1 parent 5c5c0f3 commit ebcd84b

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/images/buffer.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ where
10131013
///
10141014
/// Returns an [`ImageError::Parameter`] if the mask dimensions do not match the image
10151015
/// dimensions. Otherwise, if the pixel type does not have an alpha channel this is a no-op.
1016-
pub fn apply_alpha_mask<RhsContainer>(
1016+
pub fn apply_alpha_channel<RhsContainer>(
10171017
&mut self,
10181018
mask: &ImageBuffer<Luma<P::Subpixel>, RhsContainer>,
10191019
) -> ImageResult<()>
@@ -2190,6 +2190,44 @@ mod test {
21902190
let mask = image.to_alpha_mask();
21912191
assert_eq!(mask.as_raw(), &(1u8..17).collect::<Vec<_>>());
21922192
}
2193+
2194+
#[test]
2195+
fn apply_alpha_mask() {
2196+
let mut image: ImageBuffer<LumaA<u8>, _> = ImageBuffer::new(4, 4);
2197+
2198+
let alpha = ImageBuffer::from_pixel(4, 4, Luma([255]));
2199+
image.apply_alpha_channel(&alpha).expect("can apply");
2200+
2201+
for pixel in image.pixels() {
2202+
assert_eq!(pixel.0, [0, 255]);
2203+
}
2204+
}
2205+
2206+
#[test]
2207+
fn apply_alpha_mask_rgb() {
2208+
let mut image: ImageBuffer<Rgba<u8>, _> = ImageBuffer::new(4, 4);
2209+
2210+
let alpha = ImageBuffer::from_pixel(4, 4, Luma([255]));
2211+
image.apply_alpha_channel(&alpha).expect("can apply");
2212+
2213+
for pixel in image.pixels() {
2214+
assert_eq!(pixel.0, [0, 0, 0, 255]);
2215+
}
2216+
}
2217+
2218+
#[test]
2219+
fn can_not_apply_alpha_mask() {
2220+
ImageBuffer::<LumaA<u8>, _>::new(4, 4)
2221+
.apply_alpha_channel(&ImageBuffer::new(1, 1))
2222+
.expect_err("can not apply with wrong dimensions");
2223+
2224+
ImageBuffer::<Luma<u8>, _>::new(4, 4)
2225+
.apply_alpha_channel(&ImageBuffer::new(4, 4))
2226+
.expect_err("can not apply without alpha channel");
2227+
ImageBuffer::<Rgb<u8>, _>::new(4, 4)
2228+
.apply_alpha_channel(&ImageBuffer::new(4, 4))
2229+
.expect_err("can not apply without alpha channel");
2230+
}
21932231
}
21942232

21952233
#[cfg(test)]

0 commit comments

Comments
 (0)