diff --git a/src/image.rs b/src/image.rs index 071daa9..5210581 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,17 +1,17 @@ -use std::fs::File; use crate::vec2::Vec2; +use std::fs::File; //todo: make dynamically different bitdepths pub struct Image { data: Vec, - size: Vec2 + size: Vec2, } impl Image { pub fn new(size: Vec2) -> Self { Image { - data: vec![0u8; (size.x * size.y) as usize], - size + data: vec![0u8; size.size()], + size, } } @@ -29,7 +29,10 @@ impl Image { decoder.read_into_buffer(&mut data).unwrap(); Image { data, - size: Vec2 {x: x as i32, y: y as i32} + size: Vec2 { + x: x as i32, + y: y as i32, + }, } } @@ -50,10 +53,20 @@ impl Image { for y in 0..image.size.y { for x in 0..image.size.x { //todo: implement better(very stupid to do per pixel) - if x + pos.x >= 0 && y + pos.y >= 0 && x + pos.x < self.size.x && y + pos.y < self.size.y { + if x + pos.x >= 0 + && y + pos.y >= 0 + && x + pos.x < self.size.x + && y + pos.y < self.size.y + { let p = image.get_pixel(Vec2 { x, y }); if p > 0 { - self.set_pixel(Vec2 { x: x + pos.x, y: y + pos.y }, p); + self.set_pixel( + Vec2 { + x: x + pos.x, + y: y + pos.y, + }, + p, + ); } } } @@ -75,7 +88,13 @@ impl Image { let xs = xdiff / step; let ys = ydiff / step; for _ in 1..=(step as i32) { - self.set_pixel(Vec2 { x: x as i32, y: y as i32 }, color); + self.set_pixel( + Vec2 { + x: x as i32, + y: y as i32, + }, + color, + ); x += xs; y += ys; } diff --git a/src/vec2.rs b/src/vec2.rs index f4dcc9b..0cf1959 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -1,22 +1,31 @@ -use num::Num; +use num::{Num, ToPrimitive}; #[derive(Copy, Clone)] -pub struct Vec2 { +pub struct Vec2 { pub x: T, pub y: T, } -impl Vec2 { +impl Vec2 { + //pub static pub fn zero() -> Self { - Vec2 {x: T::zero(), y: T::zero()} + Vec2 { + x: T::zero(), + y: T::zero(), + } + } + + //pub + pub fn size(&self) -> usize { + (self.x * self.y) as usize } } -impl Default for Vec2 { +impl Default for Vec2 { fn default() -> Self { Vec2 { x: T::default(), - y: T::default() + y: T::default(), } } -} \ No newline at end of file +}