[gdk-pixbuf] Add gdk_pixbuf_read_pixel_bytes()



commit 3b40f1e8ba369f131ecc5c7f26bacbfef19b23f2
Author: Colin Walters <walters verbum org>
Date:   Mon Jul 14 13:28:07 2014 -0400

    Add gdk_pixbuf_read_pixel_bytes()
    
    This can be convenient for language bindings to access the readonly
    data in a form that includes length, and also avoids a copy (for
    readonly pixbufs).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=732297

 gdk-pixbuf/gdk-pixbuf-core.h       |    5 ++---
 gdk-pixbuf/gdk-pixbuf.c            |   24 ++++++++++++++++++++++++
 gdk-pixbuf/gdk-pixbuf.symbols      |    1 +
 tests/pixbuf-readonly-to-mutable.c |   26 ++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 3 deletions(-)
---
diff --git a/gdk-pixbuf/gdk-pixbuf-core.h b/gdk-pixbuf/gdk-pixbuf-core.h
index 9f11d87..b152aa9 100644
--- a/gdk-pixbuf/gdk-pixbuf-core.h
+++ b/gdk-pixbuf/gdk-pixbuf-core.h
@@ -241,9 +241,8 @@ gsize         gdk_pixbuf_get_byte_length     (const GdkPixbuf *pixbuf);
 guchar       *gdk_pixbuf_get_pixels_with_length (const GdkPixbuf *pixbuf,
                                                  guint           *length);
 
-const guint8* gdk_pixbuf_read_pixels    (const GdkPixbuf  *pixbuf);
-                                             
-
+const guint8* gdk_pixbuf_read_pixels         (const GdkPixbuf  *pixbuf);
+GBytes *      gdk_pixbuf_read_pixel_bytes    (const GdkPixbuf  *pixbuf);
 
 
 
diff --git a/gdk-pixbuf/gdk-pixbuf.c b/gdk-pixbuf/gdk-pixbuf.c
index 1f5e5d4..6552521 100644
--- a/gdk-pixbuf/gdk-pixbuf.c
+++ b/gdk-pixbuf/gdk-pixbuf.c
@@ -709,6 +709,30 @@ gdk_pixbuf_read_pixels (const GdkPixbuf  *pixbuf)
 }
 
 /**
+ * gdk_pixbuf_read_pixel_bytes:
+ * @pixbuf: A pixbuf
+ *
+ * Returns: (transfer full): A new reference to a read-only copy of
+ * the pixel data.  Note that for mutable pixbufs, this function will
+ * incur a one-time copy of the pixel data for conversion into the
+ * returned #GBytes.
+ *
+ * Since: 2.32
+ */
+GBytes *
+gdk_pixbuf_read_pixel_bytes (const GdkPixbuf  *pixbuf)
+{
+        g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
+
+        if (pixbuf->bytes) {
+                return g_bytes_ref (pixbuf->bytes);
+        } else {
+                return g_bytes_new (pixbuf->pixels,
+                                    gdk_pixbuf_get_byte_length (pixbuf));
+        }
+}
+
+/**
  * gdk_pixbuf_get_width:
  * @pixbuf: A pixbuf.
  *
diff --git a/gdk-pixbuf/gdk-pixbuf.symbols b/gdk-pixbuf/gdk-pixbuf.symbols
index 3f292e0..1da56da 100644
--- a/gdk-pixbuf/gdk-pixbuf.symbols
+++ b/gdk-pixbuf/gdk-pixbuf.symbols
@@ -24,6 +24,7 @@ gdk_pixbuf_get_has_alpha
 gdk_pixbuf_get_height
 gdk_pixbuf_get_n_channels
 gdk_pixbuf_read_pixels
+gdk_pixbuf_read_pixel_bytes
 gdk_pixbuf_get_pixels
 gdk_pixbuf_get_pixels_with_length
 gdk_pixbuf_get_byte_length
diff --git a/tests/pixbuf-readonly-to-mutable.c b/tests/pixbuf-readonly-to-mutable.c
index 8f81c87..456f5a2 100644
--- a/tests/pixbuf-readonly-to-mutable.c
+++ b/tests/pixbuf-readonly-to-mutable.c
@@ -148,12 +148,38 @@ test_mutate_readonly (void)
   g_object_unref (src);
 }
 
+static void
+test_read_pixel_bytes (void)
+{
+  GdkPixbuf *src;
+  GBytes *bytes;
+  
+  if (!format_supported ("png"))
+    {
+      g_test_skip ("format not supported");
+      return;
+    }
+  
+  src = get_readonly_pixbuf ();
+  bytes = gdk_pixbuf_read_pixel_bytes (src);
+  g_object_unref (src);
+  g_bytes_unref (bytes);
+
+  src = get_readonly_pixbuf ();
+  /* Force a mutable conversion */
+  (void) gdk_pixbuf_get_pixels (src);
+  bytes = gdk_pixbuf_read_pixel_bytes (src);
+  g_object_unref (src);
+  g_bytes_unref (bytes);
+}
+
 int
 main (int argc, char **argv)
 {
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/pixbuf/readonly/mutate", test_mutate_readonly);
+  g_test_add_func ("/pixbuf/readonly/readpixelbytes", test_read_pixel_bytes);
 
   return g_test_run ();
 }


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