[librsvg] Add IRect::width() and height()



commit ec2bbf25d1924e43323de22ecd15ac3c8edc4e8f
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Nov 26 18:29:06 2019 +0100

    Add IRect::width() and height()
    
    Now euclid has this methods, so it is useful to have them in
    preparation.

 rsvg_internals/src/filters/context.rs         | 21 ++++++---------------
 rsvg_internals/src/filters/light/lighting.rs  | 13 ++++---------
 rsvg_internals/src/filters/light/mod.rs       | 24 ++++++++++++------------
 rsvg_internals/src/filters/tile.rs            |  4 ++--
 rsvg_internals/src/filters/turbulence.rs      |  4 ++--
 rsvg_internals/src/rect.rs                    | 10 ++++++++++
 rsvg_internals/src/surface_utils/iterators.rs |  9 +++------
 7 files changed, 39 insertions(+), 46 deletions(-)
---
diff --git a/rsvg_internals/src/filters/context.rs b/rsvg_internals/src/filters/context.rs
index 85055722..1dd8a886 100644
--- a/rsvg_internals/src/filters/context.rs
+++ b/rsvg_internals/src/filters/context.rs
@@ -481,18 +481,9 @@ mod tests {
     fn test_extract_alpha() {
         const WIDTH: i32 = 32;
         const HEIGHT: i32 = 64;
-        const BOUNDS: IRect = IRect {
-            x0: 8,
-            x1: 24,
-            y0: 16,
-            y1: 48,
-        };
-        const FULL_BOUNDS: IRect = IRect {
-            x0: 0,
-            x1: WIDTH,
-            y0: 0,
-            y1: HEIGHT,
-        };
+
+        let bounds = IRect::new(8, 24, 16, 48);
+        let full_bounds = IRect::from_size(WIDTH, HEIGHT);
 
         let mut surface =
             cairo::ImageSurface::create(cairo::Format::ARgb32, WIDTH, HEIGHT).unwrap();
@@ -509,16 +500,16 @@ mod tests {
         }
 
         let surface = SharedImageSurface::new(surface, SurfaceType::SRgb).unwrap();
-        let alpha = surface.extract_alpha(BOUNDS).unwrap();
+        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::new(&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);
             assert_eq!(pa.b, 0);
 
-            if !BOUNDS.contains(x as i32, y as i32) {
+            if !bounds.contains(x as i32, y as i32) {
                 assert_eq!(pa.a, 0);
             } else {
                 assert_eq!(pa.a, p.a);
diff --git a/rsvg_internals/src/filters/light/lighting.rs b/rsvg_internals/src/filters/light/lighting.rs
index 132c732a..3e31a931 100644
--- a/rsvg_internals/src/filters/light/lighting.rs
+++ b/rsvg_internals/src/filters/light/lighting.rs
@@ -313,7 +313,7 @@ macro_rules! impl_lighting_filter {
 
                 // Check if the surface is too small for normal computation. This case is unspecified;
                 // WebKit doesn't render anything in this case.
-                if bounds.x1 < bounds.x0 + 2 || bounds.y1 < bounds.y0 + 2 {
+                if bounds.width() < 2 || bounds.height() < 2 {
                     return Err(FilterError::LightingInputTooSmall);
                 }
 
@@ -349,12 +349,7 @@ macro_rules! impl_lighting_filter {
                             let b = compute(light_color.blue);
                             let a = $alpha_func(r, g, b);
 
-                            let output_pixel = Pixel {
-                                r,
-                                g,
-                                b,
-                                a,
-                            };
+                            let output_pixel = Pixel { r, g, b, a };
 
                             output_slice.set_pixel(output_stride, output_pixel, x, y - base_y);
                         };
@@ -443,7 +438,7 @@ macro_rules! impl_lighting_filter {
                         }
                     }
 
-                    if bounds.x1 - bounds.x0 >= 3 && bounds.y1 - bounds.y0 >= 3 {
+                    if bounds.width() >= 3 && bounds.height() >= 3 {
                         // Interior pixels.
                         let first_row = bounds.y0 as u32 + 1;
                         let one_past_last_row = bounds.y1 as u32 - 1;
@@ -506,7 +501,7 @@ macro_rules! impl_lighting_filter {
                 true
             }
         }
-    }
+    };
 }
 
 const fn diffuse_alpha(_r: u8, _g: u8, _b: u8) -> u8 {
diff --git a/rsvg_internals/src/filters/light/mod.rs b/rsvg_internals/src/filters/light/mod.rs
index e8df5bec..c80cd7c8 100644
--- a/rsvg_internals/src/filters/light/mod.rs
+++ b/rsvg_internals/src/filters/light/mod.rs
@@ -33,8 +33,8 @@ fn return_normal(factor_x: f64, nx: i16, factor_y: f64, ny: i16) -> Normal {
 #[inline]
 pub fn top_left_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal {
     // Surface needs to be at least 2×2.
-    assert!(bounds.x1 >= bounds.x0 + 2);
-    assert!(bounds.y1 >= bounds.y0 + 2);
+    assert!(bounds.width() >= 2);
+    assert!(bounds.height() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let x = bounds.x0 as u32;
@@ -58,7 +58,7 @@ pub fn top_left_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal {
 pub fn top_row_normal(surface: &SharedImageSurface, bounds: IRect, x: u32) -> Normal {
     assert!(x as i32 > bounds.x0);
     assert!((x as i32) + 1 < bounds.x1);
-    assert!(bounds.y1 >= bounds.y0 + 2);
+    assert!(bounds.height() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let y = bounds.y0 as u32;
@@ -82,8 +82,8 @@ pub fn top_row_normal(surface: &SharedImageSurface, bounds: IRect, x: u32) -> No
 #[inline]
 pub fn top_right_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal {
     // Surface needs to be at least 2×2.
-    assert!(bounds.x1 >= bounds.x0 + 2);
-    assert!(bounds.y1 >= bounds.y0 + 2);
+    assert!(bounds.width() >= 2);
+    assert!(bounds.height() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let x = bounds.x1 as u32 - 1;
@@ -107,7 +107,7 @@ pub fn top_right_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal {
 pub fn left_column_normal(surface: &SharedImageSurface, bounds: IRect, y: u32) -> Normal {
     assert!(y as i32 > bounds.y0);
     assert!((y as i32) + 1 < bounds.y1);
-    assert!(bounds.x1 >= bounds.x0 + 2);
+    assert!(bounds.width() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let x = bounds.x0 as u32;
@@ -159,7 +159,7 @@ pub fn interior_normal(surface: &SharedImageSurface, bounds: IRect, x: u32, y: u
 pub fn right_column_normal(surface: &SharedImageSurface, bounds: IRect, y: u32) -> Normal {
     assert!(y as i32 > bounds.y0);
     assert!((y as i32) + 1 < bounds.y1);
-    assert!(bounds.x1 >= bounds.x0 + 2);
+    assert!(bounds.width() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let x = bounds.x1 as u32 - 1;
@@ -183,8 +183,8 @@ pub fn right_column_normal(surface: &SharedImageSurface, bounds: IRect, y: u32)
 #[inline]
 pub fn bottom_left_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal {
     // Surface needs to be at least 2×2.
-    assert!(bounds.x1 >= bounds.x0 + 2);
-    assert!(bounds.y1 >= bounds.y0 + 2);
+    assert!(bounds.width() >= 2);
+    assert!(bounds.height() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let x = bounds.x0 as u32;
@@ -208,7 +208,7 @@ pub fn bottom_left_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal
 pub fn bottom_row_normal(surface: &SharedImageSurface, bounds: IRect, x: u32) -> Normal {
     assert!(x as i32 > bounds.x0);
     assert!((x as i32) + 1 < bounds.x1);
-    assert!(bounds.y1 >= bounds.y0 + 2);
+    assert!(bounds.height() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let y = bounds.y1 as u32 - 1;
@@ -232,8 +232,8 @@ pub fn bottom_row_normal(surface: &SharedImageSurface, bounds: IRect, x: u32) ->
 #[inline]
 pub fn bottom_right_normal(surface: &SharedImageSurface, bounds: IRect) -> Normal {
     // Surface needs to be at least 2×2.
-    assert!(bounds.x1 >= bounds.x0 + 2);
-    assert!(bounds.y1 >= bounds.y0 + 2);
+    assert!(bounds.width() >= 2);
+    assert!(bounds.height() >= 2);
 
     let get = |x, y| i16::from(surface.get_pixel(x, y).a);
     let x = bounds.x1 as u32 - 1;
diff --git a/rsvg_internals/src/filters/tile.rs b/rsvg_internals/src/filters/tile.rs
index 90d6ffdf..41af64a4 100644
--- a/rsvg_internals/src/filters/tile.rs
+++ b/rsvg_internals/src/filters/tile.rs
@@ -52,8 +52,8 @@ impl FilterEffect for FeTile {
                 // Create a surface containing just the region to tile.
                 let bounded_input_surface = ImageSurface::create(
                     cairo::Format::ARgb32,
-                    input_bounds.x1 - input_bounds.x0,
-                    input_bounds.y1 - input_bounds.y0,
+                    input_bounds.width(),
+                    input_bounds.height(),
                 )?;
 
                 {
diff --git a/rsvg_internals/src/filters/turbulence.rs b/rsvg_internals/src/filters/turbulence.rs
index 2c39ee2a..874c57bc 100644
--- a/rsvg_internals/src/filters/turbulence.rs
+++ b/rsvg_internals/src/filters/turbulence.rs
@@ -352,8 +352,8 @@ impl FilterEffect for FeTurbulence {
             self.num_octaves,
             self.type_,
             self.stitch_tiles,
-            f64::from(bounds.x1 - bounds.x0),
-            f64::from(bounds.y1 - bounds.y0),
+            f64::from(bounds.width()),
+            f64::from(bounds.height()),
         );
 
         let mut output_surface = ImageSurface::create(
diff --git a/rsvg_internals/src/rect.rs b/rsvg_internals/src/rect.rs
index 31e187a8..8fa400c9 100644
--- a/rsvg_internals/src/rect.rs
+++ b/rsvg_internals/src/rect.rs
@@ -158,6 +158,16 @@ impl IRect {
         }
     }
 
+    #[inline]
+    pub fn width(&self) -> i32 {
+        self.x1 - self.x0
+    }
+
+    #[inline]
+    pub fn height(&self) -> i32 {
+        self.y1 - self.y0
+    }
+
     #[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 d817d396..373fe1cf 100644
--- a/rsvg_internals/src/surface_utils/iterators.rs
+++ b/rsvg_internals/src/surface_utils/iterators.rs
@@ -108,8 +108,7 @@ impl<'a> Iterator for Pixels<'a> {
         if self.x + 1 == self.bounds.x1 as u32 {
             self.x = self.bounds.x0 as u32;
             self.y += 1;
-            self.offset +=
-                self.surface.stride() - (self.bounds.x1 - self.bounds.x0 - 1) as isize * 4;
+            self.offset += self.surface.stride() - (self.bounds.width() - 1) as isize * 4;
         } else {
             self.x += 1;
             self.offset += 4;
@@ -152,10 +151,8 @@ impl<'a> Iterator for PixelRectangle<'a> {
                                 x % v
                             };
 
-                            let x = self.bounds.x0
-                                + wrap(x - self.bounds.x0, self.bounds.x1 - self.bounds.x0);
-                            let y = self.bounds.y0
-                                + wrap(y - self.bounds.y0, self.bounds.y1 - self.bounds.y0);
+                            let x = self.bounds.x0 + wrap(x - self.bounds.x0, self.bounds.width());
+                            let y = self.bounds.y0 + wrap(y - self.bounds.y0, self.bounds.height());
                             self.surface.get_pixel(x as u32, y as u32)
                         }
                     }


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