Browse Source

Better Canvas::blur()

v1.x
Tj Holowaychuk 15 years ago
parent
commit
42aca813f1
  1. 155
      src/blur.cc

155
src/blur.cc

@ -23,118 +23,73 @@
*/ */
#include <math.h> #include <math.h>
#include <memory.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <cairo.h> #include <cairo.h>
#include "canvas.h" #include "canvas.h"
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
void void
Canvas::blur(cairo_surface_t *surface, int radius) { Canvas::blur(cairo_surface_t *surface, int radius) {
cairo_surface_t *tmp; // Steve Hanov, 2009
int width, height; // Released into the public domain.
int src_stride, dst_stride;
int x, y, z, w; // get width, height
uint8_t *src, *dst; int width = cairo_image_surface_get_width( surface );
uint32_t *s, *d, a, p; int height = cairo_image_surface_get_height( surface );
int i, j, k; unsigned char* dst = (unsigned char*)malloc(width*height*4);
uint8_t kernel[17]; unsigned* precalc =
const int size = ARRAY_LENGTH (kernel); (unsigned*)malloc(width*height*sizeof(unsigned));
const int half = size / 2; unsigned char* src = cairo_image_surface_get_data( surface );
double mul=1.f/((radius*2)*(radius*2));
if (cairo_surface_status (surface)) int channel;
return;
// The number of times to perform the averaging. According to wikipedia,
width = cairo_image_surface_get_width (surface); // three iterations is good enough to pass for a gaussian.
height = cairo_image_surface_get_height (surface); const int MAX_ITERATIONS = 3;
int iteration;
switch (cairo_image_surface_get_format (surface)) {
case CAIRO_FORMAT_A1: memcpy( dst, src, width*height*4 );
default:
/* Don't even think about it! */ for ( iteration = 0; iteration < MAX_ITERATIONS; iteration++ ) {
return; for( channel = 0; channel < 4; channel++ ) {
int x,y;
case CAIRO_FORMAT_A8:
/* Handle a8 surfaces by effectively unrolling the loops by a // precomputation step.
* factor of 4 - this is safe since we know that stride has to be a unsigned char* pix = src;
* multiple of uint32_t. */ unsigned* pre = precalc;
width /= 4;
break; pix += channel;
for (y=0;y<height;y++) {
case CAIRO_FORMAT_RGB24: for (x=0;x<width;x++) {
case CAIRO_FORMAT_ARGB32: int tot=pix[0];
break; if (x>0) tot+=pre[-1];
if (y>0) tot+=pre[-width];
if (x>0 && y>0) tot-=pre[-width-1];
*pre++=tot;
pix += 4;
} }
tmp = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
if (cairo_surface_status (tmp))
return;
src = cairo_image_surface_get_data (surface);
src_stride = cairo_image_surface_get_stride (surface);
dst = cairo_image_surface_get_data (tmp);
dst_stride = cairo_image_surface_get_stride (tmp);
a = 0;
for (i = 0; i < size; i++) {
double f = i - half;
a += kernel[i] = exp (- f * f / 30.0) * 80;
} }
/* Horizontally blur from surface -> tmp */ // blur step.
for (i = 0; i < height; i++) { pix = dst + (int)radius * width * 4 + (int)radius * 4 + channel;
s = (uint32_t *) (src + i * src_stride); for (y=radius;y<height-radius;y++) {
d = (uint32_t *) (dst + i * dst_stride); for (x=radius;x<width-radius;x++) {
for (j = 0; j < width; j++) { int l = x < radius ? 0 : x - radius;
if (radius < j && j < width - radius) { int t = y < radius ? 0 : y - radius;
d[j] = s[j]; int r = x + radius >= width ? width - 1 : x + radius;
continue; int b = y + radius >= height ? height - 1 : y + radius;
int tot = precalc[r+b*width] + precalc[l+t*width] -
precalc[l+b*width] - precalc[r+t*width];
*pix=(unsigned char)(tot*mul);
pix += 4;
} }
pix += (int)radius * 2 * 4;
x = y = z = 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) & 0xff) * kernel[k];
y += ((p >> 16) & 0xff) * kernel[k];
z += ((p >> 8) & 0xff) * kernel[k];
w += ((p >> 0) & 0xff) * kernel[k];
}
d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
}
}
/* Then vertically blur from tmp -> surface */
for (i = 0; i < height; i++) {
s = (uint32_t *) (dst + i * dst_stride);
d = (uint32_t *) (src + i * src_stride);
for (j = 0; j < width; j++) {
if (radius <= i && i < height - radius) {
d[j] = s[j];
continue;
}
x = y = z = 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) * dst_stride);
p = s[j];
x += ((p >> 24) & 0xff) * kernel[k];
y += ((p >> 16) & 0xff) * kernel[k];
z += ((p >> 8) & 0xff) * kernel[k];
w += ((p >> 0) & 0xff) * kernel[k];
} }
d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
} }
memcpy( src, dst, width*height*4 );
} }
cairo_surface_destroy (tmp); free( dst );
cairo_surface_mark_dirty (surface); free( precalc );
} }
Loading…
Cancel
Save