diff options
Diffstat (limited to 'evc')
-rw-r--r-- | evc/src/bin/encode.rs | 19 | ||||
-rw-r--r-- | evc/src/block.rs | 10 | ||||
-rw-r--r-- | evc/src/frame.rs | 66 | ||||
-rw-r--r-- | evc/src/pixel.rs | 11 |
4 files changed, 91 insertions, 15 deletions
diff --git a/evc/src/bin/encode.rs b/evc/src/bin/encode.rs index 1090831..c932fe9 100644 --- a/evc/src/bin/encode.rs +++ b/evc/src/bin/encode.rs @@ -1,5 +1,5 @@ use clap::Parser; -use evc::{pixel::Pixel, ser::Source}; +use evc::{frame::Frame}; use std::io::{self, BufReader}; #[derive(Parser)] @@ -16,13 +16,16 @@ fn main() -> io::Result<()> { let mut input = BufReader::new(std::io::stdin()); - loop { - for x in 0..args.width { - for y in 0..args.height { - let pixel = input.get::<Pixel>()?; - println!("P({x}|{y}) = {pixel:?}") - } - } + let size = (args.width, args.height); + + let mut prev_frame = Frame::new(size); + for i in 0.. { + println!("frame {i}"); + let frame = Frame::read(&mut input, size)?; + + + + prev_frame = frame; } Ok(()) diff --git a/evc/src/block.rs b/evc/src/block.rs index 8d98d55..05ff56d 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -1,5 +1,7 @@ -use crate::{ser::{Ser, Sink, Source}, pixel::Pixel}; - +use crate::{ + pixel::Pixel, + ser::{Ser, Sink, Source}, +}; #[derive(Clone, Debug)] pub struct Block { @@ -26,7 +28,7 @@ impl Block { a.write(sink)?; b.write(sink)?; } - BlockInner::Reference { translation } => { + BlockInner::Reference { translation: _ } => { sink.put(2u8)?; } } @@ -50,4 +52,4 @@ impl Block { Ok(Self { size, inner }) } -}
\ No newline at end of file +} 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)] + } } diff --git a/evc/src/pixel.rs b/evc/src/pixel.rs index adeaf84..559dedc 100644 --- a/evc/src/pixel.rs +++ b/evc/src/pixel.rs @@ -17,3 +17,14 @@ impl Ser for Pixel { Ok(Self { r, g, b }) } } + +impl Pixel { + pub fn distance(a: Pixel, b: Pixel) -> f64 { + let (rd, gd, bd) = ( + a.r.abs_diff(b.r) as f64, + a.r.abs_diff(b.r) as f64, + a.r.abs_diff(b.r) as f64, + ); + (rd * rd + gd * gd + bd * bd).sqrt() + } +} |