Skip to content

Commit 4eede62

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 2dd196b commit 4eede62

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
@@ -1005,7 +1005,7 @@ where
10051005
///
10061006
/// Returns an [`ImageError::Parameter`] if the mask dimensions do not match the image
10071007
/// dimensions. Otherwise, if the pixel type does not have an alpha channel this is a no-op.
1008-
pub fn apply_alpha_mask<RhsContainer>(
1008+
pub fn apply_alpha_channel<RhsContainer>(
10091009
&mut self,
10101010
mask: &ImageBuffer<Luma<P::Subpixel>, RhsContainer>,
10111011
) -> ImageResult<()>
@@ -1830,6 +1830,44 @@ mod test {
18301830
let mask = image.to_alpha_mask();
18311831
assert_eq!(mask.as_raw(), &(1u8..17).collect::<Vec<_>>());
18321832
}
1833+
1834+
#[test]
1835+
fn apply_alpha_mask() {
1836+
let mut image: ImageBuffer<LumaA<u8>, _> = ImageBuffer::new(4, 4);
1837+
1838+
let alpha = ImageBuffer::from_pixel(4, 4, Luma([255]));
1839+
image.apply_alpha_channel(&alpha).expect("can apply");
1840+
1841+
for pixel in image.pixels() {
1842+
assert_eq!(pixel.0, [0, 255]);
1843+
}
1844+
}
1845+
1846+
#[test]
1847+
fn apply_alpha_mask_rgb() {
1848+
let mut image: ImageBuffer<Rgba<u8>, _> = ImageBuffer::new(4, 4);
1849+
1850+
let alpha = ImageBuffer::from_pixel(4, 4, Luma([255]));
1851+
image.apply_alpha_channel(&alpha).expect("can apply");
1852+
1853+
for pixel in image.pixels() {
1854+
assert_eq!(pixel.0, [0, 0, 0, 255]);
1855+
}
1856+
}
1857+
1858+
#[test]
1859+
fn can_not_apply_alpha_mask() {
1860+
ImageBuffer::<LumaA<u8>, _>::new(4, 4)
1861+
.apply_alpha_channel(&ImageBuffer::new(1, 1))
1862+
.expect_err("can not apply with wrong dimensions");
1863+
1864+
ImageBuffer::<Luma<u8>, _>::new(4, 4)
1865+
.apply_alpha_channel(&ImageBuffer::new(4, 4))
1866+
.expect_err("can not apply without alpha channel");
1867+
ImageBuffer::<Rgb<u8>, _>::new(4, 4)
1868+
.apply_alpha_channel(&ImageBuffer::new(4, 4))
1869+
.expect_err("can not apply without alpha channel");
1870+
}
18331871
}
18341872

18351873
#[cfg(test)]

0 commit comments

Comments
 (0)