blob: ca21be6765716efbc01f3188f16c42972b24ffbc [file] [log] [blame]
Michalis Spyrou17220e22018-09-12 13:35:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Michalis Spyrou17220e22018-09-12 13:35:38 +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_asymm.h"
25
26/** Clamps the given coordinates to the borders according to the border size.
27 *
28 * @param[in] coords Vector of 2D coordinates to clamp. Even positions are X coords, odd positions are Y coords.
29 * @param[in] width Width of the image
30 * @param[in] height Height of the image
31 * @param[in] border_size Border size of the image
32 *
33 */
34inline const float8 clamp_to_border_with_size_quantized(float8 coords, const float width, const float height, const float border_size)
35{
36 const float4 clamped_x = clamp(coords.even, 0.0f - border_size, width - 1 + border_size);
37 const float4 clamped_y = clamp(coords.odd, 0.0f - border_size, height - 1 + border_size);
38 return (float8)(clamped_x.s0, clamped_y.s0, clamped_x.s1, clamped_y.s1, clamped_x.s2, clamped_y.s2, clamped_x.s3, clamped_y.s3);
39}
40
41/* FIXME(COMPMID-682): Clamp border properly in UNDEFINED border mode in Warp, Scale, Remap */
42/** Clamps the given coordinates to the borders.
43 *
44 * @param[in] coords Vector of 2D coordinates to clamp. Even positions are X coords, odd positions are Y coords.
45 * @param[in] width Width of the image
46 * @param[in] height Height of the image
47 *
48 */
49inline const float8 clamp_to_border_quantized(float8 coords, const float width, const float height)
50{
51 return clamp_to_border_with_size_quantized(coords, width, height, 1);
52}
53
54/** Given a texel coordinates this function will return the following array of coordinates:
55 * [ P, right neighbour, below neighbour, below right neighbour ]
56 *
57 * @note No checks to see if the coordinates are out of the image are done here.
58 *
59 * @param[in] coord Input coordinates
60 *
61 * @return vector of 8 floats with the coordinates, even positions are x and odd y.
62 */
63inline const float8 get_neighbour_coords_quantized(const float2 coord)
64{
65 return (float8)(/*tl*/ coord.s0, coord.s1, /*tr*/ coord.s0 + 1, coord.s1, /*bl*/ coord.s0, coord.s1 + 1, /*br*/ coord.s0 + 1, coord.s1 + 1);
66}
67
68/** Returns the current thread coordinates. */
69inline const float2 get_current_coords_quantized()
70{
71 return (float2)(get_global_id(0) * 4, get_global_id(1));
72}
73
74/** Computes the bilinear interpolation for each set of coordinates in the vector coords and returns the values
75 *
76 * @param[in] in Pointer to the source image.
77 * @param[in] coords Vector of four 2D coordinates. Even pos is x and odd y.
78 * @param[in] width Width of the image
79 * @param[in] height Height of the image
80 * @param[in] border_size Border size
81 * @param[in] scale Scale value
82 * @param[in] offset_qasymm Offset value
83 */
84inline const VEC_DATA_TYPE(DATA_TYPE, 4) bilinear_interpolate_with_border_quantized(const Image *in, const float8 coords, const float width, const float height, const float border_size,
85 const float scale, const int offset_qasymm)
86{
87 // If any of the 4 texels is out of the image's boundaries we use the border value (REPLICATE or CONSTANT) for any texel out of the image.
88
89 // Sets the 4x4 coordinates for each of the four input texels
90 const float8 fc = floor(coords);
91 const float16 c1 = (float16)(
92 clamp_to_border_with_size_quantized(get_neighbour_coords_quantized((float2)(fc.s0, fc.s1)), width, height, border_size),
93 clamp_to_border_with_size_quantized(get_neighbour_coords_quantized((float2)(fc.s2, fc.s3)), width, height, border_size));
94 const float16 c2 = (float16)(
95 clamp_to_border_with_size_quantized(get_neighbour_coords_quantized((float2)(fc.s4, fc.s5)), width, height, border_size),
96 clamp_to_border_with_size_quantized(get_neighbour_coords_quantized((float2)(fc.s6, fc.s7)), width, height, border_size));
97
98 // Loads the values from the input image
99 const int16 t = (int16)(
100 /* tl, tr, bl, br */
101 * ((__global DATA_TYPE *)offset(in, c1.s0, c1.s1)), *((__global DATA_TYPE *)offset(in, c1.s2, c1.s3)),
102 *((__global DATA_TYPE *)offset(in, c1.s4, c1.s5)), *((__global DATA_TYPE *)offset(in, c1.s6, c1.s7)),
103 *((__global DATA_TYPE *)offset(in, c1.s8, c1.s9)), *((__global DATA_TYPE *)offset(in, c1.sa, c1.sb)),
104 *((__global DATA_TYPE *)offset(in, c1.sc, c1.sd)), *((__global DATA_TYPE *)offset(in, c1.se, c1.sf)),
105 *((__global DATA_TYPE *)offset(in, c2.s0, c2.s1)), *((__global DATA_TYPE *)offset(in, c2.s2, c2.s3)),
106 *((__global DATA_TYPE *)offset(in, c2.s4, c2.s5)), *((__global DATA_TYPE *)offset(in, c2.s6, c2.s7)),
107 *((__global DATA_TYPE *)offset(in, c2.s8, c2.s9)), *((__global DATA_TYPE *)offset(in, c2.sa, c2.sb)),
108 *((__global DATA_TYPE *)offset(in, c2.sc, c2.sd)), *((__global DATA_TYPE *)offset(in, c2.se, c2.sf)));
109
110 const float16 inf32 = convert_float16(t - (int16)offset_qasymm) * (float16)scale;
111
112 const float8 a = coords - fc;
113 const float8 b = ((float8)(1.f)) - a;
114 const float4 fr = (float4)(
115 ((inf32.s0 * b.s0 * b.s1) + (inf32.s1 * a.s0 * b.s1) + (inf32.s2 * b.s0 * a.s1) + (inf32.s3 * a.s0 * a.s1)),
116 ((inf32.s4 * b.s2 * b.s3) + (inf32.s5 * a.s2 * b.s3) + (inf32.s6 * b.s2 * a.s3) + (inf32.s7 * a.s2 * a.s3)),
117 ((inf32.s8 * b.s4 * b.s5) + (inf32.s9 * a.s4 * b.s5) + (inf32.sa * b.s4 * a.s5) + (inf32.sb * a.s4 * a.s5)),
118 ((inf32.sc * b.s6 * b.s7) + (inf32.sd * a.s6 * b.s7) + (inf32.se * b.s6 * a.s7) + (inf32.sf * a.s6 * a.s7)));
119
Manuel Bottini8481d832019-12-10 15:28:40 +0000120 const VEC_DATA_TYPE(DATA_TYPE, 4) res = CONVERT_SAT(convert_int4_sat_rtp(fr / scale) + offset_qasymm, VEC_DATA_TYPE(DATA_TYPE, 4));
Michalis Spyrou17220e22018-09-12 13:35:38 +0100121
122 return res;
123}
124
125/* FIXME(COMPMID-682): Clamp border properly in UNDEFINED border mode in Warp, Scale, Remap */
126/** Computes the bilinear interpolation for each set of coordinates in the vector coords and returns the values
127 *
128 * @param[in] in Pointer to the source image.
129 * @param[in] coords Vector of four 2D coordinates. Even pos is x and odd y.
130 * @param[in] width Width of the image
131 * @param[in] height Height of the image
132 * @param[in] scale Scale value
133 * @param[in] offset_qasymm Offset value
134 */
135inline const VEC_DATA_TYPE(DATA_TYPE, 4) bilinear_interpolate_quantized(const Image *in, const float8 coords, const float width, const float height, const float scale, const int offset_qasymm)
136{
137 return bilinear_interpolate_with_border_quantized(in, coords, width, height, 1, scale, offset_qasymm);
138}