/* * 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 *src, float radius) { pipi_image_t *dst; pipi_pixels_t *srcp, *dstp; float *srcdata, *dstdata; double *kernel; double K, L; 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++) kernel[(j + kr) * kw + (i + kr)] = exp(L * (i * i + j * j)) * K; for(y = 0; y < h; y++) { for(x = 0; x < w; x++) { double R = 0., G = 0., B = 0., t = 0.; int x2, y2; for(j = -kr; j <= kr; j++) { y2 = y + j; if(y2 < 0) y2 = 0; else if(y2 >= h) y2 = h - 1; 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[(y2 * w + x2) * 4]; G += f * srcdata[(y2 * w + x2) * 4 + 1]; B += f * srcdata[(y2 * w + x2) * 4 + 2]; t += f; } } dstdata[(y * w + x) * 4] = R / t; dstdata[(y * w + x) * 4 + 1] = G / t; dstdata[(y * w + x) * 4 + 2] = B / t; } } return dst; }