remove unnecessary Img clone

This commit is contained in:
nworbnhoj 2022-06-17 23:06:39 +10:00
parent 39815cc596
commit 148e0fdd99
2 changed files with 13 additions and 19 deletions

View file

@ -235,8 +235,8 @@ impl From<[Option<Tile>; 6]> for Bitmap {
}
}
impl From<Img> 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<Img> for Bitmap {
};
let mut mosaic: Vec<Tile> = 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<PixelColor> {
pub fn dither(&mut self, image: &Img) -> Vec<PixelColor> {
let (width, height, length) = image.size();
self.provision(width.try_into().unwrap());
let mut pixels: Vec<PixelColor> = Vec::with_capacity(length);

View file

@ -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;