[librsvg] surface_utils: clarify iterator constructors



commit 93cbf4bab3593056ed424b66ae72f4a55af621c3
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Jan 9 21:04:43 2020 +0100

    surface_utils: clarify iterator constructors
    
    Have two iterator constructors: Pixels::new and Pixels::within, where
    the latter is the one that takes the bounds as a parameter.

 librsvg/pixbuf_utils.rs                            |  2 +-
 librsvg_crate/tests/utils/compare_surfaces.rs      | 11 +-----
 rsvg_internals/benches/lighting.rs                 |  4 +--
 rsvg_internals/benches/pixel_iterators.rs          |  2 +-
 rsvg_internals/src/filters/color_matrix.rs         |  2 +-
 rsvg_internals/src/filters/component_transfer.rs   |  2 +-
 rsvg_internals/src/filters/convolve_matrix.rs      |  4 +--
 rsvg_internals/src/filters/displacement_map.rs     |  2 +-
 rsvg_internals/src/filters/morphology.rs           |  4 +--
 rsvg_internals/src/surface_utils/iterators.rs      | 41 ++++++++++++++--------
 rsvg_internals/src/surface_utils/shared_surface.rs | 20 ++++++-----
 rsvg_internals/src/surface_utils/srgb.rs           |  2 +-
 12 files changed, 50 insertions(+), 46 deletions(-)
---
diff --git a/librsvg/pixbuf_utils.rs b/librsvg/pixbuf_utils.rs
index a2fae219..72f09718 100644
--- a/librsvg/pixbuf_utils.rs
+++ b/librsvg/pixbuf_utils.rs
@@ -36,7 +36,7 @@ pub fn pixbuf_from_surface(surface: &SharedImageSurface) -> Result<Pixbuf, Rende
     let pixbuf = pixbuf_new(width as i32, height as i32)?;
     let bounds = IRect::from_size(width, height);
 
-    for (x, y, pixel) in Pixels::new(&surface, bounds) {
+    for (x, y, pixel) in Pixels::within(&surface, bounds) {
         let (r, g, b, a) = if pixel.a == 0 {
             (0, 0, 0, 0)
         } else {
diff --git a/librsvg_crate/tests/utils/compare_surfaces.rs b/librsvg_crate/tests/utils/compare_surfaces.rs
index b75c4b0e..f5b5ada8 100644
--- a/librsvg_crate/tests/utils/compare_surfaces.rs
+++ b/librsvg_crate/tests/utils/compare_surfaces.rs
@@ -81,22 +81,13 @@ pub fn compare_surfaces(
     let mut surf_diff = ImageSurface::create(cairo::Format::ARgb32, a_width, a_height)?;
     let diff_stride = surf_diff.get_stride() as usize;
 
-    let bounds = IRect {
-        x0: 0,
-        y0: 0,
-        x1: a_width,
-        y1: a_height,
-    };
-
     let mut num_pixels_changed = 0;
     let mut max_diff = 0;
 
     {
         let mut diff_data = surf_diff.get_data().unwrap();
 
-        for ((xa, ya, pixel_a), (_, _, pixel_b)) in
-            Pixels::new(surf_a, bounds).zip(Pixels::new(surf_b, bounds))
-        {
+        for ((xa, ya, pixel_a), (_, _, pixel_b)) in Pixels::new(surf_a).zip(Pixels::new(surf_b)) {
             if pixel_a != pixel_b {
                 num_pixels_changed += 1;
 
diff --git a/rsvg_internals/benches/lighting.rs b/rsvg_internals/benches/lighting.rs
index 8a485c2a..bfeea330 100644
--- a/rsvg_internals/benches/lighting.rs
+++ b/rsvg_internals/benches/lighting.rs
@@ -158,7 +158,7 @@ fn normal(surface: &SharedImageSurface, bounds: IRect, x: u32, y: u32) -> Normal
 
     let mut nx = 0;
     let mut ny = 0;
-    for (x, y, pixel) in PixelRectangle::new(surface, bounds, kernel_bounds, EdgeMode::None) {
+    for (x, y, pixel) in PixelRectangle::within(surface, bounds, kernel_bounds, EdgeMode::None) {
         let kernel_x = (x - kernel_bounds.x0) as usize;
         let kernel_y = (y - kernel_bounds.y0) as usize;
 
@@ -189,7 +189,7 @@ fn bench_normal(c: &mut Criterion) {
 
         b.iter(|| {
             let mut z = 0;
-            for (x, y, _pixel) in Pixels::new(&input_surface, BOUNDS) {
+            for (x, y, _pixel) in Pixels::within(&input_surface, BOUNDS) {
                 let n = normal(&input_surface, BOUNDS, x, y);
                 z += n.normal.x;
             }
diff --git a/rsvg_internals/benches/pixel_iterators.rs b/rsvg_internals/benches/pixel_iterators.rs
index 729e35b5..a85c69a0 100644
--- a/rsvg_internals/benches/pixel_iterators.rs
+++ b/rsvg_internals/benches/pixel_iterators.rs
@@ -87,7 +87,7 @@ fn bench_pixel_iterators(c: &mut Criterion) {
             let mut b = 0usize;
             let mut a = 0usize;
 
-            for (_x, _y, pixel) in Pixels::new(&data, bounds) {
+            for (_x, _y, pixel) in Pixels::within(&data, bounds) {
                 r += pixel.r as usize;
                 g += pixel.g as usize;
                 b += pixel.b as usize;
diff --git a/rsvg_internals/src/filters/color_matrix.rs b/rsvg_internals/src/filters/color_matrix.rs
index 1b66882b..42cec296 100644
--- a/rsvg_internals/src/filters/color_matrix.rs
+++ b/rsvg_internals/src/filters/color_matrix.rs
@@ -176,7 +176,7 @@ impl FilterEffect for FeColorMatrix {
         {
             let mut output_data = output_surface.get_data().unwrap();
 
-            for (x, y, pixel) in Pixels::new(input.surface(), bounds) {
+            for (x, y, pixel) in Pixels::within(input.surface(), bounds) {
                 let alpha = f64::from(pixel.a) / 255f64;
 
                 let pixel_vec = if alpha == 0.0 {
diff --git a/rsvg_internals/src/filters/component_transfer.rs 
b/rsvg_internals/src/filters/component_transfer.rs
index f75923d9..c555fda2 100644
--- a/rsvg_internals/src/filters/component_transfer.rs
+++ b/rsvg_internals/src/filters/component_transfer.rs
@@ -363,7 +363,7 @@ impl FilterEffect for FeComponentTransfer {
         {
             let mut output_data = output_surface.get_data().unwrap();
 
-            for (x, y, pixel) in Pixels::new(input.surface(), bounds) {
+            for (x, y, pixel) in Pixels::within(input.surface(), bounds) {
                 let alpha = f64::from(pixel.a) / 255f64;
                 let new_alpha = compute_a(alpha);
 
diff --git a/rsvg_internals/src/filters/convolve_matrix.rs b/rsvg_internals/src/filters/convolve_matrix.rs
index 785e5352..870e6f66 100644
--- a/rsvg_internals/src/filters/convolve_matrix.rs
+++ b/rsvg_internals/src/filters/convolve_matrix.rs
@@ -239,7 +239,7 @@ impl FilterEffect for FeConvolveMatrix {
         {
             let mut output_data = output_surface.get_data().unwrap();
 
-            for (x, y, pixel) in Pixels::new(&input_surface, bounds) {
+            for (x, y, pixel) in Pixels::within(&input_surface, bounds) {
                 // Compute the convolution rectangle bounds.
                 let kernel_bounds = IRect::new(
                     x as i32 - self.target_x.unwrap() as i32,
@@ -255,7 +255,7 @@ impl FilterEffect for FeConvolveMatrix {
                 let mut a = 0.0;
 
                 for (x, y, pixel) in
-                    PixelRectangle::new(&input_surface, bounds, kernel_bounds, self.edge_mode)
+                    PixelRectangle::within(&input_surface, bounds, kernel_bounds, self.edge_mode)
                 {
                     let kernel_x = (kernel_bounds.x1 - x - 1) as usize;
                     let kernel_y = (kernel_bounds.y1 - y - 1) as usize;
diff --git a/rsvg_internals/src/filters/displacement_map.rs b/rsvg_internals/src/filters/displacement_map.rs
index 13d206b1..c61c0eb2 100644
--- a/rsvg_internals/src/filters/displacement_map.rs
+++ b/rsvg_internals/src/filters/displacement_map.rs
@@ -99,7 +99,7 @@ impl FilterEffect for FeDisplacementMap {
         {
             let cr = cairo::Context::new(&output_surface);
 
-            for (x, y, displacement_pixel) in Pixels::new(&displacement_surface, bounds) {
+            for (x, y, displacement_pixel) in Pixels::within(&displacement_surface, bounds) {
                 let get_value = |channel| match channel {
                     ColorChannel::R => displacement_pixel.r,
                     ColorChannel::G => displacement_pixel.g,
diff --git a/rsvg_internals/src/filters/morphology.rs b/rsvg_internals/src/filters/morphology.rs
index dc26e8f0..b602f9d5 100644
--- a/rsvg_internals/src/filters/morphology.rs
+++ b/rsvg_internals/src/filters/morphology.rs
@@ -102,7 +102,7 @@ impl FilterEffect for FeMorphology {
         {
             let mut output_data = output_surface.get_data().unwrap();
 
-            for (x, y, _pixel) in Pixels::new(input.surface(), bounds) {
+            for (x, y, _pixel) in Pixels::within(input.surface(), bounds) {
                 // Compute the kernel rectangle bounds.
                 let kernel_bounds = IRect::new(
                     (f64::from(x) - rx).floor() as i32,
@@ -125,7 +125,7 @@ impl FilterEffect for FeMorphology {
                 };
 
                 for (_x, _y, pixel) in
-                    PixelRectangle::new(&input.surface(), bounds, kernel_bounds, EdgeMode::None)
+                    PixelRectangle::within(&input.surface(), bounds, kernel_bounds, EdgeMode::None)
                 {
                     let op = match self.operator {
                         Operator::Erode => min,
diff --git a/rsvg_internals/src/surface_utils/iterators.rs b/rsvg_internals/src/surface_utils/iterators.rs
index 9cecae10..7899e129 100644
--- a/rsvg_internals/src/surface_utils/iterators.rs
+++ b/rsvg_internals/src/surface_utils/iterators.rs
@@ -27,9 +27,17 @@ pub struct PixelRectangle<'a> {
 }
 
 impl<'a> Pixels<'a> {
+    /// Creates an iterator over the image surface pixels
+    #[inline]
+    pub fn new(surface: &'a SharedImageSurface) -> Self {
+        let bounds = IRect::from_size(surface.width(), surface.height());
+
+        Self::within(surface, bounds)
+    }
+
     /// Creates an iterator over the image surface pixels, constrained within the given bounds.
     #[inline]
-    pub fn new(surface: &'a SharedImageSurface, bounds: IRect) -> Self {
+    pub fn within(surface: &'a SharedImageSurface, bounds: IRect) -> Self {
         // Sanity checks.
         assert!(bounds.x0 >= 0);
         assert!(bounds.x0 <= surface.width());
@@ -51,9 +59,17 @@ impl<'a> Pixels<'a> {
 }
 
 impl<'a> PixelRectangle<'a> {
+    /// Creates an iterator over the image surface pixels
+    #[inline]
+    pub fn new(surface: &'a SharedImageSurface, rectangle: IRect, edge_mode: EdgeMode) -> Self {
+        let bounds = IRect::from_size(surface.width(), surface.height());
+
+        Self::within(surface, bounds, rectangle, edge_mode)
+    }
+
     /// Creates an iterator over the image surface pixels, constrained within the given bounds.
     #[inline]
-    pub fn new(
+    pub fn within(
         surface: &'a SharedImageSurface,
         bounds: IRect,
         rectangle: IRect,
@@ -192,35 +208,31 @@ mod tests {
         .unwrap();
 
         // Full image.
-        let bounds = IRect::from_size(WIDTH, HEIGHT);
-        assert_eq!(
-            Pixels::new(&surface, bounds).count(),
-            (WIDTH * HEIGHT) as usize
-        );
+        assert_eq!(Pixels::new(&surface).count(), (WIDTH * HEIGHT) as usize);
 
         // 1-wide column.
         let bounds = IRect::from_size(1, HEIGHT);
-        assert_eq!(Pixels::new(&surface, bounds).count(), HEIGHT as usize);
+        assert_eq!(Pixels::within(&surface, bounds).count(), HEIGHT as usize);
 
         // 1-tall row.
         let bounds = IRect::from_size(WIDTH, 1);
-        assert_eq!(Pixels::new(&surface, bounds).count(), WIDTH as usize);
+        assert_eq!(Pixels::within(&surface, bounds).count(), WIDTH as usize);
 
         // 1×1.
         let bounds = IRect::from_size(1, 1);
-        assert_eq!(Pixels::new(&surface, bounds).count(), 1);
+        assert_eq!(Pixels::within(&surface, bounds).count(), 1);
 
         // Nothing (x0 == x1).
         let bounds = IRect::from_size(0, HEIGHT);
-        assert_eq!(Pixels::new(&surface, bounds).count(), 0);
+        assert_eq!(Pixels::within(&surface, bounds).count(), 0);
 
         // Nothing (y0 == y1).
         let bounds = IRect::from_size(WIDTH, 0);
-        assert_eq!(Pixels::new(&surface, bounds).count(), 0);
+        assert_eq!(Pixels::within(&surface, bounds).count(), 0);
 
         // Nothing (x0 == x1, y0 == y1).
         let bounds = IRect::new(0, 0, 0, 0);
-        assert_eq!(Pixels::new(&surface, bounds).count(), 0);
+        assert_eq!(Pixels::within(&surface, bounds).count(), 0);
     }
 
     #[test]
@@ -234,10 +246,9 @@ mod tests {
         )
         .unwrap();
 
-        let bounds = IRect::from_size(WIDTH, HEIGHT);
         let rect_bounds = IRect::new(-8, -8, 8, 8);
         assert_eq!(
-            PixelRectangle::new(&surface, bounds, rect_bounds, EdgeMode::None).count(),
+            PixelRectangle::new(&surface, rect_bounds, EdgeMode::None).count(),
             (16 * 16) as usize
         );
     }
diff --git a/rsvg_internals/src/surface_utils/shared_surface.rs 
b/rsvg_internals/src/surface_utils/shared_surface.rs
index 6332764f..6e978900 100644
--- a/rsvg_internals/src/surface_utils/shared_surface.rs
+++ b/rsvg_internals/src/surface_utils/shared_surface.rs
@@ -394,7 +394,7 @@ impl SharedImageSurface {
         {
             let mut output_data = output_surface.get_data().unwrap();
 
-            for (x, y, Pixel { a, .. }) in Pixels::new(self, bounds) {
+            for (x, y, Pixel { a, .. }) in Pixels::within(self, bounds) {
                 let output_pixel = Pixel {
                     r: 0,
                     g: 0,
@@ -425,7 +425,7 @@ impl SharedImageSurface {
             let mut data = output_surface.get_data().unwrap();
             let opacity = u8::from(opacity);
 
-            for (x, y, pixel) in Pixels::new(self, bounds) {
+            for (x, y, pixel) in Pixels::within(self, bounds) {
                 data.set_pixel(stride, pixel.to_mask(opacity), x, y);
             }
         }
@@ -450,7 +450,7 @@ impl SharedImageSurface {
         {
             let mut data = output_surface.get_data().unwrap();
 
-            for (x, y, pixel) in Pixels::new(self, bounds) {
+            for (x, y, pixel) in Pixels::within(self, bounds) {
                 data.set_pixel(stride, pixel.unpremultiply(), x, y);
             }
         }
@@ -508,7 +508,7 @@ impl SharedImageSurface {
             let mut output_data = output_surface.get_data().unwrap();
 
             if self.is_alpha_only() {
-                for (x, y, _pixel) in Pixels::new(self, bounds) {
+                for (x, y, _pixel) in Pixels::within(self, bounds) {
                     let kernel_bounds = IRect::new(
                         x as i32 - target.0,
                         y as i32 - target.1,
@@ -518,7 +518,8 @@ impl SharedImageSurface {
 
                     let mut a = 0.0;
 
-                    for (x, y, pixel) in PixelRectangle::new(self, bounds, kernel_bounds, edge_mode)
+                    for (x, y, pixel) in
+                        PixelRectangle::within(self, bounds, kernel_bounds, edge_mode)
                     {
                         let kernel_x = (kernel_bounds.x1 - x - 1) as usize;
                         let kernel_y = (kernel_bounds.y1 - y - 1) as usize;
@@ -539,7 +540,7 @@ impl SharedImageSurface {
                     output_data.set_pixel(output_stride, output_pixel, x, y);
                 }
             } else {
-                for (x, y, _pixel) in Pixels::new(self, bounds) {
+                for (x, y, _pixel) in Pixels::within(self, bounds) {
                     let kernel_bounds = IRect::new(
                         x as i32 - target.0,
                         y as i32 - target.1,
@@ -552,7 +553,8 @@ impl SharedImageSurface {
                     let mut b = 0.0;
                     let mut a = 0.0;
 
-                    for (x, y, pixel) in PixelRectangle::new(self, bounds, kernel_bounds, edge_mode)
+                    for (x, y, pixel) in
+                        PixelRectangle::within(self, bounds, kernel_bounds, edge_mode)
                     {
                         let kernel_x = (kernel_bounds.x1 - x - 1) as usize;
                         let kernel_y = (kernel_bounds.y1 - y - 1) as usize;
@@ -1135,7 +1137,7 @@ pub fn composite_arithmetic(
         let mut output_data = output_surface.get_data().unwrap();
 
         for (x, y, pixel, pixel_2) in
-            Pixels::new(surface1, bounds).map(|(x, y, p)| (x, y, p, surface2.get_pixel(x, y)))
+            Pixels::within(surface1, bounds).map(|(x, y, p)| (x, y, p, surface2.get_pixel(x, y)))
         {
             let i1a = f64::from(pixel.a) / 255f64;
             let i2a = f64::from(pixel_2.a) / 255f64;
@@ -1198,7 +1200,7 @@ mod tests {
         let alpha = surface.extract_alpha(bounds).unwrap();
 
         for (x, y, p, pa) in
-            Pixels::new(&surface, full_bounds).map(|(x, y, p)| (x, y, p, alpha.get_pixel(x, y)))
+            Pixels::within(&surface, full_bounds).map(|(x, y, p)| (x, y, p, alpha.get_pixel(x, y)))
         {
             assert_eq!(pa.r, 0);
             assert_eq!(pa.g, 0);
diff --git a/rsvg_internals/src/surface_utils/srgb.rs b/rsvg_internals/src/surface_utils/srgb.rs
index 6c96d329..d637abb4 100644
--- a/rsvg_internals/src/surface_utils/srgb.rs
+++ b/rsvg_internals/src/surface_utils/srgb.rs
@@ -36,7 +36,7 @@ pub fn map_unpremultiplied_components_loop<F: Fn(u8) -> u8>(
     {
         let mut output_data = output_surface.get_data().unwrap();
 
-        for (x, y, pixel) in Pixels::new(surface, bounds) {
+        for (x, y, pixel) in Pixels::within(surface, bounds) {
             if pixel.a > 0 {
                 let alpha = f64::from(pixel.a) / 255f64;
 


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