blob: 3aef0af16a3537c48a3fa9222eb24a8dee04d568 [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øgsberg2f2cfae2008-11-08 22:46:30 -050052 for (i = 0; i < size; i++) {
53 f = (i - half);
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040054 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050055 }
56
57 for (i = 0; i < height; i++) {
58 s = (uint32_t *) (src + i * stride);
59 d = (uint32_t *) (dst + i * stride);
60 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -050061 if (margin < j && j < width - margin) {
62 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -050063 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -050064 }
65
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050066 x = 0;
67 y = 0;
68 z = 0;
69 w = 0;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040070 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050071 for (k = 0; k < size; k++) {
72 if (j - half + k < 0 || j - half + k >= width)
73 continue;
74 p = s[j - half + k];
75
76 x += (p >> 24) * kernel[k];
77 y += ((p >> 16) & 0xff) * kernel[k];
78 z += ((p >> 8) & 0xff) * kernel[k];
79 w += (p & 0xff) * kernel[k];
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040080 a += kernel[k];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050081 }
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;
Kristian Høgsberg49e868c2010-06-15 16:18:58 -040099 a = 0;
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500100 for (k = 0; k < size; k++) {
101 if (i - half + k < 0 || i - half + k >= height)
102 continue;
103 s = (uint32_t *) (dst + (i - half + k) * stride);
104 p = s[j];
105
106 x += (p >> 24) * kernel[k];
107 y += ((p >> 16) & 0xff) * kernel[k];
108 z += ((p >> 8) & 0xff) * kernel[k];
109 w += (p & 0xff) * kernel[k];
Kristian Høgsberg49e868c2010-06-15 16:18:58 -0400110 a += kernel[k];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500111 }
112 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
113 }
114 }
115
Kristian Høgsberg5fc96ff2009-09-12 15:58:48 -0400116 free(dst);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400117 cairo_surface_mark_dirty(surface);
118}
119
120void
121tile_mask(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400122 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400123{
124 cairo_pattern_t *pattern;
125 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400126 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400127
128 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
129 pattern = cairo_pattern_create_for_surface (surface);
130
131 for (i = 0; i < 4; i++) {
132 fx = i & 1;
133 fy = i >> 1;
134
135 cairo_matrix_init_translate(&matrix,
136 -x + fx * (128 - width),
137 -y + fy * (128 - height));
138 cairo_pattern_set_matrix(pattern, &matrix);
139
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400140 if (fy)
141 vmargin = margin;
142 else
143 vmargin = top_margin;
144
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400145 cairo_reset_clip(cr);
146 cairo_rectangle(cr,
147 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400148 y + fy * (height - vmargin),
149 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400150 cairo_clip (cr);
151 cairo_mask(cr, pattern);
152 }
153
154 /* Top strecth */
155 cairo_matrix_init_translate(&matrix, 64, 0);
156 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
157 cairo_matrix_translate(&matrix, -x - width / 2, -y);
158 cairo_pattern_set_matrix(pattern, &matrix);
159 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
160
161 cairo_reset_clip(cr);
162 cairo_rectangle(cr,
163 x + margin,
164 y,
165 width - 2 * margin, margin);
166 cairo_clip (cr);
167 cairo_mask(cr, pattern);
168
169 /* Bottom strecth */
170 cairo_matrix_translate(&matrix, 0, -height + 128);
171 cairo_pattern_set_matrix(pattern, &matrix);
172
173 cairo_reset_clip(cr);
174 cairo_rectangle(cr, x + margin, y + height - margin,
175 width - 2 * margin, margin);
176 cairo_clip (cr);
177 cairo_mask(cr, pattern);
178
179 /* Left strecth */
180 cairo_matrix_init_translate(&matrix, 0, 64);
181 cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin));
182 cairo_matrix_translate(&matrix, -x, -y - height / 2);
183 cairo_pattern_set_matrix(pattern, &matrix);
184 cairo_reset_clip(cr);
185 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
186 cairo_clip (cr);
187 cairo_mask(cr, pattern);
188
189 /* Right strecth */
190 cairo_matrix_translate(&matrix, -width + 128, 0);
191 cairo_pattern_set_matrix(pattern, &matrix);
192 cairo_rectangle(cr, x + width - margin, y + margin,
193 margin, height - 2 * margin);
194 cairo_reset_clip(cr);
195 cairo_clip (cr);
196 cairo_mask(cr, pattern);
197
198 cairo_pattern_destroy(pattern);
199 cairo_reset_clip(cr);
200}
201
202void
203tile_source(cairo_t *cr, cairo_surface_t *surface,
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400204 int x, int y, int width, int height, int margin, int top_margin)
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400205{
206 cairo_pattern_t *pattern;
207 cairo_matrix_t matrix;
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400208 int i, fx, fy, vmargin;
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400209
210 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
211 pattern = cairo_pattern_create_for_surface (surface);
212 cairo_set_source(cr, pattern);
213 cairo_pattern_destroy(pattern);
214
215 for (i = 0; i < 4; i++) {
216 fx = i & 1;
217 fy = i >> 1;
218
219 cairo_matrix_init_translate(&matrix,
220 -x + fx * (128 - width),
221 -y + fy * (128 - height));
222 cairo_pattern_set_matrix(pattern, &matrix);
223
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400224 if (fy)
225 vmargin = margin;
226 else
227 vmargin = top_margin;
228
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400229 cairo_rectangle(cr,
230 x + fx * (width - margin),
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400231 y + fy * (height - vmargin),
232 margin, vmargin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400233 cairo_fill(cr);
234 }
235
236 /* Top strecth */
237 cairo_matrix_init_translate(&matrix, 64, 0);
238 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
239 cairo_matrix_translate(&matrix, -x - width / 2, -y);
240 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400241 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400242 cairo_fill(cr);
243
244 /* Bottom strecth */
245 cairo_matrix_translate(&matrix, 0, -height + 128);
246 cairo_pattern_set_matrix(pattern, &matrix);
247 cairo_rectangle(cr, x + margin, y + height - margin,
248 width - 2 * margin, margin);
249 cairo_fill(cr);
250
251 /* Left strecth */
252 cairo_matrix_init_translate(&matrix, 0, 64);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400253 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400254 cairo_matrix_translate(&matrix, -x, -y - height / 2);
255 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400256 cairo_rectangle(cr, x, y + top_margin,
257 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400258 cairo_fill(cr);
259
260 /* Right strecth */
261 cairo_matrix_translate(&matrix, -width + 128, 0);
262 cairo_pattern_set_matrix(pattern, &matrix);
Kristian Høgsberg9a686242010-08-18 15:28:04 -0400263 cairo_rectangle(cr, x + width - margin, y + top_margin,
264 margin, height - margin - top_margin);
Kristian Høgsbergdcb71b62010-06-15 17:16:35 -0400265 cairo_fill(cr);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500266}