aboutsummaryrefslogtreecommitdiff
path: root/src/cairo-utils.vala
blob: a30a5807f84d4b7e85a8eb6987004b8f603fe8b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 *
 * Copyright (C) 2013 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Marco Trevisan <marco.trevisan@canonical.com>
 *          Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
 */

namespace CairoUtils
{

public void rounded_rectangle (Cairo.Context c, double x, double y,
                               double width, double height, double radius)
{
    var w = width - radius * 2;
    var h = height - radius * 2;
    var kappa = 0.5522847498 * radius;
    c.move_to (x + radius, y);
    c.rel_line_to (w, 0);
    c.rel_curve_to (kappa, 0, radius, radius - kappa, radius, radius);
    c.rel_line_to (0, h);
    c.rel_curve_to (0, kappa, kappa - radius, radius, -radius, radius);
    c.rel_line_to (-w, 0);
    c.rel_curve_to (-kappa, 0, -radius, kappa - radius, -radius, -radius);
    c.rel_line_to (0, -h);
    c.rel_curve_to (0, -kappa, radius - kappa, -radius, radius, -radius);
}

class GaussianBlur
{
    /* Gaussian Blur, based on Mirco Mueller work on notify-osd */

    public static void surface (Cairo.ImageSurface surface, uint radius, double sigma = 0.0f)
    {
        if (surface.get_format () != Cairo.Format.ARGB32)
        {
            warning ("Impossible to blur a non ARGB32-formatted ImageSurface");
            return;
        }

        surface.flush ();

        double radiusf = Math.fabs (radius) + 1.0f;

        if (sigma == 0.0f)
            sigma = Math.sqrt (-(radiusf * radiusf) / (2.0f * Math.log (1.0f / 255.0f)));

        int w = surface.get_width ();
        int h = surface.get_height ();
        int s = surface.get_stride ();

        // create pixman image for cairo image surface
        unowned uchar[] p = surface.get_data ();
        var src = new Pixman.Image.bits (Pixman.Format.A8R8G8B8, w, h, p, s);

        // attach gaussian kernel to pixman image
        var params = create_gaussian_blur_kernel ((int) radius, sigma);
        src.set_filter (Pixman.Filter.CONVOLUTION, params);

        // render blured image to new pixman image
        Pixman.Image.composite (Pixman.Operation.SRC, src, null, src,
                                0, 0, 0, 0, 0, 0, (uint16) w, (uint16) h);

        surface.mark_dirty ();
    }

    private static Pixman.Fixed[] create_gaussian_blur_kernel (int radius, double sigma)
    {
        double scale2 = 2.0f * sigma * sigma;
        double scale1 = 1.0f / (Math.PI * scale2);
        int size = 2 * radius + 1;
        int n_params = size * size;
        double sum = 0;

        var tmp = new double[n_params];

        // caluclate gaussian kernel in floating point format
        for (int i = 0, x = -radius; x <= radius; ++x)
        {
            for (int y = -radius; y <= radius; ++y, ++i)
            {
                double u = x * x;
                double v = y * y;

                tmp[i] = scale1 * Math.exp (-(u+v)/scale2);

                sum += tmp[i];
            }
        }

        // normalize gaussian kernel and convert to fixed point format
        var params = new Pixman.Fixed[n_params + 2];

        params[0] = Pixman.Fixed.int (size);
        params[1] = Pixman.Fixed.int (size);

        for (int i = 2; i < params.length; ++i)
            params[i] = Pixman.Fixed.double (tmp[i] / sum);

        return params;
    }
}

class ExponentialBlur
{
    /* Exponential Blur, based on the Nux version */

    const int APREC = 16;
    const int ZPREC = 7;

    public static void surface (Cairo.ImageSurface surface, int radius)
    {
        if (radius < 1)
            return;

        // before we mess with the surface execute any pending drawing
        surface.flush ();

        unowned uchar[] pixels = surface.get_data ();
        var width  = surface.get_width ();
        var height = surface.get_height ();
        var format = surface.get_format ();

        switch (format)
        {
            case Cairo.Format.ARGB32:
                blur (pixels, width, height, 4, radius);
                break;

            case Cairo.Format.RGB24:
                blur (pixels, width, height, 3, radius);
                break;

            case Cairo.Format.A8:
                blur (pixels, width, height, 1, radius);
                break;

            default :
                // do nothing
                break;
        }

        // inform cairo we altered the surfaces contents
        surface.mark_dirty ();
    }

    static void blur (uchar[] pixels, int width, int height, int channels, int radius)
    {
        // calculate the alpha such that 90% of
        // the kernel is within the radius.
        // (Kernel extends to infinity)

        int alpha = (int) ((1 << APREC) * (1.0f - Math.expf(-2.3f / (radius + 1.0f))));

        for (int row = 0; row < height; ++row)
          blurrow (pixels, width, height, channels, row, alpha);

        for (int col = 0; col < width; ++col)
          blurcol (pixels, width, height, channels, col, alpha);
    }

    static void blurrow (uchar[] pixels, int width, int height, int channels, int line, int alpha)
    {
        var scanline = &(pixels[line * width * channels]);

        int zR = *scanline << ZPREC;
        int zG = *(scanline + 1) << ZPREC;
        int zB = *(scanline + 2) << ZPREC;
        int zA = *(scanline + 3) << ZPREC;

        for (int index = 0; index < width; ++index)
        {
          blurinner (&scanline[index * channels], alpha, ref zR, ref zG, ref zB, ref zA);
        }

        for (int index = width - 2; index >= 0; --index)
        {
          blurinner (&scanline[index * channels], alpha, ref zR, ref zG, ref zB, ref zA);
        }
    }

    static void blurcol (uchar[] pixels, int width, int height, int channels, int x, int alpha)
    {
        var ptr = &(pixels[x * channels]);

        int zR = *ptr << ZPREC;
        int zG = *(ptr + 1) << ZPREC;
        int zB = *(ptr + 2) << ZPREC;
        int zA = *(ptr + 3) << ZPREC;

        for (int index = width; index < (height - 1) * width; index += width)
        {
            blurinner (&ptr[index * channels], alpha, ref zR, ref zG, ref zB, ref zA);
        }

        for (int index = (height - 2) * width; index >= 0; index -= width)
        {
            blurinner (&ptr[index * channels], alpha, ref zR, ref zG, ref zB, ref zA);
        }
    }

    static void blurinner (uchar *pixel, int alpha, ref int zR, ref int zG, ref int zB, ref int zA)
    {
        int R;
        int G;
        int B;
        uchar A;

        R = *pixel;
        G = *(pixel + 1);
        B = *(pixel + 2);
        A = *(pixel + 3);

        zR += (alpha * ((R << ZPREC) - zR)) >> APREC;
        zG += (alpha * ((G << ZPREC) - zG)) >> APREC;
        zB += (alpha * ((B << ZPREC) - zB)) >> APREC;
        zA += (alpha * ((A << ZPREC) - zA)) >> APREC;

        *pixel = zR >> ZPREC;
        *(pixel + 1) = zG >> ZPREC;
        *(pixel + 2) = zB >> ZPREC;
        *(pixel + 3) = zA >> ZPREC;
    }
}

}