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"
33 #ifdef WORDS_BIGENDIAN
34 #define MASK 0x28, 0x55
36 #define MASK 0x14, 0xAA
38 static unsigned char mask[(MASK_WIDTH + 7) / 8 * MASK_HEIGHT] = {
49 static cairo_test_status_t
50 check_status (const cairo_test_context_t *ctx,
51 cairo_status_t status,
52 cairo_status_t expected)
54 if (status == expected)
55 return CAIRO_TEST_SUCCESS;
58 "Error: Expected status value %d (%s), received %d (%s)\n",
60 cairo_status_to_string (expected),
62 cairo_status_to_string (status));
63 return CAIRO_TEST_FAILURE;
66 static cairo_test_status_t
67 test_surface_with_width_and_stride (const cairo_test_context_t *ctx,
68 int width, int stride,
69 cairo_status_t expected)
71 cairo_test_status_t status;
72 cairo_surface_t *surface;
78 "Creating surface with width %d and stride %d\n",
86 surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_A1,
88 cr = cairo_create (surface);
92 status = check_status (ctx, cairo_surface_status (surface), expected);
96 status = check_status (ctx, cairo_status (cr), expected);
102 cairo_surface_destroy (surface);
107 static cairo_test_status_t
108 draw (cairo_t *cr, int dst_width, int dst_height)
110 unsigned char *mask_aligned;
111 cairo_surface_t *surface;
113 surface = cairo_image_surface_create (CAIRO_FORMAT_A1,
117 mask_aligned = cairo_image_surface_get_data (surface);
118 if (mask_aligned != NULL) {
119 int stride = cairo_image_surface_get_stride (surface), row;
120 const unsigned char *src = mask;
121 unsigned char *dst = mask_aligned;
122 for (row = 0; row < MASK_HEIGHT; row++) {
123 memcpy (dst, src, (MASK_WIDTH + 7) / 8);
124 src += (MASK_WIDTH + 7) / 8;
128 cairo_surface_mark_dirty (surface);
130 /* Paint background blue */
131 cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
134 /* Then paint red through our mask */
135 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
136 cairo_mask_surface (cr, surface, 0, 0);
137 cairo_surface_destroy (surface);
139 return CAIRO_TEST_SUCCESS;
142 static cairo_test_status_t
143 preamble (cairo_test_context_t *ctx)
145 cairo_test_status_t status = CAIRO_TEST_SUCCESS;
148 /* first check the API strictness */
149 for (test_width = 0; test_width < 40; test_width++) {
150 int test_stride = (test_width + 7) / 8;
151 int stride = cairo_format_stride_for_width (CAIRO_FORMAT_A1,
153 cairo_status_t expected;
155 /* First create a surface using the width as the stride,
156 * (most of these should fail).
158 expected = (stride == test_stride) ?
159 CAIRO_STATUS_SUCCESS : CAIRO_STATUS_INVALID_STRIDE;
161 status = test_surface_with_width_and_stride (ctx,
168 status = test_surface_with_width_and_stride (ctx,
176 /* Then create a surface using the correct stride,
177 * (should always succeed).
179 status = test_surface_with_width_and_stride (ctx,
182 CAIRO_STATUS_SUCCESS);
186 status = test_surface_with_width_and_stride (ctx,
189 CAIRO_STATUS_SUCCESS);
198 "test masks of CAIRO_FORMAT_A1",
199 "alpha, mask", /* keywords */
200 NULL, /* requirements */
201 MASK_WIDTH, MASK_HEIGHT,