Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 1 | /* |
| 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 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 "WarpAffine.h" |
| 25 | |
| 26 | #include "Utils.h" |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | namespace reference |
| 35 | { |
| 36 | bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode) |
| 37 | { |
| 38 | if(border_mode != BorderMode::UNDEFINED) |
| 39 | { |
| 40 | return true; |
| 41 | } |
| 42 | if((0 <= yn + 1) && (yn + 1 < height) && (0 <= xn + 1) && (xn + 1 < width)) |
| 43 | { |
| 44 | return true; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | template <typename T> |
| 50 | SimpleTensor<T> warp_affine(const SimpleTensor<T> &src, SimpleTensor<T> &valid_mask, const float *matrix, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value) |
| 51 | { |
| 52 | SimpleTensor<T> dst(src.shape(), src.data_type()); |
| 53 | |
| 54 | // x0 = M00 * x + M01 * y + M02 |
| 55 | // y0 = M10 * x + M11 * y + M12 |
| 56 | const float M00 = matrix[0]; |
| 57 | const float M10 = matrix[1]; |
| 58 | const float M01 = matrix[0 + 1 * 2]; |
| 59 | const float M11 = matrix[1 + 1 * 2]; |
| 60 | const float M02 = matrix[0 + 2 * 2]; |
| 61 | const float M12 = matrix[1 + 2 * 2]; |
| 62 | |
| 63 | const int width = src.shape().x(); |
| 64 | const int height = src.shape().y(); |
| 65 | |
| 66 | for(int element_idx = 0; element_idx < src.num_elements(); ++element_idx) |
| 67 | { |
| 68 | valid_mask[element_idx] = 1; |
| 69 | Coordinates id = index2coord(src.shape(), element_idx); |
| 70 | int idx = id.x(); |
| 71 | int idy = id.y(); |
| 72 | |
| 73 | float x0 = M00 * idx + M01 * idy + M02; |
| 74 | float y0 = M10 * idx + M11 * idy + M12; |
| 75 | |
| 76 | id.set(0, static_cast<int>(std::floor(x0))); |
| 77 | id.set(1, static_cast<int>(std::floor(y0))); |
| 78 | if((0 <= y0) && (y0 < height) && (0 <= x0) && (x0 < width)) |
| 79 | { |
| 80 | switch(policy) |
| 81 | { |
| 82 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 83 | dst[element_idx] = tensor_elem_at(src, id, border_mode, constant_border_value); |
| 84 | break; |
| 85 | case InterpolationPolicy::BILINEAR: |
| 86 | (valid_bilinear_policy(x0, y0, width, height, border_mode)) ? dst[element_idx] = bilinear_policy(src, id, x0, y0, border_mode, constant_border_value) : |
| 87 | valid_mask[element_idx] = 0; |
| 88 | break; |
| 89 | case InterpolationPolicy::AREA: |
| 90 | default: |
| 91 | ARM_COMPUTE_ERROR("Interpolation not supported"); |
| 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | if(border_mode == BorderMode::UNDEFINED) |
| 97 | { |
| 98 | valid_mask[element_idx] = 0; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | switch(policy) |
| 103 | { |
| 104 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 105 | if(border_mode == BorderMode::CONSTANT) |
| 106 | { |
| 107 | dst[element_idx] = constant_border_value; |
| 108 | } |
| 109 | else if(border_mode == BorderMode::REPLICATE) |
| 110 | { |
| 111 | id.set(0, std::max(0, std::min(static_cast<int>(x0), width - 1))); |
| 112 | id.set(1, std::max(0, std::min(static_cast<int>(y0), height - 1))); |
| 113 | dst[element_idx] = src[coord2index(src.shape(), id)]; |
| 114 | } |
| 115 | break; |
| 116 | case InterpolationPolicy::BILINEAR: |
| 117 | dst[element_idx] = bilinear_policy(src, id, x0, y0, border_mode, constant_border_value); |
| 118 | break; |
| 119 | case InterpolationPolicy::AREA: |
| 120 | default: |
| 121 | ARM_COMPUTE_ERROR("Interpolation not supported"); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return dst; |
| 128 | } |
| 129 | |
| 130 | template SimpleTensor<uint8_t> warp_affine(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &valid_mask, const float *matrix, InterpolationPolicy policy, BorderMode border_mode, |
| 131 | uint8_t constant_border_value); |
| 132 | } // namespace reference |
| 133 | } // namespace validation |
| 134 | } // namespace test |
| 135 | } // namespace arm_compute |