1 #include <gtk.h>
2 
3 static gboolean
compare_pixels(int width,int height,guchar * data1,gsize stride1,guchar * data2,gsize stride2)4 compare_pixels (int     width,
5                 int     height,
6                 guchar *data1,
7                 gsize   stride1,
8                 guchar *data2,
9                 gsize   stride2)
10 {
11   int i;
12   for (i = 0; i < height; i++)
13     {
14       gconstpointer p1 = data1 + i * stride1;
15       gconstpointer p2 = data2 + i * stride2;
16       if (memcmp (p1, p2, width * 4) != 0)
17         return FALSE;
18     }
19   return TRUE;
20 }
21 
22 static void
test_texture_from_pixbuf(void)23 test_texture_from_pixbuf (void)
24 {
25   GdkPixbuf *pixbuf;
26   GdkTexture *texture;
27   GError *error = NULL;
28   guchar *data;
29   int width, height;
30   gsize stride;
31   cairo_surface_t *surface;
32   cairo_t *cr;
33 
34   pixbuf = gdk_pixbuf_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png", &error);
35   g_assert_no_error (error);
36   g_assert_nonnull (pixbuf);
37   g_assert_true (gdk_pixbuf_get_has_alpha (pixbuf));
38 
39   width = gdk_pixbuf_get_width (pixbuf);
40   height = gdk_pixbuf_get_height (pixbuf);
41 
42   texture = gdk_texture_new_for_pixbuf (pixbuf);
43 
44   g_assert_nonnull (texture);
45   g_assert_cmpint (gdk_texture_get_width (texture), ==, width);
46   g_assert_cmpint (gdk_texture_get_height (texture), ==, height);
47 
48   stride = 4 * width;
49   data = g_new0 (guchar, stride * height);
50   gdk_texture_download (texture, data, stride);
51 
52   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
53   cr = cairo_create (surface);
54   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
55   cairo_paint (cr);
56   cairo_destroy (cr);
57 
58   g_assert_true (compare_pixels (width, height,
59                                  data, stride,
60                                  cairo_image_surface_get_data (surface),
61                                  cairo_image_surface_get_stride (surface)));
62 
63   g_free (data);
64 
65   g_object_unref (pixbuf);
66   g_object_unref (texture);
67   cairo_surface_destroy (surface);
68 }
69 
70 static void
test_texture_from_resource(void)71 test_texture_from_resource (void)
72 {
73   GdkTexture *texture;
74   int width, height;
75 
76   texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png");
77 
78   g_assert_nonnull (texture);
79   g_object_get (texture,
80                 "width", &width,
81                 "height", &height,
82                 NULL);
83   g_assert_cmpint (width, ==, 16);
84   g_assert_cmpint (height, ==, 16);
85 
86   g_object_unref (texture);
87 }
88 
89 static void
test_texture_save_to_png(void)90 test_texture_save_to_png (void)
91 {
92   GdkTexture *texture;
93   GError *error = NULL;
94   GFile *file;
95   GdkTexture *texture2;
96 
97   texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png");
98 
99   gdk_texture_save_to_png (texture, "test.png");
100   file = g_file_new_for_path ("test.png");
101   texture2 = gdk_texture_new_from_file (file, &error);
102   g_object_unref (file);
103   g_assert_no_error (error);
104 
105   g_object_unref (texture);
106   g_object_unref (texture2);
107 }
108 
109 int
main(int argc,char * argv[])110 main (int argc, char *argv[])
111 {
112   /* We want to use resources from libgtk, so we need gtk initialized */
113   gtk_test_init (&argc, &argv, NULL);
114 
115   g_test_add_func ("/texture/from-pixbuf", test_texture_from_pixbuf);
116   g_test_add_func ("/texture/from-resource", test_texture_from_resource);
117   g_test_add_func ("/texture/save-to-png", test_texture_save_to_png);
118 
119   return g_test_run ();
120 }
121