[gtk/wip/otte/color-profiles: 23/28] Support color profiles in pixbufs




commit b3a191b6d942166d2745bd32875028f0ce7faf2b
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Sep 29 15:40:56 2021 -0400

    Support color profiles in pixbufs
    
    When creating a GdkTexture from a GdkPixbuf,
    see if it has an icc profile attached, and if
    so, use it.

 gdk/gdktexture.c | 45 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 4fac27212e..03b55a7fb6 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -435,6 +435,30 @@ gdk_texture_new_for_surface (cairo_surface_t *surface)
   return texture;
 }
 
+static GdkColorProfile *
+gdk_color_profile_from_pixbuf (GdkPixbuf *pixbuf)
+{
+  const char *icc_profile_base64;
+  GdkColorProfile *profile = NULL;
+
+  icc_profile_base64 = gdk_pixbuf_get_option (pixbuf, "icc-profile");
+  if (icc_profile_base64)
+    {
+      guchar *icc_data;
+      gsize icc_len;
+      GBytes *bytes;
+
+      icc_data = g_base64_decode (icc_profile_base64, &icc_len);
+      bytes = g_bytes_new_take (icc_data, icc_len);
+      profile = gdk_color_profile_new_from_icc_bytes (bytes, NULL);
+      g_bytes_unref (bytes);
+    }
+  if (!profile)
+    profile = g_object_ref (gdk_color_profile_get_srgb ());
+
+  return profile;
+}
+
 /**
  * gdk_texture_new_for_pixbuf:
  * @pixbuf: a `GdkPixbuf`
@@ -452,24 +476,31 @@ gdk_texture_new_for_pixbuf (GdkPixbuf *pixbuf)
 {
   GdkTexture *texture;
   GBytes *bytes;
+  GdkColorProfile *profile;
 
   g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
 
+  profile = gdk_color_profile_from_pixbuf (pixbuf);
+
   bytes = g_bytes_new_with_free_func (gdk_pixbuf_get_pixels (pixbuf),
                                       gdk_pixbuf_get_height (pixbuf)
                                       * gdk_pixbuf_get_rowstride (pixbuf),
                                       g_object_unref,
                                       g_object_ref (pixbuf));
-  texture = gdk_memory_texture_new (gdk_pixbuf_get_width (pixbuf),
-                                    gdk_pixbuf_get_height (pixbuf),
-                                    gdk_pixbuf_get_has_alpha (pixbuf)
-                                    ? GDK_MEMORY_GDK_PIXBUF_ALPHA
-                                    : GDK_MEMORY_GDK_PIXBUF_OPAQUE,
-                                    bytes,
-                                    gdk_pixbuf_get_rowstride (pixbuf));
+
+  texture = gdk_memory_texture_new_with_color_profile (gdk_pixbuf_get_width (pixbuf),
+                                                       gdk_pixbuf_get_height (pixbuf),
+                                                       gdk_pixbuf_get_has_alpha (pixbuf)
+                                                       ? GDK_MEMORY_GDK_PIXBUF_ALPHA
+                                                       : GDK_MEMORY_GDK_PIXBUF_OPAQUE,
+                                                       profile,
+                                                       bytes,
+                                                       gdk_pixbuf_get_rowstride (pixbuf));
 
   g_bytes_unref (bytes);
 
+  g_object_unref (profile);
+
   return texture;
 }
 


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