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
2
no_memory_error (void *closure, unsigned char *data, unsigned int size)
35
{
36
2
    return CAIRO_STATUS_NO_MEMORY;
37
}
38

            
39
static cairo_status_t
40
2
read_error (void *closure, unsigned char *data, unsigned int size)
41
{
42
2
    return CAIRO_STATUS_READ_ERROR;
43
}
44

            
45
static cairo_test_status_t
46
3
draw (cairo_t *cr, int width, int height)
47
{
48
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
49
    char *filename;
50
    cairo_surface_t *surface;
51

            
52
3
    xasprintf (&filename, "%s/reference/%s",
53
3
	       ctx->srcdir, "create-from-png.ref.png");
54

            
55
3
    surface = cairo_image_surface_create_from_png (filename);
56
3
    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
3
    cairo_surface_flush (surface);
73
3
    cairo_surface_mark_dirty (surface);
74

            
75
3
    cairo_set_source_surface (cr, surface, 0, 0);
76
3
    cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
77
3
    cairo_paint (cr);
78

            
79
3
    cairo_surface_destroy (surface);
80

            
81
3
    free (filename);
82
3
    return CAIRO_TEST_SUCCESS;
83
}
84

            
85
static cairo_test_status_t
86
1
preamble (cairo_test_context_t *ctx)
87
{
88
    char *filename;
89
    char *path;
90
    cairo_surface_t *surface;
91
    cairo_status_t status;
92
1
    cairo_test_status_t result = CAIRO_TEST_SUCCESS;
93

            
94
1
    surface = cairo_image_surface_create_from_png ("___THIS_FILE_DOES_NOT_EXIST___");
95
1
    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
1
    cairo_surface_destroy (surface);
104
1
    if (result != CAIRO_TEST_SUCCESS)
105
	return result;
106

            
107
1
    surface = cairo_image_surface_create_from_png_stream (no_memory_error, NULL);
108
1
    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
1
    cairo_surface_destroy (surface);
117
1
    if (result != CAIRO_TEST_SUCCESS)
118
	return result;
119

            
120
1
    surface = cairo_image_surface_create_from_png_stream (read_error, NULL);
121
1
    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
1
    cairo_surface_destroy (surface);
130
1
    if (result != CAIRO_TEST_SUCCESS)
131
	return result;
132

            
133
    /* cheekily test error propagation from the user write funcs as well ... */
134
1
    xasprintf (&path, "%s/reference", ctx->srcdir);
135
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
136

            
137
1
    surface = cairo_image_surface_create_from_png (filename);
138
1
    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
1
	status = cairo_surface_write_to_png_stream (surface,
148
					       (cairo_write_func_t) no_memory_error,
149
					       NULL);
150
1
	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
1
	status = cairo_surface_write_to_png_stream (surface,
159
						    (cairo_write_func_t) read_error,
160
						    NULL);
161
1
	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
1
	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
1
    cairo_surface_destroy (surface);
180
1
    free (filename);
181
1
    if (result != CAIRO_TEST_SUCCESS)
182
	return result;
183

            
184
    /* check that loading alpha/opaque PNGs generate the correct surfaces */
185
    /* TODO: Avoid using target-specific references as sample images */
186
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.alpha.ref.png");
187
1
    surface = cairo_image_surface_create_from_png (filename);
188
1
    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",
193
			    filename,
194
			    cairo_status_to_string (cairo_surface_status (surface)));
195
	}
196
1
    } 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",
198
			filename);
199
	result = CAIRO_TEST_FAILURE;
200
    }
201
1
    free (filename);
202
1
    cairo_surface_destroy (surface);
203
1
    if (result != CAIRO_TEST_SUCCESS)
204
	return result;
205

            
206
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.ref.png");
207
1
    surface = cairo_image_surface_create_from_png (filename);
208
1
    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",
213
			    filename,
214
			    cairo_status_to_string (cairo_surface_status (surface)));
215
	}
216
1
    } 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",
218
			filename);
219
	result = CAIRO_TEST_FAILURE;
220
    }
221
1
    free (filename);
222
1
    cairo_surface_destroy (surface);
223
1
    if (result != CAIRO_TEST_SUCCESS)
224
	return result;
225

            
226
    /* check paletted PNGs */
227
    /* TODO: Avoid using target-specific references as sample images */
228
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.indexed-alpha.ref.png");
229
1
    surface = cairo_image_surface_create_from_png (filename);
230
1
    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",
235
			    filename,
236
			    cairo_status_to_string (cairo_surface_status (surface)));
237
	}
238
1
    } 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",
240
			filename);
241
	result = CAIRO_TEST_FAILURE;
242
    }
243
1
    free (filename);
244
1
    cairo_surface_destroy (surface);
245
1
    if (result != CAIRO_TEST_SUCCESS)
246
	return result;
247

            
248
    /* TODO: Avoid using target-specific references as sample images */
249
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.indexed.ref.png");
250
1
    surface = cairo_image_surface_create_from_png (filename);
251
1
    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",
256
			    filename,
257
			    cairo_status_to_string (cairo_surface_status (surface)));
258
	}
259
1
    } 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",
261
			filename);
262
	result = CAIRO_TEST_FAILURE;
263
    }
264
1
    free (filename);
265
1
    cairo_surface_destroy (surface);
266
1
    if (result != CAIRO_TEST_SUCCESS)
267
	return result;
268

            
269
    /* check grayscale PNGs */
270
    /* TODO: Avoid using target-specific references as sample images */
271
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.gray-alpha.ref.png");
272
1
    surface = cairo_image_surface_create_from_png (filename);
273
1
    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",
278
			    filename,
279
			    cairo_status_to_string (cairo_surface_status (surface)));
280
	}
281
1
    } 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",
283
			filename);
284
	result = CAIRO_TEST_FAILURE;
285
    }
286
1
    free (filename);
287
1
    cairo_surface_destroy (surface);
288
1
    if (result != CAIRO_TEST_SUCCESS)
289
	return result;
290

            
291
    /* TODO: Avoid using target-specific references as sample images */
292
1
    xasprintf (&filename, "%s/%s", path, "create-from-png.gray.ref.png");
293
1
    surface = cairo_image_surface_create_from_png (filename);
294
1
    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",
299
			    filename,
300
			    cairo_status_to_string (cairo_surface_status (surface)));
301
	}
302
1
    } 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",
304
			filename);
305
	result = CAIRO_TEST_FAILURE;
306
    }
307
1
    free (filename);
308
1
    cairo_surface_destroy (surface);
309

            
310
1
    free (path);
311

            
312
1
    return result;
313
}
314

            
315
1
CAIRO_TEST (create_from_png,
316
	    "Tests the creation of an image surface from a PNG file",
317
	    "png", /* keywords */
318
	    NULL, /* requirements */
319
	    WIDTH, HEIGHT,
320
	    preamble, draw)