diff options
author | Ryan Lortie <desrt@moonpix.lan> | 2013-02-26 16:01:32 -0500 |
---|---|---|
committer | Ryan Lortie <desrt@desrt.ca> | 2013-04-21 16:39:14 -0400 |
commit | aa0f928631d7c3877633337960c10963fb6eaecd (patch) | |
tree | 390a033e7d659c04ff6e3ebc2b3b538a06f1e51d /tests/pixbuf-icon-serialize.c | |
parent | cb0d6bef7f241d21f6efd6af377cab3a33b7de09 (diff) | |
download | gdk-pixbuf-aa0f928631d7c3877633337960c10963fb6eaecd.tar.gz |
Fix GIcon implementation
The "new rules" for GIcon say that we must support serialisation via
g_icon_serialize() and loadability via GLoadableIcon, so implement both
of those.
Serialise GdkPixbuf by emitting a GVariant that will result in a
png-encoded GBytesIcon when deserialized. The GLoadableIcon interface
is similar: we return a stream that will read out as a png.
Test the serialisation by round-tripping an image through this process
and ensuring that it is pixel-perfect.
https://bugzilla.gnome.org/show_bug.cgi?id=688820
Diffstat (limited to 'tests/pixbuf-icon-serialize.c')
-rw-r--r-- | tests/pixbuf-icon-serialize.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/pixbuf-icon-serialize.c b/tests/pixbuf-icon-serialize.c new file mode 100644 index 000000000..7d35a20af --- /dev/null +++ b/tests/pixbuf-icon-serialize.c @@ -0,0 +1,60 @@ +#include "config.h" +#include "gdk-pixbuf/gdk-pixbuf.h" +#include <string.h> +#include <glib.h> + +static void +test_serialize (void) +{ + GError *error = NULL; + GdkPixbuf *pixbuf; + GdkPixbuf *pixbuf2; + GVariant *data; + GIcon *icon; + GInputStream *stream; + + pixbuf = gdk_pixbuf_new_from_file (ABS_SRCDIR "/test-image.png", &error); + g_assert_no_error (error); + g_assert (pixbuf != NULL); + + /* turn it into a GVariant */ + data = g_icon_serialize (G_ICON (pixbuf)); + + /* back to a GIcon, but this will be a GBytesIcon, not GdkPixbuf */ + icon = g_icon_deserialize (data); + g_assert (G_IS_BYTES_ICON (icon)); + + /* but since that is a GLoadableIcon, we can load it again */ + stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), 0, NULL, NULL, &error); + g_assert_no_error (error); + pixbuf2 = gdk_pixbuf_new_from_stream (stream, NULL, &error); + g_assert_no_error (error); + + /* make sure that the pixels are the same. + * our _serialize() uses png, so this should be perfect. + */ + { + guchar *pixels_a, *pixels_b; + guint len_a, len_b; + pixels_a = gdk_pixbuf_get_pixels_with_length (pixbuf, &len_a); + pixels_b = gdk_pixbuf_get_pixels_with_length (pixbuf2, &len_b); + g_assert (len_a == len_b); + g_assert (memcmp (pixels_a, pixels_b, len_a) == 0); + } + + g_object_unref (pixbuf2); + g_object_unref (pixbuf); + g_object_unref (stream); + g_variant_unref (data); + +} + +int +main (int argc, char **argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/pixbuf/icon/serialize", test_serialize); + + return g_test_run (); +} |