|
- /*
- * 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 const *src, float radius)
- {
- pipi_image_t *dst;
- double *kernel;
- double K, L, t = 0.;
- int x, y, i, j, w, h, kr, kw;
-
- w = src->width;
- h = src->height;
-
- 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;
-
- dst = pipi_new(w, h);
-
- 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 r, g, b, f = kernel[(j + kr) * kw + (i + kr)];
-
- pipi_getpixel(src, x + i, y + j, &r, &g, &b);
- R += f * r;
- G += f * g;
- B += f * b;
- }
-
- pipi_setpixel(dst, x, y, R, G, B);
- }
- }
-
- return dst;
- }
|