From 9afaf0de5bdf88b58aa24e1b43a8c6c0d88c07ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20M=C3=BCller?= Date: Mon, 5 Jun 2023 19:54:35 +0200 Subject: [PATCH] Added Documentation changed the requirements for the generic wrote tests for the struct and all functions --- src/image/mod.rs | 115 ++++++++++++++++++++++++++++++++++++----------- src/lib.rs | 2 +- 2 files changed, 91 insertions(+), 26 deletions(-) diff --git a/src/image/mod.rs b/src/image/mod.rs index dc6de1c..9414d03 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -1,17 +1,47 @@ -use std::ops::{Add, Div, Index, IndexMut, Mul, Sub}; -use std::vec::IntoIter; +//! +//! This module provides the struct and basic functions to represent images. +//! The image struct has the width and height dimensions of the image in px as usize. +//! The pixels of the image itself are stored in a vector where a tuple with four elements represent the RGBA values of a pixel. +//! +//! A pixel is a tuple with four elements where the elements are RGBA. For convenience the tuple is as an generic Datatype specified +//! so that you can represent images with pixels where the RGBA values are between 0 and 1 as f32 or between 0 and 255 as u8. +//! +//! It has the 'Index', 'IndexMut' and 'IntoIterator' traits implemented so you can properly work with the image. +//! +//! for simplicity the values of the pixels should always be between 0 and 255. +//! +//! +//! #Examples +//! +//! to initialise a image +//! ``` +//! # use imsearch::image::Image; +//! +//! let image: Image = Default::default(); +//! ``` +//! +//! ``` +//! # use imsearch::image::Image; +//! let vec:Vec<(u8,u8,u8,u8)> = vec![(135,32,255,79),(1,79,255,1),(79,1,32,1), +//! (255,1,135,32),(79,32,255,1),(1,135,135,1), +//! (1,1,1,255),(1,79,135,79),(32,1,79,1)]; +//! let mut image:Image = Image::new(3,3,vec); +//! ``` +//! +use std::ops::{Index, IndexMut}; +use std::vec::IntoIter; #[allow(unused)] #[derive(Default)] pub struct Image where - T: Add + Sub + Mul + Div + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy, { ///the width of the Picture in px - width: u32, + width: usize, ///the height of the Picture in px - height: u32, + height: usize, ///the raw RGBA data of the Picture where the RGBA values of an pixel is one tuple pixels: Vec<(T, T, T, T)>, } @@ -19,12 +49,12 @@ where #[allow(unused)] impl Image where - T: Add + Sub + Mul + Div + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy, { ///gives an Image with specified values if the Vec matches the width times the height of the Image - - pub fn new(width: u32, height: u32, pixels: Vec<(T, T, T, T)>) -> Self { - if width * height != pixels.len() as u32 { + /// if the width and height dont make sense for the Image then will this function panic. + pub fn new(width: usize, height: usize, pixels: Vec<(T, T, T, T)>) -> Self { + if width * height != pixels.len() { panic!("The Image does not have the same number of pixel as width and height implies") } else { Self { @@ -35,14 +65,15 @@ where } } - pub fn width(&self) -> u32 { + /// Gives back the width of the image + pub fn width(&self) -> usize { self.width } - - pub fn height(&self) -> u32 { + /// Gives back the height of the image + pub fn height(&self) -> usize { self.height } - + /// Gives back a specified pixel of the image pub fn pixel(&self, index: usize) -> (T, T, T, T) { *self.index(index) } @@ -50,7 +81,7 @@ where impl Index for Image where - T: Add + Sub + Mul + Div + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy, { type Output = (T, T, T, T); fn index(&self, index: usize) -> &Self::Output { @@ -60,7 +91,7 @@ where impl IndexMut for Image where - T: Add + Sub + Mul + Div + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy, { fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut self.pixels[index] @@ -69,7 +100,7 @@ where impl IntoIterator for Image where - T: Add + Sub + Mul + Div + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy, { type Item = (T, T, T, T); type IntoIter = IntoIter; @@ -79,23 +110,57 @@ where } } - #[cfg(test)] mod tests { use super::*; #[test] - fn it_works(){ - let image:Image = Default::default(); - let default_width = image.width(); - println!("{:?}",default_width); + fn new_image() { + let image: Image = Default::default(); + assert_eq!(image.width(), 0); + assert_eq!(image.height(), 0); + assert_eq!(image.pixels, vec![]); + } + #[test] + fn new_image_with_values() { + let vec: Vec<(u8, u8, u8, u8)> = vec![ + (135, 32, 255, 79), + (1, 79, 255, 1), + (79, 1, 32, 1), + (255, 1, 135, 32), + (79, 32, 255, 1), + (1, 135, 135, 1), + (1, 1, 1, 255), + (1, 79, 135, 79), + (32, 1, 79, 1), + ]; + let mut image: Image = Image::new(3, 3, vec); + assert_eq!(*image.index(4), (79, 32, 255, 1)); - //assert_eq!(default_width, 1); - let default_height = image.height(); - println!("{:?}",default_height); + let result = std::panic::catch_unwind(|| { + let _ = image.index(9); + }); + assert!(result.is_err()); - //assert_eq!(default_height,1); + image[5] = (1, 135, 135, 2); + assert_eq!(image[5], (1, 135, 135, 2)); + assert_eq!(image[4].0, 79); + let vec = image.into_iter().collect::>(); + assert_eq!( + vec, + vec![ + (135, 32, 255, 79), + (1, 79, 255, 1), + (79, 1, 32, 1), + (255, 1, 135, 32), + (79, 32, 255, 1), + (1, 135, 135, 2), + (1, 1, 1, 255), + (1, 79, 135, 79), + (32, 1, 79, 1) + ] + ); } } diff --git a/src/lib.rs b/src/lib.rs index df17e00..517875c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ extern crate core; -mod image; +pub mod image; pub fn add(left: usize, right: usize) -> usize { left + right