Bladeren bron

* blur.c: separated blur; it's now incredibly faster.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2608 92316355-f0b4-4df1-b90c-862c8a59935f
remotes/tiles
sam 16 jaren geleden
bovenliggende
commit
42af43ad99
1 gewijzigde bestanden met toevoegingen van 47 en 23 verwijderingen
  1. +47
    -23
      pipi/filter/blur.c

+ 47
- 23
pipi/filter/blur.c Bestand weergeven

@@ -32,9 +32,9 @@ pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius)
pipi_image_t *dst;
pipi_pixels_t *srcp, *dstp;
float *srcdata, *dstdata;
double *kernel;
double *kernel, *buffer;
double K, L;
int x, y, i, j, w, h, kr, kw;
int x, y, i, w, h, kr, kw;

w = src->w;
h = src->h;
@@ -48,40 +48,61 @@ pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius)

kr = (int)(3. * radius + 0.99999);
kw = 2 * kr + 1;
K = 1. / (2. * M_PI * radius * radius);
K = 1. / (sqrt(2. * M_PI) * radius);
L = -1. / (2. * radius * radius);

kernel = malloc(kw * kw * sizeof(double));
for(j = -kr; j <= kr; j++)
for(i = -kr; i <= kr; i++)
kernel[(j + kr) * kw + (i + kr)] = exp(L * (i * i + j * j)) * K;
kernel = malloc(kw * sizeof(double));
for(i = -kr; i <= kr; i++)
kernel[i + kr] = exp(L * i * i) * K;

buffer = malloc(w * h * 4 * sizeof(double));

for(y = 0; y < h; y++)
{
for(x = 0; x < w; x++)
{
double R = 0., G = 0., B = 0., t = 0.;
int x2, y2;
int x2;

for(j = -kr; j <= kr; j++)
for(i = -kr; i <= kr; i++)
{
y2 = y + j;
if(y2 < 0) y2 = 0;
else if(y2 >= h) y2 = h - 1;
double f = kernel[i + kr];

for(i = -kr; i <= kr; i++)
{
double f = kernel[(j + kr) * kw + (i + kr)];
x2 = x + i;
if(x2 < 0) x2 = 0;
else if(x2 >= w) x2 = w - 1;

R += f * srcdata[(y * w + x2) * 4];
G += f * srcdata[(y * w + x2) * 4 + 1];
B += f * srcdata[(y * w + x2) * 4 + 2];
t += f;
}

x2 = x + i;
if(x2 < 0) x2 = 0;
else if(x2 >= w) x2 = w - 1;
buffer[(y * w + x) * 4] = R / t;
buffer[(y * w + x) * 4 + 1] = G / t;
buffer[(y * w + x) * 4 + 2] = B / t;
}
}

R += f * srcdata[(y2 * w + x2) * 4];
G += f * srcdata[(y2 * w + x2) * 4 + 1];
B += f * srcdata[(y2 * w + x2) * 4 + 2];
t += f;
}
for(y = 0; y < h; y++)
{
for(x = 0; x < w; x++)
{
double R = 0., G = 0., B = 0., t = 0.;
int y2;

for(i = -kr; i <= kr; i++)
{
double f = kernel[i + kr];

y2 = y + i;
if(y2 < 0) y2 = 0;
else if(y2 >= h) y2 = h - 1;

R += f * buffer[(y2 * w + x) * 4];
G += f * buffer[(y2 * w + x) * 4 + 1];
B += f * buffer[(y2 * w + x) * 4 + 2];
t += f;
}

dstdata[(y * w + x) * 4] = R / t;
@@ -90,6 +111,9 @@ pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius)
}
}

free(buffer);
free(kernel);

return dst;
}


Laden…
Annuleren
Opslaan