Merge pull request #24 from programmieren-mit-rust/Image_Path

added Path to the Image Struct
This commit is contained in:
teridax 2023-06-08 15:40:11 +00:00 committed by GitHub
commit 243e94a506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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),
//! (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);
//! let mut image:Image<u8> = 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<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));
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<f32> = Image::new(1, 3, vec);
let mut image: Image<f32> = Image::new(1, 3, vec, PathBuf::new());
assert!(image.validate());
println!("{:?}", image.pixel(0));