Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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 "arm_compute/core/NEON/kernels/NEFillInnerBorderKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class Coordinates; |
| 42 | } // namespace arm_compute |
| 43 | |
| 44 | NEFillInnerBorderKernel::NEFillInnerBorderKernel() |
| 45 | : _tensor(nullptr), _border_size(0), _constant_border_value(0) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void NEFillInnerBorderKernel::configure(ITensor *input, BorderSize border_size, const PixelValue &constant_border_value) |
| 50 | { |
| 51 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::S32, DataType::F32); |
| 52 | |
| 53 | _tensor = input; |
| 54 | _border_size = border_size; |
| 55 | _constant_border_value = constant_border_value; |
| 56 | |
| 57 | Window win; |
| 58 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 59 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 60 | win.use_tensor_dimensions(_tensor->info()->tensor_shape(), Window::DimZ); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | INEKernel::configure(win); |
| 62 | } |
| 63 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 64 | void NEFillInnerBorderKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 68 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 69 | |
| 70 | // If there is no border: early exit |
| 71 | if(_border_size.empty()) |
| 72 | { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | switch(_tensor->info()->data_type()) |
| 77 | { |
| 78 | case DataType::U8: |
| 79 | fill_value_single_channel<uint8_t>(window); |
| 80 | break; |
| 81 | case DataType::S16: |
| 82 | fill_value_single_channel<int16_t>(window); |
| 83 | break; |
| 84 | case DataType::S32: |
| 85 | fill_value_single_channel<int32_t>(window); |
| 86 | break; |
| 87 | case DataType::F32: |
| 88 | static_assert(sizeof(float) == 4, "Float must be 32 bit"); |
| 89 | fill_value_single_channel<float>(window); |
| 90 | break; |
| 91 | default: |
| 92 | ARM_COMPUTE_ERROR("Not handled"); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | template <typename T> |
| 98 | void NEFillInnerBorderKernel::fill_value_single_channel(const Window &window) |
| 99 | { |
| 100 | const size_t stride = _tensor->info()->strides_in_bytes()[1]; |
| 101 | const size_t width = _tensor->info()->dimension(0); |
| 102 | const size_t height = _tensor->info()->dimension(1); |
| 103 | |
| 104 | T constant_border_value; |
| 105 | _constant_border_value.get(constant_border_value); |
| 106 | |
| 107 | // Left and right border |
| 108 | // All X values are set at once |
| 109 | Window vertical(window); |
| 110 | vertical.set(Window::DimY, Window::Dimension(0, height, 1)); |
| 111 | |
| 112 | Iterator vertical_it(_tensor, vertical); |
| 113 | |
| 114 | execute_window_loop(vertical, [&](const Coordinates & id) |
| 115 | { |
| 116 | std::fill_n(reinterpret_cast<T *>(vertical_it.ptr()), _border_size.left, constant_border_value); |
| 117 | std::fill_n(reinterpret_cast<T *>(vertical_it.ptr()) + width - _border_size.right, _border_size.right, constant_border_value); |
| 118 | }, |
| 119 | vertical_it); |
| 120 | |
| 121 | // Top and bottom border |
| 122 | // All values are set at once |
| 123 | Iterator horizontal_it(_tensor, window); |
| 124 | |
| 125 | execute_window_loop(window, [&](const Coordinates & id) |
| 126 | { |
| 127 | for(size_t i = 0; i < _border_size.top; ++i) |
| 128 | { |
| 129 | std::fill_n(reinterpret_cast<T *>(horizontal_it.ptr() + i * stride), width, constant_border_value); |
| 130 | } |
| 131 | |
| 132 | for(size_t i = 0; i < _border_size.bottom; ++i) |
| 133 | { |
| 134 | std::fill_n(reinterpret_cast<T *>(horizontal_it.ptr() + (height - i - 1) * stride), width, constant_border_value); |
| 135 | } |
| 136 | }, |
| 137 | horizontal_it); |
| 138 | } |