forked from kornelski/pngquant
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblur.cpp
128 lines (113 loc) · 2.56 KB
/
blur.cpp
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
#include "pam.h"
#include "blur.h"
/*
Blurs image horizontally (width 2*size+1) and writes it transposed to dst (called twice gives 2d blur)
*/
static
void transposing_1d_blur(
const double* src,
double* dst,
uint width,
uint height,
const uint size
)
{
const double sizef = size;
const double sizef2 = 1.0 / (sizef*2.0);
for (uint j=0; j<height; ++j) {
const double* row = src + j*width;
double* dstLine = dst + j;
// accumulate sum for pixels outside line
double sum;
sum = row[0] * sizef;
for (uint i=0; i<size; ++i) {
sum += row[i];
}
// blur with left side outside line
for (uint i=0; i<size; ++i) {
sum -= row[0];
sum += row[i+size];
dstLine[i*height] = sum * sizef2;
}
for (uint i=size; i<width-size; ++i) {
sum -= row[i-size];
sum += row[i+size];
dstLine[i*height] = sum * sizef2;
}
// blur with right side outside line
for (uint i=width-size; i<width; ++i) {
sum -= row[i-size];
sum += row[width-1];
dstLine[i*height] = sum * sizef2;
}
}
}
/**
* Picks maximum of neighboring pixels (blur + lighten)
*/
void max3(
const double* src,
double* dst,
uint width,
uint height
)
{
for (uint j=0; j<height; ++j) {
const double* row = src + j*width,
*prevrow = src + (j>1 ? j-1 : 0)*width,
*nextrow = src + min(height-1,j+1)*width;
double prev;
double curr = row[0];
double next = row[0];
for (uint i=0; i<width-1; ++i) {
prev = curr;
curr = next;
next = row[i+1];
*dst++ = max(curr, prev, next, nextrow[i], prevrow[i]);
}
*dst++ = max(curr, next, nextrow[width-1], prevrow[width-1]);
}
}
/**
* Picks minimum of neighboring pixels (blur + darken)
*/
void min3(
const double* src,
double* dst,
uint width,
uint height
)
{
for (uint j=0; j<height; ++j) {
const double* row = src + j*width,
*prevrow = src + (j>1 ? j-1 : 0)*width,
*nextrow = src + min(height-1,j+1)*width;
double prev;
double curr = row[0];
double next = row[0];
for (uint i=0; i<width-1; ++i) {
prev = curr;
curr = next;
next = row[i+1];
*dst++ = min(curr, prev, next, nextrow[i], prevrow[i]);
}
*dst++ = min(curr, next, nextrow[width-1], prevrow[width-1]);
}
}
/*
Filters src image and saves it to dst, overwriting tmp in the process.
Image must be width*height pixels high. Size controls radius of box blur.
*/
void blur(
const double* src,
double* tmp,
double* dst,
uint width,
uint height,
uint size
)
{
if (width<2*size+1 || height<2*size+1) return;
transposing_1d_blur(src, tmp, width, height, size);
transposing_1d_blur(tmp, dst, height, width, size);
}