add example of jpeg decoding

This commit is contained in:
nworbnhoj 2022-06-04 21:18:54 +10:00
parent 4b12915df0
commit f7395ae0d5
2 changed files with 44 additions and 4 deletions

View file

@ -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 = []

View file

@ -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<Img<RGB<u8>>> {
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<Img<RGB<u8>>> {
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<Img<RGB<u8>>> {
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<u8>, width: u32, height: u32) -> Img<RGB<u8>> {
let px = width * height;
let mut pixels: Vec<RGB<u8>> = Vec::with_capacity(px.try_into().unwrap());
let mut i = 0;
@ -175,8 +215,7 @@ fn get_test_png(path: &str) -> Option<Img<RGB<u8>>> {
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<xous_ipc::String<256>> {