blob: 74489aaa9694f4b242b722f7b02faa0bce4d5a17 [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
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
25#include "arm_compute/core/Helpers.h"
26
27#include "Scale.h"
28#include "Utils.h"
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38template <typename T>
Georgios Pinitas583137c2017-08-31 18:12:42 +010039SimpleTensor<T> scale(const SimpleTensor<T> &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010040{
41 TensorShape shape_scaled(in.shape());
42 shape_scaled.set(0, in.shape()[0] * scale_x);
43 shape_scaled.set(1, in.shape()[1] * scale_y);
44 SimpleTensor<T> out(shape_scaled, in.data_type());
45
46 // Compute the ratio between source width/height and destination width/height
47 const auto wr = static_cast<float>(in.shape()[0]) / static_cast<float>(out.shape()[0]);
48 const auto hr = static_cast<float>(in.shape()[1]) / static_cast<float>(out.shape()[1]);
49
50 const auto width = static_cast<int>(in.shape().x());
51 const auto height = static_cast<int>(in.shape().y());
52
53 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
54 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
55 {
56 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
57 }
58
59 for(int element_idx = 0, count = 0; element_idx < out.num_elements(); ++element_idx, ++count)
60 {
61 Coordinates id = index2coord(out.shape(), element_idx);
62 int idx = id.x();
63 int idy = id.y();
64 float x_src = (idx + 0.5f) * wr - 0.5f;
65 float y_src = (idy + 0.5f) * hr - 0.5f;
66
67 switch(policy)
68 {
69 case InterpolationPolicy::NEAREST_NEIGHBOR:
70 {
71 //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords
72 x_src = (idx + 0.5f) * wr;
73 y_src = (idy + 0.5f) * hr;
74 id.set(0, x_src);
75 id.set(1, y_src);
76
77 // If coordinates in range of tensor's width or height
78 if(x_src >= -1 || y_src >= -1 || x_src <= width || y_src <= height)
79 {
80 out[element_idx] = tensor_elem_at(in, id, border_mode, constant_border_value);
81 }
82 else
83 {
84 if(border_mode == BorderMode::CONSTANT)
85 {
86 out[element_idx] = constant_border_value;
87 }
88 else if(border_mode == BorderMode::REPLICATE)
89 {
90 id.set(0, clamp(static_cast<int>(x_src), 0, width - 1));
91 id.set(1, clamp(static_cast<int>(y_src), 0, height - 1));
92 out[element_idx] = in[coord2index(in.shape(), id)];
93 }
94 }
95 break;
96 }
97 case InterpolationPolicy::BILINEAR:
98 {
99 id.set(0, std::floor(x_src));
100 id.set(1, std::floor(y_src));
101 if(x_src >= -1 || y_src >= -1 || x_src <= width || y_src <= height)
102 {
103 out[element_idx] = bilinear_policy(in, id, x_src, y_src, border_mode, constant_border_value);
104 }
105 else
106 {
107 if(border_mode == BorderMode::CONSTANT)
108 {
109 out[element_idx] = constant_border_value;
110 }
111 else if(border_mode == BorderMode::REPLICATE)
112 {
113 id.set(0, clamp(static_cast<int>(x_src), 0, width - 1));
114 id.set(1, clamp(static_cast<int>(y_src), 0, height - 1));
115 out[element_idx] = in[coord2index(in.shape(), id)];
116 }
117 }
118 break;
119 }
120 case InterpolationPolicy::AREA:
121 {
122 int x_from = std::floor(idx * wr - 0.5f - x_src);
123 int y_from = std::floor(idy * hr - 0.5f - y_src);
124 int x_to = std::ceil((idx + 1) * wr - 0.5f - x_src);
125 int y_to = std::ceil((idy + 1) * hr - 0.5f - y_src);
126 const int xi = std::floor(x_src);
127 const int yi = std::floor(y_src);
128
129 // Clamp position to borders
130 x_src = std::max(-1.f, std::min(x_src, static_cast<float>(width)));
131 y_src = std::max(-1.f, std::min(y_src, static_cast<float>(height)));
132
133 // Clamp bounding box offsets to borders
134 x_from = ((x_src + x_from) < -1) ? -1 : x_from;
135 y_from = ((y_src + y_from) < -1) ? -1 : y_from;
136 x_to = ((x_src + x_to) > width) ? (width - x_src) : x_to;
137 y_to = ((y_src + y_to) > height) ? (height - y_src) : y_to;
138 ARM_COMPUTE_ERROR_ON((x_to - x_from + 1) == 0 || (y_to - y_from + 1) == 0);
139
140 float sum = 0;
141 for(int j = yi + y_from, je = yi + y_to; j <= je; ++j)
142 {
143 for(int i = xi + x_from, ie = xi + x_to; i <= ie; ++i)
144 {
145 id.set(0, static_cast<int>(i));
146 id.set(1, static_cast<int>(j));
147 sum += tensor_elem_at(in, id, border_mode, constant_border_value);
148 }
149 }
150 out[element_idx] = sum / ((x_to - x_from + 1) * (y_to - y_from + 1));
151
152 break;
153 }
154 default:
155 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
156 }
157 }
158
159 return out;
160}
161
162template SimpleTensor<uint8_t> scale(const SimpleTensor<uint8_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100163template SimpleTensor<int16_t> scale(const SimpleTensor<int16_t> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, int16_t constant_border_value);
164template SimpleTensor<half> scale(const SimpleTensor<half> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, half constant_border_value);
165template SimpleTensor<float> scale(const SimpleTensor<float> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, float constant_border_value);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100166} // namespace reference
167} // namespace validation
168} // namespace test
steniu01f81652d2017-09-11 15:29:12 +0100169} // namespace arm_compute