1 | /* -*- Mode: C; c-basic-offset: 2; -*- */ |
2 | /* GdkPixbuf library - test loaders |
3 | * |
4 | * Copyright (C) 2013 Red Hat, Inc. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | * |
19 | * Author: Matthias Clasen |
20 | */ |
21 | |
22 | #include "config.h" |
23 | #include "gdk-pixbuf/gdk-pixbuf.h" |
24 | #include "test-common.h" |
25 | #include <string.h> |
26 | |
27 | #define compare_option(p1, p2, key) \ |
28 | g_strcmp0 (gdk_pixbuf_get_option (p1, key), gdk_pixbuf_get_option (p2, key)) |
29 | |
30 | static gboolean |
31 | pixbuf_equal (GdkPixbuf *p1, GdkPixbuf *p2) |
32 | { |
33 | if (gdk_pixbuf_get_colorspace (pixbuf: p1) != gdk_pixbuf_get_colorspace (pixbuf: p2)) |
34 | return FALSE; |
35 | if (gdk_pixbuf_get_n_channels (pixbuf: p1) != gdk_pixbuf_get_n_channels (pixbuf: p2)) |
36 | return FALSE; |
37 | if (gdk_pixbuf_get_bits_per_sample (pixbuf: p1) != gdk_pixbuf_get_bits_per_sample (pixbuf: p2)) |
38 | return FALSE; |
39 | if (gdk_pixbuf_get_width (pixbuf: p1) != gdk_pixbuf_get_width (pixbuf: p2)) |
40 | return FALSE; |
41 | if (gdk_pixbuf_get_height (pixbuf: p1) != gdk_pixbuf_get_height (pixbuf: p2)) |
42 | return FALSE; |
43 | if (gdk_pixbuf_get_rowstride (pixbuf: p1) != gdk_pixbuf_get_rowstride (pixbuf: p2)) |
44 | return FALSE; |
45 | if (memcmp (s1: gdk_pixbuf_get_pixels (pixbuf: p1), s2: gdk_pixbuf_get_pixels (pixbuf: p2), |
46 | n: gdk_pixbuf_get_byte_length (pixbuf: p1)) != 0) |
47 | return FALSE; |
48 | if (compare_option (p1, p2, "Title" ) != 0) |
49 | return FALSE; |
50 | if (compare_option (p1, p2, "Artist" ) != 0) |
51 | return FALSE; |
52 | if (compare_option (p1, p2, "x_hot" ) != 0) |
53 | return FALSE; |
54 | if (compare_option (p1, p2, "y_hot" ) != 0) |
55 | return FALSE; |
56 | if (compare_option (p1, p2, "orientation" ) != 0) |
57 | return FALSE; |
58 | if (compare_option (p1, p2, "icc-profile" ) != 0) |
59 | return FALSE; |
60 | |
61 | return TRUE; |
62 | } |
63 | |
64 | static void |
65 | test_stream (gconstpointer data) |
66 | { |
67 | const gchar *filename = data; |
68 | const gchar *path; |
69 | GError *error = NULL; |
70 | GdkPixbuf *pixbuf, *ref; |
71 | GFile *file; |
72 | GInputStream *stream; |
73 | |
74 | if (!format_supported (filename)) |
75 | { |
76 | g_test_skip (msg: "format not supported" ); |
77 | return; |
78 | } |
79 | |
80 | path = g_test_get_filename (file_type: G_TEST_DIST, first_path: filename, NULL); |
81 | ref = gdk_pixbuf_new_from_file (filename: path, error: &error); |
82 | g_assert_no_error (error); |
83 | |
84 | file = g_file_new_for_path (path); |
85 | stream = (GInputStream *)g_file_read (file, NULL, error: &error); |
86 | g_assert_no_error (error); |
87 | |
88 | pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error: &error); |
89 | g_assert_no_error (error); |
90 | g_assert (pixbuf_equal (pixbuf, ref)); |
91 | g_object_unref (object: pixbuf); |
92 | |
93 | g_object_unref (object: stream); |
94 | g_object_unref (object: file); |
95 | g_object_unref (object: ref); |
96 | } |
97 | |
98 | static void |
99 | async_done_cb (GObject *source, GAsyncResult *res, gpointer data) |
100 | { |
101 | GdkPixbuf *ref = data; |
102 | GdkPixbuf *pixbuf; |
103 | GError *error = NULL; |
104 | |
105 | pixbuf = gdk_pixbuf_new_from_stream_finish (async_result: res, error: &error); |
106 | g_assert_no_error (error); |
107 | |
108 | g_assert (pixbuf_equal (pixbuf, ref)); |
109 | |
110 | g_object_unref (object: pixbuf); |
111 | g_object_unref (object: ref); |
112 | } |
113 | |
114 | static void |
115 | test_stream_async (gconstpointer data) |
116 | { |
117 | const gchar *filename = data; |
118 | const gchar *path; |
119 | GError *error = NULL; |
120 | GdkPixbuf *ref; |
121 | gchar *buffer; |
122 | gsize size; |
123 | GInputStream *stream; |
124 | |
125 | if (!format_supported (filename)) |
126 | { |
127 | g_test_skip (msg: "format not supported" ); |
128 | return; |
129 | } |
130 | |
131 | path = g_test_get_filename (file_type: G_TEST_DIST, first_path: filename, NULL); |
132 | ref = gdk_pixbuf_new_from_file (filename: path, error: &error); |
133 | g_assert_no_error (error); |
134 | |
135 | g_file_get_contents (filename: path, contents: &buffer, length: &size, error: &error); |
136 | g_assert_no_error (error); |
137 | |
138 | stream = g_memory_input_stream_new_from_data (data: buffer, len: size, destroy: g_free); |
139 | gdk_pixbuf_new_from_stream_async (stream, NULL, callback: async_done_cb, user_data: ref); |
140 | g_object_unref (object: stream); |
141 | } |
142 | |
143 | static void |
144 | test_stream_at_scale (gconstpointer data) |
145 | { |
146 | const gchar *filename = data; |
147 | const gchar *path; |
148 | GError *error = NULL; |
149 | GdkPixbuf *pixbuf, *ref; |
150 | GFile *file; |
151 | GInputStream *stream; |
152 | |
153 | if (!format_supported (filename)) |
154 | { |
155 | g_test_skip (msg: "format not supported" ); |
156 | return; |
157 | } |
158 | |
159 | path = g_test_get_filename (file_type: G_TEST_DIST, first_path: filename, NULL); |
160 | ref = gdk_pixbuf_new_from_file_at_scale (filename: path, width: 20, height: 30, TRUE, error: &error); |
161 | g_assert_no_error (error); |
162 | |
163 | file = g_file_new_for_path (path); |
164 | stream = (GInputStream *)g_file_read (file, NULL, error: &error); |
165 | g_assert_no_error (error); |
166 | |
167 | pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream, width: 20, height: 30, TRUE, NULL, error: &error); |
168 | g_assert_no_error (error); |
169 | g_assert (pixbuf_equal (pixbuf, ref)); |
170 | g_object_unref (object: pixbuf); |
171 | |
172 | g_object_unref (object: stream); |
173 | g_object_unref (object: file); |
174 | g_object_unref (object: ref); |
175 | } |
176 | |
177 | static void |
178 | test_stream_at_scale_async (gconstpointer data) |
179 | { |
180 | const gchar *filename = data; |
181 | const gchar *path; |
182 | GError *error = NULL; |
183 | GdkPixbuf *ref; |
184 | gchar *buffer; |
185 | gsize size; |
186 | GInputStream *stream; |
187 | |
188 | if (!format_supported (filename)) |
189 | { |
190 | g_test_skip (msg: "format not supported" ); |
191 | return; |
192 | } |
193 | |
194 | path = g_test_get_filename (file_type: G_TEST_DIST, first_path: filename, NULL); |
195 | ref = gdk_pixbuf_new_from_file_at_scale (filename: path, width: 40, height: 10, FALSE, error: &error); |
196 | g_assert_no_error (error); |
197 | |
198 | g_file_get_contents (filename: path, contents: &buffer, length: &size, error: &error); |
199 | g_assert_no_error (error); |
200 | |
201 | stream = g_memory_input_stream_new_from_data (data: buffer, len: size, destroy: g_free); |
202 | gdk_pixbuf_new_from_stream_at_scale_async (stream, width: 40, height: 10, FALSE, NULL, callback: async_done_cb, user_data: ref); |
203 | g_object_unref (object: stream); |
204 | } |
205 | |
206 | int |
207 | main (int argc, char **argv) |
208 | { |
209 | g_test_init (argc: &argc, argv: &argv, NULL); |
210 | |
211 | g_test_add_data_func (testpath: "/pixbuf/stream" , test_data: "icc-profile.png" , test_func: test_stream); |
212 | g_test_add_data_func (testpath: "/pixbuf/stream/async" , test_data: "icc-profile.png" , test_func: test_stream_async); |
213 | g_test_add_data_func (testpath: "/pixbuf/stream/scale" , test_data: "icc-profile.png" , test_func: test_stream_at_scale); |
214 | g_test_add_data_func (testpath: "/pixbuf/stream/scale/async" , test_data: "icc-profile.png" , test_func: test_stream_at_scale_async); |
215 | |
216 | return g_test_run (); |
217 | } |
218 | |