From 148e0fdd9972bf90c133a11a0e12eb9a85839907 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 17 Jun 2022 23:06:39 +1000 Subject: [PATCH] remove unnecessary Img clone --- services/gam/src/bitmap.rs | 8 ++++---- services/modals/src/lib.rs | 24 +++++++++--------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 46923cc2f..960989744 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -235,8 +235,8 @@ impl From<[Option; 6]> for Bitmap { } } -impl From for Bitmap { - fn from(image: Img) -> Self { +impl From<&Img> for Bitmap { + fn from(image: &Img) -> Self { let (bm_width, bm_height, _) = image.size(); let bm_bottom = bm_height - 1; let bm_right = bm_width - 1; @@ -251,7 +251,7 @@ impl From for Bitmap { }; let mut mosaic: Vec = Vec::new(); - let pixels = Dither::new(BURKES.to_vec()).dither(image); + let pixels = Dither::new(BURKES.to_vec()).dither(&image); let mut px_index = 0; let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap(); for t in 0..tile_count { @@ -456,7 +456,7 @@ impl Dither { PixelColor::Light } } - pub fn dither(&mut self, image: Img) -> Vec { + pub fn dither(&mut self, image: &Img) -> Vec { let (width, height, length) = image.size(); self.provision(width.try_into().unwrap()); let mut pixels: Vec = Vec::with_capacity(length); diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index c46b2f161..51afbf40e 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -210,27 +210,21 @@ impl Modals { let portrait_scale = (modal_width / img_width).min(modal_height / img_height); let landscape_scale = (modal_width / img_height).min(modal_height / img_width); - let mut rotate = false; - let modal_img; - if portrait_scale >= 1.0 { - modal_img = img.clone(); - } else if landscape_scale >= 1.0 { - modal_img = img.clone(); - rotate = true; + let bm = if portrait_scale >= 1.0 { + Bitmap::from(img) + } else if landscape_scale >= 1.0 { + Bitmap::from(img).rotate90() } else if portrait_scale >= landscape_scale { - modal_img = Modals::resize_image(img.clone(), portrait_scale) + let resized = Modals::resize_image(img, portrait_scale); + Bitmap::from(&resized) } else { - rotate = true; - modal_img = Modals::resize_image(img.clone(), landscape_scale) + let resized = Modals::resize_image(img, landscape_scale); + Bitmap::from(&resized).rotate90() }; - let mut bm = Bitmap::from(modal_img); - bm = if rotate { bm.rotate90() } else { bm }; let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); - let bm = Bitmap::from(img.clone()); - // center image in modal let center = Point::new( ((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), @@ -260,7 +254,7 @@ impl Modals { #[allow(dead_code)] #[cfg(feature = "ditherpunk")] - fn resize_image(img: Img, scale: f32) -> Img { + fn resize_image(img: &Img, scale: f32) -> Img { let (w, h, _) = img.size(); let (img_width, img_height) = (w as f32, h as f32); let height: u32 = (scale * img_height).floor() as u32;