[librsvg: 5/9] Implement `contrast()` filter function.
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 5/9] Implement `contrast()` filter function.
- Date: Wed, 26 May 2021 15:04:44 +0000 (UTC)
commit 59dfc6f1ed5047a0b7e08de93f0f3514bcd9a8d3
Author: John Ledbetter <john ledbetter gmail com>
Date: Sat May 22 10:11:29 2021 -0400
Implement `contrast()` filter function.
Contrast accepts an optional number-or-percentage > 0.0 which
describes the proportion of the transformation to apply. 0% means
grayscale; 100% means unchanged; values over 100% mean increased contrast.
See:
* https://www.w3.org/TR/filter-effects/#contrastEquivalent
* https://www.w3.org/TR/filter-effects/#funcdef-filter-contrast
src/filter_func.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
tests/src/filters.rs | 24 ++++++++++++++++
2 files changed, 104 insertions(+)
---
diff --git a/src/filter_func.rs b/src/filter_func.rs
index aaa29003..c13d486a 100644
--- a/src/filter_func.rs
+++ b/src/filter_func.rs
@@ -19,6 +19,7 @@ use crate::{drawing_ctx::DrawingCtx, filters::component_transfer};
pub enum FilterFunction {
Blur(Blur),
Brightness(Brightness),
+ Contrast(Contrast),
Grayscale(Grayscale),
Invert(Invert),
Opacity(Opacity),
@@ -42,6 +43,14 @@ pub struct Brightness {
proportion: Option<f64>,
}
+/// Parameters for the `contrast()` filter function
+///
+/// https://www.w3.org/TR/filter-effects/#funcdef-filter-contrast
+#[derive(Debug, Clone, PartialEq)]
+pub struct Contrast {
+ proportion: Option<f64>,
+}
+
/// Parameters for the `grayscale()` filter function
///
/// https://www.w3.org/TR/filter-effects/#funcdef-filter-grayscale
@@ -132,6 +141,13 @@ fn parse_brightness<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, P
Ok(FilterFunction::Brightness(Brightness { proportion }))
}
+#[allow(clippy::unnecessary_wraps)]
+fn parse_contrast<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
+ let proportion = parse_num_or_percentage(parser);
+
+ Ok(FilterFunction::Contrast(Contrast { proportion }))
+}
+
#[allow(clippy::unnecessary_wraps)]
fn parse_grayscale<'i>(parser: &mut Parser<'i, '_>) -> Result<FilterFunction, ParseError<'i>> {
let proportion = parse_num_or_percentage_clamped(parser);
@@ -228,6 +244,48 @@ impl Brightness {
}
}
+impl Contrast {
+ fn to_filter_spec(&self, params: &NormalizeParams) -> FilterSpec {
+ let user_space_filter = Filter::default().to_user_space(params);
+ let slope = self.proportion.unwrap_or(1.0);
+ let intercept = -(0.5 * slope) + 0.5;
+
+ let contrast = ResolvedPrimitive {
+ primitive: Primitive::default(),
+ params: PrimitiveParams::ComponentTransfer(component_transfer::ComponentTransfer {
+ functions: component_transfer::Functions {
+ r: component_transfer::FeFuncR {
+ function_type: component_transfer::FunctionType::Linear,
+ slope,
+ intercept,
+ ..component_transfer::FeFuncR::default()
+ },
+ g: component_transfer::FeFuncG {
+ function_type: component_transfer::FunctionType::Linear,
+ slope,
+ intercept,
+ ..component_transfer::FeFuncG::default()
+ },
+ b: component_transfer::FeFuncB {
+ function_type: component_transfer::FunctionType::Linear,
+ slope,
+ intercept,
+ ..component_transfer::FeFuncB::default()
+ },
+ ..component_transfer::Functions::default()
+ },
+ ..component_transfer::ComponentTransfer::default()
+ }),
+ }
+ .into_user_space(params);
+
+ FilterSpec {
+ user_space_filter,
+ primitives: vec![contrast],
+ }
+ }
+}
+
impl Grayscale {
fn to_filter_spec(&self, params: &NormalizeParams) -> FilterSpec {
// grayscale is implemented as the inverse of a saturate operation,
@@ -379,6 +437,7 @@ impl Parse for FilterFunction {
let fns: Vec<(&str, &dyn Fn(&mut Parser<'i, '_>) -> _)> = vec![
("blur", &parse_blur),
("brightness", &parse_brightness),
+ ("contrast", &parse_contrast),
("grayscale", &parse_grayscale),
("invert", &parse_invert),
("opacity", &parse_opacity),
@@ -411,6 +470,7 @@ impl FilterFunction {
match self {
FilterFunction::Blur(v) => Ok(v.to_filter_spec(¶ms)),
FilterFunction::Brightness(v) => Ok(v.to_filter_spec(¶ms)),
+ FilterFunction::Contrast(v) => Ok(v.to_filter_spec(¶ms)),
FilterFunction::Grayscale(v) => Ok(v.to_filter_spec(¶ms)),
FilterFunction::Invert(v) => Ok(v.to_filter_spec(¶ms)),
FilterFunction::Opacity(v) => Ok(v.to_filter_spec(¶ms)),
@@ -456,6 +516,21 @@ mod tests {
);
}
+ #[test]
+ fn parses_contrast() {
+ assert_eq!(
+ FilterFunction::parse_str("contrast()").unwrap(),
+ FilterFunction::Contrast(Contrast { proportion: None })
+ );
+
+ assert_eq!(
+ FilterFunction::parse_str("contrast(50%)").unwrap(),
+ FilterFunction::Contrast(Contrast {
+ proportion: Some(0.50_f32.into()),
+ })
+ );
+ }
+
#[test]
fn parses_grayscale() {
assert_eq!(
@@ -563,6 +638,11 @@ mod tests {
assert!(FilterFunction::parse_str("brightness(foo)").is_err());
}
+ #[test]
+ fn invalid_contrast_yields_error() {
+ assert!(FilterFunction::parse_str("contrast(foo)").is_err());
+ }
+
#[test]
fn invalid_grayscale_yields_error() {
assert!(FilterFunction::parse_str("grayscale(foo)").is_err());
diff --git a/tests/src/filters.rs b/tests/src/filters.rs
index 0c59b4e6..32c85c2f 100644
--- a/tests/src/filters.rs
+++ b/tests/src/filters.rs
@@ -182,6 +182,30 @@ br##"<?xml version="1.0" encoding="UTF-8"?>
"##,
);
+test_compare_render_output!(
+contrast_filter_func,
+br##"<?xml version="1.0" encoding="UTF-8"?>
+<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400"
height="400">
+ <rect x="100" y="100" width="200" height="200" fill="lime" filter="contrast(125%)"/>
+</svg>
+"##,
+br##"<?xml version="1.0" encoding="UTF-8"?>
+<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400"
height="400">
+ <defs>
+ <filter id="filter">
+ <feComponentTransfer>
+ <feFuncR type="linear" slope="1.25" intercept="-0.125" />
+ <feFuncG type="linear" slope="1.25" intercept="-0.125" />
+ <feFuncB type="linear" slope="1.25" intercept="-0.125" />
+ </feComponentTransfer>
+ </filter>
+ </defs>
+
+ <rect x="100" y="100" width="200" height="200" fill="lime" filter="url(#filter)"/>
+</svg>
+"##,
+);
+
test_compare_render_output!(
grayscale_filter_func,
br##"<?xml version="1.0" encoding="UTF-8"?>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]