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 "gdk-pixbuf/gdk-pixbuf.h" |
23 | #include "test-common.h" |
24 | #include <string.h> |
25 | #include <glib/gstdio.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 (!pixdata_equal (test: p1, ref: p2, NULL)) |
34 | return FALSE; |
35 | if (compare_option (p1, p2, "Title" ) != 0) |
36 | return FALSE; |
37 | if (compare_option (p1, p2, "Artist" ) != 0) |
38 | return FALSE; |
39 | if (compare_option (p1, p2, "x_hot" ) != 0) |
40 | return FALSE; |
41 | if (compare_option (p1, p2, "y_hot" ) != 0) |
42 | return FALSE; |
43 | if (compare_option (p1, p2, "orientation" ) != 0) |
44 | return FALSE; |
45 | if (compare_option (p1, p2, "icc-profile" ) != 0) |
46 | return FALSE; |
47 | |
48 | return TRUE; |
49 | } |
50 | |
51 | static void |
52 | test_save_roundtrip (void) |
53 | { |
54 | GError *error = NULL; |
55 | GdkPixbuf *ref; |
56 | GdkPixbuf *pixbuf; |
57 | |
58 | if (!format_supported (filename: "png" )) |
59 | { |
60 | g_test_skip (msg: "format not supported" ); |
61 | return; |
62 | } |
63 | |
64 | ref = gdk_pixbuf_new_from_file (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "test-image.png" , NULL), error: &error); |
65 | g_assert_no_error (error); |
66 | |
67 | gdk_pixbuf_save (pixbuf: ref, filename: "pixbuf-save-roundtrip" , type: "png" , error: &error, NULL); |
68 | g_assert_no_error (error); |
69 | |
70 | pixbuf = gdk_pixbuf_new_from_file (filename: "pixbuf-save-roundtrip" , error: &error); |
71 | g_assert_no_error (error); |
72 | |
73 | g_assert (pixbuf_equal (pixbuf, ref)); |
74 | |
75 | g_object_unref (object: pixbuf); |
76 | g_object_unref (object: ref); |
77 | |
78 | g_unlink (filename: "pixbuf-save-roundtrip" ); |
79 | } |
80 | |
81 | static void |
82 | test_save_ico (void) |
83 | { |
84 | GError *error = NULL; |
85 | GdkPixbuf *ref, *ref2; |
86 | GdkPixbuf *pixbuf; |
87 | |
88 | if (!format_supported (filename: "ico" ) || !format_supported (filename: "png" )) |
89 | { |
90 | g_test_skip (msg: "format not supported" ); |
91 | return; |
92 | } |
93 | |
94 | ref = gdk_pixbuf_new_from_file (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "test-image.png" , NULL), error: &error); |
95 | g_assert_no_error (error); |
96 | |
97 | ref2 = gdk_pixbuf_scale_simple (src: ref, dest_width: 256, dest_height: 256, interp_type: GDK_INTERP_NEAREST); |
98 | g_object_unref (object: ref); |
99 | ref = ref2; |
100 | |
101 | gdk_pixbuf_save (pixbuf: ref, filename: "pixbuf-save-roundtrip" , type: "ico" , error: &error, NULL); |
102 | g_assert_no_error (error); |
103 | |
104 | pixbuf = gdk_pixbuf_new_from_file (filename: "pixbuf-save-roundtrip" , error: &error); |
105 | g_assert_no_error (error); |
106 | |
107 | g_assert (pixbuf_equal (pixbuf, ref)); |
108 | |
109 | g_object_unref (object: pixbuf); |
110 | g_object_unref (object: ref); |
111 | |
112 | g_unlink (filename: "pixbuf-save-roundtrip" ); |
113 | } |
114 | |
115 | static void |
116 | test_save_options (void) |
117 | { |
118 | GdkPixbuf *ref; |
119 | GdkPixbuf *pixbuf, *pixbuf2; |
120 | GError *error = NULL; |
121 | |
122 | if (!format_supported (filename: "png" )) |
123 | { |
124 | g_test_skip (msg: "format not supported" ); |
125 | return; |
126 | } |
127 | |
128 | ref = gdk_pixbuf_new (colorspace: GDK_COLORSPACE_RGB, FALSE, bits_per_sample: 8, width: 10, height: 10); |
129 | gdk_pixbuf_fill (pixbuf: ref, pixel: 0xff00ff00); |
130 | |
131 | gdk_pixbuf_save (pixbuf: ref, filename: "pixbuf-save-options" , type: "png" , error: &error, |
132 | "tEXt::option1" , "Some text to transport via option" , |
133 | "tEXt::long-option-name123456789123456789123456789" , "" , |
134 | #ifdef PNG_iTXt_SUPPORTED |
135 | "tEXt::3" , "αβγδ" , |
136 | #endif |
137 | NULL); |
138 | g_assert_no_error (error); |
139 | |
140 | pixbuf = gdk_pixbuf_new_from_file (filename: "pixbuf-save-options" , error: &error); |
141 | g_assert_no_error (error); |
142 | |
143 | g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf, "tEXt::option1" ), ==, "Some text to transport via option" ); |
144 | g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf, "tEXt::long-option-name123456789123456789123456789" ), ==, "" ); |
145 | #ifdef PNG_iTXt_SUPPORTED |
146 | g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf, "tEXt::3" ), ==, "αβγδ" ); |
147 | #endif |
148 | |
149 | pixbuf2 = gdk_pixbuf_copy (pixbuf); |
150 | g_assert_null (gdk_pixbuf_get_option (pixbuf2, "tEXt::option1" )); |
151 | gdk_pixbuf_copy_options (src_pixbuf: pixbuf, dest_pixbuf: pixbuf2); |
152 | g_assert_cmpstr (gdk_pixbuf_get_option (pixbuf2, "tEXt::option1" ), ==, "Some text to transport via option" ); |
153 | g_assert_true (gdk_pixbuf_remove_option (pixbuf2, "tEXt::option1" )); |
154 | g_assert_null (gdk_pixbuf_get_option (pixbuf2, "tEXt::option1" )); |
155 | g_assert_false (gdk_pixbuf_remove_option (pixbuf2, "tEXt::option1" )); |
156 | #ifdef PNG_iTXt_SUPPORTED |
157 | gdk_pixbuf_remove_option (pixbuf2, "tEXt::3" ); |
158 | #endif |
159 | gdk_pixbuf_remove_option (pixbuf: pixbuf2, key: "tEXt::long-option-name123456789123456789123456789" ); |
160 | g_assert_false (gdk_pixbuf_remove_option (pixbuf2, "tEXt::option1" )); |
161 | |
162 | g_object_unref (object: pixbuf2); |
163 | g_object_unref (object: pixbuf); |
164 | g_object_unref (object: ref); |
165 | } |
166 | |
167 | int |
168 | main (int argc, char **argv) |
169 | { |
170 | g_test_init (argc: &argc, argv: &argv, NULL); |
171 | |
172 | g_test_add_func (testpath: "/pixbuf/save/roundtrip" , test_func: test_save_roundtrip); |
173 | g_test_add_func (testpath: "/pixbuf/save/options" , test_func: test_save_options); |
174 | g_test_add_func (testpath: "/pixbuf/save/ico" , test_func: test_save_ico); |
175 | |
176 | return g_test_run (); |
177 | } |
178 | |