2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
25 #include "non_linear_filter_helpers.h"
27 /** This function applies a non linear filter on a 3x3 box basis on an input image.
29 * @note The needed filter operation is defined through the preprocessor by passing either -DMIN, -DMAX or -DMEDIAN.
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
44 __kernel void non_linear_filter_box3x3(
45 IMAGE_DECLARATION(src),
46 IMAGE_DECLARATION(dst))
48 Image src = CONVERT_TO_IMAGE_STRUCT(src);
49 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
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));
56 // Apply respective filter
58 uchar16 tmp = min(top, min(middle, bottom));
59 uchar8 out = row_reduce_min_3(tmp);
61 uchar16 tmp = max(top, max(middle, bottom));
62 uchar8 out = row_reduce_max_3(tmp);
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 */
79 vstore8(out, 0, dst.ptr);
82 /** This function applies a non linear filter on a 3x3 cross basis on an input image.
84 * @note The needed filter operation is defined through the preprocessor by passing either -DMIN, -DMAX or -DMEDIAN.
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
99 __kernel void non_linear_filter_cross3x3(
100 IMAGE_DECLARATION(src),
101 IMAGE_DECLARATION(dst))
103 Image src = CONVERT_TO_IMAGE_STRUCT(src);
104 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
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));
111 // Apply respective filter
113 uchar8 tmp_middle = row_reduce_min_3(middle);
114 uchar8 out = min(tmp_middle, min(top, bottom));
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 */
130 vstore8(out, 0, dst.ptr);
133 /** This function applies a non linear filter on a 3x3 disk basis on an input image.
135 * @note The needed filter operation is defined through the preprocessor by passing either -DMIN, -DMAX or -DMEDIAN.
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
150 __kernel void non_linear_filter_disk3x3(
151 IMAGE_DECLARATION(src),
152 IMAGE_DECLARATION(dst))
154 Image src = CONVERT_TO_IMAGE_STRUCT(src);
155 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
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));
162 // Apply respective filter
164 uchar16 tmp = min(top, min(middle, bottom));
165 uchar8 out = row_reduce_min_3(tmp);
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 */
185 vstore8(out, 0, dst.ptr);