2008-12-02 23:15:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting documentation, and
|
|
|
|
* that the name of the copyright holders not be used in advertising or
|
|
|
|
* publicity pertaining to distribution of the software without specific,
|
|
|
|
* written prior permission. The copyright holders make no representations
|
|
|
|
* about the suitability of this software for any purpose. It is provided "as
|
|
|
|
* is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-11-09 06:46:30 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
#include "cairo-util.h"
|
|
|
|
|
2008-11-18 06:23:55 +03:00
|
|
|
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
|
|
|
|
|
2008-11-09 06:46:30 +03:00
|
|
|
void
|
2008-11-09 07:27:27 +03:00
|
|
|
blur_surface(cairo_surface_t *surface, int margin)
|
2008-11-09 06:46:30 +03:00
|
|
|
{
|
|
|
|
int32_t width, height, stride, x, y, z, w;
|
|
|
|
uint8_t *src, *dst;
|
|
|
|
uint32_t *s, *d, a, p;
|
2008-11-18 06:23:55 +03:00
|
|
|
int i, j, k, size, half;
|
2009-09-21 21:43:46 +04:00
|
|
|
uint8_t kernel[35];
|
2008-11-09 06:46:30 +03:00
|
|
|
double f;
|
|
|
|
|
2008-11-18 06:23:55 +03:00
|
|
|
size = ARRAY_LENGTH(kernel);
|
2008-11-09 06:46:30 +03:00
|
|
|
width = cairo_image_surface_get_width(surface);
|
|
|
|
height = cairo_image_surface_get_height(surface);
|
|
|
|
stride = cairo_image_surface_get_stride(surface);
|
|
|
|
src = cairo_image_surface_get_data(surface);
|
|
|
|
|
2009-09-12 23:58:48 +04:00
|
|
|
dst = malloc(height * stride);
|
2008-11-09 06:46:30 +03:00
|
|
|
|
|
|
|
half = size / 2;
|
|
|
|
a = 0;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
f = (i - half);
|
|
|
|
kernel[i] = exp(- f * f / 30.0) * 80;
|
|
|
|
a += kernel[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
s = (uint32_t *) (src + i * stride);
|
|
|
|
d = (uint32_t *) (dst + i * stride);
|
|
|
|
for (j = 0; j < width; j++) {
|
2008-11-18 06:23:55 +03:00
|
|
|
if (margin < j && j < width - margin) {
|
|
|
|
d[j] = s[j];
|
2008-11-09 07:27:27 +03:00
|
|
|
continue;
|
2008-11-18 06:23:55 +03:00
|
|
|
}
|
|
|
|
|
2008-11-09 06:46:30 +03:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
z = 0;
|
|
|
|
w = 0;
|
|
|
|
for (k = 0; k < size; k++) {
|
|
|
|
if (j - half + k < 0 || j - half + k >= width)
|
|
|
|
continue;
|
|
|
|
p = s[j - half + k];
|
|
|
|
|
|
|
|
x += (p >> 24) * kernel[k];
|
|
|
|
y += ((p >> 16) & 0xff) * kernel[k];
|
|
|
|
z += ((p >> 8) & 0xff) * kernel[k];
|
|
|
|
w += (p & 0xff) * kernel[k];
|
|
|
|
}
|
|
|
|
d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
s = (uint32_t *) (dst + i * stride);
|
|
|
|
d = (uint32_t *) (src + i * stride);
|
|
|
|
for (j = 0; j < width; j++) {
|
2008-11-18 06:23:55 +03:00
|
|
|
if (margin <= i && i < height - margin) {
|
|
|
|
d[j] = s[j];
|
2008-11-09 07:27:27 +03:00
|
|
|
continue;
|
2008-11-18 06:23:55 +03:00
|
|
|
}
|
|
|
|
|
2008-11-09 06:46:30 +03:00
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
z = 0;
|
|
|
|
w = 0;
|
|
|
|
for (k = 0; k < size; k++) {
|
|
|
|
if (i - half + k < 0 || i - half + k >= height)
|
|
|
|
continue;
|
|
|
|
s = (uint32_t *) (dst + (i - half + k) * stride);
|
|
|
|
p = s[j];
|
|
|
|
|
|
|
|
x += (p >> 24) * kernel[k];
|
|
|
|
y += ((p >> 16) & 0xff) * kernel[k];
|
|
|
|
z += ((p >> 8) & 0xff) * kernel[k];
|
|
|
|
w += (p & 0xff) * kernel[k];
|
|
|
|
}
|
|
|
|
d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-12 23:58:48 +04:00
|
|
|
free(dst);
|
2008-11-09 06:46:30 +03:00
|
|
|
}
|