Skip to content

Commit

Permalink
Clipping and masking is up to 20% faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
RazrFalcon committed Apr 23, 2023
1 parent 902037c commit d68ffc3
Show file tree
Hide file tree
Showing 32 changed files with 12 additions and 66 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
This changelog also contains important changes in dependencies.

## [Unreleased]
### Added
- Clipping and masking is up to 20% faster.

### Fixed
- Improve rectangular clipping anti-aliasing quality.
- Mask's RGB to Luminosity converter was ignoring premultiplied alpha.

## [0.31.1] - 2023-04-22
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ png = { version = "0.17", optional = true }
rgb = "0.8"
svgfilters = { path = "svgfilters", version = "0.4", optional = true }
svgtypes = "0.11"
tiny-skia = { git = "https://github.com/RazrFalcon/tiny-skia", rev = "465404a" }
tiny-skia = { git = "https://github.com/RazrFalcon/tiny-skia", rev = "3690003" }
usvg = { path = "usvg", version = "0.31.0", default-features = false }

[dev-dependencies]
Expand Down
13 changes: 3 additions & 10 deletions src/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,9 @@ pub fn clip(
clip(tree, cp, bbox, canvas);
}

let mut paint = tiny_skia::PixmapPaint::default();
paint.blend_mode = tiny_skia::BlendMode::DestinationOut;
canvas.pixmap.draw_pixmap(
0,
0,
clip_pixmap.as_ref(),
&paint,
tiny_skia::Transform::identity(),
None,
);
let mut mask = tiny_skia::Mask::from_pixmap(clip_pixmap.as_ref(), tiny_skia::MaskType::Alpha);
mask.invert();
canvas.pixmap.apply_mask(&mask);

Some(())
}
Expand Down
55 changes: 2 additions & 53 deletions src/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,63 +56,12 @@ pub fn mask(
crate::render::render_group(tree, &mask.root, &mut RenderState::Ok, &mut mask_canvas);
}

{
use rgb::FromSlice;
image_to_mask(mask_pixmap.data_mut().as_rgba_mut());
}

if let Some(ref mask) = mask.mask {
self::mask(tree, mask, bbox.to_path_bbox(), canvas);
}

let mut paint = tiny_skia::PixmapPaint::default();
paint.blend_mode = tiny_skia::BlendMode::DestinationIn;

canvas.pixmap.draw_pixmap(
0,
0,
mask_pixmap.as_ref(),
&paint,
tiny_skia::Transform::identity(),
None,
);
let mask = tiny_skia::Mask::from_pixmap(mask_pixmap.as_ref(), tiny_skia::MaskType::Luminosity);
canvas.pixmap.apply_mask(&mask);

Some(())
}

/// Converts an image into an alpha mask.
fn image_to_mask(data: &mut [rgb::RGBA8]) {
let coeff_r = 0.2125 / 255.0;
let coeff_g = 0.7154 / 255.0;
let coeff_b = 0.0721 / 255.0;

for pixel in data {
let r = pixel.r as f64;
let g = pixel.g as f64;
let b = pixel.b as f64;

let luma = r * coeff_r + g * coeff_g + b * coeff_b;

pixel.r = 0;
pixel.g = 0;
pixel.b = 0;
pixel.a = f64_bound(0.0, luma * 255.0, 255.0).ceil() as u8;
}
}

// TODO: https://github.com/rust-lang/rust/issues/44095
/// Bounds `f64` number.
#[inline]
fn f64_bound(min: f64, val: f64, max: f64) -> f64 {
debug_assert!(min.is_finite());
debug_assert!(val.is_finite());
debug_assert!(max.is_finite());

if val > max {
max
} else if val < min {
min
} else {
val
}
}
Binary file modified tests/png/a-mask-001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/a-visibility-007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-filter-052.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-filter-053.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-filter-054.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-marker-019.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-012.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-013.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-016.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-017.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-021.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-022.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-023.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-025.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-027.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-028.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/png/e-mask-029.png
Binary file modified tests/png/e-mask-030.png

0 comments on commit d68ffc3

Please sign in to comment.