blob: 5ac6443c9857c12e2e4aa67bc4d701789552d7ac [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
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000137#if defined(DEPTH_OUT)
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100138/** Performs scale on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel F32. (NHWC)
139 *
140 * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000141 * @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 +0100142 *
143 * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32.
144 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
145 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
146 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
147 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
148 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
149 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
150 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
151 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
152 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
153 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
154 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
155 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
156 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
157 * @param[in] out_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes)
158 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
159 * @param[in] input_width Input image width
160 * @param[in] input_height Input image height
161 * @param[in] scale_x The scale factor along x dimension
162 * @param[in] scale_y The scale factor along y dimension
163 */
164__kernel void scale_nearest_neighbour_nhwc(
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000165 TENSOR4D_DECLARATION(in),
166 TENSOR4D_DECLARATION(out),
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100167 const float input_width,
168 const float input_height,
169 const float scale_x,
170 const float scale_y)
171{
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000172 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
173 Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100174
175 const float new_x = (get_global_id(1) + 0.5f) * scale_x;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000176 const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100177 const float clamped_x = clamp(new_x, 0.0f, input_width - 1);
178 const float clamped_y = clamp(new_y, 0.0f, input_height - 1);
179
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000180 *((__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 +0100181}
182
183/** Performs scale on an image interpolating with the BILINEAR method. (NHWC)
184 *
185 * @note Sampling policy to be used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
186 * @note If border mode replicate is used, is should be passed as -DBORDER_MODE_REPLICATE
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000187 * @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 +0100188 *
189 * @param[in] in_ptr Pointer to the source image. Supported data types: U8/S16/F16/F32.
190 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
191 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
192 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
193 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
194 * @param[in] in_stride_z Stride of the source image in Z dimension (in bytes)
195 * @param[in] in_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
196 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
197 * @param[out] out_ptr Pointer to the destination image. Supported data types: same as @p in_ptr
198 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
199 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
200 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
201 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
202 * @param[in] out_stride_z Stride of the destination image in Z dimension (in bytes)
203 * @param[in] out_step_z dst_stride_y * number of elements along Z processed per workitem(in bytes)
204 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
205 * @param[in] input_width Input image width
206 * @param[in] input_height Input image height
207 * @param[in] scale_x The scale factor along x dimension
208 * @param[in] scale_y The scale factor along y dimension
209 */
210__kernel void scale_bilinear_nhwc(
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000211 TENSOR4D_DECLARATION(in),
212 TENSOR4D_DECLARATION(out),
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100213 const float input_width,
214 const float input_height,
215 const float scale_x,
216 const float scale_y)
217{
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000218 Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0);
219 Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT);
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100220
221#ifdef SAMPLING_POLICY_TOP_LEFT
222 const float new_x = get_global_id(1) * scale_x;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000223 const float new_y = (get_global_id(2) % DEPTH_OUT) * scale_y;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100224#elif SAMPLING_POLICY_CENTER
225 const float new_x = (get_global_id(1) + 0.5f) * scale_x - 0.5f;
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000226 const float new_y = ((get_global_id(2) % DEPTH_OUT) + 0.5f) * scale_y - 0.5f;
Michalis Spyrou46da23f2018-04-10 13:41:30 +0100227#else /* SAMPLING_POLICY */
228#error("Unsupported sampling policy");
229#endif /* SAMPLING_POLICY */
230
231 const float new_xf = floor(new_x);
232 const float new_yf = floor(new_y);
233 float clamped_x = clamp(new_xf, 0.0f, input_width - 1);
234 float clamped_x1 = clamp(new_xf + 1, 0.0f, input_width - 1);
235 float clamped_x_ = clamped_x;
236 float clamped_x1_ = clamped_x1;
237 const float clamped_y = clamp(new_yf, 0.0f, input_height - 1);
238 const float clamped_y1 = clamp(new_yf + 1, 0.0f, input_height - 1);
239
240#ifndef BORDER_MODE_REPLICATE
241 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);
242 clamped_x_ = select(clamped_x_, 0.0f - BORDER_SIZE, new_yf + 1 > input_height - 1 || new_xf < 0.f || new_xf > input_width - 1);
243 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);
244 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);
245#endif /* BORDER_MODE_REPLICATE */
246
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000247 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))),
248 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1_), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))),
249 *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x_), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))),
250 *((__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 +0100251
252 const float a = new_x - new_xf;
253 const float b = 1.f - a;
254 const float a1 = new_y - new_yf;
255 const float b1 = 1.f - a1;
256 const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1));
257
258 *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE);
259}
Michalis Spyrou1f8db2b2018-12-10 16:19:20 +0000260#endif /* defined(DEPTH_OUT) */