|
- /*
- * libpipi Proper image processing implementation library
- * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
- * All Rights Reserved
- *
- * $Id$
- *
- * This library is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details.
- */
-
- /*
- * blur.c: blur functions
- */
-
- #include "config.h"
- #include "common.h"
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
-
- #include "pipi.h"
- #include "pipi_internals.h"
-
- 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 K, L, t = 0.;
- int x, y, i, j, w, h, kr, kw;
-
- w = src->w;
- h = src->h;
-
- srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
- srcdata = (float *)srcp->pixels;
-
- dst = pipi_new(w, h);
- dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
- dstdata = (float *)dstp->pixels;
-
- kr = (int)(3. * radius + 0.99999);
- kw = 2 * kr + 1;
- K = 1. / (2. * M_PI * radius * radius);
- L = -1. / (2. * radius * radius);
-
- kernel = malloc(kw * kw * sizeof(double));
- for(j = -kr; j <= kr; j++)
- for(i = -kr; i <= kr; i++)
- t += kernel[(j + kr) * kw + (i + kr)]
- = exp(L * (i * i + j * j)) * K;
-
- for(j = -kr; j <= kr; j++)
- for(i = -kr; i <= kr; i++)
- kernel[(j + kr) * kw + (i + kr)] /= t;
-
- for(y = 0; y < h; y++)
- {
- for(x = 0; x < w; x++)
- {
- double R = 0., G = 0., B = 0.;
-
- for(j = -kr; j <= kr; j++)
- for(i = -kr; i <= kr; i++)
- {
- double f = kernel[(j + kr) * kw + (i + kr)];
-
- R += f * srcdata[((y + j) * w + x + i) * 4];
- G += f * srcdata[((y + j) * w + x + i) * 4 + 1];
- B += f * srcdata[((y + j) * w + x + i) * 4 + 2];
- }
-
- dstdata[(y * w + x) * 4] = R;
- dstdata[(y * w + x) * 4 + 1] = G;
- dstdata[(y * w + x) * 4 + 2] = B;
- }
- }
-
- return dst;
- }
|