• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2016-2020 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}
149
150#if defined(DEPTH_OUT)
151/** Performs scale on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel F32. (NHWC)
152 *
153 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
154 * @note Output tensor's depth should be given as a preprocessor argument using -DDEPTH_OUT=size. e.g. -DDEPTH=16
155 *
156 * @param[in]  in_ptr                            Pointer to the source image. Supported data types: U8/S16/F16/F32.
157 * @param[in]  in_stride_x                       Stride of the source image in X dimension (in bytes)
158 * @param[in]  in_step_x                         src_stride_x * number of elements along X processed per workitem(in bytes)
159 * @param[in]  in_stride_y                       Stride of the source image in Y dimension (in bytes)
160 * @param[in]  in_step_y                         src_stride_y * number of elements along Y processed per workitem(in bytes)
161 * @param[in]  in_stride_z                       Stride of the source image in Z dimension (in bytes)
162 * @param[in]  in_step_z                         src_stride_z * number of elements along Z processed per workitem(in bytes)
163 * @param[in]  in_offset_first_element_in_bytes  The offset of the first element in the source image
164 * @param[out] out_ptr                           Pointer to the destination image. Supported data types: same as @p in_ptr
165 * @param[in]  out_stride_x                      Stride of the destination image in X dimension (in bytes)
166 * @param[in]  out_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
167 * @param[in]  out_stride_y                      Stride of the destination image in Y dimension (in bytes)
168 * @param[in]  out_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
169 * @param[in]  out_stride_z                      Stride of the destination image in Z dimension (in bytes)
170 * @param[in]  out_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
171 * @param[in]  out_offset_first_element_in_bytes The offset of the first element in the destination image
172 * @param[in]  input_width                       Input image width
173 * @param[in]  input_height                      Input image height
174 * @param[in]  scale_x                           The scale factor along x dimension
175 * @param[in]  scale_y                           The scale factor along y dimension
176 */
177__kernel void scale_nearest_neighbour_nhwc(
178    TENSOR4D_DECLARATION(in),
179    TENSOR4D_DECLARATION(out),
180    const float input_width,
181    const float input_height,
182    const float scale_x,
183    const float scale_y)
184{
185    Tensor4D in  = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
186    Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
187
188#ifdef SAMPLING_POLICY_TOP_LEFT
189    float new_x = get_global_id(1) * scale_x;
190    float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
191#elif SAMPLING_POLICY_CENTER
192    float new_x = (get_global_id(1) + 0.5f) * scale_x;
193    float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y;
194#else /* SAMPLING_POLICY */
195#error("Unsupported sampling policy");
196#endif /* SAMPLING_POLICY */
197#ifdef ALIGN_CORNERS
198    new_x = round(new_x);
199    new_y = round(new_y);
200#endif /* ALIGN_CORNERS */
201    const float clamped_x = clamp(new_x, 0.0f, input_width - 1);
202    const float clamped_y = clamp(new_y, 0.0f, input_height - 1);
203
204    *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT)));
205}
206
207/** Performs scale on an image interpolating with the BILINEAR method. (NHWC)
208 *
209 * @note Sampling policy to be used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
210 * @note If border mode replicate is used, is should be passed as -DBORDER_MODE_REPLICATE
211 * @note Output tensor's depth should be given as a preprocessor argument using -DDEPTH_OUT=size. e.g. -DDEPTH=16
212 *
213 * @param[in]  in_ptr                            Pointer to the source image. Supported data types: U8/S16/F16/F32.
214 * @param[in]  in_stride_x                       Stride of the source image in X dimension (in bytes)
215 * @param[in]  in_step_x                         src_stride_x * number of elements along X processed per workitem(in bytes)
216 * @param[in]  in_stride_y                       Stride of the source image in Y dimension (in bytes)
217 * @param[in]  in_step_y                         src_stride_y * number of elements along Y processed per workitem(in bytes)
218 * @param[in]  in_stride_z                       Stride of the source image in Z dimension (in bytes)
219 * @param[in]  in_step_z                         src_stride_z * number of elements along Z processed per workitem(in bytes)
220 * @param[in]  in_offset_first_element_in_bytes  The offset of the first element in the source image
221 * @param[out] out_ptr                           Pointer to the destination image. Supported data types: same as @p in_ptr
222 * @param[in]  out_stride_x                      Stride of the destination image in X dimension (in bytes)
223 * @param[in]  out_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
224 * @param[in]  out_stride_y                      Stride of the destination image in Y dimension (in bytes)
225 * @param[in]  out_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
226 * @param[in]  out_stride_z                      Stride of the destination image in Z dimension (in bytes)
227 * @param[in]  out_step_z                        dst_stride_y * number of elements along Z processed per workitem(in bytes)
228 * @param[in]  out_offset_first_element_in_bytes The offset of the first element in the destination image
229 * @param[in]  input_width                       Input image width
230 * @param[in]  input_height                      Input image height
231 * @param[in]  scale_x                           The scale factor along x dimension
232 * @param[in]  scale_y                           The scale factor along y dimension
233 */
234__kernel void scale_bilinear_nhwc(
235    TENSOR4D_DECLARATION(in),
236    TENSOR4D_DECLARATION(out),
237    const float input_width,
238    const float input_height,
239    const float scale_x,
240    const float scale_y)
241{
242    Tensor4D in  = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
243    Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
244
245#ifdef SAMPLING_POLICY_TOP_LEFT
246    const float new_x = get_global_id(1) * scale_x;
247    const float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
248#elif SAMPLING_POLICY_CENTER
249    const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f;
250    const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y - 0.5f;
251#else /* SAMPLING_POLICY */
252#error("Unsupported sampling policy");
253#endif /* SAMPLING_POLICY */
254
255    const float new_xf      = floor(new_x);
256    const float new_yf      = floor(new_y);
257    float       clamped_x   = clamp(new_xf, 0.0f, input_width - 1);
258    float       clamped_x1  = clamp(new_xf + 1, 0.0f, input_width - 1);
259    float       clamped_x_  = clamped_x;
260    float       clamped_x1_ = clamped_x1;
261    const float clamped_y   = clamp(new_yf, 0.0f, input_height - 1);
262    const float clamped_y1  = clamp(new_yf + 1, 0.0f, input_height - 1);
263
264#ifndef BORDER_MODE_REPLICATE
265    clamped_x1  = select(clamped_x1, 0.0f - BORDER_SIZE, new_yf + 1 < 0.f || new_yf + 1 > input_height - 1 || new_xf + 1 < 0.f || new_xf + 1 > input_width - 1);
266    clamped_x_  = select(clamped_x_, 0.0f - BORDER_SIZE, new_yf + 1 > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
267    clamped_x   = select(clamped_x, 0.0f - BORDER_SIZE, new_yf < 0.f || new_yf > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
268    clamped_x1_ = select(clamped_x1_, 0.0f - BORDER_SIZE, new_xf + 1 < 0.f || new_xf + 1 > input_width - 1 || new_yf < 0.f || new_yf > input_height - 1);
269#endif /* BORDER_MODE_REPLICATE */
270
271    float4 ins = (float4)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
272                          *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1_), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
273                          *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x_), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))),
274                          *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))));
275
276    const float a  = new_x - new_xf;
277    const float b  = 1.f - a;
278    const float a1 = new_y - new_yf;
279    const float b1 = 1.f - a1;
280    const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1));
281
282    *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE);
283}
284#endif /* defined(DEPTH_OUT) */