implement gam Bitmap Dither

- remove depencency on dither crate
- Burkes diffusion scheme
This commit is contained in:
nworbnhoj 2022-06-10 19:49:18 +10:00
parent f7395ae0d5
commit 0bf71b84c1
5 changed files with 226 additions and 78 deletions

View file

@ -34,15 +34,11 @@ tts-frontend = {path="../tts"}
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, optional = true }
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = {path = "../../utralib"}
[features]
debugprint = []
tts = []
ditherpunk = ["dither"]
# default = ["debugprint"] # "debugprint"
default = []

View file

@ -1,5 +1,4 @@
use core::cmp::{max, min};
use dither::prelude::{Dither, Img, RGB};
use graphics_server::api::*;
use graphics_server::PixelColor;
use std::convert::TryInto;
@ -236,45 +235,34 @@ impl From<[Option<Tile>; 6]> for Bitmap {
}
}
impl From<Img<RGB<u8>>> for Bitmap {
fn from(image: Img<RGB<u8>>) -> Self {
let img = image.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());
let ditherer = dither::ditherer::BURKES;
let output_img = ditherer.dither(bw_img, quantize);
let bm_width: usize = output_img.width().try_into().unwrap();
let img_vec = output_img.into_vec();
let bm_height = img_vec.len() / bm_width;
let bm_bottom = (bm_height - 1) as i16;
let bm_right = (bm_width - 1) as i16;
let bm_br = Point::new(bm_right, bm_bottom);
impl From<Img> for Bitmap {
fn from(image: Img) -> Self {
let (bm_width, bm_height, _) = image.size();
let bm_bottom = bm_height - 1;
let bm_right = bm_width - 1;
let bm_br = Point::new(bm_right as i16, bm_bottom as i16);
let bound = Rectangle::new(Point::new(0, 0), bm_br);
let (tile_size, tile_width_words) = Bitmap::tile_spec(bm_br);
let tile_height = tile_size.y as usize;
let tile_height: usize = tile_size.y.try_into().unwrap();
let tile_count = match bm_height % tile_height {
0 => bm_height / tile_height,
_ => bm_height / tile_height + 1,
};
let mut mosaic: Vec<Tile> = Vec::new();
let mut img_vec_index = 0;
let pixels = Dither::new(BURKES.to_vec()).dither(image);
let mut px_index = 0;
let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap();
let dark: usize = PixelColor::Dark.try_into().unwrap();
let light: usize = PixelColor::Light.try_into().unwrap();
for t in 0..tile_count {
let t_top = (t * tile_height) as i16;
let t_top = t * tile_height;
let t_left = 0;
let t_bottom = min(bm_bottom, ((t + 1) * tile_height - 1) as i16);
let t_bottom = min(bm_bottom, (t + 1) * tile_height - 1);
let t_right = tile_size.x - 1;
let t_tl = Point::new(t_left, t_top);
let t_br = Point::new(t_right, t_bottom);
let t_tl = Point::new(t_left, t_top.try_into().unwrap());
let t_br = Point::new(t_right, t_bottom.try_into().unwrap());
let t_bound = Rectangle::new(t_tl, t_br);
let mut tile = Tile::new(t_bound, tile_width_words as u16);
let mut tile = Tile::new(t_bound, tile_width_words.try_into().unwrap());
for y in t_top..=t_bottom {
let mut x = t_left;
while x <= t_right {
@ -283,12 +271,12 @@ impl From<Img<RGB<u8>>> for Bitmap {
if (x + w) > t_right {
continue;
}
let pixel = img_vec[img_vec_index];
let color = if pixel > 125.0 { dark } else { light };
let color = pixels[px_index] as usize;
word = word | (color << w);
img_vec_index += 1;
px_index += 1;
}
tile.set_word(Point::new(x, y), word.try_into().unwrap());
let anchor = Point::new(x.try_into().unwrap(), y.try_into().unwrap());
tile.set_word(anchor, word.try_into().unwrap());
x += bits_per_word;
}
}
@ -302,6 +290,192 @@ impl From<Img<RGB<u8>>> for Bitmap {
}
}
/*
* Image as a minimal flat buffer of RGB pixels; accessible by (x, y)
*
* 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)]
pub struct Img {
pixels: Vec<RGB>,
width: usize,
}
impl Img {
pub fn new(buf: Vec<u8>, width: usize) -> Self {
let px_len = buf.len() / 3;
let mut pixels: Vec<RGB> = Vec::with_capacity(px_len);
for px in 0..px_len {
let b = px * 3;
pixels.push(RGB {
0: buf[b],
1: buf[b + 1],
2: buf[b + 2],
});
}
Self { pixels, width }
}
pub fn get(&self, x: usize, y: usize) -> Option<&RGB> {
let i: usize = (y * self.width) + x;
self.pixels.get(i)
}
pub fn size(&self) -> (usize, usize, usize) {
let width: usize = self.width.try_into().unwrap();
let length: usize = self.pixels.len().try_into().unwrap();
let height: usize = length / width;
(width, height, length)
}
pub fn into_raw(&self) -> Vec<u8> {
let len = self.pixels.len() as usize;
let mut bytes: Vec<u8> = Vec::with_capacity(len);
for px in &self.pixels {
bytes.push(px.0);
bytes.push(px.1);
bytes.push(px.2);
}
bytes
}
}
/*
* 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
* the surrounding pixels. So the luminosity lost by forcing a pixel down to Black,
* results in the surrounding pixels incrementally more likely to round up to White.
* Pixels are processed from left to right and then top to bottom. The residual
* error from each Black/White determination is carried-forward to pixels to the
* right and below as per the diffusion scheme.
* https://tannerhelland.com/2012/12/28/dithering-eleven-algorithms-source-code.html
*
* author: nworbnhoj
*/
/// Burkes dithering diffusion scheme was chosen for its modest resource
/// requirements with impressive quality outcome.
/// Burkes dithering. Div=32.
/// - ` . . x 8 4`
/// - ` 2 4 8 4 2`
const BURKES: [(isize, isize, i16); 7] = [
// (dx, dy, mul)
(1, 0, 8),
(2, 0, 4),
//
(-2, 1, 2),
(-1, 1, 4),
(0, 1, 8),
(1, 1, 4),
(2, 1, 2),
];
struct Dither {
// the width of the image to be dithered
width: usize,
// the error diffusion scheme (dx, dy, multiplier)
diffusion: Vec<(isize, isize, i16)>,
// the sum of the multipliers in the diffusion
denominator: i16,
// a circular array of errors representing dy rows of the image,
err: Vec<i16>,
// the position in err representing the carry forward error for the current pixel
origin: usize,
}
impl Dither {
const THRESHOLD: i16 = u8::MAX as i16 / 2;
pub fn new(diffusion: Vec<(isize, isize, i16)>) -> Self {
let mut denominator: i16 = 0;
for (_, _, mul) in &diffusion {
denominator += mul;
}
Self {
width: 0,
diffusion,
denominator,
err: Vec::<i16>::new(),
origin: 0,
}
}
fn provision(&mut self, width: usize) {
self.width = width;
let (mut max_dx, mut max_dy) = (0, 0);
for (dx, dy, _) in &self.diffusion {
max_dx = max(*dx, max_dx);
max_dy = max(*dy, max_dy);
}
let length: usize = width * max_dy as usize + max_dx as usize + 1;
self.err = vec![0i16; length];
}
fn index(&self, dx: isize, dy: isize) -> usize {
let width: isize = self.width.try_into().unwrap();
let offset: usize = (width * dy + dx).try_into().unwrap();
let linear: usize = self.origin + offset;
linear % self.err.len()
}
fn next(&mut self) {
self.err[self.origin] = 0;
self.origin = self.index(1, 0);
}
fn get(&self) -> i16 {
self.err[self.origin] / self.denominator
}
fn carry(&mut self, err: i16) {
for (dx, dy, mul) in &self.diffusion {
let i = self.index(*dx, *dy);
self.err[i] += mul * err;
}
}
fn pixel(&mut self, grey: u8) -> PixelColor {
let grey: i16 = grey as i16 + self.get();
if grey < Dither::THRESHOLD {
self.carry(grey);
PixelColor::Dark
} else {
self.carry(grey - u8::MAX as i16);
PixelColor::Light
}
}
pub fn dither(&mut self, image: Img) -> Vec<PixelColor> {
let (width, height, length) = image.size();
self.provision(width.try_into().unwrap());
let mut pixels: Vec<PixelColor> = Vec::with_capacity(length);
for y in 0..height {
for x in 0..width {
let pixel = match image.get(x, y) {
Some(rgb) => self.pixel(rgb.to_grey()),
None => PixelColor::Dark,
};
pixels.push(pixel);
self.next();
}
}
pixels
}
}
#[cfg(test)]
mod tests {
use super::*;

View file

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

View file

@ -22,9 +22,6 @@ 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", default-features = false, optional = true }
fast_image_resize = {version = "0.9.2", optional = true}
png = {version = "0.17.5", optional = true}
jpeg-decoder = {version = "0.2.6", optional = true}
@ -34,5 +31,5 @@ utralib = { path = "../../utralib"}
[features]
tts = []
ditherpunk = ["dither", "fast_image_resize", "png", "jpeg-decoder"]
ditherpunk = ["fast_image_resize", "png", "jpeg-decoder"]
default = []

View file

@ -6,10 +6,6 @@ pub mod tests;
use bit_field::BitField;
use core::cell::Cell;
#[cfg(feature = "ditherpunk")]
use dither::prelude::{Img, RGB};
#[cfg(feature = "ditherpunk")]
use fast_image_resize as fir;
use gam::*;
use num_traits::*;
#[cfg(feature = "ditherpunk")]
@ -204,25 +200,26 @@ impl Modals {
const MODAL_HEIGHT: u32 = 370;
/// this blocks until the image has been dismissed.
#[cfg(feature = "ditherpunk")]
pub fn show_image(&self, img: &Img<RGB<u8>>) -> Result<(), xous::Error> {
pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> {
self.lock();
// resize and/or rotate
let (modal_width, modal_height) = (Modals::MODAL_WIDTH as f32, Modals::MODAL_HEIGHT as f32);
let (img_width, img_height) = (img.width() as f32, img.height() as f32);
let (w, h, _) = img.size();
let (img_width, img_height) = (w as f32, 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 rotate = false;
let mut modal_img = img.clone();
let modal_img;
if portrait_scale >= 1.0 {
// noop
modal_img = img.clone();
} else if landscape_scale >= 1.0 {
modal_img = img.clone();
rotate = true;
} else if portrait_scale >= landscape_scale {
modal_img = Modals::resize_image(modal_img, portrait_scale)
modal_img = Modals::resize_image(img.clone(), portrait_scale)
} else {
rotate = true;
modal_img = Modals::resize_image(modal_img, landscape_scale)
modal_img = Modals::resize_image(img.clone(), landscape_scale)
};
let mut bm = Bitmap::from(modal_img);
@ -258,30 +255,22 @@ impl Modals {
}
#[cfg(feature = "ditherpunk")]
fn resize_image(img: Img<RGB<u8>>, scale: f32) -> Img<RGB<u8>> {
let (img_width, img_height) = (img.width() as f32, img.height() as f32);
let height: u32 = (img_height * scale).floor() as u32;
let width: u32 = (img_width * scale).floor() as u32;
let b = 3 * width * height;
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 mut bytes: Vec<u8> = Vec::with_capacity(b.try_into().unwrap());
let pixels = img.into_vec();
for px in pixels {
bytes.push(px.0);
bytes.push(px.1);
bytes.push(px.2);
}
let fir_img = fir::Image::from_vec_u8(
let fir_img = fast_image_resize::Image::from_vec_u8(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
bytes,
fir::PixelType::U8x3,
img.into_raw(),
fast_image_resize::PixelType::U8x3,
)
.unwrap();
// Create container for data of destination image
let mut resized = fir::Image::new(
let mut resized = fast_image_resize::Image::new(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
fir_img.pixel_type(),
@ -289,19 +278,11 @@ impl Modals {
let mut rsz_view = resized.view_mut();
// resize image with fastest available algorithm
let mut resizer = fir::Resizer::new(fir::ResizeAlg::Nearest);
let mut resizer = fast_image_resize::Resizer::new(fast_image_resize::ResizeAlg::Nearest);
resizer.resize(&fir_img.view(), &mut rsz_view).unwrap();
// convert back into an Img<RGB<u8>>
let px = resized.width().get() * resized.height().get();
let mut pixels: Vec<RGB<u8>> = Vec::with_capacity(px.try_into().unwrap());
let bytes = resized.into_vec();
let mut i = 0;
while i < bytes.len() {
pixels.push(RGB::from([bytes[i], bytes[i + 1], bytes[i + 2]]));
i += 3;
}
Img::new(pixels, width).expect("failed to create Img")
let width: usize = resized.width().get().try_into().unwrap();
Img::new(resized.into_vec(), width )
}
pub fn start_progress(