summaryrefslogtreecommitdiffstats
path: root/src/kernels/nlm.cl
blob: df96fbd314c4fbec3cac2a22d9d27f6952a5a499 (plain)
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
/*
 * Copyright (C) 2011-2018 Karlsruhe Institute of Technology
 *
 * This file is part of Ufo.
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

float
dist (read_only image2d_t input,
      sampler_t sampler,
      float2 p,
      float2 q,
      int radius,
      int width,
      int height,
      float variance)
{
    float dist = 0.0f, tmp;
    float wsize = (2.0f * radius + 1.0f);
    wsize *= wsize;

    for (int i = -radius; i < radius + 1; i++) {
        for (int j = -radius; j < radius + 1; j++) {
            tmp = read_imagef (input, sampler, (float2) ((p.x + i) / width, (p.y + j) / height)).x -
                    read_imagef (input, sampler, (float2) ((q.x + i) / width, (q.y + j) / height)).x;
            dist += fmax (0.0f, tmp * tmp - 2 * variance);
        }
    }

    return dist / wsize;
}

kernel void
nlm_noise_reduction (read_only image2d_t input,
                     global float *output,
                     sampler_t sampler,
                     const int search_radius,
                     const int patch_radius,
                     const float h_2,
                     const float variance)
{
    const int x = get_global_id (0);
    const int y = get_global_id (1);
    const int width = get_global_size (0);
    const int height = get_global_size (1);
    float d, weight;

    float total_weight = 0.0f;
    float pixel_value = 0.0f;

    for (int i = x - search_radius; i < x + search_radius + 1; i++) {
        for (int j = y - search_radius; j < y + search_radius + 1; j++) {
            d = dist (input, sampler, (float2) (x + 0.5f, y + 0.5f), (float2) (i + 0.5f, j + 0.5f),
                      patch_radius, width, height, variance);
            weight = exp (- h_2 * d);
            pixel_value += weight * read_imagef (input, sampler, (float2) ((i + 0.5f) / width, (j + 0.5f) / height)).x;
            total_weight += weight;
        }
    }

    output[y * width + x] = pixel_value / total_weight;
}