mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
implement auto size rotate & center for Image Modal
This commit is contained in:
parent
5952f80763
commit
f19b2ac939
6 changed files with 106 additions and 29 deletions
|
|
@ -36,6 +36,8 @@ qrcode = { version = "0.12", default-features = false }
|
|||
|
||||
#dither = "1.3.10" pending https://gitlab.com/efronlicht/dither/-/merge_requests/28
|
||||
dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false }
|
||||
# note: image version is matched to version used by dither
|
||||
image = { version = "0.23", default-features = false, features = ["jpeg", "png"] }
|
||||
|
||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||
utralib = {path = "../../utralib"}
|
||||
|
|
|
|||
|
|
@ -188,9 +188,12 @@ impl From<[Option<Tile>; 6]> for Bitmap {
|
|||
}
|
||||
}
|
||||
|
||||
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));
|
||||
impl From<&image::RgbImage> for Bitmap {
|
||||
fn from(image: &image::RgbImage) -> Self {
|
||||
let pixels: Vec<RGB<u8>> = image.pixels().map(|p| RGB::from(p.0)).collect();
|
||||
let img = Img::new(pixels, image.width()).expect("failed to create dither Img");
|
||||
|
||||
let img = img.convert_with(|rgb: RGB<u8>| rgb.convert_with(f64::from));
|
||||
let bit_depth = 1;
|
||||
let quantize = dither::create_quantize_n_bits_func(bit_depth).unwrap();
|
||||
let bw_img = img.convert_with(|rgb| rgb.to_chroma_corrected_black_and_white());
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ pub use checkboxes::*;
|
|||
mod notification;
|
||||
pub use notification::*;
|
||||
mod image;
|
||||
pub use image::*;
|
||||
pub use self::image::*;
|
||||
mod slider;
|
||||
pub use slider::*;
|
||||
mod progressbar;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ 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", default-features = false }
|
||||
# note: image version is matched to version used by dither
|
||||
image = { version = "0.23", default-features = false, features = ["jpeg", "png"] }
|
||||
fast_image_resize = "0.9.2"
|
||||
|
||||
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||
utralib = { path = "../../utralib"}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,14 @@ use api::*;
|
|||
|
||||
use bit_field::BitField;
|
||||
use core::cell::Cell;
|
||||
use fast_image_resize as fir;
|
||||
use gam::*;
|
||||
use image::{imageops, RgbImage};
|
||||
use num_traits::*;
|
||||
use std::convert::TryInto;
|
||||
use std::num::NonZeroU32;
|
||||
use xous::{send_message, Message, CID};
|
||||
use xous_ipc::Buffer;
|
||||
use dither::prelude::{Img, RGB};
|
||||
|
||||
|
||||
pub type TextValidationFn = fn(TextEntryPayload) -> Option<ValidatorErr>;
|
||||
|
||||
|
|
@ -189,28 +191,54 @@ impl Modals {
|
|||
.or(Err(xous::Error::InternalError))?;
|
||||
self.unlock();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
const MODAL_WIDTH: u32 = 300;
|
||||
const MODAL_HEIGHT: u32 = 370;
|
||||
|
||||
/// this blocks until the image has been dismissed.
|
||||
pub fn show_image(
|
||||
&self,
|
||||
img: Img<RGB<u8>>,
|
||||
) -> Result<(), xous::Error> {
|
||||
pub fn show_image(&self, img: &RgbImage) -> 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);
|
||||
|
||||
// resize and/or rotate
|
||||
let (modal_width, modal_height) = (Modals::MODAL_WIDTH as f32, Modals::MODAL_HEIGHT as f32);
|
||||
let (img_w, img_h) = img.dimensions();
|
||||
let (img_width, img_height) = (img_w as f32, img_h as f32);
|
||||
let portrait_scale = (modal_width / img_width).min(modal_height / img_height);
|
||||
let landscape_scale = (modal_width / img_height).min(modal_height / img_width);
|
||||
let mut modal_img: RgbImage = img.clone();
|
||||
if portrait_scale >= 1.0 {
|
||||
//noop
|
||||
} else if landscape_scale >= 1.0 {
|
||||
modal_img = imageops::rotate90(&modal_img);
|
||||
} else if portrait_scale >= landscape_scale {
|
||||
modal_img = Modals::resize_image(modal_img, portrait_scale);
|
||||
} else {
|
||||
modal_img = Modals::resize_image(modal_img, landscape_scale);
|
||||
modal_img = imageops::rotate90(&modal_img);
|
||||
}
|
||||
|
||||
// center image in modal
|
||||
let center = Point::new(
|
||||
((Modals::MODAL_WIDTH - modal_img.width()) / 2)
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
((Modals::MODAL_HEIGHT - modal_img.height()) / 2)
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let bm = Bitmap::from(&modal_img);
|
||||
let mut tiles: [Option<Tile>; 6] = [None; 6];
|
||||
for (t, tile) in bm.iter().enumerate(){
|
||||
if t >= tiles.len() { continue }
|
||||
for (t, tile) in bm.iter().enumerate() {
|
||||
if t >= tiles.len() {
|
||||
continue;
|
||||
}
|
||||
let mut copy = tile.clone();
|
||||
copy.translate(offset);
|
||||
copy.translate(center);
|
||||
tiles[t] = Some(copy);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
let spec = ManagedImage {
|
||||
token: self.token,
|
||||
tiles: tiles,
|
||||
|
|
@ -222,6 +250,38 @@ impl Modals {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn resize_image(img: RgbImage, scale: f32) -> RgbImage {
|
||||
let width: u32 = (img.width() as f32 * scale).floor() as u32;
|
||||
let height: u32 = (img.height() as f32 * scale).floor() as u32;
|
||||
|
||||
let fir_img = fir::Image::from_vec_u8(
|
||||
NonZeroU32::new(img.width()).unwrap(),
|
||||
NonZeroU32::new(img.height()).unwrap(),
|
||||
img.into_vec(),
|
||||
fir::PixelType::U8x3,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Create container for data of destination image
|
||||
let mut resized = fir::Image::new(
|
||||
NonZeroU32::new(width).unwrap(),
|
||||
NonZeroU32::new(height).unwrap(),
|
||||
fir_img.pixel_type(),
|
||||
);
|
||||
let mut rsz_view = resized.view_mut();
|
||||
|
||||
// resize image with fastest available algorithm
|
||||
let mut resizer = fir::Resizer::new(fir::ResizeAlg::Nearest);
|
||||
resizer.resize(&fir_img.view(), &mut rsz_view).unwrap();
|
||||
|
||||
// convert back into an RgbImage
|
||||
RgbImage::from_vec(
|
||||
resized.width().try_into().unwrap(),
|
||||
resized.height().try_into().unwrap(),
|
||||
resized.into_vec(),
|
||||
)
|
||||
.expect("failed to convert to RgbImage")
|
||||
}
|
||||
|
||||
pub fn start_progress(
|
||||
&self,
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
#![allow(dead_code)]
|
||||
use gam::*;
|
||||
use std::thread;
|
||||
|
||||
use xous_names::XousNames;
|
||||
use dither::prelude::{Img, RGB};
|
||||
use image::io::{Reader};
|
||||
|
||||
const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"];
|
||||
|
||||
|
|
@ -80,7 +79,6 @@ pub(crate) fn spawn_test() {
|
|||
.expect("notification failed");
|
||||
log::info!("notification test done");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
thread::spawn({
|
||||
|
|
@ -138,10 +136,21 @@ pub(crate) fn spawn_test() {
|
|||
|
||||
// 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");
|
||||
let reader = match Reader::open("../services/modals/src/tests/bunny.png") {
|
||||
Err(err) => {
|
||||
log::error!("cannot load image, {}", err);
|
||||
return
|
||||
}
|
||||
Ok(r) => r,
|
||||
};
|
||||
let img = match reader.decode() {
|
||||
Err(e) => {
|
||||
log::error!("failed to decode image, {}", e);
|
||||
return
|
||||
}
|
||||
Ok(img) => img.into_rgb8(),
|
||||
};
|
||||
modals.show_image(&img).expect("image modal failed");
|
||||
log::info!("image test done");
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue