]> Pileus Git - ~andy/gtk/blob - tests/testpixbuf-color.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / 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_tiff (const gchar *filename, GdkPixbuf *pixbuf, GError **error)
40 {
41         gchar *contents = NULL;
42         gchar *contents_encode = NULL;
43         gsize length;
44         gboolean ret;
45         gint len;
46
47         /* get icc file */
48         ret = g_file_get_contents (ICC_PROFILE, &contents, &length, error);
49         if (!ret)
50                 goto out;
51         contents_encode = g_base64_encode ((const guchar *) contents, length);
52         ret = gdk_pixbuf_save (pixbuf, filename, "tiff", error,
53                                "icc-profile", contents_encode,
54                                NULL);
55         len = strlen (contents_encode);
56         g_debug ("ICC profile was %i bytes", len);
57 out:
58         g_free (contents);
59         g_free (contents_encode);
60         return ret;
61 }
62
63 static gboolean
64 save_image_verify (const gchar *filename, GError **error)
65 {
66         gboolean ret = FALSE;
67         GdkPixbuf *pixbuf = NULL;
68         const gchar *option;
69         gchar *icc_profile = NULL;
70         gsize len = 0;
71
72         /* load */
73         pixbuf = gdk_pixbuf_new_from_file (filename, error);
74         if (pixbuf == NULL)
75                 goto out;
76
77         /* check values */
78         option = gdk_pixbuf_get_option (pixbuf, "icc-profile");
79         if (option == NULL) {
80                 *error = g_error_new (1, 0, "no profile set");
81                 goto out;
82         }
83
84         /* decode base64 */
85         icc_profile = (gchar *) g_base64_decode (option, &len);
86         if (len != ICC_PROFILE_SIZE) {
87                 *error = g_error_new (1, 0,
88                                       "profile length invalid, got %" G_GSIZE_FORMAT,
89                                       len);
90                 g_file_set_contents ("error.icc", icc_profile, len, NULL);
91                 goto out;
92         }
93
94         /* success */
95         ret = TRUE;
96 out:
97         if (pixbuf != NULL)
98                 g_object_unref (pixbuf);
99         g_free (icc_profile);
100         return ret;
101 }
102
103 int
104 main (int argc, char **argv)
105 {
106         GdkWindow *root;
107         GdkPixbuf *pixbuf;
108         gboolean ret;
109         gint retval = 1;
110         GError *error = NULL;
111
112         gtk_init (&argc, &argv);
113
114         root = gdk_get_default_root_window ();
115         pixbuf = gdk_pixbuf_get_from_window (root,
116                                              0, 0, 150, 160);
117
118         /* PASS */
119         g_debug ("try to save PNG with a profile");
120         ret = save_image_png ("icc-profile.png", pixbuf, &error);
121         if (!ret) {
122                 g_warning ("FAILED: did not save image: %s", error->message);
123                 g_error_free (error);
124                 goto out;
125         }
126
127         /* PASS */
128         g_debug ("try to save TIFF with a profile");
129         ret = save_image_tiff ("icc-profile.tiff", pixbuf, &error);
130         if (!ret) {
131                 g_warning ("FAILED: did not save image: %s", error->message);
132                 g_error_free (error);
133                 goto out;
134         }
135
136         /* PASS */
137         g_debug ("try to load PNG and get color attributes");
138         ret = save_image_verify ("icc-profile.png", &error);
139         if (!ret) {
140                 g_warning ("FAILED: did not load image: %s", error->message);
141                 g_error_free (error);
142                 goto out;
143         }
144
145         /* PASS */
146         g_debug ("try to load TIFF and get color attributes");
147         ret = save_image_verify ("icc-profile.tiff", &error);
148         if (!ret) {
149                 g_warning ("FAILED: did not load image: %s", error->message);
150                 g_error_free (error);
151                 goto out;
152         }
153
154         /* success */
155         retval = 0;
156         g_debug ("ALL OKAY!");
157 out:
158         return retval;
159 }