Fix Operation::perform in image::raster

Flipping diagonally isn't the same as flipping each axis individually 😅
This commit is contained in:
Héctor Ramón Jiménez 2021-09-30 16:39:43 +07:00
parent 60070eef27
commit 2cc7e0a449
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -175,16 +175,20 @@ impl Operation {
.unwrap_or_else(Self::empty)) .unwrap_or_else(Self::empty))
} }
fn perform<I>(self, mut image: I) -> I fn perform<P>(
self,
image: image_rs::ImageBuffer<P, Vec<P::Subpixel>>,
) -> image_rs::ImageBuffer<P, Vec<P::Subpixel>>
where where
I: image_rs::GenericImage, P: image_rs::Pixel + 'static,
{ {
use image_rs::imageops; use image_rs::imageops;
if self.contains(Self::FLIP_DIAGONALLY) { let mut image = if self.contains(Self::FLIP_DIAGONALLY) {
imageops::flip_horizontal_in_place(&mut image); flip_diagonally(image)
imageops::flip_vertical_in_place(&mut image); } else {
} image
};
if self.contains(Self::ROTATE_180) { if self.contains(Self::ROTATE_180) {
imageops::rotate180_in_place(&mut image); imageops::rotate180_in_place(&mut image);
@ -197,3 +201,24 @@ impl Operation {
image image
} }
} }
fn flip_diagonally<I>(
image: I,
) -> image_rs::ImageBuffer<I::Pixel, Vec<<I::Pixel as image_rs::Pixel>::Subpixel>>
where
I: image_rs::GenericImage,
I::Pixel: 'static,
{
let (width, height) = image.dimensions();
let mut out = image_rs::ImageBuffer::new(height, width);
for x in 0..width {
for y in 0..height {
let p = image.get_pixel(x, y);
out.put_pixel(y, x, p);
}
}
out
}