From 3522b9ee1906dc36d04ee3ef9eab99305dcac5c4 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 7 Dec 2019 17:29:12 +0100 Subject: improve grayscale rendering Apparently, turning the picture to grayscale also messes with the alpha channel. A lot of trait icons have a small transparent border though. Therefore, we want to preserve the alpha channel by copying it back from the original trait icon. This improves render quality and removes some of the artifacts. --- src/render.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/render.rs b/src/render.rs index 77a847f..927782c 100644 --- a/src/render.rs +++ b/src/render.rs @@ -112,10 +112,7 @@ impl<'r> Renderer<'r> { self.options.trait_size, imageops::FilterType::CatmullRom, ); - let minor_img = imageproc::map::map_pixels(&minor_img, |x, y, p| { - let alpha = self.minor_mask.get_pixel(x, y)[3]; - Rgba([p[0], p[1], p[2], alpha]) - }); + let minor_img = with_mask(&minor_img, &self.minor_mask); let y_pos = (buffer.height() - minor_img.height()) / 2; let x_slice = (buffer.width() - self.options.traitline_x_offset) / 6; let x_pos = 2 * (minor.tier - 1) * x_slice @@ -138,9 +135,9 @@ impl<'r> Renderer<'r> { imageops::FilterType::CatmullRom, ); let major_img = if !chosen { - major_img.grayscale() + with_mask(&major_img.grayscale(), &major_img) } else { - major_img + major_img.to_rgba() }; let y_slice = buffer.height() / 3; let y_pos = vertical_pos as u32 * y_slice + (y_slice - major_img.height()) / 2; @@ -323,3 +320,14 @@ impl<'r> Renderer<'r> { Ok(buffer) } } + +fn with_mask(input: &I, mask: &J) -> RgbaImage +where + I: GenericImage>, + J: GenericImage>, +{ + imageproc::map::map_pixels(input, |x, y, p| { + let alpha = mask.get_pixel(x, y)[3]; + Rgba([p[0], p[1], p[2], alpha]) + }) +} -- cgit v1.2.3