]> Pileus Git - ~andy/gtk/blob - demos/testpixbuf-color.c
Add color management support to gdk_pixbuf_save
[~andy/gtk] / demos / testpixbuf-color.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 #include "config.h"
4 #include <stdio.h>
5 #include <string.h>
6
7 #include <gtk/gtk.h>
8
9 #define ICC_PROFILE             "/usr/share/color/icc/bluish.icc"
10 #define ICC_PROFILE_SIZE        3966
11
12 static gboolean
13 save_image_png (const gchar *filename, GdkPixbuf *pixbuf, GError **error)
14 {
15         gchar *contents = NULL;
16         gchar *contents_encode = NULL;
17         gsize length;
18         gboolean ret;
19         gint len;
20
21         /* get icc file */
22         ret = g_file_get_contents (ICC_PROFILE, &contents, &length, error);
23         if (!ret)
24                 goto out;
25         contents_encode = g_base64_encode ((const guchar *) contents, length);
26         ret = gdk_pixbuf_save (pixbuf, filename, "png", error,
27                                "tEXt::Software", "Hello my name is dave",
28                                "icc-profile", contents_encode,
29                                NULL);
30         len = strlen (contents_encode);
31         g_debug ("ICC profile was %i bytes", len);
32 out:
33         g_free (contents);
34         g_free (contents_encode);
35         return ret;
36 }
37
38 static gboolean
39 save_image_verify (const gchar *filename, GError **error)
40 {
41         gboolean ret = FALSE;
42         GdkPixbuf *pixbuf = NULL;
43         const gchar *option;
44         gchar *icc_profile = NULL;
45         gsize len = 0;
46
47         /* load */
48         pixbuf = gdk_pixbuf_new_from_file (filename, error);
49         if (pixbuf == NULL)
50                 goto out;
51
52         /* check values */
53         option = gdk_pixbuf_get_option (pixbuf, "icc-profile");
54         if (option == NULL) {
55                 *error = g_error_new (1, 0, "no profile set");
56                 goto out;
57         }
58
59         /* decode base64 */
60         icc_profile = (gchar *) g_base64_decode (option, &len);
61         if (len != ICC_PROFILE_SIZE) {
62                 *error = g_error_new (1, 0, "profile length invalid, got %i", len);
63                 g_file_set_contents ("error.icc", icc_profile, len, NULL);
64                 goto out;
65         }
66
67         /* success */
68         ret = TRUE;
69 out:
70         if (pixbuf != NULL)
71                 g_object_unref (pixbuf);
72         g_free (icc_profile);
73         return ret;
74 }
75
76 int
77 main (int argc, char **argv)
78 {
79         GdkWindow *root;
80         GdkPixbuf *pixbuf;
81         gboolean ret;
82         gint retval = 1;
83         GError *error = NULL;
84
85         gtk_init (&argc, &argv);
86
87         gtk_widget_set_default_colormap (gdk_rgb_get_colormap ());
88
89         root = gdk_get_default_root_window ();
90         pixbuf = gdk_pixbuf_get_from_drawable (NULL, root, NULL,
91                                                0, 0, 0, 0, 150, 160);
92
93         /* PASS */
94         g_debug ("try to save PNG with a profile");
95         ret = save_image_png ("icc-profile.png", pixbuf, &error);
96         if (!ret) {
97                 g_warning ("FAILED: did not save image: %s", error->message);
98                 g_error_free (error);
99                 goto out;
100         }
101
102         /* PASS */
103         g_debug ("try to load PNG and get color attributes");
104         ret = save_image_verify ("icc-profile.png", &error);
105         if (!ret) {
106                 g_warning ("FAILED: did not load image: %s", error->message);
107                 g_error_free (error);
108                 goto out;
109         }
110
111         /* success */
112         retval = 0;
113         g_debug ("ALL OKAY!");
114 out:
115         return retval;
116 }