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 | */ |
| 24 | #include "helpers.h" |
| 25 | |
| 26 | #ifndef DATA_TYPE |
| 27 | #define DATA_TYPE short |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 28 | #endif /* DATA_TYPE */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | #ifndef DATA_TYPE_OUT |
| 31 | #define DATA_TYPE_OUT uchar |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 32 | #endif /* DATA_TYPE_OUT */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
| 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 | */ |
| 43 | inline 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 |
| 75 | * @param[in] mat0 Coefficient from the convolution matrix |
| 76 | * @param[in] mat7 Coefficient from the convolution matrix |
| 77 | * @param[in] mat8 Coefficient from the convolution matrix |
| 78 | * @param[in] scale Convolution matrix scale (Sum of the coefficients, or 1 if the sum is 0) |
| 79 | * |
| 80 | * @return a short8 containing 8 convoluted and scaled values. |
| 81 | */ |
| 82 | inline VEC_DATA_TYPE(DATA_TYPE, 8) convolution3x3( |
| 83 | Image *src, |
| 84 | const short mat0, const short mat1, const short mat2, |
| 85 | const short mat3, const short mat4, const short mat5, |
| 86 | const short mat6, const short mat7, const short mat8, uint scale) |
| 87 | { |
| 88 | // Output pixels |
| 89 | VEC_DATA_TYPE(DATA_TYPE, 8) |
| 90 | pixels; |
| 91 | |
| 92 | // Row 0 |
| 93 | pixels = convolution1x3(offset(src, -1, -1), mat0, mat1, mat2); |
| 94 | // Row |
| 95 | pixels += convolution1x3(offset(src, -1, 0), mat3, mat4, mat5); |
| 96 | // Row 2 |
| 97 | pixels += convolution1x3(offset(src, -1, 1), mat6, mat7, mat8); |
| 98 | |
| 99 | // Divide by the scale |
| 100 | return pixels / (VEC_DATA_TYPE(DATA_TYPE, 8))scale; |
| 101 | } |
| 102 | |
| 103 | #ifndef DYNAMIC_MATRIX_CONVOLUTION |
| 104 | |
| 105 | /** Apply a 3x3 static convolution matrix to a single channel U8 input image and output a single channel image. |
| 106 | * |
| 107 | * @attention The matrix coefficients(MAT0, MAT1, ... MAT8, SCALE), DATA_TYPE, and DATA_TYPE_OUT need to be passed at compile time.\n |
| 108 | * e.g. -DMAT0=1 -DMAT2=2, ...-DMAT8=8, -DSCALE=1, -DDATA_TYPE=int, -DDATA_TYPE_OUT=int |
| 109 | * |
| 110 | * @param[in] src_ptr Pointer to the source image |
| 111 | * @param[in] src_stride_x Stride of the source image in X dimension (in bytes) |
| 112 | * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes) |
| 113 | * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes) |
| 114 | * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes) |
| 115 | * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image |
| 116 | * @param[out] dst_ptr Pointer to the destination image. Supported data types: U8, S16 |
| 117 | * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes) |
| 118 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 119 | * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes) |
| 120 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 121 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image |
| 122 | */ |
| 123 | __kernel void convolution3x3_static( |
| 124 | IMAGE_DECLARATION(src), |
| 125 | IMAGE_DECLARATION(dst)) |
| 126 | { |
| 127 | Image src = CONVERT_TO_IMAGE_STRUCT(src); |
| 128 | Image dst = CONVERT_TO_IMAGE_STRUCT(dst); |
| 129 | |
| 130 | VEC_DATA_TYPE(DATA_TYPE, 8) |
| 131 | pixels = convolution3x3(&src, |
| 132 | MAT0, MAT1, MAT2, MAT3, MAT4, MAT5, MAT6, MAT7, MAT8, SCALE); |
| 133 | |
| 134 | // Store the result as is in dst |
| 135 | vstore8(CONVERT_SAT(pixels, VEC_DATA_TYPE(DATA_TYPE_OUT, 8)), 0, (__global DATA_TYPE_OUT *)dst.ptr); |
| 136 | } |
| 137 | |
| 138 | #endif // DYNAMIC_MATRIX_CONVOLUTION |