arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / scale.cl
1 /*
2  * Copyright (c) 2016, 2017 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  */
34 inline 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  */
49 inline 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);
52 #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);
55     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);
56 #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 */
63 }
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  *
67  * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
68  *
69  * @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
83  * @param[in]  scale_x                           The scale factor along x dimension
84  * @param[in]  scale_y                           The scale factor along y dimension
85  */
86 __kernel void scale_nearest_neighbour(
87     IMAGE_DECLARATION(in),
88     IMAGE_DECLARATION(out),
89     const float input_width,
90     const float input_height,
91     const float scale_x,
92     const float scale_y)
93 {
94     Image        in  = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
95     Image        out = CONVERT_TO_IMAGE_STRUCT(out);
96     const float2 r   = (float2)(scale_x, scale_y);
97     const float8 tc  = clamp_to_border_with_size(transform_nearest(get_current_coords(), r), input_width, input_height, BORDER_SIZE);
98     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  *
103  * @note Sampling policy to used is passed as -DSAMPLING_POLICY_(TYPE) e.g. -DSAMPLING_POLICY_TOP_LEFT
104  *
105  * @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
119  * @param[in]  scale_x                           The scale factor along x dimension
120  * @param[in]  scale_y                           The scale factor along y dimension
121  */
122 __kernel void scale_bilinear(
123     IMAGE_DECLARATION(in),
124     IMAGE_DECLARATION(out),
125     const float input_width,
126     const float input_height,
127     const float scale_x,
128     const float scale_y)
129 {
130     Image        in  = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
131     Image        out = CONVERT_TO_IMAGE_STRUCT(out);
132     const float2 r   = (float2)(scale_x, scale_y);
133     const float8 tc  = transform_bilinear(get_current_coords(), r);
134     vstore4(bilinear_interpolate_with_border(&in, tc, input_width, input_height, BORDER_SIZE), 0, (__global DATA_TYPE *)out.ptr);
135 }