[librsvg: 5/10] rect: add constructrors for IRect



commit db5a6c6cf92e2d98f3bebbbba3472cfe71fb7596
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Nov 24 15:20:02 2019 +0100

    rect: add constructrors for IRect

 librsvg/pixbuf_utils.rs                            |  8 +--
 rsvg_internals/benches/lighting.rs                 |  7 +--
 rsvg_internals/src/filters/convolve_matrix.rs      | 12 ++--
 rsvg_internals/src/filters/morphology.rs           | 12 ++--
 rsvg_internals/src/filters/offset.rs               | 12 ++--
 rsvg_internals/src/rect.rs                         | 15 +++++
 rsvg_internals/src/surface_utils/iterators.rs      | 64 +++-------------------
 rsvg_internals/src/surface_utils/shared_surface.rs | 40 +++++---------
 8 files changed, 58 insertions(+), 112 deletions(-)
---
diff --git a/librsvg/pixbuf_utils.rs b/librsvg/pixbuf_utils.rs
index e34bbed7..fa3b8ede 100644
--- a/librsvg/pixbuf_utils.rs
+++ b/librsvg/pixbuf_utils.rs
@@ -37,13 +37,7 @@ pub fn pixbuf_from_surface(surface: &SharedImageSurface) -> Result<Pixbuf, Rende
     let height = surface.height();
 
     let pixbuf = pixbuf_new(width as i32, height as i32)?;
-
-    let bounds = IRect {
-        x0: 0,
-        y0: 0,
-        x1: width,
-        y1: height,
-    };
+    let bounds = IRect::from_size(width, height);
 
     for (x, y, pixel) in Pixels::new(&surface, bounds) {
         let (r, g, b, a) = if pixel.a == 0 {
diff --git a/rsvg_internals/benches/lighting.rs b/rsvg_internals/benches/lighting.rs
index 9a89e1b5..df353a71 100644
--- a/rsvg_internals/benches/lighting.rs
+++ b/rsvg_internals/benches/lighting.rs
@@ -166,12 +166,7 @@ fn normal(surface: &SharedImageSurface, bounds: IRect, x: u32, y: u32) -> Normal
         ),
     };
 
-    let kernel_bounds = IRect {
-        x0: x as i32 - 1,
-        y0: y as i32 - 1,
-        x1: x as i32 + 2,
-        y1: y as i32 + 2,
-    };
+    let kernel_bounds = IRect::new(x as i32 - 1, y as i32 - 1, x as i32 + 2, y as i32 + 2);
 
     let mut nx = 0;
     let mut ny = 0;
diff --git a/rsvg_internals/src/filters/convolve_matrix.rs b/rsvg_internals/src/filters/convolve_matrix.rs
index e6c0ab6a..320ece76 100644
--- a/rsvg_internals/src/filters/convolve_matrix.rs
+++ b/rsvg_internals/src/filters/convolve_matrix.rs
@@ -272,12 +272,12 @@ impl FilterEffect for FeConvolveMatrix {
 
             for (x, y, pixel) in Pixels::new(&input_surface, bounds) {
                 // Compute the convolution rectangle bounds.
-                let kernel_bounds = IRect {
-                    x0: x as i32 - self.target_x.unwrap() as i32,
-                    y0: y as i32 - self.target_y.unwrap() as i32,
-                    x1: x as i32 - self.target_x.unwrap() as i32 + self.order.0 as i32,
-                    y1: y as i32 - self.target_y.unwrap() as i32 + self.order.1 as i32,
-                };
+                let kernel_bounds = IRect::new(
+                    x as i32 - self.target_x.unwrap() as i32,
+                    y as i32 - self.target_y.unwrap() as i32,
+                    x as i32 - self.target_x.unwrap() as i32 + self.order.0 as i32,
+                    y as i32 - self.target_y.unwrap() as i32 + self.order.1 as i32,
+                );
 
                 // Do the convolution.
                 let mut r = 0.0;
diff --git a/rsvg_internals/src/filters/morphology.rs b/rsvg_internals/src/filters/morphology.rs
index f0d7f8be..c986e908 100644
--- a/rsvg_internals/src/filters/morphology.rs
+++ b/rsvg_internals/src/filters/morphology.rs
@@ -107,12 +107,12 @@ impl FilterEffect for FeMorphology {
 
             for (x, y, _pixel) in Pixels::new(input.surface(), bounds) {
                 // Compute the kernel rectangle bounds.
-                let kernel_bounds = IRect {
-                    x0: (f64::from(x) - rx).floor() as i32,
-                    y0: (f64::from(y) - ry).floor() as i32,
-                    x1: (f64::from(x) + rx).ceil() as i32 + 1,
-                    y1: (f64::from(y) + ry).ceil() as i32 + 1,
-                };
+                let kernel_bounds = IRect::new(
+                    (f64::from(x) - rx).floor() as i32,
+                    (f64::from(y) - ry).floor() as i32,
+                    (f64::from(x) + rx).ceil() as i32 + 1,
+                    (f64::from(y) + ry).ceil() as i32 + 1,
+                );
 
                 // Compute the new pixel values.
                 let initial = match self.operator {
diff --git a/rsvg_internals/src/filters/offset.rs b/rsvg_internals/src/filters/offset.rs
index 7d4ddc61..d606a869 100644
--- a/rsvg_internals/src/filters/offset.rs
+++ b/rsvg_internals/src/filters/offset.rs
@@ -68,12 +68,12 @@ impl FilterEffect for FeOffset {
 
         // output_bounds contains all pixels within bounds,
         // for which (x - ox) and (y - oy) also lie within bounds.
-        let output_bounds = IRect {
-            x0: clamp(bounds.x0 + ox as i32, bounds.x0, bounds.x1),
-            y0: clamp(bounds.y0 + oy as i32, bounds.y0, bounds.y1),
-            x1: clamp(bounds.x1 + ox as i32, bounds.x0, bounds.x1),
-            y1: clamp(bounds.y1 + oy as i32, bounds.y0, bounds.y1),
-        };
+        let output_bounds = IRect::new(
+            clamp(bounds.x0 + ox as i32, bounds.x0, bounds.x1),
+            clamp(bounds.y0 + oy as i32, bounds.y0, bounds.y1),
+            clamp(bounds.x1 + ox as i32, bounds.x0, bounds.x1),
+            clamp(bounds.y1 + oy as i32, bounds.y0, bounds.y1),
+        );
 
         let output_surface = ImageSurface::create(
             cairo::Format::ARgb32,
diff --git a/rsvg_internals/src/rect.rs b/rsvg_internals/src/rect.rs
index c8c34971..f8c892a9 100644
--- a/rsvg_internals/src/rect.rs
+++ b/rsvg_internals/src/rect.rs
@@ -138,6 +138,21 @@ pub struct IRect {
 }
 
 impl IRect {
+    #[inline]
+    pub fn new(x0: i32, y0: i32, x1: i32, y1: i32) -> IRect {
+        IRect { x0, y0, x1, y1 }
+    }
+
+    #[inline]
+    pub fn from_size(w: i32, h: i32) -> IRect {
+        IRect {
+            x0: 0,
+            y0: 0,
+            x1: w,
+            y1: h,
+        }
+    }
+
     #[inline]
     pub fn x_range(&self) -> Range<i32> {
         self.x0..self.x1
diff --git a/rsvg_internals/src/surface_utils/iterators.rs b/rsvg_internals/src/surface_utils/iterators.rs
index 72de12de..d817d396 100644
--- a/rsvg_internals/src/surface_utils/iterators.rs
+++ b/rsvg_internals/src/surface_utils/iterators.rs
@@ -196,69 +196,34 @@ mod tests {
         .unwrap();
 
         // Full image.
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: WIDTH,
-            y1: HEIGHT,
-        };
+        let bounds = IRect::from_size(WIDTH, HEIGHT);
         assert_eq!(
             Pixels::new(&surface, bounds).count(),
             (WIDTH * HEIGHT) as usize
         );
 
         // 1-wide column.
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: 1,
-            y1: HEIGHT,
-        };
+        let bounds = IRect::from_size(1, HEIGHT);
         assert_eq!(Pixels::new(&surface, bounds).count(), HEIGHT as usize);
 
         // 1-tall row.
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: WIDTH,
-            y1: 1,
-        };
+        let bounds = IRect::from_size(WIDTH, 1);
         assert_eq!(Pixels::new(&surface, bounds).count(), WIDTH as usize);
 
         // 1×1.
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: 1,
-            y1: 1,
-        };
+        let bounds = IRect::from_size(1, 1);
         assert_eq!(Pixels::new(&surface, bounds).count(), 1);
 
         // Nothing (x0 == x1).
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: 0,
-            y1: HEIGHT,
-        };
+        let bounds = IRect::from_size(0, HEIGHT);
         assert_eq!(Pixels::new(&surface, bounds).count(), 0);
 
         // Nothing (y0 == y1).
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: WIDTH,
-            y1: 0,
-        };
+        let bounds = IRect::from_size(WIDTH, 0);
         assert_eq!(Pixels::new(&surface, bounds).count(), 0);
 
         // Nothing (x0 == x1, y0 == y1).
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: 0,
-            y1: 0,
-        };
+        let bounds = IRect::new(0, 0, 0, 0);
         assert_eq!(Pixels::new(&surface, bounds).count(), 0);
     }
 
@@ -273,19 +238,8 @@ mod tests {
         )
         .unwrap();
 
-        let bounds = IRect {
-            x0: 0,
-            y0: 0,
-            x1: WIDTH,
-            y1: HEIGHT,
-        };
-
-        let rect_bounds = IRect {
-            x0: -8,
-            y0: -8,
-            x1: 8,
-            y1: 8,
-        };
+        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(),
             (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 b0be7354..6ba6f421 100644
--- a/rsvg_internals/src/surface_utils/shared_surface.rs
+++ b/rsvg_internals/src/surface_utils/shared_surface.rs
@@ -160,14 +160,7 @@ impl SharedImageSurface {
             Ok(self.surface)
         } else {
             // If there are any other references, copy the underlying surface.
-            let bounds = IRect {
-                x0: 0,
-                y0: 0,
-                x1: self.width,
-                y1: self.height,
-            };
-
-            self.copy_surface(bounds)
+            self.copy_surface(IRect::from_size(self.width, self.height))
         }
     }
 
@@ -406,12 +399,7 @@ impl SharedImageSurface {
     ///
     /// 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 bounds = IRect::from_size(self.width, self.height);
 
         let mut output_surface =
             ImageSurface::create(cairo::Format::ARgb32, self.width, self.height)?;
@@ -504,12 +492,12 @@ impl SharedImageSurface {
 
             if self.is_alpha_only() {
                 for (x, y, _pixel) in Pixels::new(self, bounds) {
-                    let kernel_bounds = IRect {
-                        x0: x as i32 - target.0,
-                        y0: y as i32 - target.1,
-                        x1: x as i32 - target.0 + kernel.ncols() as i32,
-                        y1: y as i32 - target.1 + kernel.nrows() as i32,
-                    };
+                    let kernel_bounds = IRect::new(
+                        x as i32 - target.0,
+                        y as i32 - target.1,
+                        x as i32 - target.0 + kernel.ncols() as i32,
+                        y as i32 - target.1 + kernel.nrows() as i32,
+                    );
 
                     let mut a = 0.0;
 
@@ -535,12 +523,12 @@ impl SharedImageSurface {
                 }
             } else {
                 for (x, y, _pixel) in Pixels::new(self, bounds) {
-                    let kernel_bounds = IRect {
-                        x0: x as i32 - target.0,
-                        y0: y as i32 - target.1,
-                        x1: x as i32 - target.0 + kernel.ncols() as i32,
-                        y1: y as i32 - target.1 + kernel.nrows() as i32,
-                    };
+                    let kernel_bounds = IRect::new(
+                        x as i32 - target.0,
+                        y as i32 - target.1,
+                        x as i32 - target.0 + kernel.ncols() as i32,
+                        y as i32 - target.1 + kernel.nrows() as i32,
+                    );
 
                     let mut r = 0.0;
                     let mut g = 0.0;


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