My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
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
/*
Copyright (c) 2011 Michael Zucchi

This file is part of socles, an OpenCL image processing library.

socles is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

socles is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with socles. If not, see <http://www.gnu.org/licenses/>.
*/

/*
Median filters
*/

// 84uS
#define cas(a, b) \
do { \
float x = a; \
int c = a > b; \
a = c ? b : a; \
b = c ? x : b; \
} while (0)

#define cas2(a, b) \
do { \
float4 x = a; \
int4 c = a> b; \
a.s01 = select(b, a, c).s01; \
b.s01 = select(x, b, c).s01; \
} while (0)

// 175
#define cas3(a, b) \
do { \
float4 x = a; \
int4 c = a> b; \
a.s012 = select(b, a, c).s012; \
b.s012 = select(x, b, c).s012; \
} while (0)

// 238
#define cas4(a, b) \
do { \
float4 x = a; \
int4 c = a > b; \
a = select(b, a, c); \
b = select(x, b, c); \
} while (0)


// 3x3 median filter
// uses a sorting network to sort entirely in registers with no branches
// sorting network from knuth volume 3, S5.3.4
kernel void
median3x3_r(image2d_t src, write_only image2d_t dst) {
int gx = get_global_id(0), gy = get_global_id(1);
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

if ((gx >= get_image_width(dst)) | (gy >= get_image_height(dst)))
return;

float s0 = read_imagef(src, smp, (int2) { gx-1, gy-1 }).s0;
float s1 = read_imagef(src, smp, (int2) { gx, gy-1 }).s0;
float s2 = read_imagef(src, smp, (int2) { gx+1, gy-1 }).s0;
float s3 = read_imagef(src, smp, (int2) { gx-1, gy }).s0;
float s4 = read_imagef(src, smp, (int2) { gx, gy }).s0;
float s5 = read_imagef(src, smp, (int2) { gx+1, gy }).s0;
float s6 = read_imagef(src, smp, (int2) { gx-1, gy+1 }).s0;
float s7 = read_imagef(src, smp, (int2) { gx, gy+1 }).s0;
float s8 = read_imagef(src, smp, (int2) { gx+1, gy+1 }).s0;

// stage0
cas(s1, s2);
cas(s4, s5);
cas(s7, s8);

// 1
cas(s0, s1);
cas(s3, s4);
cas(s6, s7);

// 2
cas(s1, s2);
cas(s4, s5);
cas(s7, s8);

// 3/4
cas(s3, s6);
cas(s4, s7);
cas(s5, s8);
cas(s0, s3);

cas(s1, s4);
cas(s2, s5);
cas(s3, s6);

// cas(s5, s8); // not needed for median
cas(s4, s7);
cas(s1, s3);

cas(s2, s6);
// cas(s5, s7); // not needed for median
cas(s2, s3);
cas(s4, s6);

cas(s3, s4);
// cas(s5, s6); // not needed for median

write_imagef(dst, (int2) (gx, gy), (float4)s4);
}

// rgb version
kernel void
median3x3_rgb(image2d_t src, write_only image2d_t dst) {
int gx = get_global_id(0), gy = get_global_id(1);
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

if ((gx >= get_image_width(dst)) | (gy >= get_image_height(dst)))
return;

float4 s0 = read_imagef(src, smp, (int2) { gx-1, gy-1 });
float4 s1 = read_imagef(src, smp, (int2) { gx, gy-1 });
float4 s2 = read_imagef(src, smp, (int2) { gx+1, gy-1 });
float4 s3 = read_imagef(src, smp, (int2) { gx-1, gy });
float4 s4 = read_imagef(src, smp, (int2) { gx, gy });
float4 s5 = read_imagef(src, smp, (int2) { gx+1, gy });
float4 s6 = read_imagef(src, smp, (int2) { gx-1, gy+1 });
float4 s7 = read_imagef(src, smp, (int2) { gx, gy+1 });
float4 s8 = read_imagef(src, smp, (int2) { gx+1, gy+1 });

// stage0
cas3(s1, s2);
cas3(s4, s5);
cas3(s7, s8);

// 1
cas3(s0, s1);
cas3(s3, s4);
cas3(s6, s7);

// 2
cas3(s1, s2);
cas3(s4, s5);
cas3(s7, s8);

// 3/4
cas3(s3, s6);
cas3(s4, s7);
cas3(s5, s8);
cas3(s0, s3);

cas3(s1, s4);
cas3(s2, s5);
cas3(s3, s6);

// cas3(s5, s8); // not needed for median
cas3(s4, s7);
cas3(s1, s3);

cas3(s2, s6);
// cas3(s5, s7); // not needed for median
cas3(s2, s3);
cas3(s4, s6);

cas3(s3, s4);
// cas3(s5, s6); // not needed for median

write_imagef(dst, (int2) (gx, gy), s4);
}

Change log

r36 by notzed on Oct 22, 2011   Diff
Range check targets.
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by notzed on May 17, 2011   Diff
Initial import.
All revisions of this file

File info

Size: 4254 bytes, 178 lines
Powered by Google Project Hosting