1
/*
2
 * Copyright © 2008 Chris Wilson
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
 * Chris Wilson not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Chris Wilson 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
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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: Chris Wilson <chris@chris-wilson.co.uk>
24
 */
25

            
26
#include "cairo-test.h"
27

            
28
#include <assert.h>
29

            
30
/* Test the idempotency of write_png->read_png */
31

            
32
#define RGB_MASK 0x00ffffff
33
#define BASENAME "png.out"
34

            
35
static cairo_bool_t
36
2
image_surface_equals (cairo_surface_t *A, cairo_surface_t *B)
37
{
38
4
    if (cairo_image_surface_get_format (A) !=
39
2
	cairo_image_surface_get_format (B))
40
	return 0;
41

            
42
4
    if (cairo_image_surface_get_width (A) !=
43
2
	cairo_image_surface_get_width (B))
44
	return 0;
45

            
46
4
    if (cairo_image_surface_get_height (A) !=
47
2
	cairo_image_surface_get_height (B))
48
	return 0;
49

            
50
2
    return 1;
51
}
52

            
53
static const char *
54
format_to_string (cairo_format_t format)
55
{
56
    switch (format) {
57
    case CAIRO_FORMAT_A1:     return "a1";
58
    case CAIRO_FORMAT_A8:     return "a8";
59
    case CAIRO_FORMAT_RGB16_565:  return "rgb16";
60
    case CAIRO_FORMAT_RGB24:  return "rgb24";
61
    case CAIRO_FORMAT_RGB30:  return "rgb30";
62
    case CAIRO_FORMAT_ARGB32: return "argb32";
63
    case CAIRO_FORMAT_RGB96F: return "rgb96f";
64
    case CAIRO_FORMAT_RGBA128F: return "rgba128f";
65
    case CAIRO_FORMAT_INVALID:
66
    default: return "???";
67
    }
68
}
69

            
70
static void
71
print_surface (const cairo_test_context_t *ctx, cairo_surface_t *surface)
72
{
73
    cairo_test_log (ctx,
74
		    "%s (%dx%d)\n",
75
		    format_to_string (cairo_image_surface_get_format (surface)),
76
		    cairo_image_surface_get_width (surface),
77
		    cairo_image_surface_get_height (surface));
78
}
79

            
80
static cairo_test_status_t
81
1
preamble (cairo_test_context_t *ctx)
82
{
83
    cairo_surface_t *surface0, *surface1;
84
    cairo_status_t status;
85
1
    uint32_t argb32 = 0xdeadbede;
86
    char *filename;
87
1
    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
88

            
89
1
    xasprintf (&filename, "%s/%s.png", path, BASENAME);
90
1
    surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
91
						    CAIRO_FORMAT_ARGB32,
92
						    1, 1, 4);
93
1
    status = cairo_surface_write_to_png (surface0, filename);
94
1
    if (status) {
95
	cairo_test_log (ctx, "Error writing '%s': %s\n",
96
			filename, cairo_status_to_string (status));
97

            
98
	cairo_surface_destroy (surface0);
99
	free (filename);
100
	return cairo_test_status_from_status (ctx, status);
101
    }
102
1
    surface1 = cairo_image_surface_create_from_png (filename);
103
1
    status = cairo_surface_status (surface1);
104
1
    if (status) {
105
	cairo_test_log (ctx, "Error reading '%s': %s\n",
106
			filename, cairo_status_to_string (status));
107

            
108
	cairo_surface_destroy (surface1);
109
	cairo_surface_destroy (surface0);
110
	free (filename);
111
	return cairo_test_status_from_status (ctx, status);
112
    }
113

            
114
1
    if (! image_surface_equals (surface0, surface1)) {
115
	cairo_test_log (ctx, "Error surface mismatch.\n");
116
	cairo_test_log (ctx, "to png: "); print_surface (ctx, surface0);
117
	cairo_test_log (ctx, "from png: "); print_surface (ctx, surface1);
118

            
119
	cairo_surface_destroy (surface0);
120
	cairo_surface_destroy (surface1);
121
	free (filename);
122
	return CAIRO_TEST_FAILURE;
123
    }
124
1
    assert (*(uint32_t *) cairo_image_surface_get_data (surface1) == argb32);
125

            
126
1
    cairo_surface_destroy (surface0);
127
1
    cairo_surface_destroy (surface1);
128

            
129
1
    surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
130
						    CAIRO_FORMAT_RGB24,
131
						    1, 1, 4);
132
1
    status = cairo_surface_write_to_png (surface0, filename);
133
1
    if (status) {
134
	cairo_test_log (ctx, "Error writing '%s': %s\n",
135
			filename, cairo_status_to_string (status));
136
	cairo_surface_destroy (surface0);
137
	return cairo_test_status_from_status (ctx, status);
138
    }
139
1
    surface1 = cairo_image_surface_create_from_png (filename);
140
1
    status = cairo_surface_status (surface1);
141
1
    if (status) {
142
	cairo_test_log (ctx, "Error reading '%s': %s\n",
143
			filename, cairo_status_to_string (status));
144
	free (filename);
145

            
146
	cairo_surface_destroy (surface1);
147
	cairo_surface_destroy (surface0);
148
	return cairo_test_status_from_status (ctx, status);
149
    }
150
1
    free (filename);
151

            
152
1
    if (! image_surface_equals (surface0, surface1)) {
153
	cairo_test_log (ctx, "Error surface mismatch.\n");
154
	cairo_test_log (ctx, "to png: "); print_surface (ctx, surface0);
155
	cairo_test_log (ctx, "from png: "); print_surface (ctx, surface1);
156

            
157
	cairo_surface_destroy (surface0);
158
	cairo_surface_destroy (surface1);
159
	return CAIRO_TEST_FAILURE;
160
    }
161
1
    assert ((*(uint32_t *) cairo_image_surface_get_data (surface1) & RGB_MASK)
162
	    == (argb32 & RGB_MASK));
163

            
164
1
    cairo_surface_destroy (surface0);
165
1
    cairo_surface_destroy (surface1);
166

            
167
1
    return CAIRO_TEST_SUCCESS;
168
}
169

            
170
1
CAIRO_TEST (png,
171
	    "Check that the png export/import is idempotent.",
172
	    "png, api", /* keywords */
173
	    NULL, /* requirements */
174
	    0, 0,
175
	    preamble, NULL)