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