* MoonlightTypeConverter.cs: Convert CacheMode's from strings.
[moon.git] / cairo / test / create-from-png.c
blob5ed3f61691bd99be9efdd5e9d43f8e2cbdd46c05
1 /*
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"
28 #include <stdlib.h>
30 #define WIDTH 2
31 #define HEIGHT 2
33 static cairo_test_draw_function_t draw;
35 static const cairo_test_t test = {
36 "create-from-png",
37 "Tests the creation of an image surface from a PNG file",
38 WIDTH, HEIGHT,
39 draw
42 static cairo_status_t
43 no_memory_error (void *closure, unsigned char *data, unsigned int size)
45 return CAIRO_STATUS_NO_MEMORY;
48 static cairo_status_t
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);
58 char *filename;
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",
67 filename,
68 cairo_status_to_string (cairo_surface_status (surface)));
69 free (filename);
70 return CAIRO_TEST_FAILURE;
72 free (filename);
74 cairo_set_source_surface (cr, surface, 0, 0);
75 cairo_paint (cr);
77 cairo_surface_destroy (surface);
79 return CAIRO_TEST_SUCCESS;
82 int
83 main (void)
85 cairo_test_context_t ctx;
86 char *filename;
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",
124 filename,
125 cairo_status_to_string (cairo_surface_status (surface)));
126 result = CAIRO_TEST_FAILURE;
127 } else {
128 status = cairo_surface_write_to_png_stream (surface,
129 (cairo_write_func_t) no_memory_error,
130 NULL);
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,
139 NULL);
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);
154 free (filename);
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",
162 filename,
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",
167 filename);
168 result = CAIRO_TEST_FAILURE;
170 free (filename);
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",
178 filename,
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",
183 filename);
184 result = CAIRO_TEST_FAILURE;
186 free (filename);
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",
195 filename,
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",
200 filename);
201 result = CAIRO_TEST_FAILURE;
203 free (filename);
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",
211 filename,
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",
216 filename);
217 result = CAIRO_TEST_FAILURE;
219 free (filename);
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",
228 filename,
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",
233 filename);
234 result = CAIRO_TEST_FAILURE;
236 free (filename);
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",
244 filename,
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",
249 filename);
250 result = CAIRO_TEST_FAILURE;
252 free (filename);
253 cairo_surface_destroy (surface);
255 cairo_test_fini (&ctx);
257 if (result != CAIRO_TEST_SUCCESS)
258 return result;
260 return cairo_test (&test);