a7611c50f9507a840b10bb1d75911cac49c72d46
[profile/ivi/opencv.git] / modules / imgproc / src / opencl / morph.cl
1 //                           License Agreement
2 //                For Open Source Computer Vision Library
3 //
4 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
5 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
6 // Third party copyrights are property of their respective owners.
7 //
8 // @Authors
9 //    Niko Li, newlife20080214@gmail.com
10 //    Zero Lin, zero.lin@amd.com
11 //    Yao Wang, bitwangyaoyao@gmail.com
12 // Redistribution and use in source and binary forms, with or without modification,
13 // are permitted provided that the following conditions are met:
14 //
15 //   * Redistribution's of source code must retain the above copyright notice,
16 //     this list of conditions and the following disclaimer.
17 //
18 //   * Redistribution's in binary form must reproduce the above copyright notice,
19 //     this list of conditions and the following disclaimer in the documentation
20 //     and/or other materials provided with the distribution.
21 //
22 //   * The name of the copyright holders may not be used to endorse or promote products
23 //     derived from this software without specific prior written permission.
24 //
25 // This software is provided by the copyright holders and contributors as is and
26 // any express or implied warranties, including, but not limited to, the implied
27 // warranties of merchantability and fitness for a particular purpose are disclaimed.
28 // In no event shall the Intel Corporation or contributors be liable for any direct,
29 // indirect, incidental, special, exemplary, or consequential damages
30 // (including, but not limited to, procurement of substitute goods or services;
31 // loss of use, data, or profits; or business interruption) however caused
32 // and on any theory of liability, whether in contract, strict liability,
33 // or tort (including negligence or otherwise) arising in any way out of
34 // the use of this software, even if advised of the possibility of such damage.
35 //
36 //
37
38 #ifdef DOUBLE_SUPPORT
39 #ifdef cl_amd_fp64
40 #pragma OPENCL EXTENSION cl_amd_fp64:enable
41 #elif defined (cl_khr_fp64)
42 #pragma OPENCL EXTENSION cl_khr_fp64:enable
43 #endif
44 #endif
45
46 #if cn != 3
47 #define loadpix(addr) *(__global const T *)(addr)
48 #define storepix(val, addr)  *(__global T *)(addr) = val
49 #define TSIZE (int)sizeof(T)
50 #else
51 #define loadpix(addr) vload3(0, (__global const T1 *)(addr))
52 #define storepix(val, addr) vstore3(val, 0, (__global T1 *)(addr))
53 #define TSIZE ((int)sizeof(T1)*3)
54 #endif
55
56 #ifdef DEPTH_0
57 #ifdef ERODE
58 #define VAL 255
59 #endif
60 #ifdef DILATE
61 #define VAL 0
62 #endif
63 #elif defined DEPTH_5
64 #ifdef ERODE
65 #define VAL FLT_MAX
66 #endif
67 #ifdef DILATE
68 #define VAL -FLT_MAX
69 #endif
70 #elif defined DEPTH_6
71 #ifdef ERODE
72 #define VAL DBL_MAX
73 #endif
74 #ifdef DILATE
75 #define VAL -DBL_MAX
76 #endif
77 #endif
78
79 #ifdef ERODE
80 #if defined(INTEL_DEVICE) && (DEPTH_0)
81 // workaround for bug in Intel HD graphics drivers (10.18.10.3496 or older)
82 #define __CAT(x, y) x##y
83 #define CAT(x, y) __CAT(x, y)
84 #define WA_CONVERT_1 CAT(convert_uint, cn)
85 #define WA_CONVERT_2 CAT(convert_, T)
86 #define convert_uint1 convert_uint
87 #define MORPH_OP(A,B) WA_CONVERT_2(min(WA_CONVERT_1(A),WA_CONVERT_1(B)))
88 #else
89 #define MORPH_OP(A,B) min((A),(B))
90 #endif
91 #endif
92 #ifdef DILATE
93 #define MORPH_OP(A,B) max((A),(B))
94 #endif
95
96 // BORDER_CONSTANT:      iiiiii|abcdefgh|iiiiiii
97 #define ELEM(i, l_edge, r_edge, elem1, elem2) (i) < (l_edge) | (i) >= (r_edge) ? (elem1) : (elem2)
98
99 __kernel void morph(__global const uchar * srcptr, int src_step, int src_offset,
100                     __global uchar * dstptr, int dst_step, int dst_offset,
101                     int src_offset_x, int src_offset_y, int cols, int rows,
102                     __constant uchar * mat_kernel, int src_whole_cols, int src_whole_rows)
103 {
104     int gidx = get_global_id(0), gidy = get_global_id(1);
105     int l_x = get_local_id(0), l_y = get_local_id(1);
106     int x = get_group_id(0) * LSIZE0, y = get_group_id(1) * LSIZE1;
107     int start_x = x + src_offset_x - RADIUSX;
108     int end_x = x + src_offset_x + LSIZE0 + RADIUSX;
109     int width = end_x - (x + src_offset_x - RADIUSX) + 1;
110     int start_y = y + src_offset_y - RADIUSY;
111     int point1 = mad24(l_y, LSIZE0, l_x);
112     int point2 = point1 + LSIZE0 * LSIZE1;
113     int tl_x = point1 % width, tl_y = point1 / width;
114     int tl_x2 = point2 % width, tl_y2 = point2 / width;
115     int cur_x = start_x + tl_x, cur_y = start_y + tl_y;
116     int cur_x2 = start_x + tl_x2, cur_y2 = start_y + tl_y2;
117     int start_addr = mad24(cur_y, src_step, cur_x * TSIZE);
118     int start_addr2 = mad24(cur_y2, src_step, cur_x2 * TSIZE);
119
120     __local T LDS_DAT[2*LSIZE1*LSIZE0];
121
122     // read pixels from src
123     int end_addr = mad24(src_whole_rows - 1, src_step, src_whole_cols * TSIZE);
124     start_addr = start_addr < end_addr && start_addr > 0 ? start_addr : 0;
125     start_addr2 = start_addr2 < end_addr && start_addr2 > 0 ? start_addr2 : 0;
126
127     T temp0 = loadpix(srcptr + start_addr);
128     T temp1 = loadpix(srcptr + start_addr2);
129
130     // judge if read out of boundary
131     temp0 = ELEM(cur_x, 0, src_whole_cols, (T)(VAL),temp0);
132     temp0 = ELEM(cur_y, 0, src_whole_rows, (T)(VAL),temp0);
133
134     temp1 = ELEM(cur_x2, 0, src_whole_cols, (T)(VAL), temp1);
135     temp1 = ELEM(cur_y2, 0, src_whole_rows, (T)(VAL), temp1);
136
137     LDS_DAT[point1] = temp0;
138     LDS_DAT[point2] = temp1;
139     barrier(CLK_LOCAL_MEM_FENCE);
140
141     T res = (T)(VAL);
142     for (int i = 0, sizey = 2 * RADIUSY + 1; i < sizey; i++)
143         for (int j = 0, sizex = 2 * RADIUSX + 1; j < sizex; j++)
144         {
145             res =
146 #ifndef RECTKERNEL
147                 mat_kernel[i*(2*RADIUSX+1)+j] ?
148 #endif
149                 MORPH_OP(res, LDS_DAT[mad24(l_y + i, width, l_x + j)])
150 #ifndef RECTKERNEL
151                 : res
152 #endif
153                 ;
154         }
155
156     if (gidx < cols && gidy < rows)
157     {
158         int dst_index = mad24(gidy, dst_step, mad24(gidx, TSIZE, dst_offset));
159         storepix(res, dstptr + dst_index);
160     }
161 }