From 52cb8639ead838b8ebbcab3b78b9440f367798eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20M=C3=BCller?= Date: Tue, 6 Jun 2023 21:48:19 +0200 Subject: [PATCH] added the validate function for images. added the "iter" and "iter_mut" functions added a test for the validate function added "From" and "PartialOrd" to the generic added documentation for the added features --- src/image/mod.rs | 56 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/image/mod.rs b/src/image/mod.rs index 9414d03..9f8d779 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -30,13 +30,14 @@ //! use std::ops::{Index, IndexMut}; +use std::slice::{Iter, IterMut}; use std::vec::IntoIter; #[allow(unused)] #[derive(Default)] pub struct Image where - T: Into + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy + From + PartialOrd, { ///the width of the Picture in px width: usize, @@ -49,7 +50,7 @@ where #[allow(unused)] impl Image where - T: Into + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy + From + PartialOrd, { ///gives an Image with specified values if the Vec matches the width times the height of the Image /// if the width and height dont make sense for the Image then will this function panic. @@ -77,11 +78,45 @@ where pub fn pixel(&self, index: usize) -> (T, T, T, T) { *self.index(index) } + + /// Returns the iterator of the pixels vector + pub fn iter(&self) -> Iter<'_, (T, T, T, T)> { + self.pixels.iter() + } + /// Returns the mutable iterator of the pixels vector + pub fn iter_mut(&mut self) -> IterMut<'_, (T, T, T, T)> { + self.pixels.iter_mut() + } + /// validates if every pixel of the Picture is between 0 and 255 using clamp(). + /// if not then the Value gets changed and the result returns true. + pub fn validate(&mut self) -> bool { + let mut result = false; + + for pixel in self.iter_mut() { + Image::clamp(&mut pixel.0, 255.into(), 0.into(), &mut result); + Image::clamp(&mut pixel.1, 255.into(), 0.into(), &mut result); + Image::clamp(&mut pixel.2, 255.into(), 0.into(), &mut result); + Image::clamp(&mut pixel.3, 255.into(), 0.into(), &mut result); + } + result + } + + /// validates if the given Value is between given min and max and changes it to min/ max if not. + ///result will be true if something is changed + fn clamp(pixel_color: &mut T, max: T, min: T, result: &mut bool) { + if *pixel_color > max { + *pixel_color = max; + *result = true; + } else if *pixel_color < min { + *pixel_color = min; + *result = true; + } + } } impl Index for Image where - T: Into + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy + From + PartialOrd, { type Output = (T, T, T, T); fn index(&self, index: usize) -> &Self::Output { @@ -91,7 +126,7 @@ where impl IndexMut for Image where - T: Into + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy + From + PartialOrd, { fn index_mut(&mut self, index: usize) -> &mut Self::Output { &mut self.pixels[index] @@ -100,7 +135,7 @@ where impl IntoIterator for Image where - T: Into + PartialEq + Default + Copy, + T: Into + PartialEq + Default + Copy + From + PartialOrd, { type Item = (T, T, T, T); type IntoIter = IntoIter; @@ -112,7 +147,6 @@ where #[cfg(test)] mod tests { - use super::*; #[test] @@ -162,5 +196,15 @@ mod tests { (32, 1, 79, 1) ] ); + + let vec: Vec<(f32, f32, f32, f32)> = vec![ + (-33.0, 7732.0, 2564355.0, -79.0), + (1.0, 79.0, 255.0, 1.05), + (300.0, 300.0, 300.0, 300.0), + ]; + let mut image: Image = Image::new(1, 3, vec); + + assert!(image.validate()); + println!("{:?}", image.pixel(0)); } }