Augment tests to do better testing of paths/images with alpha.

Add a new cairo_test_paint_checkered function so that tests that draw
with alpha can easily put an easy-to-see checkered background in place
first.

Add new tests caps-joins-alpha and paint-source-alpha that do simple
tests of strokes and image painting with source pattern alpha.

Also, add the checkered background to paint-with-alpha for
consistency.
This commit is contained in:
Carl Worth 2006-04-25 01:56:51 -07:00
parent a5afc59d0a
commit bef621e870
13 changed files with 225 additions and 3 deletions

2
test/.gitignore vendored
View file

@ -4,6 +4,7 @@ Makefile
Makefile.in
a8-mask
caps-joins
caps-joins-alpha
caps-sub-paths
clip-all
clip-fill-rule
@ -44,6 +45,7 @@ nil-surface
operator-clear
operator-source
paint
paint-source-alpha
paint-with-alpha
path-data
pattern-get-type

View file

@ -2,6 +2,7 @@
TESTS = \
a8-mask \
caps-joins \
caps-joins-alpha \
caps-sub-paths \
clip-all \
clip-fill-rule \
@ -36,6 +37,7 @@ nil-surface \
operator-clear \
operator-source \
paint \
paint-source-alpha \
paint-with-alpha \
path-data \
pattern-get-type \
@ -113,6 +115,8 @@ a8-mask-rgb24-ref.png \
caps-joins-ps-rgb24-ref.png \
caps-joins-ref.png \
caps-joins-rgb24-ref.png \
caps-joins-alpha-ref.png \
caps-joins-alpha-rgb24-ref.png \
caps-sub-paths-ref.png \
caps-sub-paths-rgb24-ref.png \
clip-all-ref.png \
@ -178,6 +182,8 @@ operator-source-ref.png \
operator-source-rgb24-ref.png \
paint-ref.png \
paint-rgb24-ref.png \
paint-source-alpha-ref.png \
paint-source-alpha-rgb24-ref.png \
paint-with-alpha-ref.png \
paint-with-alpha-rgb24-ref.png \
path-data-ref.png \
@ -309,6 +315,7 @@ endif
# from autogen.sh. My, but this is painful...
a8_mask_LDADD = $(LDADDS)
caps_joins_LDADD = $(LDADDS)
caps_joins_alpha_LDADD = $(LDADDS)
caps_sub_paths_LDADD = $(LDADDS)
clip_all_LDADD = $(LDADDS)
clip_fill_rule_LDADD = $(LDADDS)
@ -345,6 +352,7 @@ nil_surface_LDADD = $(LDADDS)
operator_clear_LDADD = $(LDADDS)
operator_source_LDADD = $(LDADDS)
paint_LDADD = $(LDADDS)
paint_source_alpha_LDADD = $(LDADDS)
paint_with_alpha_LDADD = $(LDADDS)
path_data_LDADD = $(LDADDS)
pattern_get_type_LDADD = $(LDADDS)

View file

@ -1803,3 +1803,48 @@ cairo_test_create_pattern_from_png (const char *filename)
return pattern;
}
static cairo_status_t
_draw_check (cairo_surface_t *surface, int width, int height)
{
cairo_t *cr;
cairo_status_t status;
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 0.75, 0.75, 0.75); /* light gray */
cairo_paint (cr);
cairo_set_source_rgb (cr, 0.25, 0.25, 0.25); /* dark gray */
cairo_rectangle (cr, width / 2, 0, width / 2, height / 2);
cairo_rectangle (cr, 0, height / 2, width / 2, height / 2);
cairo_fill (cr);
status = cairo_status (cr);
cairo_destroy (cr);
return status;
}
cairo_status_t
cairo_test_paint_checkered (cairo_t *cr)
{
cairo_status_t status;
cairo_surface_t *check;
check = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 12, 12);
status = _draw_check (check, 12, 12);
if (status)
return status;
cairo_save (cr);
cairo_set_source_surface (cr, check, 0, 0);
cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
cairo_paint (cr);
cairo_restore (cr);
cairo_surface_destroy (check);
return CAIRO_STATUS_SUCCESS;
}

View file

@ -138,6 +138,9 @@ cairo_test_create_surface_from_png (const char *filename);
cairo_pattern_t *
cairo_test_create_pattern_from_png (const char *filename);
cairo_status_t
cairo_test_paint_checkered (cairo_t *cr);
void
xasprintf (char **strp, const char *fmt, ...);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

94
test/caps-joins-alpha.c Normal file
View file

@ -0,0 +1,94 @@
/*
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2006 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of
* Red Hat, Inc. not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Red Hat, Inc. makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Carl D. Worth <cworth@cworth.org>
*/
#include "cairo-test.h"
#define LINE_WIDTH 10.
#define SIZE (5 * LINE_WIDTH)
#define PAD (2 * LINE_WIDTH)
cairo_test_t test = {
"caps-joins-alpha",
"Test caps and joins with some source alpha",
3 * (PAD + SIZE) + PAD,
PAD + SIZE + PAD
};
static void
make_path (cairo_t *cr)
{
cairo_move_to (cr, 0., 0.);
cairo_rel_line_to (cr, 0., SIZE);
cairo_rel_line_to (cr, SIZE, 0.);
cairo_close_path (cr);
cairo_move_to (cr, 2 * LINE_WIDTH, 0.);
cairo_rel_line_to (cr, 3 * LINE_WIDTH, 0.);
cairo_rel_line_to (cr, 0., 3 * LINE_WIDTH);
}
static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
/* First draw a checkered background */
cairo_test_paint_checkered (cr);
/* Then draw the original caps-joins test but with a bit of alphs thrown in. */
cairo_set_line_width (cr, LINE_WIDTH);
cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.5); /* 50% red */
cairo_translate (cr, PAD, PAD);
make_path (cr);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_BEVEL);
cairo_stroke (cr);
cairo_set_source_rgba (cr, 0.0, 1.0, 0.0, 0.5); /* 50% green */
cairo_translate (cr, SIZE + PAD, 0.);
make_path (cr);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_stroke (cr);
cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.5); /* 50% blue */
cairo_translate (cr, SIZE + PAD, 0.);
make_path (cr);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
cairo_stroke (cr);
return CAIRO_TEST_SUCCESS;
}
int
main (void)
{
return cairo_test (&test, draw);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 B

67
test/paint-source-alpha.c Normal file
View file

@ -0,0 +1,67 @@
/*
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2006 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of
* Red Hat, Inc. not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Red Hat, Inc. makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Carl D. Worth <cworth@cworth.org>
*/
#include "cairo-test.h"
cairo_test_t test = {
"paint-source-alpha",
"Simple test of cairo_paint with a source surface with non-opaque alpha",
32, 32
};
static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
cairo_surface_t *surface;
static uint32_t data[16] = {
0x80808080, 0x80808080, 0x80800000, 0x80800000,
0x80808080, 0x80808080, 0x80800000, 0x80800000,
0x80008000, 0x80008000, 0x80000080, 0x80000080,
0x80008000, 0x80008000, 0x80000080, 0x80000080
};
surface = cairo_image_surface_create_for_data ((unsigned char *) data,
CAIRO_FORMAT_ARGB32, 4, 4, 16);
cairo_test_paint_checkered (cr);
cairo_scale (cr, 4, 4);
cairo_set_source_surface (cr, surface, 2 , 2);
cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
cairo_paint (cr);
cairo_surface_destroy (surface);
return CAIRO_TEST_SUCCESS;
}
int
main (void)
{
return cairo_test (&test, draw);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 164 B

After

Width:  |  Height:  |  Size: 268 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 142 B

After

Width:  |  Height:  |  Size: 255 B

View file

@ -1,5 +1,6 @@
/*
* Copyright © 2005 Red Hat, Inc.
* Copyright © 2006 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
@ -28,7 +29,7 @@
cairo_test_t test = {
"paint-with-alpha",
"Simple test of cairo_paint_with_alpha",
12, 12
32, 32
};
static cairo_test_status_t
@ -46,9 +47,11 @@ draw (cairo_t *cr, int width, int height)
surface = cairo_image_surface_create_for_data ((unsigned char *) data,
CAIRO_FORMAT_RGB24, 4, 4, 16);
cairo_scale (cr, 2, 2);
cairo_test_paint_checkered (cr);
cairo_set_source_surface (cr, surface, 1 , 1);
cairo_scale (cr, 4, 4);
cairo_set_source_surface (cr, surface, 2 , 2);
cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
cairo_paint_with_alpha (cr, 0.5);