summaryrefslogtreecommitdiff
path: root/opencl/edge-laplace.cl
blob: d03883679b399e20ddbc7db9e8e8e9baa0ebcb52 (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
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
#define LAPLACE_RADIUS 2
#define EPSILON        1e-5f

void minmax(float x1, float x2, float x3,
            float x4, float x5,
            float *min_result,
            float *max_result)
{
    float min1, min2, max1, max2;

    if (x1 > x2)
    {
        max1 = x1;
        min1 = x2;
    }
    else
    {
        max1 = x2;
        min1 = x1;
    }

    if (x3 > x4)
    {
        max2 = x3;
        min2 = x4;
    }
    else
    {
        max2 = x4;
        min2 = x3;
    }

    if (min1 < min2)
        *min_result = fmin(min1, x5);
    else
        *min_result = fmin(min2, x5);
    if (max1 > max2)
        *max_result = fmax(max1, x5);
    else
        *max_result = fmax(max2, x5);
}

kernel void pre_edgelaplace (global float4 *in,
                             global float4 *out)
{
    int gidx = get_global_id(0);
    int gidy = get_global_id(1);

    int src_width  = get_global_size(0) + LAPLACE_RADIUS * 2;
    int src_height = get_global_size(1);

    int i = gidx + LAPLACE_RADIUS, j = gidy + LAPLACE_RADIUS;
    int gid1d = i + j * src_width;

    float pix_fl[4] = {
        in[gid1d - 1 - src_width].x, in[gid1d - 1 - src_width].y,
        in[gid1d - 1 - src_width].z, in[gid1d - 1 - src_width].w
    };
    float pix_fm[4] = {
        in[gid1d     - src_width].x, in[gid1d     - src_width].y,
        in[gid1d     - src_width].z, in[gid1d     - src_width].w
    };
    float pix_fr[4] = {
        in[gid1d + 1 - src_width].x, in[gid1d + 1 - src_width].y,
        in[gid1d + 1 - src_width].z, in[gid1d + 1 - src_width].w
    };
    float pix_ml[4] = {
        in[gid1d - 1            ].x, in[gid1d - 1            ].y,
        in[gid1d - 1            ].z, in[gid1d - 1            ].w
    };
    float pix_mm[4] = {
        in[gid1d                ].x, in[gid1d                ].y,
        in[gid1d                ].z, in[gid1d                ].w
    };
    float pix_mr[4] = {
        in[gid1d + 1            ].x, in[gid1d + 1            ].y,
        in[gid1d + 1            ].z, in[gid1d + 1            ].w
    };
    float pix_bl[4] = {
        in[gid1d - 1 + src_width].x, in[gid1d - 1 + src_width].y,
        in[gid1d - 1 + src_width].z, in[gid1d - 1 + src_width].w
    };
    float pix_bm[4] = {
        in[gid1d     + src_width].x, in[gid1d     + src_width].y,
        in[gid1d     + src_width].z, in[gid1d     + src_width].w
    };
    float pix_br[4] = {
        in[gid1d + 1 + src_width].x, in[gid1d + 1 + src_width].y,
        in[gid1d + 1 + src_width].z, in[gid1d + 1 + src_width].w
    };

    int c;
    float minval, maxval;
    float gradient[4];

    for (c = 0;c < 3; ++c)
    {
        minmax(pix_fm[c], pix_bm[c], pix_ml[c], pix_mr[c],
            pix_mm[c], &minval, &maxval);
        gradient[c] = 0.5f *
            fmax((maxval - pix_mm[c]),(pix_mm[c] - minval));
        gradient[c] =
            (pix_fl[c] + pix_fm[c] + pix_fr[c] +
             pix_bm[c] - 8.0f * pix_mm[c] + pix_br[c] +
             pix_ml[c] + pix_mr[c] + pix_bl[c]) <
             EPSILON ? -1.0f * gradient[c] : gradient[c];
    }
    gradient[3] = pix_mm[3];

    out[gid1d] = (float4)
        (gradient[0], gradient[1], gradient[2], gradient[3]);
}

kernel void knl_edgelaplace (global float4 *in,
                             global float4 *out)
{
    int gidx = get_global_id(0);
    int gidy = get_global_id(1);

    int src_width  = get_global_size(0) + LAPLACE_RADIUS * 2;
    int src_height = get_global_size(1);

    int i = gidx + LAPLACE_RADIUS, j = gidy + LAPLACE_RADIUS;
    int gid1d = i + j * src_width;

    float pix_fl[4] = {
        in[gid1d - 1 - src_width].x, in[gid1d - 1 - src_width].y,
        in[gid1d - 1 - src_width].z, in[gid1d - 1 - src_width].w
    };
    float pix_fm[4] = {
        in[gid1d     - src_width].x, in[gid1d     - src_width].y,
        in[gid1d     - src_width].z, in[gid1d     - src_width].w
    };
    float pix_fr[4] = {
        in[gid1d + 1 - src_width].x, in[gid1d + 1 - src_width].y,
        in[gid1d + 1 - src_width].z, in[gid1d + 1 - src_width].w
    };
    float pix_ml[4] = {
        in[gid1d - 1            ].x, in[gid1d - 1            ].y,
        in[gid1d - 1            ].z, in[gid1d - 1            ].w
    };
    float pix_mm[4] = {
        in[gid1d                ].x, in[gid1d                ].y,
        in[gid1d                ].z, in[gid1d                ].w
    };
    float pix_mr[4] = {
        in[gid1d + 1            ].x, in[gid1d + 1            ].y,
        in[gid1d + 1            ].z, in[gid1d + 1            ].w
    };
    float pix_bl[4] = {
        in[gid1d - 1 + src_width].x, in[gid1d - 1 + src_width].y,
        in[gid1d - 1 + src_width].z, in[gid1d - 1 + src_width].w
    };
    float pix_bm[4] = {
        in[gid1d     + src_width].x, in[gid1d     + src_width].y,
        in[gid1d     + src_width].z, in[gid1d     + src_width].w
    };
    float pix_br[4] = {
        in[gid1d + 1 + src_width].x, in[gid1d + 1 + src_width].y,
        in[gid1d + 1 + src_width].z, in[gid1d + 1 + src_width].w
    };

    int c;
    float value[4];

    for (c = 0;c < 3; ++c)
    {
        float current = pix_mm[c];
        current =
            ((current > 0.0f) &&
             (pix_fl[c] < 0.0f || pix_fm[c] < 0.0f ||
              pix_fr[c] < 0.0f || pix_ml[c] < 0.0f ||
              pix_mr[c] < 0.0f || pix_bl[c] < 0.0f ||
              pix_bm[c] < 0.0f || pix_br[c] < 0.0f )
            ) ? current : 0.0f;
        value[c] = current;
    }
    value[3] = pix_mm[3];

    out[gidx + gidy * get_global_size(0)] = (float4)
        (value[0], value[1], value[2], value[3]);
}