arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / non_linear_filter3x3.cl
1 /*
2  * Copyright (c) 2017 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "helpers.h"
25 #include "non_linear_filter_helpers.h"
26
27 /** This function applies a non linear filter on a 3x3 box basis on an input image.
28  *
29  * @note The needed filter operation is defined through the preprocessor by passing either -DMIN, -DMAX or -DMEDIAN.
30  *
31  * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
32  * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
33  * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
34  * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
35  * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
36  * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
37  * @param[out] dst_ptr                           Pointer to the destination image. Supported data types: U8
38  * @param[in]  dst_stride_x                      Stride of the destination image in X dimension (in bytes)
39  * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
40  * @param[in]  dst_stride_y                      Stride of the destination image in Y dimension (in bytes)
41  * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
42  * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination image
43  */
44 __kernel void non_linear_filter_box3x3(
45     IMAGE_DECLARATION(src),
46     IMAGE_DECLARATION(dst))
47 {
48     Image src = CONVERT_TO_IMAGE_STRUCT(src);
49     Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
50
51     // Load values
52     uchar16 top    = vload16(0, offset(&src, -1, -1));
53     uchar16 middle = vload16(0, offset(&src, -1, 0));
54     uchar16 bottom = vload16(0, offset(&src, -1, 1));
55
56     // Apply respective filter
57 #ifdef MIN
58     uchar16 tmp = min(top, min(middle, bottom));
59     uchar8  out = row_reduce_min_3(tmp);
60 #elif defined(MAX)
61     uchar16 tmp = max(top, max(middle, bottom));
62     uchar8  out = row_reduce_max_3(tmp);
63 #elif defined(MEDIAN)
64     uchar8 p0  = top.s01234567;
65     uchar8 p1  = top.s12345678;
66     uchar8 p2  = top.s23456789;
67     uchar8 p3  = middle.s01234567;
68     uchar8 p4  = middle.s12345678;
69     uchar8 p5  = middle.s23456789;
70     uchar8 p6  = bottom.s01234567;
71     uchar8 p7  = bottom.s12345678;
72     uchar8 p8  = bottom.s23456789;
73     uchar8 out = sort9(p0, p1, p2, p3, p4, p5, p6, p7, p8);
74 #else /* MIN or MAX or MEDIAN */
75 #error "Unsupported filter function"
76 #endif /* MIN or MAX or MEDIAN */
77
78     // Store result
79     vstore8(out, 0, dst.ptr);
80 }
81
82 /** This function applies a non linear filter on a 3x3 cross basis on an input image.
83  *
84  * @note The needed filter operation is defined through the preprocessor by passing either -DMIN, -DMAX or -DMEDIAN.
85  *
86  * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
87  * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
88  * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
89  * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
90  * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
91  * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
92  * @param[out] dst_ptr                           Pointer to the destination image. Supported data types: U8
93  * @param[in]  dst_stride_x                      Stride of the destination image in X dimension (in bytes)
94  * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
95  * @param[in]  dst_stride_y                      Stride of the destination image in Y dimension (in bytes)
96  * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
97  * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination image
98  */
99 __kernel void non_linear_filter_cross3x3(
100     IMAGE_DECLARATION(src),
101     IMAGE_DECLARATION(dst))
102 {
103     Image src = CONVERT_TO_IMAGE_STRUCT(src);
104     Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
105
106     // Load values
107     uchar8  top    = vload8(0, offset(&src, 0, -1));
108     uchar16 middle = vload16(0, offset(&src, -1, 0));
109     uchar8  bottom = vload8(0, offset(&src, 0, 1));
110
111     // Apply respective filter
112 #ifdef MIN
113     uchar8 tmp_middle = row_reduce_min_3(middle);
114     uchar8 out        = min(tmp_middle, min(top, bottom));
115 #elif defined(MAX)
116     uchar8  tmp_middle = row_reduce_max_3(middle);
117     uchar8  out        = max(tmp_middle, max(top, bottom));
118 #elif defined(MEDIAN)
119     uchar8 p0  = top.s01234567;
120     uchar8 p1  = middle.s01234567;
121     uchar8 p2  = middle.s12345678;
122     uchar8 p3  = middle.s23456789;
123     uchar8 p4  = bottom.s01234567;
124     uchar8 out = sort5(p0, p1, p2, p3, p4);
125 #else /* MIN or MAX or MEDIAN */
126 #error "Unsupported filter function"
127 #endif /* MIN or MAX or MEDIAN */
128
129     // Store result
130     vstore8(out, 0, dst.ptr);
131 }
132
133 /** This function applies a non linear filter on a 3x3 disk basis on an input image.
134  *
135  * @note The needed filter operation is defined through the preprocessor by passing either -DMIN, -DMAX or -DMEDIAN.
136  *
137  * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
138  * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
139  * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
140  * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
141  * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
142  * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
143  * @param[out] dst_ptr                           Pointer to the destination image. Supported data types: U8
144  * @param[in]  dst_stride_x                      Stride of the destination image in X dimension (in bytes)
145  * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
146  * @param[in]  dst_stride_y                      Stride of the destination image in Y dimension (in bytes)
147  * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
148  * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination image
149  */
150 __kernel void non_linear_filter_disk3x3(
151     IMAGE_DECLARATION(src),
152     IMAGE_DECLARATION(dst))
153 {
154     Image src = CONVERT_TO_IMAGE_STRUCT(src);
155     Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
156
157     // Load values
158     uchar16 top    = vload16(0, offset(&src, -1, -1));
159     uchar16 middle = vload16(0, offset(&src, -1, 0));
160     uchar16 bottom = vload16(0, offset(&src, -1, 1));
161
162     // Apply respective filter
163 #ifdef MIN
164     uchar16 tmp = min(top, min(middle, bottom));
165     uchar8  out = row_reduce_min_3(tmp);
166 #elif defined(MAX)
167     uchar16 tmp        = max(top, max(middle, bottom));
168     uchar8  out        = row_reduce_max_3(tmp);
169 #elif defined(MEDIAN)
170     uchar8 p0  = top.s01234567;
171     uchar8 p1  = top.s12345678;
172     uchar8 p2  = top.s23456789;
173     uchar8 p3  = middle.s01234567;
174     uchar8 p4  = middle.s12345678;
175     uchar8 p5  = middle.s23456789;
176     uchar8 p6  = bottom.s01234567;
177     uchar8 p7  = bottom.s12345678;
178     uchar8 p8  = bottom.s23456789;
179     uchar8 out = sort9(p0, p1, p2, p3, p4, p5, p6, p7, p8);
180 #else /* MIN or MAX or MEDIAN */
181 #error "Unsupported filter function"
182 #endif /* MIN or MAX or MEDIAN */
183
184     // Store result
185     vstore8(out, 0, dst.ptr);
186 }