1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #ifndef ARM_COMPUTE_TEST_VALIDATION_UTILS_H
25*c217d954SCole Faust #define ARM_COMPUTE_TEST_VALIDATION_UTILS_H
26*c217d954SCole Faust
27*c217d954SCole Faust #include "arm_compute/core/Types.h"
28*c217d954SCole Faust #include "tests/Globals.h"
29*c217d954SCole Faust #include "tests/Types.h"
30*c217d954SCole Faust
31*c217d954SCole Faust #include <array>
32*c217d954SCole Faust #include <random>
33*c217d954SCole Faust #include <type_traits>
34*c217d954SCole Faust #include <utility>
35*c217d954SCole Faust #include <vector>
36*c217d954SCole Faust
37*c217d954SCole Faust namespace arm_compute
38*c217d954SCole Faust {
39*c217d954SCole Faust namespace test
40*c217d954SCole Faust {
41*c217d954SCole Faust namespace validation
42*c217d954SCole Faust {
43*c217d954SCole Faust /** Checks if a pixel has valid coordinates
44*c217d954SCole Faust *
45*c217d954SCole Faust * @param x X coordinate
46*c217d954SCole Faust * @param y Y coordinate
47*c217d954SCole Faust * @param width Width of the image
48*c217d954SCole Faust * @param height Height of the image
49*c217d954SCole Faust * @param border_size Border size
50*c217d954SCole Faust *
51*c217d954SCole Faust * @return True if pixel is valid else false
52*c217d954SCole Faust */
is_valid_pixel_index(int x,int y,int width,int height,int border_size)53*c217d954SCole Faust inline bool is_valid_pixel_index(int x, int y, int width, int height, int border_size)
54*c217d954SCole Faust {
55*c217d954SCole Faust return ((x >= -border_size) && (y >= -border_size) && (x < (width + border_size)) && (y < height + border_size));
56*c217d954SCole Faust }
57*c217d954SCole Faust
58*c217d954SCole Faust #pragma GCC diagnostic push
59*c217d954SCole Faust #pragma GCC diagnostic ignored "-Wstrict-overflow"
60*c217d954SCole Faust
61*c217d954SCole Faust // Return a tensor element at a specified coordinate with different border modes
62*c217d954SCole Faust template <typename T>
tensor_elem_at(const SimpleTensor<T> & src,Coordinates coord,BorderMode border_mode,T constant_border_value)63*c217d954SCole Faust T tensor_elem_at(const SimpleTensor<T> &src, Coordinates coord, BorderMode border_mode, T constant_border_value)
64*c217d954SCole Faust {
65*c217d954SCole Faust const int x = coord.x();
66*c217d954SCole Faust const int y = coord.y();
67*c217d954SCole Faust const int z = coord.z();
68*c217d954SCole Faust const int width = src.shape().x();
69*c217d954SCole Faust const int height = src.shape().y();
70*c217d954SCole Faust const int depth = src.shape().z();
71*c217d954SCole Faust
72*c217d954SCole Faust // If coordinates beyond range of tensor's width or height
73*c217d954SCole Faust if(x < 0 || y < 0 || z < 0 || x >= width || y >= height || z >= depth)
74*c217d954SCole Faust {
75*c217d954SCole Faust if(border_mode == BorderMode::REPLICATE)
76*c217d954SCole Faust {
77*c217d954SCole Faust coord.set(0, std::max(0, std::min(x, width - 1)));
78*c217d954SCole Faust coord.set(1, std::max(0, std::min(y, height - 1)));
79*c217d954SCole Faust }
80*c217d954SCole Faust else
81*c217d954SCole Faust {
82*c217d954SCole Faust return constant_border_value;
83*c217d954SCole Faust }
84*c217d954SCole Faust }
85*c217d954SCole Faust
86*c217d954SCole Faust return src[coord2index(src.shape(), coord)];
87*c217d954SCole Faust }
88*c217d954SCole Faust
89*c217d954SCole Faust #pragma GCC diagnostic pop
90*c217d954SCole Faust
91*c217d954SCole Faust template <typename T>
92*c217d954SCole Faust T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value);
93*c217d954SCole Faust
94*c217d954SCole Faust /* Apply 2D spatial filter on a single element of @p in at coordinates @p coord
95*c217d954SCole Faust *
96*c217d954SCole Faust * - filter sizes have to be odd number
97*c217d954SCole Faust * - Row major order of filter assumed
98*c217d954SCole Faust * - TO_ZERO rounding policy assumed
99*c217d954SCole Faust * - SATURATE convert policy assumed
100*c217d954SCole Faust */
101*c217d954SCole Faust template <typename T, typename U, typename V>
102*c217d954SCole Faust void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<T> &src, SimpleTensor<U> &dst, const TensorShape &filter_shape, const V *filter_itr, double scale, BorderMode border_mode,
103*c217d954SCole Faust T constant_border_value = T(0))
104*c217d954SCole Faust {
105*c217d954SCole Faust double val = 0.;
106*c217d954SCole Faust const int x = coord.x();
107*c217d954SCole Faust const int y = coord.y();
108*c217d954SCole Faust for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j)
109*c217d954SCole Faust {
110*c217d954SCole Faust for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i)
111*c217d954SCole Faust {
112*c217d954SCole Faust coord.set(0, i);
113*c217d954SCole Faust coord.set(1, j);
114*c217d954SCole Faust val += static_cast<double>(*filter_itr) * tensor_elem_at(src, coord, border_mode, constant_border_value);
115*c217d954SCole Faust ++filter_itr;
116*c217d954SCole Faust }
117*c217d954SCole Faust }
118*c217d954SCole Faust coord.set(0, x);
119*c217d954SCole Faust coord.set(1, y);
120*c217d954SCole Faust dst[coord2index(src.shape(), coord)] = saturate_cast<U>(support::cpp11::trunc(val * scale));
121*c217d954SCole Faust }
122*c217d954SCole Faust
123*c217d954SCole Faust RawTensor transpose(const RawTensor &src, int chunk_width = 1);
124*c217d954SCole Faust
125*c217d954SCole Faust bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode);
126*c217d954SCole Faust } // namespace validation
127*c217d954SCole Faust } // namespace test
128*c217d954SCole Faust } // namespace arm_compute
129*c217d954SCole Faust #endif /* ARM_COMPUTE_TEST_VALIDATION_UTILS_H */
130