Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Michele Di Giorgio | 6c92834 | 2017-06-22 16:55:57 +0100 | [diff] [blame] | 24 | #include "fixed_point.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | #include "helpers.h" |
| 26 | |
steniu01 | 0c7614f | 2017-06-23 17:00:26 +0100 | [diff] [blame^] | 27 | #if defined(FIXED_POINT_POSITION) |
| 28 | |
| 29 | #include "fixed_point.h" |
| 30 | |
| 31 | #endif /* FIXED_POINT_POSITION */ |
| 32 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | /** Fill N pixel of the padding edge of a single channel image by replicating the closest valid pixel. |
| 34 | * |
| 35 | * @attention The DATA_TYPE needs to be passed at the compile time. |
| 36 | * e.g. -DDATA_TYPE=int |
| 37 | * |
| 38 | * @attention The border size for top, bottom, left, right needs to be passed at the compile time. |
| 39 | * e.g. --DBORDER_SIZE_TOP=0 -DBORDER_SIZE_BOTTOM=2 -DBORDER_SIZE_LEFT=0 -DBORDER_SIZE_RIGHT=2 |
| 40 | * |
| 41 | * @param[in,out] buf_ptr Pointer to the source image. Supported data types: U8, U16, S16, U32, S32, F32 |
| 42 | * @param[in] buf_stride_x Stride of the source image in X dimension (in bytes) |
| 43 | * @param[in] buf_step_x buf_stride_x * number of elements along X processed per workitem(in bytes) |
| 44 | * @param[in] buf_stride_y Stride of the source image in Y dimension (in bytes) |
| 45 | * @param[in] buf_step_y buf_stride_y * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 46 | * @param[in] buf_stride_z Stride between images if batching images (in bytes) |
| 47 | * @param[in] buf_step_z buf_stride_z * number of elements along Z processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | * @param[in] buf_offset_first_element_in_bytes The offset of the first element in the source image |
| 49 | * @param[in] width Width of the valid region of the image |
| 50 | * @param[in] height Height of the valid region of the image |
| 51 | * @param[in] start_pos XY coordinate indicating the start point of the valid region |
| 52 | */ |
| 53 | __kernel void fill_image_borders_replicate( |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 54 | TENSOR3D_DECLARATION(buf), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | uint width, |
| 56 | uint height, |
| 57 | int2 start_pos) |
| 58 | { |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 59 | Image buf = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(buf); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | |
| 61 | // Update pointer to point to the starting point of the valid region |
| 62 | buf.ptr += start_pos.y * buf.stride_y + start_pos.x * buf.stride_x; |
| 63 | |
| 64 | const int total_width = BORDER_SIZE_LEFT + width + BORDER_SIZE_RIGHT; |
| 65 | const int gid0 = get_global_id(0); |
| 66 | const int gidH = gid0 - total_width; |
| 67 | const int gidW = gid0 - BORDER_SIZE_LEFT; |
| 68 | |
| 69 | if(gidH >= 0) |
| 70 | { |
| 71 | // Handle left border |
| 72 | DATA_TYPE left_val = *(__global DATA_TYPE *)offset(&buf, 0, gidH); |
| 73 | for(int i = -BORDER_SIZE_LEFT; i < 0; ++i) |
| 74 | { |
| 75 | *(__global DATA_TYPE *)offset(&buf, i, gidH) = left_val; |
| 76 | } |
| 77 | // Handle right border |
| 78 | DATA_TYPE right_val = *(__global DATA_TYPE *)offset(&buf, width - 1, gidH); |
| 79 | for(int i = 0; i < BORDER_SIZE_RIGHT; ++i) |
| 80 | { |
| 81 | *(__global DATA_TYPE *)offset(&buf, width + i, gidH) = right_val; |
| 82 | } |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | // Get value for corners |
| 87 | int val_idx = gidW; |
| 88 | if(gidW < 0 || gidW > (width - 1)) |
| 89 | { |
| 90 | val_idx = gidW < 0 ? 0 : width - 1; |
| 91 | } |
| 92 | |
| 93 | // Handle top border |
| 94 | DATA_TYPE top_val = *(__global DATA_TYPE *)offset(&buf, val_idx, 0); |
| 95 | for(int i = -BORDER_SIZE_TOP; i < 0; ++i) |
| 96 | { |
| 97 | *(__global DATA_TYPE *)offset(&buf, gidW, i) = top_val; |
| 98 | } |
| 99 | // Handle bottom border |
| 100 | DATA_TYPE bottom_val = *(__global DATA_TYPE *)offset(&buf, val_idx, height - 1); |
| 101 | for(int i = 0; i < BORDER_SIZE_BOTTOM; ++i) |
| 102 | { |
| 103 | *(__global DATA_TYPE *)offset(&buf, gidW, height + i) = bottom_val; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** Fill N pixels of the padding edge of a single channel image with a constant value. |
| 109 | * |
| 110 | * @attention The DATA_TYPE needs to be passed at the compile time. |
| 111 | * e.g. -DDATA_TYPE=int |
| 112 | * |
| 113 | * @attention The border size for top, bottom, left, right needs to be passed at the compile time. |
| 114 | * e.g. --DBORDER_SIZE_TOP=0 -DBORDER_SIZE_BOTTOM=2 -DBORDER_SIZE_LEFT=0 -DBORDER_SIZE_RIGHT=2 |
| 115 | * |
| 116 | * @param[out] buf_ptr Pointer to the source image. Supported data types: U8, U16, S16, U32, S32, F32 |
| 117 | * @param[in] buf_stride_x Stride of the source image in X dimension (in bytes) |
| 118 | * @param[in] buf_step_x buf_stride_x * number of elements along X processed per workitem(in bytes) |
| 119 | * @param[in] buf_stride_y Stride of the source image in Y dimension (in bytes) |
| 120 | * @param[in] buf_step_y buf_stride_y * number of elements along Y processed per workitem(in bytes) |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 121 | * @param[in] buf_stride_z Stride between images if batching images (in bytes) |
| 122 | * @param[in] buf_step_z buf_stride_z * number of elements along Z processed per workitem(in bytes) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | * @param[in] buf_offset_first_element_in_bytes The offset of the first element in the source image |
| 124 | * @param[in] width Width of the valid region of the image |
| 125 | * @param[in] height Height of the valid region of the image |
| 126 | * @param[in] start_pos XY coordinate indicating the start point of the valid region |
| 127 | * @param[in] constant_value Constant value to use to fill the edges |
| 128 | */ |
| 129 | __kernel void fill_image_borders_constant( |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 130 | TENSOR3D_DECLARATION(buf), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | uint width, |
| 132 | uint height, |
| 133 | int2 start_pos, |
| 134 | DATA_TYPE constant_value) |
| 135 | { |
Anthony Barbier | 7ff47a3 | 2017-07-11 16:54:04 +0100 | [diff] [blame] | 136 | Image buf = CONVERT_TENSOR3D_TO_IMAGE_STRUCT_NO_STEP(buf); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | |
| 138 | // Update pointer to point to the starting point of the valid region |
| 139 | buf.ptr += start_pos.y * buf.stride_y + start_pos.x * buf.stride_x; |
| 140 | |
| 141 | const int total_width = BORDER_SIZE_LEFT + width + BORDER_SIZE_RIGHT; |
| 142 | const int gid0 = get_global_id(0); |
| 143 | const int gidH = gid0 - total_width; |
| 144 | const int gidW = gid0 - BORDER_SIZE_LEFT; |
| 145 | |
| 146 | if(gidH >= 0) |
| 147 | { |
| 148 | // Handle left border |
| 149 | for(int i = -BORDER_SIZE_LEFT; i < 0; ++i) |
| 150 | { |
| 151 | *(__global DATA_TYPE *)offset(&buf, i, gidH) = constant_value; |
| 152 | } |
| 153 | // Handle right border |
| 154 | for(int i = 0; i < BORDER_SIZE_RIGHT; ++i) |
| 155 | { |
| 156 | *(__global DATA_TYPE *)offset(&buf, width + i, gidH) = constant_value; |
| 157 | } |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | // Handle top border |
| 162 | for(int i = -BORDER_SIZE_TOP; i < 0; ++i) |
| 163 | { |
| 164 | *(__global DATA_TYPE *)offset(&buf, gidW, i) = constant_value; |
| 165 | } |
| 166 | // Handle bottom border |
| 167 | for(int i = 0; i < BORDER_SIZE_BOTTOM; ++i) |
| 168 | { |
| 169 | *(__global DATA_TYPE *)offset(&buf, gidW, height + i) = constant_value; |
| 170 | } |
| 171 | } |
| 172 | } |