From 0f971fcc33831e36e64063a7f2214b2ad40619ef Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 20 Jun 2022 08:03:03 +1000 Subject: [PATCH 1/7] implement 3xU8 RGB to U8 Greyscale adapter iterator --- services/gam/src/bitmap.rs | 81 ++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 605e382c7..49f7f177e 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -289,6 +289,64 @@ pub enum PixelType { U8x4, } +pub struct GreyScale { + iter: I, + px_type: PixelType, +} + +impl<'a, I: Iterator> GreyScale { + fn new(iter: I, px_type: PixelType) -> GreyScale { + Self { iter, px_type } + } +} + +impl<'a, I: Iterator> Iterator for GreyScale { + type Item = u8; + + fn next(&mut self) -> Option { + // chromatic coversion from RGB to Greyscale + const R: u32 = 2126; + const G: u32 = 7152; + const B: u32 = 722; + const BLACK: u32 = R + G + B; + match self.px_type { + PixelType::U8 => { + match self.iter.next() { + Some(gr) => Some(*gr), + None => None, + } + } + PixelType::U8x3 => { + let r = self.iter.next(); + let g = self.iter.next(); + let b = self.iter.next(); + if r.is_some() && g.is_some() && b.is_some() { + let grey_r = R * *r.unwrap() as u32; + let grey_g = G * *g.unwrap() as u32; + let grey_b = B * *b.unwrap() as u32; + let grey: u8 = ((grey_r + grey_g + grey_b) / BLACK).try_into().unwrap(); + Some(grey) + } else { + None + } + } + _ => { + log::warn!("unsupported PixelType"); + None + } + } + } +} + +pub trait GreyScaleIterator<'a>: Iterator + Sized { + /// converts pixels of PixelType to u8 greyscale + fn to_grey(self, px_type: PixelType) -> GreyScale { + GreyScale::new(self, px_type) + } +} + +impl<'a, I: Iterator> GreyScaleIterator<'a> for I {} + /* * Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y) * @@ -303,33 +361,16 @@ pub struct Img { impl Img { pub fn new(buf: Vec, width: usize, px_type: PixelType) -> Self { - const R: u32 = 2126; - 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 = Vec::with_capacity(px_len); - for px in 0..px_len { - let pixel: u8 = match px_type { - PixelType::U8 => buf[px], - PixelType::U8x3 => { - let b = px * 3; - 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 - } - }; + + for pixel in buf.iter().to_grey(px_type) { pixels.push(pixel); - } + } Self { pixels, width } } pub fn get(&self, x: usize, y: usize) -> Option<&u8> { From 549178e148ef0f1b5b9b66bc6f942893511e4cf0 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 21 Jun 2022 08:48:30 +1000 Subject: [PATCH 2/7] implement Shrink adaptor iterator --- services/gam/src/bitmap.rs | 273 +++++++++++++++++++++++++++---------- services/modals/src/lib.rs | 12 +- 2 files changed, 201 insertions(+), 84 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 49f7f177e..2f4959504 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -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 = 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) { let bm_width_bits = 1 + bm_size.x as usize; let mut tile_width_bits = bm_width_bits; @@ -237,52 +287,13 @@ impl From<[Option; 6]> for Bitmap { 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: 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 = 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, - } + let (img_width, _, _) = image.size(); + Bitmap::new_resize(image, img_width) } } +// ********************************************************************** + pub enum PixelType { U8, U8x3, @@ -310,12 +321,10 @@ impl<'a, I: Iterator> Iterator for GreyScale { const B: u32 = 722; const BLACK: u32 = R + G + B; match self.px_type { - PixelType::U8 => { - match self.iter.next() { - Some(gr) => Some(*gr), - None => None, - } - } + PixelType::U8 => match self.iter.next() { + Some(gr) => Some(*gr), + None => None, + }, PixelType::U8x3 => { let r = self.iter.next(); let g = self.iter.next(); @@ -347,6 +356,139 @@ pub trait GreyScaleIterator<'a>: Iterator + Sized { impl<'a, I: Iterator> GreyScaleIterator<'a> for I {} +// ********************************************************************** + +pub struct Shrink { + /// 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, + /// 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, + /// 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> Shrink { + fn new(iter: I, in_width: usize, out_width: usize) -> Shrink { + let scale = in_width as f32 / out_width as f32; + // set up a buffer to average the surrounding pixels + let buf: Vec = if scale <= 1.0 { + Vec::new() + } else { + vec![0u16; out_width] + }; + + // Pretabulate horizontal pixel positions + let mut in_x_cap: Vec = 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> Iterator for Shrink { + 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 { + // 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 + Sized { + fn shrink(self, in_width: usize, out_width: usize) -> Shrink { + Shrink::new(self, in_width, out_width) + } +} + +impl<'a, I: Iterator> ShrinkIterator<'a> for I {} + +// ********************************************************************** + /* * Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y) * @@ -355,8 +497,8 @@ impl<'a, I: Iterator> GreyScaleIterator<'a> for I {} #[derive(Debug)] pub struct Img { - pixels: Vec, - width: usize, + pub pixels: Vec, + pub width: usize, } impl Img { @@ -370,7 +512,7 @@ impl Img { for pixel in buf.iter().to_grey(px_type) { pixels.push(pixel); - } + } Self { pixels, width } } pub fn get(&self, x: usize, y: usize) -> Option<&u8> { @@ -386,30 +528,13 @@ impl Img { pub fn as_slice(&self) -> &[u8] { 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 - let mut self_x_tab: Vec = 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)); - } +impl Deref for Img { + type Target = Vec; - 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) + fn deref(&self) -> &Self::Target { + &self.pixels } } diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 4bdfaeb31..472ae14c0 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -209,17 +209,9 @@ impl Modals { } else if landscape_scale >= 1.0 { Bitmap::from(img).rotate90() } else if portrait_scale >= landscape_scale { - let resized = img.resize( - (portrait_scale * img_width) as usize, - (portrait_scale * img_height) as usize, - ); - Bitmap::from(&resized) + Bitmap::new_resize(img, (portrait_scale * img_width) as usize) } else { - let resized = img.resize( - (landscape_scale * img_width) as usize, - (landscape_scale * img_height) as usize, - ); - Bitmap::from(&resized).rotate90() + Bitmap::new_resize(img, (landscape_scale * img_width) as usize).rotate90() }; let (bm_width, bm_height) = bm.size(); From 29925a5caa6a26aa28f8a07591e389421b69fd86 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 28 Jun 2022 07:31:18 +1000 Subject: [PATCH 3/7] implement Dither adaptor iterator --- services/gam/src/bitmap.rs | 278 ++++++++++++----------- services/graphics-server/src/api/tile.rs | 74 +++++- 2 files changed, 211 insertions(+), 141 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 2f4959504..c21532244 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -22,109 +22,89 @@ use std::ops::Deref; #[derive(Debug)] pub struct Bitmap { + width: usize, pub bound: Rectangle, - tile_size: Point, + tile_bits: usize, mosaic: Vec, } impl Bitmap { pub fn new(size: Point) -> Self { - let bound = Rectangle::new(Point::new(0, 0), size); - log::trace!("new Bitmap {:?}", bound); - - let (tile_size, tile_width_words) = Bitmap::tile_spec(size); - let tile_height = tile_size.y as usize; - let bm_height = (size.y + 1) as usize; - let tile_count = match bm_height % tile_height { - 0 => bm_height / tile_height, - _ => bm_height / tile_height + 1, - }; - let mut mosaic: Vec = Vec::new(); - for y in 0..tile_count { - let tl = Point::new(0, (y * tile_height) as i16); - let mut br = Point::new(tile_size.x - 1, ((y + 1) * tile_height - 1) as i16); - if br.y > size.y { - br.y = size.y; + let mut tl = Point::new(0, 0); + let mut br = Point::new(size.x, 0); + let mut tile_bits = 0; + while tl.y <= size.y { + let mut tile = Tile::new(Rectangle::new(tl, br)); + let max_bound = tile.max_bound(); + br = if max_bound.br.y > size.y { + size + } else { + Point::new(size.x, max_bound.br.y) + }; + tile.set_bound(Rectangle::new(tl, br)); + if tile_bits == 0 { + tile_bits = ((br.x - tl.x + 1) * (br.y - tl.y + 1)) as usize; } - let tile = Tile::new(Rectangle::new(tl, br), tile_width_words as u16); mosaic.push(tile); + tl = Point::new(0, br.y + 1); + br = Point::new(size.x, tl.y); } Self { - bound, - tile_size, + width: size.x as usize + 1, + bound: Rectangle::new(Point::new(0, 0), size), + tile_bits, //: (size.x as usize + 1) * (size.y as usize + 1), mosaic, } } 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 burkes = BURKES.to_vec(); + let words = image.iter().shrink(img_width, width).dither(&burkes, width); - 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 = 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; - } + let single_line = Point::new((width - 1).try_into().unwrap(), 0); + let mut bound = Rectangle::new(Point::new(0, 0), single_line); + let mut tile = Tile::new(bound); + let mut blank_tile = true; + let (mut x, mut y) = (0, 0); + for word in words { + tile.set_word(Point::new(x, y), word); + blank_tile = false; + x += BITS_PER_WORD as i16; + if x >= width.try_into().unwrap() { + (x, y) = (0, y + 1); } + if y > tile.max_bound().br.y { + mosaic.push(tile); + bound = Rectangle::new( + Point::new(x, y), + Point::new((width - 1).try_into().unwrap(), y), + ); + tile = Tile::new(bound); + blank_tile = true; + } + } + if !blank_tile { + bound.br = Point::new((width - 1).try_into().unwrap(), y - 1); + tile.crop(bound); mosaic.push(tile); } + + bound.tl = Point::new(0, 0); + let max = tile.max_bound(); + let tile_bits = width as i16 * (max.br.y - max.tl.y + 1); + Self { + width, bound, - tile_size, + tile_bits: tile_bits.try_into().unwrap(), mosaic, } } - fn tile_spec(bm_size: Point) -> (Point, i16) { - let bm_width_bits = 1 + bm_size.x as usize; - let mut tile_width_bits = bm_width_bits; - let tile_width_words = if bm_width_bits > BITS_PER_TILE { - log::warn!("Bitmap max width exceeded"); - tile_width_bits = WORDS_PER_TILE * BITS_PER_WORD; - WORDS_PER_TILE - } else { - match bm_width_bits % BITS_PER_WORD { - 0 => bm_width_bits / BITS_PER_WORD, - _ => bm_width_bits / BITS_PER_WORD + 1, - } - }; - let tile_height_bits = WORDS_PER_TILE / tile_width_words; - let tile_size = Point::new(tile_width_bits as i16, tile_height_bits as i16); - (tile_size, tile_width_words as i16) - } - #[allow(dead_code)] fn area(&self) -> u32 { let (x, y) = self.size(); @@ -139,10 +119,7 @@ impl Bitmap { if self.bound.intersects_point(point) { let x = point.x as usize; let y = point.y as usize; - let tile_width = self.tile_size.x as usize; - let tile_height = self.tile_size.y as usize; - let tile_size_bits = tile_width * tile_height; - (x + y * tile_width) / tile_size_bits + (x + y * self.width) / self.tile_bits } else { log::warn!("Out of bounds {:?}", point); 0 @@ -164,9 +141,19 @@ impl Bitmap { } 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"); + log::warn!( + "Bitmap Tile gaps: tile_area={} hull_area={} {:?}", + tile_area, + hull_area, + mosaic + ); } else if tile_area > hull_area { - log::warn!("Bitmap Tile overlap"); + log::warn!( + "Bitmap Tile overlap: tile_area={} hull_area={} {:?}", + tile_area, + hull_area, + mosaic + ); } Rectangle::new(hull_tl, hull_br) } @@ -234,6 +221,7 @@ impl Bitmap { y -= 1; b += 1; } + // rotate the block and write to r90 // beginning from top-left, and progressing right in strips, from top to bottom for w in 0..bits_per_word { @@ -278,8 +266,9 @@ impl From<[Option; 6]> for Bitmap { } Self { + width: (tile_size.x + 1) as usize, bound: Self::hull(&mosaic), - tile_size: tile_size, + tile_bits: (tile_size.x + 1) as usize * (tile_size.y + 1) as usize, mosaic: mosaic, } } @@ -399,7 +388,6 @@ impl<'a, I: Iterator> Shrink { in_x_cap.push((in_x).min(max_width)); } let out_x_last = out_width; - Self { iter, out_width, @@ -429,7 +417,7 @@ impl<'a, I: Iterator> Iterator for Shrink { /// 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. + /// Note that with a non-integer scale the strips will be of variable width ±1. fn next(&mut self) -> Option { // if there is no reduction in image size then simple return image as-is if self.scale <= 1.0 { @@ -444,7 +432,7 @@ impl<'a, I: Iterator> Iterator for Shrink { } // 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 { + 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; @@ -538,6 +526,8 @@ impl Deref for Img { } } +// ********************************************************************** + /* * 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 @@ -568,66 +558,74 @@ const BURKES: [(isize, isize, i16); 7] = [ (2, 1, 2), ]; -struct Dither { +pub struct Dither<'a, I> { + /// iterator over inbound pixels + iter: I, // the width of the image to be dithered width: usize, // the error diffusion scheme (dx, dy, multiplier) - diffusion: Vec<(isize, isize, i16)>, + diffusion: &'a 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, // the position in err representing the carry forward error for the current pixel origin: usize, + next_x: usize, + next_y: usize, } -impl Dither { - const THRESHOLD: i16 = u8::MAX as i16 / 2; - pub fn new(diffusion: Vec<(isize, isize, i16)>) -> Self { +const THRESHOLD: i16 = u8::MAX as i16 / 2; + +impl<'a, I: Iterator> Dither<'a, I> { + // const THRESHOLD: i16 = u8::MAX as i16 / 2; results in: cannot satisfy `<_ as Iterator>::Item == u8` + fn new(iter: I, diffusion: &'a Vec<(isize, isize, i16)>, width: usize) -> Dither { let mut denominator: i16 = 0; - for (_, _, mul) in &diffusion { + for (_, _, mul) in diffusion { denominator += mul; } - Self { - width: 0, - diffusion, - denominator, - err: Vec::::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 { + for (dx, dy, _) in 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]; + + Self { + iter, + width, + diffusion, + denominator, + err: vec![0i16; length], + origin: 0, + next_x: 0, + next_y: 0, + } } + + #[allow(dead_code)] + fn next_xy(&self) -> (usize, usize) { + (self.next_x, self.next_y) + } + 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 { + fn err(&self) -> i16 { self.err[self.origin] / self.denominator } fn carry(&mut self, err: i16) { - for (dx, dy, mul) in &self.diffusion { + 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 { + let grey: i16 = grey as i16 + self.err(); + if grey < THRESHOLD { self.carry(grey); PixelColor::Dark } else { @@ -635,32 +633,52 @@ impl Dither { PixelColor::Light } } - pub fn dither(&mut self, image: &Img) -> Vec { - let bits_per_word: u32 = BITS_PER_WORD.try_into().unwrap(); - let (width, height, _) = image.size(); - self.provision(width.try_into().unwrap()); - let mut words: Vec = Vec::with_capacity((1 + width / BITS_PER_WORD) * height); - for y in 0..height { - let (mut w, mut word): (Word, Word) = (0, 0); - for x in 0..width { - let color = match image.get(x, y) { - Some(grey) => self.pixel(*grey), - None => PixelColor::Dark, - }; - word = word | ((color as u32) << w); - w += 1; - if w >= bits_per_word { - words.push(word); - (w, word) = (0, 0); +} + +impl<'a, I: Iterator> Iterator for Dither<'a, I> { + type Item = u32; + + fn next(&mut self) -> Option { + let mut word = 0; + for w in 0..BITS_PER_WORD { + match self.iter.next() { + Some(grey) => { + let color = self.pixel(grey) as u32; + word = word | (color << w); } - self.next(); + None => { + if w > 0 { + continue; + } else { + return None; + } + } + }; + + // reset and step forward err buffer and next_x coord + self.err[self.origin] = 0; + self.origin = self.index(1, 0); + self.next_x += 1; + if self.next_x >= self.width { + break; } - words.push(word); } - words + if self.next_x >= self.width { + self.next_x = 0; + self.next_y += 1; + } + Some(word) } } +pub trait DitherIterator<'a>: Iterator + Sized { + fn dither(self, diffusion: &'a Vec<(isize, isize, i16)>, width: usize) -> Dither<'a, Self> { + Dither::new(self, diffusion, width) + } +} + +impl<'a, I: Iterator> DitherIterator<'a> for I {} + #[cfg(test)] mod tests { use super::*; diff --git a/services/graphics-server/src/api/tile.rs b/services/graphics-server/src/api/tile.rs index 6182ab52f..0fad996ac 100644 --- a/services/graphics-server/src/api/tile.rs +++ b/services/graphics-server/src/api/tile.rs @@ -1,4 +1,5 @@ use crate::api::*; +use std::cmp::max; use std::convert::TryInto; //////////////////////// Tile -------- author: nworbnhoj @@ -21,18 +22,43 @@ pub(crate) const OUT_OF_BOUNDS: usize = usize::MAX; #[derive(Debug, Clone, Copy, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] pub struct Tile { bound: Rectangle, - width_words: u16, + width_words: i16, + max_bound: Rectangle, words: [Word; WORDS_PER_TILE], } impl Tile { - pub fn new(bound: Rectangle, width_words: u16) -> Self { + pub fn new(mut bound: Rectangle) -> Self { log::trace!("new Tile {:?}", bound); - let words = [0u32; WORDS_PER_TILE]; + let bits_per_word = BITS_PER_WORD as i16; + let bits_per_tile = BITS_PER_TILE as i16; + let words_per_tile = WORDS_PER_TILE as i16; + + let width_bits = bound.br.x - bound.tl.x + 1; + // establish width limits + let width_words = if width_bits > bits_per_tile { + log::warn!("Tile max width exceeded"); + bound.br.x = bound.tl.x + words_per_tile * bits_per_word - 1; + words_per_tile + } else { + match width_bits % bits_per_word { + 0 => width_bits / bits_per_word, + _ => width_bits / bits_per_word + 1, + } + }; + let max_x = bound.tl.x + width_words * bits_per_word - 1; + // establish height limits + let max_height_bits = words_per_tile / width_words; + let max_y = bound.tl.y + max_height_bits - 1; + if bound.br.y > max_y { + log::warn!("Tile max height exceeded {} > {}", max_y, bound.br.y); + bound.br.y = max_y; + } Self { bound, width_words, - words, + max_bound: Rectangle::new(bound.tl, Point::new(max_x, max_y)), + words: [0u32; WORDS_PER_TILE], } } @@ -40,6 +66,14 @@ impl Tile { self.bound } + pub fn set_bound(&mut self, bound: Rectangle) { + self.bound = bound + } + + pub fn max_bound(&self) -> Rectangle { + self.max_bound + } + pub fn area(&self) -> u32 { let size = self.size(); (size.x * size.y).try_into().unwrap() @@ -50,10 +84,18 @@ impl Tile { Point::new(pos.br.x - pos.tl.x, pos.br.y - pos.tl.y) } + fn word_index_mut(&mut self, point: Point) -> usize { + if !self.bound.intersects_point(point) && self.max_bound.intersects_point(point) { + self.bound.br.x = max(point.x, self.bound.br.x); + self.bound.br.y = max(point.y, self.bound.br.y); + } + self.word_index(point) + } + fn word_index(&self, point: Point) -> usize { if self.bound.intersects_point(point) { - let tile_line: u16 = (point.y - self.bound.tl.y).try_into().unwrap(); - let first_word_in_line:usize = (tile_line * self.width_words).try_into().unwrap(); + let tile_line: i16 = (point.y - self.bound.tl.y).try_into().unwrap(); + let first_word_in_line: usize = (tile_line * self.width_words).try_into().unwrap(); let bit_index: usize = (point.x - self.bound.tl.x).try_into().unwrap(); first_word_in_line + (bit_index / BITS_PER_WORD) } else { @@ -66,8 +108,9 @@ impl Tile { self.words[self.word_index(point)] } + /// Adding a word outside of bound (but within max_bound) will expand bound. pub fn set_word(&mut self, point: Point, word: Word) { - self.words[self.word_index(point)] = word; + self.words[self.word_index_mut(point)] = word; } pub fn get_line(&self, point: Point) -> Vec { @@ -83,20 +126,22 @@ impl Tile { } } - pub fn set_line(&self, _point: Point, _pixels: Vec){ + /// Adding a line outside of bound (but within max_bound) will expant bound. + pub fn set_line(&self, _point: Point, _pixels: Vec) { log::warn!("not implemented"); } pub fn get_pixel(&self, point: Point) -> PixelColor { let word: usize = self.get_word(point).try_into().unwrap(); - let bpw: i16 = BITS_PER_WORD.try_into().unwrap(); + let bpw: i16 = BITS_PER_WORD.try_into().unwrap(); let bit = point.x % bpw; PixelColor::from((word >> bit) & 1) } + /// Adding a pixel outside of bound (but within max_bound) will expand bound. pub fn set_pixel(&mut self, point: Point, color: PixelColor) { - let word_index = self.word_index(point); - let bpw: i16 = BITS_PER_WORD.try_into().unwrap(); + let word_index = self.word_index_mut(point); + let bpw: i16 = BITS_PER_WORD.try_into().unwrap(); match word_index == OUT_OF_BOUNDS { true => {} false => { @@ -115,5 +160,12 @@ impl Tile { self.bound.tl.y += offset.y; self.bound.br.x += offset.x; self.bound.br.y += offset.y; + let max_x = self.bound.tl.x + self.width_words * BITS_PER_WORD as i16 - 1; + let max_y = self.bound.tl.y + WORDS_PER_TILE as i16 / self.width_words - 1; + self.max_bound = Rectangle::new(self.bound.tl, Point::new(max_x, max_y)); + } + + pub fn crop(&mut self, bound: Rectangle) { + self.bound = bound; } } From 6d1d514fe5e8f88bf90c0b5503a34b3ce45427a5 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 28 Jun 2022 07:32:55 +1000 Subject: [PATCH 4/7] refine modal show_image alignment --- services/modals/src/lib.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 472ae14c0..9679886e4 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -197,30 +197,41 @@ impl Modals { pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { self.lock(); // resize and/or rotate - let (modal_width, modal_height) = - (gam::IMG_MODAL_WIDTH as f32, gam::IMG_MODAL_HEIGHT as f32); + const BORDER: u32 = 3; + + let (modal_width, modal_height) = ( + gam::IMG_MODAL_WIDTH - 2 * BORDER, + gam::IMG_MODAL_HEIGHT - 2 * BORDER, + ); + 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 portrait_scale = (modal_width as f32 / img_width).min(modal_height as f32 / img_height); + let landscape_scale = + (modal_width as f32 / img_height).min(modal_height as f32 / img_width); let mut bm = if portrait_scale >= 1.0 { + log::info!("show image as is"); Bitmap::from(img) } else if landscape_scale >= 1.0 { + log::info!("rotate image"); Bitmap::from(img).rotate90() } else if portrait_scale >= landscape_scale { + log::info!("scale image {}", portrait_scale); Bitmap::new_resize(img, (portrait_scale * img_width) as usize) } else { + log::info!("scale image {} and rotate", landscape_scale); Bitmap::new_resize(img, (landscape_scale * img_width) as usize).rotate90() }; - let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); // center image in modal let center = Point::new( - ((gam::IMG_MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), - ((gam::IMG_MODAL_HEIGHT - bm_height) / 2) + (BORDER + (gam::IMG_MODAL_WIDTH - 2 * BORDER - bm_width) / 2) + .try_into() + .unwrap(), + (BORDER + (gam::IMG_MODAL_HEIGHT - 2 * BORDER - bm_height) / 2) .try_into() .unwrap(), ); From 35cd593f24c138033adebea92310c39cc55beb96 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 28 Jun 2022 07:39:02 +1000 Subject: [PATCH 5/7] refine clifford attractor generation --- services/modals/src/tests.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 6194386e1..2bc6ba58f 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -1,9 +1,9 @@ use super::*; +#[cfg(feature = "ditherpunk")] +use bitmap::PixelType; use gam::*; use std::thread; use xous_names::XousNames; -#[cfg(feature = "ditherpunk")] -use bitmap::PixelType; const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"]; @@ -175,23 +175,33 @@ pub fn spawn_test() { // https://sequelaencollection.home.blog/2d-chaotic-attractors/ #[cfg(feature = "ditherpunk")] fn clifford() -> Img { - const SIZE: u32 = gam::IMG_MODAL_WIDTH - 10; - const CENTER: f32 = (SIZE / 2) as f32; - const SCALE: f32 = 60.0; - let mut buf = vec![255u8; (SIZE * SIZE).try_into().unwrap()]; + // IMW+20, IMW-10 Line (resize + rotate) + // IMW-20, IMW-10 OK () + // IMW+10, IMW+20 Line (resize) + // IMH-15 IMW-15 Line (rotate) + + // width & height chosen to force resize & rotation + const WIDTH: u32 = gam::IMG_MODAL_HEIGHT * 2; + const HEIGHT: u32 = gam::IMG_MODAL_WIDTH * 2; + const X_CENTER: f32 = (WIDTH / 2) as f32; + const Y_CENTER: f32 = (HEIGHT / 2) as f32; + const SCALE: f32 = WIDTH as f32 / 5.1; //70.0; + const STEP: u8 = 32; + const ITERATIONS: u32 = 400000; + let mut buf = vec![255u8; (WIDTH * HEIGHT).try_into().unwrap()]; let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9); let (mut x, mut y): (f32, f32) = (0.0, 0.0); - for _ in 0..=4000000 { + for _ in 0..=ITERATIONS { let x1 = f32::sin(a * y) + c * f32::cos(a * x); let y1 = f32::sin(b * x) + d * f32::cos(b * y); (x, y) = (x1, y1); - let (a, b): (u32, u32) = ((x * SCALE + CENTER) as u32, (y * SCALE + CENTER) as u32); - let i: usize = (a + SIZE * b).try_into().unwrap(); - if buf[i] > 0 { - buf[i] -= 1; + let (a, b): (u32, u32) = ((x * SCALE + X_CENTER) as u32, (y * SCALE + Y_CENTER) as u32); + let i: usize = (a + WIDTH * b).try_into().unwrap(); + if buf[i] >= STEP { + buf[i] -= STEP; } } - Img::new(buf, SIZE.try_into().unwrap(), PixelType::U8) + Img::new(buf, WIDTH.try_into().unwrap(), PixelType::U8) } fn test_validator(input: TextEntryPayload) -> Option> { From 247dfd2fe7b0ef71cc5e9fd8ba332e2ce54a4ea5 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Wed, 29 Jun 2022 07:32:49 +1000 Subject: [PATCH 6/7] simplify gam bitmap img struct --- services/gam/src/bitmap.rs | 121 ++++++++++++++++++------------------- services/modals/src/lib.rs | 2 +- 2 files changed, 61 insertions(+), 62 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index c21532244..e235cf170 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -59,9 +59,12 @@ impl Bitmap { } pub fn new_resize(image: &Img, width: usize) -> Self { - let (img_width, _, _) = image.size(); let burkes = BURKES.to_vec(); - let words = image.iter().shrink(img_width, width).dither(&burkes, width); + let words = image + .iter() + .to_grey(image.px_type) + .shrink(image.width, width) + .dither(&burkes, width); let mut mosaic: Vec = Vec::new(); @@ -274,21 +277,67 @@ impl From<[Option; 6]> for Bitmap { } } -impl From<&Img> for Bitmap { +impl<'a> From<&Img> for Bitmap { fn from(image: &Img) -> Self { - let (img_width, _, _) = image.size(); - Bitmap::new_resize(image, img_width) + Bitmap::new_resize(image, image.width) } } // ********************************************************************** +#[derive(Debug, Clone, Copy)] pub enum PixelType { U8, U8x3, U8x4, } +/* + * Image as a minimal flat buffer of u8; accessible by (x, y) + * + * author: nworbnhoj + */ + +//#[derive(Debug)] +pub struct Img { + pub pixels: Vec, + pub width: usize, + pub px_type: PixelType, +} + +impl Img { + pub fn new(pixels: Vec, width: usize, px_type: PixelType) -> Self { + Self { + pixels, + width, + px_type, + } + } + pub fn width(&self) -> usize { + self.width + } + pub fn height(&self) -> usize { + match self.px_type { + PixelType::U8 => self.pixels.len() / self.width, + PixelType::U8x3 => self.pixels.len() / (self.width * 3), + _ => { + log::warn!("PixelType not implemented"); + 0 + } + } + } +} + +impl Deref for Img { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.pixels + } +} + +// ********************************************************************** + pub struct GreyScale { iter: I, px_type: PixelType, @@ -370,7 +419,7 @@ pub struct Shrink { out_x_last: usize, } -impl<'a, I: Iterator> Shrink { +impl> Shrink { fn new(iter: I, in_width: usize, out_width: usize) -> Shrink { let scale = in_width as f32 / out_width as f32; // set up a buffer to average the surrounding pixels @@ -408,7 +457,7 @@ impl<'a, I: Iterator> Shrink { } } /// Adaptor Iterator to shrink an image dimensions from in_width to out_width -impl<'a, I: Iterator> Iterator for Shrink { +impl> Iterator for Shrink { type Item = u8; /// Shrinks an image from in_width to out_width @@ -422,7 +471,7 @@ impl<'a, I: Iterator> Iterator for Shrink { // 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), + Some(pixel) => Some(pixel), None => None, }; } @@ -439,7 +488,7 @@ impl<'a, I: Iterator> Iterator for Shrink { let mut x_div: u16 = 0; while in_x <= *in_x_cap { x_total += match self.iter.next() { - Some(pixel) => *pixel as u16, + Some(pixel) => pixel as u16, None => { self.out_x_last = out_x - 1; 0 @@ -467,64 +516,14 @@ impl<'a, I: Iterator> Iterator for Shrink { } } -pub trait ShrinkIterator<'a>: Iterator + Sized { +pub trait ShrinkIterator: Iterator + Sized { fn shrink(self, in_width: usize, out_width: usize) -> Shrink { Shrink::new(self, in_width, out_width) } } -impl<'a, I: Iterator> ShrinkIterator<'a> for I {} +impl> ShrinkIterator for I {} -// ********************************************************************** - -/* - * Image as a minimal flat buffer of grey u8 pixels; accessible by (x, y) - * - * author: nworbnhoj - */ - -#[derive(Debug)] -pub struct Img { - pub pixels: Vec, - pub width: usize, -} - -impl Img { - pub fn new(buf: Vec, width: usize, px_type: PixelType) -> Self { - let px_len = match px_type { - PixelType::U8 => buf.len(), - PixelType::U8x3 => buf.len() / 3, - _ => 0, - }; - let mut pixels: Vec = Vec::with_capacity(px_len); - - for pixel in buf.iter().to_grey(px_type) { - pixels.push(pixel); - } - Self { pixels, width } - } - pub fn get(&self, x: usize, y: usize) -> Option<&u8> { - 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 as_slice(&self) -> &[u8] { - self.pixels.as_slice() - } -} - -impl Deref for Img { - type Target = Vec; - - fn deref(&self) -> &Self::Target { - &self.pixels - } -} // ********************************************************************** diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 9679886e4..a8888b448 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -204,7 +204,7 @@ impl Modals { gam::IMG_MODAL_HEIGHT - 2 * BORDER, ); - let (w, h, _) = img.size(); + let (w, h) = (img.width(), img.height()); let (img_width, img_height) = (w as f32, h as f32); let portrait_scale = (modal_width as f32 / img_width).min(modal_height as f32 / img_height); From bc2e4a4b2ce7abfee35ba98deb655ff01472b507 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Wed, 29 Jun 2022 12:32:58 +1000 Subject: [PATCH 7/7] tweak modals test clifford attractor generation --- services/modals/src/tests.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 2bc6ba58f..b2bd77fc1 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -175,19 +175,14 @@ pub fn spawn_test() { // https://sequelaencollection.home.blog/2d-chaotic-attractors/ #[cfg(feature = "ditherpunk")] fn clifford() -> Img { - // IMW+20, IMW-10 Line (resize + rotate) - // IMW-20, IMW-10 OK () - // IMW+10, IMW+20 Line (resize) - // IMH-15 IMW-15 Line (rotate) - // width & height chosen to force resize & rotation - const WIDTH: u32 = gam::IMG_MODAL_HEIGHT * 2; - const HEIGHT: u32 = gam::IMG_MODAL_WIDTH * 2; + const WIDTH: u32 = gam::IMG_MODAL_HEIGHT + 2; + const HEIGHT: u32 = gam::IMG_MODAL_WIDTH + 2; const X_CENTER: f32 = (WIDTH / 2) as f32; const Y_CENTER: f32 = (HEIGHT / 2) as f32; - const SCALE: f32 = WIDTH as f32 / 5.1; //70.0; - const STEP: u8 = 32; - const ITERATIONS: u32 = 400000; + const SCALE: f32 = WIDTH as f32 / 5.1; + const STEP: u8 = 16; + const ITERATIONS: u32 = 200000; let mut buf = vec![255u8; (WIDTH * HEIGHT).try_into().unwrap()]; let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9); let (mut x, mut y): (f32, f32) = (0.0, 0.0);