implement Image modal

Now you can:
let mut input = std::env::current_dir().unwrap();
input.push("../services/modals/src/tests/bunny.png");
let img = Img::<RGB<u8>>::load(&input).unwrap();
modals.show_image(img).expect("image failed");
This commit is contained in:
nworbnhoj 2022-05-21 06:42:12 +10:00
parent 11f6fb4aeb
commit 7f66291a38
11 changed files with 180 additions and 7 deletions

View file

@ -1,9 +1,8 @@
use core::cmp::{max, min};
use dither::prelude::{Dither, Img, RGB};
use graphics_server::api::shapes;
use graphics_server::api::{Point, Rectangle, Tile};
use graphics_server::PixelColor;
use core::cmp::min;
use dither::prelude::{Dither, Img, RGB};
use std::convert::TryInto;
use std::ops::Deref;
@ -78,6 +77,13 @@ impl Bitmap {
(tile_size, tile_width_words as i16)
}
#[allow(dead_code)]
fn area(&self) -> u32 {
let (x, y) = self.size();
(x * y) as u32
}
#[allow(dead_code)]
fn size(&self) -> (usize, usize) {
(self.bound.br.x as usize, self.bound.br.y as usize)
}
@ -96,6 +102,28 @@ impl Bitmap {
}
}
fn hull(mosaic: &Vec<Tile>) -> Rectangle {
let mut hull_tl = Point::new(i16::MAX, i16::MAX);
let mut hull_br = Point::new(i16::MIN, i16::MIN);
let mut tile_area = 0;
for (_i, tile) in mosaic.iter().enumerate() {
let tile_bound = tile.bound();
hull_tl.x = min(hull_tl.x, tile_bound.tl.x);
hull_tl.y = min(hull_tl.y, tile_bound.tl.y);
hull_br.x = max(hull_br.x, tile_bound.br.x);
hull_br.y = max(hull_br.y, tile_bound.br.y);
tile_area +=
(1 + tile_bound.br.x - tile_bound.tl.x) * (1 + tile_bound.br.y - tile_bound.tl.y);
}
let hull_area = (1 + hull_br.x - hull_tl.x) * (1 + hull_br.y - hull_tl.y);
if tile_area < hull_area {
log::warn!("Bitmap Tile gaps");
} else if tile_area > hull_area {
log::warn!("Bitmap Tile overlap");
}
Rectangle::new(hull_tl, hull_br)
}
pub fn get_tile(&self, point: Point) -> Tile {
let tile = self.get_tile_index(point);
self.mosaic.as_slice()[tile]
@ -110,6 +138,7 @@ impl Bitmap {
self.get_tile(point).get_line(point)
}
#[allow(dead_code)]
fn get_word(&self, point: Point) -> shapes::Word {
self.get_tile(point).get_word(point)
}
@ -137,6 +166,28 @@ impl Deref for Bitmap {
}
}
impl From<[Option<Tile>; 6]> for Bitmap {
fn from(tiles: [Option<Tile>; 6]) -> Self {
let mut mosaic: Vec<Tile> = Vec::new();
let mut tile_size = Point::new(0, 0);
for t in 0..tiles.len() {
if tiles[t].is_some() {
let tile = tiles[t].unwrap();
mosaic.push(tile);
if tile_size.x == 0 {
tile_size = tile.size();
}
}
}
Self {
bound: Self::hull(&mosaic),
tile_size: tile_size,
mosaic: mosaic,
}
}
}
impl From<Img<RGB<u8>>> for Bitmap {
fn from(img: Img<RGB<u8>>) -> Self {
let img = img.convert_with(|rgb| rgb.convert_with(<f64 as From<u8>>::from));

View file

@ -14,7 +14,8 @@ pub mod bitmap;
pub use bitmap::*;
use graphics_server::api::{TextOp, TextView};
use graphics_server::api::{Point, Gid, Line, Rectangle, Circle, RoundedRectangle, TokenClaim};
use graphics_server::api::{Gid, Line, Circle, RoundedRectangle, TokenClaim};
pub use graphics_server::api::{Point, Tile, Rectangle};
pub use graphics_server::api::GlyphStyle;
pub use graphics_server::api::PixelColor;
use api::Opcode; // if you prefer to map the api into your local namespace
@ -190,7 +191,7 @@ impl Gam {
let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|())
}
pub fn draw_bitmap(&self, gid: Gid, bm: Bitmap) -> Result<(), xous::Error> {
pub fn draw_bitmap(&self, gid: Gid, bm: &Bitmap) -> Result<(), xous::Error> {
let mut list = GamObjectList::new(gid);
for (_i, tile) in bm.iter().enumerate(){
list.push(GamObjectType::Tile(*tile)).unwrap();

View file

@ -8,6 +8,8 @@ mod checkboxes;
pub use checkboxes::*;
mod notification;
pub use notification::*;
mod image;
pub use image::*;
mod slider;
pub use slider::*;
mod progressbar;
@ -37,6 +39,7 @@ pub enum ActionType {
CheckBoxes,
Slider,
Notification,
Image,
ConsoleInput
}

View file

@ -388,6 +388,11 @@ impl Tile {
self.bound
}
pub fn area(&self) -> u32 {
let size = self.size();
(size.x * size.y) as u32
}
pub fn size(&self) -> Point {
let pos = self.bound;
Point::new(pos.br.x - pos.tl.x, pos.br.y - pos.tl.y)

View file

@ -626,8 +626,7 @@ pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option<Rectangle>) {
let mut fb_word = fb[fb_ln + fb_wd];
let mut left_part = fb_word & mask;
let mut right_part;
let mut tile_word: u32 = 0;
let mut word = fb_word;
let mut tile_word: u32;
while (fb_wd <= last_word) & (tile_wd < tile_line.len()) {
fb_word = fb[fb_ln + fb_wd];
tile_word = tile_line[tile_wd];

View file

@ -22,6 +22,9 @@ 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" }
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}

View file

@ -1,4 +1,6 @@
use gam::modal::*;
use gam::Tile;
pub(crate) const SERVER_NAME_MODALS: &str = "_Modal Dialog Server_";
@ -40,6 +42,13 @@ pub struct ManagedNotification {
// A Type 40 (177x177) qrcode with Medium data correction can encode max 3391 alphanumeric characters
pub qrtext: Option<xous_ipc::String<4096>>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ManagedImage {
pub token: [u32; 4],
pub tiles: [Option<Tile>; 6],
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ManagedProgress {
pub token: [u32; 4],
@ -72,6 +81,8 @@ pub(crate) enum Opcode {
PromptWithMultiResponse,
/// simple notification
Notification,
/// display an image
Image,
/// dynamic notification - a simple non-interactive notification that allows its text to be dynamically updated
DynamicNotification,
@ -113,6 +124,7 @@ pub(crate) enum Opcode {
RadioReturn,
CheckBoxReturn,
NotificationReturn,
ImageReturn,
DoUpdateDynamicNotification,
DoCloseDynamicNotification,

View file

@ -9,6 +9,8 @@ use gam::*;
use num_traits::*;
use xous::{send_message, Message, CID};
use xous_ipc::Buffer;
use dither::prelude::{Img, RGB};
pub type TextValidationFn = fn(TextEntryPayload) -> Option<ValidatorErr>;
@ -187,8 +189,40 @@ impl Modals {
.or(Err(xous::Error::InternalError))?;
self.unlock();
Ok(())
}
/// this blocks until the image has been dismissed.
pub fn show_image(
&self,
img: Img<RGB<u8>>,
) -> Result<(), xous::Error> {
self.lock();
// resize image with fast_image_resize
// rotate Img with img?
let bm = Bitmap::from(img);
let offset = Point::new(-bm.bound.tl.x, -bm.bound.tl.y);
let mut tiles: [Option<Tile>; 6] = [None; 6];
for (t, tile) in bm.iter().enumerate(){
if t >= tiles.len() { continue }
let mut copy = tile.clone();
copy.translate(offset);
tiles[t] = Some(copy);
};
let spec = ManagedImage {
token: self.token,
tiles: tiles,
};
let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::Image.to_u32().unwrap())
.or(Err(xous::Error::InternalError))?;
self.unlock();
Ok(())
}
pub fn start_progress(
&self,
title: &str,

View file

@ -28,6 +28,7 @@
mod api;
use api::*;
mod tests;
use gam::Bitmap;
use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message};
use xous_ipc::Buffer;
@ -54,6 +55,7 @@ enum RendererState {
RunText(ManagedPromptWithTextResponse),
RunProgress(ManagedProgress),
RunNotification(ManagedNotification),
RunImage(ManagedImage),
RunDynamicNotification(DynamicNotification),
}
@ -241,6 +243,24 @@ fn xmain() -> ! {
)
.expect("couldn't initiate UX op");
}
Some(Opcode::Image) => {
let spec = {
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
buffer.to_original::<ManagedImage, _>().unwrap()
};
if spec.token != token_lock.unwrap_or(default_nonce) {
log::warn!("Attempt to access modals without a mutex lock. Ignoring.");
continue;
}
op = RendererState::RunImage(spec);
dr = Some(msg);
send_message(
renderer_cid,
Message::new_scalar(Opcode::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0),
)
.expect("couldn't initiate UX op");
}
Some(Opcode::StartProgress) => {
let spec = {
let buffer =
@ -426,6 +446,22 @@ fn xmain() -> ! {
);
renderer_modal.activate();
}
RendererState::RunImage(config) => {
let mut image = gam::modal::Image::new(
renderer_cid,
Opcode::ImageReturn.to_u32().unwrap(),
);
image.set_bitmap(Some(Bitmap::from(config.tiles)));
renderer_modal.modify(
Some(ActionType::Image(image)),
None,
false,
None,
true,
None,
);
renderer_modal.activate();
}
RendererState::RunProgress(config) => {
start_work = config.start_work;
end_work = config.end_work;
@ -641,6 +677,25 @@ fn xmain() -> ! {
panic!("UX return opcode does not match our current operation in flight. This is a serious internal error.");
}
}
},
Some(Opcode::ImageReturn) => {
match op {
RendererState::RunImage(_) => {
op = RendererState::None;
dr.take(); // unblocks the caller, but without any response data
token_lock = next_lock(&mut work_queue);
}
RendererState::None => {
log::warn!("Notification detected a fat finger event, ignoring.")
}
_ => {
log::error!(
"UX return opcode does not match our current operation in flight: {:?}",
op
);
panic!("UX return opcode does not match our current operation in flight. This is a serious internal error.");
}
}
}
Some(Opcode::Gutter) => {
log::info!("gutter op, doing nothing");

View file

@ -3,6 +3,7 @@ use gam::*;
use std::thread;
use xous_names::XousNames;
use dither::prelude::{Img, RGB};
const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"];
@ -79,6 +80,7 @@ pub(crate) fn spawn_test() {
.expect("notification failed");
log::info!("notification test done");
}
});
thread::spawn({
@ -133,6 +135,14 @@ pub(crate) fn spawn_test() {
)
.expect("qrcode failed");
log::info!("qrcode test done");
// 5. test image
log::info!("testing image");
let mut input = std::env::current_dir().unwrap();
input.push("../services/modals/src/tests/bunny.png");
let img = Img::<RGB<u8>>::load(&input).unwrap();
modals.show_image(img).expect("image failed");
log::info!("image test done");
}
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB