mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
add example of jpeg decoding
This commit is contained in:
parent
4b12915df0
commit
f7395ae0d5
2 changed files with 44 additions and 4 deletions
|
|
@ -27,11 +27,12 @@ bit_field = "0.9.0"
|
||||||
dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false, optional = true }
|
dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false, optional = true }
|
||||||
fast_image_resize = {version = "0.9.2", optional = true}
|
fast_image_resize = {version = "0.9.2", optional = true}
|
||||||
png = {version = "0.17.5", optional = true}
|
png = {version = "0.17.5", optional = true}
|
||||||
|
jpeg-decoder = {version = "0.2.6", optional = true}
|
||||||
|
|
||||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||||
utralib = { path = "../../utralib"}
|
utralib = { path = "../../utralib"}
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
tts = []
|
tts = []
|
||||||
ditherpunk = ["dither", "fast_image_resize", "png"]
|
ditherpunk = ["dither", "fast_image_resize", "png", "jpeg-decoder"]
|
||||||
default = []
|
default = []
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ use super::*;
|
||||||
use dither::prelude::{Img, RGB};
|
use dither::prelude::{Img, RGB};
|
||||||
use gam::*;
|
use gam::*;
|
||||||
#[cfg(feature = "ditherpunk")]
|
#[cfg(feature = "ditherpunk")]
|
||||||
|
use jpeg_decoder as jpeg;
|
||||||
|
#[cfg(feature = "ditherpunk")]
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use xous_names::XousNames;
|
use xous_names::XousNames;
|
||||||
|
|
@ -21,6 +23,7 @@ const CHECKBOX_TEST: [&'static str; 5] = ["happy", "😃", "安", "peaceful", ".
|
||||||
/// check boxes and radio boxes.
|
/// check boxes and radio boxes.
|
||||||
pub fn spawn_test() {
|
pub fn spawn_test() {
|
||||||
// spawn two threads that compete for modal resources, to test the interlocking mechanisms
|
// spawn two threads that compete for modal resources, to test the interlocking mechanisms
|
||||||
|
|
||||||
thread::spawn({
|
thread::spawn({
|
||||||
move || {
|
move || {
|
||||||
let xns = XousNames::new().unwrap();
|
let xns = XousNames::new().unwrap();
|
||||||
|
|
@ -84,6 +87,7 @@ pub fn spawn_test() {
|
||||||
log::info!("notification test done");
|
log::info!("notification test done");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
thread::spawn({
|
thread::spawn({
|
||||||
move || {
|
move || {
|
||||||
let xns = XousNames::new().unwrap();
|
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>>> {
|
fn get_test_png(path: &str) -> Option<Img<RGB<u8>>> {
|
||||||
let file = match File::open(path) {
|
let file = match File::open(path) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("cannot load image, {}", err);
|
log::error!("cannot load png image, {}", err);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Ok(r) => r,
|
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 info = reader.next_frame(&mut buf).expect("failed to decode png");
|
||||||
|
|
||||||
let (width, height) = (info.width, info.height);
|
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 px = width * height;
|
||||||
let mut pixels: Vec<RGB<u8>> = Vec::with_capacity(px.try_into().unwrap());
|
let mut pixels: Vec<RGB<u8>> = Vec::with_capacity(px.try_into().unwrap());
|
||||||
let mut i = 0;
|
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]]));
|
pixels.push(RGB::from([buf[i], buf[i + 1], buf[i + 2]]));
|
||||||
i += 3;
|
i += 3;
|
||||||
}
|
}
|
||||||
let img = Img::new(pixels, width).expect("failed to create Image");
|
Img::new(pixels, width).expect("failed to create Image")
|
||||||
Some(img)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_validator(input: TextEntryPayload) -> Option<xous_ipc::String<256>> {
|
fn test_validator(input: TextEntryPayload) -> Option<xous_ipc::String<256>> {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue