| 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)] |
| 4 | pub struct Mask { |
| 5 | pub width: usize, |
| 6 | pub height: usize, |
| 7 | pub values: Vec<f32>, |
| 8 | pub matte: bool, |
| 9 | } |
| 10 | |
| 11 | impl 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 { width, height, values, matte: false } |
| 15 | } |
| 16 | |
| 17 | pub fn filled(width: usize, height: usize, value: f32) -> Mask { |
| 18 | Mask::new(width, height, vec![value; width * height]) |
| 19 | } |
| 20 | |
| 21 | pub fn into_matte(self) -> Mask { |
| 22 | Mask { matte: true, ..self } |
| 23 | } |
| 24 | |
| 25 | pub fn from_fn(width: usize, height: usize, person: impl Fn(f32, f32) -> f32) -> Mask { |
| 26 | let values = (0..width * height).map(|index| person(((index % width) as f32 + 0.5) / width as f32, ((index / width) as f32 + 0.5) / height as f32)).collect(); |
| 27 | Mask::new(width, height, values) |
| 28 | } |
| 29 | |
| 30 | pub fn is_empty(&self) -> bool { |
| 31 | self.values.is_empty() |
| 32 | } |
| 33 | |
| 34 | pub fn resampled(&self, width: usize, height: usize) -> Vec<f32> { |
| 35 | let (across, down) = (axis(width, self.width), axis(height, self.height)); |
| 36 | let mut output = Vec::with_capacity(width * height); |
| 37 | for row in 0..height { |
| 38 | let (top, bottom, vertical) = (down.first[row] * self.width, down.second[row] * self.width, down.weight[row]); |
| 39 | for column in 0..width { |
| 40 | let (left, right, horizontal) = (across.first[column], across.second[column], across.weight[column]); |
| 41 | let upper = self.values[top + left] * (1. - horizontal) + self.values[top + right] * horizontal; |
| 42 | let lower = self.values[bottom + left] * (1. - horizontal) + self.values[bottom + right] * horizontal; |
| 43 | output.push(upper * (1. - vertical) + lower * vertical); |
| 44 | } |
| 45 | } |
| 46 | output |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub(crate) struct Axis { |
| 51 | pub(crate) first: Vec<usize>, |
| 52 | pub(crate) second: Vec<usize>, |
| 53 | pub(crate) weight: Vec<f32>, |
| 54 | } |
| 55 | |
| 56 | pub(crate) fn axis(output: usize, input: usize) -> Axis { |
| 57 | let mut axis = Axis { first: Vec::with_capacity(output), second: Vec::with_capacity(output), weight: Vec::with_capacity(output) }; |
| 58 | for index in 0..output { |
| 59 | let position = ((index as f32 + 0.5) * input as f32 / output as f32 - 0.5).clamp(0., (input - 1) as f32); |
| 60 | let first = position.floor() as usize; |
| 61 | axis.first.push(first); |
| 62 | axis.second.push((first + 1).min(input - 1)); |
| 63 | axis.weight.push(position - first as f32); |
| 64 | } |
| 65 | axis |
| 66 | } |
| 67 | |
| 68 | #[cfg(test)] |
| 69 | mod tests { |
| 70 | use super::*; |
| 71 | |
| 72 | #[test] |
| 73 | fn resampling_keeps_flat_masks_and_interpolates_steps() { |
| 74 | assert!(Mask::filled(4, 3, 0.7).resampled(9, 5).iter().all(|&value| (value - 0.7).abs() < 1e-6)); |
| 75 | let step = Mask::new(2, 1, vec![0., 1.]); |
| 76 | assert_eq!(step.resampled(4, 1), vec![0., 0.25, 0.75, 1.]); |
| 77 | let centre = Mask::from_fn(4, 2, |x, y| x + y); |
| 78 | assert_eq!(centre.values, vec![0.375, 0.625, 0.875, 1.125, 0.875, 1.125, 1.375, 1.625]); |
| 79 | } |
| 80 | } |