From f19b2ac93959e0814ca4deab14029ae2b1efb5da Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 24 May 2022 12:08:07 +1000 Subject: [PATCH] implement auto size rotate & center for Image Modal --- services/gam/Cargo.toml | 2 + services/gam/src/bitmap.rs | 9 ++-- services/gam/src/modal.rs | 2 +- services/modals/Cargo.toml | 3 ++ services/modals/src/lib.rs | 96 +++++++++++++++++++++++++++++------- services/modals/src/tests.rs | 23 ++++++--- 6 files changed, 106 insertions(+), 29 deletions(-) diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index 59f2cc0ad..4a713ec8c 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -36,6 +36,8 @@ qrcode = { version = "0.12", default-features = false } #dither = "1.3.10" pending https://gitlab.com/efronlicht/dither/-/merge_requests/28 dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false } +# note: image version is matched to version used by dither +image = { version = "0.23", default-features = false, features = ["jpeg", "png"] } [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = {path = "../../utralib"} diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index f8b665d5b..f4fcfdb38 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -188,9 +188,12 @@ impl From<[Option; 6]> for Bitmap { } } -impl From>> for Bitmap { - fn from(img: Img>) -> Self { - let img = img.convert_with(|rgb| rgb.convert_with(>::from)); +impl From<&image::RgbImage> for Bitmap { + fn from(image: &image::RgbImage) -> Self { + let pixels: Vec> = image.pixels().map(|p| RGB::from(p.0)).collect(); + let img = Img::new(pixels, image.width()).expect("failed to create dither Img"); + + let img = img.convert_with(|rgb: RGB| rgb.convert_with(f64::from)); let bit_depth = 1; let quantize = dither::create_quantize_n_bits_func(bit_depth).unwrap(); let bw_img = img.convert_with(|rgb| rgb.to_chroma_corrected_black_and_white()); diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index 60a15fcc0..0014ee026 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -9,7 +9,7 @@ pub use checkboxes::*; mod notification; pub use notification::*; mod image; -pub use image::*; +pub use self::image::*; mod slider; pub use slider::*; mod progressbar; diff --git a/services/modals/Cargo.toml b/services/modals/Cargo.toml index fbc923726..084e728fd 100644 --- a/services/modals/Cargo.toml +++ b/services/modals/Cargo.toml @@ -24,6 +24,9 @@ bit_field = "0.9.0" #dither = "1.3.10" pending https://gitlab.com/efronlicht/dither/-/merge_requests/28 dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false } +# note: image version is matched to version used by dither +image = { version = "0.23", default-features = false, features = ["jpeg", "png"] } +fast_image_resize = "0.9.2" [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 5cffbf2be..1da1ad614 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -5,12 +5,14 @@ use api::*; use bit_field::BitField; use core::cell::Cell; +use fast_image_resize as fir; use gam::*; +use image::{imageops, RgbImage}; use num_traits::*; +use std::convert::TryInto; +use std::num::NonZeroU32; use xous::{send_message, Message, CID}; use xous_ipc::Buffer; -use dither::prelude::{Img, RGB}; - pub type TextValidationFn = fn(TextEntryPayload) -> Option; @@ -189,28 +191,54 @@ impl Modals { .or(Err(xous::Error::InternalError))?; self.unlock(); Ok(()) - } + } + + const MODAL_WIDTH: u32 = 300; + const MODAL_HEIGHT: u32 = 370; /// this blocks until the image has been dismissed. - pub fn show_image( - &self, - img: Img>, - ) -> Result<(), xous::Error> { + pub fn show_image(&self, img: &RgbImage) -> Result<(), xous::Error> { self.lock(); - - // resize image with fast_image_resize - // rotate Img with img? - - let bm = Bitmap::from(img); - let offset = Point::new(-bm.bound.tl.x, -bm.bound.tl.y); + + // resize and/or rotate + let (modal_width, modal_height) = (Modals::MODAL_WIDTH as f32, Modals::MODAL_HEIGHT as f32); + let (img_w, img_h) = img.dimensions(); + let (img_width, img_height) = (img_w as f32, img_h as f32); + 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 modal_img: RgbImage = img.clone(); + if portrait_scale >= 1.0 { + //noop + } else if landscape_scale >= 1.0 { + modal_img = imageops::rotate90(&modal_img); + } else if portrait_scale >= landscape_scale { + modal_img = Modals::resize_image(modal_img, portrait_scale); + } else { + modal_img = Modals::resize_image(modal_img, landscape_scale); + modal_img = imageops::rotate90(&modal_img); + } + + // center image in modal + let center = Point::new( + ((Modals::MODAL_WIDTH - modal_img.width()) / 2) + .try_into() + .unwrap(), + ((Modals::MODAL_HEIGHT - modal_img.height()) / 2) + .try_into() + .unwrap(), + ); + + let bm = Bitmap::from(&modal_img); let mut tiles: [Option; 6] = [None; 6]; - for (t, tile) in bm.iter().enumerate(){ - if t >= tiles.len() { continue } + for (t, tile) in bm.iter().enumerate() { + if t >= tiles.len() { + continue; + } let mut copy = tile.clone(); - copy.translate(offset); + copy.translate(center); tiles[t] = Some(copy); - }; - + } + let spec = ManagedImage { token: self.token, tiles: tiles, @@ -222,6 +250,38 @@ impl Modals { Ok(()) } + fn resize_image(img: RgbImage, scale: f32) -> RgbImage { + let width: u32 = (img.width() as f32 * scale).floor() as u32; + let height: u32 = (img.height() as f32 * scale).floor() as u32; + + let fir_img = fir::Image::from_vec_u8( + NonZeroU32::new(img.width()).unwrap(), + NonZeroU32::new(img.height()).unwrap(), + img.into_vec(), + fir::PixelType::U8x3, + ) + .unwrap(); + + // Create container for data of destination image + let mut resized = fir::Image::new( + NonZeroU32::new(width).unwrap(), + NonZeroU32::new(height).unwrap(), + fir_img.pixel_type(), + ); + let mut rsz_view = resized.view_mut(); + + // resize image with fastest available algorithm + let mut resizer = fir::Resizer::new(fir::ResizeAlg::Nearest); + resizer.resize(&fir_img.view(), &mut rsz_view).unwrap(); + + // convert back into an RgbImage + RgbImage::from_vec( + resized.width().try_into().unwrap(), + resized.height().try_into().unwrap(), + resized.into_vec(), + ) + .expect("failed to convert to RgbImage") + } pub fn start_progress( &self, diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index fbd6caccb..49093eeb8 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -1,9 +1,8 @@ #![allow(dead_code)] use gam::*; use std::thread; - use xous_names::XousNames; -use dither::prelude::{Img, RGB}; +use image::io::{Reader}; const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"]; @@ -80,7 +79,6 @@ pub(crate) fn spawn_test() { .expect("notification failed"); log::info!("notification test done"); } - }); thread::spawn({ @@ -138,10 +136,21 @@ pub(crate) fn spawn_test() { // 5. test image log::info!("testing image"); - let mut input = std::env::current_dir().unwrap(); - input.push("../services/modals/src/tests/bunny.png"); - let img = Img::>::load(&input).unwrap(); - modals.show_image(img).expect("image failed"); + let reader = match Reader::open("../services/modals/src/tests/bunny.png") { + Err(err) => { + log::error!("cannot load image, {}", err); + return + } + Ok(r) => r, + }; + let img = match reader.decode() { + Err(e) => { + log::error!("failed to decode image, {}", e); + return + } + Ok(img) => img.into_rgb8(), + }; + modals.show_image(&img).expect("image modal failed"); log::info!("image test done"); } });