diff --git a/services/modals/Cargo.toml b/services/modals/Cargo.toml index f45c62e48..0cde50d0b 100644 --- a/services/modals/Cargo.toml +++ b/services/modals/Cargo.toml @@ -22,16 +22,16 @@ tts-frontend = {path="../tts"} locales = {path = "../../locales"} 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, optional = true } -# note: image version is matched to version used by dither -image = { version = "0.23", default-features = false, features = ["png"], optional = true } +dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false, optional = true } fast_image_resize = {version = "0.9.2", optional = true} +png = {version = "0.17.5", optional = true} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} [features] tts = [] -ditherpunk = ["image", "fast_image_resize"] +ditherpunk = ["dither", "fast_image_resize", "png"] default = [] diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 4247a8bc1..83235a41b 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -7,10 +7,10 @@ pub mod tests; use bit_field::BitField; use core::cell::Cell; #[cfg(feature = "ditherpunk")] +use dither::prelude::{Img, RGB}; +#[cfg(feature = "ditherpunk")] use fast_image_resize as fir; use gam::*; -#[cfg(feature = "ditherpunk")] -use image::RgbImage; use num_traits::*; #[cfg(feature = "ditherpunk")] use std::convert::TryInto; @@ -204,29 +204,28 @@ impl Modals { const MODAL_HEIGHT: u32 = 370; /// this blocks until the image has been dismissed. #[cfg(feature = "ditherpunk")] - pub fn show_image(&self, img: &RgbImage) -> Result<(), xous::Error> { + pub fn show_image(&self, img: &Img>) -> Result<(), xous::Error> { self.lock(); // 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 (img_width, img_height) = (img.width() as f32, img.height() 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(); let mut rotate = false; + let mut modal_img = img.clone(); if portrait_scale >= 1.0 { - //noop + // noop } else if landscape_scale >= 1.0 { rotate = true; } else if portrait_scale >= landscape_scale { - modal_img = Modals::resize_image(modal_img, portrait_scale); + modal_img = Modals::resize_image(modal_img, portrait_scale) } else { - modal_img = Modals::resize_image(modal_img, landscape_scale); rotate = true; - } + modal_img = Modals::resize_image(modal_img, landscape_scale) + }; - let mut bm = Bitmap::from(&modal_img); + 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); @@ -259,14 +258,24 @@ impl Modals { } #[cfg(feature = "ditherpunk")] - 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; + fn resize_image(img: Img>, scale: f32) -> Img> { + let (img_width, img_height) = (img.width() as f32, img.height() as f32); + let height: u32 = (img_height * scale).floor() as u32; + let width: u32 = (img_width * scale).floor() as u32; + let b = 3 * width * height; + + let mut bytes: Vec = Vec::with_capacity(b.try_into().unwrap()); + let pixels = img.into_vec(); + for px in pixels { + bytes.push(px.0); + bytes.push(px.1); + bytes.push(px.2); + } let fir_img = fir::Image::from_vec_u8( - NonZeroU32::new(img.width()).unwrap(), - NonZeroU32::new(img.height()).unwrap(), - img.into_vec(), + NonZeroU32::new(width).unwrap(), + NonZeroU32::new(height).unwrap(), + bytes, fir::PixelType::U8x3, ) .unwrap(); @@ -283,13 +292,16 @@ impl Modals { 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") + // convert back into an Img> + let px = resized.width().get() * resized.height().get(); + let mut pixels: Vec> = Vec::with_capacity(px.try_into().unwrap()); + let bytes = resized.into_vec(); + let mut i = 0; + while i < bytes.len() { + pixels.push(RGB::from([bytes[i], bytes[i + 1], bytes[i + 2]])); + i += 3; + } + Img::new(pixels, width).expect("failed to create Img") } pub fn start_progress( diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index d1aa840f1..b9a4d0a0c 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -1,10 +1,12 @@ #![allow(dead_code)] +use super::*; +#[cfg(feature = "ditherpunk")] +use dither::prelude::{Img, RGB}; use gam::*; +#[cfg(feature = "ditherpunk")] +use std::fs::File; use std::thread; use xous_names::XousNames; -#[cfg(feature="ditherpunk")] -use image::io::Reader; -use super::*; const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"]; @@ -82,7 +84,6 @@ pub fn spawn_test() { log::info!("notification test done"); } }); - thread::spawn({ move || { let xns = XousNames::new().unwrap(); @@ -137,30 +138,47 @@ pub fn spawn_test() { log::info!("qrcode test done"); // 5. test image - because it reads a local file, only makes sense on hosted mode - #[cfg(all(feature="ditherpunk", not(any(target_os = "none", target_os = "xous"))))] + #[cfg(all( + feature = "ditherpunk", + not(any(target_os = "none", target_os = "xous")) + ))] { log::info!("testing image"); - 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"); + match get_test_png("../services/modals/src/tests/bunny.png") { + Some(img) => modals.show_image(&img).expect("show image modal failed"), + None => log::warn!("failed to load test image"), + } + log::info!("image modal test done"); } } }); } +fn get_test_png(path: &str) -> Option>> { + let file = match File::open(path) { + Err(err) => { + log::error!("cannot load image, {}", err); + return None; + } + Ok(r) => r, + }; + let decoder = png::Decoder::new(file); + let mut reader = decoder.read_info().expect("failed to read png info"); + let mut buf = vec![0; reader.output_buffer_size()]; + let info = reader.next_frame(&mut buf).expect("failed to decode png"); + + let (width, height) = (info.width, info.height); + let px = width * height; + let mut pixels: Vec> = Vec::with_capacity(px.try_into().unwrap()); + let mut i = 0; + while i < buf.len() { + pixels.push(RGB::from([buf[i], buf[i + 1], buf[i + 2]])); + i += 3; + } + let img = Img::new(pixels, width).expect("failed to create Image"); + Some(img) +} + fn test_validator(input: TextEntryPayload) -> Option> { let text_str = input.as_str(); match text_str.parse::() {