Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016, 2017 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 | /** Returns the perspective matrix */ |
| 28 | inline const float16 build_perspective_mtx() |
| 29 | { |
| 30 | return (float16)(MAT0, MAT1, MAT2, MAT3, MAT4, MAT5, MAT6, MAT7, MAT8, 0, 0, 0, (float4)0); |
| 31 | } |
| 32 | |
| 33 | /** Transforms four 2D coordinates using the formula: |
| 34 | * |
| 35 | * x0 = M[1][1] * x + M[1][2] * y + M[1][3] |
| 36 | * y0 = M[2][1] * x + M[2][2] * y + M[2][3] |
| 37 | * z0 = M[3][1] * x + M[3][2] * y + M[3][3] |
| 38 | * |
| 39 | * (x0/z0,y0/z0) |
| 40 | * |
| 41 | * @param[in] coord 2D coordinate to transform. |
| 42 | * @param[in] mtx perspective matrix |
| 43 | * |
| 44 | * @return a vector float8 containing four 2D transformed values. |
| 45 | */ |
| 46 | inline const float8 apply_perspective_transform(const float2 coord, const float16 mtx) |
| 47 | { |
| 48 | const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0); |
| 49 | // transform [z,z+1,z+2,z+3] |
| 50 | const float4 z = (float4)mad(in_x_coords, (float4)(mtx.s2), mad((float4)(coord.s1), (float4)(mtx.s5), (float4)(mtx.s8))); |
| 51 | // NOTE: Do not multiply x&y by 1.f/Z as this will result in loss of accuracy and mismatches with VX reference implementation |
| 52 | // transform [x,x+1,x+2,x+3] |
| 53 | const float4 new_x = (float4)mad(in_x_coords, (float4)(mtx.s0), mad((float4)(coord.s1), (float4)(mtx.s3), (float4)(mtx.s6))) / z; |
| 54 | // transform [y,y+1,y+2,y+3] |
| 55 | const float4 new_y = (float4)mad(in_x_coords, (float4)(mtx.s1), mad((float4)(coord.s1), (float4)(mtx.s4), (float4)(mtx.s7))) / z; |
| 56 | return (float8)(new_x.s0, new_y.s0, new_x.s1, new_y.s1, new_x.s2, new_y.s2, new_x.s3, new_y.s3); |
| 57 | } |
| 58 | |
| 59 | /** Performs perspective transformation on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel U8. |
| 60 | * |
| 61 | * This kernel performs perspective transform with a 3x3 Matrix M with this method of pixel coordinate translation: |
| 62 | * x0 = M[1][1] * x + M[1][2] * y + M[1][3] |
| 63 | * y0 = M[2][1] * x + M[2][2] * y + M[2][3] |
| 64 | * z0 = M[3][1] * x + M[3][2] * y + M[3][3] |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 65 | * |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | * output(x,y) = input(x0/z0,y0/z0) |
| 67 | * |
| 68 | * @attention The matrix coefficients need to be passed at compile time:\n |
| 69 | * const char build_options [] = "-DMAT0=1 -DMAT1=2 -DMAT2=3 -DMAT3=4 -DMAT4=5 -DMAT5=6 -DMAT6=7 -DMAT7=8 -DMAT8=9"\n |
| 70 | * clBuildProgram( program, 0, NULL, build_options, NULL, NULL); |
| 71 | * |
| 72 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 73 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 74 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 75 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 76 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 77 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 78 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 79 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 80 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 81 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 82 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 83 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 84 | * @param[in] width Width of the destination image |
| 85 | * @param[in] height Height of the destination image |
| 86 | */ |
| 87 | __kernel void warp_perspective_nearest_neighbour( |
| 88 | IMAGE_DECLARATION(in), |
| 89 | IMAGE_DECLARATION(out), |
| 90 | const int width, |
| 91 | const int height) |
| 92 | { |
| 93 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 94 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
Isabella Gottardi | f9bae2e | 2017-07-28 17:24:08 +0100 | [diff] [blame] | 95 | vstore4(read_texels4(&in, convert_int8_rtn(clamp_to_border(apply_perspective_transform(get_current_coords(), build_perspective_mtx()), width, height))), 0, out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** Performs a perspective transform on an image interpolating with the BILINEAR method. Input and output are single channel U8. |
| 99 | * |
| 100 | * @attention The matrix coefficients need to be passed at compile time:\n |
| 101 | * const char build_options [] = "-DMAT0=1 -DMAT1=2 -DMAT2=3 -DMAT3=4 -DMAT4=5 -DMAT5=6 -DMAT6=7 -DMAT7=8 -DMAT8=9"\n |
| 102 | * clBuildProgram( program, 0, NULL, build_options, NULL, NULL); |
| 103 | * |
| 104 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 105 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 106 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 107 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 108 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 109 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 110 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 111 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 112 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 113 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 114 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 115 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 116 | * @param[in] width Width of the destination image |
| 117 | * @param[in] height Height of the destination image |
| 118 | */ |
| 119 | __kernel void warp_perspective_bilinear( |
| 120 | IMAGE_DECLARATION(in), |
| 121 | IMAGE_DECLARATION(out), |
| 122 | const int width, |
| 123 | const int height) |
| 124 | { |
| 125 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 126 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
Isabella Gottardi | f9bae2e | 2017-07-28 17:24:08 +0100 | [diff] [blame] | 127 | vstore4(bilinear_interpolate(&in, apply_perspective_transform(get_current_coords(), build_perspective_mtx()), width, height), 0, out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | } |