blob: 727325f675e5eeccb7685a30dcc4bca6ef395a14 [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"
Daniil Efremov02bf80d2017-11-22 00:26:51 +070029#include "support/ToolchainSupport.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010030
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39template <typename T>
Daniil Efremov02bf80d2017-11-22 00:26:51 +070040SimpleTensor<T> scale(const SimpleTensor<T> &in, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value,
41 SamplingPolicy sampling_policy, bool ceil_policy_scale)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010042{
Gian Marco37908d92017-11-07 14:38:22 +000043 // Add 1 if ceil_policy_scale is true
44 const size_t round_value = ceil_policy_scale ? 1U : 0U;
45 TensorShape shape_scaled(in.shape());
46 shape_scaled.set(0, (in.shape()[0] + round_value) * scale_x);
47 shape_scaled.set(1, (in.shape()[1] + round_value) * scale_y);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010048 SimpleTensor<T> out(shape_scaled, in.data_type());
49
50 // Compute the ratio between source width/height and destination width/height
51 const auto wr = static_cast<float>(in.shape()[0]) / static_cast<float>(out.shape()[0]);
52 const auto hr = static_cast<float>(in.shape()[1]) / static_cast<float>(out.shape()[1]);
53
54 const auto width = static_cast<int>(in.shape().x());
55 const auto height = static_cast<int>(in.shape().y());
56
Daniil Efremov7a49c792017-11-14 21:25:34 +070057 // Determine border size
58 const int border_size = (border_mode == BorderMode::UNDEFINED) ? 0 : 1;
59
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010060 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
61 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
62 {
63 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
64 }
65
66 for(int element_idx = 0, count = 0; element_idx < out.num_elements(); ++element_idx, ++count)
67 {
68 Coordinates id = index2coord(out.shape(), element_idx);
69 int idx = id.x();
70 int idy = id.y();
Daniil Efremov02bf80d2017-11-22 00:26:51 +070071 float x_src = 0;
72 float y_src = 0;
73
74 switch(sampling_policy)
75 {
76 case SamplingPolicy::TOP_LEFT:
77 x_src = idx * wr;
78 y_src = idy * hr;
79 break;
80 case SamplingPolicy::CENTER:
81 x_src = (idx + 0.5f) * wr - 0.5f;
82 y_src = (idy + 0.5f) * hr - 0.5f;
83 break;
84 default:
85 ARM_COMPUTE_ERROR("Unsupported sampling policy.");
86 break;
87 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010088
89 switch(policy)
90 {
91 case InterpolationPolicy::NEAREST_NEIGHBOR:
92 {
93 //Calculate the source coords without -0.5f is equivalent to round the x_scr/y_src coords
94 x_src = (idx + 0.5f) * wr;
95 y_src = (idy + 0.5f) * hr;
96 id.set(0, x_src);
97 id.set(1, y_src);
98
99 // If coordinates in range of tensor's width or height
Daniil Efremov7a49c792017-11-14 21:25:34 +0700100 if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100101 {
102 out[element_idx] = tensor_elem_at(in, id, border_mode, constant_border_value);
103 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100104 break;
105 }
106 case InterpolationPolicy::BILINEAR:
107 {
108 id.set(0, std::floor(x_src));
109 id.set(1, std::floor(y_src));
Daniil Efremov7a49c792017-11-14 21:25:34 +0700110 if(is_valid_pixel_index(x_src, y_src, width, height, border_size))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100111 {
112 out[element_idx] = bilinear_policy(in, id, x_src, y_src, border_mode, constant_border_value);
113 }
114 else
115 {
116 if(border_mode == BorderMode::CONSTANT)
117 {
118 out[element_idx] = constant_border_value;
119 }
120 else if(border_mode == BorderMode::REPLICATE)
121 {
122 id.set(0, clamp(static_cast<int>(x_src), 0, width - 1));
123 id.set(1, clamp(static_cast<int>(y_src), 0, height - 1));
124 out[element_idx] = in[coord2index(in.shape(), id)];
125 }
126 }
127 break;
128 }
129 case InterpolationPolicy::AREA:
130 {
131 int x_from = std::floor(idx * wr - 0.5f - x_src);
132 int y_from = std::floor(idy * hr - 0.5f - y_src);
133 int x_to = std::ceil((idx + 1) * wr - 0.5f - x_src);
134 int y_to = std::ceil((idy + 1) * hr - 0.5f - y_src);
135 const int xi = std::floor(x_src);
136 const int yi = std::floor(y_src);
137
138 // Clamp position to borders
Daniil Efremov7a49c792017-11-14 21:25:34 +0700139 x_src = std::max(-static_cast<float>(border_size), std::min(x_src, static_cast<float>(width - 1 + border_size)));
140 y_src = std::max(-static_cast<float>(border_size), std::min(y_src, static_cast<float>(height - 1 + border_size)));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100141
142 // Clamp bounding box offsets to borders
Daniil Efremov7a49c792017-11-14 21:25:34 +0700143 x_from = ((x_src + x_from) < -border_size) ? -border_size : x_from;
144 y_from = ((y_src + y_from) < -border_size) ? -border_size : y_from;
145 x_to = ((x_src + x_to) >= (width + border_size)) ? (width - 1 + border_size) : x_to;
146 y_to = ((y_src + y_to) >= (height + border_size)) ? (height - 1 + border_size) : y_to;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100147 ARM_COMPUTE_ERROR_ON((x_to - x_from + 1) == 0 || (y_to - y_from + 1) == 0);
148
149 float sum = 0;
150 for(int j = yi + y_from, je = yi + y_to; j <= je; ++j)
151 {
152 for(int i = xi + x_from, ie = xi + x_to; i <= ie; ++i)
153 {
154 id.set(0, static_cast<int>(i));
155 id.set(1, static_cast<int>(j));
156 sum += tensor_elem_at(in, id, border_mode, constant_border_value);
157 }
158 }
159 out[element_idx] = sum / ((x_to - x_from + 1) * (y_to - y_from + 1));
160
161 break;
162 }
163 default:
164 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
165 }
166 }
167
168 return out;
169}
170
Gian Marco37908d92017-11-07 14:38:22 +0000171template 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,
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700172 SamplingPolicy sampling_policy, bool ceil_policy_scale);
Gian Marco37908d92017-11-07 14:38:22 +0000173template 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,
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700174 SamplingPolicy sampling_policy, bool ceil_policy_scale);
175template SimpleTensor<half> scale(const SimpleTensor<half> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, half constant_border_value,
176 SamplingPolicy sampling_policy, bool ceil_policy_scale);
Gian Marco37908d92017-11-07 14:38:22 +0000177template SimpleTensor<float> scale(const SimpleTensor<float> &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, float constant_border_value,
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700178 SamplingPolicy sampling_policy, bool ceil_policy_scale);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100179} // namespace reference
180} // namespace validation
181} // namespace test
steniu01f81652d2017-09-11 15:29:12 +0100182} // namespace arm_compute