Added Documentation

changed the requirements for the generic

wrote tests for the struct and all functions
This commit is contained in:
Felix Müller 2023-06-05 19:54:35 +02:00
parent fcb904c5cd
commit 9afaf0de5b
2 changed files with 91 additions and 26 deletions

View File

@ -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<f32> = 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<u8> = Image::new(3,3,vec);
//! ```
//!
use std::ops::{Index, IndexMut};
use std::vec::IntoIter;
#[allow(unused)]
#[derive(Default)]
pub struct Image<T>
where
T: Add + Sub + Mul + Div + PartialEq + Default + Copy,
T: Into<f32> + 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<T> Image<T>
where
T: Add + Sub + Mul + Div + PartialEq + Default + Copy,
T: Into<f32> + 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<T> Index<usize> for Image<T>
where
T: Add + Sub + Mul + Div + PartialEq + Default + Copy,
T: Into<f32> + PartialEq + Default + Copy,
{
type Output = (T, T, T, T);
fn index(&self, index: usize) -> &Self::Output {
@ -60,7 +91,7 @@ where
impl<T> IndexMut<usize> for Image<T>
where
T: Add + Sub + Mul + Div + PartialEq + Default + Copy,
T: Into<f32> + PartialEq + Default + Copy,
{
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.pixels[index]
@ -69,7 +100,7 @@ where
impl<T> IntoIterator for Image<T>
where
T: Add + Sub + Mul + Div + PartialEq + Default + Copy,
T: Into<f32> + PartialEq + Default + Copy,
{
type Item = (T, T, T, T);
type IntoIter = IntoIter<Self::Item>;
@ -79,23 +110,57 @@ where
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works(){
let image:Image<f32> = Default::default();
let default_width = image.width();
println!("{:?}",default_width);
fn new_image() {
let image: Image<f32> = 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<u8> = 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::<Vec<(u8, u8, u8, u8)>>();
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)
]
);
}
}

View File

@ -1,6 +1,6 @@
extern crate core;
mod image;
pub mod image;
pub fn add(left: usize, right: usize) -> usize {
left + right