2 * Copyright © 2008 Chris Wilson
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.
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.
23 * Author: Chris Wilson <chris@chris-wilson.co.uk>
26 #include "cairo-test.h"
31 /* Test the idempotency of write_png->read_png */
33 #define RGB_MASK 0x00ffffff
36 image_surface_equals (cairo_surface_t
*A
, cairo_surface_t
*B
)
38 if (cairo_image_surface_get_format (A
) !=
39 cairo_image_surface_get_format (B
))
42 if (cairo_image_surface_get_width (A
) !=
43 cairo_image_surface_get_width (B
))
46 if (cairo_image_surface_get_height (A
) !=
47 cairo_image_surface_get_height (B
))
54 format_to_string (cairo_format_t format
)
57 case CAIRO_FORMAT_A1
: return "a1";
58 case CAIRO_FORMAT_A8
: return "a8";
59 case CAIRO_FORMAT_RGB24
: return "rgb24";
60 case CAIRO_FORMAT_ARGB32
: return "argb32";
61 default: return "???";
66 print_surface (cairo_test_context_t
*ctx
, cairo_surface_t
*surface
)
70 format_to_string (cairo_image_surface_get_format (surface
)),
71 cairo_image_surface_get_width (surface
),
72 cairo_image_surface_get_height (surface
));
78 cairo_test_context_t ctx
;
79 cairo_surface_t
*surface0
, *surface1
;
80 cairo_status_t status
;
81 uint32_t argb32
= 0xdeadbede;
82 cairo_test_status_t result
= CAIRO_TEST_SUCCESS
;
84 cairo_test_init (&ctx
, "png");
86 surface0
= cairo_image_surface_create_for_data ((unsigned char *) &argb32
,
89 assert (cairo_surface_status (surface0
) == CAIRO_STATUS_SUCCESS
);
90 status
= cairo_surface_write_to_png (surface0
, "png-test.png");
92 cairo_test_log (&ctx
, "Error writing 'png-test.png': %s\n",
93 cairo_status_to_string (status
));
94 result
= CAIRO_TEST_FAILURE
;
96 surface1
= cairo_image_surface_create_from_png ("png-test.png");
97 status
= cairo_surface_status (surface1
);
99 cairo_test_log (&ctx
, "Error reading 'png-test.png': %s\n",
100 cairo_status_to_string (status
));
101 result
= CAIRO_TEST_FAILURE
;
104 if (! image_surface_equals (surface0
, surface1
)) {
105 cairo_test_log (&ctx
, "Error surface mismatch.\n");
106 cairo_test_log (&ctx
, "to png: "); print_surface (&ctx
, surface0
);
107 cairo_test_log (&ctx
, "from png: "); print_surface (&ctx
, surface1
);
108 result
= CAIRO_TEST_FAILURE
;
110 assert (*(uint32_t *) cairo_image_surface_get_data (surface1
) == argb32
);
112 cairo_surface_destroy (surface0
);
113 cairo_surface_destroy (surface1
);
116 surface0
= cairo_image_surface_create_for_data ((unsigned char *) &argb32
,
119 assert (cairo_surface_status (surface0
) == CAIRO_STATUS_SUCCESS
);
120 status
= cairo_surface_write_to_png (surface0
, "png-test.png");
122 cairo_test_log (&ctx
, "Error writing 'png-test.png': %s\n",
123 cairo_status_to_string (status
));
124 result
= CAIRO_TEST_FAILURE
;
126 surface1
= cairo_image_surface_create_from_png ("png-test.png");
127 status
= cairo_surface_status (surface1
);
129 cairo_test_log (&ctx
, "Error reading 'png-test.png': %s\n",
130 cairo_status_to_string (status
));
131 result
= CAIRO_TEST_FAILURE
;
134 if (! image_surface_equals (surface0
, surface1
)) {
135 cairo_test_log (&ctx
, "Error surface mismatch.\n");
136 cairo_test_log (&ctx
, "to png: "); print_surface (&ctx
, surface0
);
137 cairo_test_log (&ctx
, "from png: "); print_surface (&ctx
, surface1
);
138 result
= CAIRO_TEST_FAILURE
;
140 assert ((*(uint32_t *) cairo_image_surface_get_data (surface1
) & RGB_MASK
)
141 == (argb32
& RGB_MASK
));
143 cairo_surface_destroy (surface0
);
144 cairo_surface_destroy (surface1
);
146 cairo_test_fini (&ctx
);