diff options
Diffstat (limited to 'old/evc/src/helpers')
-rw-r--r-- | old/evc/src/helpers/matrix.rs | 33 | ||||
-rw-r--r-- | old/evc/src/helpers/mod.rs | 4 | ||||
-rw-r--r-- | old/evc/src/helpers/pixel.rs | 81 | ||||
-rw-r--r-- | old/evc/src/helpers/threading.rs | 26 | ||||
-rw-r--r-- | old/evc/src/helpers/vector.rs | 126 |
5 files changed, 270 insertions, 0 deletions
diff --git a/old/evc/src/helpers/matrix.rs b/old/evc/src/helpers/matrix.rs new file mode 100644 index 0000000..0007440 --- /dev/null +++ b/old/evc/src/helpers/matrix.rs @@ -0,0 +1,33 @@ +use crate::helpers::vector::Vec2; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Mat2<T> { + pub a: T, + pub b: T, + pub c: T, + pub d: T, +} + +impl<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T> + Copy> Mat2<T> { + #[inline] + pub fn transform(&self, v: Vec2<T>) -> Vec2<T> { + Vec2 { + x: self.a * v.x + self.b * v.y, + y: self.c * v.x + self.d * v.y, + } + } +} + +impl<T: std::ops::Mul<Output = T> + std::ops::Add<Output = T> + Copy> std::ops::Mul for Mat2<T> { + type Output = Mat2<T>; + #[inline] + fn mul(self, rhs: Mat2<T>) -> Mat2<T> { + let (x, y) = (self, rhs); + Mat2 { + a: x.a * y.a + x.b * y.c, + b: x.a * y.b + x.b * y.d, + c: x.c * y.a + x.d * y.c, + d: x.c * y.b + x.d * y.d, + } + } +} diff --git a/old/evc/src/helpers/mod.rs b/old/evc/src/helpers/mod.rs new file mode 100644 index 0000000..d3aa3d2 --- /dev/null +++ b/old/evc/src/helpers/mod.rs @@ -0,0 +1,4 @@ +pub mod vector; +pub mod threading; +pub mod matrix; +pub mod pixel; diff --git a/old/evc/src/helpers/pixel.rs b/old/evc/src/helpers/pixel.rs new file mode 100644 index 0000000..39fe98c --- /dev/null +++ b/old/evc/src/helpers/pixel.rs @@ -0,0 +1,81 @@ +use crate::format::ser::{Ser, Sink, Source}; + +#[derive(Copy, Clone, Debug, Default, PartialEq)] +pub struct Pixel { + pub r: u8, + pub g: u8, + pub b: u8, +} + +impl Ser for Pixel { + fn write(&self, sink: &mut impl std::io::Write) -> anyhow::Result<()> { + sink.put((self.r, self.g, self.b)) + } + + fn read(source: &mut impl std::io::Read) -> anyhow::Result<Self> { + let (r, g, b) = source.get()?; + Ok(Self { r, g, b }) + } +} + +impl Pixel { + pub const BLACK: Pixel = Pixel { r: 0, g: 0, b: 0 }; + #[inline] + pub fn distance(a: Pixel, b: Pixel) -> usize { + let (rd, gd, bd) = ( + a.r.abs_diff(b.r) as usize, + a.g.abs_diff(b.g) as usize, + a.b.abs_diff(b.b) as usize, + ); + // fast_sqrt(rd * rd + gd * gd + bd * bd) + // SQRT[rd + gd + bd] + rd + gd + bd + } + + #[inline] + pub fn average(a: Pixel, b: Pixel) -> Pixel { + Pixel { + r: ((a.r as u16 + b.r as u16) >> 1) as u8, + g: ((a.g as u16 + b.g as u16) >> 1) as u8, + b: ((a.b as u16 + b.b as u16) >> 1) as u8, + } + } + + #[inline] + 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, + } + } +} + +pub const SQRT: [usize; 256 * 3] = gen_sqrt_lookup(); + +const fn gen_sqrt_lookup<const N: usize>() -> [usize; N] { + let mut arr = [0; N]; + let mut i = 0; + while i < N { + arr[i] = sqrt(i as f32) as usize; + i += 1; + } + arr +} + +const fn sqrt(x: f32) -> f32 { + let a = 1.0; + let a = (a + x / a) * 0.5; + let a = (a + x / a) * 0.5; + let a = (a + x / a) * 0.5; + a +} + +pub fn fast_sqrt(x: usize) -> usize { + let a = 1; + let a = (a + x / (a + 1)) / 2; + let a = (a + x / (a + 1)) / 2; + // let a = (a + x / (a + 1)) / 2; + // let a = (a + x / (a + 1)) / 2; + a +} diff --git a/old/evc/src/helpers/threading.rs b/old/evc/src/helpers/threading.rs new file mode 100644 index 0000000..3291172 --- /dev/null +++ b/old/evc/src/helpers/threading.rs @@ -0,0 +1,26 @@ +use std::{ + sync::atomic::{AtomicUsize, Ordering}, + thread, +}; + +static THREADS_RUNNING: AtomicUsize = AtomicUsize::new(0); + +pub fn both_par<F1, F2, O1, O2>(f1: F1, f2: F2, max_threads: usize) -> (O1, O2) +where + F1: FnOnce() -> O1 + Send + 'static, + O1: Send + 'static, + F2: FnOnce() -> O2, +{ + if THREADS_RUNNING.load(Ordering::Relaxed) < max_threads { + THREADS_RUNNING.fetch_add(1, Ordering::Relaxed); + + let o1h = thread::spawn(move || f1()); + let o2 = f2(); + let o1 = o1h.join().unwrap(); + + THREADS_RUNNING.fetch_sub(1, Ordering::Relaxed); + (o1, o2) + } else { + (f1(), f2()) + } +} diff --git a/old/evc/src/helpers/vector.rs b/old/evc/src/helpers/vector.rs new file mode 100644 index 0000000..a4766d1 --- /dev/null +++ b/old/evc/src/helpers/vector.rs @@ -0,0 +1,126 @@ +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Vec2<T> { + pub x: T, + pub y: T, +} + +impl From<Vec2<isize>> for Vec2<u16> { + fn from(value: Vec2<isize>) -> Self { + Self { + x: value.x as u16, + y: value.y as u16, + } + } +} +impl From<Vec2<u16>> for Vec2<isize> { + fn from(value: Vec2<u16>) -> Self { + Self { + x: value.x as isize, + y: value.y as isize, + } + } +} +impl From<Vec2<f32>> for Vec2<isize> { + fn from(value: Vec2<f32>) -> Self { + Self { + x: value.x as isize, + y: value.y as isize, + } + } +} + +impl Vec2<isize> { + pub const ZERO: Vec2<isize> = Vec2 { x: 0, y: 0 }; + pub const UP: Vec2<isize> = Vec2 { x: 0, y: -1 }; + pub const LEFT: Vec2<isize> = Vec2 { x: -1, y: 0 }; +} +impl Vec2<f32> { + pub const ZERO: Vec2<f32> = Vec2 { x: 0.0, y: 0.0 }; + pub const UP: Vec2<f32> = Vec2 { x: 0.0, y: -1.0 }; + pub const LEFT: Vec2<f32> = Vec2 { x: -1.0, y: 0.0 }; +} + +impl<T: std::ops::Div<Output = T> + Copy> Vec2<T> { + pub fn downscale(&self, f: T) -> Self { + Self { + x: self.x / f, + y: self.y / f, + } + } +} + +impl<T: std::ops::Mul<Output = T> + Copy> Vec2<T> { + pub fn scale(&self, f: T) -> Self { + Self { + x: self.x * f, + y: self.y * f, + } + } + pub fn area(&self) -> T { + self.x * self.y + } +} + +impl Vec2<isize> { + pub fn x_only(&self) -> Self { + Self { x: self.x, y: 0 } + } + pub fn y_only(&self) -> Self { + Self { x: 0, y: self.y } + } +} + +impl Into<Vec2<f32>> for Vec2<isize> { + fn into(self) -> Vec2<f32> { + Vec2 { + x: self.x as f32, + y: self.y as f32, + } + } +} +impl From<(isize, isize)> for Vec2<f32> { + fn from((x, y): (isize, isize)) -> Self { + Vec2 { + x: x as f32, + y: y as f32, + } + } +} + +impl<T: std::ops::Add> std::ops::Add for Vec2<T> { + type Output = Vec2<T::Output>; + #[inline] + fn add(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} +impl<T: std::ops::Sub> std::ops::Sub for Vec2<T> { + type Output = Vec2<T::Output>; + #[inline] + fn sub(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} +impl<T: std::ops::Mul> std::ops::Mul for Vec2<T> { + type Output = Vec2<T::Output>; + #[inline] + fn mul(self, rhs: Self) -> Self::Output { + Vec2 { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl<T> From<(T, T)> for Vec2<T> { + #[inline] + fn from((x, y): (T, T)) -> Self { + Vec2 { x, y } + } +} |