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

Revision 2737, 3.9 KB checked in by sam, 5 years ago (diff)
  • blur.c: improve the Gaussian kernel interpolation.
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, 0.0);
38}
39
40pipi_image_t *pipi_gaussian_blur_ext(pipi_image_t *src, float rx, float ry,
41                                     float angle, float dx, float dy)
42{
43    pipi_image_t *ret;
44    double *kernel;
45    double Kx, Ky, t = 0.0, sint, cost, bbx, bby;
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    sint = sin(angle * M_PI / 180.);
52    cost = cos(angle * M_PI / 180.);
53
54    /* Compute the final ellipse's bounding box */
55    bbx = sqrt(rx * rx * cost * cost + ry * ry * sint * sint);
56    bby = sqrt(ry * ry * cost * cost + rx * rx * sint * sint);
57
58    /* FIXME: the kernel becomes far too big with large values of dx, because
59     * we grow both left and right. Fix the growing direction. */
60    krx = (int)(3. * bbx + .99999 + ceil(abs(dx)));
61    m = 2 * krx + 1;
62    Kx = -1. / (2. * rx * rx);
63
64    kry = (int)(3. * bby + .99999 + ceil(abs(dy)));
65    n = 2 * kry + 1;
66    Ky = -1. / (2. * ry * ry);
67
68    kernel = malloc(m * n * sizeof(double));
69
70    for(j = -kry; j <= kry; j++)
71    {
72        for(i = -krx; i <= krx; i++)
73        {
74            /* FIXME: this level of interpolation sucks. We should
75             * interpolate on the full NxN grid for better quality. */
76            static double const samples[] =
77            {
78                 .0,  .0, 1,
79                -.40, -.40, 0.8,
80                -.30,  .0,  0.9,
81                -.40,  .40, 0.8,
82                 .0,   .30, 0.9,
83                 .40,  .40, 0.8,
84                 .30,  .0,  0.9,
85                 .40, -.40, 0.8,
86                 .0,  -.30, 0.9,
87            };
88            double u, v, ex, ey, d = 0.;
89            unsigned int k;
90
91            for(k = 0; k < sizeof(samples) / sizeof(*samples) / 3; k++)
92            {
93                u = ((double)i + samples[k * 3]) * cost
94                     - ((double)j + samples[k * 3 + 1]) * sint + dx;
95                v = ((double)i + samples[k * 3]) * sint
96                     + ((double)j + samples[k * 3 + 1]) * cost + dy;
97                ex = Kx * u * u;
98                ey = Ky * v * v;
99                d += samples[k * 3 + 2] * exp(ex + ey);
100            }
101
102            kernel[(j + kry) * m + i + krx] = d;
103            t += d;
104        }
105    }
106
107    for(j = 0; j < n; j++)
108        for(i = 0; i < m; i++)
109            kernel[j * m + i] /= t;
110
111    ret = pipi_convolution(src, m, n, kernel);
112
113    free(kernel);
114
115    return ret;
116}
117
118/* FIXME: box blur would be incredibly faster using an accumulator instead
119 * of a convolution filter... */
120pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
121{
122    return pipi_box_blur_ext(src, size, size);
123}
124
125pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
126{
127    pipi_image_t *ret;
128    double *kernel;
129    int i;
130
131    kernel = malloc(m * n * sizeof(double));
132    for(i = 0; i < m * n; i++)
133        kernel[i] = 1. / (m * n);
134
135    ret = pipi_convolution(src, m, n, kernel);
136
137    free(kernel);
138
139    return ret;
140}
141
Note: See TracBrowser for help on using the repository browser.