John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 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 |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 8 | * deal in the Software without restriction, including without limitation the |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 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 | * |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 13 | * The above copyright notice and this permission notice shall be included in all |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 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, |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 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 |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "NonLinearFilter.h" |
| 25 | #include "Utils.h" |
| 26 | |
| 27 | namespace arm_compute |
| 28 | { |
| 29 | namespace test |
| 30 | { |
| 31 | namespace validation |
| 32 | { |
| 33 | namespace reference |
| 34 | { |
| 35 | template <typename T> |
| 36 | SimpleTensor<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)); |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 52 | const uint32_t num_elements = src.num_elements(); |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 53 | |
Michalis Spyrou | fae513c | 2019-10-16 17:41:33 +0100 | [diff] [blame] | 54 | for(uint32_t element_idx = 0, count = 0, index = 0; element_idx < num_elements; ++element_idx, count = 0, index = 0) |
John Richardson | 6f4d49f | 2017-09-07 11:21:10 +0100 | [diff] [blame] | 55 | { |
| 56 | Coordinates id = index2coord(src.shape(), element_idx); |
| 57 | if(is_in_valid_region(valid_region, id)) |
| 58 | { |
| 59 | int idx = id.x(); |
| 60 | int idy = id.y(); |
| 61 | for(int y = idy - half_mask_size; y <= idy + half_mask_size; ++y) |
| 62 | { |
| 63 | for(int x = idx - half_mask_size; x <= idx + half_mask_size; ++x, ++index) |
| 64 | { |
| 65 | id.set(0, x); |
| 66 | id.set(1, y); |
| 67 | current_value = tensor_elem_at(src, id, border_mode, constant_border_value); |
| 68 | |
| 69 | if(mask[index] == 255) |
| 70 | { |
| 71 | vals[count] = static_cast<intermediate_type>(current_value); |
| 72 | ++count; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | std::sort(vals.begin(), vals.begin() + count); |
| 77 | |
| 78 | ARM_COMPUTE_ERROR_ON(count == 0); |
| 79 | |
| 80 | switch(function) |
| 81 | { |
| 82 | case NonLinearFilterFunction::MIN: |
| 83 | dst[element_idx] = saturate_cast<T>(vals[0]); |
| 84 | break; |
| 85 | case NonLinearFilterFunction::MAX: |
| 86 | dst[element_idx] = saturate_cast<T>(vals[count - 1]); |
| 87 | break; |
| 88 | case NonLinearFilterFunction::MEDIAN: |
| 89 | dst[element_idx] = saturate_cast<T>(vals[count / 2]); |
| 90 | break; |
| 91 | default: |
| 92 | ARM_COMPUTE_ERROR("Unsupported NonLinearFilter function."); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return dst; |
| 98 | } |
| 99 | |
| 100 | template SimpleTensor<uint8_t> non_linear_filter(const SimpleTensor<uint8_t> &src, NonLinearFilterFunction function, unsigned int mask_size, MatrixPattern pattern, const uint8_t *mask, |
| 101 | BorderMode border_mode, uint8_t constant_border_value); |
| 102 | } // namespace reference |
| 103 | } // namespace validation |
| 104 | } // namespace test |
| 105 | } // namespace arm_compute |