blob: 184f58e64e465c5a692c06cc48e585f830837968 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050023#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdio.h>
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050027#include <math.h>
28#include <cairo.h>
29#include "cairo-util.h"
30
Kristian Høgsberg87330262008-11-17 22:23:55 -050031#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
32
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050033void
Benjamin Franzke47eb8f42011-10-07 09:08:56 +020034surface_flush_device(cairo_surface_t *surface)
35{
36 cairo_device_t *device;
37
38 device = cairo_surface_get_device(surface);
39 if (device)
40 cairo_device_flush(device);
41}
42
43void
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050044blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050045{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050046 int32_t width, height, stride, x, y, z, w;
47 uint8_t *src, *dst;
48 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050049 int i, j, k, size, half;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040050 uint32_t kernel[49];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050051 double f;
52
Kristian Høgsberg87330262008-11-17 22:23:55 -050053 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050054 width = cairo_image_surface_get_width(surface);
55 height = cairo_image_surface_get_height(surface);
56 stride = cairo_image_surface_get_stride(surface);
57 src = cairo_image_surface_get_data(surface);
58
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040059 dst = malloc(height * stride);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050060
61 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050062 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050063 for (i = 0; i < size; i++) {
64 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040065 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050066 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050067 }
68
69 for (i = 0; i < height; i++) {
70 s = (uint32_t *) (src + i * stride);
71 d = (uint32_t *) (dst + i * stride);
72 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050073 if (margin < j && j < width - margin) {
74 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050075 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050076 }
77
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050078 x = 0;
79 y = 0;
80 z = 0;
81 w = 0;
82 for (k = 0; k < size; k++) {
83 if (j - half + k < 0 || j - half + k >= width)
84 continue;
85 p = s[j - half + k];
86
87 x += (p >> 24) * kernel[k];
88 y += ((p >> 16) & 0xff) * kernel[k];
89 z += ((p >> 8) & 0xff) * kernel[k];
90 w += (p & 0xff) * kernel[k];
91 }
92 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
93 }
94 }
95
96 for (i = 0; i < height; i++) {
97 s = (uint32_t *) (dst + i * stride);
98 d = (uint32_t *) (src + i * stride);
99 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500100 if (margin <= i && i < height - margin) {
101 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500102 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500103 }
104
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500105 x = 0;
106 y = 0;
107 z = 0;
108 w = 0;
109 for (k = 0; k < size; k++) {
110 if (i - half + k < 0 || i - half + k >= height)
111 continue;
112 s = (uint32_t *) (dst + (i - half + k) * stride);
113 p = s[j];
114
115 x += (p >> 24) * kernel[k];
116 y += ((p >> 16) & 0xff) * kernel[k];
117 z += ((p >> 8) & 0xff) * kernel[k];
118 w += (p & 0xff) * kernel[k];
119 }
120 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
121 }
122 }
123
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400124 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400125 cairo_surface_mark_dirty(surface);
126}
127
128void
129tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400130 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400131{
132 cairo_pattern_t *pattern;
133 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400134 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400135
136 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
137 pattern = cairo_pattern_create_for_surface (surface);
138
139 for (i = 0; i < 4; i++) {
140 fx = i & 1;
141 fy = i >> 1;
142
143 cairo_matrix_init_translate(&matrix,
144 -x + fx * (128 - width),
145 -y + fy * (128 - height));
146 cairo_pattern_set_matrix(pattern, &matrix);
147
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400148 if (fy)
149 vmargin = margin;
150 else
151 vmargin = top_margin;
152
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400153 cairo_reset_clip(cr);
154 cairo_rectangle(cr,
155 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400156 y + fy * (height - vmargin),
157 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400158 cairo_clip (cr);
159 cairo_mask(cr, pattern);
160 }
161
162 /* Top strecth */
163 cairo_matrix_init_translate(&matrix, 64, 0);
164 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
165 cairo_matrix_translate(&matrix, -x - width / 2, -y);
166 cairo_pattern_set_matrix(pattern, &matrix);
167 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
168
169 cairo_reset_clip(cr);
170 cairo_rectangle(cr,
171 x + margin,
172 y,
173 width - 2 * margin, margin);
174 cairo_clip (cr);
175 cairo_mask(cr, pattern);
176
177 /* Bottom strecth */
178 cairo_matrix_translate(&matrix, 0, -height + 128);
179 cairo_pattern_set_matrix(pattern, &matrix);
180
181 cairo_reset_clip(cr);
182 cairo_rectangle(cr, x + margin, y + height - margin,
183 width - 2 * margin, margin);
184 cairo_clip (cr);
185 cairo_mask(cr, pattern);
186
187 /* Left strecth */
188 cairo_matrix_init_translate(&matrix, 0, 64);
189 cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin));
190 cairo_matrix_translate(&matrix, -x, -y - height / 2);
191 cairo_pattern_set_matrix(pattern, &matrix);
192 cairo_reset_clip(cr);
193 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
194 cairo_clip (cr);
195 cairo_mask(cr, pattern);
196
197 /* Right strecth */
198 cairo_matrix_translate(&matrix, -width + 128, 0);
199 cairo_pattern_set_matrix(pattern, &matrix);
200 cairo_rectangle(cr, x + width - margin, y + margin,
201 margin, height - 2 * margin);
202 cairo_reset_clip(cr);
203 cairo_clip (cr);
204 cairo_mask(cr, pattern);
205
206 cairo_pattern_destroy(pattern);
207 cairo_reset_clip(cr);
208}
209
210void
211tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400212 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400213{
214 cairo_pattern_t *pattern;
215 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400216 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400217
218 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
219 pattern = cairo_pattern_create_for_surface (surface);
220 cairo_set_source(cr, pattern);
221 cairo_pattern_destroy(pattern);
222
223 for (i = 0; i < 4; i++) {
224 fx = i & 1;
225 fy = i >> 1;
226
227 cairo_matrix_init_translate(&matrix,
228 -x + fx * (128 - width),
229 -y + fy * (128 - height));
230 cairo_pattern_set_matrix(pattern, &matrix);
231
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400232 if (fy)
233 vmargin = margin;
234 else
235 vmargin = top_margin;
236
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400237 cairo_rectangle(cr,
238 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400239 y + fy * (height - vmargin),
240 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400241 cairo_fill(cr);
242 }
243
244 /* Top strecth */
245 cairo_matrix_init_translate(&matrix, 64, 0);
246 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
247 cairo_matrix_translate(&matrix, -x - width / 2, -y);
248 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400249 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400250 cairo_fill(cr);
251
252 /* Bottom strecth */
253 cairo_matrix_translate(&matrix, 0, -height + 128);
254 cairo_pattern_set_matrix(pattern, &matrix);
255 cairo_rectangle(cr, x + margin, y + height - margin,
256 width - 2 * margin, margin);
257 cairo_fill(cr);
258
259 /* Left strecth */
260 cairo_matrix_init_translate(&matrix, 0, 64);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400261 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400262 cairo_matrix_translate(&matrix, -x, -y - height / 2);
263 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400264 cairo_rectangle(cr, x, y + top_margin,
265 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400266 cairo_fill(cr);
267
268 /* Right strecth */
269 cairo_matrix_translate(&matrix, -width + 128, 0);
270 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400271 cairo_rectangle(cr, x + width - margin, y + top_margin,
272 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400273 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500274}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400275
276void
277rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
278{
279 cairo_move_to(cr, x0, y0 + radius);
280 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
281 cairo_line_to(cr, x1 - radius, y0);
282 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
283 cairo_line_to(cr, x1, y1 - radius);
284 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
285 cairo_line_to(cr, x0 + radius, y1);
286 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
287 cairo_close_path(cr);
288}