Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions crates/resvg/src/filter/morphology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ use usvg::filter::MorphologyOperator;
///
/// This method will allocate a copy of the `src` image as a back buffer.
pub fn apply(operator: MorphologyOperator, rx: f32, ry: f32, src: ImageRefMut) {
// No point in making matrix larger than image.
let columns = std::cmp::min(rx.ceil() as u32 * 2, src.width);
let rows = std::cmp::min(ry.ceil() as u32 * 2, src.height);
let target_x = (columns as f32 / 2.0).floor() as u32;
let target_y = (rows as f32 / 2.0).floor() as u32;

let width_max = src.width as i32 - 1;
let height_max = src.height as i32 - 1;
let width_max = src.width - 1;
let height_max = src.height - 1;
let rx = (rx.round() as u32).min(width_max);
let ry = (ry.round() as u32).min(height_max);

let mut buf = vec![RGBA8::default(); src.data.len()];
let mut buf = ImageRefMut::new(src.width, src.height, &mut buf);
let mut x = 0;
let mut y = 0;
let mut x: u32 = 0;
let mut y: u32 = 0;
for _ in src.data.iter() {
let mut new_p = RGBA8::default();
if operator == MorphologyOperator::Erode {
Expand All @@ -35,26 +31,22 @@ pub fn apply(operator: MorphologyOperator, rx: f32, ry: f32, src: ImageRefMut) {
new_p.a = 255;
}

for oy in 0..rows {
for ox in 0..columns {
let tx = x as i32 - target_x as i32 + ox as i32;
let ty = y as i32 - target_y as i32 + oy as i32;

if tx < 0 || tx > width_max || ty < 0 || ty > height_max {
continue;
}
let x_range = x.saturating_sub(rx)..=(x + rx).min(width_max);
let y_range = y.saturating_sub(ry)..=(y + ry).min(height_max);

let p = src.pixel_at(tx as u32, ty as u32);
for ty in y_range {
for tx in x_range.clone() {
let p = src.pixel_at(tx, ty);
if operator == MorphologyOperator::Erode {
new_p.r = std::cmp::min(p.r, new_p.r);
new_p.g = std::cmp::min(p.g, new_p.g);
new_p.b = std::cmp::min(p.b, new_p.b);
new_p.a = std::cmp::min(p.a, new_p.a);
new_p.r = p.r.min(new_p.r);
new_p.g = p.g.min(new_p.g);
new_p.b = p.b.min(new_p.b);
new_p.a = p.a.min(new_p.a);
} else {
new_p.r = std::cmp::max(p.r, new_p.r);
new_p.g = std::cmp::max(p.g, new_p.g);
new_p.b = std::cmp::max(p.b, new_p.b);
new_p.a = std::cmp::max(p.a, new_p.a);
new_p.r = p.r.max(new_p.r);
new_p.g = p.g.max(new_p.g);
new_p.b = p.b.max(new_p.b);
new_p.a = p.a.max(new_p.a);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/resvg/tests/integration/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,13 @@ use crate::render;
#[test] fn filters_feMerge_color_interpolation_filters_eq_linearRGB() { assert_eq!(render("tests/filters/feMerge/color-interpolation-filters=linearRGB"), 0); }
#[test] fn filters_feMerge_color_interpolation_filters_eq_sRGB() { assert_eq!(render("tests/filters/feMerge/color-interpolation-filters=sRGB"), 0); }
#[test] fn filters_feMerge_complex_transform() { assert_eq!(render("tests/filters/feMerge/complex-transform"), 0); }
#[test] fn filters_feMorphology_centered_kernel() { assert_eq!(render("tests/filters/feMorphology/centered-kernel"), 0); }
#[test] fn filters_feMorphology_empty_radius() { assert_eq!(render("tests/filters/feMorphology/empty-radius"), 0); }
#[test] fn filters_feMorphology_fractional_radius() { assert_eq!(render("tests/filters/feMorphology/fractional-radius"), 0); }
#[test] fn filters_feMorphology_negative_radius() { assert_eq!(render("tests/filters/feMorphology/negative-radius"), 0); }
#[test] fn filters_feMorphology_no_radius() { assert_eq!(render("tests/filters/feMorphology/no-radius"), 0); }
#[test] fn filters_feMorphology_operator_eq_dilate() { assert_eq!(render("tests/filters/feMorphology/operator=dilate"), 0); }
#[test] fn filters_feMorphology_radius_larger_than_region() { assert_eq!(render("tests/filters/feMorphology/radius-larger-than-region"), 0); }
#[test] fn filters_feMorphology_radius_with_too_many_values() { assert_eq!(render("tests/filters/feMorphology/radius-with-too-many-values"), 0); }
#[test] fn filters_feMorphology_radius_eq_0_5_with_objectBoundingBox() { assert_eq!(render("tests/filters/feMorphology/radius=0.5-with-objectBoundingBox"), 0); }
#[test] fn filters_feMorphology_radius_eq_0_5() { assert_eq!(render("tests/filters/feMorphology/radius=0.5"), 0); }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions crates/resvg/tests/tests/filters/feMorphology/centered-kernel.svg
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 crates/resvg/tests/tests/filters/feMorphology/empty-radius.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 crates/resvg/tests/tests/filters/feMorphology/no-radius.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 crates/resvg/tests/tests/filters/feMorphology/radius=0.5.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 crates/resvg/tests/tests/filters/feMorphology/radius=1-10.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 crates/resvg/tests/tests/filters/feMorphology/radius=10-0.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 crates/resvg/tests/tests/filters/feMorphology/radius=10-1.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 crates/resvg/tests/tests/filters/feMorphology/simple-case.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 crates/resvg/tests/tests/filters/feMorphology/zero-radius.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading