implement clifford attractor as modal test image

This commit is contained in:
nworbnhoj 2022-06-14 10:09:01 +10:00
parent 6dfed76b66
commit dda78b4ef3
3 changed files with 51 additions and 52 deletions

View file

@ -23,13 +23,11 @@ locales = {path = "../../locales"}
bit_field = "0.9.0"
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 = ["fast_image_resize", "png", "jpeg-decoder"]
ditherpunk = ["fast_image_resize"]
default = []

View file

@ -136,67 +136,68 @@ pub fn spawn_test() {
log::info!("qrcode test done");
// 5. test image - because it reads a local file, only makes sense on hosted mode
/*
Note there are stack allocation challenges with crates `png_encode` and `jpg`
png = {version = "0.17.5", optional = true}
-------------------------------------------
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: usize = info.width.try_into().unwrap();
let img = Img::new(buf, width);
jpeg-decoder = {version = "0.2.6", optional = true}
---------------------------------------------------
let mut decoder = jpeg_decoder::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: usize = info.width.try_into().unwrap();
let img = Img::new(pixels, width);
*/
#[cfg(all(
feature = "ditherpunk",
not(any(target_os = "none", target_os = "xous"))
))]
{
log::info!("testing image");
match get_test_png("../services/modals/src/tests/bunny.png") {
Some((pixels, width)) => {
let img = Img::new(pixels, width);
modals.show_image(&img).expect("show image modal failed");
}
None => log::warn!("failed to load test image"),
}
let img = clifford();
modals.show_image(&img).expect("show image modal failed");
log::info!("image modal test done");
}
}
});
}
#[cfg(all(
feature = "ditherpunk",
not(any(target_os = "none", target_os = "xous"))
))]
fn get_test_png(path: &str) -> Option<(Vec<u8>, usize)> {
let file = match std::fs::File::open(path) {
Err(err) => {
log::error!("cannot load png 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");
log::info!("png width {:?}", info.width);
let width: usize = info.width.try_into().unwrap();
Some((buf, width))
}
#[cfg(all(
feature = "ditherpunk",
not(any(target_os = "none", target_os = "xous"))
))]
fn get_test_jpeg(path: &str) -> Option<(Vec<u8>, usize)> {
let file = match std::fs::File::open(path) {
Err(err) => {
log::error!("cannot load jpeg image, {}", err);
return None;
}
Ok(r) => r,
};
let mut decoder = jpeg_decoder::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: usize = info.width.try_into().unwrap();
Some((pixels, width))
// https://sequelaencollection.home.blog/2d-chaotic-attractors/
fn clifford() -> Img {
const SIZE: u32 = Modals::MODAL_WIDTH;
const CENTER:f32 = (SIZE/2) as f32;
const SCALE:f32 = 70.0;
let mut buf = vec![255u8; (SIZE * SIZE).try_into().unwrap()];
let (a, b, c, d) = (-2.0, -2.4,1.1, -0.9);
let (mut x,mut y):(f32, f32) = (0.0, 0.0);
for _ in 0..=10000000 {
let x1 = f32::sin(a*y) + c * f32::cos(a*x);
let y1 = f32::sin(b*x) + d*f32::cos(b*y);
(x, y) = (x1, y1);
let (a,b):(u32, u32) = ((x * SCALE + CENTER) as u32, (y * SCALE + CENTER) as u32);
let i:usize = (a + SIZE * b).try_into().unwrap();
if buf[i] >0 { buf[i] -= 1; }
}
let mut rgb = vec![0u8; 3 * buf.len()];
let mut i:usize = 0;
while i < buf.len() {
let j = 3 * i;
(rgb[j], rgb[j+1], rgb[j+2]) = (buf[i],buf[i],buf[i]);
i += 1;
}
Img::new(rgb, SIZE.try_into().unwrap())
}
fn test_validator(input: TextEntryPayload) -> Option<xous_ipc::String<256>> {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 151 KiB