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/runtime/NEON/functions/NEScale.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/NEON/kernels/NEScaleKernel.h" |
| 31 | #include "arm_compute/core/PixelValue.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | #include "arm_compute/runtime/TensorAllocator.h" |
| 35 | |
| 36 | #include <cmath> |
| 37 | #include <cstddef> |
| 38 | #include <utility> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size) |
| 45 | { |
| 46 | ARM_COMPUTE_ERROR_ON(nullptr == offsets); |
| 47 | |
| 48 | Window win; |
| 49 | win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1)); |
| 50 | win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1)); |
| 51 | |
| 52 | if(dx != nullptr && dy != nullptr) |
| 53 | { |
| 54 | // Pre-compute the offset and pixel's distance for BILINEAR interpolation |
| 55 | Iterator offsets_it(offsets, win); |
| 56 | Iterator dx_it(dx, win); |
| 57 | Iterator dy_it(dy, win); |
| 58 | |
| 59 | execute_window_loop(win, [&](const Coordinates & id) |
| 60 | { |
| 61 | const float in_x = (id.x() + 0.5f) * wr - 0.5f; |
| 62 | const float in_y = (id.y() + 0.5f) * hr - 0.5f; |
| 63 | const int in_xi = std::floor(in_x); |
| 64 | const int in_yi = std::floor(in_y); |
| 65 | |
| 66 | *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size; |
| 67 | *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi; |
| 68 | *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi; |
| 69 | }, |
| 70 | offsets_it, dx_it, dy_it); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | // Pre-compute the offset for NEAREST interpolation |
| 75 | Iterator offsets_it(offsets, win); |
| 76 | |
| 77 | execute_window_loop(win, [&](const Coordinates & id) |
| 78 | { |
| 79 | const size_t in_xi = (id.x() + 0.5f) * wr; |
| 80 | |
| 81 | *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size; |
| 82 | }, |
| 83 | offsets_it); |
| 84 | } |
| 85 | } |
| 86 | } // namespace |
| 87 | |
| 88 | NEScale::NEScale() |
| 89 | : _offsets(), _dx(), _dy() |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value) |
| 94 | { |
| 95 | ARM_COMPUTE_ERROR_ON(nullptr == input); |
| 96 | ARM_COMPUTE_ERROR_ON(nullptr == output); |
| 97 | |
| 98 | for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i) |
| 99 | { |
| 100 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i)); |
| 101 | } |
| 102 | |
| 103 | // Get the tensor shape |
| 104 | const TensorShape shape(output->info()->dimension(0), output->info()->dimension(1)); |
| 105 | |
| 106 | // Compute the ratio between source width/height and destination width/height |
| 107 | const auto wr = static_cast<float>(input->info()->dimension(0)) / static_cast<float>(output->info()->dimension(0)); |
| 108 | const auto hr = static_cast<float>(input->info()->dimension(1)) / static_cast<float>(output->info()->dimension(1)); |
| 109 | |
| 110 | // Get the element size of the input image |
| 111 | const size_t input_element_size = input->info()->element_size(); |
| 112 | |
| 113 | // Area interpolation behaves as Nearest Neighbour in case of up-sampling |
| 114 | if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f) |
| 115 | { |
| 116 | policy = InterpolationPolicy::NEAREST_NEIGHBOR; |
| 117 | } |
| 118 | |
| 119 | auto k = arm_compute::cpp14::make_unique<NEScaleKernel>(); |
| 120 | |
| 121 | // Check if the border mode is UNDEFINED |
| 122 | const bool border_undefined = border_mode == BorderMode::UNDEFINED; |
| 123 | |
| 124 | switch(policy) |
| 125 | { |
| 126 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 127 | { |
| 128 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 129 | _offsets.allocator()->init(tensor_info_offsets); |
| 130 | |
| 131 | k->configure(input, nullptr, nullptr, &_offsets, output, policy, border_undefined); |
| 132 | |
| 133 | // Allocate once the configure methods have been called |
| 134 | _offsets.allocator()->allocate(); |
| 135 | |
| 136 | // Pre-compute offsets for nearest interpolation |
| 137 | precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size); |
| 138 | break; |
| 139 | } |
| 140 | case InterpolationPolicy::BILINEAR: |
| 141 | { |
| 142 | TensorInfo tensor_info_offsets(shape, Format::S32); |
| 143 | TensorInfo tensor_info_dxdy(shape, Format::F32); |
| 144 | |
| 145 | _offsets.allocator()->init(tensor_info_offsets); |
| 146 | _dx.allocator()->init(tensor_info_dxdy); |
| 147 | _dy.allocator()->init(tensor_info_dxdy); |
| 148 | |
| 149 | k->configure(input, &_dx, &_dy, &_offsets, output, policy, border_undefined); |
| 150 | |
| 151 | // Allocate once the configure methods have been called |
| 152 | _offsets.allocator()->allocate(); |
| 153 | _dx.allocator()->allocate(); |
| 154 | _dy.allocator()->allocate(); |
| 155 | |
| 156 | // Pre-compute dx, dy and offsets for bilinear interpolation |
| 157 | precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size); |
| 158 | break; |
| 159 | } |
| 160 | case InterpolationPolicy::AREA: |
| 161 | { |
| 162 | k->configure(input, nullptr, nullptr, nullptr, output, policy, border_undefined); |
| 163 | break; |
| 164 | } |
| 165 | default: |
| 166 | ARM_COMPUTE_ERROR("Unsupported interpolation mode"); |
| 167 | } |
| 168 | |
| 169 | _kernel = std::move(k); |
| 170 | _border_handler.configure(input, _kernel->border_size(), border_mode, PixelValue(constant_border_value)); |
| 171 | } |