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

Revision 2608, 2.9 KB checked in by sam, 5 years ago (diff)
  • blur.c: separated blur; it's now incredibly faster.
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
30pipi_image_t *pipi_gaussian_blur(pipi_image_t *src, float radius)
31{
32    pipi_image_t *dst;
33    pipi_pixels_t *srcp, *dstp;
34    float *srcdata, *dstdata;
35    double *kernel, *buffer;
36    double K, L;
37    int x, y, i, w, h, kr, kw;
38
39    w = src->w;
40    h = src->h;
41
42    srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
43    srcdata = (float *)srcp->pixels;
44
45    dst = pipi_new(w, h);
46    dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
47    dstdata = (float *)dstp->pixels;
48
49    kr = (int)(3. * radius + 0.99999);
50    kw = 2 * kr + 1;
51    K = 1. / (sqrt(2. * M_PI) * radius);
52    L = -1. / (2. * radius * radius);
53
54    kernel = malloc(kw * sizeof(double));
55    for(i = -kr; i <= kr; i++)
56        kernel[i + kr] = exp(L * i * i) * K;
57
58    buffer = malloc(w * h * 4 * sizeof(double));
59
60    for(y = 0; y < h; y++)
61    {
62        for(x = 0; x < w; x++)
63        {
64            double R = 0., G = 0., B = 0., t = 0.;
65            int x2;
66
67            for(i = -kr; i <= kr; i++)
68            {
69                double f = kernel[i + kr];
70
71                x2 = x + i;
72                if(x2 < 0) x2 = 0;
73                else if(x2 >= w) x2 = w - 1;
74
75                R += f * srcdata[(y * w + x2) * 4];
76                G += f * srcdata[(y * w + x2) * 4 + 1];
77                B += f * srcdata[(y * w + x2) * 4 + 2];
78                t += f;
79            }
80
81            buffer[(y * w + x) * 4] = R / t;
82            buffer[(y * w + x) * 4 + 1] = G / t;
83            buffer[(y * w + x) * 4 + 2] = B / t;
84        }
85    }
86
87    for(y = 0; y < h; y++)
88    {
89        for(x = 0; x < w; x++)
90        {
91            double R = 0., G = 0., B = 0., t = 0.;
92            int y2;
93
94            for(i = -kr; i <= kr; i++)
95            {
96                double f = kernel[i + kr];
97
98                y2 = y + i;
99                if(y2 < 0) y2 = 0;
100                else if(y2 >= h) y2 = h - 1;
101
102                R += f * buffer[(y2 * w + x) * 4];
103                G += f * buffer[(y2 * w + x) * 4 + 1];
104                B += f * buffer[(y2 * w + x) * 4 + 2];
105                t += f;
106            }
107
108            dstdata[(y * w + x) * 4] = R / t;
109            dstdata[(y * w + x) * 4 + 1] = G / t;
110            dstdata[(y * w + x) * 4 + 2] = B / t;
111        }
112    }
113
114    free(buffer);
115    free(kernel);
116
117    return dst;
118}
119
Note: See TracBrowser for help on using the repository browser.