From b769b26a576f600d18ee259b4564d04f219c40a0 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 19 Jul 2022 04:57:57 +1000 Subject: [PATCH] recast Image Modal to accept gam::Bitmap directly --- services/modals/src/lib.rs | 39 +++++++----------------------------- services/modals/src/tests.rs | 12 ++++++++--- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index a8888b448..a64db38e4 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -10,6 +10,7 @@ use gam::*; use num_traits::*; #[cfg(feature = "ditherpunk")] use std::convert::TryInto; +use std::cmp::min; use xous::{send_message, Message, CID}; use xous_ipc::Buffer; @@ -194,48 +195,22 @@ impl Modals { /// this blocks until the image has been dismissed. #[cfg(feature = "ditherpunk")] - pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { + pub fn show_image(&self, mut bm: Bitmap) -> Result<(), xous::Error> { self.lock(); - // resize and/or rotate - const BORDER: u32 = 3; - - let (modal_width, modal_height) = ( - gam::IMG_MODAL_WIDTH - 2 * BORDER, - gam::IMG_MODAL_HEIGHT - 2 * BORDER, - ); - - let (w, h) = (img.width(), img.height()); - let (img_width, img_height) = (w as f32, h as f32); - - let portrait_scale = (modal_width as f32 / img_width).min(modal_height as f32 / img_height); - let landscape_scale = - (modal_width as f32 / img_height).min(modal_height as f32 / img_width); - let mut bm = if portrait_scale >= 1.0 { - log::info!("show image as is"); - Bitmap::from(img) - } else if landscape_scale >= 1.0 { - log::info!("rotate image"); - Bitmap::from(img).rotate90() - } else if portrait_scale >= landscape_scale { - log::info!("scale image {}", portrait_scale); - Bitmap::new_resize(img, (portrait_scale * img_width) as usize) - } else { - log::info!("scale image {} and rotate", landscape_scale); - Bitmap::new_resize(img, (landscape_scale * img_width) as usize).rotate90() - }; let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); // center image in modal - let center = Point::new( - (BORDER + (gam::IMG_MODAL_WIDTH - 2 * BORDER - bm_width) / 2) + const BORDER: u32 = 3; + let margin = Point::new( + (BORDER + min(0,(gam::IMG_MODAL_WIDTH - 2 * BORDER - bm_width) / 2)) .try_into() .unwrap(), - (BORDER + (gam::IMG_MODAL_HEIGHT - 2 * BORDER - bm_height) / 2) + (BORDER + min(0, (gam::IMG_MODAL_HEIGHT - 2 * BORDER - bm_height) / 2)) .try_into() .unwrap(), ); - bm.translate(center); + bm.translate(margin); let mut tiles: [Option; 6] = [None; 6]; for (t, tile) in bm.iter().enumerate() { diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 668073554..2d6bf21c7 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -163,9 +163,14 @@ pub fn spawn_test() { */ #[cfg(feature = "ditherpunk")] { - let img = clifford(); + const BORDER: u32 = 3; + let modal_size = gam::Point::new( + (gam::IMG_MODAL_WIDTH - 2 * BORDER).try_into().unwrap(), + (gam::IMG_MODAL_HEIGHT - 2 * BORDER).try_into().unwrap() + ); + let bm = gam::Bitmap::from_img(&clifford(), Some(modal_size)); log::info!("showing image"); - modals.show_image(&img).expect("show image modal failed"); + modals.show_image(bm).expect("show image modal failed"); log::info!("image modal test done"); } } @@ -188,7 +193,8 @@ fn clifford() -> Img { let (mut x, mut y): (f32, f32) = (0.0, 0.0); log::info!("generating image"); - for _ in 0..=ITERATIONS { // this takes a couple minutes to run + for _ in 0..=ITERATIONS { + // this takes a couple minutes to run let x1 = f32::sin(a * y) + c * f32::cos(a * x); let y1 = f32::sin(b * x) + d * f32::cos(b * y); (x, y) = (x1, y1);