-
Notifications
You must be signed in to change notification settings - Fork 7
/
density_mask.cpp
173 lines (140 loc) · 3.44 KB
/
density_mask.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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "density_mask.hpp"
class kernel_1d {
public:
float* matrix;
unsigned length;
kernel_1d(unsigned alength): length(alength) {
matrix=new float[alength];
}
~kernel_1d() {
delete[] matrix;
}
void initialize_gauss() {
int middle=length/2;
double sigma=middle/3.0;
double a=1 / sqrt(2*M_PI*sigma*sigma);
double den=2*sigma*sigma;
for (unsigned i=0; i<length; i++) {
double x=(double)i - middle;
double v=a * exp(-(x*x) / den);
matrix[i]=v;
}
}
void print() {
for (unsigned i=0; i<length; i++) {
printf("\t%f\n", (double)matrix[i]);
}
}
};
template<class T>
void swap(T& a, T& b) {
T tmp=a;
a=b;
b=tmp;
}
void apply_horizontal(kernel_1d& kernel, float* src, float* dst, unsigned width, unsigned height) {
int w=(int)width;
int h=(int)height;
int radius=kernel.length/2;
for (int y=0; y<h; y++) {
float* src_row=src + w*y;
float* dst_row=dst + w*y;
for (int x=0; x<radius; x++) {
float accum=0;
for (int i=radius-x; i<(int)kernel.length; i++) {
accum+=src_row[x+i-radius] * kernel.matrix[i];
}
dst_row[x]=accum;
}
for (int x=radius; x<w-radius; x++) {
float accum=0;
for (int i=0; i<(int)kernel.length; i++) {
accum+=src_row[x+i-radius] * kernel.matrix[i];
}
dst_row[x]=accum;
}
for (int x=w-radius; x<w; x++) {
float accum=0;
for (int i=0; i<(int)kernel.length-radius+w-x; i++) {
accum+=src_row[x+i-radius] * kernel.matrix[i];
}
dst_row[x]=accum;
}
}
}
void apply_vertical(kernel_1d& kernel, float* src, float* dst, unsigned width, unsigned height) {
int w=(int)width;
int h=(int)height;
int radius=kernel.length/2;
for (int x=0; x<w; x++) {
float* src_col=src+x;
float* dst_col=dst+x;
for (int y=0; y<radius; y++) {
float accum=0;
for (int i=radius-y; i<(int)kernel.length; i++) {
accum+=src_col[width*(y+i-radius)] * kernel.matrix[i];
}
dst_col[y*width]=accum;
}
for (int y=radius; y<h-radius; y++) {
float accum=0;
for (unsigned i=0; i<kernel.length; i++) {
accum+=src_col[width*(y+i-radius)] * kernel.matrix[i];
}
dst_col[y*width]=accum;
}
for (int y=h-radius; y<h; y++) {
float accum=0;
for (int i=0; i<(int)kernel.length-radius+h-y; i++) {
accum+=src_col[width*(y+i-radius)] * kernel.matrix[i];
}
dst_col[y*width]=accum;
}
}
}
float density_mask::range_max() {
size_t size=m_width*m_height;
float* buffer=m_buffer;
float mx=-INFINITY;
for (size_t i=0; i<size; i++) {
float v=buffer[i];
mx=fmaxf(mx, v);
}
return mx;
}
void density_mask::multiply(float factor) {
size_t size=m_width*m_height;
float* buffer=m_buffer;
for (size_t i=0; i<size; i++) {
buffer[i]*=factor;
}
}
void density_mask::gaussian_blend(int radius, bool correct_intensity) {
float orig_mx;
if (correct_intensity) {
orig_mx=range_max();
} else {
orig_mx=0;
}
int length=radius * 2 + 1;
kernel_1d kernel(length);
kernel.initialize_gauss();
//printf("Gaussian kernel:\n");
//kernel.print();
size_t size=m_width*m_height;
float* dst=new float[size];
memset(dst, 0, size*sizeof(float));
apply_horizontal(kernel, m_buffer, dst, m_width, m_height);
apply_vertical(kernel, dst, m_buffer, m_width, m_height);
delete[] dst;
if (correct_intensity) {
float res_mx=range_max();
// Gaussian blur reduces intensity. Adjust by scaling the result
float factor=orig_mx / res_mx;
//printf("Intensity factor=%f\n", factor);
multiply(factor);
}
}