-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRBFilterPlain.cpp
390 lines (321 loc) · 9.96 KB
/
RBFilterPlain.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "stdafx.h"
#include "RBFilterPlain.h"
#include "stdafx.h"
#include "RBFilterPlain.h"
#include <algorithm>
using namespace std;
#define QX_DEF_CHAR_MAX 255
CRBFilterPlain::CRBFilterPlain()
{
}
CRBFilterPlain::~CRBFilterPlain()
{
releaseMemory();
}
// assumes 3/4 channel images, 1 byte per channel
void CRBFilterPlain::reserveMemory(int max_width, int max_height, int channels)
{
// basic sanity check
_ASSERT(max_width >= 10 && max_width < 10000);
_ASSERT(max_height >= 10 && max_height < 10000);
_ASSERT(channels >= 1 && channels <= 4);
releaseMemory();
m_reserve_width = max_width;
m_reserve_height = max_height;
m_reserve_channels = channels;
int width_height = m_reserve_width * m_reserve_height;
int width_height_channel = width_height * m_reserve_channels;
m_left_pass_color = new float[width_height_channel];
m_left_pass_factor = new float[width_height];
m_right_pass_color = new float[width_height_channel];
m_right_pass_factor = new float[width_height];
m_down_pass_color = new float[width_height_channel];
m_down_pass_factor = new float[width_height];
m_up_pass_color = new float[width_height_channel];
m_up_pass_factor = new float[width_height];
}
void CRBFilterPlain::releaseMemory()
{
m_reserve_width = 0;
m_reserve_height = 0;
m_reserve_channels = 0;
if (m_left_pass_color)
{
delete[] m_left_pass_color;
m_left_pass_color = nullptr;
}
if (m_left_pass_factor)
{
delete[] m_left_pass_factor;
m_left_pass_factor = nullptr;
}
if (m_right_pass_color)
{
delete[] m_right_pass_color;
m_right_pass_color = nullptr;
}
if (m_right_pass_factor)
{
delete[] m_right_pass_factor;
m_right_pass_factor = nullptr;
}
if (m_down_pass_color)
{
delete[] m_down_pass_color;
m_down_pass_color = nullptr;
}
if (m_down_pass_factor)
{
delete[] m_down_pass_factor;
m_down_pass_factor = nullptr;
}
if (m_up_pass_color)
{
delete[] m_up_pass_color;
m_up_pass_color = nullptr;
}
if (m_up_pass_factor)
{
delete[] m_up_pass_factor;
m_up_pass_factor = nullptr;
}
}
int CRBFilterPlain::getDiffFactor(const unsigned char* color1, const unsigned char* color2) const
{
int final_diff;
int component_diff[4];
// find absolute difference between each component
for (int i = 0; i < m_reserve_channels; i++)
{
component_diff[i] = abs(color1[i] - color2[i]);
}
// based on number of components, produce a single difference value in the 0-255 range
switch (m_reserve_channels)
{
case 1:
final_diff = component_diff[0];
break;
case 2:
final_diff = ((component_diff[0] + component_diff[1]) >> 1);
break;
case 3:
final_diff = ((component_diff[0] + component_diff[2]) >> 2) + (component_diff[1] >> 1);
break;
case 4:
final_diff = ((component_diff[0] + component_diff[1] + component_diff[2] + component_diff[3]) >> 2);
break;
default:
final_diff = 0;
}
_ASSERT(final_diff >= 0 && final_diff <= 255);
return final_diff;
}
// memory must be reserved before calling image filter
// this implementation of filter uses plain C++, single threaded
// channel count must be 3 or 4 (alpha not used)
void CRBFilterPlain::filter(unsigned char* img_src, unsigned char* img_dst,
float sigma_spatial, float sigma_range,
int width, int height, int channel)
{
_ASSERT(img_src);
_ASSERT(img_dst);
_ASSERT(m_reserve_channels == channel);
_ASSERT(m_reserve_width >= width);
_ASSERT(m_reserve_height >= height);
// compute a lookup table
float alpha_f = static_cast<float>(exp(-sqrt(2.0) / (sigma_spatial * 255)));
float inv_alpha_f = 1.f - alpha_f;
float range_table_f[QX_DEF_CHAR_MAX + 1];
float inv_sigma_range = 1.0f / (sigma_range * QX_DEF_CHAR_MAX);
{
float ii = 0.f;
for (int i = 0; i <= QX_DEF_CHAR_MAX; i++, ii -= 1.f)
{
range_table_f[i] = alpha_f * exp(ii * inv_sigma_range);
}
}
///////////////
// Left pass
{
const unsigned char* src_color = img_src;
float* left_pass_color = m_left_pass_color;
float* left_pass_factor = m_left_pass_factor;
for (int y = 0; y < height; y++)
{
const unsigned char* src_prev = src_color;
const float* prev_factor = left_pass_factor;
const float* prev_color = left_pass_color;
// process 1st pixel separately since it has no previous
*left_pass_factor++ = 1.f;
for (int c = 0; c < channel; c++)
{
*left_pass_color++ = *src_color++;
}
// handle other pixels
for (int x = 1; x < width; x++)
{
// determine difference in pixel color between current and previous
// calculation is different depending on number of channels
int diff = getDiffFactor(src_color, src_prev);
src_prev = src_color;
float alpha_f = range_table_f[diff];
*left_pass_factor++ = inv_alpha_f + alpha_f * (*prev_factor++);
for (int c = 0; c < channel; c++)
{
*left_pass_color++ = inv_alpha_f * (*src_color++) + alpha_f * (*prev_color++);
}
}
}
}
///////////////
// Right pass
{
// start from end and then go up to begining
int last_index = width * height * channel - 1;
const unsigned char* src_color = img_src + last_index;
float* right_pass_color = m_right_pass_color + last_index;
float* right_pass_factor = m_right_pass_factor + width * height - 1;
for (int y = 0; y < height; y++)
{
const unsigned char* src_prev = src_color;
const float* prev_factor = right_pass_factor;
const float* prev_color = right_pass_color;
// process 1st pixel separately since it has no previous
*right_pass_factor-- = 1.f;
for (int c = 0; c < channel; c++)
{
*right_pass_color-- = *src_color--;
}
// handle other pixels
for (int x = 1; x < width; x++)
{
// determine difference in pixel color between current and previous
// calculation is different depending on number of channels
int diff = getDiffFactor(src_color, src_color - 3);
// src_prev = src_color;
float alpha_f = range_table_f[diff];
*right_pass_factor-- = inv_alpha_f + alpha_f * (*prev_factor--);
for (int c = 0; c < channel; c++)
{
*right_pass_color-- = inv_alpha_f * (*src_color--) + alpha_f * (*prev_color--);
}
}
}
}
// vertical pass will be applied on top on horizontal pass, while using pixel differences from original image
// result color stored in 'm_left_pass_color' and vertical pass will use it as source color
{
float* img_out = m_left_pass_color; // use as temporary buffer
const float* left_pass_color = m_left_pass_color;
const float* left_pass_factor = m_left_pass_factor;
const float* right_pass_color = m_right_pass_color;
const float* right_pass_factor = m_right_pass_factor;
int width_height = width * height;
for (int i = 0; i < width_height; i++)
{
// average color divided by average factor
float factor = 1.f / ((*left_pass_factor++) + (*right_pass_factor++));
for (int c = 0; c < channel; c++)
{
*img_out++ = (factor * ((*left_pass_color++) + (*right_pass_color++)));
}
}
}
///////////////
// Down pass
{
const float* src_color_hor = m_left_pass_color; // result of horizontal pass filter
const unsigned char* src_color = img_src;
float* down_pass_color = m_down_pass_color;
float* down_pass_factor = m_down_pass_factor;
const unsigned char* src_prev = src_color;
const float* prev_color = down_pass_color;
const float* prev_factor = down_pass_factor;
// 1st line done separately because no previous line
for (int x = 0; x < width; x++)
{
*down_pass_factor++ = 1.f;
for (int c = 0; c < channel; c++)
{
*down_pass_color++ = *src_color_hor++;
}
src_color += channel;
}
// handle other lines
for (int y = 1; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// determine difference in pixel color between current and previous
// calculation is different depending on number of channels
int diff = getDiffFactor(src_color, src_prev);
src_prev += channel;
src_color += channel;
float alpha_f = range_table_f[diff];
*down_pass_factor++ = inv_alpha_f + alpha_f * (*prev_factor++);
for (int c = 0; c < channel; c++)
{
*down_pass_color++ = inv_alpha_f * (*src_color_hor++) + alpha_f * (*prev_color++);
}
}
}
}
///////////////
// Up pass
{
// start from end and then go up to begining
int last_index = width * height * channel - 1;
const unsigned char* src_color = img_src + last_index;
const float* src_color_hor = m_left_pass_color + last_index; // result of horizontal pass filter
float* up_pass_color = m_up_pass_color + last_index;
float* up_pass_factor = m_up_pass_factor + (width * height - 1);
// const unsigned char* src_prev = src_color;
const float* prev_color = up_pass_color;
const float* prev_factor = up_pass_factor;
// 1st line done separately because no previous line
for (int x = 0; x < width; x++)
{
*up_pass_factor-- = 1.f;
for (int c = 0; c < channel; c++)
{
*up_pass_color-- = *src_color_hor--;
}
src_color -= channel;
}
// handle other lines
for (int y = 1; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// determine difference in pixel color between current and previous
// calculation is different depending on number of channels
src_color -= channel;
int diff = getDiffFactor(src_color, src_color + width * channel);
float alpha_f = range_table_f[diff];
*up_pass_factor-- = inv_alpha_f + alpha_f * (*prev_factor--);
for (int c = 0; c < channel; c++)
{
*up_pass_color-- = inv_alpha_f * (*src_color_hor--) + alpha_f * (*prev_color--);
}
}
}
}
///////////////
// average result of vertical pass is written to output buffer
{
const float* down_pass_color = m_down_pass_color;
const float* down_pass_factor = m_down_pass_factor;
const float* up_pass_color = m_up_pass_color;
const float* up_pass_factor = m_up_pass_factor;
int width_height = width * height;
for (int i = 0; i < width_height; i++)
{
// average color divided by average factor
float factor = 1.f / ((*up_pass_factor++) + (*down_pass_factor++));
for (int c = 0; c < channel; c++)
{
*img_dst++ = (unsigned char)(factor * ((*up_pass_color++) + (*down_pass_color++)));
}
}
}
}