diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-05 22:27:49 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-05 22:27:49 +0100 |
commit | 4b12b320d48abf90a6f524a226604ad738e5684f (patch) | |
tree | 84aff2f65bd0714ad770ea8928ad9fabfa7263ea /evc/src/frame.rs | |
parent | 8e4ec0943973b96addbe01f4c02f91cf04d081a7 (diff) | |
download | video-codec-experiments-4b12b320d48abf90a6f524a226604ad738e5684f.tar video-codec-experiments-4b12b320d48abf90a6f524a226604ad738e5684f.tar.bz2 video-codec-experiments-4b12b320d48abf90a6f524a226604ad738e5684f.tar.zst |
even more code
Diffstat (limited to 'evc/src/frame.rs')
-rw-r--r-- | evc/src/frame.rs | 66 |
1 files changed, 63 insertions, 3 deletions
diff --git a/evc/src/frame.rs b/evc/src/frame.rs index 8e90832..1c18620 100644 --- a/evc/src/frame.rs +++ b/evc/src/frame.rs @@ -1,8 +1,11 @@ -use crate::pixel::Pixel; - +use crate::{pixel::Pixel, ser::Source}; +use std::{ + io, + ops::{Index, IndexMut}, +}; pub struct Frame { - size: (usize, usize), + pub size: (usize, usize), buffer: Vec<Vec<Pixel>>, } @@ -15,4 +18,61 @@ impl Frame { .collect(), } } + pub fn read(source: &mut impl Source, size: (usize, usize)) -> io::Result<Self> { + let mut frame = Frame::new(size); + for x in 0..size.0 { + for y in 0..size.1 { + let pixel = source.get::<Pixel>()?; + frame[(x, y)] = pixel; + } + } + Ok(frame) + } +} + +impl Index<(usize, usize)> for Frame { + type Output = Pixel; + fn index(&self, (x, y): (usize, usize)) -> &Self::Output { + &self.buffer[x][y] + } +} +impl IndexMut<(usize, usize)> for Frame { + fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Self::Output { + &mut self.buffer[x][y] + } +} + +pub struct View<'a> { + frame: &'a Frame, + offset: (usize, usize), + size: (usize, usize), +} + +impl<'a> View<'a> { + pub fn new(frame: &'a Frame, offset: (usize, usize), size: (usize, usize)) -> Self { + Self { + frame, + offset, + size, + } + } + pub fn diff(&self, rhs: &Self) -> f64 { + assert_eq!(self.size, rhs.size); + let mut acc = 0.0; + for x in 0..self.size.0 { + for y in 0..self.size.1 { + let a = self[(x, y)]; + let b = rhs[(x, y)]; + acc += Pixel::distance(a, b); + } + } + acc + } +} + +impl Index<(usize, usize)> for View<'_> { + type Output = Pixel; + fn index(&self, (x, y): (usize, usize)) -> &Self::Output { + &self.frame[(x + self.offset.0, y + self.offset.1)] + } } |