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