From 2cc7e0a44901a46887b73d9e34d9b3dcc1de8580 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Sep 2021 16:39:43 +0700 Subject: Fix `Operation::perform` in `image::raster` Flipping diagonally isn't the same as flipping each axis individually :sweat_smile: --- wgpu/src/image/raster.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'wgpu') diff --git a/wgpu/src/image/raster.rs b/wgpu/src/image/raster.rs index 318cefb6..e13987b4 100644 --- a/wgpu/src/image/raster.rs +++ b/wgpu/src/image/raster.rs @@ -175,16 +175,20 @@ impl Operation { .unwrap_or_else(Self::empty)) } - fn perform(self, mut image: I) -> I + fn perform

( + self, + image: image_rs::ImageBuffer>, + ) -> image_rs::ImageBuffer> where - I: image_rs::GenericImage, + P: image_rs::Pixel + 'static, { use image_rs::imageops; - if self.contains(Self::FLIP_DIAGONALLY) { - imageops::flip_horizontal_in_place(&mut image); - imageops::flip_vertical_in_place(&mut image); - } + let mut image = if self.contains(Self::FLIP_DIAGONALLY) { + flip_diagonally(image) + } else { + image + }; if self.contains(Self::ROTATE_180) { imageops::rotate180_in_place(&mut image); @@ -197,3 +201,24 @@ impl Operation { image } } + +fn flip_diagonally( + image: I, +) -> image_rs::ImageBuffer::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 +} -- cgit