Merge pull request #18 from programmieren-mit-rust/Image-Structs

Image structs
This commit is contained in:
teridax 2023-06-06 19:35:08 +02:00 committed by GitHub
commit d6bb8f38fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 170 additions and 0 deletions

166
src/image/mod.rs Normal file
View File

@ -0,0 +1,166 @@
//!
//! 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: Into<f32> + PartialEq + Default + Copy,
{
///the width of the Picture in px
width: usize,
///the height of the Picture in px
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)>,
}
#[allow(unused)]
impl<T> Image<T>
where
T: Into<f32> + PartialEq + Default + Copy,
{
///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 {
if width * height != pixels.len() {
panic!("The Image does not have the same number of pixel as width and height implies")
} else {
Self {
width,
height,
pixels,
}
}
}
/// Gives back the width of the image
pub fn width(&self) -> usize {
self.width
}
/// 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)
}
}
impl<T> Index<usize> for Image<T>
where
T: Into<f32> + PartialEq + Default + Copy,
{
type Output = (T, T, T, T);
fn index(&self, index: usize) -> &Self::Output {
&self.pixels[index]
}
}
impl<T> IndexMut<usize> for Image<T>
where
T: Into<f32> + PartialEq + Default + Copy,
{
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.pixels[index]
}
}
impl<T> IntoIterator for Image<T>
where
T: Into<f32> + PartialEq + Default + Copy,
{
type Item = (T, T, T, T);
type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.pixels.into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
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));
let result = std::panic::catch_unwind(|| {
let _ = image.index(9);
});
assert!(result.is_err());
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,3 +1,7 @@
extern crate core;
pub mod image;
pub mod multithreading; pub mod multithreading;
pub fn add(left: usize, right: usize) -> usize { pub fn add(left: usize, right: usize) -> usize {