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

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