added Path to the Image Struct

added path function
changed tests and comments
This commit is contained in:
Felix Müller 2023-06-08 16:16:24 +02:00
parent 52cb8639ea
commit 3d0fd76862
1 changed files with 17 additions and 8 deletions

View File

@ -21,15 +21,17 @@
//! ``` //! ```
//! //!
//! ``` //! ```
//! # use imsearch::image::Image; //! # use std::path::{Path, PathBuf};
//! use imsearch::image::Image;
//! let vec:Vec<(u8,u8,u8,u8)> = vec![(135,32,255,79),(1,79,255,1),(79,1,32,1), //! 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), //! (255,1,135,32),(79,32,255,1),(1,135,135,1),
//! (1,1,1,255),(1,79,135,79),(32,1,79,1)]; //! (1,1,1,255),(1,79,135,79),(32,1,79,1)];
//! let mut image:Image<u8> = Image::new(3,3,vec); //! let mut image:Image<u8> = Image::new(3,3,vec, PathBuf::new());
//! ``` //! ```
//! //!
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
use std::path::PathBuf;
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
use std::vec::IntoIter; use std::vec::IntoIter;
@ -45,6 +47,8 @@ where
height: usize, height: usize,
///the raw RGBA data of the Picture where the RGBA values of an pixel is one tuple ///the raw RGBA data of the Picture where the RGBA values of an pixel is one tuple
pixels: Vec<(T, T, T, T)>, pixels: Vec<(T, T, T, T)>,
///the absolute path where the picture is located
path: PathBuf,
} }
#[allow(unused)] #[allow(unused)]
@ -54,7 +58,7 @@ where
{ {
///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.
pub fn new(width: usize, height: usize, pixels: Vec<(T, T, T, T)>) -> Self { pub fn new(width: usize, height: usize, pixels: Vec<(T, T, T, T)>, path: PathBuf) -> Self {
if width * height != pixels.len() { if width * height != pixels.len() {
panic!("The Image does not have the same number of pixel as width and height implies") panic!("The Image does not have the same number of pixel as width and height implies")
} else { } else {
@ -62,22 +66,27 @@ where
width, width,
height, height,
pixels, pixels,
path,
} }
} }
} }
/// Gives back the width of the image /// Returns the width of the image
pub fn width(&self) -> usize { pub fn width(&self) -> usize {
self.width self.width
} }
/// Gives back the height of the image /// Returns the height of the image
pub fn height(&self) -> usize { pub fn height(&self) -> usize {
self.height self.height
} }
/// Gives back a specified pixel of the image /// Returns a specified pixel of the image
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 path of the image
pub fn path(&self) -> &PathBuf {
&self.path
}
/// Returns the iterator of the pixels vector /// Returns the iterator of the pixels vector
pub fn iter(&self) -> Iter<'_, (T, T, T, T)> { pub fn iter(&self) -> Iter<'_, (T, T, T, T)> {
@ -169,7 +178,7 @@ mod tests {
(1, 79, 135, 79), (1, 79, 135, 79),
(32, 1, 79, 1), (32, 1, 79, 1),
]; ];
let mut image: Image<u8> = Image::new(3, 3, vec); let mut image: Image<u8> = Image::new(3, 3, vec, PathBuf::new());
assert_eq!(*image.index(4), (79, 32, 255, 1)); assert_eq!(*image.index(4), (79, 32, 255, 1));
let result = std::panic::catch_unwind(|| { let result = std::panic::catch_unwind(|| {
@ -202,7 +211,7 @@ mod tests {
(1.0, 79.0, 255.0, 1.05), (1.0, 79.0, 255.0, 1.05),
(300.0, 300.0, 300.0, 300.0), (300.0, 300.0, 300.0, 300.0),
]; ];
let mut image: Image<f32> = Image::new(1, 3, vec); let mut image: Image<f32> = Image::new(1, 3, vec, PathBuf::new());
assert!(image.validate()); assert!(image.validate());
println!("{:?}", image.pixel(0)); println!("{:?}", image.pixel(0));