[check] Filter programlistings for check-doc-syntax.sh
[cairo/haiku.git] / test / a1-mask.c
blob946b62209a185b456482bcb8496f7f185e170e9e
1 /*
2 * Copyright © Jeff Muizelaar
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 * JEFF MUIZELAAR 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 * Authors: Jeff Muizelaar <jeff@infidigm.net>
24 * Carl Worth <cworth@cworth.org>
25 * Chris Wilson <chris@chris-wilson.co.uk>
28 #include "cairo-test.h"
30 static cairo_test_draw_function_t draw;
32 #define MASK_WIDTH 10
33 #define MASK_HEIGHT 8
35 #ifdef WORDS_BIGENDIAN
36 #define MASK 0x28, 0x55
37 #else
38 #define MASK 0x14, 0xAA
39 #endif
40 static unsigned char mask[(MASK_WIDTH + 7) / 8 * MASK_HEIGHT] = {
41 MASK,
42 MASK,
43 MASK,
44 MASK,
45 MASK,
46 MASK,
47 MASK,
48 MASK,
51 cairo_test_t test = {
52 "a1-mask",
53 "test masks of CAIRO_FORMAT_A1",
54 MASK_WIDTH, MASK_HEIGHT,
55 draw
59 static cairo_test_status_t
60 check_status (cairo_status_t status, cairo_status_t expected)
62 if (status == expected)
63 return CAIRO_TEST_SUCCESS;
65 cairo_test_log ("Error: Expected status value %d (%s), received %d (%s)\n",
66 expected,
67 cairo_status_to_string (expected),
68 status,
69 cairo_status_to_string (status));
70 return CAIRO_TEST_FAILURE;
73 static cairo_test_status_t
74 test_surface_with_width_and_stride (int width, int stride,
75 cairo_status_t expected)
77 cairo_test_status_t status;
78 cairo_surface_t *surface;
79 cairo_t *cr;
80 int len;
81 unsigned char *data;
83 cairo_test_log ("Creating surface with width %d and stride %d\n",
84 width, stride);
86 len = stride;
87 if (len < 0)
88 len = -len;
89 data = xmalloc (len);
91 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
92 width, 1, stride);
93 cr = cairo_create (surface);
95 cairo_paint (cr);
97 status = check_status (cairo_surface_status (surface), expected);
98 if (status)
99 goto BAIL;
101 status = check_status (cairo_status (cr), expected);
102 if (status)
103 goto BAIL;
105 BAIL:
106 cairo_destroy (cr);
107 cairo_surface_destroy (surface);
108 free (data);
109 return status;
112 static cairo_test_status_t
113 draw (cairo_t *cr, int dst_width, int dst_height)
115 unsigned char *mask_aligned;
116 cairo_surface_t *surface;
118 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
119 MASK_WIDTH,
120 MASK_HEIGHT);
122 mask_aligned = cairo_image_surface_get_data (surface);
123 if (mask_aligned != NULL) {
124 int stride = cairo_image_surface_get_stride (surface), row;
125 const unsigned char *src = mask;
126 unsigned char *dst = mask_aligned;
127 for (row = 0; row < MASK_HEIGHT; row++) {
128 memcpy (dst, src, (MASK_WIDTH + 7) / 8);
129 src += (MASK_WIDTH + 7) / 8;
130 dst += stride;
134 /* Paint background blue */
135 cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
136 cairo_paint (cr);
138 /* Then paint red through our mask */
139 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
140 cairo_mask_surface (cr, surface, 0, 0);
141 cairo_surface_destroy (surface);
143 return CAIRO_TEST_SUCCESS;
147 main (void)
149 int test_width;
151 cairo_test_init ("a1-mask");
153 /* first check the API strictness */
154 for (test_width = 0; test_width < 40; test_width++) {
155 int test_stride = (test_width + 7) / 8;
156 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_A1,
157 test_width);
158 cairo_test_status_t status;
159 cairo_status_t expected;
161 /* First create a surface using the width as the stride,
162 * (most of these should fail).
164 expected = (stride == test_stride) ?
165 CAIRO_STATUS_SUCCESS : CAIRO_STATUS_INVALID_STRIDE;
167 status = test_surface_with_width_and_stride (test_width,
168 test_stride,
169 expected);
170 if (status)
171 return status;
173 status = test_surface_with_width_and_stride (test_width,
174 -test_stride,
175 expected);
176 if (status)
177 return status;
180 /* Then create a surface using the correct stride,
181 * (should always succeed).
183 status = test_surface_with_width_and_stride (test_width,
184 stride,
185 CAIRO_STATUS_SUCCESS);
186 if (status)
187 return status;
189 status = test_surface_with_width_and_stride (test_width,
190 -stride,
191 CAIRO_STATUS_SUCCESS);
192 if (status)
193 return status;
196 cairo_test_fini ();
198 return cairo_test (&test);