Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 2 | * Copyright (c) 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 | #include "warp_helpers.h" |
| 26 | |
| 27 | /** Performs a remapping of an input image to an output given two remapping image using nearest neighbor as interpolation. |
| 28 | * |
| 29 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 30 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 31 | * |
| 32 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 33 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 34 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 35 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 36 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 37 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 38 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 39 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 40 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 41 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 42 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 43 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 44 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 45 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 46 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 47 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 48 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 49 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 50 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 51 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 52 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 53 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 54 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 55 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 56 | * @param[in] width Width of the input image |
| 57 | * @param[in] height Height of the input image |
| 58 | */ |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 59 | __kernel void remap_nearest_neighbour_nchw( |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | IMAGE_DECLARATION(in), |
| 61 | IMAGE_DECLARATION(out), |
| 62 | IMAGE_DECLARATION(mapx), |
| 63 | IMAGE_DECLARATION(mapy), |
| 64 | const float width, |
| 65 | const float height) |
| 66 | { |
| 67 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 68 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
| 69 | Image mapx = CONVERT_TO_IMAGE_STRUCT(mapx); |
| 70 | Image mapy = CONVERT_TO_IMAGE_STRUCT(mapy); |
| 71 | |
| 72 | float4 mapx_coords = vload4(0, (__global float *)mapx.ptr); |
| 73 | float4 mapy_coords = vload4(0, (__global float *)mapy.ptr); |
| 74 | float8 map_coords = (float8)(mapx_coords.s0, mapy_coords.s0, mapx_coords.s1, mapy_coords.s1, |
| 75 | mapx_coords.s2, mapy_coords.s2, mapx_coords.s3, mapy_coords.s3); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | |
| 77 | vstore4(read_texels4(&in, convert_int8(clamp_to_border(map_coords, width, height))), 0, out.ptr); |
| 78 | } |
| 79 | |
| 80 | /** Performs a remapping of an input image to an output given two remapping image using bilinear as interpolation. |
| 81 | * |
| 82 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 83 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 84 | * |
| 85 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 86 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 87 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 88 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 89 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 90 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 91 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 92 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 93 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 94 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 95 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 96 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 97 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 98 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 99 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 100 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 101 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 102 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 103 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 104 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 105 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 106 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 107 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 108 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 109 | * @param[in] width Width of the input image |
| 110 | * @param[in] height Height of the input image |
| 111 | */ |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 112 | __kernel void remap_bilinear_nchw( |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | IMAGE_DECLARATION(in), |
| 114 | IMAGE_DECLARATION(out), |
| 115 | IMAGE_DECLARATION(mapx), |
| 116 | IMAGE_DECLARATION(mapy), |
| 117 | const float width, |
| 118 | const float height) |
| 119 | { |
| 120 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 121 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
| 122 | Image mapx = CONVERT_TO_IMAGE_STRUCT(mapx); |
| 123 | Image mapy = CONVERT_TO_IMAGE_STRUCT(mapy); |
| 124 | |
| 125 | float4 mapx_coords = vload4(0, (__global float *)mapx.ptr); |
| 126 | float4 mapy_coords = vload4(0, (__global float *)mapy.ptr); |
| 127 | float8 map_coords = (float8)(mapx_coords.s0, mapy_coords.s0, mapx_coords.s1, mapy_coords.s1, |
| 128 | mapx_coords.s2, mapy_coords.s2, mapx_coords.s3, mapy_coords.s3); |
| 129 | |
| 130 | vstore4(bilinear_interpolate(&in, clamp_to_border(map_coords, width, height), width, height), 0, out.ptr); |
| 131 | } |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame^] | 132 | |
| 133 | /** Performs a remapping of an input image to an output given two remapping image using nearest neighbor as interpolation. |
| 134 | * Also applies constant border value, "border_val", if "CONSTANT_BORDER" is set. |
| 135 | * |
| 136 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 137 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 138 | * |
| 139 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 140 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 141 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 142 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 143 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 144 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 145 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 146 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 147 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 148 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 149 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 150 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 151 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 152 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 153 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 154 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 155 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 156 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 157 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 158 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 159 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 160 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 161 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 162 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 163 | * @param[in] width Width of the input image |
| 164 | * @param[in] height Height of the input image |
| 165 | */ |
| 166 | |
| 167 | #if defined(DEPTH_OUT) |
| 168 | |
| 169 | __kernel void remap_nearest_neighbour_nhwc( |
| 170 | TENSOR4D_DECLARATION(in), |
| 171 | TENSOR4D_DECLARATION(out), |
| 172 | TENSOR4D_DECLARATION(mapx), |
| 173 | TENSOR4D_DECLARATION(mapy), |
| 174 | const float width, |
| 175 | const float height |
| 176 | #ifdef CONSTANT_BORDER |
| 177 | , |
| 178 | const DATA_TYPE border_val |
| 179 | #endif // CONSTANT_BORDER |
| 180 | ) |
| 181 | { |
| 182 | Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0); |
| 183 | Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT); |
| 184 | Tensor4D mapx = CONVERT_TO_TENSOR4D_STRUCT(mapx, DEPTH_OUT); |
| 185 | Tensor4D mapy = CONVERT_TO_TENSOR4D_STRUCT(mapy, DEPTH_OUT); |
| 186 | |
| 187 | float mapx_coord = (float) * (__global float *)mapx.ptr; |
| 188 | float mapy_coord = (float) * (__global float *)mapy.ptr; |
| 189 | |
| 190 | #ifdef CONSTANT_BORDER |
| 191 | if(mapx_coord < 0 || mapx_coord > width - 1 || mapy_coord < 0 || mapy_coord > height - 1) |
| 192 | { |
| 193 | *((__global DATA_TYPE *)out.ptr) = border_val; |
| 194 | return; |
| 195 | } |
| 196 | #else // CONSTANT_BORDER |
| 197 | mapx_coord = clamp(mapx_coord, 0.0f, width - 1); |
| 198 | mapy_coord = clamp(mapy_coord, 0.0f, height - 1); |
| 199 | #endif // CONSTANT_BORDER |
| 200 | *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(mapx_coord), convert_int(mapy_coord), (get_global_id(2) / DEPTH_OUT))); |
| 201 | } |
| 202 | |
| 203 | /** Performs a remapping of an input image to an output given two remapping image using bilinear as interpolation. |
| 204 | * Also applies constant border value, "border_val", if "CONSTANT_BORDER" is set. |
| 205 | * |
| 206 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 207 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 208 | * |
| 209 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 210 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 211 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 212 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 213 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 214 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 215 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 216 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 217 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 218 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 219 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 220 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 221 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 222 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 223 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 224 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 225 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 226 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 227 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 228 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 229 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 230 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 231 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 232 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 233 | * @param[in] width Width of the input image |
| 234 | * @param[in] height Height of the input image |
| 235 | */ |
| 236 | __kernel void remap_bilinear_nhwc( |
| 237 | TENSOR4D_DECLARATION(in), |
| 238 | TENSOR4D_DECLARATION(out), |
| 239 | TENSOR4D_DECLARATION(mapx), |
| 240 | TENSOR4D_DECLARATION(mapy), |
| 241 | const float width, |
| 242 | const float height |
| 243 | #ifdef CONSTANT_BORDER |
| 244 | , |
| 245 | const DATA_TYPE border_val |
| 246 | #endif // CONSTANT_BORDER |
| 247 | ) |
| 248 | { |
| 249 | Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0); |
| 250 | Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT); |
| 251 | Tensor4D mapx = CONVERT_TO_TENSOR4D_STRUCT(mapx, DEPTH_OUT); |
| 252 | Tensor4D mapy = CONVERT_TO_TENSOR4D_STRUCT(mapy, DEPTH_OUT); |
| 253 | |
| 254 | float mapx_coord = (float) * (__global float *)mapx.ptr; |
| 255 | float mapy_coord = (float) * (__global float *)mapy.ptr; |
| 256 | |
| 257 | #ifdef CONSTANT_BORDER |
| 258 | if(mapx_coord < 0 || mapx_coord > width - 1 || mapy_coord < 0 || mapy_coord > height - 1) |
| 259 | { |
| 260 | *((__global DATA_TYPE *)out.ptr) = border_val; |
| 261 | return; |
| 262 | } |
| 263 | #endif // CONSTANT_BORDER |
| 264 | |
| 265 | const float new_xf = floor(mapx_coord); |
| 266 | const float new_yf = floor(mapy_coord); |
| 267 | const float clamped_x = clamp(new_xf, 0.0f, width - 1); |
| 268 | const float clamped_x1 = clamp(new_xf + 1, 0.0f, width - 1); |
| 269 | const float clamped_y = clamp(new_yf, 0.0f, height - 1); |
| 270 | const float clamped_y1 = clamp(new_yf + 1, 0.0f, height - 1); |
| 271 | |
| 272 | 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))), |
| 273 | *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))), |
| 274 | *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))), |
| 275 | *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT)))); |
| 276 | |
| 277 | const float a = mapx_coord - new_xf; |
| 278 | const float b = 1.f - a; |
| 279 | const float a1 = mapy_coord - new_yf; |
| 280 | const float b1 = 1.f - a1; |
| 281 | const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1)); |
| 282 | |
| 283 | *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE); |
| 284 | } |
| 285 | |
| 286 | #endif // DEPTH_OUT |