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"
34 no_memory_error (void *closure, unsigned char *data, unsigned int size)
36 return CAIRO_STATUS_NO_MEMORY;
40 read_error (void *closure, unsigned char *data, unsigned int size)
42 return CAIRO_STATUS_READ_ERROR;
45 static cairo_test_status_t
46 draw (cairo_t *cr, int width, int height)
48 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
50 cairo_surface_t *surface;
52 xasprintf (&filename, "%s/reference/%s",
53 ctx->srcdir, "create-from-png.ref.png");
55 surface = cairo_image_surface_create_from_png (filename);
56 if (cairo_surface_status (surface)) {
57 cairo_test_status_t result;
59 result = cairo_test_status_from_status (ctx,
60 cairo_surface_status (surface));
61 if (result == CAIRO_TEST_FAILURE) {
62 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
64 cairo_status_to_string (cairo_surface_status (surface)));
71 /* Pretend we modify the surface data (which detaches the PNG mime data) */
72 cairo_surface_flush (surface);
73 cairo_surface_mark_dirty (surface);
75 cairo_set_source_surface (cr, surface, 0, 0);
76 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
79 cairo_surface_destroy (surface);
82 return CAIRO_TEST_SUCCESS;
85 static cairo_test_status_t
86 preamble (cairo_test_context_t *ctx)
90 cairo_surface_t *surface;
91 cairo_status_t status;
92 cairo_test_status_t result = CAIRO_TEST_SUCCESS;
94 surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
95 if (cairo_surface_status (surface) != CAIRO_STATUS_FILE_NOT_FOUND) {
96 result = cairo_test_status_from_status (ctx,
97 cairo_surface_status (surface));
98 if (result == CAIRO_TEST_FAILURE) {
99 cairo_test_log (ctx, "Error: expected \"file not found\", but got: %s\n",
100 cairo_status_to_string (cairo_surface_status (surface)));
103 cairo_surface_destroy (surface);
104 if (result != CAIRO_TEST_SUCCESS)
107 surface = cairo_image_surface_create_from_png_stream (no_memory_error, NULL);
108 if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
109 result = cairo_test_status_from_status (ctx,
110 cairo_surface_status (surface));
111 if (result == CAIRO_TEST_FAILURE) {
112 cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
113 cairo_status_to_string (cairo_surface_status (surface)));
116 cairo_surface_destroy (surface);
117 if (result != CAIRO_TEST_SUCCESS)
120 surface = cairo_image_surface_create_from_png_stream (read_error, NULL);
121 if (cairo_surface_status (surface) != CAIRO_STATUS_READ_ERROR) {
122 result = cairo_test_status_from_status (ctx,
123 cairo_surface_status (surface));
124 if (result == CAIRO_TEST_FAILURE) {
125 cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
126 cairo_status_to_string (cairo_surface_status (surface)));
129 cairo_surface_destroy (surface);
130 if (result != CAIRO_TEST_SUCCESS)
133 /* cheekily test error propagation from the user write funcs as well ... */
134 xasprintf (&path, "%s/reference", ctx->srcdir);
135 xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
137 surface = cairo_image_surface_create_from_png (filename);
138 if (cairo_surface_status (surface)) {
139 result = cairo_test_status_from_status (ctx,
140 cairo_surface_status (surface));
141 if (result == CAIRO_TEST_FAILURE) {
142 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
144 cairo_status_to_string (cairo_surface_status (surface)));
147 status = cairo_surface_write_to_png_stream (surface,
148 (cairo_write_func_t) no_memory_error,
150 if (status != CAIRO_STATUS_NO_MEMORY) {
151 result = cairo_test_status_from_status (ctx, status);
152 if (result == CAIRO_TEST_FAILURE) {
153 cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
154 cairo_status_to_string (status));
158 status = cairo_surface_write_to_png_stream (surface,
159 (cairo_write_func_t) read_error,
161 if (status != CAIRO_STATUS_READ_ERROR) {
162 result = cairo_test_status_from_status (ctx, status);
163 if (result == CAIRO_TEST_FAILURE) {
164 cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
165 cairo_status_to_string (status));
169 /* and check that error has not propagated to the surface */
170 if (cairo_surface_status (surface)) {
171 result = cairo_test_status_from_status (ctx,
172 cairo_surface_status (surface));
173 if (result == CAIRO_TEST_FAILURE) {
174 cairo_test_log (ctx, "Error: user write error propagated to surface: %s",
175 cairo_status_to_string (cairo_surface_status (surface)));
179 cairo_surface_destroy (surface);
181 if (result != CAIRO_TEST_SUCCESS)
184 /* check that loading alpha/opaque PNGs generate the correct surfaces */
185 xasprintf (&filename, "%s/%s", path, "create-from-png.alpha.ref.png");
186 surface = cairo_image_surface_create_from_png (filename);
187 if (cairo_surface_status (surface)) {
188 result = cairo_test_status_from_status (ctx,
189 cairo_surface_status (surface));
190 if (result == CAIRO_TEST_FAILURE) {
191 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
193 cairo_status_to_string (cairo_surface_status (surface)));
195 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
196 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
198 result = CAIRO_TEST_FAILURE;
201 cairo_surface_destroy (surface);
202 if (result != CAIRO_TEST_SUCCESS)
205 xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
206 surface = cairo_image_surface_create_from_png (filename);
207 if (cairo_surface_status (surface)) {
208 result = cairo_test_status_from_status (ctx,
209 cairo_surface_status (surface));
210 if (result == CAIRO_TEST_FAILURE) {
211 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
213 cairo_status_to_string (cairo_surface_status (surface)));
215 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
216 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
218 result = CAIRO_TEST_FAILURE;
221 cairo_surface_destroy (surface);
222 if (result != CAIRO_TEST_SUCCESS)
225 /* check paletted PNGs */
226 xasprintf (&filename, "%s/%s", path, "create-from-png.indexed-alpha.ref.png");
227 surface = cairo_image_surface_create_from_png (filename);
228 if (cairo_surface_status (surface)) {
229 result = cairo_test_status_from_status (ctx,
230 cairo_surface_status (surface));
231 if (result == CAIRO_TEST_FAILURE) {
232 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
234 cairo_status_to_string (cairo_surface_status (surface)));
236 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
237 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
239 result = CAIRO_TEST_FAILURE;
242 cairo_surface_destroy (surface);
243 if (result != CAIRO_TEST_SUCCESS)
246 xasprintf (&filename, "%s/%s", path, "create-from-png.indexed.ref.png");
247 surface = cairo_image_surface_create_from_png (filename);
248 if (cairo_surface_status (surface)) {
249 result = cairo_test_status_from_status (ctx,
250 cairo_surface_status (surface));
251 if (result == CAIRO_TEST_FAILURE) {
252 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
254 cairo_status_to_string (cairo_surface_status (surface)));
256 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
257 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
259 result = CAIRO_TEST_FAILURE;
262 cairo_surface_destroy (surface);
263 if (result != CAIRO_TEST_SUCCESS)
266 /* check grayscale PNGs */
267 xasprintf (&filename, "%s/%s", path, "create-from-png.gray-alpha.ref.png");
268 surface = cairo_image_surface_create_from_png (filename);
269 if (cairo_surface_status (surface)) {
270 result = cairo_test_status_from_status (ctx,
271 cairo_surface_status (surface));
272 if (result == CAIRO_TEST_FAILURE) {
273 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
275 cairo_status_to_string (cairo_surface_status (surface)));
277 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
278 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
280 result = CAIRO_TEST_FAILURE;
283 cairo_surface_destroy (surface);
284 if (result != CAIRO_TEST_SUCCESS)
287 xasprintf (&filename, "%s/%s", path, "create-from-png.gray.ref.png");
288 surface = cairo_image_surface_create_from_png (filename);
289 if (cairo_surface_status (surface)) {
290 result = cairo_test_status_from_status (ctx,
291 cairo_surface_status (surface));
292 if (result == CAIRO_TEST_FAILURE) {
293 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
295 cairo_status_to_string (cairo_surface_status (surface)));
297 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
298 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
300 result = CAIRO_TEST_FAILURE;
303 cairo_surface_destroy (surface);
310 CAIRO_TEST (create_from_png,
311 "Tests the creation of an image surface from a PNG file",
312 "png", /* keywords */
313 NULL, /* requirements */