diff options
Diffstat (limited to 'evc/src/frame.rs')
-rw-r--r-- | evc/src/frame.rs | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/evc/src/frame.rs b/evc/src/frame.rs index 666dc00..81390a2 100644 --- a/evc/src/frame.rs +++ b/evc/src/frame.rs @@ -1,29 +1,28 @@ use crate::{ pixel::Pixel, ser::{Sink, Source}, + vec2::Vec2, view::View, }; use std::ops::{Index, IndexMut}; #[derive(Debug, Clone)] pub struct Frame { - pub size: (usize, usize), - buffer: Vec<Vec<Pixel>>, + pub size: Vec2, + buffer: Vec<Pixel>, } impl Frame { - pub fn new(size: (usize, usize)) -> Self { + pub fn new(size: Vec2) -> Self { Self { size, - buffer: (0..size.0) - .map(|_| (0..size.1).map(|_| Pixel::default()).collect()) - .collect(), + buffer: (0..size.x * size.y).map(|_| Pixel::default()).collect(), } } - pub fn read(source: &mut impl Source, size: (usize, usize)) -> anyhow::Result<Self> { + pub fn read(source: &mut impl Source, size: Vec2) -> anyhow::Result<Self> { let mut frame = Frame::new(size); - for y in 0..size.1 { - for x in 0..size.0 { + for y in 0..size.y { + for x in 0..size.x { let pixel = source.get::<Pixel>()?; frame[(x, y)] = pixel; } @@ -31,45 +30,55 @@ impl Frame { Ok(frame) } pub fn write(&self, sink: &mut impl Sink) -> anyhow::Result<()> { - for y in 0..self.size.1 { - for x in 0..self.size.0 { + for y in 0..self.size.y { + for x in 0..self.size.x { sink.put(self[(x, y)])?; } } Ok(()) } pub fn view<'a>(&'a self) -> View<&'a Frame> { - View::new(self, (0, 0), self.size) + View::new(self, Vec2::ZERO, self.size) } pub fn view_mut<'a>(&'a mut self) -> View<&'a mut Frame> { - View::new(self, (0, 0), self.size) + View::new(self, Vec2::ZERO, self.size) } - pub fn view_area<'a>( - &'a self, - offset: (usize, usize), - size: (usize, usize), - ) -> View<&'a Frame> { + pub fn view_area<'a>(&'a self, offset: Vec2, size: Vec2) -> View<&'a Frame> { View::new(self, offset, size) } - pub fn view_area_mut<'a>( - &'a mut self, - offset: (usize, usize), - size: (usize, usize), - ) -> View<&'a mut Frame> { + pub fn view_area_mut<'a>(&'a mut self, offset: Vec2, size: Vec2) -> View<&'a mut Frame> { View::new(self, offset, size) } } -impl Index<(usize, usize)> for Frame { +impl Index<Vec2> for Frame { type Output = Pixel; #[inline] - fn index(&self, (x, y): (usize, usize)) -> &Self::Output { - &self.buffer[x][y] + fn index(&self, Vec2 { x, y }: Vec2) -> &Self::Output { + if x >= 0 && y >= 0 && x < self.size.x && y < self.size.y { + &self.buffer[(x + y * self.size.x) as usize] + } else { + &Pixel::BLACK + } + } +} +impl IndexMut<Vec2> for Frame { + #[inline] + fn index_mut(&mut self, Vec2 { x, y }: Vec2) -> &mut Self::Output { + &mut self.buffer[(x + y * self.size.x) as usize] + } +} + +impl Index<(isize, isize)> for Frame { + type Output = Pixel; + #[inline] + fn index(&self, (x, y): (isize, isize)) -> &Self::Output { + &self[Vec2 { x, y }] } } -impl IndexMut<(usize, usize)> for Frame { +impl IndexMut<(isize, isize)> for Frame { #[inline] - fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output { - &mut self.buffer[x][y] + fn index_mut(&mut self, (x, y): (isize, isize)) -> &mut Self::Output { + &mut self[Vec2 { x, y }] } } |