added the Image Struct and basic functions and traits for it
This commit is contained in:
parent
e24028ffc6
commit
418416425b
|
@ -0,0 +1,100 @@
|
|||
use std::ops::{Add, Div, Index, IndexMut, Mul, Sub};
|
||||
use std::vec::IntoIter;
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Default)]
|
||||
pub struct Image<T>
|
||||
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<T> Image<T>
|
||||
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<T> Index<usize> for Image<T>
|
||||
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<T> IndexMut<usize> for Image<T>
|
||||
where
|
||||
T: Add + Sub + Mul + Div + 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: Add + Sub + Mul + Div + 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 it_works(){
|
||||
let image:Image<f32> = 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);
|
||||
|
||||
}
|
||||
}
|
|
@ -1,3 +1,7 @@
|
|||
extern crate core;
|
||||
|
||||
mod image;
|
||||
|
||||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue