[librsvg: 1/2] mask: move code to surface_utils



commit 571ffbef3789a98bf8283465b85f4000513d176d
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Oct 24 19:47:28 2019 +0200

    mask: move code to surface_utils
    
    Move operations that deal with the internal pixel representation
    to the shared surface module.

 rsvg_internals/src/mask.rs                         | 51 +++-------------------
 rsvg_internals/src/surface_utils/shared_surface.rs | 29 ++++++++++++
 2 files changed, 34 insertions(+), 46 deletions(-)
---
diff --git a/rsvg_internals/src/mask.rs b/rsvg_internals/src/mask.rs
index 550d2703..4b75ca6c 100644
--- a/rsvg_internals/src/mask.rs
+++ b/rsvg_internals/src/mask.rs
@@ -10,14 +10,7 @@ use crate::node::{CascadedValues, NodeDraw, NodeResult, NodeTrait, RsvgNode};
 use crate::parsers::{Parse, ParseValue};
 use crate::property_bag::PropertyBag;
 use crate::property_defs::Opacity;
-use crate::rect::IRect;
-use crate::surface_utils::{
-    iterators::Pixels,
-    shared_surface::SharedImageSurface,
-    shared_surface::SurfaceType,
-    ImageSurfaceDataExt,
-};
-use crate::unit_interval::UnitInterval;
+use crate::surface_utils::{shared_surface::SharedImageSurface, shared_surface::SurfaceType};
 
 coord_units!(MaskUnits, CoordUnits::ObjectBoundingBox);
 coord_units!(MaskContentUnits, CoordUnits::UserSpaceOnUse);
@@ -136,48 +129,14 @@ impl NodeMask {
 
         let Opacity(opacity) = values.opacity;
 
-        let mask_content_surface =
-            SharedImageSurface::new(mask_content_surface, SurfaceType::SRgb)?;
+        let mask = SharedImageSurface::new(mask_content_surface, SurfaceType::SRgb)?
+            .to_mask(u8::from(opacity))?
+            .into_image_surface()?;
 
-        Ok(Some(compute_luminance_to_alpha(&mask_content_surface, opacity)?))
+        Ok(Some(mask))
     }
 }
 
-// Returns a surface whose alpha channel for each pixel is equal to the
-// luminance of that pixel's unpremultiplied RGB values.  The resulting
-// surface's RGB values are not meanignful; only the alpha channel has
-// useful luminance data.
-//
-// This is to get a mask suitable for use with cairo_mask_surface().
-fn compute_luminance_to_alpha(
-    surface: &SharedImageSurface,
-    opacity: UnitInterval,
-) -> Result<cairo::ImageSurface, cairo::Status> {
-    let width = surface.width();
-    let height = surface.height();
-
-    let bounds = IRect {
-        x0: 0,
-        y0: 0,
-        x1: width,
-        y1: height,
-    };
-
-    let opacity = u8::from(opacity);
-    let mut output = cairo::ImageSurface::create(cairo::Format::ARgb32, width, height)?;
-    let output_stride = output.get_stride() as usize;
-
-    {
-        let mut output_data = output.get_data().unwrap();
-
-        for (x, y, pixel) in Pixels::new(surface, bounds) {
-            output_data.set_pixel(output_stride, pixel.to_mask(opacity), x, y);
-        }
-    }
-
-    Ok(output)
-}
-
 impl NodeTrait for NodeMask {
     fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
         for (attr, value) in pbag.iter() {
diff --git a/rsvg_internals/src/surface_utils/shared_surface.rs 
b/rsvg_internals/src/surface_utils/shared_surface.rs
index 67782d1c..b0be7354 100644
--- a/rsvg_internals/src/surface_utils/shared_surface.rs
+++ b/rsvg_internals/src/surface_utils/shared_surface.rs
@@ -399,6 +399,35 @@ impl SharedImageSurface {
         SharedImageSurface::new(output_surface, SurfaceType::AlphaOnly)
     }
 
+    /// Returns a surface whose alpha channel for each pixel is equal to the
+    /// luminance of that pixel's unpremultiplied RGB values.  The resulting
+    /// surface's RGB values are not meanignful; only the alpha channel has
+    /// useful luminance data.
+    ///
+    /// This is to get a mask suitable for use with cairo_mask_surface().
+    pub fn to_mask(&self, opacity: u8) -> Result<SharedImageSurface, cairo::Status> {
+        let bounds = IRect {
+            x0: 0,
+            y0: 0,
+            x1: self.width,
+            y1: self.height,
+        };
+
+        let mut output_surface =
+            ImageSurface::create(cairo::Format::ARgb32, self.width, self.height)?;
+
+        let stride = output_surface.get_stride() as usize;
+        {
+            let mut data = output_surface.get_data().unwrap();
+
+            for (x, y, pixel) in Pixels::new(self, bounds) {
+                data.set_pixel(stride, pixel.to_mask(opacity), x, y);
+            }
+        }
+
+        SharedImageSurface::new(output_surface, self.surface_type)
+    }
+
     /// Returns a surface with pre-multiplication of color values undone.
     ///
     /// HACK: this is storing unpremultiplied pixels in an ARGB32 image surface (which is supposed


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]