blob: 63a53cc4f2e0404b3a809e4d125faec1c795986f [file] [log] [blame]
Adnan AlSinan7075fe22021-07-05 13:12:52 +01001/*
2 * Copyright (c) 2016-2021 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 "helpers.h"
25#include "warp_helpers.h"
26
27/** Transforms four 2D coordinates. This is used to map the output coordinates to the input coordinates.
28 *
29 * @param[in] coord 2D coordinates to transform.
30 * @param[in] scale input/output scale ratio
31 *
32 * @return a float8 containing 4 2D transformed values in the input image.
33 */
34inline const float8 transform_nearest(const float2 coord, const float2 scale)
35{
36#ifdef SAMPLING_POLICY_TOP_LEFT
37 const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0);
38 const float4 new_x = in_x_coords * (float4)(scale.s0);
39 const float4 new_y = (float4)(coord.s1 * scale.s1);
40 return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3);
41#elif SAMPLING_POLICY_CENTER
42 const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0);
43 const float4 new_x = (in_x_coords + ((float4)(0.5f))) * (float4)(scale.s0);
44 const float4 new_y = (float4)((coord.s1 + 0.5f) * scale.s1);
45 return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3);
46#else /* SAMPLING_POLICY */
47#error("Unsupported sampling policy");
48#endif /* SAMPLING_POLICY */
49}
50
51/** Transforms four 2D coordinates. This is used to map the output coordinates to the input coordinates.
52 *
53 * @param[in] coord 2D coordinates to transform.
54 * @param[in] scale input/output scale ratio
55 *
56 * @return a float8 containing 4 2D transformed values in the input image.
57 */
58inline const float8 transform_bilinear(const float2 coord, const float2 scale)
59{
60 const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0);
61#ifdef SAMPLING_POLICY_TOP_LEFT
62 const float4 new_x = in_x_coords * (float4)(scale.s0);
63 const float4 new_y = (float4)(coord.s1 * scale.s1);
64 return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3);
65#elif SAMPLING_POLICY_CENTER
66 const float4 new_x = (in_x_coords + ((float4)(0.5f))) * (float4)(scale.s0) - (float4)(0.5f);
67 const float4 new_y = (float4)((coord.s1 + 0.5f) * scale.s1 - 0.5f);
68 return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3);
69#else /* SAMPLING_POLICY */
70#error("Unsupported sampling policy");
71#endif /* SAMPLING_POLICY */
72}
73
74/** Performs an affine transformation on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel U8 or S16.
75 *
76 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
77 *
78 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
79 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
80 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
81 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
82 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
83 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
84 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
85 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
86 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
87 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
88 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
89 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
90 * @param[in] input_width Input image width
91 * @param[in] input_height Input image height
92 * @param[in] scale_x The scale factor along x dimension
93 * @param[in] scale_y The scale factor along y dimension
94 */
95__kernel void scale_nearest_neighbour_nchw(
96 IMAGE_DECLARATION(in),
97 IMAGE_DECLARATION(out),
98 const float input_width,
99 const float input_height,
100 const float scale_x,
101 const float scale_y)
102{
103 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
104 Image out = CONVERT_TO_IMAGE_STRUCT(out);
105 const float2 r = (float2)(scale_x, scale_y);
106 float8 transformed = transform_nearest(get_current_coords(), r);
107#ifdef ALIGN_CORNERS
108 transformed = round(transformed);
109#endif // ALIGN_CORNERS
110 const float8 tc = clamp_to_border_with_size(transformed, input_width, input_height, BORDER_SIZE);
111 vstore4(read_texels4(&in, convert_int8(tc)), 0, (__global DATA_TYPE *)out.ptr);
112}
113
114/** Performs an affine transformation on an image interpolating with the BILINEAR method.
115 *
116 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
117 *
118 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
119 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
120 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
121 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
122 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
123 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
124 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
125 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
126 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
127 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
128 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
129 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
130 * @param[in] input_width Input image width
131 * @param[in] input_height Input image height
132 * @param[in] scale_x The scale factor along x dimension
133 * @param[in] scale_y The scale factor along y dimension
134 */
135__kernel void scale_bilinear_nchw(
136 IMAGE_DECLARATION(in),
137 IMAGE_DECLARATION(out),
138 const float input_width,
139 const float input_height,
140 const float scale_x,
141 const float scale_y)
142{
143 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
144 Image out = CONVERT_TO_IMAGE_STRUCT(out);
145 const float2 r = (float2)(scale_x, scale_y);
146 const float8 tc = transform_bilinear(get_current_coords(), r);
147 vstore4(bilinear_interpolate_with_border(&in, tc, input_width, input_height, BORDER_SIZE), 0, (__global DATA_TYPE *)out.ptr);
148}