blob: b3398bd11c558ca814a9490f4e29b0921be80f24 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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 */
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);
52 const float4 new_x = (in_x_coords + ((float4)(0.5f))) * (float4)(scale.s0) - (float4)(0.5f);
53 const float4 new_y = (float4)((coord.s1 + 0.5f) * scale.s1 - 0.5f);
54 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);
55}
56
57/** Performs an affine transformation on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel U8 or S16.
58 *
59 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
60 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
61 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
62 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
63 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
64 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
65 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
66 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
67 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
68 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
69 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
70 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
71 * @param[in] input_width Input image width
72 * @param[in] input_height Input image height
73 * @param[in] output_width Output image width
74 * @param[in] output_height Output image height
75 */
76__kernel void scale_nearest_neighbour(
77 IMAGE_DECLARATION(in),
78 IMAGE_DECLARATION(out),
79 const float input_width,
80 const float input_height,
81 const float output_width,
82 const float output_height)
83{
84 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
85 Image out = CONVERT_TO_IMAGE_STRUCT(out);
86 const float2 r = (float2)(input_width / output_width, input_height / output_height);
87 const float8 tc = clamp_to_border(transform_nearest(get_current_coords(), r), input_width, input_height);
88 vstore4(read_texels4(&in, convert_int8(tc)), 0, (__global DATA_TYPE *)out.ptr);
89}
90
91/** Performs an affine transformation on an image interpolating with the BILINEAR method.
92 *
93 * @param[in] in_ptr Pointer to the source image. Supported data types: U8, S16.
94 * @param[in] in_stride_x Stride of the source image in X dimension (in bytes)
95 * @param[in] in_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
96 * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes)
97 * @param[in] in_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
98 * @param[in] in_offset_first_element_in_bytes The offset of the first element in the source image
99 * @param[out] out_ptr Pointer to the destination image. Supported data types: U8, S16. (Must be the same as the input)
100 * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes)
101 * @param[in] out_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
102 * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes)
103 * @param[in] out_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
104 * @param[in] out_offset_first_element_in_bytes The offset of the first element in the destination image
105 * @param[in] input_width Input image width
106 * @param[in] input_height Input image height
107 * @param[in] output_width Output image width
108 * @param[in] output_height Output image height
109 */
110__kernel void scale_bilinear(
111 IMAGE_DECLARATION(in),
112 IMAGE_DECLARATION(out),
113 const float input_width,
114 const float input_height,
115 const float output_width,
116 const float output_height)
117{
118 Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in);
119 Image out = CONVERT_TO_IMAGE_STRUCT(out);
120 const float2 r = (float2)(input_width / output_width, input_height / output_height);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100121 const float8 tc = transform_bilinear(get_current_coords(), r);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 vstore4(bilinear_interpolate(&in, tc, input_width, input_height), 0, (__global DATA_TYPE *)out.ptr);
123}