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.rs46
1 files changed, 33 insertions, 13 deletions
diff --git a/evc/src/frame.rs b/evc/src/frame.rs
index 3c40a3d..024b240 100644
--- a/evc/src/frame.rs
+++ b/evc/src/frame.rs
@@ -1,25 +1,25 @@
use crate::{
+ helpers::vector::Vec2,
pixel::Pixel,
ser::{Sink, Source},
- vec2::Vec2,
view::View,
};
use std::ops::{Index, IndexMut};
#[derive(Debug, Clone)]
pub struct Frame {
- pub size: Vec2,
+ pub size: Vec2<isize>,
buffer: Vec<Pixel>,
}
impl Frame {
- pub fn new(size: Vec2) -> Self {
+ pub fn new(size: Vec2<isize>) -> Self {
Self {
size,
buffer: (0..size.x * size.y).map(|_| Pixel::default()).collect(),
}
}
- pub fn read(source: &mut impl Source, size: Vec2) -> anyhow::Result<Self> {
+ pub fn read(source: &mut impl Source, size: Vec2<isize>) -> anyhow::Result<Self> {
let mut frame = Frame::new(size);
for y in 0..size.y {
for x in 0..size.x {
@@ -38,28 +38,32 @@ impl Frame {
Ok(())
}
pub fn view<'a>(&'a self) -> View<&'a Frame> {
- View::new(self, Vec2::ZERO, self.size)
+ View::new(self, Vec2::<isize>::ZERO, self.size)
}
pub fn view_mut<'a>(&'a mut self) -> View<&'a mut Frame> {
- View::new(self, Vec2::ZERO, self.size)
+ View::new(self, Vec2::<isize>::ZERO, self.size)
}
- pub fn view_area<'a>(&'a self, offset: Vec2, size: Vec2) -> View<&'a Frame> {
+ pub fn view_area<'a>(&'a self, offset: Vec2<isize>, size: Vec2<isize>) -> View<&'a Frame> {
View::new(self, offset, size)
}
- pub fn view_area_mut<'a>(&'a mut self, offset: Vec2, size: Vec2) -> View<&'a mut Frame> {
+ pub fn view_area_mut<'a>(
+ &'a mut self,
+ offset: Vec2<isize>,
+ size: Vec2<isize>,
+ ) -> View<&'a mut Frame> {
View::new(self, offset, size)
}
- pub fn set(&mut self, pos: Vec2, color: Pixel) {
+ pub fn set(&mut self, pos: Vec2<isize>, color: Pixel) {
if pos.x >= 0 && pos.y >= 0 && pos.x < self.size.x && pos.y < self.size.y {
self[pos] = color
}
}
}
-impl Index<Vec2> for Frame {
+impl Index<Vec2<isize>> for Frame {
type Output = Pixel;
#[inline]
- fn index(&self, Vec2 { x, y }: Vec2) -> &Self::Output {
+ fn index(&self, Vec2 { x, y }: Vec2<isize>) -> &Self::Output {
if x >= 0 && y >= 0 && x < self.size.x && y < self.size.y {
&self.buffer[(x + y * self.size.x) as usize]
} else {
@@ -67,13 +71,29 @@ impl Index<Vec2> for Frame {
}
}
}
-impl IndexMut<Vec2> for Frame {
+impl IndexMut<Vec2<isize>> for Frame {
#[inline]
- fn index_mut(&mut self, Vec2 { x, y }: Vec2) -> &mut Self::Output {
+ fn index_mut(&mut self, Vec2 { x, y }: Vec2<isize>) -> &mut Self::Output {
&mut self.buffer[(x + y * self.size.x) as usize]
}
}
+impl Frame {
+ #[inline]
+ pub fn sample(&self, p: Vec2<f32>) -> Pixel {
+ let fx = p.x.floor() as isize;
+ let fy = p.y.floor() as isize;
+ let cx = p.x.ceil() as isize;
+ let cy = p.y.ceil() as isize;
+
+ // TODO dont loose accuracy here
+ Pixel::average(
+ Pixel::average(self[Vec2 { x: fx, y: fy }], self[Vec2 { x: fx, y: cy }]),
+ Pixel::average(self[Vec2 { x: cx, y: fx }], self[Vec2 { x: cx, y: fy }]),
+ )
+ }
+}
+
impl Index<(isize, isize)> for Frame {
type Output = Pixel;
#[inline]