1 | #include <gtk/gtk.h> |
2 | #include "gdk/loaders/gdkpngprivate.h" |
3 | #include "gdk/loaders/gdktiffprivate.h" |
4 | #include "gdk/loaders/gdkjpegprivate.h" |
5 | |
6 | static void |
7 | assert_texture_equal (GdkTexture *t1, |
8 | GdkTexture *t2) |
9 | { |
10 | int width; |
11 | int height; |
12 | int stride; |
13 | guchar *d1; |
14 | guchar *d2; |
15 | |
16 | width = gdk_texture_get_width (texture: t1); |
17 | height = gdk_texture_get_height (texture: t1); |
18 | stride = 4 * width; |
19 | |
20 | g_assert_cmpint (width, ==, gdk_texture_get_width (t2)); |
21 | g_assert_cmpint (height, ==, gdk_texture_get_height (t2)); |
22 | |
23 | d1 = g_malloc (n_bytes: stride * height); |
24 | d2 = g_malloc (n_bytes: stride * height); |
25 | |
26 | gdk_texture_download (texture: t1, data: d1, stride); |
27 | gdk_texture_download (texture: t2, data: d2, stride); |
28 | |
29 | g_assert_cmpmem (d1, stride * height, d2, stride * height); |
30 | |
31 | g_free (mem: d1); |
32 | g_free (mem: d2); |
33 | } |
34 | |
35 | static void |
36 | test_load_image (gconstpointer data) |
37 | { |
38 | const char *filename = data; |
39 | GdkTexture *texture; |
40 | char *path; |
41 | GFile *file; |
42 | GBytes *bytes; |
43 | GError *error = NULL; |
44 | |
45 | path = g_test_build_filename (file_type: G_TEST_DIST, first_path: "image-data" , filename, NULL); |
46 | file = g_file_new_for_path (path); |
47 | bytes = g_file_load_bytes (file, NULL, NULL, error: &error); |
48 | g_assert_no_error (error); |
49 | |
50 | if (g_str_has_suffix (str: filename, suffix: ".png" )) |
51 | texture = gdk_load_png (bytes, error: &error); |
52 | else if (g_str_has_suffix (str: filename, suffix: ".tiff" )) |
53 | texture = gdk_load_tiff (bytes, error: &error); |
54 | else if (g_str_has_suffix (str: filename, suffix: ".jpeg" )) |
55 | texture = gdk_load_jpeg (bytes, error: &error); |
56 | else |
57 | g_assert_not_reached (); |
58 | |
59 | g_assert_no_error (error); |
60 | g_assert_true (GDK_IS_TEXTURE (texture)); |
61 | g_assert_cmpint (gdk_texture_get_width (texture), ==, 32); |
62 | g_assert_cmpint (gdk_texture_get_height (texture), ==, 32); |
63 | |
64 | g_object_unref (object: texture); |
65 | g_bytes_unref (bytes); |
66 | g_object_unref (object: file); |
67 | g_free (mem: path); |
68 | } |
69 | |
70 | static void |
71 | test_save_image (gconstpointer test_data) |
72 | { |
73 | const char *filename = test_data; |
74 | char *path; |
75 | GFile *file; |
76 | GdkTexture *texture; |
77 | GFile *file2; |
78 | GdkTexture *texture2; |
79 | GError *error = NULL; |
80 | GBytes *bytes = NULL; |
81 | GIOStream *stream; |
82 | |
83 | path = g_test_build_filename (file_type: G_TEST_DIST, first_path: "image-data" , filename, NULL); |
84 | file = g_file_new_for_path (path); |
85 | texture = gdk_texture_new_from_file (file, error: &error); |
86 | g_assert_no_error (error); |
87 | |
88 | if (g_str_has_suffix (str: filename, suffix: ".png" )) |
89 | bytes = gdk_save_png (texture); |
90 | else if (g_str_has_suffix (str: filename, suffix: ".tiff" )) |
91 | bytes = gdk_save_tiff (texture); |
92 | else |
93 | g_assert_not_reached (); |
94 | |
95 | file2 = g_file_new_tmp (tmpl: "imageXXXXXX" , iostream: (GFileIOStream **)&stream, NULL); |
96 | g_object_unref (object: stream); |
97 | g_file_replace_contents (file: file2, |
98 | contents: g_bytes_get_data (bytes, NULL), |
99 | length: g_bytes_get_size (bytes), |
100 | NULL, FALSE, flags: 0, |
101 | NULL, NULL, error: &error); |
102 | g_assert_no_error (error); |
103 | |
104 | texture2 = gdk_texture_new_from_file (file: file2, error: &error); |
105 | g_assert_no_error (error); |
106 | |
107 | assert_texture_equal (t1: texture, t2: texture2); |
108 | |
109 | g_bytes_unref (bytes); |
110 | g_object_unref (object: texture2); |
111 | g_object_unref (object: file2); |
112 | g_object_unref (object: texture); |
113 | g_object_unref (object: file); |
114 | g_free (mem: path); |
115 | } |
116 | |
117 | int |
118 | main (int argc, char *argv[]) |
119 | { |
120 | (g_test_init) (argc: &argc, argv: &argv, NULL); |
121 | |
122 | g_test_add_data_func (testpath: "/image/load/png" , test_data: "image.png" , test_func: test_load_image); |
123 | g_test_add_data_func (testpath: "/image/load/tiff" , test_data: "image.tiff" , test_func: test_load_image); |
124 | g_test_add_data_func (testpath: "/image/load/jpeg" , test_data: "image.jpeg" , test_func: test_load_image); |
125 | g_test_add_data_func (testpath: "/image/save/png" , test_data: "image.png" , test_func: test_save_image); |
126 | g_test_add_data_func (testpath: "/image/save/tiff" , test_data: "image.tiff" , test_func: test_save_image); |
127 | |
128 | return g_test_run (); |
129 | } |
130 | |