[check] Filter programlistings for check-doc-syntax.sh
[cairo/haiku.git] / test / png.c
blob65e465dfd41661d66ea8d95683097a1a1f64decd
1 /*
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 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "cairo-test.h"
32 #include <cairo.h>
33 #include <stdio.h>
34 #include <stdint.h>
35 #include <assert.h>
37 /* Test the idempotency of write_png->read_png */
39 #define RGB_MASK 0x00ffffff
41 static cairo_bool_t
42 image_surface_equals (cairo_surface_t *A, cairo_surface_t *B)
44 if (cairo_image_surface_get_format (A) !=
45 cairo_image_surface_get_format (B))
46 return 0;
48 if (cairo_image_surface_get_width (A) !=
49 cairo_image_surface_get_width (B))
50 return 0;
52 if (cairo_image_surface_get_height (A) !=
53 cairo_image_surface_get_height (B))
54 return 0;
56 return 1;
59 static const char *
60 format_to_string (cairo_format_t format)
62 switch (format) {
63 case CAIRO_FORMAT_A1: return "a1";
64 case CAIRO_FORMAT_A8: return "a8";
65 case CAIRO_FORMAT_RGB24: return "rgb24";
66 case CAIRO_FORMAT_ARGB32: return "argb32";
67 default: return "???";
71 static void
72 print_surface (cairo_surface_t *surface)
74 printf ("%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));
80 int
81 main (void)
83 cairo_surface_t *surface0, *surface1;
84 cairo_status_t status;
85 uint32_t argb32 = 0xdeadbede;
87 surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
88 CAIRO_FORMAT_ARGB32,
89 1, 1, 4);
90 assert (cairo_surface_status (surface0) == CAIRO_STATUS_SUCCESS);
91 status = cairo_surface_write_to_png (surface0, "png-test.png");
92 if (status) {
93 printf ("Error writing 'png-test.png': %s\n",
94 cairo_status_to_string (status));
95 return CAIRO_TEST_FAILURE;
97 surface1 = cairo_image_surface_create_from_png ("png-test.png");
98 status = cairo_surface_status (surface1);
99 if (status) {
100 printf ("Error reading 'png-test.png': %s\n",
101 cairo_status_to_string (status));
102 return CAIRO_TEST_FAILURE;
105 if (! image_surface_equals (surface0, surface1)) {
106 printf ("Error surface mismatch.\n");
107 printf ("to png: "); print_surface (surface0);
108 printf ("from png: "); print_surface (surface1);
109 return CAIRO_TEST_FAILURE;
111 assert (*(uint32_t *) cairo_image_surface_get_data (surface1) == argb32);
113 cairo_surface_destroy (surface0);
114 cairo_surface_destroy (surface1);
117 surface0 = cairo_image_surface_create_for_data ((unsigned char *) &argb32,
118 CAIRO_FORMAT_RGB24,
119 1, 1, 4);
120 assert (cairo_surface_status (surface0) == CAIRO_STATUS_SUCCESS);
121 status = cairo_surface_write_to_png (surface0, "png-test.png");
122 if (status) {
123 printf ("Error writing 'png-test.png': %s\n",
124 cairo_status_to_string (status));
125 return CAIRO_TEST_FAILURE;
127 surface1 = cairo_image_surface_create_from_png ("png-test.png");
128 status = cairo_surface_status (surface1);
129 if (status) {
130 printf ("Error reading 'png-test.png': %s\n",
131 cairo_status_to_string (status));
132 return CAIRO_TEST_FAILURE;
135 if (! image_surface_equals (surface0, surface1)) {
136 printf ("Error surface mismatch.\n");
137 printf ("to png: "); print_surface (surface0);
138 printf ("from png: "); print_surface (surface1);
139 return CAIRO_TEST_FAILURE;
141 assert ((*(uint32_t *) cairo_image_surface_get_data (surface1) & RGB_MASK)
142 == (argb32 & RGB_MASK));
144 cairo_surface_destroy (surface0);
145 cairo_surface_destroy (surface1);
148 return CAIRO_TEST_SUCCESS;