From 5c05ded662785c6802ae322234fc48128dc90951 Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@gnome.org>
Date: Tue, 21 Sep 2021 12:05:27 -0500
Subject: [PATCH 1/4] GdkPixbufRGBA, ToGdkPixbufRGBA - start naming types and
 conversion traits for pixel formats

The code assumes that struct Pixel is always the layout that GdkPixbuf
uses.  This is true right now, but is a hidden assumption.  Let's
start giving better names to pixel formats.

Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/592>
---
 src/surface_utils/mod.rs            | 23 +++++++++++++++++++++++
 src/surface_utils/shared_surface.rs |  4 ++--
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/surface_utils/mod.rs b/src/surface_utils/mod.rs
index 53bbd00b..93d3b4f7 100644
--- a/src/surface_utils/mod.rs
+++ b/src/surface_utils/mod.rs
@@ -22,6 +22,9 @@ use rgb::alt::ARGB8;
 #[allow(clippy::upper_case_acronyms)]
 pub type CairoARGB = ARGB8;
 
+/// GdkPixbuf's endian-independent RGBA8 pixel layout.
+pub type GdkPixbufRGBA = rgb::RGBA8;
+
 /// Analogous to `rgb::FromSlice`, to convert from `[T]` to `[CairoARGB]`
 #[allow(clippy::upper_case_acronyms)]
 pub trait AsCairoARGB<T: Copy> {
@@ -57,6 +60,26 @@ pub enum EdgeMode {
     None,
 }
 
+/// Trait to convert pixels in various formats to RGBA, for GdkPixbuf.
+///
+/// GdkPixbuf unconditionally uses RGBA ordering regardless of endianness,
+/// but we need to convert to it from Cairo's endian-dependent 0xaarrggbb.
+pub trait ToGdkPixbufRGBA {
+    fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA;
+}
+
+impl ToGdkPixbufRGBA for Pixel {
+    #[inline]
+    fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA {
+        GdkPixbufRGBA {
+            r: self.r,
+            g: self.g,
+            b: self.b,
+            a: self.a,
+        }
+    }
+}
+
 /// Extension methods for `cairo::ImageSurfaceData`.
 pub trait ImageSurfaceDataExt: DerefMut<Target = [u8]> {
     /// Sets the pixel at the given coordinates. Assumes the `ARgb32` format.
diff --git a/src/surface_utils/shared_surface.rs b/src/surface_utils/shared_surface.rs
index 9d328923..476a6f77 100644
--- a/src/surface_utils/shared_surface.rs
+++ b/src/surface_utils/shared_surface.rs
@@ -15,7 +15,7 @@ use crate::util::clamp;
 
 use super::{
     iterators::{PixelRectangle, Pixels},
-    AsCairoARGB, CairoARGB, EdgeMode, ImageSurfaceDataExt, Pixel, PixelOps,
+    AsCairoARGB, CairoARGB, EdgeMode, ImageSurfaceDataExt, Pixel, PixelOps, ToGdkPixbufRGBA,
 };
 
 /// Types of pixel data in a `ImageSurface`.
@@ -342,7 +342,7 @@ impl ImageSurface<Shared> {
             .map(|row| row.as_rgba_mut())
             .zip(self.rows())
             .flat_map(|(dest_row, src_row)| src_row.iter().zip(dest_row.iter_mut()))
-            .for_each(|(src, dest)| *dest = Pixel::from(*src).unpremultiply());
+            .for_each(|(src, dest)| *dest = Pixel::from(*src).unpremultiply().to_pixbuf_rgba());
 
         Some(pixbuf)
     }
-- 
GitLab


From 3ba831aa4fd97ab126697478a0871e6938f56b03 Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@gnome.org>
Date: Tue, 21 Sep 2021 12:22:15 -0500
Subject: [PATCH 2/4] New ToPixel trait

Use it where we convert GdkPixbuf pixels to our own Pixel for premultiplication.

Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/592>
---
 src/surface_utils/mod.rs            | 32 +++++++++++++++++++++++++++++
 src/surface_utils/shared_surface.rs |  5 +++--
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/surface_utils/mod.rs b/src/surface_utils/mod.rs
index 93d3b4f7..58953e6a 100644
--- a/src/surface_utils/mod.rs
+++ b/src/surface_utils/mod.rs
@@ -25,6 +25,9 @@ pub type CairoARGB = ARGB8;
 /// GdkPixbuf's endian-independent RGBA8 pixel layout.
 pub type GdkPixbufRGBA = rgb::RGBA8;
 
+/// GdkPixbuf's packed RGB pixel layout.
+pub type GdkPixbufRGB = rgb::RGB8;
+
 /// Analogous to `rgb::FromSlice`, to convert from `[T]` to `[CairoARGB]`
 #[allow(clippy::upper_case_acronyms)]
 pub trait AsCairoARGB<T: Copy> {
@@ -68,6 +71,11 @@ pub trait ToGdkPixbufRGBA {
     fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA;
 }
 
+/// Trait to convert pixels in various formats to our own Pixel layout.
+pub trait ToPixel {
+    fn to_pixel(&self) -> Pixel;
+}
+
 impl ToGdkPixbufRGBA for Pixel {
     #[inline]
     fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA {
@@ -80,6 +88,30 @@ impl ToGdkPixbufRGBA for Pixel {
     }
 }
 
+impl ToPixel for GdkPixbufRGBA {
+    #[inline]
+    fn to_pixel(&self) -> Pixel {
+        Pixel {
+            r: self.r,
+            g: self.g,
+            b: self.b,
+            a: self.a,
+        }
+    }
+}
+
+impl ToPixel for GdkPixbufRGB {
+    #[inline]
+    fn to_pixel(&self) -> Pixel {
+        Pixel {
+            r: self.r,
+            g: self.g,
+            b: self.b,
+            a: 255,
+        }
+    }
+}
+
 /// Extension methods for `cairo::ImageSurfaceData`.
 pub trait ImageSurfaceDataExt: DerefMut<Target = [u8]> {
     /// Sets the pixel at the given coordinates. Assumes the `ARgb32` format.
diff --git a/src/surface_utils/shared_surface.rs b/src/surface_utils/shared_surface.rs
index 476a6f77..9fa9a2e1 100644
--- a/src/surface_utils/shared_surface.rs
+++ b/src/surface_utils/shared_surface.rs
@@ -16,6 +16,7 @@ use crate::util::clamp;
 use super::{
     iterators::{PixelRectangle, Pixels},
     AsCairoARGB, CairoARGB, EdgeMode, ImageSurfaceDataExt, Pixel, PixelOps, ToGdkPixbufRGBA,
+    ToPixel,
 };
 
 /// Types of pixel data in a `ImageSurface`.
@@ -304,13 +305,13 @@ impl ImageSurface<Shared> {
                 .map(|row| row.as_rgba())
                 .zip(surf.rows_mut())
                 .flat_map(|(src_row, dest_row)| src_row.iter().zip(dest_row.iter_mut()))
-                .for_each(|(src, dest)| *dest = src.premultiply().into());
+                .for_each(|(src, dest)| *dest = src.to_pixel().premultiply().into());
         } else {
             pixbuf_rows
                 .map(|row| row.as_rgb())
                 .zip(surf.rows_mut())
                 .flat_map(|(src_row, dest_row)| src_row.iter().zip(dest_row.iter_mut()))
-                .for_each(|(src, dest)| *dest = src.alpha(0xff).into());
+                .for_each(|(src, dest)| *dest = src.to_pixel().into());
         }
 
         if let (Some(content_type), Some(bytes)) = (content_type, mime_data) {
-- 
GitLab


From 3fb10220fd948caec7e957739fcb5e1f9b2896ef Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@gnome.org>
Date: Tue, 21 Sep 2021 12:49:53 -0500
Subject: [PATCH 3/4] New ToCairoARGB trait

Use it in the pixbuf-to-cairo-surface function.

Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/592>
---
 src/surface_utils/mod.rs            | 17 +++++++++++++++++
 src/surface_utils/shared_surface.rs |  8 ++++----
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/surface_utils/mod.rs b/src/surface_utils/mod.rs
index 58953e6a..3f915cd0 100644
--- a/src/surface_utils/mod.rs
+++ b/src/surface_utils/mod.rs
@@ -76,6 +76,11 @@ pub trait ToPixel {
     fn to_pixel(&self) -> Pixel;
 }
 
+/// Trait to convert pixels in various formats to Cairo's endian-dependent 0xaarrggbb.
+pub trait ToCairoARGB {
+    fn to_cairo_argb(&self) -> CairoARGB;
+}
+
 impl ToGdkPixbufRGBA for Pixel {
     #[inline]
     fn to_pixbuf_rgba(&self) -> GdkPixbufRGBA {
@@ -112,6 +117,18 @@ impl ToPixel for GdkPixbufRGB {
     }
 }
 
+impl ToCairoARGB for Pixel {
+    #[inline]
+    fn to_cairo_argb(&self) -> CairoARGB {
+        CairoARGB {
+            r: self.r,
+            g: self.g,
+            b: self.b,
+            a: self.a,
+        }
+    }
+}
+
 /// Extension methods for `cairo::ImageSurfaceData`.
 pub trait ImageSurfaceDataExt: DerefMut<Target = [u8]> {
     /// Sets the pixel at the given coordinates. Assumes the `ARgb32` format.
diff --git a/src/surface_utils/shared_surface.rs b/src/surface_utils/shared_surface.rs
index 9fa9a2e1..34dfc992 100644
--- a/src/surface_utils/shared_surface.rs
+++ b/src/surface_utils/shared_surface.rs
@@ -15,8 +15,8 @@ use crate::util::clamp;
 
 use super::{
     iterators::{PixelRectangle, Pixels},
-    AsCairoARGB, CairoARGB, EdgeMode, ImageSurfaceDataExt, Pixel, PixelOps, ToGdkPixbufRGBA,
-    ToPixel,
+    AsCairoARGB, CairoARGB, EdgeMode, ImageSurfaceDataExt, Pixel, PixelOps, ToCairoARGB,
+    ToGdkPixbufRGBA, ToPixel,
 };
 
 /// Types of pixel data in a `ImageSurface`.
@@ -305,13 +305,13 @@ impl ImageSurface<Shared> {
                 .map(|row| row.as_rgba())
                 .zip(surf.rows_mut())
                 .flat_map(|(src_row, dest_row)| src_row.iter().zip(dest_row.iter_mut()))
-                .for_each(|(src, dest)| *dest = src.to_pixel().premultiply().into());
+                .for_each(|(src, dest)| *dest = src.to_pixel().premultiply().to_cairo_argb());
         } else {
             pixbuf_rows
                 .map(|row| row.as_rgb())
                 .zip(surf.rows_mut())
                 .flat_map(|(src_row, dest_row)| src_row.iter().zip(dest_row.iter_mut()))
-                .for_each(|(src, dest)| *dest = src.to_pixel().into());
+                .for_each(|(src, dest)| *dest = src.to_pixel().to_cairo_argb());
         }
 
         if let (Some(content_type), Some(bytes)) = (content_type, mime_data) {
-- 
GitLab


From f2dc3265a3a021e55002a13a6dc81b945fd32b91 Mon Sep 17 00:00:00 2001
From: Federico Mena Quintero <federico@gnome.org>
Date: Tue, 21 Sep 2021 12:54:12 -0500
Subject: [PATCH 4/4] impl ToPixel for CairoARGB

Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/592>
---
 src/surface_utils/mod.rs            | 12 ++++++++++++
 src/surface_utils/shared_surface.rs |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/surface_utils/mod.rs b/src/surface_utils/mod.rs
index 3f915cd0..4f751ece 100644
--- a/src/surface_utils/mod.rs
+++ b/src/surface_utils/mod.rs
@@ -93,6 +93,18 @@ impl ToGdkPixbufRGBA for Pixel {
     }
 }
 
+impl ToPixel for CairoARGB {
+    #[inline]
+    fn to_pixel(&self) -> Pixel {
+        Pixel {
+            r: self.r,
+            g: self.g,
+            b: self.b,
+            a: self.a,
+        }
+    }
+}
+
 impl ToPixel for GdkPixbufRGBA {
     #[inline]
     fn to_pixel(&self) -> Pixel {
diff --git a/src/surface_utils/shared_surface.rs b/src/surface_utils/shared_surface.rs
index 34dfc992..20cd0f40 100644
--- a/src/surface_utils/shared_surface.rs
+++ b/src/surface_utils/shared_surface.rs
@@ -343,7 +343,7 @@ impl ImageSurface<Shared> {
             .map(|row| row.as_rgba_mut())
             .zip(self.rows())
             .flat_map(|(dest_row, src_row)| src_row.iter().zip(dest_row.iter_mut()))
-            .for_each(|(src, dest)| *dest = Pixel::from(*src).unpremultiply().to_pixbuf_rgba());
+            .for_each(|(src, dest)| *dest = src.to_pixel().unpremultiply().to_pixbuf_rgba());
 
         Some(pixbuf)
     }
-- 
GitLab

