From 7d147f3576327e6390452834227e7ebec003dcf6 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 28 Jul 2008 23:01:21 +0000 Subject: [PATCH] * Test implementation of Gaussian blurring. It's awfully slow and does not use the separation property, but it's just for a test. git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2603 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/Makefile.am | 1 + pipi/filter/blur.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++ pipi/pipi.h | 2 ++ 3 files changed, 83 insertions(+) create mode 100644 pipi/filter/blur.c diff --git a/pipi/Makefile.am b/pipi/Makefile.am index 6625645..f49d330 100644 --- a/pipi/Makefile.am +++ b/pipi/Makefile.am @@ -27,6 +27,7 @@ libpipi_la_SOURCES = \ dither.c \ test.c \ $(codec_sources) \ + filter/blur.c \ $(NULL) libpipi_la_CFLAGS = $(codec_cflags) libpipi_la_LDFLAGS = $(codec_libs) \ diff --git a/pipi/filter/blur.c b/pipi/filter/blur.c new file mode 100644 index 0000000..ac6b28f --- /dev/null +++ b/pipi/filter/blur.c @@ -0,0 +1,80 @@ +/* + * libpipi Proper image processing implementation library + * Copyright (c) 2004-2008 Sam Hocevar + * 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 +#include +#include +#include + +#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; +} + diff --git a/pipi/pipi.h b/pipi/pipi.h index a9a3128..b4f2303 100644 --- a/pipi/pipi.h +++ b/pipi/pipi.h @@ -40,6 +40,8 @@ extern int pipi_setpixel(pipi_image_t *img, int x, int y, extern pipi_image_t *pipi_resize(pipi_image_t const *, int, int); +extern pipi_image_t *pipi_gaussian_blur(pipi_image_t const *, float); + extern void pipi_dither_24to16(pipi_image_t *img); extern void pipi_test(pipi_image_t *);