blob: 499f9ea53fb72af9d0f5d508595a0a0b819ad6b1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroud4733862019-07-09 14:21:06 +01002 * Copyright (c) 2016-2019 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 "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{
Michalis Spyroud4733862019-07-09 14:21:06 +010036#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
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042 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);
Michalis Spyroud4733862019-07-09 14:21:06 +010046#else /* SAMPLING_POLICY */
47#error("Unsupported sampling policy");
48#endif /* SAMPLING_POLICY */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049}
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);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070061#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);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064 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);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070065#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 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072}
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 *
Daniil Efremov02bf80d2017-11-22 00:26:51 +070076 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
77 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 * @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
steniu01f81652d2017-09-11 15:29:12 +010092 * @param[in] scale_x The scale factor along x dimension
93 * @param[in] scale_y The scale factor along y dimension
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 */
Michalis Spyrou46da23f2018-04-10 13:41:30 +010095__kernel void scale_nearest_neighbour_nchw(
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 IMAGE_DECLARATION(in),
97 IMAGE_DECLARATION(out),
98 const float input_width,
99 const float input_height,
steniu01f81652d2017-09-11 15:29:12 +0100100 const float scale_x,
101 const float scale_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102{
103 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
104 Image out = CONVERT_TO_IMAGE_STRUCT(out);
steniu01f81652d2017-09-11 15:29:12 +0100105 const float2 r = (float2)(scale_x, scale_y);
Daniil Efremov7a49c792017-11-14 21:25:34 +0700106 const float8 tc = clamp_to_border_with_size(transform_nearest(get_current_coords(), r), input_width, input_height, BORDER_SIZE);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 vstore4(read_texels4(&in, convert_int8(tc)), 0, (__global DATA_TYPE *)out.ptr);
108}
109
110/** Performs an affine transformation on an image interpolating with the BILINEAR method.
111 *
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700112 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
113 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
115 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
116 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
117 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
118 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
119 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
120 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
121 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
122 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
123 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
124 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
125 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
126 * @param[in] input_width Input image width
127 * @param[in] input_height Input image height
steniu01f81652d2017-09-11 15:29:12 +0100128 * @param[in] scale_x The scale factor along x dimension
129 * @param[in] scale_y The scale factor along y dimension
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 */
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100131__kernel void scale_bilinear_nchw(
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 IMAGE_DECLARATION(in),
133 IMAGE_DECLARATION(out),
134 const float input_width,
135 const float input_height,
steniu01f81652d2017-09-11 15:29:12 +0100136 const float scale_x,
137 const float scale_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138{
139 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
140 Image out = CONVERT_TO_IMAGE_STRUCT(out);
steniu01f81652d2017-09-11 15:29:12 +0100141 const float2 r = (float2)(scale_x, scale_y);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100142 const float8 tc = transform_bilinear(get_current_coords(), r);
Daniil Efremov7a49c792017-11-14 21:25:34 +0700143 vstore4(bilinear_interpolate_with_border(&in, tc, input_width, input_height, BORDER_SIZE), 0, (__global DATA_TYPE *)out.ptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100145
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000146#if defined(DEPTH_OUT)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100147/** Performs scale on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel F32. (NHWC)
148 *
149 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000150 * @note Output tensor's depth should be given as a preprocessor argument using -DDEPTH_OUT=size. e.g. -DDEPTH=16
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100151 *
152 * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32.
153 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
154 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
155 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
156 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
157 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
158 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
159 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
160 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
161 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
162 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
163 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
164 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
165 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
166 * @param[in] out_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
167 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
168 * @param[in] input_width Input image width
169 * @param[in] input_height Input image height
170 * @param[in] scale_x The scale factor along x dimension
171 * @param[in] scale_y The scale factor along y dimension
172 */
173__kernel void scale_nearest_neighbour_nhwc(
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000174 TENSOR4D_DECLARATION(in),
175 TENSOR4D_DECLARATION(out),
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100176 const float input_width,
177 const float input_height,
178 const float scale_x,
179 const float scale_y)
180{
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000181 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
182 Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100183
Michalis Spyroud4733862019-07-09 14:21:06 +0100184#ifdef SAMPLING_POLICY_TOP_LEFT
185 const float new_x = get_global_id(1) * scale_x;
186 const float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
187#elif SAMPLING_POLICY_CENTER
188 const float new_x = (get_global_id(1) + 0.5f) * scale_x;
189 const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y;
190#else /* SAMPLING_POLICY */
191#error("Unsupported sampling policy");
192#endif /* SAMPLING_POLICY */
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100193 const float clamped_x = clamp(new_x, 0.0f, input_width - 1);
194 const float clamped_y = clamp(new_y, 0.0f, input_height - 1);
195
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000196 *((__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)));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100197}
198
199/** Performs scale on an image interpolating with the BILINEAR method. (NHWC)
200 *
201 * @note Sampling policy to be used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
202 * @note If border mode replicate is used, is should be passed as -DBORDER_MODE_REPLICATE
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000203 * @note Output tensor's depth should be given as a preprocessor argument using -DDEPTH_OUT=size. e.g. -DDEPTH=16
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100204 *
205 * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32.
206 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
207 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
208 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
209 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
210 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
211 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
212 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
213 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
214 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
215 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
216 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
217 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
218 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
219 * @param[in] out_step_z dst_stride_y * number of elements along Z processed per workitem(in bytes)
220 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
221 * @param[in] input_width Input image width
222 * @param[in] input_height Input image height
223 * @param[in] scale_x The scale factor along x dimension
224 * @param[in] scale_y The scale factor along y dimension
225 */
226__kernel void scale_bilinear_nhwc(
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000227 TENSOR4D_DECLARATION(in),
228 TENSOR4D_DECLARATION(out),
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100229 const float input_width,
230 const float input_height,
231 const float scale_x,
232 const float scale_y)
233{
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000234 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
235 Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100236
237#ifdef SAMPLING_POLICY_TOP_LEFT
238 const float new_x = get_global_id(1) * scale_x;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000239 const float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100240#elif SAMPLING_POLICY_CENTER
241 const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000242 const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y - 0.5f;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100243#else /* SAMPLING_POLICY */
244#error("Unsupported sampling policy");
245#endif /* SAMPLING_POLICY */
246
247 const float new_xf = floor(new_x);
248 const float new_yf = floor(new_y);
249 float clamped_x = clamp(new_xf, 0.0f, input_width - 1);
250 float clamped_x1 = clamp(new_xf + 1, 0.0f, input_width - 1);
251 float clamped_x_ = clamped_x;
252 float clamped_x1_ = clamped_x1;
253 const float clamped_y = clamp(new_yf, 0.0f, input_height - 1);
254 const float clamped_y1 = clamp(new_yf + 1, 0.0f, input_height - 1);
255
256#ifndef BORDER_MODE_REPLICATE
257 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);
258 clamped_x_ = select(clamped_x_, 0.0f - BORDER_SIZE, new_yf + 1 > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
259 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);
260 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);
261#endif /* BORDER_MODE_REPLICATE */
262
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000263 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))),
264 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1_), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
265 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x_), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))),
266 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))));
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100267
268 const float a = new_x - new_xf;
269 const float b = 1.f - a;
270 const float a1 = new_y - new_yf;
271 const float b1 = 1.f - a1;
272 const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1));
273
274 *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE);
275}
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000276#endif /* defined(DEPTH_OUT) */