Opensource Compliance Issue.
[platform/core/graphics/cairo.git] / test / mask.c
1 /*
2  * Copyright © 2005 Red Hat, Inc.
3  *
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.
14  *
15  * RED HAT, INC. 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.
22  *
23  * Authors: Owen Taylor <otaylor@redhat.com>
24  *          Kristian Høgsberg <krh@redhat.com>
25  */
26
27 #include "cairo-test.h"
28 #include <math.h>
29 #include <stdio.h>
30
31 #define WIDTH 16
32 #define HEIGHT 16
33 #define PAD 2
34
35 static const char *png_filename = "romedalen.png";
36 static cairo_surface_t *image;
37
38 static void
39 set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
40 {
41     cairo_set_source_rgb (cr, 0, 0, 0.6);
42 }
43
44 static void
45 set_translucent_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
46 {
47     cairo_set_source_rgba (cr, 0, 0, 0.6, 0.5);
48 }
49
50 static void
51 set_gradient_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
52 {
53     cairo_pattern_t *pattern;
54
55     pattern =
56         cairo_pattern_create_linear (x, y, x + WIDTH, y + HEIGHT);
57     cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 1, 1, 1);
58     cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0.4, 1);
59     cairo_set_source (cr, pattern);
60     cairo_pattern_destroy (pattern);
61 }
62
63 static void
64 set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
65 {
66     cairo_pattern_t *pattern;
67
68     if (image == NULL || cairo_surface_status (image)) {
69         cairo_surface_destroy (image);
70         image = cairo_test_create_surface_from_png (ctx, png_filename);
71     }
72
73     pattern = cairo_pattern_create_for_surface (image);
74     cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
75     cairo_set_source (cr, pattern);
76     cairo_pattern_destroy (pattern);
77 }
78
79 static void
80 mask_polygon (cairo_t *cr, int x, int y)
81 {
82     cairo_surface_t *mask_surface;
83     cairo_t *cr2;
84
85     mask_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
86                                                  CAIRO_CONTENT_ALPHA,
87                                                  WIDTH, HEIGHT);
88     cr2 = cairo_create (mask_surface);
89     cairo_surface_destroy (mask_surface);
90
91     cairo_save (cr2);
92     cairo_set_operator (cr2, CAIRO_OPERATOR_CLEAR);
93     cairo_paint (cr2);
94     cairo_restore (cr2);
95
96     cairo_set_source_rgb (cr2, 1, 1, 1); /* white */
97
98     cairo_new_path (cr2);
99     cairo_move_to (cr2, 0, 0);
100     cairo_line_to (cr2, 0, HEIGHT);
101     cairo_line_to (cr2, WIDTH / 2, 3 * HEIGHT / 4);
102     cairo_line_to (cr2, WIDTH, HEIGHT);
103     cairo_line_to (cr2, WIDTH, 0);
104     cairo_line_to (cr2, WIDTH / 2, HEIGHT / 4);
105     cairo_close_path (cr2);
106     cairo_fill (cr2);
107
108     cairo_mask_surface (cr, cairo_get_target (cr2), x, y);
109     cairo_destroy (cr2);
110 }
111
112 static void
113 mask_alpha (cairo_t *cr, int x, int y)
114 {
115     cairo_paint_with_alpha (cr, 0.75);
116 }
117
118 static void
119 mask_gradient (cairo_t *cr, int x, int y)
120 {
121     cairo_pattern_t *pattern;
122
123     pattern = cairo_pattern_create_linear (x, y,
124                                            x + WIDTH, y + HEIGHT);
125
126     cairo_pattern_add_color_stop_rgba (pattern,
127                                        0,
128                                        1, 1, 1, 1);
129     cairo_pattern_add_color_stop_rgba (pattern,
130                                        1,
131                                        1, 1, 1, 0);
132
133     cairo_mask (cr, pattern);
134
135     cairo_pattern_destroy (pattern);
136 }
137
138 static void
139 clip_none (cairo_t *cr, int x, int y)
140 {
141 }
142
143 static void
144 clip_rects (cairo_t *cr, int x, int y)
145 {
146     int height = HEIGHT / 3;
147
148     cairo_new_path (cr);
149     cairo_rectangle (cr, x, y, WIDTH, height);
150     cairo_rectangle (cr, x, y + 2 * height, WIDTH, height);
151     cairo_clip (cr);
152 }
153
154 static void
155 clip_circle (cairo_t *cr, int x, int y)
156 {
157     cairo_new_path (cr);
158     cairo_arc (cr, x + WIDTH / 2, y + HEIGHT / 2,
159                WIDTH / 2, 0, 2 * M_PI);
160     cairo_clip (cr);
161     cairo_new_path (cr);
162 }
163
164 static void (* const pattern_funcs[])(const cairo_test_context_t *ctx, cairo_t *cr, int x, int y) = {
165     set_solid_pattern,
166     set_translucent_pattern,
167     set_gradient_pattern,
168     set_image_pattern,
169 };
170
171 static void (* const mask_funcs[])(cairo_t *cr, int x, int y) = {
172     mask_alpha,
173     mask_gradient,
174     mask_polygon,
175 };
176
177 static void (* const clip_funcs[])(cairo_t *cr, int x, int y) = {
178     clip_none,
179     clip_rects,
180     clip_circle,
181 };
182
183 #define IMAGE_WIDTH (ARRAY_LENGTH (pattern_funcs) * (WIDTH + PAD) + PAD)
184 #define IMAGE_HEIGHT (ARRAY_LENGTH (mask_funcs) * ARRAY_LENGTH (clip_funcs) * (HEIGHT + PAD) + PAD)
185
186 static cairo_test_status_t
187 draw (cairo_t *cr, int width, int height)
188 {
189     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
190     cairo_surface_t *tmp_surface;
191     size_t i, j, k;
192     cairo_t *cr2;
193
194     /* Some of our drawing is unbounded, so we draw each test to
195      * a temporary surface and copy over.
196      */
197     tmp_surface = cairo_surface_create_similar (cairo_get_group_target (cr),
198                                                 CAIRO_CONTENT_COLOR_ALPHA,
199                                                 IMAGE_WIDTH, IMAGE_HEIGHT);
200     cr2 = cairo_create (tmp_surface);
201     cairo_surface_destroy (tmp_surface);
202
203     for (k = 0; k < ARRAY_LENGTH (clip_funcs); k++) {
204         for (j = 0; j < ARRAY_LENGTH (mask_funcs); j++) {
205             for (i = 0; i < ARRAY_LENGTH (pattern_funcs); i++) {
206                 int x = i * (WIDTH + PAD) + PAD;
207                 int y = (ARRAY_LENGTH (mask_funcs) * k + j) * (HEIGHT + PAD) + PAD;
208
209                 /* Clear intermediate surface we are going to be drawing onto */
210                 cairo_save (cr2);
211                 cairo_set_operator (cr2, CAIRO_OPERATOR_CLEAR);
212                 cairo_paint (cr2);
213                 cairo_restore (cr2);
214
215                 /* draw */
216                 cairo_save (cr2);
217
218                 clip_funcs[k] (cr2, x, y);
219                 pattern_funcs[i] (ctx, cr2, x, y);
220                 mask_funcs[j] (cr2, x, y);
221
222                 cairo_restore (cr2);
223
224                 /* Copy back to the main pixmap */
225                 cairo_set_source_surface (cr, cairo_get_target (cr2), 0, 0);
226                 cairo_rectangle (cr, x, y, WIDTH, HEIGHT);
227                 cairo_fill (cr);
228             }
229         }
230     }
231
232     cairo_destroy (cr2);
233
234     cairo_surface_destroy (image);
235     image = NULL;
236
237     return CAIRO_TEST_SUCCESS;
238 }
239
240 CAIRO_TEST (mask,
241             "Tests of cairo_mask",
242             "mask", /* keywords */
243             NULL, /* requirements */
244             IMAGE_WIDTH, IMAGE_HEIGHT,
245             NULL, draw)
246