2 * Copyright © 2005 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of
9 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Red Hat, Inc. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: Carl Worth <cworth@cworth.org>
26 #include "cairo-test.h"
33 static cairo_test_draw_function_t draw
;
35 static const cairo_test_t test
= {
37 "Tests the creation of an image surface from a PNG file",
43 no_memory_error (void *closure
, unsigned char *data
, unsigned int size
)
45 return CAIRO_STATUS_NO_MEMORY
;
49 read_error (void *closure
, unsigned char *data
, unsigned int size
)
51 return CAIRO_STATUS_READ_ERROR
;
54 static cairo_test_status_t
55 draw (cairo_t
*cr
, int width
, int height
)
57 const cairo_test_context_t
*ctx
= cairo_test_get_context (cr
);
59 cairo_surface_t
*surface
;
61 xasprintf (&filename
, "%s/%s", ctx
->srcdir
,
62 "create-from-png-ref.png");
64 surface
= cairo_image_surface_create_from_png (filename
);
65 if (cairo_surface_status (surface
)) {
66 cairo_test_log (ctx
, "Error reading PNG image %s: %s\n",
68 cairo_status_to_string (cairo_surface_status (surface
)));
70 return CAIRO_TEST_FAILURE
;
74 cairo_set_source_surface (cr
, surface
, 0, 0);
77 cairo_surface_destroy (surface
);
79 return CAIRO_TEST_SUCCESS
;
85 cairo_test_context_t ctx
;
87 cairo_surface_t
*surface
;
88 cairo_status_t status
;
89 cairo_test_status_t result
= CAIRO_TEST_SUCCESS
;
91 cairo_test_init (&ctx
, "create-from-png");
93 surface
= cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
94 if (cairo_surface_status (surface
) != CAIRO_STATUS_FILE_NOT_FOUND
) {
95 cairo_test_log (&ctx
, "Error: expected \"file not found\", but got: %s\n",
96 cairo_status_to_string (cairo_surface_status (surface
)));
97 result
= CAIRO_TEST_FAILURE
;
99 cairo_surface_destroy (surface
);
101 surface
= cairo_image_surface_create_from_png_stream (no_memory_error
, NULL
);
102 if (cairo_surface_status (surface
) != CAIRO_STATUS_NO_MEMORY
) {
103 cairo_test_log (&ctx
, "Error: expected \"out of memory\", but got: %s\n",
104 cairo_status_to_string (cairo_surface_status (surface
)));
105 result
= CAIRO_TEST_FAILURE
;
107 cairo_surface_destroy (surface
);
109 surface
= cairo_image_surface_create_from_png_stream (read_error
, NULL
);
110 if (cairo_surface_status (surface
) != CAIRO_STATUS_READ_ERROR
) {
111 cairo_test_log (&ctx
, "Error: expected \"read error\", but got: %s\n",
112 cairo_status_to_string (cairo_surface_status (surface
)));
113 result
= CAIRO_TEST_FAILURE
;
115 cairo_surface_destroy (surface
);
117 /* cheekily test error propagation from the user write funcs as well ... */
118 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
119 "create-from-png-ref.png");
121 surface
= cairo_image_surface_create_from_png (filename
);
122 if (cairo_surface_status (surface
)) {
123 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
125 cairo_status_to_string (cairo_surface_status (surface
)));
126 result
= CAIRO_TEST_FAILURE
;
128 status
= cairo_surface_write_to_png_stream (surface
,
129 (cairo_write_func_t
) no_memory_error
,
131 if (status
!= CAIRO_STATUS_NO_MEMORY
) {
132 cairo_test_log (&ctx
, "Error: expected \"out of memory\", but got: %s\n",
133 cairo_status_to_string (status
));
134 result
= CAIRO_TEST_FAILURE
;
137 status
= cairo_surface_write_to_png_stream (surface
,
138 (cairo_write_func_t
) read_error
,
140 if (status
!= CAIRO_STATUS_READ_ERROR
) {
141 cairo_test_log (&ctx
, "Error: expected \"read error\", but got: %s\n",
142 cairo_status_to_string (status
));
143 result
= CAIRO_TEST_FAILURE
;
146 /* and check that error has not propagated to the surface */
147 if (cairo_surface_status (surface
)) {
148 cairo_test_log (&ctx
, "Error: user write error propagated to surface: %s",
149 cairo_status_to_string (cairo_surface_status (surface
)));
150 result
= CAIRO_TEST_FAILURE
;
153 cairo_surface_destroy (surface
);
156 /* check that loading alpha/opaque PNGs generate the correct surfaces */
157 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
158 "create-from-png-alpha-ref.png");
159 surface
= cairo_image_surface_create_from_png (filename
);
160 if (cairo_surface_status (surface
)) {
161 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
163 cairo_status_to_string (cairo_surface_status (surface
)));
164 result
= CAIRO_TEST_FAILURE
;
165 } else if (cairo_image_surface_get_format (surface
) != CAIRO_FORMAT_ARGB32
) {
166 cairo_test_log (&ctx
, "Error reading PNG image %s: did not create an ARGB32 image\n",
168 result
= CAIRO_TEST_FAILURE
;
171 cairo_surface_destroy (surface
);
173 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
174 "create-from-png-ref.png");
175 surface
= cairo_image_surface_create_from_png (filename
);
176 if (cairo_surface_status (surface
)) {
177 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
179 cairo_status_to_string (cairo_surface_status (surface
)));
180 result
= CAIRO_TEST_FAILURE
;
181 } else if (cairo_image_surface_get_format (surface
) != CAIRO_FORMAT_RGB24
) {
182 cairo_test_log (&ctx
, "Error reading PNG image %s: did not create an RGB24 image\n",
184 result
= CAIRO_TEST_FAILURE
;
187 cairo_surface_destroy (surface
);
189 /* check paletted PNGs */
190 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
191 "create-from-png-indexed-alpha-ref.png");
192 surface
= cairo_image_surface_create_from_png (filename
);
193 if (cairo_surface_status (surface
)) {
194 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
196 cairo_status_to_string (cairo_surface_status (surface
)));
197 result
= CAIRO_TEST_FAILURE
;
198 } else if (cairo_image_surface_get_format (surface
) != CAIRO_FORMAT_ARGB32
) {
199 cairo_test_log (&ctx
, "Error reading PNG image %s: did not create an ARGB32 image\n",
201 result
= CAIRO_TEST_FAILURE
;
204 cairo_surface_destroy (surface
);
206 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
207 "create-from-png-indexed-ref.png");
208 surface
= cairo_image_surface_create_from_png (filename
);
209 if (cairo_surface_status (surface
)) {
210 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
212 cairo_status_to_string (cairo_surface_status (surface
)));
213 result
= CAIRO_TEST_FAILURE
;
214 } else if (cairo_image_surface_get_format (surface
) != CAIRO_FORMAT_RGB24
) {
215 cairo_test_log (&ctx
, "Error reading PNG image %s: did not create an RGB24 image\n",
217 result
= CAIRO_TEST_FAILURE
;
220 cairo_surface_destroy (surface
);
222 /* check grayscale PNGs */
223 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
224 "create-from-png-gray-alpha-ref.png");
225 surface
= cairo_image_surface_create_from_png (filename
);
226 if (cairo_surface_status (surface
)) {
227 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
229 cairo_status_to_string (cairo_surface_status (surface
)));
230 result
= CAIRO_TEST_FAILURE
;
231 } else if (cairo_image_surface_get_format (surface
) != CAIRO_FORMAT_ARGB32
) {
232 cairo_test_log (&ctx
, "Error reading PNG image %s: did not create an ARGB32 image\n",
234 result
= CAIRO_TEST_FAILURE
;
237 cairo_surface_destroy (surface
);
239 xasprintf (&filename
, "%s/%s", ctx
.srcdir
,
240 "create-from-png-gray-ref.png");
241 surface
= cairo_image_surface_create_from_png (filename
);
242 if (cairo_surface_status (surface
)) {
243 cairo_test_log (&ctx
, "Error reading PNG image %s: %s\n",
245 cairo_status_to_string (cairo_surface_status (surface
)));
246 result
= CAIRO_TEST_FAILURE
;
247 } else if (cairo_image_surface_get_format (surface
) != CAIRO_FORMAT_RGB24
) {
248 cairo_test_log (&ctx
, "Error reading PNG image %s: did not create an RGB24 image\n",
250 result
= CAIRO_TEST_FAILURE
;
253 cairo_surface_destroy (surface
);
255 cairo_test_fini (&ctx
);
257 if (result
!= CAIRO_TEST_SUCCESS
)
260 return cairo_test (&test
);