aboutsummaryrefslogtreecommitdiff
path: root/evc/src/frame.rs
diff options
context:
space:
mode:
Diffstat (limited to 'evc/src/frame.rs')
-rw-r--r--evc/src/frame.rs66
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)]
+ }
}