aboutsummaryrefslogtreecommitdiff
path: root/evc/src/view.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2022-12-05 23:11:56 +0100
committermetamuffin <metamuffin@disroot.org>2022-12-05 23:11:56 +0100
commit7282efd3720e992735840f9495cbb12ba6d880c3 (patch)
treebfe21187e147b108ab53b89161ce0c7927d23ef2 /evc/src/view.rs
parent4b4db2adea4afb5d77383007bcdc4536c1fadf43 (diff)
downloadvideo-codec-experiments-7282efd3720e992735840f9495cbb12ba6d880c3.tar
video-codec-experiments-7282efd3720e992735840f9495cbb12ba6d880c3.tar.bz2
video-codec-experiments-7282efd3720e992735840f9495cbb12ba6d880c3.tar.zst
encoding
Diffstat (limited to 'evc/src/view.rs')
-rw-r--r--evc/src/view.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/evc/src/view.rs b/evc/src/view.rs
new file mode 100644
index 0000000..ae7012f
--- /dev/null
+++ b/evc/src/view.rs
@@ -0,0 +1,74 @@
+use crate::{frame::Frame, pixel::Pixel};
+use std::ops::Index;
+
+pub struct View<'a> {
+ pub frame: &'a Frame,
+ pub offset: (usize, usize),
+ pub 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(va: &Self, vb: &Self) -> f64 {
+ assert_eq!(va.size, vb.size);
+ let mut acc = 0.0;
+ for x in 0..va.size.0 {
+ for y in 0..va.size.1 {
+ let a = va[(x, y)];
+ let b = vb[(x, y)];
+ acc += Pixel::distance(a, b);
+ }
+ }
+ acc
+ }
+ pub fn split(&self) -> [Self; 2] {
+ let vert = self.size.0 > self.size.1;
+ [
+ Self {
+ frame: self.frame,
+ offset: self.offset,
+ size: if vert {
+ (self.size.0 / 2, self.size.1)
+ } else {
+ (self.size.0, self.size.1 / 2)
+ },
+ },
+ Self {
+ frame: self.frame,
+ offset: if vert {
+ (self.offset.0 + self.size.0 / 2, self.offset.1)
+ } else {
+ (self.offset.0, self.offset.1 + self.size.1 / 2)
+ },
+ size: if vert {
+ (self.size.0 - self.size.0 / 2, self.size.1)
+ } else {
+ (self.size.0, self.size.1 - self.size.1 / 2)
+ },
+ },
+ ]
+ }
+ pub fn pixels(&self) -> Vec<Pixel> {
+ let mut v = vec![];
+ for x in 0..self.size.0 {
+ for y in 0..self.size.1 {
+ v.push(self[(x, y)]);
+ }
+ }
+ v
+ }
+}
+
+impl Index<(usize, usize)> for View<'_> {
+ type Output = Pixel;
+ #[inline]
+ fn index(&self, (x, y): (usize, usize)) -> &Self::Output {
+ &self.frame[(x + self.offset.0, y + self.offset.1)]
+ }
+}