blob: 744f28a918975c55f1bf7c0f13160acdf86e07d7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyrou46da23f2018-04-10 13:41:30 +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 "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 const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0);
37 const float4 new_x = (in_x_coords + ((float4)(0.5f))) * (float4)(scale.s0);
38 const float4 new_y = (float4)((coord.s1 + 0.5f) * scale.s1);
39 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);
40}
41
42/** Transforms four 2D coordinates. This is used to map the output coordinates to the input coordinates.
43 *
44 * @param[in] coord 2D coordinates to transform.
45 * @param[in] scale input/output scale ratio
46 *
47 * @return a float8 containing 4 2D transformed values in the input image.
48 */
49inline const float8 transform_bilinear(const float2 coord, const float2 scale)
50{
51 const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070052#ifdef SAMPLING_POLICY_TOP_LEFT
53 const float4 new_x = in_x_coords * (float4)(scale.s0);
54 const float4 new_y = (float4)(coord.s1 * scale.s1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055 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 +070056#elif SAMPLING_POLICY_CENTER
57 const float4 new_x = (in_x_coords + ((float4)(0.5f))) * (float4)(scale.s0) - (float4)(0.5f);
58 const float4 new_y = (float4)((coord.s1 + 0.5f) * scale.s1 - 0.5f);
59 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);
60#else /* SAMPLING_POLICY */
61#error("Unsupported sampling policy");
62#endif /* SAMPLING_POLICY */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063}
64
65/** Performs an affine transformation on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel U8 or S16.
66 *
Daniil Efremov02bf80d2017-11-22 00:26:51 +070067 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
68 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
70 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
71 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
72 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
73 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
74 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
75 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
76 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
77 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
78 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
79 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
80 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
81 * @param[in] input_width Input image width
82 * @param[in] input_height Input image height
steniu01f81652d2017-09-11 15:29:12 +010083 * @param[in] scale_x The scale factor along x dimension
84 * @param[in] scale_y The scale factor along y dimension
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 */
Michalis Spyrou46da23f2018-04-10 13:41:30 +010086__kernel void scale_nearest_neighbour_nchw(
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 IMAGE_DECLARATION(in),
88 IMAGE_DECLARATION(out),
89 const float input_width,
90 const float input_height,
steniu01f81652d2017-09-11 15:29:12 +010091 const float scale_x,
92 const float scale_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093{
94 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
95 Image out = CONVERT_TO_IMAGE_STRUCT(out);
steniu01f81652d2017-09-11 15:29:12 +010096 const float2 r = (float2)(scale_x, scale_y);
Daniil Efremov7a49c792017-11-14 21:25:34 +070097 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 +010098 vstore4(read_texels4(&in, convert_int8(tc)), 0, (__global DATA_TYPE *)out.ptr);
99}
100
101/** Performs an affine transformation on an image interpolating with the BILINEAR method.
102 *
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700103 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
104 *
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
106 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
107 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
108 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
109 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
110 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
111 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
112 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
113 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
114 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
115 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
116 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
117 * @param[in] input_width Input image width
118 * @param[in] input_height Input image height
steniu01f81652d2017-09-11 15:29:12 +0100119 * @param[in] scale_x The scale factor along x dimension
120 * @param[in] scale_y The scale factor along y dimension
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121 */
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100122__kernel void scale_bilinear_nchw(
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 IMAGE_DECLARATION(in),
124 IMAGE_DECLARATION(out),
125 const float input_width,
126 const float input_height,
steniu01f81652d2017-09-11 15:29:12 +0100127 const float scale_x,
128 const float scale_y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129{
130 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
131 Image out = CONVERT_TO_IMAGE_STRUCT(out);
steniu01f81652d2017-09-11 15:29:12 +0100132 const float2 r = (float2)(scale_x, scale_y);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100133 const float8 tc = transform_bilinear(get_current_coords(), r);
Daniil Efremov7a49c792017-11-14 21:25:34 +0700134 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 +0100135}
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100136
137/** Performs scale on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel F32. (NHWC)
138 *
139 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
140 *
141 * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32.
142 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
143 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
144 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
145 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
146 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
147 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
148 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
149 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
150 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
151 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
152 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
153 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
154 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
155 * @param[in] out_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
156 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
157 * @param[in] input_width Input image width
158 * @param[in] input_height Input image height
159 * @param[in] scale_x The scale factor along x dimension
160 * @param[in] scale_y The scale factor along y dimension
161 */
162__kernel void scale_nearest_neighbour_nhwc(
163 TENSOR3D_DECLARATION(in),
164 TENSOR3D_DECLARATION(out),
165 const float input_width,
166 const float input_height,
167 const float scale_x,
168 const float scale_y)
169{
170 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(in);
171 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
172
173 const float new_x = (get_global_id(1) + 0.5f) * scale_x;
174 const float new_y = (get_global_id(2) + 0.5f) * scale_y;
175 const float clamped_x = clamp(new_x, 0.0f, input_width - 1);
176 const float clamped_y = clamp(new_y, 0.0f, input_height - 1);
177
178 *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y)));
179}
180
181/** Performs scale on an image interpolating with the BILINEAR method. (NHWC)
182 *
183 * @note Sampling policy to be used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
184 * @note If border mode replicate is used, is should be passed as -DBORDER_MODE_REPLICATE
185 *
186 * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32.
187 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
188 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
189 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
190 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
191 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
192 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
193 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
194 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
195 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
196 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
197 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
198 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
199 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
200 * @param[in] out_step_z dst_stride_y * number of elements along Z processed per workitem(in bytes)
201 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
202 * @param[in] input_width Input image width
203 * @param[in] input_height Input image height
204 * @param[in] scale_x The scale factor along x dimension
205 * @param[in] scale_y The scale factor along y dimension
206 */
207__kernel void scale_bilinear_nhwc(
208 TENSOR3D_DECLARATION(in),
209 TENSOR3D_DECLARATION(out),
210 const float input_width,
211 const float input_height,
212 const float scale_x,
213 const float scale_y)
214{
215 Tensor3D in = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(in);
216 Tensor3D out = CONVERT_TO_TENSOR3D_STRUCT(out);
217
218#ifdef SAMPLING_POLICY_TOP_LEFT
219 const float new_x = get_global_id(1) * scale_x;
220 const float new_y = get_global_id(2) * scale_y;
221#elif SAMPLING_POLICY_CENTER
222 const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f;
223 const float new_y = (get_global_id(2) + 0.5f) * scale_y - 0.5f;
224#else /* SAMPLING_POLICY */
225#error("Unsupported sampling policy");
226#endif /* SAMPLING_POLICY */
227
228 const float new_xf = floor(new_x);
229 const float new_yf = floor(new_y);
230 float clamped_x = clamp(new_xf, 0.0f, input_width - 1);
231 float clamped_x1 = clamp(new_xf + 1, 0.0f, input_width - 1);
232 float clamped_x_ = clamped_x;
233 float clamped_x1_ = clamped_x1;
234 const float clamped_y = clamp(new_yf, 0.0f, input_height - 1);
235 const float clamped_y1 = clamp(new_yf + 1, 0.0f, input_height - 1);
236
237#ifndef BORDER_MODE_REPLICATE
238 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);
239 clamped_x_ = select(clamped_x_, 0.0f - BORDER_SIZE, new_yf + 1 > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
240 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);
241 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);
242#endif /* BORDER_MODE_REPLICATE */
243
244 float4 ins = (float4)(*((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y))),
245 *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x1_), convert_int(clamped_y))),
246 *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x_), convert_int(clamped_y1))),
247 *((__global DATA_TYPE *)tensor3D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1))));
248
249 const float a = new_x - new_xf;
250 const float b = 1.f - a;
251 const float a1 = new_y - new_yf;
252 const float b1 = 1.f - a1;
253 const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1));
254
255 *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE);
256}