From f7395ae0d5fc9fd44c9189c58cb37abfae6a4723 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 4 Jun 2022 21:18:54 +1000 Subject: [PATCH] add example of jpeg decoding --- services/modals/Cargo.toml | 3 ++- services/modals/src/tests.rs | 45 +++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/services/modals/Cargo.toml b/services/modals/Cargo.toml index 0cde50d0b..5993b1613 100644 --- a/services/modals/Cargo.toml +++ b/services/modals/Cargo.toml @@ -27,11 +27,12 @@ bit_field = "0.9.0" 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} +jpeg-decoder = {version = "0.2.6", optional = true} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} [features] tts = [] -ditherpunk = ["dither", "fast_image_resize", "png"] +ditherpunk = ["dither", "fast_image_resize", "png", "jpeg-decoder"] default = [] diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index b9a4d0a0c..5d3cc77d6 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -4,6 +4,8 @@ use super::*; use dither::prelude::{Img, RGB}; use gam::*; #[cfg(feature = "ditherpunk")] +use jpeg_decoder as jpeg; +#[cfg(feature = "ditherpunk")] use std::fs::File; use std::thread; use xous_names::XousNames; @@ -21,6 +23,7 @@ const CHECKBOX_TEST: [&'static str; 5] = ["happy", "😃", "安", "peaceful", ". /// check boxes and radio boxes. pub fn spawn_test() { // spawn two threads that compete for modal resources, to test the interlocking mechanisms + thread::spawn({ move || { let xns = XousNames::new().unwrap(); @@ -84,6 +87,7 @@ pub fn spawn_test() { log::info!("notification test done"); } }); + thread::spawn({ move || { let xns = XousNames::new().unwrap(); @@ -154,10 +158,14 @@ pub fn spawn_test() { }); } +#[cfg(all( + feature = "ditherpunk", + not(any(target_os = "none", target_os = "xous")) +))] fn get_test_png(path: &str) -> Option>> { let file = match File::open(path) { Err(err) => { - log::error!("cannot load image, {}", err); + log::error!("cannot load png image, {}", err); return None; } Ok(r) => r, @@ -168,6 +176,38 @@ fn get_test_png(path: &str) -> Option>> { let info = reader.next_frame(&mut buf).expect("failed to decode png"); let (width, height) = (info.width, info.height); + Some(vec_u8_to_img_rgb(buf, width, height)) +} + +#[cfg(all( + feature = "ditherpunk", + not(any(target_os = "none", target_os = "xous")) +))] +fn get_test_jpeg(path: &str) -> Option>> { + let file = match File::open(path) { + Err(err) => { + log::error!("cannot load jpeg image, {}", err); + return None; + } + Ok(r) => r, + }; + let mut decoder = jpeg::Decoder::new(file); + decoder + .scale(Modals::MODAL_WIDTH as u16, Modals::MODAL_HEIGHT as u16) + .expect("failed to scale jpeg"); + let _reader = decoder.read_info().expect("failed to read png info"); + let pixels = decoder.decode().expect("failed to decode jpeg image"); + let info = decoder.info().unwrap(); + + let (width, height) = (info.width, info.height); + Some(vec_u8_to_img_rgb(pixels, width.into(), height.into())) +} + +#[cfg(all( + feature = "ditherpunk", + not(any(target_os = "none", target_os = "xous")) +))] +fn vec_u8_to_img_rgb(buf: Vec, width: u32, height: u32) -> Img> { let px = width * height; let mut pixels: Vec> = Vec::with_capacity(px.try_into().unwrap()); let mut i = 0; @@ -175,8 +215,7 @@ fn get_test_png(path: &str) -> Option>> { 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) + Img::new(pixels, width).expect("failed to create Image") } fn test_validator(input: TextEntryPayload) -> Option> {