blob: 7bca567b116999d75761fcbf71313a52a81f1983 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2019 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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.h"
25
26#ifndef DATA_TYPE
27#define DATA_TYPE short
Anthony Barbierac69aa12017-07-03 17:39:37 +010028#endif /* DATA_TYPE */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30#ifndef DATA_TYPE_OUT
31#define DATA_TYPE_OUT uchar
Anthony Barbierac69aa12017-07-03 17:39:37 +010032#endif /* DATA_TYPE_OUT */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34/** Compute a 1D horizontal convolution of size 3 for 8 bytes assuming the input is made of 1 channel of 1 byte (i.e 8 pixels).
35 *
36 * @param[in] left_pixel Pointer to the left pixel.
37 * @param[in] left_coeff Weight of the left pixel
38 * @param[in] middle_coeff Weight of the middle pixel
39 * @param[in] right_coeff Weight of the right pixel
40 *
41 * @return a short8 containing 8 convoluted values.
42 */
43inline VEC_DATA_TYPE(DATA_TYPE, 8) convolution1x3(__global const uchar *left_pixel,
44 const short left_coeff,
45 const short middle_coeff,
46 const short right_coeff)
47{
48 uchar16 temp = vload16(0, left_pixel);
49 VEC_DATA_TYPE(DATA_TYPE, 8)
50 left = CONVERT(temp.s01234567, VEC_DATA_TYPE(DATA_TYPE, 8));
51 VEC_DATA_TYPE(DATA_TYPE, 8)
52 middle = CONVERT(temp.s12345678, VEC_DATA_TYPE(DATA_TYPE, 8));
53 VEC_DATA_TYPE(DATA_TYPE, 8)
54 right = CONVERT(temp.s23456789, VEC_DATA_TYPE(DATA_TYPE, 8));
55
56 return left * (VEC_DATA_TYPE(DATA_TYPE, 8))left_coeff + middle * (VEC_DATA_TYPE(DATA_TYPE, 8))middle_coeff + right * (VEC_DATA_TYPE(DATA_TYPE, 8))right_coeff;
57}
58
59/** Apply a 3x3 convolution matrix to a single channel U8 input image and return the result.
60 *
61 * Convolution matrix layout:
62 *
63 * [ mat0, mat1, mat2 ]\n
64 * [ mat3, mat4, mat5 ]\n
65 * [ mat6, mat7, mat8 ]\n
66 *
67 * @param[in] src A pointer to source Image structure
68 * @param[in] mat0 Coefficient from the convolution matrix
69 * @param[in] mat1 Coefficient from the convolution matrix
70 * @param[in] mat2 Coefficient from the convolution matrix
71 * @param[in] mat3 Coefficient from the convolution matrix
72 * @param[in] mat4 Coefficient from the convolution matrix
73 * @param[in] mat5 Coefficient from the convolution matrix
74 * @param[in] mat6 Coefficient from the convolution matrix
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 * @param[in] mat7 Coefficient from the convolution matrix
76 * @param[in] mat8 Coefficient from the convolution matrix
77 * @param[in] scale Convolution matrix scale (Sum of the coefficients, or 1 if the sum is 0)
78 *
79 * @return a short8 containing 8 convoluted and scaled values.
80 */
81inline VEC_DATA_TYPE(DATA_TYPE, 8) convolution3x3(
82 Image *src,
83 const short mat0, const short mat1, const short mat2,
84 const short mat3, const short mat4, const short mat5,
85 const short mat6, const short mat7, const short mat8, uint scale)
86{
87 // Output pixels
88 VEC_DATA_TYPE(DATA_TYPE, 8)
89 pixels;
90
91 // Row 0
92 pixels = convolution1x3(offset(src, -1, -1), mat0, mat1, mat2);
93 // Row
94 pixels += convolution1x3(offset(src, -1, 0), mat3, mat4, mat5);
95 // Row 2
96 pixels += convolution1x3(offset(src, -1, 1), mat6, mat7, mat8);
97
98 // Divide by the scale
99 return pixels / (VEC_DATA_TYPE(DATA_TYPE, 8))scale;
100}
101
102#ifndef DYNAMIC_MATRIX_CONVOLUTION
103
104/** Apply a 3x3 static convolution matrix to a single channel U8 input image and output a single channel image.
105 *
106 * @attention The matrix coefficients(MAT0, MAT1, ... MAT8, SCALE), DATA_TYPE, and DATA_TYPE_OUT need to be passed at compile time.\n
107 * e.g. -DMAT0=1 -DMAT2=2, ...-DMAT8=8, -DSCALE=1, -DDATA_TYPE=int, -DDATA_TYPE_OUT=int
108 *
109 * @param[in] src_ptr Pointer to the source image
110 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
111 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
112 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
113 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
114 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
115 * @param[out] dst_ptr Pointer to the destination image. Supported data types: U8, S16
116 * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes)
117 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
118 * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes)
119 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
120 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image
121 */
122__kernel void convolution3x3_static(
123 IMAGE_DECLARATION(src),
124 IMAGE_DECLARATION(dst))
125{
126 Image src = CONVERT_TO_IMAGE_STRUCT(src);
127 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
128
129 VEC_DATA_TYPE(DATA_TYPE, 8)
130 pixels = convolution3x3(&src,
131 MAT0, MAT1, MAT2, MAT3, MAT4, MAT5, MAT6, MAT7, MAT8, SCALE);
132
133 // Store the result as is in dst
134 vstore8(CONVERT_SAT(pixels, VEC_DATA_TYPE(DATA_TYPE_OUT, 8)), 0, (__global DATA_TYPE_OUT *)dst.ptr);
135}
136
137#endif // DYNAMIC_MATRIX_CONVOLUTION