added the validate function for images.

added the "iter" and "iter_mut" functions
added a test for the validate function
added  "From<u8>" and "PartialOrd" to the generic
added documentation for the added features
This commit is contained in:
Felix Müller 2023-06-06 21:48:19 +02:00
parent aed49466ce
commit 52cb8639ea
1 changed files with 50 additions and 6 deletions

View File

@ -30,13 +30,14 @@
//! //!
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::slice::{Iter, IterMut};
use std::vec::IntoIter; use std::vec::IntoIter;
#[allow(unused)] #[allow(unused)]
#[derive(Default)] #[derive(Default)]
pub struct Image<T> pub struct Image<T>
where where
T: Into<f32> + PartialEq + Default + Copy, T: Into<f32> + PartialEq + Default + Copy + From<u8> + PartialOrd,
{ {
///the width of the Picture in px ///the width of the Picture in px
width: usize, width: usize,
@ -49,7 +50,7 @@ where
#[allow(unused)] #[allow(unused)]
impl<T> Image<T> impl<T> Image<T>
where where
T: Into<f32> + PartialEq + Default + Copy, T: Into<f32> + PartialEq + Default + Copy + From<u8> + PartialOrd,
{ {
///gives an Image with specified values if the Vec matches the width times the height of the Image ///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. /// 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) { pub fn pixel(&self, index: usize) -> (T, T, T, T) {
*self.index(index) *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<T> Index<usize> for Image<T> impl<T> Index<usize> for Image<T>
where where
T: Into<f32> + PartialEq + Default + Copy, T: Into<f32> + PartialEq + Default + Copy + From<u8> + PartialOrd,
{ {
type Output = (T, T, T, T); type Output = (T, T, T, T);
fn index(&self, index: usize) -> &Self::Output { fn index(&self, index: usize) -> &Self::Output {
@ -91,7 +126,7 @@ where
impl<T> IndexMut<usize> for Image<T> impl<T> IndexMut<usize> for Image<T>
where where
T: Into<f32> + PartialEq + Default + Copy, T: Into<f32> + PartialEq + Default + Copy + From<u8> + PartialOrd,
{ {
fn index_mut(&mut self, index: usize) -> &mut Self::Output { fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.pixels[index] &mut self.pixels[index]
@ -100,7 +135,7 @@ where
impl<T> IntoIterator for Image<T> impl<T> IntoIterator for Image<T>
where where
T: Into<f32> + PartialEq + Default + Copy, T: Into<f32> + PartialEq + Default + Copy + From<u8> + PartialOrd,
{ {
type Item = (T, T, T, T); type Item = (T, T, T, T);
type IntoIter = IntoIter<Self::Item>; type IntoIter = IntoIter<Self::Item>;
@ -112,7 +147,6 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
@ -162,5 +196,15 @@ mod tests {
(32, 1, 79, 1) (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<f32> = Image::new(1, 3, vec);
assert!(image.validate());
println!("{:?}", image.pixel(0));
} }
} }