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

Last change on this file since 2719 was 2719, checked in by sam, 6 years ago
  • blur.c: improve the Gaussian kernel generation by interpolating its values in a quincunx pattern.
File size: 3.6 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, 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            static double const samples[] =
75            {
76                -.50, -.50, 0.5,
77                 .50, -.50, 0.5,
78                 .0,  .0, 1,
79                -.50,  .50, 0.5,
80                 .50,  .50, 0.5
81            };
82            double u, v, ex, ey, d = 0.;
83            int k;
84
85            for(k = 0; k < 5; k++)
86            {
87                u = ((double)i + samples[k * 3]) * cost
88                     - ((double)j + samples[k * 3 + 1]) * sint + dx;
89                v = ((double)i + samples[k * 3]) * sint
90                     + ((double)j + samples[k * 3 + 1]) * cost + dy;
91                ex = Kx * u * u;
92                ey = Ky * v * v;
93                d += samples[k * 3 + 2] * exp(ex + ey);
94            }
95
96            kernel[(j + kry) * m + i + krx] = d;
97            t += d;
98        }
99    }
100
101    for(j = 0; j < n; j++)
102        for(i = 0; i < m; i++)
103            kernel[j * m + i] /= t;
104
105    ret = pipi_convolution(src, m, n, kernel);
106
107    free(kernel);
108
109    return ret;
110}
111
112/* FIXME: box blur would be incredibly faster using an accumulator instead
113 * of a convolution filter... */
114pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
115{
116    return pipi_box_blur_ext(src, size, size);
117}
118
119pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
120{
121    pipi_image_t *ret;
122    double *kernel;
123    int i;
124
125    kernel = malloc(m * n * sizeof(double));
126    for(i = 0; i < m * n; i++)
127        kernel[i] = 1. / (m * n);
128
129    ret = pipi_convolution(src, m, n, kernel);
130
131    free(kernel);
132
133    return ret;
134}
135
Note: See TracBrowser for help on using the repository browser.