Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 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øgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 27 | #include <math.h> |
| 28 | #include <cairo.h> |
| 29 | #include "cairo-util.h" |
| 30 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 31 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 32 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 33 | void |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 34 | blur_surface(cairo_surface_t *surface, int margin) |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 35 | { |
| 36 | cairo_surface_t *tmp; |
| 37 | int32_t width, height, stride, x, y, z, w; |
| 38 | uint8_t *src, *dst; |
| 39 | uint32_t *s, *d, a, p; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 40 | int i, j, k, size, half; |
Kristian Høgsberg | e9d550b | 2008-11-19 00:49:39 -0500 | [diff] [blame] | 41 | uint8_t kernel[17]; |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 42 | double f; |
| 43 | |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 44 | size = ARRAY_LENGTH(kernel); |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 45 | width = cairo_image_surface_get_width(surface); |
| 46 | height = cairo_image_surface_get_height(surface); |
| 47 | stride = cairo_image_surface_get_stride(surface); |
| 48 | src = cairo_image_surface_get_data(surface); |
| 49 | |
| 50 | tmp = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); |
| 51 | dst = cairo_image_surface_get_data(tmp); |
| 52 | |
| 53 | half = size / 2; |
| 54 | a = 0; |
| 55 | for (i = 0; i < size; i++) { |
| 56 | f = (i - half); |
| 57 | kernel[i] = exp(- f * f / 30.0) * 80; |
| 58 | a += kernel[i]; |
| 59 | } |
| 60 | |
| 61 | for (i = 0; i < height; i++) { |
| 62 | s = (uint32_t *) (src + i * stride); |
| 63 | d = (uint32_t *) (dst + i * stride); |
| 64 | for (j = 0; j < width; j++) { |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 65 | if (margin < j && j < width - margin) { |
| 66 | d[j] = s[j]; |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 67 | continue; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 70 | x = 0; |
| 71 | y = 0; |
| 72 | z = 0; |
| 73 | w = 0; |
| 74 | for (k = 0; k < size; k++) { |
| 75 | if (j - half + k < 0 || j - half + k >= width) |
| 76 | continue; |
| 77 | p = s[j - half + k]; |
| 78 | |
| 79 | x += (p >> 24) * kernel[k]; |
| 80 | y += ((p >> 16) & 0xff) * kernel[k]; |
| 81 | z += ((p >> 8) & 0xff) * kernel[k]; |
| 82 | w += (p & 0xff) * kernel[k]; |
| 83 | } |
| 84 | d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for (i = 0; i < height; i++) { |
| 89 | s = (uint32_t *) (dst + i * stride); |
| 90 | d = (uint32_t *) (src + i * stride); |
| 91 | for (j = 0; j < width; j++) { |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 92 | if (margin <= i && i < height - margin) { |
| 93 | d[j] = s[j]; |
Kristian Høgsberg | 10bdd29 | 2008-11-08 23:27:27 -0500 | [diff] [blame] | 94 | continue; |
Kristian Høgsberg | 8733026 | 2008-11-17 22:23:55 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Kristian Høgsberg | 2f2cfae | 2008-11-08 22:46:30 -0500 | [diff] [blame] | 97 | x = 0; |
| 98 | y = 0; |
| 99 | z = 0; |
| 100 | w = 0; |
| 101 | for (k = 0; k < size; k++) { |
| 102 | if (i - half + k < 0 || i - half + k >= height) |
| 103 | continue; |
| 104 | s = (uint32_t *) (dst + (i - half + k) * stride); |
| 105 | p = s[j]; |
| 106 | |
| 107 | x += (p >> 24) * kernel[k]; |
| 108 | y += ((p >> 16) & 0xff) * kernel[k]; |
| 109 | z += ((p >> 8) & 0xff) * kernel[k]; |
| 110 | w += (p & 0xff) * kernel[k]; |
| 111 | } |
| 112 | d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | cairo_surface_destroy(tmp); |
| 117 | } |