[librsvg: 1/5] GdkPixbufRGBA, ToGdkPixbufRGBA - start naming types and conversion traits for pixel formats
- From: Marge Bot <marge-bot src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/5] GdkPixbufRGBA, ToGdkPixbufRGBA - start naming types and conversion traits for pixel formats
- Date: Wed, 22 Sep 2021 01:11:18 +0000 (UTC)
commit 5c05ded662785c6802ae322234fc48128dc90951
Author: Federico Mena Quintero <federico gnome org>
Date: Tue Sep 21 12:05:27 2021 -0500
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)
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]