From 549178e148ef0f1b5b9b66bc6f942893511e4cf0 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 21 Jun 2022 08:48:30 +1000 Subject: [PATCH] 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();