From 3d0fd76862071f45ee7811d89f54ae4cd0829fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20M=C3=BCller?= Date: Thu, 8 Jun 2023 16:16:24 +0200 Subject: [PATCH] added Path to the Image Struct added path function changed tests and comments --- src/image/mod.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/image/mod.rs b/src/image/mod.rs index 9f8d779..685524b 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -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), //! (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); +//! let mut image:Image = Image::new(3,3,vec, PathBuf::new()); //! ``` //! use std::ops::{Index, IndexMut}; +use std::path::PathBuf; use std::slice::{Iter, IterMut}; use std::vec::IntoIter; @@ -45,6 +47,8 @@ where 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)>, + ///the absolute path where the picture is located + path: PathBuf, } #[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 /// 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() { panic!("The Image does not have the same number of pixel as width and height implies") } else { @@ -62,22 +66,27 @@ where width, height, pixels, + path, } } } - /// Gives back the width of the image + /// Returns the width of the image pub fn width(&self) -> usize { self.width } - /// Gives back the height of the image + /// Returns the height of the image pub fn height(&self) -> usize { 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) { *self.index(index) } + /// Returns the path of the image + pub fn path(&self) -> &PathBuf { + &self.path + } /// Returns the iterator of the pixels vector pub fn iter(&self) -> Iter<'_, (T, T, T, T)> { @@ -169,7 +178,7 @@ mod tests { (1, 79, 135, 79), (32, 1, 79, 1), ]; - let mut image: Image = Image::new(3, 3, vec); + let mut image: Image = Image::new(3, 3, vec, PathBuf::new()); assert_eq!(*image.index(4), (79, 32, 255, 1)); let result = std::panic::catch_unwind(|| { @@ -202,7 +211,7 @@ mod tests { (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); + let mut image: Image = Image::new(1, 3, vec, PathBuf::new()); assert!(image.validate()); println!("{:?}", image.pixel(0));