2 * Copyright © 2008 Kristian Høgsberg
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.
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
29 #include "cairo-util.h"
33 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
36 surface_flush_device(cairo_surface_t *surface)
38 cairo_device_t *device;
40 device = cairo_surface_get_device(surface);
42 cairo_device_flush(device);
46 blur_surface(cairo_surface_t *surface, int margin)
48 int32_t width, height, stride, x, y, z, w;
50 uint32_t *s, *d, a, p;
51 int i, j, k, size, half;
55 size = ARRAY_LENGTH(kernel);
56 width = cairo_image_surface_get_width(surface);
57 height = cairo_image_surface_get_height(surface);
58 stride = cairo_image_surface_get_stride(surface);
59 src = cairo_image_surface_get_data(surface);
61 dst = malloc(height * stride);
65 for (i = 0; i < size; i++) {
67 kernel[i] = exp(- f * f / ARRAY_LENGTH(kernel)) * 10000;
71 for (i = 0; i < height; i++) {
72 s = (uint32_t *) (src + i * stride);
73 d = (uint32_t *) (dst + i * stride);
74 for (j = 0; j < width; j++) {
75 if (margin < j && j < width - margin) {
84 for (k = 0; k < size; k++) {
85 if (j - half + k < 0 || j - half + k >= width)
89 x += (p >> 24) * kernel[k];
90 y += ((p >> 16) & 0xff) * kernel[k];
91 z += ((p >> 8) & 0xff) * kernel[k];
92 w += (p & 0xff) * kernel[k];
94 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
98 for (i = 0; i < height; i++) {
99 s = (uint32_t *) (dst + i * stride);
100 d = (uint32_t *) (src + i * stride);
101 for (j = 0; j < width; j++) {
102 if (margin <= i && i < height - margin) {
111 for (k = 0; k < size; k++) {
112 if (i - half + k < 0 || i - half + k >= height)
114 s = (uint32_t *) (dst + (i - half + k) * stride);
117 x += (p >> 24) * kernel[k];
118 y += ((p >> 16) & 0xff) * kernel[k];
119 z += ((p >> 8) & 0xff) * kernel[k];
120 w += (p & 0xff) * kernel[k];
122 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
127 cairo_surface_mark_dirty(surface);
131 tile_mask(cairo_t *cr, cairo_surface_t *surface,
132 int x, int y, int width, int height, int margin, int top_margin)
134 cairo_pattern_t *pattern;
135 cairo_matrix_t matrix;
136 int i, fx, fy, vmargin;
138 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
139 pattern = cairo_pattern_create_for_surface (surface);
141 for (i = 0; i < 4; i++) {
145 cairo_matrix_init_translate(&matrix,
146 -x + fx * (128 - width),
147 -y + fy * (128 - height));
148 cairo_pattern_set_matrix(pattern, &matrix);
153 vmargin = top_margin;
155 cairo_reset_clip(cr);
157 x + fx * (width - margin),
158 y + fy * (height - vmargin),
161 cairo_mask(cr, pattern);
165 cairo_matrix_init_translate(&matrix, 64, 0);
166 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
167 cairo_matrix_translate(&matrix, -x - width / 2, -y);
168 cairo_pattern_set_matrix(pattern, &matrix);
169 cairo_rectangle(cr, x + margin, y, width - 2 * margin, margin);
171 cairo_reset_clip(cr);
175 width - 2 * margin, margin);
177 cairo_mask(cr, pattern);
180 cairo_matrix_translate(&matrix, 0, -height + 128);
181 cairo_pattern_set_matrix(pattern, &matrix);
183 cairo_reset_clip(cr);
184 cairo_rectangle(cr, x + margin, y + height - margin,
185 width - 2 * margin, margin);
187 cairo_mask(cr, pattern);
190 cairo_matrix_init_translate(&matrix, 0, 64);
191 cairo_matrix_scale(&matrix, 1, 64.0 / (height - 2 * margin));
192 cairo_matrix_translate(&matrix, -x, -y - height / 2);
193 cairo_pattern_set_matrix(pattern, &matrix);
194 cairo_reset_clip(cr);
195 cairo_rectangle(cr, x, y + margin, margin, height - 2 * margin);
197 cairo_mask(cr, pattern);
200 cairo_matrix_translate(&matrix, -width + 128, 0);
201 cairo_pattern_set_matrix(pattern, &matrix);
202 cairo_rectangle(cr, x + width - margin, y + margin,
203 margin, height - 2 * margin);
204 cairo_reset_clip(cr);
206 cairo_mask(cr, pattern);
208 cairo_pattern_destroy(pattern);
209 cairo_reset_clip(cr);
213 tile_source(cairo_t *cr, cairo_surface_t *surface,
214 int x, int y, int width, int height, int margin, int top_margin)
216 cairo_pattern_t *pattern;
217 cairo_matrix_t matrix;
218 int i, fx, fy, vmargin;
220 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
221 pattern = cairo_pattern_create_for_surface (surface);
222 cairo_set_source(cr, pattern);
223 cairo_pattern_destroy(pattern);
225 for (i = 0; i < 4; i++) {
229 cairo_matrix_init_translate(&matrix,
230 -x + fx * (128 - width),
231 -y + fy * (128 - height));
232 cairo_pattern_set_matrix(pattern, &matrix);
237 vmargin = top_margin;
240 x + fx * (width - margin),
241 y + fy * (height - vmargin),
247 cairo_matrix_init_translate(&matrix, 64, 0);
248 cairo_matrix_scale(&matrix, 64.0 / (width - 2 * margin), 1);
249 cairo_matrix_translate(&matrix, -x - width / 2, -y);
250 cairo_pattern_set_matrix(pattern, &matrix);
251 cairo_rectangle(cr, x + margin, y, width - 2 * margin, top_margin);
255 cairo_matrix_translate(&matrix, 0, -height + 128);
256 cairo_pattern_set_matrix(pattern, &matrix);
257 cairo_rectangle(cr, x + margin, y + height - margin,
258 width - 2 * margin, margin);
262 cairo_matrix_init_translate(&matrix, 0, 64);
263 cairo_matrix_scale(&matrix, 1, 64.0 / (height - margin - top_margin));
264 cairo_matrix_translate(&matrix, -x, -y - height / 2);
265 cairo_pattern_set_matrix(pattern, &matrix);
266 cairo_rectangle(cr, x, y + top_margin,
267 margin, height - margin - top_margin);
271 cairo_matrix_translate(&matrix, -width + 128, 0);
272 cairo_pattern_set_matrix(pattern, &matrix);
273 cairo_rectangle(cr, x + width - margin, y + top_margin,
274 margin, height - margin - top_margin);
279 rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
281 cairo_move_to(cr, x0, y0 + radius);
282 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
283 cairo_line_to(cr, x1 - radius, y0);
284 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
285 cairo_line_to(cr, x1, y1 - radius);
286 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
287 cairo_line_to(cr, x0 + radius, y1);
288 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
289 cairo_close_path(cr);
293 load_jpeg(const char *filename)
295 struct jpeg_decompress_struct cinfo;
296 struct jpeg_error_mgr jerr;
299 JSAMPLE *data, *rows[4];
301 cinfo.err = jpeg_std_error(&jerr);
302 jpeg_create_decompress(&cinfo);
304 fp = fopen(filename, "rb");
306 fprintf(stderr, "can't open %s\n", filename);
309 jpeg_stdio_src(&cinfo, fp);
311 jpeg_read_header(&cinfo, TRUE);
313 cinfo.out_color_space = JCS_EXT_BGRX;
314 jpeg_start_decompress(&cinfo);
316 stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24,
318 data = malloc(stride * cinfo.output_height);
320 fprintf(stderr, "couldn't allocate image data\n");
324 while (cinfo.output_scanline < cinfo.output_height) {
325 for (i = 0; i < ARRAY_LENGTH(rows); i++, p += stride)
326 rows[i] = data + (cinfo.output_scanline + i) * stride;
328 jpeg_read_scanlines(&cinfo, rows, ARRAY_LENGTH(rows));
331 jpeg_finish_decompress(&cinfo);
335 jpeg_destroy_decompress(&cinfo);
337 return cairo_image_surface_create_for_data (data,