Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +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 |
| 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 "Remap.h" |
| 25 | |
| 26 | #include "Utils.h" |
| 27 | #include "tests/validation/Helpers.h" |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <array> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | namespace reference |
| 39 | { |
| 40 | template <typename T> |
| 41 | SimpleTensor<T> remap(const SimpleTensor<T> &in, SimpleTensor<float> &map_x, SimpleTensor<float> &map_y, SimpleTensor<T> &valid_mask, InterpolationPolicy policy, BorderMode border_mode, |
| 42 | T constant_border_value) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON_MSG(border_mode == BorderMode::REPLICATE, "BorderMode not supported"); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 45 | SimpleTensor<T> out(in.shape(), in.data_type()); |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_ON(out.num_elements() != map_x.num_elements()); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 47 | const int width = in.shape().x(); |
| 48 | const int height = in.shape().y(); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 49 | for(int idx = 0; idx < out.num_elements(); idx++) |
| 50 | { |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 51 | const Coordinates id_out = index2coord(out.shape(), idx); |
| 52 | valid_mask[idx] = 1; |
| 53 | Coordinates src_idx = id_out; // need to setup all coordinates and not just xy |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 54 | src_idx.set(0, static_cast<int>(std::floor(map_x[idx]))); |
| 55 | src_idx.set(1, static_cast<int>(std::floor(map_y[idx]))); |
| 56 | if((0 <= map_y[idx]) && (map_y[idx] < height) && (0 <= map_x[idx]) && (map_x[idx] < width)) |
| 57 | { |
| 58 | switch(policy) |
| 59 | { |
| 60 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 61 | { |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 62 | out[idx] = tensor_elem_at(in, src_idx, border_mode, constant_border_value); |
| 63 | break; |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 64 | } |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 65 | case InterpolationPolicy::BILINEAR: |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 66 | { |
| 67 | (valid_bilinear_policy(map_x[idx], map_y[idx], width, height, border_mode)) ? |
| 68 | out[idx] = bilinear_policy(in, src_idx, map_x[idx], map_y[idx], border_mode, constant_border_value) : |
| 69 | valid_mask[idx] = 0; |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 70 | break; |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 71 | } |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 72 | case InterpolationPolicy::AREA: |
| 73 | default: |
| 74 | ARM_COMPUTE_ERROR("Interpolation not supported"); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | if(border_mode == BorderMode::UNDEFINED) |
| 81 | { |
| 82 | valid_mask[idx] = 0; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | switch(policy) |
| 87 | { |
| 88 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 89 | out[idx] = constant_border_value; |
| 90 | break; |
| 91 | case InterpolationPolicy::BILINEAR: |
| 92 | out[idx] = bilinear_policy(in, src_idx, map_x[idx], map_y[idx], border_mode, constant_border_value); |
| 93 | break; |
| 94 | case InterpolationPolicy::AREA: |
| 95 | default: |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return out; |
| 103 | } |
| 104 | |
| 105 | template SimpleTensor<uint8_t> remap(const SimpleTensor<uint8_t> &src, SimpleTensor<float> &map_x, SimpleTensor<float> &map_y, SimpleTensor<uint8_t> &valid_mask, InterpolationPolicy policy, |
| 106 | BorderMode border_mode, |
| 107 | uint8_t constant_border_value); |
| 108 | } // namespace reference |
| 109 | } // namespace validation |
| 110 | } // namespace test |
| 111 | } // namespace arm_compute |