-
Notifications
You must be signed in to change notification settings - Fork 4
/
blur.c
61 lines (49 loc) · 1.39 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
#include "blur.h"
#include "util.h"
static int bRadius;
static int bRadiusSq;
static int *bDirRow;
static int *bDirCol;
void blur_init(int radius)
{
int index = 0;
if(radius < 0)
return;
else if(radius % 2 == 0)
radius++;
bRadius = radius;
bRadiusSq = radius * radius;
bDirRow = (int*)malloc((bRadiusSq + 1) * sizeof(int));
bDirCol = (int*)malloc((bRadiusSq + 1) * sizeof(int));
for(int i = -radius / 2; i <= radius / 2; ++i)
for(int j = -radius / 2; j <= radius / 2; ++j) {
bDirRow[index] = i;
bDirCol[index] = j;
++index;
}
}
void blur_free()
{
free(bDirRow);
free(bDirCol);
}
void blur_apply(ctve_frame_t *frame)
{
if(!frame || !bDirCol || !bDirCol)
return;
for(int i = 0; i < frame->height; ++i) {
for(int j = 0; j < 3 * frame->width; j += 3) {
float r = 0, g = 0, b = 0;
for(int dir = 0; dir < bRadiusSq; ++dir) {
int newY = MAX(0, MIN(i + bDirRow[dir], frame->height - 1));
int newX = MAX(0, MIN(j + 3 * bDirCol[dir], 3 * frame->width - 1));
r += (float)frame->data[newY * 3 * frame->width + newX] / (float)bRadiusSq;
g += (float)frame->data[newY * 3 * frame->width + newX + 1] / (float)bRadiusSq;
b += (float)frame->data[newY * 3 * frame->width + newX + 2] / (float)bRadiusSq;
}
frame->data[i * 3 * frame->width + j] = r;
frame->data[i * 3 * frame->width + j + 1] = g;
frame->data[i * 3 * frame->width + j + 2] = b;
}
}
}