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
|
use crate::{block::Block, frame::Frame, vec2::Vec2, view::View};
#[derive(Debug, Clone)]
pub struct EncodeConfig {
pub translate: bool,
pub ref_thres: f64,
pub max_diff_size: isize,
pub min_block_size: isize,
}
pub fn encode_block(view: View<&Frame>, prev: View<&Frame>, config: &EncodeConfig) -> Block {
let (diff, translation) = if view.area() > config.max_diff_size {
(f64::INFINITY, Vec2::ZERO)
} else if config.translate {
let mut best_diff = f64::INFINITY;
let mut best_translation = Vec2::ZERO;
for x in [-32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32] {
for y in [-32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32] {
let translation = Vec2 { x, y };
let diff = View::diff(&view, &prev.offset(translation)) / view.area() as f64;
if diff < best_diff {
best_translation = translation;
best_diff = diff;
}
}
}
(best_diff, best_translation)
} else {
(View::diff(&view, &prev) / view.area() as f64, Vec2::ZERO)
};
if diff < config.ref_thres {
Block::Reference { translation }
} else {
if view.size.x < config.min_block_size {
Block::Literal(view.pixels())
} else {
let [av, bv] = view.split();
let [ap, bp] = prev.split();
let a = encode_block(av, ap, config);
let b = encode_block(bv, bp, config);
if a.is_literal() && b.is_literal() {
Block::Literal(view.pixels())
} else {
Block::Split(Box::new([a, b]))
}
}
}
}
|