Viewport clipping now works with both GL and Gallium.
[cairo/gpu.git] / test / create-from-png.c
blob585fac2c10d0dff93e484542a83430f95232849a
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_status_t
34 no_memory_error (void *closure, unsigned char *data, unsigned int size)
36 return CAIRO_STATUS_NO_MEMORY;
39 static cairo_status_t
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);
49 char *filename;
50 cairo_surface_t *surface;
52 xasprintf (&filename, "%s/%s", ctx->srcdir,
53 "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",
63 filename,
64 cairo_status_to_string (cairo_surface_status (surface)));
67 free (filename);
68 return result;
71 cairo_set_source_surface (cr, surface, 0, 0);
72 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
73 cairo_paint (cr);
75 cairo_surface_destroy (surface);
77 free (filename);
78 return CAIRO_TEST_SUCCESS;
81 static cairo_test_status_t
82 preamble (cairo_test_context_t *ctx)
84 char *filename;
85 cairo_surface_t *surface;
86 cairo_status_t status;
87 cairo_test_status_t result = CAIRO_TEST_SUCCESS;
89 surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
90 if (cairo_surface_status (surface) != CAIRO_STATUS_FILE_NOT_FOUND) {
91 result = cairo_test_status_from_status (ctx,
92 cairo_surface_status (surface));
93 if (result == CAIRO_TEST_FAILURE) {
94 cairo_test_log (ctx, "Error: expected \"file not found\", but got: %s\n",
95 cairo_status_to_string (cairo_surface_status (surface)));
98 cairo_surface_destroy (surface);
99 if (result != CAIRO_TEST_SUCCESS)
100 return result;
102 surface = cairo_image_surface_create_from_png_stream (no_memory_error, NULL);
103 if (cairo_surface_status (surface) != CAIRO_STATUS_NO_MEMORY) {
104 result = cairo_test_status_from_status (ctx,
105 cairo_surface_status (surface));
106 if (result == CAIRO_TEST_FAILURE) {
107 cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
108 cairo_status_to_string (cairo_surface_status (surface)));
111 cairo_surface_destroy (surface);
112 if (result != CAIRO_TEST_SUCCESS)
113 return result;
115 surface = cairo_image_surface_create_from_png_stream (read_error, NULL);
116 if (cairo_surface_status (surface) != CAIRO_STATUS_READ_ERROR) {
117 result = cairo_test_status_from_status (ctx,
118 cairo_surface_status (surface));
119 if (result == CAIRO_TEST_FAILURE) {
120 cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
121 cairo_status_to_string (cairo_surface_status (surface)));
124 cairo_surface_destroy (surface);
125 if (result != CAIRO_TEST_SUCCESS)
126 return result;
128 /* cheekily test error propagation from the user write funcs as well ... */
129 xasprintf (&filename, "%s/%s", ctx->srcdir,
130 "create-from-png.ref.png");
132 surface = cairo_image_surface_create_from_png (filename);
133 if (cairo_surface_status (surface)) {
134 result = cairo_test_status_from_status (ctx,
135 cairo_surface_status (surface));
136 if (result == CAIRO_TEST_FAILURE) {
137 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
138 filename,
139 cairo_status_to_string (cairo_surface_status (surface)));
141 } else {
142 status = cairo_surface_write_to_png_stream (surface,
143 (cairo_write_func_t) no_memory_error,
144 NULL);
145 if (status != CAIRO_STATUS_NO_MEMORY) {
146 result = cairo_test_status_from_status (ctx, status);
147 if (result == CAIRO_TEST_FAILURE) {
148 cairo_test_log (ctx, "Error: expected \"out of memory\", but got: %s\n",
149 cairo_status_to_string (status));
153 status = cairo_surface_write_to_png_stream (surface,
154 (cairo_write_func_t) read_error,
155 NULL);
156 if (status != CAIRO_STATUS_READ_ERROR) {
157 result = cairo_test_status_from_status (ctx, status);
158 if (result == CAIRO_TEST_FAILURE) {
159 cairo_test_log (ctx, "Error: expected \"read error\", but got: %s\n",
160 cairo_status_to_string (status));
164 /* and check that error has not propagated to the surface */
165 if (cairo_surface_status (surface)) {
166 result = cairo_test_status_from_status (ctx,
167 cairo_surface_status (surface));
168 if (result == CAIRO_TEST_FAILURE) {
169 cairo_test_log (ctx, "Error: user write error propagated to surface: %s",
170 cairo_status_to_string (cairo_surface_status (surface)));
174 cairo_surface_destroy (surface);
175 free (filename);
176 if (result != CAIRO_TEST_SUCCESS)
177 return result;
179 /* check that loading alpha/opaque PNGs generate the correct surfaces */
180 xasprintf (&filename, "%s/%s", ctx->srcdir,
181 "create-from-png.alpha.ref.png");
182 surface = cairo_image_surface_create_from_png (filename);
183 if (cairo_surface_status (surface)) {
184 result = cairo_test_status_from_status (ctx,
185 cairo_surface_status (surface));
186 if (result == CAIRO_TEST_FAILURE) {
187 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
188 filename,
189 cairo_status_to_string (cairo_surface_status (surface)));
191 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
192 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
193 filename);
194 result = CAIRO_TEST_FAILURE;
196 free (filename);
197 cairo_surface_destroy (surface);
198 if (result != CAIRO_TEST_SUCCESS)
199 return result;
201 xasprintf (&filename, "%s/%s", ctx->srcdir,
202 "create-from-png.ref.png");
203 surface = cairo_image_surface_create_from_png (filename);
204 if (cairo_surface_status (surface)) {
205 result = cairo_test_status_from_status (ctx,
206 cairo_surface_status (surface));
207 if (result == CAIRO_TEST_FAILURE) {
208 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
209 filename,
210 cairo_status_to_string (cairo_surface_status (surface)));
212 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
213 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
214 filename);
215 result = CAIRO_TEST_FAILURE;
217 free (filename);
218 cairo_surface_destroy (surface);
219 if (result != CAIRO_TEST_SUCCESS)
220 return result;
222 /* check paletted PNGs */
223 xasprintf (&filename, "%s/%s", ctx->srcdir,
224 "create-from-png.indexed-alpha.ref.png");
225 surface = cairo_image_surface_create_from_png (filename);
226 if (cairo_surface_status (surface)) {
227 result = cairo_test_status_from_status (ctx,
228 cairo_surface_status (surface));
229 if (result == CAIRO_TEST_FAILURE) {
230 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
231 filename,
232 cairo_status_to_string (cairo_surface_status (surface)));
234 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_ARGB32) {
235 cairo_test_log (ctx, "Error reading PNG image %s: did not create an ARGB32 image\n",
236 filename);
237 result = CAIRO_TEST_FAILURE;
239 free (filename);
240 cairo_surface_destroy (surface);
241 if (result != CAIRO_TEST_SUCCESS)
242 return result;
244 xasprintf (&filename, "%s/%s", ctx->srcdir,
245 "create-from-png.indexed.ref.png");
246 surface = cairo_image_surface_create_from_png (filename);
247 if (cairo_surface_status (surface)) {
248 result = cairo_test_status_from_status (ctx,
249 cairo_surface_status (surface));
250 if (result == CAIRO_TEST_FAILURE) {
251 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
252 filename,
253 cairo_status_to_string (cairo_surface_status (surface)));
255 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
256 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
257 filename);
258 result = CAIRO_TEST_FAILURE;
260 free (filename);
261 cairo_surface_destroy (surface);
262 if (result != CAIRO_TEST_SUCCESS)
263 return result;
265 /* check grayscale PNGs */
266 xasprintf (&filename, "%s/%s", ctx->srcdir,
267 "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)));
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;
282 free (filename);
283 cairo_surface_destroy (surface);
284 if (result != CAIRO_TEST_SUCCESS)
285 return result;
287 xasprintf (&filename, "%s/%s", ctx->srcdir,
288 "create-from-png.gray.ref.png");
289 surface = cairo_image_surface_create_from_png (filename);
290 if (cairo_surface_status (surface)) {
291 result = cairo_test_status_from_status (ctx,
292 cairo_surface_status (surface));
293 if (result == CAIRO_TEST_FAILURE) {
294 cairo_test_log (ctx, "Error reading PNG image %s: %s\n",
295 filename,
296 cairo_status_to_string (cairo_surface_status (surface)));
298 } else if (cairo_image_surface_get_format (surface) != CAIRO_FORMAT_RGB24) {
299 cairo_test_log (ctx, "Error reading PNG image %s: did not create an RGB24 image\n",
300 filename);
301 result = CAIRO_TEST_FAILURE;
303 free (filename);
304 cairo_surface_destroy (surface);
306 return result;
309 CAIRO_TEST (create_from_png,
310 "Tests the creation of an image surface from a PNG file",
311 "png", /* keywords */
312 NULL, /* requirements */
313 WIDTH, HEIGHT,
314 preamble, draw)