blob: d04bc07d3a8b1eaf501fa92ff425a66bf327f889 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Pablo Tellod8b03dd2018-08-07 11:23:54 +01002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/core/NEON/kernels/NEWarpKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35#include <cstddef>
36
37using namespace arm_compute;
38
39namespace
40{
41inline uint8_t nearest_interpolation(const uint8_t *in_ptr, int x, int y, size_t stride)
42{
43 return in_ptr[x + y * stride];
44}
45} // namespace
46
47INEWarpKernel::INEWarpKernel()
Pablo Tellod8b03dd2018-08-07 11:23:54 +010048 : _func(nullptr), _input(nullptr), _output(nullptr), _constant_border_value(0), _matrix()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049{
50}
51
Isabella Gottardif9bae2e2017-07-28 17:24:08 +010052BorderSize INEWarpKernel::border_size() const
53{
54 return BorderSize(1);
55}
56
Moritz Pflanzerc186b572017-09-07 09:48:04 +010057void INEWarpKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010059 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
61 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
62 ARM_COMPUTE_ERROR_ON(_func == nullptr);
63
64 (this->*_func)(window);
65}
66
Pablo Tellod8b03dd2018-08-07 11:23:54 +010067void INEWarpKernel::configure(const ITensor *input, ITensor *output, const std::array<float, 9> &matrix, BorderMode border_mode, uint8_t constant_border_value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068{
69 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
70 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
72 _matrix = matrix;
73 _constant_border_value = constant_border_value;
74
75 switch(border_mode)
76 {
77 case BorderMode::UNDEFINED:
78 _func = &INEWarpKernel::warp_undefined;
79 break;
80 case BorderMode::CONSTANT:
81 _func = &INEWarpKernel::warp_constant;
82 break;
83 case BorderMode::REPLICATE:
84 _func = &INEWarpKernel::warp_replicate;
85 break;
86 default:
87 ARM_COMPUTE_ERROR("Border mode not supported");
88 break;
89 }
90
91 _input = input;
92 _output = output;
93
94 // Configure kernel window
95 Window win = calculate_max_window(*output->info(), Steps(1U));
96
97 const ValidRegion &input_valid_region = input->info()->valid_region();
98
99 // Reads can occur within the valid region of the input
100 AccessWindowStatic input_access(input->info(),
Isabella Gottardif9bae2e2017-07-28 17:24:08 +0100101 input_valid_region.anchor[0] - border_size().left, input_valid_region.anchor[1] - border_size().top,
102 input_valid_region.anchor[0] + input_valid_region.shape[0] + border_size().right,
103 input_valid_region.anchor[1] + input_valid_region.shape[1] + border_size().bottom);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 AccessWindowHorizontal output_access(output->info(), 0, 1);
105
106 update_window_and_padding(win, input_access, output_access);
107
108 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
109
110 INEKernel::configure(win);
111}
112
113template <InterpolationPolicy interpolation>
114void NEWarpAffineKernel<interpolation>::warp_undefined(const Window &window)
115{
116 // Don't increment in X and Y direction for the input tensor
117 // A pointer to the start of this plane is needed as base for the precomputed offsets
118 Window win_in(window);
119 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
120 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
121
122 Iterator in(_input, win_in);
123 Iterator out(_output, window);
124
125 const int min_x = _input->info()->valid_region().anchor[0];
126 const int max_x = min_x + _input->info()->valid_region().shape[0];
127 const int min_y = _input->info()->valid_region().anchor[1];
128 const int max_y = min_y + _input->info()->valid_region().shape[1];
129 const size_t stride = _input->info()->strides_in_bytes()[1];
130
131 // x0 = M01 * x + M01 * y + M02
132 // y0 = M11 * x + M11 * y + M12
133 const float M00 = _matrix[0];
134 const float M10 = _matrix[1];
135 const float M01 = _matrix[0 + 1 * 2];
136 const float M11 = _matrix[1 + 1 * 2];
137 const float M02 = _matrix[0 + 2 * 2];
138 const float M12 = _matrix[1 + 2 * 2];
139
140 // "M00 * x" and "M10 * x", when x = window.x.start
141 const float start_x0 = M00 * window.x().start();
142 const float start_y0 = M10 * window.x().start();
143
144 // Current row
Isabella Gottardi83be7452017-08-29 13:47:03 +0100145 int y_cur = window.y().start();
146 int z_cur = window.z().start();
147 int d3_cur = window[3].start();
148 int d4_cur = window[4].start();
149 int d5_cur = window[5].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
151 // const_x0 and const_y0 are the constant parts of x0 and y0 during the row processing
152 float const_x0 = M01 * y_cur + M02;
153 float const_y0 = M11 * y_cur + M12;
154
155 // Affine warp coordinates
156 float x0 = start_x0 + const_x0;
157 float y0 = start_y0 + const_y0;
158
159 execute_window_loop(window, [&](const Coordinates & id)
160 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100161 // Check if we are processing a new row. If so, update the current processed row (y_cur), x0, y0 and z0
162 if((y_cur != id.y()) || (z_cur != id.z()) || (d3_cur != id[3]) || (d4_cur != id[4]) || (d5_cur != id[5]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100164 y_cur = id.y();
165 z_cur = id.z();
166 d3_cur = id[3];
167 d4_cur = id[4];
168 d5_cur = id[5];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169
170 const_x0 = M01 * y_cur + M02;
171 const_y0 = M11 * y_cur + M12;
172
173 x0 = start_x0 + const_x0;
174 y0 = start_y0 + const_y0;
175 }
176
177 // Only write to output if x0 and y0 are within the valid region.
178 // Otherwise the read value would be undefined.
179 if((min_y <= y0) && (y0 < max_y) && (min_x <= x0) && (x0 < max_x))
180 {
181 switch(interpolation)
182 {
183 case InterpolationPolicy::NEAREST_NEIGHBOR:
184 *out.ptr() = nearest_interpolation(in.ptr(), x0, y0, stride);
185 break;
186 case InterpolationPolicy::BILINEAR:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100187 *out.ptr() = pixel_bilinear_c1(in.ptr(), stride, x0, y0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 break;
189 default:
190 ARM_COMPUTE_ERROR("Interpolation not supported");
191 }
192 }
193
194 x0 += M00;
195 y0 += M10;
196 },
197 in, out);
198}
199
200template <InterpolationPolicy interpolation>
201void NEWarpAffineKernel<interpolation>::warp_constant(const Window &window)
202{
203 // Don't increment in X and Y direction for the input tensor
204 // A pointer to the start of this plane is needed as base for the precomputed offsets
205 Window win_in(window);
206 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
207 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
208
209 Iterator in(_input, win_in);
210 Iterator out(_output, window);
211
212 const int min_x = _input->info()->valid_region().anchor[0];
213 const int max_x = min_x + _input->info()->valid_region().shape[0];
214 const int min_y = _input->info()->valid_region().anchor[1];
215 const int max_y = min_y + _input->info()->valid_region().shape[1];
216 const size_t stride = _input->info()->strides_in_bytes()[1];
217
218 // x0 = M01 * x + M01 * y + M02
219 // y0 = M11 * x + M11 * y + M12
220 const float M00 = _matrix[0];
221 const float M10 = _matrix[1];
222 const float M01 = _matrix[0 + 1 * 2];
223 const float M11 = _matrix[1 + 1 * 2];
224 const float M02 = _matrix[0 + 2 * 2];
225 const float M12 = _matrix[1 + 2 * 2];
226
227 // "M00 * x" and "M10 * x", when x = window.x.start
228 const float start_x0 = M00 * window.x().start();
229 const float start_y0 = M10 * window.x().start();
230
231 // Current row
Isabella Gottardi83be7452017-08-29 13:47:03 +0100232 int y_cur = window.y().start();
233 int z_cur = window.z().start();
234 int d3_cur = window[3].start();
235 int d4_cur = window[4].start();
236 int d5_cur = window[5].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237
238 // const_x0 and const_y0 are the constant parts of x0 and y0 during the row processing
239 float const_x0 = M01 * y_cur + M02;
240 float const_y0 = M11 * y_cur + M12;
241
242 // Affine warp coordinates
243 float x0 = start_x0 + const_x0;
244 float y0 = start_y0 + const_y0;
245
246 execute_window_loop(window, [&](const Coordinates & id)
247 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100248 // Check if we are processing a new row. If so, update the current processed row (y_cur), x0, y0 and z0
249 if((y_cur != id.y()) || (z_cur != id.z()) || (d3_cur != id[3]) || (d4_cur != id[4]) || (d5_cur != id[5]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100250 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100251 y_cur = id.y();
252 z_cur = id.z();
253 d3_cur = id[3];
254 d4_cur = id[4];
255 d5_cur = id[5];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256
257 const_x0 = M01 * y_cur + M02;
258 const_y0 = M11 * y_cur + M12;
259
260 x0 = start_x0 + const_x0;
261 y0 = start_y0 + const_y0;
262 }
263
264 // Only use input values if x0 and y0 are within the valid region.
265 // Otherwise write the constant border value.
266 if((min_y <= y0) && (y0 < max_y) && (min_x <= x0) && (x0 < max_x))
267 {
268 switch(interpolation)
269 {
270 case InterpolationPolicy::NEAREST_NEIGHBOR:
271 *out.ptr() = nearest_interpolation(in.ptr(), x0, y0, stride);
272 break;
273 case InterpolationPolicy::BILINEAR:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100274 *out.ptr() = pixel_bilinear_c1(in.ptr(), stride, x0, y0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275 break;
276 default:
277 ARM_COMPUTE_ERROR("Interpolation not supported");
278 }
279 }
280 else
281 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100282 switch(interpolation)
283 {
284 case InterpolationPolicy::NEAREST_NEIGHBOR:
285 *out.ptr() = _constant_border_value;
286 break;
287 case InterpolationPolicy::BILINEAR:
288 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000289 const auto xi = utility::clamp<int>(std::floor(x0), min_x - 1, max_x);
290 const auto yi = utility::clamp<int>(std::floor(y0), min_y - 1, max_y);
291 const auto xi_1 = utility::clamp<int>(std::floor(x0 + 1), min_x - 1, max_x);
292 const auto yi_1 = utility::clamp<int>(std::floor(y0 + 1), min_y - 1, max_y);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100293
294 const float dx = x0 - std::floor(x0);
295 const float dy = y0 - std::floor(y0);
296 const float dx1 = 1.0f - dx;
297 const float dy1 = 1.0f - dy;
298
299 const float a00 = *(in.ptr() + xi + yi * stride);
300 const float a01 = *(in.ptr() + xi_1 + yi * stride);
301 const float a10 = *(in.ptr() + xi + yi_1 * stride);
302 const float a11 = *(in.ptr() + xi_1 + yi_1 * stride);
303
304 *out.ptr() = a00 * (dx1 * dy1) + a01 * (dx * dy1) + a10 * (dx1 * dy) + a11 * (dx * dy);
305 }
306 break;
307 default:
308 ARM_COMPUTE_ERROR("Interpolation not supported");
309 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310 }
311
312 x0 += M00;
313 y0 += M10;
314 },
315 in, out);
316}
317
318template <InterpolationPolicy interpolation>
319void NEWarpAffineKernel<interpolation>::warp_replicate(const Window &window)
320{
321 // Don't increment in X and Y direction for the input tensor
322 // A pointer to the start of this plane is needed as base for the precomputed offsets
323 Window win_in(window);
324 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
325 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
326
327 Iterator in(_input, win_in);
328 Iterator out(_output, window);
329
330 const int min_x = _input->info()->valid_region().anchor[0];
331 const int max_x = min_x + _input->info()->valid_region().shape[0];
332 const int min_y = _input->info()->valid_region().anchor[1];
333 const int max_y = min_y + _input->info()->valid_region().shape[1];
334 const size_t stride = _input->info()->strides_in_bytes()[1];
335
336 // Current row
Isabella Gottardi83be7452017-08-29 13:47:03 +0100337 int y_cur = window.y().start();
338 int z_cur = window.z().start();
339 int d3_cur = window[3].start();
340 int d4_cur = window[4].start();
341 int d5_cur = window[5].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342
343 const float M00 = _matrix[0];
344 const float M10 = _matrix[1];
345 const float M01 = _matrix[0 + 1 * 2];
346 const float M11 = _matrix[1 + 1 * 2];
347 const float M02 = _matrix[0 + 2 * 2];
348 const float M12 = _matrix[1 + 2 * 2];
349
350 // "M00 * x" and "M10 * x", when x = window.x.start
351 const float start_x0 = M00 * window.x().start();
352 const float start_y0 = M10 * window.x().start();
353
354 // const_x0 and const_y0 are the constant parts of x0 and y0 during the row processing
355 float const_x0 = M01 * y_cur + M02;
356 float const_y0 = M11 * y_cur + M12;
357
358 float x0 = start_x0 + const_x0;
359 float y0 = start_y0 + const_y0;
360
361 execute_window_loop(window, [&](const Coordinates & id)
362 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100363 // Check if we are processing a new row. If so, update the current processed row (y_cur), x0, y0 and z0
364 if((y_cur != id.y()) || (z_cur != id.z()) || (d3_cur != id[3]) || (d4_cur != id[4]) || (d5_cur != id[5]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100365 {
Isabella Gottardi83be7452017-08-29 13:47:03 +0100366 y_cur = id.y();
367 z_cur = id.z();
368 d3_cur = id[3];
369 d4_cur = id[4];
370 d5_cur = id[5];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100371
372 const_x0 = M01 * y_cur + M02;
373 const_y0 = M11 * y_cur + M12;
374
375 x0 = start_x0 + const_x0;
376 y0 = start_y0 + const_y0;
377 }
378
379 // Only load from (x0, y0) if the point is within the valid region.
380 // Otherwise load from the edge of the valid region.
381 if((min_y <= y0) && (y0 < max_y) && (min_x <= x0) && (x0 < max_x))
382 {
383 switch(interpolation)
384 {
385 case InterpolationPolicy::NEAREST_NEIGHBOR:
386 *out.ptr() = nearest_interpolation(in.ptr(), x0, y0, stride);
387 break;
388 case InterpolationPolicy::BILINEAR:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100389 *out.ptr() = pixel_bilinear_c1(in.ptr(), stride, x0, y0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100390 break;
391 default:
392 ARM_COMPUTE_ERROR("Interpolation not supported");
393 }
394 }
395 else
396 {
397 // Clamp coordinates
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000398 const auto xi = utility::clamp<int>(std::floor(x0), min_x, max_x - 1);
399 const auto yi = utility::clamp<int>(std::floor(y0), min_y, max_y - 1);
Isabella Gottardi83be7452017-08-29 13:47:03 +0100400 switch(interpolation)
401 {
402 case InterpolationPolicy::NEAREST_NEIGHBOR:
403 *out.ptr() = *(in.ptr() + xi + yi * stride);
404 break;
405 case InterpolationPolicy::BILINEAR:
406 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000407 const auto xi_1 = utility::clamp<int>(std::floor(x0 + 1), min_x, max_x - 1);
408 const auto yi_1 = utility::clamp<int>(std::floor(y0 + 1), min_y, max_y - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100409
Isabella Gottardi83be7452017-08-29 13:47:03 +0100410 const float dx = x0 - std::floor(x0);
411 const float dy = y0 - std::floor(y0);
412 const float dx1 = 1.0f - dx;
413 const float dy1 = 1.0f - dy;
414
415 const float a00 = *(in.ptr() + xi + yi * stride);
416 const float a01 = *(in.ptr() + xi_1 + yi * stride);
417 const float a10 = *(in.ptr() + xi + yi_1 * stride);
418 const float a11 = *(in.ptr() + xi_1 + yi_1 * stride);
419
420 *out.ptr() = a00 * (dx1 * dy1) + a01 * (dx * dy1) + a10 * (dx1 * dy) + a11 * (dx * dy);
421 }
422 break;
423 default:
424 ARM_COMPUTE_ERROR("Interpolation not supported");
425 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100426 }
427
428 x0 += M00;
429 y0 += M10;
430 },
431 in, out);
432}
433
434template <InterpolationPolicy interpolation>
435void NEWarpPerspectiveKernel<interpolation>::warp_undefined(const Window &window)
436{
437 // Don't increment in X and Y direction for the input tensor
438 // A pointer to the start of this plane is needed as base for the precomputed offsets
439 Window win_in(window);
440 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
441 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
442
443 Iterator in(_input, win_in);
444 Iterator out(_output, window);
445
446 const int min_x = _input->info()->valid_region().anchor[0];
447 const int max_x = min_x + _input->info()->valid_region().shape[0];
448 const int min_y = _input->info()->valid_region().anchor[1];
449 const int max_y = min_y + _input->info()->valid_region().shape[1];
450 const size_t stride = _input->info()->strides_in_bytes()[1];
451
452 // x0 = M00 * x + M01 * y + M02
453 // y0 = M10 * x + M11 * y + M12
454 // z0 = M20 * x + M21 * y + M22
455 // xn = x0 / z0
456 // yn = y0 / z0
457 const float M00 = _matrix[0];
458 const float M10 = _matrix[1];
459 const float M20 = _matrix[2];
460 const float M01 = _matrix[0 + 1 * 3];
461 const float M11 = _matrix[1 + 1 * 3];
462 const float M21 = _matrix[2 + 1 * 3];
463 const float M02 = _matrix[0 + 2 * 3];
464 const float M12 = _matrix[1 + 2 * 3];
465 const float M22 = _matrix[2 + 2 * 3];
466
467 // "M00 * x", "M10 * x" and "M20 * x", when x = window.x.start
468 const float start_x0 = M00 * window.x().start();
469 const float start_y0 = M10 * window.x().start();
470 const float start_z0 = M20 * window.x().start();
471
472 // Current row
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100473 int y_cur = window.y().start();
474 int z_cur = window.z().start();
475 int d3_cur = window[3].start();
476 int d4_cur = window[4].start();
477 int d5_cur = window[5].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100478
479 // const_x0, const_y0 and const_z0 are the constant parts of x0, y0 and z0 during the row processing
480 float const_x0 = M01 * y_cur + M02;
481 float const_y0 = M11 * y_cur + M12;
482 float const_z0 = M21 * y_cur + M22;
483
484 // Perspective warp coordinates
485 float x0 = start_x0 + const_x0;
486 float y0 = start_y0 + const_y0;
487 float z0 = start_z0 + const_z0;
488
489 execute_window_loop(window, [&](const Coordinates & id)
490 {
491 // Check if we are processing a new row. If so, update the current processed row (y_cur), x0, y0 and z0
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100492 if((y_cur != id.y()) || (z_cur != id.z()) || (d3_cur != id[3]) || (d4_cur != id[4]) || (d5_cur != id[5]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100493 {
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100494 y_cur = id.y();
495 z_cur = id.z();
496 d3_cur = id[3];
497 d4_cur = id[4];
498 d5_cur = id[5];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100499
500 const_x0 = M01 * y_cur + M02;
501 const_y0 = M11 * y_cur + M12;
502 const_z0 = M21 * y_cur + M22;
503
504 x0 = start_x0 + const_x0;
505 y0 = start_y0 + const_y0;
506 z0 = start_z0 + const_z0;
507 }
508
509 const float xn = x0 / z0;
510 const float yn = y0 / z0;
511
512 // Only write to output if xn and yn are within the valid region.
513 // Otherwise the read value would be undefined.
514 if((min_y <= yn) && (yn < max_y) && (min_x <= xn) && (xn < max_x))
515 {
516 switch(interpolation)
517 {
518 case InterpolationPolicy::NEAREST_NEIGHBOR:
519 *out.ptr() = nearest_interpolation(in.ptr(), xn, yn, stride);
520 break;
521 case InterpolationPolicy::BILINEAR:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100522 *out.ptr() = pixel_bilinear_c1(in.ptr(), stride, xn, yn);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100523 break;
524 default:
525 ARM_COMPUTE_ERROR("Interpolation not supported");
526 }
527 }
528
529 x0 += M00;
530 y0 += M10;
531 z0 += M20;
532 },
533 in, out);
534}
535
536template <InterpolationPolicy interpolation>
537void NEWarpPerspectiveKernel<interpolation>::warp_constant(const Window &window)
538{
539 // Don't increment in X and Y direction for the input tensor
540 // A pointer to the start of this plane is needed as base for the precomputed offsets
541 Window win_in(window);
542 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
543 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
544
545 Iterator in(_input, win_in);
546 Iterator out(_output, window);
547
548 const int min_x = _input->info()->valid_region().anchor[0];
549 const int max_x = min_x + _input->info()->valid_region().shape[0];
550 const int min_y = _input->info()->valid_region().anchor[1];
551 const int max_y = min_y + _input->info()->valid_region().shape[1];
552 const size_t stride = _input->info()->strides_in_bytes()[1];
553
554 // x0 = M00 * x + M01 * y + M02
555 // y0 = M10 * x + M11 * y + M12
556 // z0 = M20 * x + M21 * y + M22
557 // xn = x0 / z0
558 // yn = y0 / z0
559 const float M00 = _matrix[0];
560 const float M10 = _matrix[1];
561 const float M20 = _matrix[2];
562 const float M01 = _matrix[0 + 1 * 3];
563 const float M11 = _matrix[1 + 1 * 3];
564 const float M21 = _matrix[2 + 1 * 3];
565 const float M02 = _matrix[0 + 2 * 3];
566 const float M12 = _matrix[1 + 2 * 3];
567 const float M22 = _matrix[2 + 2 * 3];
568
569 // "M00 * x", "M10 * x" and "M20 * x", when x = window.x.start
570 const float start_x0 = M00 * window.x().start();
571 const float start_y0 = M10 * window.x().start();
572 const float start_z0 = M20 * window.x().start();
573
574 // Current row
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100575 int y_cur = window.y().start();
576 int z_cur = window.z().start();
577 int d3_cur = window[3].start();
578 int d4_cur = window[4].start();
579 int d5_cur = window[5].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100580
581 // const_x0, const_y0 and const_z0 are the constant parts of x0, y0 and z0 during the row processing
582 float const_x0 = M01 * y_cur + M02;
583 float const_y0 = M11 * y_cur + M12;
584 float const_z0 = M21 * y_cur + M22;
585
586 // Perspective warp coordinates
587 float x0 = start_x0 + const_x0;
588 float y0 = start_y0 + const_y0;
589 float z0 = start_z0 + const_z0;
590
591 execute_window_loop(window, [&](const Coordinates & id)
592 {
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100593 // Check if we are processing a new row. If so, update the current processed row (y_cur), x0, y0 and z0
594 if((y_cur != id.y()) || (z_cur != id.z()) || (d3_cur != id[3]) || (d4_cur != id[4]) || (d5_cur != id[5]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100595 {
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100596 y_cur = id.y();
597 z_cur = id.z();
598 d3_cur = id[3];
599 d4_cur = id[4];
600 d5_cur = id[5];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100601
602 const_x0 = M01 * y_cur + M02;
603 const_y0 = M11 * y_cur + M12;
604 const_z0 = M21 * y_cur + M22;
605
606 x0 = start_x0 + const_x0;
607 y0 = start_y0 + const_y0;
608 z0 = start_z0 + const_z0;
609 }
610
611 const float xn = x0 / z0;
612 const float yn = y0 / z0;
613
614 // Only use input values if xn and yn are within the valid region.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100615 if((min_y <= yn) && (yn < max_y) && (min_x <= xn) && (xn < max_x))
616 {
617 switch(interpolation)
618 {
619 case InterpolationPolicy::NEAREST_NEIGHBOR:
620 *out.ptr() = nearest_interpolation(in.ptr(), xn, yn, stride);
621 break;
622 case InterpolationPolicy::BILINEAR:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100623 *out.ptr() = pixel_bilinear_c1(in.ptr(), stride, xn, yn);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100624 break;
625 default:
626 ARM_COMPUTE_ERROR("Interpolation not supported");
627 }
628 }
629 else
630 {
Isabella Gottardi62031532017-07-04 11:21:28 +0100631 switch(interpolation)
632 {
633 case InterpolationPolicy::NEAREST_NEIGHBOR:
634 *out.ptr() = _constant_border_value;
635 break;
636 case InterpolationPolicy::BILINEAR:
637 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000638 const auto xi = utility::clamp<int>(std::floor(xn), min_x - 1, max_x);
639 const auto yi = utility::clamp<int>(std::floor(yn), min_y - 1, max_y);
640 const auto xi_1 = utility::clamp<int>(std::floor(xn + 1), min_x - 1, max_x);
641 const auto yi_1 = utility::clamp<int>(std::floor(yn + 1), min_y - 1, max_y);
Isabella Gottardi62031532017-07-04 11:21:28 +0100642
643 const float dx = xn - std::floor(xn);
644 const float dy = yn - std::floor(yn);
645 const float dx1 = 1.0f - dx;
646 const float dy1 = 1.0f - dy;
647
648 const float a00 = *(in.ptr() + xi + yi * stride);
649 const float a01 = *(in.ptr() + xi_1 + yi * stride);
650 const float a10 = *(in.ptr() + xi + yi_1 * stride);
651 const float a11 = *(in.ptr() + xi_1 + yi_1 * stride);
652
653 *out.ptr() = a00 * (dx1 * dy1) + a01 * (dx * dy1) + a10 * (dx1 * dy) + a11 * (dx * dy);
654 }
655 break;
656 default:
657 ARM_COMPUTE_ERROR("Interpolation not supported");
658 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100659 }
660
661 x0 += M00;
662 y0 += M10;
663 z0 += M20;
664 },
665 in, out);
666}
667
668template <InterpolationPolicy interpolation>
669void NEWarpPerspectiveKernel<interpolation>::warp_replicate(const Window &window)
670{
671 // Don't increment in X and Y direction for the input tensor
672 // A pointer to the start of this plane is needed as base for the precomputed offsets
673 Window win_in(window);
674 win_in.set(Window::DimX, Window::Dimension(0, 0, 0));
675 win_in.set(Window::DimY, Window::Dimension(0, 0, 0));
676
677 Iterator in(_input, win_in);
678 Iterator out(_output, window);
679
680 const int min_x = _input->info()->valid_region().anchor[0];
681 const int max_x = min_x + _input->info()->valid_region().shape[0];
682 const int min_y = _input->info()->valid_region().anchor[1];
683 const int max_y = min_y + _input->info()->valid_region().shape[1];
684 const size_t stride = _input->info()->strides_in_bytes()[1];
685
686 // Current row
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100687 int y_cur = window.y().start();
688 int z_cur = window.z().start();
689 int d3_cur = window[3].start();
690 int d4_cur = window[4].start();
691 int d5_cur = window[5].start();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100692
693 // x0 = M00 * x + M01 * y + M02
694 // y0 = M10 * x + M11 * y + M12
695 // z0 = M20 * x + M21 * y + M22
696 // xn = x0 / z0
697 // yn = y0 / z0
698 const float M00 = _matrix[0];
699 const float M10 = _matrix[1];
700 const float M20 = _matrix[2];
701 const float M01 = _matrix[0 + 1 * 3];
702 const float M11 = _matrix[1 + 1 * 3];
703 const float M21 = _matrix[2 + 1 * 3];
704 const float M02 = _matrix[0 + 2 * 3];
705 const float M12 = _matrix[1 + 2 * 3];
706 const float M22 = _matrix[2 + 2 * 3];
707
708 // "M00 * x", "M10 * x" and "M20 * x", when x = window.x.start
709 const float start_x0 = M00 * window.x().start();
710 const float start_y0 = M10 * window.x().start();
711 const float start_z0 = M20 * window.x().start();
712
713 // const_x0, const_y0 and const_z0 are the constant parts of x0, y0 and z0 during the row processing
714 float const_x0 = M01 * y_cur + M02;
715 float const_y0 = M11 * y_cur + M12;
716 float const_z0 = M21 * y_cur + M22;
717
718 // Perspective warp coordinates
719 float x0 = start_x0 + const_x0;
720 float y0 = start_y0 + const_y0;
721 float z0 = start_z0 + const_z0;
722
723 execute_window_loop(window, [&](const Coordinates & id)
724 {
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100725 // Check if we are processing a new row. If so, update the current processed row (y_cur), x0, y0 and z0
726 if((y_cur != id.y()) || (z_cur != id.z()) || (d3_cur != id[3]) || (d4_cur != id[4]) || (d5_cur != id[5]))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100727 {
Isabella Gottardi40ff03b2017-08-10 16:44:42 +0100728 y_cur = id.y();
729 z_cur = id.z();
730 d3_cur = id[3];
731 d4_cur = id[4];
732 d5_cur = id[5];
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100733
734 const_x0 = M01 * y_cur + M02;
735 const_y0 = M11 * y_cur + M12;
736 const_z0 = M21 * y_cur + M22;
737
738 x0 = start_x0 + const_x0;
739 y0 = start_y0 + const_y0;
740 z0 = start_z0 + const_z0;
741 }
742
743 const float xn = x0 / z0;
744 const float yn = y0 / z0;
745
746 // Only load from (x0, y0) if the point is within the valid region.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100747 if((min_y <= yn) && (yn < max_y) && (min_x <= xn) && (xn < max_x))
748 {
749 switch(interpolation)
750 {
751 case InterpolationPolicy::NEAREST_NEIGHBOR:
752 *out.ptr() = nearest_interpolation(in.ptr(), xn, yn, stride);
753 break;
754 case InterpolationPolicy::BILINEAR:
Georgios Pinitas583137c2017-08-31 18:12:42 +0100755 *out.ptr() = pixel_bilinear_c1(in.ptr(), stride, xn, yn);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100756 break;
757 default:
758 ARM_COMPUTE_ERROR("Interpolation not supported");
759 }
760 }
761 else
762 {
763 // Clamp coordinates
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000764 const auto xi = utility::clamp<int>(std::floor(xn), min_x, max_x - 1);
765 const auto yi = utility::clamp<int>(std::floor(yn), min_y, max_y - 1);
Isabella Gottardi62031532017-07-04 11:21:28 +0100766 switch(interpolation)
767 {
768 case InterpolationPolicy::NEAREST_NEIGHBOR:
769 *out.ptr() = *(in.ptr() + xi + yi * stride);
770 break;
771 case InterpolationPolicy::BILINEAR:
772 {
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000773 const auto xi_1 = utility::clamp<int>(std::floor(xn + 1), min_x, max_x - 1);
774 const auto yi_1 = utility::clamp<int>(std::floor(yn + 1), min_y, max_y - 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100775
Isabella Gottardi62031532017-07-04 11:21:28 +0100776 const float dx = xn - std::floor(xn);
777 const float dy = yn - std::floor(yn);
778 const float dx1 = 1.0f - dx;
779 const float dy1 = 1.0f - dy;
780
781 const float a00 = *(in.ptr() + xi + yi * stride);
782 const float a01 = *(in.ptr() + xi_1 + yi * stride);
783 const float a10 = *(in.ptr() + xi + yi_1 * stride);
784 const float a11 = *(in.ptr() + xi_1 + yi_1 * stride);
785
786 *out.ptr() = a00 * (dx1 * dy1) + a01 * (dx * dy1) + a10 * (dx1 * dy) + a11 * (dx * dy);
787 }
788 break;
789 default:
790 ARM_COMPUTE_ERROR("Interpolation not supported");
791 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100792 }
793
794 x0 += M00;
795 y0 += M10;
796 z0 += M20;
797 },
798 in, out);
799}
800
801template class arm_compute::NEWarpAffineKernel<InterpolationPolicy::NEAREST_NEIGHBOR>;
802template class arm_compute::NEWarpAffineKernel<InterpolationPolicy::BILINEAR>;
803template class arm_compute::NEWarpPerspectiveKernel<InterpolationPolicy::NEAREST_NEIGHBOR>;
804template class arm_compute::NEWarpPerspectiveKernel<InterpolationPolicy::BILINEAR>;