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
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
/*
* Copyright (c) 2010, Guoshen Yu <yu@cmap.polytechnique.fr>,
* Guillermo Sapiro <guille@umn.edu>
* Copyright (C) 2011, Michael Zucchi <notzed@gmail.com>
* All rights reserved.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

//#pragma OPENCL EXTENSION cl_amd_printf : enable
#define d(x)

/* This code implements "DCT image denoising: a simple and effective image
* denoising algorithm".
*
* http://www.ipol.im/pub/algo/ys_dct_denoising
*/

#define PATCHSIZE 8

/*
8x8 DCT basis
*/
constant float DCTbasis[] = {
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
0.35355339059327373085750423342688009142875671386719f,
//
0.49039264020161521528962111915461719036102294921875f,
0.41573480615127261783570133957255166023969650268555f,
0.27778511650980114433551193542371038347482681274414f,
0.09754516100806412404189416065491968765854835510254f,
-0.09754516100806412404189416065491968765854835510254f,
-0.27778511650980114433551193542371038347482681274414f,
-0.41573480615127261783570133957255166023969650268555f,
-0.49039264020161521528962111915461719036102294921875f,
//
0.46193976625564336924156805253005586564540863037109f,
0.19134171618254489088961634024599334225058555603027f,
-0.19134171618254489088961634024599334225058555603027f,
-0.46193976625564336924156805253005586564540863037109f,
-0.46193976625564336924156805253005586564540863037109f,
-0.19134171618254489088961634024599334225058555603027f,
0.19134171618254489088961634024599334225058555603027f,
0.46193976625564336924156805253005586564540863037109f,
//
0.41573480615127261783570133957255166023969650268555f,
-0.09754516100806417955304539191274670884013175964355f,
-0.49039264020161521528962111915461719036102294921875f,
-0.27778511650980108882436070416588336229324340820312f,
0.27778511650980108882436070416588336229324340820312f,
0.49039264020161521528962111915461719036102294921875f,
0.09754516100806417955304539191274670884013175964355f,
-0.41573480615127261783570133957255166023969650268555f,
//
0.35355339059327378636865546468470711261034011840820f,
-0.35355339059327378636865546468470711261034011840820f,
-0.35355339059327378636865546468470711261034011840820f,
0.35355339059327378636865546468470711261034011840820f,
0.35355339059327378636865546468470711261034011840820f,
-0.35355339059327378636865546468470711261034011840820f,
-0.35355339059327378636865546468470711261034011840820f,
0.35355339059327378636865546468470711261034011840820f,
//
0.27778511650980114433551193542371038347482681274414f,
-0.49039264020161532631192358167027123272418975830078f,
0.09754516100806412404189416065491968765854835510254f,
0.41573480615127261783570133957255166023969650268555f,
-0.41573480615127261783570133957255166023969650268555f,
-0.09754516100806412404189416065491968765854835510254f,
0.49039264020161532631192358167027123272418975830078f,
-0.27778511650980114433551193542371038347482681274414f,
//
0.19134171618254491864519195587490685284137725830078f,
-0.46193976625564336924156805253005586564540863037109f,
0.46193976625564336924156805253005586564540863037109f,
-0.19134171618254491864519195587490685284137725830078f,
-0.19134171618254491864519195587490685284137725830078f,
0.46193976625564336924156805253005586564540863037109f,
-0.46193976625564336924156805253005586564540863037109f,
0.19134171618254491864519195587490685284137725830078f,
//
0.09754516100806416567525758409828995354473590850830f,
-0.27778511650980108882436070416588336229324340820312f,
0.41573480615127267334685257083037868142127990722656f,
-0.49039264020161521528962111915461719036102294921875f,
0.49039264020161521528962111915461719036102294921875f,
-0.41573480615127267334685257083037868142127990722656f,
0.27778511650980108882436070416588336229324340820312f,
-0.09754516100806416567525758409828995354473590850830f
};

#if 0
float threshold(float v, float thr) {
float m = max(fabs(v) - thr, 0);
return (m / (m+thr)) * v;
}
#else
float threshold(float v, float thr) {
return fabs(v) < thr ? 0 : v;
}
#endif

/**
* Perform 8x8 forward dct on data stored in local array
*/
void dct_forward_threshold(local float *data, int soff, int doff, int j, int stride, float thr) {
// rows
float v = 0;
for (int i = 0; i < PATCHSIZE; i++) {
v += data[i + soff] * DCTbasis[j*PATCHSIZE+i];
}

barrier(CLK_LOCAL_MEM_FENCE);
data[j + soff] = v;
barrier(CLK_LOCAL_MEM_FENCE);

// columns
v = 0;
for (int i = 0; i < PATCHSIZE; i++) {
v += data[i * stride + doff] * DCTbasis[j*PATCHSIZE+i];
}

v = threshold(v, thr);

barrier(CLK_LOCAL_MEM_FENCE);
data[j * stride + doff] = v;
barrier(CLK_LOCAL_MEM_FENCE);
}

void dct_inverse(local float *data, int soff, int doff, int j, int stride) {
// rows
float v = 0;

for (int i = 0; i < PATCHSIZE; i++) {
v += data[i + soff] * DCTbasis[i*PATCHSIZE+j];
}

barrier(CLK_LOCAL_MEM_FENCE);
data[j + soff] = v;
barrier(CLK_LOCAL_MEM_FENCE);

// cols
v = 0;
for (int i = 0; i < PATCHSIZE; i++) {
v += data[i * stride + doff] * DCTbasis[i*PATCHSIZE+j];
}

barrier(CLK_LOCAL_MEM_FENCE);
data[j * stride + doff] = v;
barrier(CLK_LOCAL_MEM_FENCE);
}

constant float DCTbasis3x3[] = {
0.5773502588272094726562500000000000000000f,
0.5773502588272094726562500000000000000000f,
0.5773502588272094726562500000000000000000f,
//
0.7071067690849304199218750000000000000000f,
0.0000000000000000000000000000000000000000f,
-0.7071067690849304199218750000000000000000f,
//
0.4082483053207397460937500000000000000000f,
-0.8164966106414794921875000000000000000000f,
0.4082483053207397460937500000000000000000f
};

/**
* Performs 3-way DCT of 3 planes of data.
*
* Data is stored in planes slice size
*
* soff points to the index of the item in question.
* slice is the offset to the next plane.
*/
void dct3_forward(local float *data, int soff, int slice) {
float r0, r1, r2;

r0 = data[soff] * DCTbasis3x3[0*3+0]
+ data[soff+slice] * DCTbasis3x3[0*3+1]
+ data[soff+slice*2] * DCTbasis3x3[0*3+2];

r1 = data[soff] * DCTbasis3x3[1*3+0]
+ data[soff+slice] * DCTbasis3x3[1*3+1]
+ data[soff+slice*2] * DCTbasis3x3[1*3+2];

r2 = data[soff] * DCTbasis3x3[2*3+0]
+ data[soff+slice] * DCTbasis3x3[2*3+1]
+ data[soff+slice*2] * DCTbasis3x3[2*3+2];

barrier(CLK_LOCAL_MEM_FENCE);
data[soff] = r0;
data[soff+slice] = r1;
data[soff+slice*2] = r2;
barrier(CLK_LOCAL_MEM_FENCE);
}

void dct3_inverse(local float *data, int soff, int slice) {
float r0, r1, r2;

r0 = data[soff] * DCTbasis3x3[0*3+0]
+ data[soff+slice] * DCTbasis3x3[1*3+0]
+ data[soff+slice*2] * DCTbasis3x3[2*3+0];

r1 = data[soff] * DCTbasis3x3[0*3+1]
+ data[soff+slice] * DCTbasis3x3[1*3+1]
+ data[soff+slice*2] * DCTbasis3x3[2*3+1];

r2 = data[soff] * DCTbasis3x3[0*3+2]
+ data[soff+slice] * DCTbasis3x3[1*3+2]
+ data[soff+slice*2] * DCTbasis3x3[2*3+2];

barrier(CLK_LOCAL_MEM_FENCE);
data[soff] = r0;
data[soff+slice] = r1;
data[soff+slice*2] = r2;
barrier(CLK_LOCAL_MEM_FENCE);
}


#define DFSTRIDE (9)

/**
* Process a single 8x8 tile using DCT denoising algorithm.
*
* Forward DCT in local, threshold, then save.
*/
kernel void
__attribute__((reqd_work_group_size(8, 8, 1)))
dct_denoise_8x8_r(read_only image2d_t src, global float *acc, float thr, int dx, int dy, int astride, int set) {
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
local float ldata[8*DFSTRIDE];
int lx = get_local_id(0);
int ly = get_local_id(1);
int x = get_global_id(0) + dx;
int y = get_global_id(1) + dy;
float v = read_imagef(src, smp, (int2) { x, y }).s0;

ldata[lx + ly*DFSTRIDE] = v;
barrier(CLK_LOCAL_MEM_FENCE);

dct_forward_threshold(ldata, ly*DFSTRIDE, ly, lx, DFSTRIDE, thr);
dct_inverse(ldata, ly*DFSTRIDE, ly, lx, DFSTRIDE);

// accumulate results: always in-range by design
v = ldata[lx + ly*DFSTRIDE];
if (set)
acc[x + y * astride] = v;
else
acc[x + y * astride] += v;
}

/**
* Fix the accumulation results.
*/
kernel void
__attribute__((reqd_work_group_size(8, 8, 1)))
dct_denoise_normalise_r(const global float *acc, write_only image2d_t dst, int astride) {
int lx = get_local_id(0);
int ly = get_local_id(1);
int x = get_global_id(0);
int y = get_global_id(1);
float v;

if ((x >= get_image_width(dst)) | (y >= get_image_height(dst)))
return;

v = acc[x + y * astride];
#if 0
v *= 1.0f / 64.0f;
#else
float xc = (x < 8) ? x + 1 : x >= get_image_width(dst) - 8 ? get_image_width(dst) - x : 8;
float yc = (y < 8) ? y + 1 : y >= get_image_height(dst) - 8 ? get_image_height(dst) - y : 8;

v *= 1.0f / (xc * yc);
#endif

write_imagef(dst, (int2) { x, y }, (float4) v);
}

/**
* Process a single 8x8 tile using DCT denoising algorithm.
*
* Forward DCT in local, threshold, then save.
*
* This is the 3 channel RGB version.
*/
kernel void
__attribute__((reqd_work_group_size(8, 8, 1)))
dct_denoise_8x8_rgb(read_only image2d_t src, global float4 *acc, float thr, int dx, int dy, int astride, int set) {
const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
local float ldata[8*3*DFSTRIDE];
int lx = get_local_id(0);
int ly = get_local_id(1);
int lid = lx + ly * DFSTRIDE;
const int slice = DFSTRIDE * 8;
int x = get_global_id(0) + dx;
int y = get_global_id(1) + dy;
float4 v = read_imagef(src, smp, (int2) { x, y });

ldata[lid] = v.s0;
ldata[lid+slice] = v.s1;
ldata[lid+slice*2] = v.s2;
barrier(CLK_LOCAL_MEM_FENCE);

dct3_forward(ldata, lid, slice);

for (int i=0;i<3;i++) {
dct_forward_threshold(ldata, ly*DFSTRIDE + i*slice, ly+i*slice, lx, DFSTRIDE, thr);
dct_inverse(ldata, ly*DFSTRIDE + i*slice, ly+i*slice, lx, DFSTRIDE);
}

dct3_inverse(ldata, lid, slice);

// accumulate results: always in-range by design
v.s0 = ldata[lid];
v.s1 = ldata[lid+slice];
v.s2 = ldata[lid+slice*2];
if (set)
acc[x + y * astride] = v;
else
acc[x + y * astride] += v;
}

/**
* Fix the accumulation results.
*
* Colour data.
*/
kernel void
__attribute__((reqd_work_group_size(8, 8, 1)))
dct_denoise_normalise_rgb(const global float4 *acc, write_only image2d_t dst, int astride) {
int lx = get_local_id(0);
int ly = get_local_id(1);
int x = get_global_id(0);
int y = get_global_id(1);
float4 v;

if ((x >= get_image_width(dst)) | (y >= get_image_height(dst)))
return;

v = acc[x + y * astride];
#if 0
v *= 1.0f / 64.0f;
#else
float xc = (x < 8) ? x + 1 : x >= get_image_width(dst) - 8 ? get_image_width(dst) - x : 8;
float yc = (y < 8) ? y + 1 : y >= get_image_height(dst) - 8 ? get_image_height(dst) - y : 8;

v *= 1.0f / (xc * yc);
#endif

write_imagef(dst, (int2) { x, y }, v);
}

Change log

r39 by notzed on Oct 27, 2011   Diff
Implemented colour version of DCT
denoising.
Go to: 
Project members, sign in to write a code review

Older revisions

r38 by notzed on Oct 24, 2011   Diff
Added DCT de-noising implementation.
All revisions of this file

File info

Size: 11568 bytes, 369 lines
Powered by Google Project Hosting