ported changes from PR #2867
[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 #define MIN_VAL 0
58 #define MAX_VAL UCHAR_MAX
59 #elif defined DEPTH_1
60 #define MIN_VAL SCHAR_MIN
61 #define MAX_VAL SCHAR_MAX
62 #elif defined DEPTH_2
63 #define MIN_VAL 0
64 #define MAX_VAL USHRT_MAX
65 #elif defined DEPTH_3
66 #define MIN_VAL SHRT_MIN
67 #define MAX_VAL SHRT_MAX
68 #elif defined DEPTH_4
69 #define MIN_VAL INT_MIN
70 #define MAX_VAL INT_MAX
71 #elif defined DEPTH_5
72 #define MIN_VAL (-FLT_MAX)
73 #define MAX_VAL FLT_MAX
74 #elif defined DEPTH_6
75 #define MIN_VAL (-DBL_MAX)
76 #define MAX_VAL DBL_MAX
77 #endif
78
79 #ifdef OP_ERODE
80 #define VAL MAX_VAL
81 #elif defined OP_DILATE
82 #define VAL MIN_VAL
83 #else
84 #error "Unknown operation"
85 #endif
86
87 #ifdef OP_ERODE
88 #if defined INTEL_DEVICE && defined DEPTH_0
89 // workaround for bug in Intel HD graphics drivers (10.18.10.3496 or older)
90 #define __CAT(x, y) x##y
91 #define CAT(x, y) __CAT(x, y)
92 #define WA_CONVERT_1 CAT(convert_uint, cn)
93 #define WA_CONVERT_2 CAT(convert_, T)
94 #define convert_uint1 convert_uint
95 #define MORPH_OP(A, B) WA_CONVERT_2(min(WA_CONVERT_1(A), WA_CONVERT_1(B)))
96 #else
97 #define MORPH_OP(A, B) min((A), (B))
98 #endif
99 #endif
100 #ifdef OP_DILATE
101 #define MORPH_OP(A, B) max((A), (B))
102 #endif
103
104 #define PROCESS(y, x) \
105     res = MORPH_OP(res, LDS_DAT[mad24(l_y + y, width, l_x + x)]);
106
107 // BORDER_CONSTANT:      iiiiii|abcdefgh|iiiiiii
108 #define ELEM(i, l_edge, r_edge, elem1, elem2) (i) < (l_edge) | (i) >= (r_edge) ? (elem1) : (elem2)
109
110
111 __kernel void morph(__global const uchar * srcptr, int src_step, int src_offset,
112                     __global uchar * dstptr, int dst_step, int dst_offset,
113                     int src_offset_x, int src_offset_y, int cols, int rows,
114                     int src_whole_cols, int src_whole_rows EXTRA_PARAMS)
115 {
116     int gidx = get_global_id(0), gidy = get_global_id(1);
117     int l_x = get_local_id(0), l_y = get_local_id(1);
118     int x = get_group_id(0) * LSIZE0, y = get_group_id(1) * LSIZE1;
119     int start_x = x + src_offset_x - RADIUSX;
120     int width = mad24(RADIUSX, 2, LSIZE0 + 1);
121     int start_y = y + src_offset_y - RADIUSY;
122     int point1 = mad24(l_y, LSIZE0, l_x);
123     int point2 = point1 + LSIZE0 * LSIZE1;
124     int tl_x = point1 % width, tl_y = point1 / width;
125     int tl_x2 = point2 % width, tl_y2 = point2 / width;
126     int cur_x = start_x + tl_x, cur_y = start_y + tl_y;
127     int cur_x2 = start_x + tl_x2, cur_y2 = start_y + tl_y2;
128     int start_addr = mad24(cur_y, src_step, cur_x * TSIZE);
129     int start_addr2 = mad24(cur_y2, src_step, cur_x2 * TSIZE);
130
131     __local T LDS_DAT[2 * LSIZE1 * LSIZE0];
132
133     // read pixels from src
134     int end_addr = mad24(src_whole_rows - 1, src_step, src_whole_cols * TSIZE);
135     start_addr = start_addr < end_addr && start_addr > 0 ? start_addr : 0;
136     start_addr2 = start_addr2 < end_addr && start_addr2 > 0 ? start_addr2 : 0;
137
138     T temp0 = loadpix(srcptr + start_addr);
139     T temp1 = loadpix(srcptr + start_addr2);
140
141     // judge if read out of boundary
142     temp0 = ELEM(cur_x, 0, src_whole_cols, (T)(VAL), temp0);
143     temp0 = ELEM(cur_y, 0, src_whole_rows, (T)(VAL), temp0);
144
145     temp1 = ELEM(cur_x2, 0, src_whole_cols, (T)(VAL), temp1);
146     temp1 = ELEM(cur_y2, 0, src_whole_rows, (T)(VAL), temp1);
147
148     LDS_DAT[point1] = temp0;
149     LDS_DAT[point2] = temp1;
150     barrier(CLK_LOCAL_MEM_FENCE);
151
152     if (gidx < cols && gidy < rows)
153     {
154         T res = (T)(VAL);
155         PROCESS_ELEMS;
156
157         int dst_index = mad24(gidy, dst_step, mad24(gidx, TSIZE, dst_offset));
158         storepix(res, dstptr + dst_index);
159     }
160 }