summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2014-07-14 13:28:07 -0400
committerMatthias Clasen <mclasen@redhat.com>2014-07-18 16:46:48 -0400
commit3b40f1e8ba369f131ecc5c7f26bacbfef19b23f2 (patch)
treea560ab9c7891266da58e0ae59e19562385300fcb
parent862e3890f54aaeb8589a6ee4c146f1fb50556004 (diff)
downloadgdk-pixbuf-3b40f1e8ba369f131ecc5c7f26bacbfef19b23f2.tar.gz
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
-rw-r--r--gdk-pixbuf/gdk-pixbuf-core.h5
-rw-r--r--gdk-pixbuf/gdk-pixbuf.c24
-rw-r--r--gdk-pixbuf/gdk-pixbuf.symbols1
-rw-r--r--tests/pixbuf-readonly-to-mutable.c26
4 files changed, 53 insertions, 3 deletions
diff --git a/gdk-pixbuf/gdk-pixbuf-core.h b/gdk-pixbuf/gdk-pixbuf-core.h
index 9f11d8781..b152aa911 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 1f5e5d425..655252107 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 3f292e0cf..1da56da77 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 8f81c8711..456f5a2e6 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 ();
}