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 | #include "warp_helpers.h" |
| 26 | |
| 27 | /** Returns a vector of floats contaning the matrix coefficients. */ |
| 28 | inline const float8 build_affine_mtx() |
| 29 | { |
| 30 | return (float8)(MAT0, MAT1, MAT2, MAT3, MAT4, MAT5, 0, 0); |
| 31 | } |
| 32 | |
| 33 | /** Transforms 4 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 | * |
| 38 | * @param[in] coord 2D coordinate to transform. |
| 39 | * @param[in] mtx affine matrix |
| 40 | * |
| 41 | * @return a int8 containing 4 2D transformed values. |
| 42 | */ |
| 43 | inline const float8 apply_affine_transform(const float2 coord, const float8 mtx) |
| 44 | { |
| 45 | const float4 in_x_coords = (float4)(coord.s0, 1 + coord.s0, 2 + coord.s0, 3 + coord.s0); |
| 46 | // transform [x,x+1,x+2,x+3] |
| 47 | const float4 new_x = mad(/*A*/ in_x_coords, (float4)(mtx.s0) /*B*/, mad((float4)(coord.s1), (float4)(mtx.s2), (float4)(mtx.s4))); |
| 48 | // transform [y,y+1,y+2,y+3] |
| 49 | const float4 new_y = mad(in_x_coords, (float4)(mtx.s1), mad((float4)(coord.s1), (float4)(mtx.s3), (float4)(mtx.s5))); |
| 50 | 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); |
| 51 | } |
| 52 | |
| 53 | /** Performs an affine transform on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel U8. |
| 54 | * |
| 55 | * This kernel performs an affine transform with a 2x3 Matrix M with this method of pixel coordinate translation: |
| 56 | * x0 = M[1][1] * x + M[1][2] * y + M[1][3] |
| 57 | * y0 = M[2][1] * x + M[2][2] * y + M[2][3] |
| 58 | * output(x,y) = input(x0,y0) |
| 59 | * |
| 60 | * @attention The matrix coefficients need to be passed at compile time:\n |
| 61 | * const char build_options [] = "-DMAT0=1 -DMAT1=2 -DMAT2=1 -DMAT3=2 -DMAT4=4 -DMAT5=2 "\n |
| 62 | * clBuildProgram( program, 0, NULL, build_options, NULL, NULL); |
| 63 | * |
| 64 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 65 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 66 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 67 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 68 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 69 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 70 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 71 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 72 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 73 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 74 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 75 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 76 | * @param[in] width Width of the destination image |
| 77 | * @param[in] height Height of the destination image |
| 78 | */ |
| 79 | __kernel void warp_affine_nearest_neighbour( |
| 80 | IMAGE_DECLARATION(in), |
| 81 | IMAGE_DECLARATION(out), |
| 82 | const int width, |
| 83 | const int height) |
| 84 | { |
| 85 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 86 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 87 | vstore4(read_texels4(&in, convert_int8_rtn(clamp_to_border(apply_affine_transform(get_current_coords(), build_affine_mtx()), width, height))), 0, out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** Performs an affine transform on an image interpolating with the BILINEAR method. Input and output are single channel U8. |
| 91 | * |
| 92 | * @attention The matrix coefficients need to be passed at compile time:\n |
| 93 | * const char build_options [] = "-DMAT0=1 -DMAT1=2 -DMAT2=1 -DMAT3=2 -DMAT4=4 -DMAT5=2 "\n |
| 94 | * clBuildProgram( program, 0, NULL, build_options, NULL, NULL); |
| 95 | * |
| 96 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 97 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 98 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 99 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 100 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 101 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 102 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 103 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 104 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 105 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 106 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 107 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 108 | * @param[in] width Width of the destination image |
| 109 | * @param[in] height Height of the destination image |
| 110 | */ |
| 111 | __kernel void warp_affine_bilinear( |
| 112 | IMAGE_DECLARATION(in), |
| 113 | IMAGE_DECLARATION(out), |
| 114 | const int width, |
| 115 | const int height) |
| 116 | { |
| 117 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 118 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
Isabella Gottardi | 83be745 | 2017-08-29 13:47:03 +0100 | [diff] [blame] | 119 | vstore4(bilinear_interpolate(&in, apply_affine_transform(get_current_coords(), build_affine_mtx()), width, height), 0, out.ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | } |