mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-19 09:00:54 +00:00
implement Shrink adaptor iterator
This commit is contained in:
parent
0f971fcc33
commit
549178e148
2 changed files with 201 additions and 84 deletions
|
|
@ -57,6 +57,56 @@ impl Bitmap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_resize(image: &Img, width: usize) -> Self {
|
||||||
|
let (img_width, _, _) = image.size();
|
||||||
|
let pixels = image.iter().shrink(img_width, width).collect();
|
||||||
|
let image = Img::new(pixels, width, PixelType::U8);
|
||||||
|
let (img_width, img_height, _) = image.size();
|
||||||
|
|
||||||
|
let bm_bottom = img_height - 1;
|
||||||
|
let bm_right = img_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: usize = tile_size.y.try_into().unwrap();
|
||||||
|
let tile_count = match img_height % tile_height {
|
||||||
|
0 => img_height / tile_height,
|
||||||
|
_ => img_height / tile_height + 1,
|
||||||
|
};
|
||||||
|
let mut mosaic: Vec<Tile> = Vec::new();
|
||||||
|
|
||||||
|
let words = Dither::new(BURKES.to_vec()).dither(&image);
|
||||||
|
let mut wd_index = 0;
|
||||||
|
let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap();
|
||||||
|
for t in 0..tile_count {
|
||||||
|
let t_top = t * tile_height;
|
||||||
|
let t_left = 0;
|
||||||
|
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.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.try_into().unwrap());
|
||||||
|
for y in t_top..=t_bottom {
|
||||||
|
let mut x = t_left;
|
||||||
|
while x <= t_right {
|
||||||
|
let word = words[wd_index];
|
||||||
|
wd_index += 1;
|
||||||
|
let anchor = Point::new(x.try_into().unwrap(), y.try_into().unwrap());
|
||||||
|
tile.set_word(anchor, word.try_into().unwrap());
|
||||||
|
x += bits_per_word;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mosaic.push(tile);
|
||||||
|
}
|
||||||
|
Self {
|
||||||
|
bound,
|
||||||
|
tile_size,
|
||||||
|
mosaic,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn tile_spec(bm_size: Point) -> (Point, i16) {
|
fn tile_spec(bm_size: Point) -> (Point, i16) {
|
||||||
let bm_width_bits = 1 + bm_size.x as usize;
|
let bm_width_bits = 1 + bm_size.x as usize;
|
||||||
let mut tile_width_bits = bm_width_bits;
|
let mut tile_width_bits = bm_width_bits;
|
||||||
|
|
@ -237,52 +287,13 @@ impl From<[Option<Tile>; 6]> for Bitmap {
|
||||||
|
|
||||||
impl From<&Img> for Bitmap {
|
impl From<&Img> for Bitmap {
|
||||||
fn from(image: &Img) -> Self {
|
fn from(image: &Img) -> Self {
|
||||||
let (bm_width, bm_height, _) = image.size();
|
let (img_width, _, _) = image.size();
|
||||||
let bm_bottom = bm_height - 1;
|
Bitmap::new_resize(image, img_width)
|
||||||
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: 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 words = Dither::new(BURKES.to_vec()).dither(&image);
|
|
||||||
let mut wd_index = 0;
|
|
||||||
let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap();
|
|
||||||
for t in 0..tile_count {
|
|
||||||
let t_top = t * tile_height;
|
|
||||||
let t_left = 0;
|
|
||||||
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.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.try_into().unwrap());
|
|
||||||
for y in t_top..=t_bottom {
|
|
||||||
let mut x = t_left;
|
|
||||||
while x <= t_right {
|
|
||||||
let word = words[wd_index];
|
|
||||||
wd_index += 1;
|
|
||||||
let anchor = Point::new(x.try_into().unwrap(), y.try_into().unwrap());
|
|
||||||
tile.set_word(anchor, word.try_into().unwrap());
|
|
||||||
x += bits_per_word;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mosaic.push(tile);
|
|
||||||
}
|
|
||||||
Self {
|
|
||||||
bound,
|
|
||||||
tile_size,
|
|
||||||
mosaic,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// **********************************************************************
|
||||||
|
|
||||||
pub enum PixelType {
|
pub enum PixelType {
|
||||||
U8,
|
U8,
|
||||||
U8x3,
|
U8x3,
|
||||||
|
|
@ -310,12 +321,10 @@ impl<'a, I: Iterator<Item = &'a u8>> Iterator for GreyScale<I> {
|
||||||
const B: u32 = 722;
|
const B: u32 = 722;
|
||||||
const BLACK: u32 = R + G + B;
|
const BLACK: u32 = R + G + B;
|
||||||
match self.px_type {
|
match self.px_type {
|
||||||
PixelType::U8 => {
|
PixelType::U8 => match self.iter.next() {
|
||||||
match self.iter.next() {
|
Some(gr) => Some(*gr),
|
||||||
Some(gr) => Some(*gr),
|
None => None,
|
||||||
None => None,
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
PixelType::U8x3 => {
|
PixelType::U8x3 => {
|
||||||
let r = self.iter.next();
|
let r = self.iter.next();
|
||||||
let g = self.iter.next();
|
let g = self.iter.next();
|
||||||
|
|
@ -347,6 +356,139 @@ pub trait GreyScaleIterator<'a>: Iterator<Item = &'a u8> + Sized {
|
||||||
|
|
||||||
impl<'a, I: Iterator<Item = &'a u8>> GreyScaleIterator<'a> for I {}
|
impl<'a, I: Iterator<Item = &'a u8>> GreyScaleIterator<'a> for I {}
|
||||||
|
|
||||||
|
// **********************************************************************
|
||||||
|
|
||||||
|
pub struct Shrink<I> {
|
||||||
|
/// iterator over inbound pixels
|
||||||
|
iter: I,
|
||||||
|
/// width of the outbound image
|
||||||
|
out_width: usize,
|
||||||
|
/// the scale factor between inbound and outbound images (ie in_width/out_width)
|
||||||
|
scale: f32,
|
||||||
|
/// a pre-tabulated list of the trailing edge of each inbound strip of pixels
|
||||||
|
in_x_cap: Vec<u16>,
|
||||||
|
/// the current y coord of the inbound image
|
||||||
|
in_y: usize,
|
||||||
|
/// the current x coord of the outbound image
|
||||||
|
out_x: usize,
|
||||||
|
/// the current y coord of the outbound image
|
||||||
|
out_y: usize,
|
||||||
|
/// a buffer the width of the outbound image to stove horizontal averages
|
||||||
|
buf: Vec<u16>,
|
||||||
|
/// the width of the current stri in the inbound image
|
||||||
|
y_div: u16,
|
||||||
|
/// the x coord of the final pixel in the inbound image
|
||||||
|
out_x_last: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, I: Iterator<Item = &'a u8>> Shrink<I> {
|
||||||
|
fn new(iter: I, in_width: usize, out_width: usize) -> Shrink<I> {
|
||||||
|
let scale = in_width as f32 / out_width as f32;
|
||||||
|
// set up a buffer to average the surrounding pixels
|
||||||
|
let buf: Vec<u16> = if scale <= 1.0 {
|
||||||
|
Vec::new()
|
||||||
|
} else {
|
||||||
|
vec![0u16; out_width]
|
||||||
|
};
|
||||||
|
|
||||||
|
// Pretabulate horizontal pixel positions
|
||||||
|
let mut in_x_cap: Vec<u16> = Vec::with_capacity(out_width);
|
||||||
|
let max_width: u16 = (in_width - 1).try_into().unwrap();
|
||||||
|
for out_x in 1..=out_width {
|
||||||
|
let in_x: u16 = (scale * out_x as f32) as u16;
|
||||||
|
in_x_cap.push((in_x).min(max_width));
|
||||||
|
}
|
||||||
|
let out_x_last = out_width;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
iter,
|
||||||
|
out_width,
|
||||||
|
scale,
|
||||||
|
in_x_cap,
|
||||||
|
in_y: 0,
|
||||||
|
out_x: 0,
|
||||||
|
out_y: 1,
|
||||||
|
buf,
|
||||||
|
y_div: 0,
|
||||||
|
out_x_last,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn next_xy(&self) -> (usize, usize) {
|
||||||
|
(self.out_x, self.out_y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Adaptor Iterator to shrink an image dimensions from in_width to out_width
|
||||||
|
impl<'a, I: Iterator<Item = &'a u8>> Iterator for Shrink<I> {
|
||||||
|
type Item = u8;
|
||||||
|
|
||||||
|
/// Shrinks an image from in_width to out_width
|
||||||
|
/// The algorithm divides the inbound image into vertical and horivontal strips,
|
||||||
|
/// correspoding to the columns and rows of the outbound image. Each outbound
|
||||||
|
/// pixel is the average of the pixels contained within each intersaction of
|
||||||
|
/// vertical and horizontal strips. For example, when in_width = 3 x out_width
|
||||||
|
/// each outbound pixel will be the average of 9 pixels in a 3x3 inbound block.
|
||||||
|
/// Note that with a non-integer scale the strips will be of variable width.
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
// if there is no reduction in image size then simple return image as-is
|
||||||
|
if self.scale <= 1.0 {
|
||||||
|
return match self.iter.next() {
|
||||||
|
Some(pixel) => Some(*pixel),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// processed the last inbound pixel
|
||||||
|
if self.out_x > self.out_x_last {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
// take the average of pixels in the horizontal, and then vertical.
|
||||||
|
let in_y_cap = (self.scale * self.out_y as f32) as usize;
|
||||||
|
while self.in_y < in_y_cap {
|
||||||
|
let mut in_x = 0;
|
||||||
|
for (out_x, in_x_cap) in self.in_x_cap.iter().enumerate() {
|
||||||
|
let mut x_total: u16 = 0;
|
||||||
|
let mut x_div: u16 = 0;
|
||||||
|
while in_x <= *in_x_cap {
|
||||||
|
x_total += match self.iter.next() {
|
||||||
|
Some(pixel) => *pixel as u16,
|
||||||
|
None => {
|
||||||
|
self.out_x_last = out_x - 1;
|
||||||
|
0
|
||||||
|
}
|
||||||
|
};
|
||||||
|
in_x += 1;
|
||||||
|
x_div += 1;
|
||||||
|
}
|
||||||
|
self.buf[out_x] += x_total / x_div;
|
||||||
|
}
|
||||||
|
self.in_y += 1;
|
||||||
|
self.y_div += 1;
|
||||||
|
}
|
||||||
|
// calculate the average of the sum of pixels in the buffer, and reset buffer
|
||||||
|
let pixel: u8 = (self.buf[self.out_x] / self.y_div).try_into().unwrap();
|
||||||
|
self.buf[self.out_x] = 0;
|
||||||
|
// prepare for the next pixel in the row or column
|
||||||
|
self.out_x += 1;
|
||||||
|
if self.out_x >= self.out_width {
|
||||||
|
self.out_x = 0;
|
||||||
|
self.out_y += 1;
|
||||||
|
self.y_div = 0;
|
||||||
|
}
|
||||||
|
Some(pixel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ShrinkIterator<'a>: Iterator<Item = &'a u8> + Sized {
|
||||||
|
fn shrink(self, in_width: usize, out_width: usize) -> Shrink<Self> {
|
||||||
|
Shrink::new(self, in_width, out_width)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, I: Iterator<Item = &'a u8>> ShrinkIterator<'a> for I {}
|
||||||
|
|
||||||
|
// **********************************************************************
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y)
|
* Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y)
|
||||||
*
|
*
|
||||||
|
|
@ -355,8 +497,8 @@ impl<'a, I: Iterator<Item = &'a u8>> GreyScaleIterator<'a> for I {}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Img {
|
pub struct Img {
|
||||||
pixels: Vec<u8>,
|
pub pixels: Vec<u8>,
|
||||||
width: usize,
|
pub width: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Img {
|
impl Img {
|
||||||
|
|
@ -386,30 +528,13 @@ impl Img {
|
||||||
pub fn as_slice(&self) -> &[u8] {
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
self.pixels.as_slice()
|
self.pixels.as_slice()
|
||||||
}
|
}
|
||||||
// simply retains the closest pixel
|
}
|
||||||
pub fn resize(&self, width: usize, height: usize) -> Img {
|
|
||||||
let mut buf = vec![0u8; (width * height).try_into().unwrap()];
|
|
||||||
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
|
impl Deref for Img {
|
||||||
let mut self_x_tab: Vec<usize> = Vec::with_capacity(width);
|
type Target = Vec<u8>;
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
for buf_y in 0..height {
|
fn deref(&self) -> &Self::Target {
|
||||||
let self_y = ((self_y_base + y_scale * buf_y as f32) as usize).min(self_height - 1);
|
&self.pixels
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -209,17 +209,9 @@ 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 = img.resize(
|
Bitmap::new_resize(img, (portrait_scale * img_width) as usize)
|
||||||
(portrait_scale * img_width) as usize,
|
|
||||||
(portrait_scale * img_height) as usize,
|
|
||||||
);
|
|
||||||
Bitmap::from(&resized)
|
|
||||||
} else {
|
} else {
|
||||||
let resized = img.resize(
|
Bitmap::new_resize(img, (landscape_scale * img_width) as usize).rotate90()
|
||||||
(landscape_scale * img_width) as usize,
|
|
||||||
(landscape_scale * img_height) as usize,
|
|
||||||
);
|
|
||||||
Bitmap::from(&resized).rotate90()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bm_width, bm_height) = bm.size();
|
let (bm_width, bm_height) = bm.size();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue