forked from rodrigomas/DCTLs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Median_5x5.dctl
40 lines (31 loc) · 1.08 KB
/
Median_5x5.dctl
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
// Median 5x5
__DEVICE__ float median(float p_Table[], int m) {
float temp;
int i, j;
for(i = 0; i < m - 1; i++) {
for(j = i + 1; j < m; j++) {
if(p_Table[j] < p_Table[i]) {
temp = p_Table[i];
p_Table[i] = p_Table[j];
p_Table[j] = temp; }}}
return p_Table[(m - 1) / 2]; }
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, __TEXTURE__ p_TexR, __TEXTURE__ p_TexG, __TEXTURE__ p_TexB)
{
// Enter length for sample area e.g. 3 for 3x3, 5 for 5x5, 7 for 7x7, etc.
int length = 5;
int area = length * length;
float colRGB[3];
float TableR[area], TableG[area], TableB[area];
for (int i = 0; i < area; ++i) {
int offset = (length - 1) / 2;
int xx = (i >= length ? _fmod(i, length/1.0f) : i) - offset;
int yy = _floor(i / (length/1.0f)) - offset;
TableR[i] = _tex2D(p_TexR, p_X + xx, p_Y + yy);
TableG[i] = _tex2D(p_TexG, p_X + xx, p_Y + yy);
TableB[i] = _tex2D(p_TexB, p_X + xx, p_Y + yy);
}
for (int c = 0; c < 3; ++c) {
colRGB[c] = c == 0 ? median(TableR, area) : c == 1 ? median(TableG, area) : median(TableB, area);
}
return make_float3(colRGB[0], colRGB[1], colRGB[2]);
}