diff options
author | Richard Hughes <richard@hughsie.com> | 2009-12-18 15:17:13 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2009-12-18 15:17:13 +0000 |
commit | ba651d4022ed4dceb6ad7394adcb0ff6c8006581 (patch) | |
tree | 07879e078ed5659f57d359067872d57dcbe63d51 /demos | |
parent | 7e9d89b555d7d22a89645e0821a334dec318bc21 (diff) | |
download | gtk+-ba651d4022ed4dceb6ad7394adcb0ff6c8006581.tar.gz |
Add color management support to gdk_pixbuf_save
This patch adds an icc-profile option to a GdkPixbuf which can
be used to read or write an embedded ICC profile.
Add PNG support for now, but other image formats are awaiting
review.
Diffstat (limited to 'demos')
-rw-r--r-- | demos/Makefile.am | 4 | ||||
-rw-r--r-- | demos/testpixbuf-color.c | 116 |
2 files changed, 120 insertions, 0 deletions
diff --git a/demos/Makefile.am b/demos/Makefile.am index c8c9bbb1c5..81e07f427a 100644 --- a/demos/Makefile.am +++ b/demos/Makefile.am @@ -24,6 +24,7 @@ LDADDS = \ noinst_PROGRAMS = \ testpixbuf-drawable \ testanimation \ + testpixbuf-color \ testpixbuf-save \ testpixbuf-scale \ pixbuf-demo @@ -52,6 +53,7 @@ test-inline-pixbufs.h: $(pixbuf_csource_deps) apple-red.png gnome-foot.png testpixbuf_DEPENDENCIES = $(DEPS) testpixbuf_drawable_DEPENDENCIES = $(DEPS) testpixbuf_save_DEPENDENCIES = $(DEPS) +testpixbuf_color_DEPENDENCIES = $(DEPS) testpixbuf_scale_DEPENDENCIES = $(DEPS) testanimation_DEPENDENCIES = $(DEPS) pixbuf_demo_DEPENDENCIES = $(DEPS) @@ -59,6 +61,7 @@ pixbuf_demo_DEPENDENCIES = $(DEPS) testpixbuf_LDADD = $(LDADDS) testpixbuf_drawable_LDADD = $(LDADDS) testpixbuf_save_LDADD = $(LDADDS) +testpixbuf_color_LDADD = $(LDADDS) testpixbuf_scale_LDADD = $(LDADDS) testanimation_LDADD = $(LDADDS) pixbuf_demo_LDADD = $(LDADDS) @@ -66,6 +69,7 @@ pixbuf_demo_LDADD = $(LDADDS) testpixbuf_SOURCES = testpixbuf.c pixbuf-init.c testpixbuf_drawable_SOURCES = testpixbuf-drawable.c pixbuf-init.c testpixbuf_save_SOURCES = testpixbuf-save.c +testpixbuf_color_SOURCES = testpixbuf-color.c testpixbuf_scale_SOURCES = testpixbuf-scale.c pixbuf-init.c testanimation_SOURCES = testanimation.c pixbuf-init.c pixbuf_demo_SOURCES = pixbuf-demo.c pixbuf-init.c diff --git a/demos/testpixbuf-color.c b/demos/testpixbuf-color.c new file mode 100644 index 0000000000..b87d850a0e --- /dev/null +++ b/demos/testpixbuf-color.c @@ -0,0 +1,116 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ + +#include "config.h" +#include <stdio.h> +#include <string.h> + +#include <gtk/gtk.h> + +#define ICC_PROFILE "/usr/share/color/icc/bluish.icc" +#define ICC_PROFILE_SIZE 3966 + +static gboolean +save_image_png (const gchar *filename, GdkPixbuf *pixbuf, GError **error) +{ + gchar *contents = NULL; + gchar *contents_encode = NULL; + gsize length; + gboolean ret; + gint len; + + /* get icc file */ + ret = g_file_get_contents (ICC_PROFILE, &contents, &length, error); + if (!ret) + goto out; + contents_encode = g_base64_encode ((const guchar *) contents, length); + ret = gdk_pixbuf_save (pixbuf, filename, "png", error, + "tEXt::Software", "Hello my name is dave", + "icc-profile", contents_encode, + NULL); + len = strlen (contents_encode); + g_debug ("ICC profile was %i bytes", len); +out: + g_free (contents); + g_free (contents_encode); + return ret; +} + +static gboolean +save_image_verify (const gchar *filename, GError **error) +{ + gboolean ret = FALSE; + GdkPixbuf *pixbuf = NULL; + const gchar *option; + gchar *icc_profile = NULL; + gsize len = 0; + + /* load */ + pixbuf = gdk_pixbuf_new_from_file (filename, error); + if (pixbuf == NULL) + goto out; + + /* check values */ + option = gdk_pixbuf_get_option (pixbuf, "icc-profile"); + if (option == NULL) { + *error = g_error_new (1, 0, "no profile set"); + goto out; + } + + /* decode base64 */ + icc_profile = (gchar *) g_base64_decode (option, &len); + if (len != ICC_PROFILE_SIZE) { + *error = g_error_new (1, 0, "profile length invalid, got %i", len); + g_file_set_contents ("error.icc", icc_profile, len, NULL); + goto out; + } + + /* success */ + ret = TRUE; +out: + if (pixbuf != NULL) + g_object_unref (pixbuf); + g_free (icc_profile); + return ret; +} + +int +main (int argc, char **argv) +{ + GdkWindow *root; + GdkPixbuf *pixbuf; + gboolean ret; + gint retval = 1; + GError *error = NULL; + + gtk_init (&argc, &argv); + + gtk_widget_set_default_colormap (gdk_rgb_get_colormap ()); + + root = gdk_get_default_root_window (); + pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL, + 0, 0, 0, 0, 150, 160); + + /* PASS */ + g_debug ("try to save PNG with a profile"); + ret = save_image_png ("icc-profile.png", pixbuf, &error); + if (!ret) { + g_warning ("FAILED: did not save image: %s", error->message); + g_error_free (error); + goto out; + } + + /* PASS */ + g_debug ("try to load PNG and get color attributes"); + ret = save_image_verify ("icc-profile.png", &error); + if (!ret) { + g_warning ("FAILED: did not load image: %s", error->message); + g_error_free (error); + goto out; + } + + /* success */ + retval = 0; + g_debug ("ALL OKAY!"); +out: + return retval; +} |