Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / create-from-png.c
1 /*
2  * Copyright © 2005 Red Hat, Inc.
3  *
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.
14  *
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.
22  *
23  * Author: Carl Worth <cworth@cworth.org>
24  */
25
26 #include "cairo-test.h"
27
28 #include <stdlib.h>
29
30 #define WIDTH 2
31 #define HEIGHT 2
32
33 static cairo_status_t
34 no_memory_error (void *closure, unsigned char *data, unsigned int size)
35 {
36     return CAIRO_STATUS_NO_MEMORY;
37 }
38
39 static cairo_status_t
40 read_error (void *closure, unsigned char *data, unsigned int size)
41 {
42     return CAIRO_STATUS_READ_ERROR;
43 }
44
45 static cairo_test_status_t
46 draw (cairo_t *cr, int width, int height)
47 {
48     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
49     char *filename;
50     cairo_surface_t *surface;
51
52     xasprintf (&filename, "%s/reference/%s",
53                ctx->srcdir, "create-from-png.ref.png");
54
55     surface = cairo_image_surface_create_from_png (filename);
56     if (cairo_surface_status (surface)) {
57         cairo_test_status_t result;
58
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",
63                             filename,
64                             cairo_status_to_string (cairo_surface_status (surface)));
65         }
66
67         free (filename);
68         return result;
69     }
70
71     /* Pretend we modify the surface data (which detaches the PNG mime data) */
72     cairo_surface_flush (surface);
73     cairo_surface_mark_dirty (surface);
74
75     cairo_set_source_surface (cr, surface, 0, 0);
76     cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
77     cairo_paint (cr);
78
79     cairo_surface_destroy (surface);
80
81     free (filename);
82     return CAIRO_TEST_SUCCESS;
83 }
84
85 static cairo_test_status_t
86 preamble (cairo_test_context_t *ctx)
87 {
88     char *filename;
89     char *path;
90     cairo_surface_t *surface;
91     cairo_status_t status;
92     cairo_test_status_t result = CAIRO_TEST_SUCCESS;
93
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)));
101         }
102     }
103     cairo_surface_destroy (surface);
104     if (result != CAIRO_TEST_SUCCESS)
105         return result;
106
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)));
114         }
115     }
116     cairo_surface_destroy (surface);
117     if (result != CAIRO_TEST_SUCCESS)
118         return result;
119
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)));
127         }
128     }
129     cairo_surface_destroy (surface);
130     if (result != CAIRO_TEST_SUCCESS)
131         return result;
132
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");
136
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",
143                             filename,
144                             cairo_status_to_string (cairo_surface_status (surface)));
145         }
146     } else {
147         status = cairo_surface_write_to_png_stream (surface,
148                                                (cairo_write_func_t) no_memory_error,
149                                                NULL);
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));
155             }
156         }
157
158         status = cairo_surface_write_to_png_stream (surface,
159                                                     (cairo_write_func_t) read_error,
160                                                     NULL);
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));
166             }
167         }
168
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)));
176             }
177         }
178     }
179     cairo_surface_destroy (surface);
180     free (filename);
181     if (result != CAIRO_TEST_SUCCESS)
182         return result;
183
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",
192                             filename,
193                             cairo_status_to_string (cairo_surface_status (surface)));
194         }
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",
197                         filename);
198         result = CAIRO_TEST_FAILURE;
199     }
200     free (filename);
201     cairo_surface_destroy (surface);
202     if (result != CAIRO_TEST_SUCCESS)
203         return result;
204
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",
212                             filename,
213                             cairo_status_to_string (cairo_surface_status (surface)));
214         }
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",
217                         filename);
218         result = CAIRO_TEST_FAILURE;
219     }
220     free (filename);
221     cairo_surface_destroy (surface);
222     if (result != CAIRO_TEST_SUCCESS)
223         return result;
224
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",
233                             filename,
234                             cairo_status_to_string (cairo_surface_status (surface)));
235         }
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",
238                         filename);
239         result = CAIRO_TEST_FAILURE;
240     }
241     free (filename);
242     cairo_surface_destroy (surface);
243     if (result != CAIRO_TEST_SUCCESS)
244         return result;
245
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",
253                             filename,
254                             cairo_status_to_string (cairo_surface_status (surface)));
255         }
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",
258                         filename);
259         result = CAIRO_TEST_FAILURE;
260     }
261     free (filename);
262     cairo_surface_destroy (surface);
263     if (result != CAIRO_TEST_SUCCESS)
264         return result;
265
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",
274                             filename,
275                             cairo_status_to_string (cairo_surface_status (surface)));
276         }
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",
279                         filename);
280         result = CAIRO_TEST_FAILURE;
281     }
282     free (filename);
283     cairo_surface_destroy (surface);
284     if (result != CAIRO_TEST_SUCCESS)
285         return result;
286
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",
294                             filename,
295                             cairo_status_to_string (cairo_surface_status (surface)));
296         }
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",
299                         filename);
300         result = CAIRO_TEST_FAILURE;
301     }
302     free (filename);
303     cairo_surface_destroy (surface);
304
305     free (path);
306
307     return result;
308 }
309
310 CAIRO_TEST (create_from_png,
311             "Tests the creation of an image surface from a PNG file",
312             "png", /* keywords */
313             NULL, /* requirements */
314             WIDTH, HEIGHT,
315             preamble, draw)