From 418416425b2211ce55d1ff3a0e3883978cdb330c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20M=C3=BCller?= Date: Sun, 4 Jun 2023 22:28:33 +0200 Subject: [PATCH] added the Image Struct and basic functions and traits for it --- src/image/mod.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++ 2 files changed, 104 insertions(+) create mode 100644 src/image/mod.rs diff --git a/src/image/mod.rs b/src/image/mod.rs new file mode 100644 index 0000000..0802113 --- /dev/null +++ b/src/image/mod.rs @@ -0,0 +1,100 @@ +use std::ops::{Add, Div, Index, IndexMut, Mul, Sub}; +use std::vec::IntoIter; + + +#[allow(unused)] +#[derive(Default)] +pub struct Image +where + T: Add + Sub + Mul + Div + PartialEq + Default + Copy, +{ + ///the width of the Picture in px + width: u32, + ///the height of the Picture in px + height: u32, + ///the raw RGBA data of the Picture where the RGBA values of an pixel is one tuple + pixels: Vec<(T, T, T, T)>, +} + +impl Image +where + T: Add + Sub + Mul + Div + 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 { + panic!("The Image does not have the same number of pixel as width and height implies") + } else { + Self { + width, + height, + pixels, + } + } + } + + pub fn width(&self) -> u32 { + self.width + } + + pub fn height(&self) -> u32 { + self.height + } + + pub fn pixel(&self, index: usize) -> (T, T, T, T) { + *self.index(index) + } +} + +impl Index for Image +where + T: Add + Sub + Mul + Div + PartialEq + Default + Copy, +{ + type Output = (T, T, T, T); + fn index(&self, index: usize) -> &Self::Output { + &self.pixels[index] + } +} + +impl IndexMut for Image +where + T: Add + Sub + Mul + Div + PartialEq + Default + Copy, +{ + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + &mut self.pixels[index] + } +} + +impl IntoIterator for Image +where + T: Add + Sub + Mul + Div + PartialEq + Default + Copy, +{ + type Item = (T, T, T, T); + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.pixels.into_iter() + } +} + + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn it_works(){ + let image:Image = Default::default(); + let default_width = image.width(); + println!("{:?}",default_width); + + //assert_eq!(default_width, 1); + let default_height = image.height(); + println!("{:?}",default_height); + + //assert_eq!(default_height,1); + + } +} diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..df17e00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +extern crate core; + +mod image; + pub fn add(left: usize, right: usize) -> usize { left + right }