summaryrefslogtreecommitdiff
path: root/opencl/edge-sobel.cl.h
blob: 2dc5b0c67b54550d2f6a84c375c5659155dd3e5e (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
static const char* edge_sobel_cl_source =
"#define SOBEL_RADIUS 1                                                        \n"
"kernel void kernel_edgesobel(global float4 *in,                               \n"
"                             global float4 *out,                              \n"
"                             const int horizontal,                            \n"
"                             const int vertical,                              \n"
"                             const int keep_sign,                             \n"
"                             const int has_alpha)                             \n"
"{                                                                             \n"
"    int gidx = get_global_id(0);                                              \n"
"    int gidy = get_global_id(1);                                              \n"
"                                                                              \n"
"    float4 hor_grad = 0.0f;                                                   \n"
"    float4 ver_grad = 0.0f;                                                   \n"
"    float4 gradient = 0.0f;                                                   \n"
"                                                                              \n"
"    int dst_width = get_global_size(0);                                       \n"
"    int src_width = dst_width + SOBEL_RADIUS * 2;                             \n"
"                                                                              \n"
"    int i = gidx + SOBEL_RADIUS, j = gidy + SOBEL_RADIUS;                     \n"
"    int gid1d = i + j * src_width;                                            \n"
"                                                                              \n"
"    float4 pix_fl = in[gid1d - 1 - src_width];                                \n"
"    float4 pix_fm = in[gid1d     - src_width];                                \n"
"    float4 pix_fr = in[gid1d + 1 - src_width];                                \n"
"    float4 pix_ml = in[gid1d - 1            ];                                \n"
"    float4 pix_mm = in[gid1d                ];                                \n"
"    float4 pix_mr = in[gid1d + 1            ];                                \n"
"    float4 pix_bl = in[gid1d - 1 + src_width];                                \n"
"    float4 pix_bm = in[gid1d     + src_width];                                \n"
"    float4 pix_br = in[gid1d + 1 + src_width];                                \n"
"                                                                              \n"
"    if (horizontal)                                                           \n"
"    {                                                                         \n"
"        hor_grad +=                                                           \n"
"            - 1.0f * pix_fl + 1.0f * pix_fr                                   \n"
"            - 2.0f * pix_ml + 2.0f * pix_mr                                   \n"
"            - 1.0f * pix_bl + 1.0f * pix_br;                                  \n"
"    }                                                                         \n"
"    if (vertical)                                                             \n"
"    {                                                                         \n"
"        ver_grad +=                                                           \n"
"            - 1.0f * pix_fl - 2.0f * pix_fm                                   \n"
"            - 1.0f * pix_fr + 1.0f * pix_bl                                   \n"
"            + 2.0f * pix_bm + 1.0f * pix_br;                                  \n"
"    }                                                                         \n"
"                                                                              \n"
"    if (horizontal && vertical)                                               \n"
"    {                                                                         \n"
"        gradient = sqrt(                                                      \n"
"            hor_grad * hor_grad +                                             \n"
"            ver_grad * ver_grad) / 1.41f;                                     \n"
"    }                                                                         \n"
"    else                                                                      \n"
"    {                                                                         \n"
"        if (keep_sign)                                                        \n"
"            gradient = hor_grad + ver_grad;                                   \n"
"        else                                                                  \n"
"            gradient = fabs(hor_grad + ver_grad);                             \n"
"    }                                                                         \n"
"                                                                              \n"
"    if (has_alpha)                                                            \n"
"    {                                                                         \n"
"      gradient.w = pix_mm.w;                                                  \n"
"    }                                                                         \n"
"    else                                                                      \n"
"    {                                                                         \n"
"      gradient.w = 1.0f;                                                      \n"
"    }                                                                         \n"
"                                                                              \n"
"    out[gidx + gidy * dst_width] = gradient;                                  \n"
"}                                                                             \n"
;