blob: 7bcfe414eb4fb237070824c05944b97373af1546 [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
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050034blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050035{
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050036 int32_t width, height, stride, x, y, z, w;
37 uint8_t *src, *dst;
38 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -050039 int i, j, k, size, half;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040040 uint32_t kernel[49];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050041 double f;
42
Kristian Høgsberg87330262008-11-17 22:23:55 -050043 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050044 width = cairo_image_surface_get_width(surface);
45 height = cairo_image_surface_get_height(surface);
46 stride = cairo_image_surface_get_stride(surface);
47 src = cairo_image_surface_get_data(surface);
48
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -040049 dst = malloc(height * stride);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050050
51 half = size / 2;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050052 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050053 for (i = 0; i < size; i++) {
54 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040055 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg0cd8f6e2011-01-21 22:19:40 -050056 a += kernel[i];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050057 }
58
59 for (i = 0; i < height; i++) {
60 s = (uint32_t *) (src + i * stride);
61 d = (uint32_t *) (dst + i * stride);
62 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050063 if (margin < j && j < width - margin) {
64 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050065 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050066 }
67
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050068 x = 0;
69 y = 0;
70 z = 0;
71 w = 0;
72 for (k = 0; k < size; k++) {
73 if (j - half + k < 0 || j - half + k >= width)
74 continue;
75 p = s[j - half + k];
76
77 x += (p >> 24) * kernel[k];
78 y += ((p >> 16) & 0xff) * kernel[k];
79 z += ((p >> 8) & 0xff) * kernel[k];
80 w += (p & 0xff) * kernel[k];
81 }
82 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
83 }
84 }
85
86 for (i = 0; i < height; i++) {
87 s = (uint32_t *) (dst + i * stride);
88 d = (uint32_t *) (src + i * stride);
89 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050090 if (margin <= i && i < height - margin) {
91 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050092 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050093 }
94
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050095 x = 0;
96 y = 0;
97 z = 0;
98 w = 0;
99 for (k = 0; k < size; k++) {
100 if (i - half + k < 0 || i - half + k >= height)
101 continue;
102 s = (uint32_t *) (dst + (i - half + k) * stride);
103 p = s[j];
104
105 x += (p >> 24) * kernel[k];
106 y += ((p >> 16) & 0xff) * kernel[k];
107 z += ((p >> 8) & 0xff) * kernel[k];
108 w += (p & 0xff) * kernel[k];
109 }
110 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
111 }
112 }
113
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400114 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400115 cairo_surface_mark_dirty(surface);
116}
117
118void
119tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400120 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400121{
122 cairo_pattern_t *pattern;
123 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400124 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400125
126 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
127 pattern = cairo_pattern_create_for_surface (surface);
128
129 for (i = 0; i < 4; i++) {
130 fx = i & 1;
131 fy = i >> 1;
132
133 cairo_matrix_init_translate(&matrix,
134 -x + fx * (128 - width),
135 -y + fy * (128 - height));
136 cairo_pattern_set_matrix(pattern, &matrix);
137
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400138 if (fy)
139 vmargin = margin;
140 else
141 vmargin = top_margin;
142
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400143 cairo_reset_clip(cr);
144 cairo_rectangle(cr,
145 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400146 y + fy * (height - vmargin),
147 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400148 cairo_clip (cr);
149 cairo_mask(cr, pattern);
150 }
151
152 /* Top strecth */
153 cairo_matrix_init_translate(&matrix, 64, 0);
154 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
155 cairo_matrix_translate(&matrix, -x - width / 2, -y);
156 cairo_pattern_set_matrix(pattern, &matrix);
157 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
158
159 cairo_reset_clip(cr);
160 cairo_rectangle(cr,
161 x + margin,
162 y,
163 width - 2 * margin, margin);
164 cairo_clip (cr);
165 cairo_mask(cr, pattern);
166
167 /* Bottom strecth */
168 cairo_matrix_translate(&matrix, 0, -height + 128);
169 cairo_pattern_set_matrix(pattern, &matrix);
170
171 cairo_reset_clip(cr);
172 cairo_rectangle(cr, x + margin, y + height - margin,
173 width - 2 * margin, margin);
174 cairo_clip (cr);
175 cairo_mask(cr, pattern);
176
177 /* Left strecth */
178 cairo_matrix_init_translate(&matrix, 0, 64);
179 cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin));
180 cairo_matrix_translate(&matrix, -x, -y - height / 2);
181 cairo_pattern_set_matrix(pattern, &matrix);
182 cairo_reset_clip(cr);
183 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
184 cairo_clip (cr);
185 cairo_mask(cr, pattern);
186
187 /* Right strecth */
188 cairo_matrix_translate(&matrix, -width + 128, 0);
189 cairo_pattern_set_matrix(pattern, &matrix);
190 cairo_rectangle(cr, x + width - margin, y + margin,
191 margin, height - 2 * margin);
192 cairo_reset_clip(cr);
193 cairo_clip (cr);
194 cairo_mask(cr, pattern);
195
196 cairo_pattern_destroy(pattern);
197 cairo_reset_clip(cr);
198}
199
200void
201tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400202 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400203{
204 cairo_pattern_t *pattern;
205 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400206 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400207
208 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
209 pattern = cairo_pattern_create_for_surface (surface);
210 cairo_set_source(cr, pattern);
211 cairo_pattern_destroy(pattern);
212
213 for (i = 0; i < 4; i++) {
214 fx = i & 1;
215 fy = i >> 1;
216
217 cairo_matrix_init_translate(&matrix,
218 -x + fx * (128 - width),
219 -y + fy * (128 - height));
220 cairo_pattern_set_matrix(pattern, &matrix);
221
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400222 if (fy)
223 vmargin = margin;
224 else
225 vmargin = top_margin;
226
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400227 cairo_rectangle(cr,
228 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400229 y + fy * (height - vmargin),
230 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400231 cairo_fill(cr);
232 }
233
234 /* Top strecth */
235 cairo_matrix_init_translate(&matrix, 64, 0);
236 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
237 cairo_matrix_translate(&matrix, -x - width / 2, -y);
238 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400239 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400240 cairo_fill(cr);
241
242 /* Bottom strecth */
243 cairo_matrix_translate(&matrix, 0, -height + 128);
244 cairo_pattern_set_matrix(pattern, &matrix);
245 cairo_rectangle(cr, x + margin, y + height - margin,
246 width - 2 * margin, margin);
247 cairo_fill(cr);
248
249 /* Left strecth */
250 cairo_matrix_init_translate(&matrix, 0, 64);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400251 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400252 cairo_matrix_translate(&matrix, -x, -y - height / 2);
253 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400254 cairo_rectangle(cr, x, y + top_margin,
255 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400256 cairo_fill(cr);
257
258 /* Right strecth */
259 cairo_matrix_translate(&matrix, -width + 128, 0);
260 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400261 cairo_rectangle(cr, x + width - margin, y + top_margin,
262 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400263 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500264}
Kristian Høgsberg1e164b92011-09-13 14:47:46 -0400265
266void
267rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
268{
269 cairo_move_to(cr, x0, y0 + radius);
270 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
271 cairo_line_to(cr, x1 - radius, y0);
272 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
273 cairo_line_to(cr, x1, y1 - radius);
274 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
275 cairo_line_to(cr, x0 + radius, y1);
276 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
277 cairo_close_path(cr);
278}