Merge pull request #192 from nworbnhoj/main

Image processing optimisations
This commit is contained in:
bunnie 2022-06-18 14:00:30 +08:00 committed by GitHub
commit fdcadf35dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 105 deletions

View file

@ -290,51 +290,56 @@ impl From<&Img> for Bitmap {
} }
} }
pub enum PixelType {
U8,
U8x3,
U8x4,
}
/* /*
* Image as a minimal flat buffer of RGB pixels; accessible by (x, y) * Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y)
* *
* author: nworbnhoj * author: nworbnhoj
*/ */
#[derive(Debug, Clone, Copy)]
pub struct RGB(pub u8, pub u8, pub u8);
impl RGB {
const R: u32 = 2126;
const G: u32 = 7152;
const B: u32 = 722;
const BLACK: u32 = RGB::R + RGB::G + RGB::B;
/// chromatic conversion from RGB to grey
pub fn to_grey(&self) -> u8 {
let grey_r = RGB::R * self.0 as u32;
let grey_g = RGB::G * self.1 as u32;
let grey_b = RGB::B * self.2 as u32;
((grey_r + grey_g + grey_b) / RGB::BLACK).try_into().unwrap()
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Img { pub struct Img {
pixels: Vec<RGB>, pixels: Vec<u8>,
width: usize, width: usize,
} }
impl Img { impl Img {
pub fn new(buf: Vec<u8>, width: usize) -> Self { pub fn new(buf: Vec<u8>, width: usize, px_type: PixelType) -> Self {
let px_len = buf.len() / 3; const R: u32 = 2126;
let mut pixels: Vec<RGB> = Vec::with_capacity(px_len); const G: u32 = 7152;
const B: u32 = 722;
const BLACK: u32 = R + G + B;
let px_len = match px_type {
PixelType::U8 => buf.len(),
PixelType::U8x3 => buf.len() / 3,
_ => 0,
};
let mut pixels: Vec<u8> = Vec::with_capacity(px_len);
for px in 0..px_len { for px in 0..px_len {
let b = px * 3; let pixel: u8 = match px_type {
pixels.push(RGB { PixelType::U8 => buf[px],
0: buf[b], PixelType::U8x3 => {
1: buf[b + 1], let b = px * 3;
2: buf[b + 2], let grey_r = R * buf[b] as u32;
}); let grey_g = G * buf[b + 1] as u32;
let grey_b = B * buf[b + 2] as u32;
((grey_r + grey_g + grey_b) / BLACK).try_into().unwrap()
}
_ => {
log::warn!("unsupported PixelType");
0
}
};
pixels.push(pixel);
} }
Self { pixels, width } Self { pixels, width }
} }
pub fn get(&self, x: usize, y: usize) -> Option<&RGB> { pub fn get(&self, x: usize, y: usize) -> Option<&u8> {
let i: usize = (y * self.width) + x; let i: usize = (y * self.width) + x;
self.pixels.get(i) self.pixels.get(i)
} }
@ -344,19 +349,36 @@ impl Img {
let height: usize = length / width; let height: usize = length / width;
(width, height, length) (width, height, length)
} }
pub fn into_raw(&self) -> Vec<u8> { pub fn as_slice(&self) -> &[u8] {
let len = self.pixels.len() as usize; self.pixels.as_slice()
let mut bytes: Vec<u8> = Vec::with_capacity(len); }
for px in &self.pixels { // simply retains the closest pixel
bytes.push(px.0); pub fn resize(&self, width: usize, height: usize) -> Img {
bytes.push(px.1); let mut buf = vec![0u8; (width * height).try_into().unwrap()];
bytes.push(px.2); let (self_width, self_height, _) = self.size();
let x_scale = self_width as f32 / width as f32;
let y_scale = self_height as f32 / height as f32;
let self_x_base = x_scale * 0.5;
let self_y_base = y_scale * 0.5;
// Pretabulate horizontal pixel positions
let mut self_x_tab: Vec<usize> = Vec::with_capacity(width);
for buf_x in 0..width {
self_x_tab.push(((self_x_base + x_scale * buf_x as f32) as usize).min(self_width - 1));
} }
bytes
for buf_y in 0..height {
let self_y = ((self_y_base + y_scale * buf_y as f32) as usize).min(self_height - 1);
let self_row = self_y * self_width;
let buf_row = buf_y * width;
for (buf_x, self_x) in self_x_tab.iter().enumerate() {
buf[buf_row + buf_x] = self.pixels[self_row + self_x];
}
}
Img::new(buf, width.try_into().unwrap(), PixelType::U8)
} }
} }
/* /*
* Dithering involves aplying a threshold to each pixel to round down to Black * Dithering involves aplying a threshold to each pixel to round down to Black
* or up to White. The residual error from this blunt instrument is diffused amongst * or up to White. The residual error from this blunt instrument is diffused amongst
@ -370,7 +392,6 @@ impl Img {
* author: nworbnhoj * author: nworbnhoj
*/ */
/// Burkes dithering diffusion scheme was chosen for its modest resource /// Burkes dithering diffusion scheme was chosen for its modest resource
/// requirements with impressive quality outcome. /// requirements with impressive quality outcome.
/// Burkes dithering. Div=32. /// Burkes dithering. Div=32.
@ -388,7 +409,6 @@ const BURKES: [(isize, isize, i16); 7] = [
(2, 1, 2), (2, 1, 2),
]; ];
struct Dither { struct Dither {
// the width of the image to be dithered // the width of the image to be dithered
width: usize, width: usize,
@ -463,7 +483,7 @@ impl Dither {
for y in 0..height { for y in 0..height {
for x in 0..width { for x in 0..width {
let pixel = match image.get(x, y) { let pixel = match image.get(x, y) {
Some(rgb) => self.pixel(rgb.to_grey()), Some(grey) => self.pixel(*grey),
None => PixelColor::Dark, None => PixelColor::Dark,
}; };
pixels.push(pixel); pixels.push(pixel);
@ -474,8 +494,6 @@ impl Dither {
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -13,7 +13,7 @@ pub use apps::*;
#[cfg(feature="ditherpunk")] #[cfg(feature="ditherpunk")]
pub mod bitmap; pub mod bitmap;
#[cfg(feature="ditherpunk")] #[cfg(feature="ditherpunk")]
pub use bitmap::{Bitmap, Img, RGB}; pub use bitmap::{Bitmap, Img, PixelType};
use graphics_server::api::{TextOp, TextView}; use graphics_server::api::{TextOp, TextView};
use graphics_server::api::{Gid, Line, Circle, RoundedRectangle, TokenClaim}; use graphics_server::api::{Gid, Line, Circle, RoundedRectangle, TokenClaim};

View file

@ -22,12 +22,10 @@ tts-frontend = {path="../tts"}
locales = {path = "../../locales"} locales = {path = "../../locales"}
bit_field = "0.9.0" bit_field = "0.9.0"
fast_image_resize = {version = "0.9.2", 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 = ["fast_image_resize"] ditherpunk = []
default = [] default = []

View file

@ -10,8 +10,6 @@ use gam::*;
use num_traits::*; use num_traits::*;
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
use std::convert::TryInto; use std::convert::TryInto;
#[cfg(feature = "ditherpunk")]
use std::num::NonZeroU32;
use xous::{send_message, Message, CID}; use xous::{send_message, Message, CID};
use xous_ipc::Buffer; use xous_ipc::Buffer;
@ -199,7 +197,8 @@ impl Modals {
pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> {
self.lock(); self.lock();
// resize and/or rotate // resize and/or rotate
let (modal_width, modal_height) = (gam::IMG_MODAL_WIDTH as f32, gam::IMG_MODAL_HEIGHT as f32); let (modal_width, modal_height) =
(gam::IMG_MODAL_WIDTH as f32, gam::IMG_MODAL_HEIGHT as f32);
let (w, h, _) = img.size(); let (w, h, _) = img.size();
let (img_width, img_height) = (w as f32, h as f32); let (img_width, img_height) = (w as f32, h as f32);
@ -210,10 +209,16 @@ impl Modals {
} else if landscape_scale >= 1.0 { } else if landscape_scale >= 1.0 {
Bitmap::from(img).rotate90() Bitmap::from(img).rotate90()
} else if portrait_scale >= landscape_scale { } else if portrait_scale >= landscape_scale {
let resized = Modals::resize_image(img, portrait_scale); let resized = img.resize(
(portrait_scale * img_width) as usize,
(portrait_scale * img_height) as usize,
);
Bitmap::from(&resized) Bitmap::from(&resized)
} else { } else {
let resized = Modals::resize_image(img, landscape_scale); let resized = img.resize(
(landscape_scale * img_width) as usize,
(landscape_scale * img_height) as usize,
);
Bitmap::from(&resized).rotate90() Bitmap::from(&resized).rotate90()
}; };
@ -223,7 +228,9 @@ impl Modals {
// center image in modal // center image in modal
let center = Point::new( let center = Point::new(
((gam::IMG_MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), ((gam::IMG_MODAL_WIDTH - bm_width) / 2).try_into().unwrap(),
((gam::IMG_MODAL_HEIGHT - bm_height) / 2).try_into().unwrap(), ((gam::IMG_MODAL_HEIGHT - bm_height) / 2)
.try_into()
.unwrap(),
); );
bm.translate(center); bm.translate(center);
@ -246,38 +253,6 @@ impl Modals {
Ok(()) Ok(())
} }
#[allow(dead_code)]
#[cfg(feature = "ditherpunk")]
fn resize_image(img: &Img, scale: f32) -> Img {
let (w, h, _) = img.size();
let (img_width, img_height) = (w as f32, h as f32);
let height: u32 = (scale * img_height).floor() as u32;
let width: u32 = (scale * img_width).floor() as u32;
let fir_img = fast_image_resize::Image::from_vec_u8(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
img.into_raw(),
fast_image_resize::PixelType::U8x3,
)
.unwrap();
// Create container for data of destination image
let mut resized = fast_image_resize::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 = fast_image_resize::Resizer::new(fast_image_resize::ResizeAlg::Nearest);
resizer.resize(&fir_img.view(), &mut rsz_view).unwrap();
let width: usize = resized.width().get().try_into().unwrap();
Img::new(resized.into_vec(), width )
}
pub fn start_progress( pub fn start_progress(
&self, &self,
title: &str, title: &str,
@ -530,10 +505,14 @@ impl Modals {
fn unlock(&self) { fn unlock(&self) {
self.have_lock.set(false); self.have_lock.set(false);
} }
pub fn conn(&self) -> CID {self.conn} pub fn conn(&self) -> CID {
self.conn
}
/// Don't leak this token outside of your server, otherwise, another server can pretend to be you and /// Don't leak this token outside of your server, otherwise, another server can pretend to be you and
/// steal your modal information! /// steal your modal information!
pub fn token(&self) -> [u32; 4] {self.token} pub fn token(&self) -> [u32; 4] {
self.token
}
} }
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
@ -561,7 +540,10 @@ impl Drop for Modals {
/// ///
/// This function is "broken out" so that it can be called from a thread without having /// This function is "broken out" so that it can be called from a thread without having
/// to wrap a mutex around the primary Modals structure. /// to wrap a mutex around the primary Modals structure.
pub fn dynamic_notification_blocking_listener(token: [u32; 4], conn: CID) -> Result<Option<char>, xous::Error> { pub fn dynamic_notification_blocking_listener(
token: [u32; 4],
conn: CID,
) -> Result<Option<char>, xous::Error> {
match send_message( match send_message(
conn, conn,
Message::new_blocking_scalar( Message::new_blocking_scalar(
@ -571,7 +553,9 @@ pub fn dynamic_notification_blocking_listener(token: [u32; 4], conn: CID) -> Res
token[2] as usize, token[2] as usize,
token[3] as usize, token[3] as usize,
), ),
).expect("couldn't listen") { )
.expect("couldn't listen")
{
xous::Result::Scalar2(is_some, code) => { xous::Result::Scalar2(is_some, code) => {
if is_some == 1 { if is_some == 1 {
let c = char::from_u32(code as u32).unwrap_or('\u{0000}'); let c = char::from_u32(code as u32).unwrap_or('\u{0000}');
@ -583,6 +567,6 @@ pub fn dynamic_notification_blocking_listener(token: [u32; 4], conn: CID) -> Res
Ok(None) Ok(None)
} }
} }
_ => Err(xous::Error::InternalError) _ => Err(xous::Error::InternalError),
} }
} }

View file

@ -2,6 +2,8 @@ use super::*;
use gam::*; use gam::*;
use std::thread; use std::thread;
use xous_names::XousNames; use xous_names::XousNames;
#[cfg(feature = "ditherpunk")]
use bitmap::PixelType;
const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"]; const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"];
@ -189,14 +191,7 @@ fn clifford() -> Img {
buf[i] -= 1; buf[i] -= 1;
} }
} }
let mut rgb = vec![0u8; 3 * buf.len()]; Img::new(buf, SIZE.try_into().unwrap(), PixelType::U8)
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>> { fn test_validator(input: TextEntryPayload) -> Option<xous_ipc::String<256>> {

View file

@ -17,7 +17,7 @@ use dns::Dns; // necessary to work around https://github.com/rust-lang/rust/issu
#[cfg(feature="ditherpunk")] #[cfg(feature="ditherpunk")]
use std::str::FromStr; use std::str::FromStr;
#[cfg(feature="ditherpunk")] #[cfg(feature="ditherpunk")]
use gam::Img; use gam::{Img, PixelType};
pub struct NetCmd { pub struct NetCmd {
callback_id: Option<u32>, callback_id: Option<u32>,
@ -389,7 +389,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd {
let (width, height) = (info.width, info.height); let (width, height) = (info.width, info.height);
log::info!("image is {} x {}", width, height); log::info!("image is {} x {}", width, height);
log::info!("some image bytes: {:x?}", &buf[..64]); log::info!("some image bytes: {:x?}", &buf[..64]);
let img = Img::new(buf, width as usize); let img = Img::new(buf, width as usize, PixelType::U8x3);
modals.show_image(&img).expect("show image modal failed"); modals.show_image(&img).expect("show image modal failed");
write!(ret, "Image of {} bytes read successfully", content_length).unwrap(); write!(ret, "Image of {} bytes read successfully", content_length).unwrap();