Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | 78e7691 | 2021-05-06 10:05:22 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016, 2017, 2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 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 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | inline const float8 |
| 35 | clamp_to_border_with_size(float8 coords, const float width, const float height, const float border_size) |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 36 | { |
| 37 | const float4 clamped_x = clamp(coords.even, 0.0f - border_size, width - 1 + border_size); |
| 38 | const float4 clamped_y = clamp(coords.odd, 0.0f - border_size, height - 1 + border_size); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | return (float8)(clamped_x.s0, clamped_y.s0, clamped_x.s1, clamped_y.s1, clamped_x.s2, clamped_y.s2, clamped_x.s3, |
| 40 | clamped_y.s3); |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 41 | } |
| 42 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 43 | /** Clamps the given coordinates to the borders. |
| 44 | * |
| 45 | * @param[in] coords Vector of 2D coordinates to clamp. Even positions are X coords, odd positions are Y coords. |
| 46 | * @param[in] width Width of the image |
| 47 | * @param[in] height Height of the image |
| 48 | * |
| 49 | */ |
| 50 | inline const float8 clamp_to_border(float8 coords, const float width, const float height) |
| 51 | { |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 52 | return clamp_to_border_with_size(coords, width, height, 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** Reads four texels from the input image. The coords vector is used to determine which texels to be read. |
| 56 | * |
| 57 | * @param[in] in Pointer to the source image. |
| 58 | * @param[in] coords Vector of coordinates to be read from the image. |
| 59 | */ |
| 60 | inline const VEC_DATA_TYPE(DATA_TYPE, 4) read_texels4(const Image *in, const int8 coords) |
| 61 | { |
| 62 | return (VEC_DATA_TYPE(DATA_TYPE, 4))(*((__global DATA_TYPE *)offset(in, coords.s0, coords.s1)), |
| 63 | *((__global DATA_TYPE *)offset(in, coords.s2, coords.s3)), |
| 64 | *((__global DATA_TYPE *)offset(in, coords.s4, coords.s5)), |
| 65 | *((__global DATA_TYPE *)offset(in, coords.s6, coords.s7))); |
| 66 | } |
| 67 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | /** Given a texel coordinates this function will return the following array of coordinates: |
| 69 | * [ P, right neighbour, below neighbour, below right neighbour ] |
| 70 | * |
| 71 | * @note No checks to see if the coordinates are out of the image are done here. |
| 72 | * |
| 73 | * @param[in] coord Input coordinates |
| 74 | * |
| 75 | * @return vector of 8 floats with the coordinates, even positions are x and odd y. |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 76 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | inline const float8 get_neighbour_coords(const float2 coord) |
| 78 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 79 | return (float8)(/*tl*/ coord.s0, coord.s1, /*tr*/ coord.s0 + 1, coord.s1, /*bl*/ coord.s0, coord.s1 + 1, |
| 80 | /*br*/ coord.s0 + 1, coord.s1 + 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** Computes the bilinear interpolation for each set of coordinates in the vector coords and returns the values |
| 84 | * |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 85 | * @param[in] in Pointer to the source image. |
| 86 | * @param[in] coords Vector of four 2D coordinates. Even pos is x and odd y. |
| 87 | * @param[in] width Width of the image |
| 88 | * @param[in] height Height of the image |
| 89 | * @param[in] border_size Border size |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 90 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | inline const VEC_DATA_TYPE(DATA_TYPE, 4) bilinear_interpolate_with_border( |
| 92 | const Image *in, const float8 coords, const float width, const float height, const float border_size) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | { |
| 94 | // 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. |
| 95 | |
| 96 | // Sets the 4x4 coordinates for each of the four input texels |
| 97 | const float8 fc = floor(coords); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | const float16 c1 = |
| 99 | (float16)(clamp_to_border_with_size(get_neighbour_coords((float2)(fc.s0, fc.s1)), width, height, border_size), |
| 100 | clamp_to_border_with_size(get_neighbour_coords((float2)(fc.s2, fc.s3)), width, height, border_size)); |
| 101 | const float16 c2 = |
| 102 | (float16)(clamp_to_border_with_size(get_neighbour_coords((float2)(fc.s4, fc.s5)), width, height, border_size), |
| 103 | clamp_to_border_with_size(get_neighbour_coords((float2)(fc.s6, fc.s7)), width, height, border_size)); |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 104 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | // Loads the values from the input image |
| 106 | const float16 t = (float16)( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | /* tl, tr, bl, br */ |
| 108 | *((__global DATA_TYPE *)offset(in, c1.s0, c1.s1)), *((__global DATA_TYPE *)offset(in, c1.s2, c1.s3)), |
| 109 | *((__global DATA_TYPE *)offset(in, c1.s4, c1.s5)), *((__global DATA_TYPE *)offset(in, c1.s6, c1.s7)), |
| 110 | *((__global DATA_TYPE *)offset(in, c1.s8, c1.s9)), *((__global DATA_TYPE *)offset(in, c1.sa, c1.sb)), |
| 111 | *((__global DATA_TYPE *)offset(in, c1.sc, c1.sd)), *((__global DATA_TYPE *)offset(in, c1.se, c1.sf)), |
| 112 | *((__global DATA_TYPE *)offset(in, c2.s0, c2.s1)), *((__global DATA_TYPE *)offset(in, c2.s2, c2.s3)), |
| 113 | *((__global DATA_TYPE *)offset(in, c2.s4, c2.s5)), *((__global DATA_TYPE *)offset(in, c2.s6, c2.s7)), |
| 114 | *((__global DATA_TYPE *)offset(in, c2.s8, c2.s9)), *((__global DATA_TYPE *)offset(in, c2.sa, c2.sb)), |
| 115 | *((__global DATA_TYPE *)offset(in, c2.sc, c2.sd)), *((__global DATA_TYPE *)offset(in, c2.se, c2.sf))); |
| 116 | const float8 a = coords - fc; |
| 117 | const float8 b = ((float8)(1.f)) - a; |
| 118 | const float4 fr = |
| 119 | (float4)(((t.s0 * b.s0 * b.s1) + (t.s1 * a.s0 * b.s1) + (t.s2 * b.s0 * a.s1) + (t.s3 * a.s0 * a.s1)), |
| 120 | ((t.s4 * b.s2 * b.s3) + (t.s5 * a.s2 * b.s3) + (t.s6 * b.s2 * a.s3) + (t.s7 * a.s2 * a.s3)), |
| 121 | ((t.s8 * b.s4 * b.s5) + (t.s9 * a.s4 * b.s5) + (t.sa * b.s4 * a.s5) + (t.sb * a.s4 * a.s5)), |
| 122 | ((t.sc * b.s6 * b.s7) + (t.sd * a.s6 * b.s7) + (t.se * b.s6 * a.s7) + (t.sf * a.s6 * a.s7))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | return CONVERT(fr, VEC_DATA_TYPE(DATA_TYPE, 4)); |
| 124 | } |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 125 | |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 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 |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 132 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | inline const VEC_DATA_TYPE(DATA_TYPE, 4) |
| 134 | bilinear_interpolate(const Image *in, const float8 coords, const float width, const float height) |
Daniil Efremov | 7a49c79 | 2017-11-14 21:25:34 +0700 | [diff] [blame] | 135 | { |
| 136 | return bilinear_interpolate_with_border(in, coords, width, height, 1); |
| 137 | } |