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

Revision 2619, 3.6 KB checked in by sam, 5 years ago (diff)
  • filter/blur.c: avoid annoying side effects with very small or negative blur radii.
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 *dst;
44    pipi_pixels_t *srcp, *dstp;
45    float *srcdata, *dstdata;
46    double *kernel, *buffer;
47    double K;
48    int x, y, i, w, h, kr, kw;
49
50    if(rx < BLUR_EPSILON) rx = BLUR_EPSILON;
51    if(ry < BLUR_EPSILON) ry = BLUR_EPSILON;
52
53    w = src->w;
54    h = src->h;
55
56    srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
57    srcdata = (float *)srcp->pixels;
58
59    dst = pipi_new(w, h);
60    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
61    dstdata = (float *)dstp->pixels;
62
63    buffer = malloc(w * h * 4 * sizeof(double));
64
65    kr = (int)(3. * rx + 1.99999);
66    kw = 2 * kr + 1;
67    K = -1. / (2. * rx * rx);
68
69    kernel = malloc(kw * sizeof(double));
70    for(i = -kr; i <= kr; i++)
71        kernel[i + kr] = exp(K * ((double)i - dx) * ((double)i - dx));
72
73    for(y = 0; y < h; y++)
74    {
75        for(x = 0; x < w; x++)
76        {
77            double R = 0., G = 0., B = 0., t = 0.;
78            int x2, off = 4 * (y * w + x);
79
80            for(i = -kr; i <= kr; i++)
81            {
82                double f = kernel[i + kr];
83
84                x2 = x + i;
85                if(x2 < 0) x2 = 0;
86                else if(x2 >= w) x2 = w - 1;
87
88                R += f * srcdata[(y * w + x2) * 4];
89                G += f * srcdata[(y * w + x2) * 4 + 1];
90                B += f * srcdata[(y * w + x2) * 4 + 2];
91                t += f;
92            }
93
94            buffer[off] = R / t;
95            buffer[off + 1] = G / t;
96            buffer[off + 2] = B / t;
97        }
98    }
99
100    free(kernel);
101
102    kr = (int)(3. * ry + 1.99999);
103    kw = 2 * kr + 1;
104    K = -1. / (2. * ry * ry);
105
106    kernel = malloc(kw * sizeof(double));
107    for(i = -kr; i <= kr; i++)
108        kernel[i + kr] = exp(K * ((double)i - dy) * ((double)i - dy));
109
110    for(y = 0; y < h; y++)
111    {
112        for(x = 0; x < w; x++)
113        {
114            double R = 0., G = 0., B = 0., t = 0.;
115            int y2, off = 4 * (y * w + x);
116
117            for(i = -kr; i <= kr; i++)
118            {
119                double f = kernel[i + kr];
120
121                y2 = y + i;
122                if(y2 < 0) y2 = 0;
123                else if(y2 >= h) y2 = h - 1;
124
125                R += f * buffer[(y2 * w + x) * 4];
126                G += f * buffer[(y2 * w + x) * 4 + 1];
127                B += f * buffer[(y2 * w + x) * 4 + 2];
128                t += f;
129            }
130
131            dstdata[off] = R / t;
132            dstdata[off + 1] = G / t;
133            dstdata[off + 2] = B / t;
134        }
135    }
136
137    free(buffer);
138    free(kernel);
139
140    return dst;
141}
142
Note: See TracBrowser for help on using the repository browser.