Skip to content

Commit a2568b5

Browse files
committed
Only create soft mask for image if really necessary
1 parent c84eeea commit a2568b5

7 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/render/image.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,21 @@ fn handle_transparent_image(image: &DynamicImage) -> (Vec<u8>, Filter, Option<Ve
127127

128128
let encoded_mask: Option<Vec<u8>> = if color.has_alpha() {
129129
if bits / channels > 8 {
130-
Some(
131-
image
132-
.to_rgba16()
133-
.pixels()
134-
.flat_map(|&Rgba([.., a])| a.to_be_bytes())
135-
.collect(),
136-
)
130+
let image = image.to_rgba16();
131+
132+
if image.pixels().any(|&Rgba([.., a])| a != u16::MAX) {
133+
Some(image.pixels().flat_map(|&Rgba([.., a])| a.to_be_bytes()).collect())
134+
} else {
135+
None
136+
}
137137
} else {
138-
Some(image.to_rgba8().pixels().map(|&Rgba([.., a])| a).collect())
138+
let image = image.to_rgba8();
139+
140+
if image.pixels().any(|&Rgba([.., a])| a != u8::MAX) {
141+
Some(image.pixels().map(|&Rgba([.., a])| a).collect())
142+
} else {
143+
None
144+
}
139145
}
140146
} else {
141147
None
137 Bytes
Loading
272 Bytes
Loading

tests/scripts/gen-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
IGNORE_TESTS = {
1818
# The following test cases still need to be investigated
1919
"svg/resvg/filters/fePointLight/primitiveUnits=objectBoundingBox.svg": INVESTIGATE,
20-
"svg/resvg/filters/filter/with-mask-on-parent.svg": INVESTIGATE,
2120
"svg/resvg/masking/mask/recursive-on-child.svg": INVESTIGATE,
2221
"svg/resvg/paint-servers/pattern/nested-objectBoundingBox.svg": INVESTIGATE,
2322
"svg/resvg/paint-servers/radialGradient/fr=0.2.svg": INVESTIGATE,
@@ -79,6 +78,7 @@
7978
"svg/resvg/paint-servers/radialGradient/attributes-via-xlink-href-from-linearGradient.svg": NO_REFLECT,
8079
"svg/resvg/paint-servers/radialGradient/spreadMethod=reflect.svg": NO_REFLECT,
8180
"svg/resvg/paint-servers/radialGradient/spreadMethod=repeat.svg": NO_REPEAT,
81+
"svg/custom/masking/mask/mask-and-image-with-transparency.svg": "bug",
8282
}
8383

8484

tests/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ fn is_pix_diff(pixel1: &Rgba<u8>, pixel2: &Rgba<u8>) -> bool {
114114
/// Runs a single test instance.
115115
pub fn run_test(svg_path: &str, ref_path: &str, diff_path: &str, replace: bool) -> i32 {
116116
let (_, actual_image) = convert_svg(&fs::read_to_string(svg_path).unwrap());
117+
if !Path::new(ref_path).exists() {
118+
fs::create_dir_all(Path::new(ref_path).parent().unwrap()).unwrap();
119+
save_image(&actual_image, Path::new(ref_path));
120+
return 0;
121+
}
117122
let expected_image = Reader::open(ref_path).unwrap().decode().unwrap().into_rgba8();
118123

119124
let width = max(expected_image.width(), actual_image.width());

tests/src/test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ use crate::run_test;
288288
#[test] fn resvg_filters_filter_on_an_empty_group_2() {assert_eq!(run_test("svg/resvg/filters/filter/on-an-empty-group-2.svg", "ref/resvg/filters/filter/on-an-empty-group-2.png", "diff/resvg/filters/filter/on-an-empty-group-2.png", false), 0)}
289289
#[test] fn resvg_filters_filter_in_BackgroundAlpha() {assert_eq!(run_test("svg/resvg/filters/filter/in=BackgroundAlpha.svg", "ref/resvg/filters/filter/in=BackgroundAlpha.png", "diff/resvg/filters/filter/in=BackgroundAlpha.png", false), 0)}
290290
#[test] fn resvg_filters_filter_with_clip_path_and_mask() {assert_eq!(run_test("svg/resvg/filters/filter/with-clip-path-and-mask.svg", "ref/resvg/filters/filter/with-clip-path-and-mask.png", "diff/resvg/filters/filter/with-clip-path-and-mask.png", false), 0)}
291-
// need to investigate
292-
#[ignore] #[test] fn resvg_filters_filter_with_mask_on_parent() {assert_eq!(run_test("svg/resvg/filters/filter/with-mask-on-parent.svg", "ref/resvg/filters/filter/with-mask-on-parent.png", "diff/resvg/filters/filter/with-mask-on-parent.png", false), 0)}
291+
#[test] fn resvg_filters_filter_with_mask_on_parent() {assert_eq!(run_test("svg/resvg/filters/filter/with-mask-on-parent.svg", "ref/resvg/filters/filter/with-mask-on-parent.png", "diff/resvg/filters/filter/with-mask-on-parent.png", false), 0)}
293292
#[test] fn resvg_filters_filter_with_region_outside_the_canvas() {assert_eq!(run_test("svg/resvg/filters/filter/with-region-outside-the-canvas.svg", "ref/resvg/filters/filter/with-region-outside-the-canvas.png", "diff/resvg/filters/filter/with-region-outside-the-canvas.png", false), 0)}
294293
#[test] fn resvg_filters_filter_transform_on_shape() {assert_eq!(run_test("svg/resvg/filters/filter/transform-on-shape.svg", "ref/resvg/filters/filter/transform-on-shape.png", "diff/resvg/filters/filter/transform-on-shape.png", false), 0)}
295294
#[test] fn resvg_filters_filter_in_BackgroundImage_with_enable_background() {assert_eq!(run_test("svg/resvg/filters/filter/in=BackgroundImage-with-enable-background.svg", "ref/resvg/filters/filter/in=BackgroundImage-with-enable-background.png", "diff/resvg/filters/filter/in=BackgroundImage-with-enable-background.png", false), 0)}
@@ -1775,3 +1774,5 @@ use crate::run_test;
17751774
#[test] fn custom_structure_viewbox_negative_viewbox() {assert_eq!(run_test("svg/custom/structure/viewbox/negative_viewbox.svg", "ref/custom/structure/viewbox/negative_viewbox.png", "diff/custom/structure/viewbox/negative_viewbox.png", false), 0)}
17761775
#[test] fn custom_masking_clipPath_clip_path_with_nested_clip_path_and_transform() {assert_eq!(run_test("svg/custom/masking/clipPath/clip-path-with-nested-clip-path-and-transform.svg", "ref/custom/masking/clipPath/clip-path-with-nested-clip-path-and-transform.png", "diff/custom/masking/clipPath/clip-path-with-nested-clip-path-and-transform.png", false), 0)}
17771776
#[test] fn custom_masking_clipPath_complex_clip_path_with_nested_clip_path_on_child() {assert_eq!(run_test("svg/custom/masking/clipPath/complex-clip-path-with-nested-clip-path-on-child.svg", "ref/custom/masking/clipPath/complex-clip-path-with-nested-clip-path-on-child.png", "diff/custom/masking/clipPath/complex-clip-path-with-nested-clip-path-on-child.png", false), 0)}
1777+
// bug
1778+
#[ignore] #[test] fn custom_masking_mask_mask_and_image_with_transparency() {assert_eq!(run_test("svg/custom/masking/mask/mask-and-image-with-transparency.svg", "ref/custom/masking/mask/mask-and-image-with-transparency.png", "diff/custom/masking/mask/mask-and-image-with-transparency.png", false), 0)}
Lines changed: 12 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)