source: libpipi/trunk/pipi/filter/blur.c @ 2681

Last change on this file since 2681 was 2681, checked in by Sam Hocevar, 12 years ago
  • blur.c: implement box blur; currently runs in O(n) but we could make it O(1) trivially.
File size: 2.8 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * blur.c: blur functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <math.h>
26
27#include "pipi.h"
28#include "pipi_internals.h"
29
30/* Any standard deviation below this value will be rounded up, in order
31 * to avoid ridiculously low values. exp(-1/(2*0.2*0.2)) is < 10^-5 so
32 * there is little chance that any value below 0.2 will be useful. */
33#define BLUR_EPSILON 0.2
34
35pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius)
36{
37    return pipi_gaussian_blur_ext(src, radius, radius, 0.0, 0.0);
38}
39
40pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry,
41                                     float dx, float dy)
42{
43    pipi_image_t *ret;
44    double *kernel;
45    double Kx, Ky, t = 0.0;
46    int i, j, krx, kry, m, n;
47
48    if(rx < BLUR_EPSILON) rx = BLUR_EPSILON;
49    if(ry < BLUR_EPSILON) ry = BLUR_EPSILON;
50
51    /* FIXME: the kernel becomes far too big with large values of dx, because
52     * we grow both left and right. Fix the growing direction. */
53    krx = (int)(3. * rx + .99999 + ceil(abs(dx)));
54    m = 2 * krx + 1;
55    Kx = -1. / (2. * rx * rx);
56
57    kry = (int)(3. * ry + .99999 + ceil(abs(dy)));
58    n = 2 * kry + 1;
59    Ky = -1. / (2. * ry * ry);
60
61    kernel = malloc(m * n * sizeof(double));
62
63    for(j = -kry; j <= kry; j++)
64    {
65        double ey = Ky * ((double)j + dy) * ((double)j + dy);
66
67        for(i = -krx; i <= krx; i++)
68        {
69            double ex = Kx * ((double)i + dx) * ((double)i + dx);
70            double d = exp(ex + ey);
71
72            kernel[(j + kry) * m + i + krx] = d;
73            t += d;
74        }
75    }
76
77    for(j = 0; j < n; j++)
78        for(i = 0; i < m; i++)
79            kernel[j * m + i] /= t;
80
81    ret = pipi_convolution(src, m, n, kernel);
82
83    free(kernel);
84
85    return ret;
86}
87
88/* FIXME: box blur would be incredibly faster using an accumulator instead
89 * of a convolution filter... */
90pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
91{
92    return pipi_box_blur_ext(src, size, size);
93}
94
95pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
96{
97    pipi_image_t *ret;
98    double *kernel;
99    int i;
100
101    kernel = malloc(m * n * sizeof(double));
102    for(i = 0; i < m * n; i++)
103        kernel[i] = 1. / (m * n);
104
105    ret = pipi_convolution(src, m, n, kernel);
106
107    free(kernel);
108
109    return ret;
110}
111
Note: See TracBrowser for help on using the repository browser.