blob: 8669c9c7764c6b28cf868fc561a709c31e516ca6 [file] [log] [blame]
John Richardson6f4d49f2017-09-07 11:21:10 +01001/*
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 src 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 src 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. src NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER src AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * dst OF OR src CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "NonLinearFilter.h"
25#include "Utils.h"
26
27namespace arm_compute
28{
29namespace test
30{
31namespace validation
32{
33namespace reference
34{
35template <typename T>
36SimpleTensor<T> non_linear_filter(const SimpleTensor<T> &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode,
37 uint8_t constant_border_value)
38{
39 SimpleTensor<T> dst(src.shape(), src.data_type());
40
41 ARM_COMPUTE_ERROR_ON(pattern == MatrixPattern::OTHER && mask == nullptr);
42 ARM_COMPUTE_UNUSED(pattern);
43
44 using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type;
45
46 const int sq_mask_size = mask_size * mask_size;
47 const int half_mask_size = mask_size / 2;
48 std::vector<intermediate_type> vals(sq_mask_size);
49 intermediate_type current_value = 0;
50
51 const ValidRegion valid_region = shape_to_valid_region(src.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(half_mask_size));
52
53 for(int element_idx = 0, count = 0, index = 0; element_idx < src.num_elements(); ++element_idx, count = 0, index = 0)
54 {
55 Coordinates id = index2coord(src.shape(), element_idx);
56 if(is_in_valid_region(valid_region, id))
57 {
58 int idx = id.x();
59 int idy = id.y();
60 for(int y = idy - half_mask_size; y <= idy + half_mask_size; ++y)
61 {
62 for(int x = idx - half_mask_size; x <= idx + half_mask_size; ++x, ++index)
63 {
64 id.set(0, x);
65 id.set(1, y);
66 current_value = tensor_elem_at(src, id, border_mode, constant_border_value);
67
68 if(mask[index] == 255)
69 {
70 vals[count] = static_cast<intermediate_type>(current_value);
71 ++count;
72 }
73 }
74 }
75 std::sort(vals.begin(), vals.begin() + count);
76
77 ARM_COMPUTE_ERROR_ON(count == 0);
78
79 switch(function)
80 {
81 case NonLinearFilterFunction::MIN:
82 dst[element_idx] = saturate_cast<T>(vals[0]);
83 break;
84 case NonLinearFilterFunction::MAX:
85 dst[element_idx] = saturate_cast<T>(vals[count - 1]);
86 break;
87 case NonLinearFilterFunction::MEDIAN:
88 dst[element_idx] = saturate_cast<T>(vals[count / 2]);
89 break;
90 default:
91 ARM_COMPUTE_ERROR("Unsupported NonLinearFilter function.");
92 }
93 }
94 }
95
96 return dst;
97}
98
99template SimpleTensor<uint8_t> non_linear_filter(const SimpleTensor<uint8_t> &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask,
100 BorderMode border_mode, uint8_t constant_border_value);
101} // namespace reference
102} // namespace validation
103} // namespace test
104} // namespace arm_compute