From f9ee19e5269c3228c41bb43c18808e298675f60b Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 28 Jul 2022 07:35:51 +1000 Subject: [PATCH] fix merge blunder caused by flakey Internet connection --- services/gam/src/bitmap.rs | 310 ------------------------------------- 1 file changed, 310 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 710b17c50..773e4c409 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -367,316 +367,6 @@ impl<'a> From<&Img> for Bitmap { // ********************************************************************** -<<<<<<< HEAD -======= -#[derive(Debug, Clone, Copy)] -pub enum PixelType { - U0, // Error - U8, - U8x2, - U8x3, - U8x4, - U16, - U16x2, - U16x3, - U16x4, -} - -/* - * 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::U8x2 => self.pixels.len() / (self.width * 2), - PixelType::U8x3 => self.pixels.len() / (self.width * 3), - PixelType::U8x4 => self.pixels.len() / (self.width * 4), - PixelType::U16 => self.pixels.len() / (self.width * 2), - PixelType::U16x2 => self.pixels.len() / (self.width * 4), - PixelType::U16x3 => self.pixels.len() / (self.width * 6), - PixelType::U16x4 => self.pixels.len() / (self.width * 8), - _ => { - 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, -} - -impl> GreyScale { - fn new(iter: I, px_type: PixelType) -> GreyScale { - Self { iter, px_type } - } -} - -impl> Iterator for GreyScale { - type Item = u8; - - fn next(&mut self) -> Option { - match self.px_type { - PixelType::U8 => match self.iter.next() { - Some(gr) => Some(gr), - None => None, - }, - PixelType::U8x2 => match self.iter.next() { - Some(gr) => { - let _alpha = self.iter.next(); - Some(gr) - } - None => None, - }, - PixelType::U8x3 => { - let r = self.iter.next(); - let g = self.iter.next(); - let b = self.iter.next(); - grey(r, g, b) - } - PixelType::U8x4 => { - let r = self.iter.next(); - let g = self.iter.next(); - let b = self.iter.next(); - let _alpha = self.iter.next(); - grey(r, g, b) - } - PixelType::U16 => match self.iter.next() { - Some(gr) => { - let _lower_bits = self.iter.next(); - Some(gr) - } - None => None, - }, - PixelType::U16x2 => match self.iter.next() { - Some(gr) => { - let _lower_bits = self.iter.next(); - let _alpha = self.iter.next(); - let _alpha = self.iter.next(); - Some(gr) - } - None => None, - }, - PixelType::U16x3 => { - let r = self.iter.next(); - let _lower_bits = self.iter.next(); - let g = self.iter.next(); - let _lower_bits = self.iter.next(); - let b = self.iter.next(); - let _lower_bits = self.iter.next(); - grey(r, g, b) - } - PixelType::U16x4 => { - let r = self.iter.next(); - let _lower_bits = self.iter.next(); - let g = self.iter.next(); - let _lower_bits = self.iter.next(); - let b = self.iter.next(); - let _lower_bits = self.iter.next(); - let _alpha = self.iter.next(); - let _lower_bits = self.iter.next(); - grey(r, g, b) - } - _ => { - log::warn!("unsupported PixelType {:?}", self.px_type); - None - } - } - } -} - - -// chromatic coversion from RGB to Greyscale -fn grey(r: Option, g: Option, b: Option) -> Option { - const R: u32 = 2126; - const G: u32 = 7152; - const B: u32 = 722; - const BLACK: u32 = R + G + B; - 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 - } -} - -pub trait GreyScaleIterator: Iterator + Sized { - /// converts pixels of PixelType to u8 greyscale - fn to_grey(self, px_type: PixelType) -> GreyScale { - GreyScale::new(self, px_type) - } -} - -impl> GreyScaleIterator 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> 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> 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 ±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 { - 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: Iterator + Sized { - fn shrink(self, in_width: usize, out_width: usize) -> Shrink { - Shrink::new(self, in_width, out_width) - } -} - -impl> ShrinkIterator for I {} - -// ********************************************************************** - #[cfg(test)] mod tests { use super::*;