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

Last change on this file since 2715 was 2715, checked in by Sam Hocevar, 15 years ago
  • blur.c: add an argument to the gaussian convolution to specify an angle. Of course since the kernel is no longer separable, it becomes awfully slow with large images. Here is a tileable brushed metal texture: # pipi pipi:random512x512 --wrap --blur 20x0r25 -o image.png
File size: 3.2 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            double u = (double)i * cost - (double)j * sint + dx;
75            double v = (double)j * cost + (double)i * sint + dy;
76            double ex = Kx * u * u;
77            double ey = Ky * v * v;
78            double d = exp(ex + ey);
79
80            kernel[(j + kry) * m + i + krx] = d;
81            t += d;
82        }
83    }
84
85    for(j = 0; j < n; j++)
86        for(i = 0; i < m; i++)
87            kernel[j * m + i] /= t;
88
89    ret = pipi_convolution(src, m, n, kernel);
90
91    free(kernel);
92
93    return ret;
94}
95
96/* FIXME: box blur would be incredibly faster using an accumulator instead
97 * of a convolution filter... */
98pipi_image_t *pipi_box_blur(pipi_image_t *src, int size)
99{
100    return pipi_box_blur_ext(src, size, size);
101}
102
103pipi_image_t *pipi_box_blur_ext(pipi_image_t *src, int m, int n)
104{
105    pipi_image_t *ret;
106    double *kernel;
107    int i;
108
109    kernel = malloc(m * n * sizeof(double));
110    for(i = 0; i < m * n; i++)
111        kernel[i] = 1. / (m * n);
112
113    ret = pipi_convolution(src, m, n, kernel);
114
115    free(kernel);
116
117    return ret;
118}
119
Note: See TracBrowser for help on using the repository browser.