[librsvg] srgb: use ExclusiveImageSurface



commit c22856e3a73ae55f028c65a3388f679536e6cb44
Author: Paolo Borelli <pborelli gnome org>
Date:   Wed Jan 15 02:36:14 2020 +0100

    srgb: use ExclusiveImageSurface

 rsvg_internals/benches/srgb.rs           | 40 +++++++++-------------
 rsvg_internals/src/surface_utils/srgb.rs | 58 ++++++++++++++------------------
 2 files changed, 42 insertions(+), 56 deletions(-)
---
diff --git a/rsvg_internals/benches/srgb.rs b/rsvg_internals/benches/srgb.rs
index 8e70ae47..39cf9578 100644
--- a/rsvg_internals/benches/srgb.rs
+++ b/rsvg_internals/benches/srgb.rs
@@ -4,9 +4,9 @@ use criterion::{black_box, Criterion};
 
 use rsvg_internals::rect::IRect;
 use rsvg_internals::surface_utils::{
-    shared_surface::{SharedImageSurface, SurfaceType},
+    shared_surface::{ExclusiveImageSurface, SurfaceType},
     srgb::{linearize, map_unpremultiplied_components_loop},
-    ImageSurfaceDataExt, Pixel,
+    Pixel,
 };
 
 const SURFACE_SIDE: i32 = 512;
@@ -20,33 +20,25 @@ const BOUNDS: IRect = IRect {
 fn bench_srgb_linearization(c: &mut Criterion) {
     c.bench_function("srgb map_unpremultiplied_components", |b| {
         let mut surface =
-            cairo::ImageSurface::create(cairo::Format::ARgb32, SURFACE_SIDE, SURFACE_SIDE).unwrap();
-        let mut output_surface =
-            cairo::ImageSurface::create(cairo::Format::ARgb32, SURFACE_SIDE, SURFACE_SIDE).unwrap();
+            ExclusiveImageSurface::new(SURFACE_SIDE, SURFACE_SIDE, SurfaceType::LinearRgb).unwrap();
 
         // Fill the surface with non-zero alpha (otherwise linearization is a no-op).
-        let stride = surface.get_stride() as usize;
-        {
-            let mut data = surface.get_data().unwrap();
-            for y in BOUNDS.y_range() {
-                for x in BOUNDS.x_range() {
-                    data.set_pixel(
-                        stride,
-                        Pixel {
-                            r: 0,
-                            g: 0,
-                            b: 0,
-                            a: 127,
-                        },
-                        x as u32,
-                        y as u32,
-                    );
-                }
+        for y in BOUNDS.y_range() {
+            for x in BOUNDS.x_range() {
+               let pixel = Pixel {
+                    r: 0,
+                    g: 0,
+                    b: 0,
+                    a: 127,
+                };
+
+                surface.set_pixel(pixel, x as u32, y as u32);
             }
         }
 
-        let surface = SharedImageSurface::wrap(surface, SurfaceType::LinearRgb).unwrap();
-
+        let surface = surface.share().unwrap();
+        let mut output_surface =
+            ExclusiveImageSurface::new(SURFACE_SIDE, SURFACE_SIDE, SurfaceType::SRgb).unwrap();
         let bounds = black_box(BOUNDS);
 
         b.iter(|| {
diff --git a/rsvg_internals/src/surface_utils/srgb.rs b/rsvg_internals/src/surface_utils/srgb.rs
index 39ca2d31..462fe94a 100644
--- a/rsvg_internals/src/surface_utils/srgb.rs
+++ b/rsvg_internals/src/surface_utils/srgb.rs
@@ -5,8 +5,8 @@
 use crate::rect::IRect;
 use crate::surface_utils::{
     iterators::Pixels,
-    shared_surface::{SharedImageSurface, SurfaceType},
-    ImageSurfaceDataExt, Pixel,
+    shared_surface::{ExclusiveImageSurface, SharedImageSurface, SurfaceType},
+    Pixel,
 };
 
 // Include the linearization and unlinearization tables.
@@ -28,34 +28,30 @@ pub fn unlinearize(c: u8) -> u8 {
 #[inline]
 pub fn map_unpremultiplied_components_loop<F: Fn(u8) -> u8>(
     surface: &SharedImageSurface,
-    output_surface: &mut cairo::ImageSurface,
+    output_surface: &mut ExclusiveImageSurface,
     bounds: IRect,
     f: F,
 ) {
-    let output_stride = output_surface.get_stride() as usize;
-    {
-        let mut output_data = output_surface.get_data().unwrap();
-
-        for (x, y, pixel) in Pixels::within(surface, bounds) {
-            if pixel.a > 0 {
-                let alpha = f64::from(pixel.a) / 255f64;
-
-                let compute = |x| {
-                    let x = f64::from(x) / alpha; // Unpremultiply alpha.
-                    let x = (x + 0.5) as u8; // Round to nearest u8.
-                    let x = f(x);
-                    let x = f64::from(x) * alpha; // Premultiply alpha again.
-                    (x + 0.5) as u8
-                };
-
-                let output_pixel = Pixel {
-                    r: compute(pixel.r),
-                    g: compute(pixel.g),
-                    b: compute(pixel.b),
-                    a: pixel.a,
-                };
-                output_data.set_pixel(output_stride, output_pixel, x, y);
-            }
+    for (x, y, pixel) in Pixels::within(surface, bounds) {
+        if pixel.a > 0 {
+            let alpha = f64::from(pixel.a) / 255f64;
+
+            let compute = |x| {
+                let x = f64::from(x) / alpha; // Unpremultiply alpha.
+                let x = (x + 0.5) as u8; // Round to nearest u8.
+                let x = f(x);
+                let x = f64::from(x) * alpha; // Premultiply alpha again.
+                (x + 0.5) as u8
+            };
+
+            let output_pixel = Pixel {
+                r: compute(pixel.r),
+                g: compute(pixel.g),
+                b: compute(pixel.b),
+                a: pixel.a,
+            };
+
+            output_surface.set_pixel(output_pixel, x, y);
         }
     }
 }
@@ -72,13 +68,11 @@ fn map_unpremultiplied_components<F: Fn(u8) -> u8>(
         return Ok(surface.clone());
     }
 
-    let width = surface.width();
-    let height = surface.height();
-
-    let mut output_surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width, height)?;
+    let (width, height) = (surface.width(), surface.height());
+    let mut output_surface = ExclusiveImageSurface::new(width, height, new_type)?;
     map_unpremultiplied_components_loop(surface, &mut output_surface, bounds, f);
 
-    SharedImageSurface::wrap(output_surface, new_type)
+    output_surface.share()
 }
 
 /// Converts an sRGB surface to a linear sRGB surface (undoes the gamma correction).


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