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 /* TODO: Avoid using target-specific references as sample images */
186 xasprintf (&filename, "%s/%s", path, "create-from-png.alpha.ref.png");
187 surface = cairo_image_surface_create_from_png (filename);
188 if (cairo_surface_status (surface)) {
189 result = cairo_test_status_from_status (ctx,
190 cairo_surface_status (surface));
191 if (result == CAIRO_TEST_FAILURE) {
192 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
194 cairo_status_to_string (cairo_surface_status (surface)));
196 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
197 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
199 result = CAIRO_TEST_FAILURE;
202 cairo_surface_destroy (surface);
203 if (result != CAIRO_TEST_SUCCESS)
206 xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
207 surface = cairo_image_surface_create_from_png (filename);
208 if (cairo_surface_status (surface)) {
209 result = cairo_test_status_from_status (ctx,
210 cairo_surface_status (surface));
211 if (result == CAIRO_TEST_FAILURE) {
212 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
214 cairo_status_to_string (cairo_surface_status (surface)));
216 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
217 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
219 result = CAIRO_TEST_FAILURE;
222 cairo_surface_destroy (surface);
223 if (result != CAIRO_TEST_SUCCESS)
226 /* check paletted PNGs */
227 /* TODO: Avoid using target-specific references as sample images */
228 xasprintf (&filename, "%s/%s", path, "create-from-png.indexed-alpha.ref.png");
229 surface = cairo_image_surface_create_from_png (filename);
230 if (cairo_surface_status (surface)) {
231 result = cairo_test_status_from_status (ctx,
232 cairo_surface_status (surface));
233 if (result == CAIRO_TEST_FAILURE) {
234 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
236 cairo_status_to_string (cairo_surface_status (surface)));
238 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
239 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
241 result = CAIRO_TEST_FAILURE;
244 cairo_surface_destroy (surface);
245 if (result != CAIRO_TEST_SUCCESS)
248 /* TODO: Avoid using target-specific references as sample images */
249 xasprintf (&filename, "%s/%s", path, "create-from-png.indexed.ref.png");
250 surface = cairo_image_surface_create_from_png (filename);
251 if (cairo_surface_status (surface)) {
252 result = cairo_test_status_from_status (ctx,
253 cairo_surface_status (surface));
254 if (result == CAIRO_TEST_FAILURE) {
255 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
257 cairo_status_to_string (cairo_surface_status (surface)));
259 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
260 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
262 result = CAIRO_TEST_FAILURE;
265 cairo_surface_destroy (surface);
266 if (result != CAIRO_TEST_SUCCESS)
269 /* check grayscale PNGs */
270 /* TODO: Avoid using target-specific references as sample images */
271 xasprintf (&filename, "%s/%s", path, "create-from-png.gray-alpha.ref.png");
272 surface = cairo_image_surface_create_from_png (filename);
273 if (cairo_surface_status (surface)) {
274 result = cairo_test_status_from_status (ctx,
275 cairo_surface_status (surface));
276 if (result == CAIRO_TEST_FAILURE) {
277 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
279 cairo_status_to_string (cairo_surface_status (surface)));
281 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
282 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
284 result = CAIRO_TEST_FAILURE;
287 cairo_surface_destroy (surface);
288 if (result != CAIRO_TEST_SUCCESS)
291 /* TODO: Avoid using target-specific references as sample images */
292 xasprintf (&filename, "%s/%s", path, "create-from-png.gray.ref.png");
293 surface = cairo_image_surface_create_from_png (filename);
294 if (cairo_surface_status (surface)) {
295 result = cairo_test_status_from_status (ctx,
296 cairo_surface_status (surface));
297 if (result == CAIRO_TEST_FAILURE) {
298 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
300 cairo_status_to_string (cairo_surface_status (surface)));
302 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
303 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
305 result = CAIRO_TEST_FAILURE;
308 cairo_surface_destroy (surface);
315 CAIRO_TEST (create_from_png,
316 "Tests the creation of an image surface from a PNG file",
317 "png", /* keywords */
318 NULL, /* requirements */