diff options
author | metamuffin <metamuffin@disroot.org> | 2022-12-07 17:55:27 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-12-07 17:55:27 +0100 |
commit | 8ca219c6b0d5448fd4529713ccd093e89de4e252 (patch) | |
tree | 8fe02fd35ae337766d6f8e7533da170830162d14 /evc/src | |
parent | a99674b911cb9b2fa398ccf61830d5933ccaf931 (diff) | |
download | video-codec-experiments-8ca219c6b0d5448fd4529713ccd093e89de4e252.tar video-codec-experiments-8ca219c6b0d5448fd4529713ccd093e89de4e252.tar.bz2 video-codec-experiments-8ca219c6b0d5448fd4529713ccd093e89de4e252.tar.zst |
refactor
Diffstat (limited to 'evc/src')
-rw-r--r-- | evc/src/bin/decode.rs | 8 | ||||
-rw-r--r-- | evc/src/bin/encode.rs | 3 | ||||
-rw-r--r-- | evc/src/block.rs | 6 | ||||
-rw-r--r-- | evc/src/codec/encode.rs | 4 | ||||
-rw-r--r-- | evc/src/debug.rs | 2 | ||||
-rw-r--r-- | evc/src/format/header.rs (renamed from evc/src/header.rs) | 2 | ||||
-rw-r--r-- | evc/src/format/mod.rs | 2 | ||||
-rw-r--r-- | evc/src/format/ser.rs (renamed from evc/src/ser.rs) | 35 | ||||
-rw-r--r-- | evc/src/frame.rs | 5 | ||||
-rw-r--r-- | evc/src/helpers/mod.rs | 3 | ||||
-rw-r--r-- | evc/src/helpers/pixel.rs (renamed from evc/src/pixel.rs) | 9 | ||||
-rw-r--r-- | evc/src/helpers/vector.rs | 29 | ||||
-rw-r--r-- | evc/src/lib.rs | 4 | ||||
-rw-r--r-- | evc/src/refsampler.rs | 17 | ||||
-rw-r--r-- | evc/src/view.rs | 5 |
15 files changed, 78 insertions, 56 deletions
diff --git a/evc/src/bin/decode.rs b/evc/src/bin/decode.rs index b15467f..b7dd4e1 100644 --- a/evc/src/bin/decode.rs +++ b/evc/src/bin/decode.rs @@ -2,8 +2,12 @@ use anyhow::Context; use clap::Parser; use evc::{ - block::Block, codec::decode::decode_block, frame::Frame, header::Header, pixel::Pixel, - ser::Source, view::View, + block::Block, + codec::decode::decode_block, + format::{header::Header, ser::Source}, + frame::Frame, + helpers::pixel::Pixel, + view::View, }; use log::info; use std::io::{BufReader, BufWriter}; diff --git a/evc/src/bin/encode.rs b/evc/src/bin/encode.rs index 611347f..f81bb5e 100644 --- a/evc/src/bin/encode.rs +++ b/evc/src/bin/encode.rs @@ -5,10 +5,9 @@ use evc::{ decode::decode_block, encode::{encode_block, EncodeConfig}, }, + format::{header::Header, ser::Sink}, frame::Frame, - header::Header, helpers::vector::Vec2, - ser::Sink, }; use log::info; use std::io::{BufReader, BufWriter}; diff --git a/evc/src/block.rs b/evc/src/block.rs index dd23207..3a37820 100644 --- a/evc/src/block.rs +++ b/evc/src/block.rs @@ -1,9 +1,9 @@ use anyhow::bail; use crate::{ - pixel::Pixel, - ser::{Ser, Sink, Source}, - helpers::vector::{Small, Vec2}, + format::ser::{Ser, Sink, Source, Small}, + helpers::pixel::Pixel, + helpers::vector::Vec2, }; #[derive(Clone, Debug)] diff --git a/evc/src/codec/encode.rs b/evc/src/codec/encode.rs index ce5d71b..b11bbc0 100644 --- a/evc/src/codec/encode.rs +++ b/evc/src/codec/encode.rs @@ -1,5 +1,7 @@ use crate::{ - block::Block, frame::Frame, helpers::threading::both_par, helpers::vector::Vec2, pixel::Pixel, + block::Block, + frame::Frame, + helpers::{pixel::Pixel, threading::both_par, vector::Vec2}, view::View, }; diff --git a/evc/src/debug.rs b/evc/src/debug.rs index d2102ba..fb2b53d 100644 --- a/evc/src/debug.rs +++ b/evc/src/debug.rs @@ -1,4 +1,4 @@ -use crate::{frame::Frame, helpers::vector::Vec2, pixel::Pixel, view::View}; +use crate::{frame::Frame, helpers::pixel::Pixel, helpers::vector::Vec2, view::View}; impl View<&mut Frame> { pub fn draw_box(&mut self, color: Pixel) { diff --git a/evc/src/header.rs b/evc/src/format/header.rs index 4c9118e..371b4ba 100644 --- a/evc/src/header.rs +++ b/evc/src/format/header.rs @@ -1,6 +1,6 @@ use crate::{ helpers::vector::Vec2, - ser::{Ser, Sink, Source}, + format::ser::{Ser, Sink, Source}, }; #[derive(Debug, Clone, PartialEq, Copy)] diff --git a/evc/src/format/mod.rs b/evc/src/format/mod.rs new file mode 100644 index 0000000..d4fb18c --- /dev/null +++ b/evc/src/format/mod.rs @@ -0,0 +1,2 @@ +pub mod header; +pub mod ser;
\ No newline at end of file diff --git a/evc/src/ser.rs b/evc/src/format/ser.rs index d05e42e..65d5e26 100644 --- a/evc/src/ser.rs +++ b/evc/src/format/ser.rs @@ -1,6 +1,8 @@ use anyhow::Context; use std::io::{Read, Write}; +use crate::helpers::vector::Vec2; + pub trait Sink { fn put<V: Ser>(&mut self, value: V) -> anyhow::Result<()>; } @@ -190,12 +192,39 @@ impl Ser for f64 { } } + +impl Ser for Vec2<isize> { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.x, self.y)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (x, y) = source.get()?; + Ok(Vec2 { x, y }) + } +} + +pub struct Small<T>(pub T); +impl Ser for Small<Vec2<isize>> { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.0.x as i8, self.0.y as i8)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (x, y): (i8, i8) = source.get()?; + Ok(Small(Vec2 { + x: x as isize, + y: y as isize, + })) + } +} + + #[cfg(test)] mod test { use super::{Ser, Sink}; - use crate::header::Header; - use crate::helpers::vector::Vec2; - use crate::ser::Source; + use crate::format::header::Header; + use crate::{format::ser::Source, helpers::vector::Vec2}; use std::fmt::Debug; use std::io::Cursor; diff --git a/evc/src/frame.rs b/evc/src/frame.rs index 024b240..e1c3a6e 100644 --- a/evc/src/frame.rs +++ b/evc/src/frame.rs @@ -1,7 +1,6 @@ use crate::{ - helpers::vector::Vec2, - pixel::Pixel, - ser::{Sink, Source}, + format::ser::{Sink, Source}, + helpers::{pixel::Pixel, vector::Vec2}, view::View, }; use std::ops::{Index, IndexMut}; diff --git a/evc/src/helpers/mod.rs b/evc/src/helpers/mod.rs index c5eed9d..d3aa3d2 100644 --- a/evc/src/helpers/mod.rs +++ b/evc/src/helpers/mod.rs @@ -1,3 +1,4 @@ pub mod vector; pub mod threading; -pub mod matrix;
\ No newline at end of file +pub mod matrix; +pub mod pixel; diff --git a/evc/src/pixel.rs b/evc/src/helpers/pixel.rs index 121cb95..7b7c8d5 100644 --- a/evc/src/pixel.rs +++ b/evc/src/helpers/pixel.rs @@ -1,4 +1,4 @@ -use crate::ser::{Ser, Sink, Source}; +use crate::format::ser::{Ser, Sink, Source}; #[derive(Copy, Clone, Debug, Default)] pub struct Pixel { @@ -37,6 +37,13 @@ impl Pixel { b: (a.b >> 2) + (b.b >> 2), } } + pub fn scale(&self, factor: f32) -> Pixel { + Pixel { + r: ((self.r as f32) * factor).clamp(0.0, 255.0) as u8, + g: ((self.g as f32) * factor).clamp(0.0, 255.0) as u8, + b: ((self.b as f32) * factor).clamp(0.0, 255.0) as u8, + } + } } const SQRT: [usize; 256 * 3] = gen_sqrt_lookup(); diff --git a/evc/src/helpers/vector.rs b/evc/src/helpers/vector.rs index 9e7369e..4daa849 100644 --- a/evc/src/helpers/vector.rs +++ b/evc/src/helpers/vector.rs @@ -1,5 +1,3 @@ -use crate::ser::{Ser, Sink, Source}; - #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Vec2<T> { pub x: T, @@ -26,32 +24,6 @@ impl<T: std::ops::Div<Output = T> + Copy> Vec2<T> { } } -impl Ser for Vec2<isize> { - fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { - sink.put((self.x, self.y)) - } - - fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { - let (x, y) = source.get()?; - Ok(Vec2 { x, y }) - } -} - -pub struct Small<T>(pub T); -impl Ser for Small<Vec2<isize>> { - fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { - sink.put((self.0.x as i8, self.0.y as i8)) - } - - fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { - let (x, y): (i8, i8) = source.get()?; - Ok(Small(Vec2 { - x: x as isize, - y: y as isize, - })) - } -} - impl<T: std::ops::Add> std::ops::Add for Vec2<T> { type Output = Vec2<T::Output>; #[inline] @@ -89,4 +61,3 @@ impl<T> From<(T, T)> for Vec2<T> { Vec2 { x, y } } } - diff --git a/evc/src/lib.rs b/evc/src/lib.rs index ffacf59..d269471 100644 --- a/evc/src/lib.rs +++ b/evc/src/lib.rs @@ -7,9 +7,7 @@ pub mod block; pub mod codec; pub mod debug; pub mod frame; -pub mod header; -pub mod pixel; -pub mod ser; pub mod view; pub mod refsampler; pub mod helpers; +pub mod format; diff --git a/evc/src/refsampler.rs b/evc/src/refsampler.rs index 95e123b..db0ab6f 100644 --- a/evc/src/refsampler.rs +++ b/evc/src/refsampler.rs @@ -1,14 +1,21 @@ -use crate::{helpers::{matrix::Mat2, vector::Vec2}, frame::Frame}; +use crate::{ + frame::Frame, + helpers::{matrix::Mat2, pixel::Pixel, vector::Vec2}, +}; + +pub struct Sampler<'a> { + frame: &'a Frame, -pub struct Sampler { translation: Vec2<f32>, transform: Mat2<f32>, value_scale: f32, } -impl Sampler { - pub fn sample(&self, frame: &Frame, p: Vec2<f32>) { - +impl Sampler<'_> { + pub fn sample(&self, p: Vec2<f32>) -> Pixel { + self.frame + .sample(self.translation + self.transform.transform(p)) + .scale(self.value_scale) } } diff --git a/evc/src/view.rs b/evc/src/view.rs index 5596637..3843128 100644 --- a/evc/src/view.rs +++ b/evc/src/view.rs @@ -1,4 +1,7 @@ -use crate::{frame::Frame, helpers::vector::Vec2, pixel::Pixel}; +use crate::{ + frame::Frame, + helpers::{pixel::Pixel, vector::Vec2}, +}; use std::ops::{Index, IndexMut}; pub struct View<T> { |