aboutsummaryrefslogtreecommitdiff
path: root/old/evc/src/refsampler.rs
blob: 8a8f44f03b374664f4ff9cbe87a4af375f76c80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::{
    block::AdvancedReference,
    format::ser::map_scalar8,
    frame::Frame,
    helpers::{matrix::Mat2, pixel::Pixel, vector::Vec2},
    view::View,
};

#[derive(Debug, Clone)]
pub struct Sampler<'a> {
    pub view: View<&'a Frame>,
    pub halfsize: Vec2<f32>,

    pub translation: Vec2<f32>,
    pub transform: Mat2<f32>,

    pub value_scale: f32,
}

impl<'a> Sampler<'a> {
    #[inline]
    pub fn sample(&self, p: Vec2<f32>) -> Pixel {
        self.view
            .sample(self.translation + self.transform.transform(p - self.halfsize) + self.halfsize)
            .scale(self.value_scale)
    }
    pub fn from_refblock(
        view: View<&'a Frame>,
        AdvancedReference {
            translation,
            transform,
            value_scale,
        }: &AdvancedReference,
    ) -> Self {
        Self {
            transform: Mat2 {
                a: map_scalar8(transform.a),
                b: map_scalar8(transform.b),
                c: map_scalar8(transform.c),
                d: map_scalar8(transform.d),
            },
            halfsize: Into::<Vec2<f32>>::into(view.size).scale(0.5),
            translation: Vec2 {
                x: map_scalar8(translation.x),
                y: map_scalar8(translation.y),
            },
            value_scale: 1.05f32.powf(*value_scale as f32),
            view,
        }
    }
}