summaryrefslogtreecommitdiff
path: root/opencl/snn-mean.cl.h
blob: 21b4935d081f1c013e4f59d8acac099bb697823a (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
static const char* snn_mean_cl_source =
"float colordiff (float4 pixA,                                                 \n"
"                 float4 pixB)                                                 \n"
"{                                                                             \n"
"    float4 pix = pixA-pixB;                                                   \n"
"    pix *= pix;                                                               \n"
"    return pix.x+pix.y+pix.z;                                                 \n"
"}                                                                             \n"
"                                                                              \n"
"__kernel void snn_mean (__global const   float4 *src_buf,                     \n"
"                                         int src_width,                       \n"
"                                         int src_height,                      \n"
"                        __global         float4 *dst_buf,                     \n"
"                                         int radius,                          \n"
"                                         int pairs)                           \n"
"{                                                                             \n"
"    int gidx   =get_global_id(0);                                             \n"
"    int gidy   =get_global_id(1);                                             \n"
"    int offset =gidy * get_global_size(0) + gidx;                             \n"
"                                                                              \n"
"    __global const float4 *center_pix=                                        \n"
"        src_buf + ((radius+gidx) + (gidy+radius)* src_width);                 \n"
"    float4 accumulated=0;                                                     \n"
"                                                                              \n"
"    int count=0;                                                              \n"
"    if(pairs==2)                                                              \n"
"    {                                                                         \n"
"        for(int i=-radius;i<0;i++)                                            \n"
"        {                                                                     \n"
"            for(int j=-radius;j<0;j++)                                        \n"
"            {                                                                 \n"
"                __global const float4 *selected_pix = center_pix;             \n"
"                float  best_diff = 1000.0f;                                   \n"
"                                                                              \n"
"                    int xs[4]={                                               \n"
"                        gidx+j+radius, gidx-j+radius,                         \n"
"                        gidx-j+radius, gidx+j+radius                          \n"
"                    };                                                        \n"
"                    int ys[4]={                                               \n"
"                        gidy+i+radius, gidy-i+radius,                         \n"
"                        gidy+i+radius, gidy-i+radius};                        \n"
"                                                                              \n"
"                    for (int k=0;k<4;k++)                                     \n"
"                    {                                                         \n"
"                        if (xs[k] >= 0 && xs[k] < src_width &&                \n"
"                            ys[k] >= 0 && ys[k] < src_height)                 \n"
"                        {                                                     \n"
"                            __global const float4 *tpix =                     \n"
"                                src_buf + (xs[k] + ys[k] * src_width);        \n"
"                            float diff=colordiff(*tpix, *center_pix);         \n"
"                            if (diff < best_diff)                             \n"
"                            {                                                 \n"
"                                best_diff = diff;                             \n"
"                                selected_pix = tpix;                          \n"
"                            }                                                 \n"
"                        }                                                     \n"
"                    }                                                         \n"
"                                                                              \n"
"                accumulated += *selected_pix;                                 \n"
"                                                                              \n"
"                ++count;                                                      \n"
"                if (i==0 && j==0)                                             \n"
"                    break;                                                    \n"
"            }                                                                 \n"
"        }                                                                     \n"
"        dst_buf[offset] = accumulated/count;                                  \n"
"        return;                                                               \n"
"    }                                                                         \n"
"    else if(pairs==1)                                                         \n"
"    {                                                                         \n"
"        for(int i=-radius;i<=0;i++)                                           \n"
"        {                                                                     \n"
"            for(int j=-radius;j<=radius;j++)                                  \n"
"            {                                                                 \n"
"                __global const float4 *selected_pix = center_pix;             \n"
"                float  best_diff = 1000.0f;                                   \n"
"                                                                              \n"
"                /* skip computations for the center pixel */                  \n"
"                if (i != 0 && j != 0)                                         \n"
"                {                                                             \n"
"                    int xs[4]={                                               \n"
"                        gidx+i+radius, gidx-i+radius,                         \n"
"                        gidx-i+radius, gidx+i+radius                          \n"
"                    };                                                        \n"
"                    int ys[4]={                                               \n"
"                        gidy+j+radius, gidy-j+radius,                         \n"
"                        gidy+j+radius, gidy-j+radius                          \n"
"                    };                                                        \n"
"                                                                              \n"
"                    for (i=0;i<2;i++)                                         \n"
"                    {                                                         \n"
"                        if (xs[i] >= 0 && xs[i] < src_width &&                \n"
"                            ys[i] >= 0 && ys[i] < src_height)                 \n"
"                        {                                                     \n"
"                            __global const float4 *tpix =                     \n"
"                                src_buf + (xs[i] + ys[i] * src_width);        \n"
"                            float diff=colordiff (*tpix, *center_pix);        \n"
"                            if (diff < best_diff)                             \n"
"                            {                                                 \n"
"                                best_diff = diff;                             \n"
"                                selected_pix = tpix;                          \n"
"                            }                                                 \n"
"                        }                                                     \n"
"                    }                                                         \n"
"                }                                                             \n"
"                accumulated += *selected_pix;                                 \n"
"                ++count;                                                      \n"
"                if (i==0 && j==0)                                             \n"
"                    break;                                                    \n"
"            }                                                                 \n"
"        }                                                                     \n"
"        dst_buf[offset] = accumulated/count;                                  \n"
"        return;                                                               \n"
"    }                                                                         \n"
"    return;                                                                   \n"
"}                                                                             \n"
;