Blame demos/testpixbuf-color.c

Packit 98cdb6
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
#include <stdio.h>
Packit 98cdb6
#include <string.h>
Packit 98cdb6
Packit 98cdb6
#include <gtk/gtk.h>
Packit 98cdb6
Packit 98cdb6
#define ICC_PROFILE             "/usr/share/color/icc/bluish.icc"
Packit 98cdb6
#define ICC_PROFILE_SIZE        3966
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
save_image_png (const gchar *filename, GdkPixbuf *pixbuf, GError **error)
Packit 98cdb6
{
Packit 98cdb6
	gchar *contents = NULL;
Packit 98cdb6
	gchar *contents_encode = NULL;
Packit 98cdb6
	gsize length;
Packit 98cdb6
	gboolean ret;
Packit 98cdb6
	gint len;
Packit 98cdb6
Packit 98cdb6
	/* get icc file */
Packit 98cdb6
	ret = g_file_get_contents (ICC_PROFILE, &contents, &length, error);
Packit 98cdb6
	if (!ret)
Packit 98cdb6
		goto out;
Packit 98cdb6
	contents_encode = g_base64_encode ((const guchar *) contents, length);
Packit 98cdb6
	ret = gdk_pixbuf_save (pixbuf, filename, "png", error,
Packit 98cdb6
			       "tEXt::Software", "Hello my name is dave",
Packit 98cdb6
			       "icc-profile", contents_encode,
Packit 98cdb6
			       NULL);
Packit 98cdb6
	len = strlen (contents_encode);
Packit 98cdb6
	g_debug ("ICC profile was %i bytes", len);
Packit 98cdb6
out:
Packit 98cdb6
	g_free (contents);
Packit 98cdb6
	g_free (contents_encode);
Packit 98cdb6
	return ret;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
save_image_tiff (const gchar *filename, GdkPixbuf *pixbuf, GError **error)
Packit 98cdb6
{
Packit 98cdb6
	gchar *contents = NULL;
Packit 98cdb6
	gchar *contents_encode = NULL;
Packit 98cdb6
	gsize length;
Packit 98cdb6
	gboolean ret;
Packit 98cdb6
	gint len;
Packit 98cdb6
Packit 98cdb6
	/* get icc file */
Packit 98cdb6
	ret = g_file_get_contents (ICC_PROFILE, &contents, &length, error);
Packit 98cdb6
	if (!ret)
Packit 98cdb6
		goto out;
Packit 98cdb6
	contents_encode = g_base64_encode ((const guchar *) contents, length);
Packit 98cdb6
	ret = gdk_pixbuf_save (pixbuf, filename, "tiff", error,
Packit 98cdb6
			       "icc-profile", contents_encode,
Packit 98cdb6
			       NULL);
Packit 98cdb6
	len = strlen (contents_encode);
Packit 98cdb6
	g_debug ("ICC profile was %i bytes", len);
Packit 98cdb6
out:
Packit 98cdb6
	g_free (contents);
Packit 98cdb6
	g_free (contents_encode);
Packit 98cdb6
	return ret;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
save_image_verify (const gchar *filename, GError **error)
Packit 98cdb6
{
Packit 98cdb6
	gboolean ret = FALSE;
Packit 98cdb6
	GdkPixbuf *pixbuf = NULL;
Packit 98cdb6
	const gchar *option;
Packit 98cdb6
	gchar *icc_profile = NULL;
Packit 98cdb6
	gsize len = 0;
Packit 98cdb6
Packit 98cdb6
	/* load */
Packit 98cdb6
	pixbuf = gdk_pixbuf_new_from_file (filename, error);
Packit 98cdb6
	if (pixbuf == NULL)
Packit 98cdb6
		goto out;
Packit 98cdb6
Packit 98cdb6
	/* check values */
Packit 98cdb6
	option = gdk_pixbuf_get_option (pixbuf, "icc-profile");
Packit 98cdb6
	if (option == NULL) {
Packit 98cdb6
		*error = g_error_new (1, 0, "no profile set");
Packit 98cdb6
		goto out;
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
	/* decode base64 */
Packit 98cdb6
	icc_profile = (gchar *) g_base64_decode (option, &len;;
Packit 98cdb6
	if (len != ICC_PROFILE_SIZE) {
Packit 98cdb6
		*error = g_error_new (1, 0,
Packit 98cdb6
		                      "profile length invalid, got %" G_GSIZE_FORMAT,
Packit 98cdb6
		                      len);
Packit 98cdb6
		g_file_set_contents ("error.icc", icc_profile, len, NULL);
Packit 98cdb6
		goto out;
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
	/* success */
Packit 98cdb6
	ret = TRUE;
Packit 98cdb6
out:
Packit 98cdb6
	if (pixbuf != NULL)
Packit 98cdb6
		g_object_unref (pixbuf);
Packit 98cdb6
	g_free (icc_profile);
Packit 98cdb6
	return ret;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
int
Packit 98cdb6
main (int argc, char **argv)
Packit 98cdb6
{
Packit 98cdb6
	GdkWindow *root;
Packit 98cdb6
	GdkPixbuf *pixbuf;
Packit 98cdb6
	gboolean ret;
Packit 98cdb6
	gint retval = 1;
Packit 98cdb6
	GError *error = NULL;
Packit 98cdb6
Packit 98cdb6
	gtk_init (&argc, &argv);
Packit 98cdb6
Packit 98cdb6
	root = gdk_get_default_root_window ();
Packit 98cdb6
	pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
Packit 98cdb6
					       0, 0, 0, 0, 150, 160);
Packit 98cdb6
Packit 98cdb6
	/* PASS */
Packit 98cdb6
	g_debug ("try to save PNG with a profile");
Packit 98cdb6
	ret = save_image_png ("icc-profile.png", pixbuf, &error);
Packit 98cdb6
	if (!ret) {
Packit 98cdb6
		g_warning ("FAILED: did not save image: %s", error->message);
Packit 98cdb6
		g_error_free (error);
Packit 98cdb6
		goto out;
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
	/* PASS */
Packit 98cdb6
	g_debug ("try to save TIFF with a profile");
Packit 98cdb6
	ret = save_image_tiff ("icc-profile.tiff", pixbuf, &error);
Packit 98cdb6
	if (!ret) {
Packit 98cdb6
		g_warning ("FAILED: did not save image: %s", error->message);
Packit 98cdb6
		g_error_free (error);
Packit 98cdb6
		goto out;
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
	/* PASS */
Packit 98cdb6
	g_debug ("try to load PNG and get color attributes");
Packit 98cdb6
	ret = save_image_verify ("icc-profile.png", &error);
Packit 98cdb6
	if (!ret) {
Packit 98cdb6
		g_warning ("FAILED: did not load image: %s", error->message);
Packit 98cdb6
		g_error_free (error);
Packit 98cdb6
		goto out;
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
	/* PASS */
Packit 98cdb6
	g_debug ("try to load TIFF and get color attributes");
Packit 98cdb6
	ret = save_image_verify ("icc-profile.tiff", &error);
Packit 98cdb6
	if (!ret) {
Packit 98cdb6
		g_warning ("FAILED: did not load image: %s", error->message);
Packit 98cdb6
		g_error_free (error);
Packit 98cdb6
		goto out;
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
	/* success */
Packit 98cdb6
	retval = 0;
Packit 98cdb6
	g_debug ("ALL OKAY!");
Packit 98cdb6
out:
Packit 98cdb6
	return retval;
Packit 98cdb6
}