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
;
35 #ifdef WORDS_BIGENDIAN
36 #define MASK 0x28, 0x55
38 #define MASK 0x14, 0xAA
40 static unsigned char mask
[(MASK_WIDTH
+ 7) / 8 * MASK_HEIGHT
] = {
51 static const cairo_test_t test
= {
53 "test masks of CAIRO_FORMAT_A1",
54 MASK_WIDTH
, MASK_HEIGHT
,
59 static cairo_test_status_t
60 check_status (const cairo_test_context_t
*ctx
,
61 cairo_status_t status
,
62 cairo_status_t expected
)
64 if (status
== expected
)
65 return CAIRO_TEST_SUCCESS
;
68 "Error: Expected status value %d (%s), received %d (%s)\n",
70 cairo_status_to_string (expected
),
72 cairo_status_to_string (status
));
73 return CAIRO_TEST_FAILURE
;
76 static cairo_test_status_t
77 test_surface_with_width_and_stride (const cairo_test_context_t
*ctx
,
78 int width
, int stride
,
79 cairo_status_t expected
)
81 cairo_test_status_t status
;
82 cairo_surface_t
*surface
;
88 "Creating surface with width %d and stride %d\n",
96 surface
= cairo_image_surface_create_for_data (data
, CAIRO_FORMAT_A1
,
98 cr
= cairo_create (surface
);
102 status
= check_status (ctx
, cairo_surface_status (surface
), expected
);
106 status
= check_status (ctx
, cairo_status (cr
), expected
);
112 cairo_surface_destroy (surface
);
117 static cairo_test_status_t
118 draw (cairo_t
*cr
, int dst_width
, int dst_height
)
120 unsigned char *mask_aligned
;
121 cairo_surface_t
*surface
;
123 surface
= cairo_image_surface_create (CAIRO_FORMAT_A1
,
127 mask_aligned
= cairo_image_surface_get_data (surface
);
128 if (mask_aligned
!= NULL
) {
129 int stride
= cairo_image_surface_get_stride (surface
), row
;
130 const unsigned char *src
= mask
;
131 unsigned char *dst
= mask_aligned
;
132 for (row
= 0; row
< MASK_HEIGHT
; row
++) {
133 memcpy (dst
, src
, (MASK_WIDTH
+ 7) / 8);
134 src
+= (MASK_WIDTH
+ 7) / 8;
139 /* Paint background blue */
140 cairo_set_source_rgb (cr
, 0, 0, 1); /* blue */
143 /* Then paint red through our mask */
144 cairo_set_source_rgb (cr
, 1, 0, 0); /* red */
145 cairo_mask_surface (cr
, surface
, 0, 0);
146 cairo_surface_destroy (surface
);
148 return CAIRO_TEST_SUCCESS
;
154 cairo_test_context_t ctx
;
157 cairo_test_init (&ctx
, "a1-mask");
159 /* first check the API strictness */
160 for (test_width
= 0; test_width
< 40; test_width
++) {
161 int test_stride
= (test_width
+ 7) / 8;
162 int stride
= cairo_format_stride_for_width (CAIRO_FORMAT_A1
,
164 cairo_test_status_t status
;
165 cairo_status_t expected
;
167 /* First create a surface using the width as the stride,
168 * (most of these should fail).
170 expected
= (stride
== test_stride
) ?
171 CAIRO_STATUS_SUCCESS
: CAIRO_STATUS_INVALID_STRIDE
;
173 status
= test_surface_with_width_and_stride (&ctx
,
180 status
= test_surface_with_width_and_stride (&ctx
,
188 /* Then create a surface using the correct stride,
189 * (should always succeed).
191 status
= test_surface_with_width_and_stride (&ctx
,
194 CAIRO_STATUS_SUCCESS
);
198 status
= test_surface_with_width_and_stride (&ctx
,
201 CAIRO_STATUS_SUCCESS
);
206 cairo_test_fini (&ctx
);
208 return cairo_test (&test
);