BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / mask.rs

mask.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
mask.rs 3.7 KiB Download Raw
1/// Person probability (0..1) on a grid of its own size, independent of the frame size.
2/// `matte` marks a matting model's output: already a soft alpha with real edges, not a coarse probability.
3#[derive(Clone, Debug, Default, PartialEq)]
4pub struct Mask {
5 pub width: usize,
6 pub height: usize,
7 pub values: Vec<f32>,
8 pub matte: bool,
9}
10
11impl Mask {
12 pub fn new(width: usize, height: usize, values: Vec<f32>) -> Mask {
13 assert_eq!(values.len(), width * height, "mask values match its size");
14 Mask {
15 width,
16 height,
17 values,
18 matte: false,
19 }
20 }
21
22 pub fn filled(width: usize, height: usize, value: f32) -> Mask {
23 Mask::new(width, height, vec![value; width * height])
24 }
25
26 pub fn into_matte(self) -> Mask {
27 Mask {
28 matte: true,
29 ..self
30 }
31 }
32
33 pub fn from_fn(width: usize, height: usize, person: impl Fn(f32, f32) -> f32) -> Mask {
34 let values = (0..width * height)
35 .map(|index| {
36 person(
37 ((index % width) as f32 + 0.5) / width as f32,
38 ((index / width) as f32 + 0.5) / height as f32,
39 )
40 })
41 .collect();
42 Mask::new(width, height, values)
43 }
44
45 pub fn is_empty(&self) -> bool {
46 self.values.is_empty()
47 }
48
49 pub fn resampled(&self, width: usize, height: usize) -> Vec<f32> {
50 let (across, down) = (axis(width, self.width), axis(height, self.height));
51 let mut output = Vec::with_capacity(width * height);
52 for row in 0..height {
53 let (top, bottom, vertical) = (
54 down.first[row] * self.width,
55 down.second[row] * self.width,
56 down.weight[row],
57 );
58 for column in 0..width {
59 let (left, right, horizontal) = (
60 across.first[column],
61 across.second[column],
62 across.weight[column],
63 );
64 let upper = self.values[top + left] * (1. - horizontal)
65 + self.values[top + right] * horizontal;
66 let lower = self.values[bottom + left] * (1. - horizontal)
67 + self.values[bottom + right] * horizontal;
68 output.push(upper * (1. - vertical) + lower * vertical);
69 }
70 }
71 output
72 }
73}
74
75pub(crate) struct Axis {
76 pub(crate) first: Vec<usize>,
77 pub(crate) second: Vec<usize>,
78 pub(crate) weight: Vec<f32>,
79}
80
81pub(crate) fn axis(output: usize, input: usize) -> Axis {
82 let mut axis = Axis {
83 first: Vec::with_capacity(output),
84 second: Vec::with_capacity(output),
85 weight: Vec::with_capacity(output),
86 };
87 for index in 0..output {
88 let position = ((index as f32 + 0.5) * input as f32 / output as f32 - 0.5)
89 .clamp(0., (input - 1) as f32);
90 let first = position.floor() as usize;
91 axis.first.push(first);
92 axis.second.push((first + 1).min(input - 1));
93 axis.weight.push(position - first as f32);
94 }
95 axis
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn resampling_keeps_flat_masks_and_interpolates_steps() {
104 assert!(
105 Mask::filled(4, 3, 0.7)
106 .resampled(9, 5)
107 .iter()
108 .all(|&value| (value - 0.7).abs() < 1e-6)
109 );
110 let step = Mask::new(2, 1, vec![0., 1.]);
111 assert_eq!(step.resampled(4, 1), vec![0., 0.25, 0.75, 1.]);
112 let centre = Mask::from_fn(4, 2, |x, y| x + y);
113 assert_eq!(
114 centre.values,
115 vec![0.375, 0.625, 0.875, 1.125, 0.875, 1.125, 1.375, 1.625]
116 );
117 }
118}