forked from Lixxia/i3lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blur.c
210 lines (187 loc) · 6.37 KB
/
blur.c
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
/*
* Copyright © 2008 Kristian Høgsberg
* Copyright © 2009 Chris Wilson
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <cairo.h>
#include <stdlib.h>
/* Performs a simple 2D Gaussian blur of radius @radius on surface @surface. */
uint32_t* boxesForGauss(double sigma, double n) // standard deviation, number of boxes
{
uint32_t wl = sqrt(12*sigma*sigma/n+1.); // Ideal averaging filter width // floor
if(wl%2==0)
wl--;
uint32_t wu = wl+2;
uint32_t m = (12*sigma*sigma - n*wl*wl - 4*n*wl - 3*n)/(-4*wl - 4); // round
uint32_t* sizes = (uint32_t*) malloc(n*sizeof(uint32_t));
for(uint32_t i=0; i<n; i++)
sizes[i] = (i<m?wl:wu);
return sizes;
}
/*
function boxBlurH_4 (scl, tcl, w, h, r) {
var iarr = 1 / (r+r+1);
for(var i=0; i<h; i++) {
var ti = i*w, li = ti, ri = ti+r;
var fv = scl[ti], lv = scl[ti+w-1], val = (r+1)*fv;
for(var j=0; j<r; j++) val += scl[ti+j];
for(var j=0 ; j<=r ; j++) { val += scl[ri++] - fv ; tcl[ti++] = Math.round(val*iarr); }
for(var j=r+1; j<w-r; j++) { val += scl[ri++] - scl[li++]; tcl[ti++] = Math.round(val*iarr); }
for(var j=w-r; j<w ; j++) { val += lv - scl[li++]; tcl[ti++] = Math.round(val*iarr); }
}
}
*/
void boxBlurH_4 (uint8_t *scl, uint8_t *tcl, int w, int h, int r, int off) {
double iarr = 1. / (double)(r+r+1);
for(int i=0; i<h; i++) {
int ti = i*w,
li = ti,
ri = ti+r;
double fv = scl[ti*4+off],
lv = scl[(ti+w-1)*4+off],
val = (r+1)*(fv);
for(int j=0; j<r; j++) {
val += scl[(ti+j)*4+off];
}
for(int j=0 ; j<=r ; j++) {
val += scl[ri++*4+off] - fv;
tcl[ti++*4+off] = val * iarr;
}
for(int j=r+1; j<w-r; j++) {
val += scl[ri++*4+off] - scl[li++*4+off];
tcl[ti++*4+off] = val * iarr;
}
for(int j=w-r; j<w; j++) {
val += lv - scl[li++*4+off];
tcl[ti++*4+off] = val * iarr;
}
}
}
/*
function boxBlurT_4 (scl, tcl, w, h, r) {
var iarr = 1 / (r+r+1);
for(var i=0; i<w; i++) {
var ti = i, li = ti, ri = ti+r*w;
var fv = scl[ti], lv = scl[ti+w*(h-1)], val = (r+1)*fv;
for(var j=0; j<r; j++) val += scl[ti+j*w];
for(var j=0 ; j<=r ; j++) { val += scl[ri] - fv ; tcl[ti] = Math.round(val*iarr); ri+=w; ti+=w; }
for(var j=r+1; j<h-r; j++) { val += scl[ri] - scl[li]; tcl[ti] = Math.round(val*iarr); li+=w; ri+=w; ti+=w; }
for(var j=h-r; j<h ; j++) { val += lv - scl[li]; tcl[ti] = Math.round(val*iarr); li+=w; ti+=w; }
}
}
*/
void boxBlurT_4 (uint8_t *scl, uint8_t *tcl, int w, int h, int r, int off) {
double iarr = 1. / (double)(r+r+1);
for(int i=0; i<w; i++) {
int ti = i,
li = ti,
ri = ti+r*w;
double fv = scl[ti*4+off],
lv = scl[(ti+w*(h-1))*4+off],
val = (r+1)*(fv);
for(int j=0; j<r; j++) {
val += scl[(ti+j*w)*4+off];
}
for(int j=0 ; j<=r ; j++) {
val += scl[ri*4+off] - fv;
tcl[ti*4+off] = val * iarr;
ri += w; ti += w;
}
for(int j=r+1; j<h-r; j++) {
val += scl[ri*4+off] - scl[li*4+off];
tcl[ti*4+off] = val * iarr;
ri += w; li += w; ti += w;
}
for(int j=h-r; j<h; j++) {
val += lv - scl[li*4+off];
tcl[ti*4+off] = val * iarr;
li += w; ti += w;
}
}
}
void boxBlur_4 (uint8_t *scl, uint8_t *tcl, int w, int h, int r, char off) {
// memcpy(tcl, scl, h*w*4);
// for(int i=0; i<h*w*4; i++)
// tcl[i] = scl[i];
boxBlurH_4(scl, tcl, w, h, r, off);
boxBlurT_4(tcl, scl, w, h, r, off);
// memcpy(scl, tcl, h*w*4);
}
void gaussBlur_4 (uint8_t *scl, uint8_t *tcl, int w, int h, int r) {
uint32_t* bxs = boxesForGauss(r, 4);
for (char off = 0; off < 3; off++) {
boxBlur_4 (scl, tcl, w, h, (bxs[0]-1)/3, off);
boxBlur_4 (scl, tcl, w, h, (bxs[1]-1)/3, off);
boxBlur_4 (scl, tcl, w, h, (bxs[2]-1)/3, off);
boxBlur_4 (scl, tcl, w, h, (bxs[3]-1)/3, off);
// boxBlur_4 (scl, tcl, w, h, (bxs[4]-1)/2, off);
}
free(bxs);
}
void blur_image_surface (cairo_surface_t *surface, int radius)
{
cairo_surface_t *tmp;
int width, height;
int src_stride, dst_stride;
// int x, y, z, w;
uint8_t *src, *dst;
// uint32_t *s, *d, a, p;
// int i, j, k;
// const int size = radius;
// const int half = size / 2;
if (cairo_surface_status (surface))
return;
width = cairo_image_surface_get_width (surface);
height = cairo_image_surface_get_height (surface);
switch (cairo_image_surface_get_format (surface)) {
case CAIRO_FORMAT_A1:
default:
/* Don't even think about it! */
return;
case CAIRO_FORMAT_A8:
/* Handle a8 surfaces by effectively unrolling the loops by a
* factor of 4 - this is safe since we know that stride has to be a
* multiple of uint32_t. */
width /= 4;
break;
case CAIRO_FORMAT_RGB24:
case CAIRO_FORMAT_ARGB32:
break;
}
tmp = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
if (cairo_surface_status (tmp))
return;
src = cairo_image_surface_get_data (surface);
src_stride = cairo_image_surface_get_stride (surface);
dst = cairo_image_surface_get_data (tmp);
dst_stride = cairo_image_surface_get_stride (tmp);
if (src_stride != dst_stride) {
cairo_surface_destroy (tmp);
cairo_surface_flush (surface);
cairo_surface_mark_dirty (surface);
}
gaussBlur_4(src, dst, width, height, radius);
cairo_surface_destroy (tmp);
cairo_surface_flush (surface);
cairo_surface_mark_dirty (surface);
}