Skip to main content

vello_cpu/filter/
flood.rs

1// Copyright 2025 the Vello Authors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! Flood filter implementation.
5
6use super::FilterEffect;
7use crate::filter::context::ScratchBuffer;
8use vello_common::filter::flood::Flood;
9use vello_common::pixmap::Pixmap;
10
11impl FilterEffect for Flood {
12    fn execute_lowp(&self, pixmap: &mut Pixmap, _filter_scratch: &mut ScratchBuffer) {
13        pixmap.data_mut().fill(self.color.premultiply().to_rgba8());
14    }
15
16    fn execute_highp(&self, pixmap: &mut Pixmap, _filter_scratch: &mut ScratchBuffer) {
17        pixmap.data_mut().fill(self.color.premultiply().to_rgba8());
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use crate::color::AlphaColor;
25    use crate::filter::context::ScratchBuffer;
26    use vello_common::color::{PremulRgba8, Srgb};
27
28    /// Test flood with semi-transparent color - verifies correct premultiplication.
29    #[test]
30    fn test_flood_semi_transparent_lowp() {
31        let mut pixmap = Pixmap::new(2, 2);
32        let mut filter_scratch = ScratchBuffer::new();
33
34        // Semi-transparent white (50% alpha)
35        let color = AlphaColor {
36            components: [1.0, 1.0, 1.0, 0.5],
37            cs: std::marker::PhantomData::<Srgb>,
38        };
39        let flood = Flood::new(color);
40        flood.execute_lowp(&mut pixmap, &mut filter_scratch);
41
42        // RGB should be premultiplied by alpha: 255 * 0.5 = 127-128
43        for y in 0..2 {
44            for x in 0..2 {
45                let pixel = pixmap.sample(x, y);
46                assert_eq!(
47                    pixel,
48                    PremulRgba8 {
49                        r: 128,
50                        g: 128,
51                        b: 128,
52                        a: 128
53                    }
54                );
55            }
56        }
57    }
58
59    /// Test flood highp with semi-transparent color - verifies correct premultiplication.
60    #[test]
61    fn test_flood_semi_transparent_highp() {
62        let mut pixmap = Pixmap::new(2, 2);
63        let mut filter_scratch = ScratchBuffer::new();
64
65        // Semi-transparent white (50% alpha)
66        let color = AlphaColor {
67            components: [1.0, 1.0, 1.0, 0.5],
68            cs: std::marker::PhantomData::<Srgb>,
69        };
70        let flood = Flood::new(color);
71        flood.execute_highp(&mut pixmap, &mut filter_scratch);
72
73        // RGB should be premultiplied by alpha: 255 * 0.5 = 127-128
74        for y in 0..2 {
75            for x in 0..2 {
76                let pixel = pixmap.sample(x, y);
77                assert_eq!(
78                    pixel,
79                    PremulRgba8 {
80                        r: 128,
81                        g: 128,
82                        b: 128,
83                        a: 128
84                    }
85                );
86            }
87        }
88    }
89}