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