1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved. 14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. 15 // Third party copyrights are property of their respective owners. 16 // 17 // @Authors 18 // Zhang Ying, zhangying913 (at) gmail.com 19 // 20 // Redistribution and use in source and binary forms, with or without modification, 21 // are permitted provided that the following conditions are met: 22 // 23 // * Redistribution's of source code must retain the above copyright notice, 24 // this list of conditions and the following disclaimer. 25 // 26 // * Redistribution's in binary form must reproduce the above copyright notice, 27 // this list of conditions and the following disclaimer in the documentation 28 // and/or other materials provided with the distribution. 29 // 30 // * The name of the copyright holders may not be used to endorse or promote products 31 // derived from this software without specific prior written permission. 32 // 33 // This software is provided by the copyright holders and contributors as is and 34 // any express or implied warranties, including, but not limited to, the implied 35 // warranties of merchantability and fitness for a particular purpose are disclaimed. 36 // In no event shall the Intel Corporation or contributors be liable for any direct, 37 // indirect, incidental, special, exemplary, or consequential damages 38 // (including, but not limited to, procurement of substitute goods or services; 39 // loss of use, data, or profits; or business interruption) however caused 40 // and on any theory of liability, whether in contract, strict liability, 41 // or tort (including negligence or otherwise) arising in any way out of 42 // the use of this software, even if advised of the possibility of such damage. 43 // 44 //M*/ 45 46 #ifdef DOUBLE_SUPPORT 47 #ifdef cl_amd_fp64 48 #pragma OPENCL EXTENSION cl_amd_fp64:enable 49 #elif defined (cl_khr_fp64) 50 #pragma OPENCL EXTENSION cl_khr_fp64:enable 51 #endif 52 #define CT double 53 #else 54 #define CT float 55 #endif 56 57 #define INTER_BITS 5 58 #define INTER_TAB_SIZE (1 << INTER_BITS) 59 #define INTER_SCALE 1.f / INTER_TAB_SIZE 60 #define AB_BITS max(10, (int)INTER_BITS) 61 #define AB_SCALE (1 << AB_BITS) 62 #define INTER_REMAP_COEF_BITS 15 63 #define INTER_REMAP_COEF_SCALE (1 << INTER_REMAP_COEF_BITS) 64 65 #define noconvert 66 67 #ifndef ST 68 #define ST T 69 #endif 70 71 #if cn != 3 72 #define loadpix(addr) *(__global const T*)(addr) 73 #define storepix(val, addr) *(__global T*)(addr) = val 74 #define scalar scalar_ 75 #define pixsize (int)sizeof(T) 76 #else 77 #define loadpix(addr) vload3(0, (__global const T1*)(addr)) 78 #define storepix(val, addr) vstore3(val, 0, (__global T1*)(addr)) 79 #ifdef INTER_NEAREST 80 #define scalar (T)(scalar_.x, scalar_.y, scalar_.z) 81 #else 82 #define scalar (WT)(scalar_.x, scalar_.y, scalar_.z) 83 #endif 84 #define pixsize ((int)sizeof(T1)*3) 85 #endif 86 87 #ifdef INTER_NEAREST 88 89 __kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, 90 __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, 91 __constant CT * M, ST scalar_) 92 { 93 int dx = get_global_id(0); 94 int dy = get_global_id(1); 95 96 if (dx < dst_cols && dy < dst_rows) 97 { 98 CT X0 = M[0] * dx + M[1] * dy + M[2]; 99 CT Y0 = M[3] * dx + M[4] * dy + M[5]; 100 CT W = M[6] * dx + M[7] * dy + M[8]; 101 W = W != 0.0f ? 1.f / W : 0.0f; 102 short sx = convert_short_sat_rte(X0*W); 103 short sy = convert_short_sat_rte(Y0*W); 104 105 int dst_index = mad24(dy, dst_step, dx * pixsize + dst_offset); 106 107 if (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) 108 { 109 int src_index = mad24(sy, src_step, sx * pixsize + src_offset); 110 storepix(loadpix(srcptr + src_index), dstptr + dst_index); 111 } 112 else 113 storepix(scalar, dstptr + dst_index); 114 } 115 } 116 117 #elif defined INTER_LINEAR 118 119 __kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, 120 __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, 121 __constant CT * M, ST scalar_) 122 { 123 int dx = get_global_id(0); 124 int dy = get_global_id(1); 125 126 if (dx < dst_cols && dy < dst_rows) 127 { 128 CT X0 = M[0] * dx + M[1] * dy + M[2]; 129 CT Y0 = M[3] * dx + M[4] * dy + M[5]; 130 CT W = M[6] * dx + M[7] * dy + M[8]; 131 W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f; 132 int X = rint(X0 * W), Y = rint(Y0 * W); 133 134 short sx = convert_short_sat(X >> INTER_BITS); 135 short sy = convert_short_sat(Y >> INTER_BITS); 136 short ay = (short)(Y & (INTER_TAB_SIZE - 1)); 137 short ax = (short)(X & (INTER_TAB_SIZE - 1)); 138 139 WT v0 = (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) ? 140 convertToWT(loadpix(srcptr + mad24(sy, src_step, src_offset + sx * pixsize))) : scalar; 141 WT v1 = (sx+1 >= 0 && sx+1 < src_cols && sy >= 0 && sy < src_rows) ? 142 convertToWT(loadpix(srcptr + mad24(sy, src_step, src_offset + (sx+1) * pixsize))) : scalar; 143 WT v2 = (sx >= 0 && sx < src_cols && sy+1 >= 0 && sy+1 < src_rows) ? 144 convertToWT(loadpix(srcptr + mad24(sy+1, src_step, src_offset + sx * pixsize))) : scalar; 145 WT v3 = (sx+1 >= 0 && sx+1 < src_cols && sy+1 >= 0 && sy+1 < src_rows) ? 146 convertToWT(loadpix(srcptr + mad24(sy+1, src_step, src_offset + (sx+1) * pixsize))) : scalar; 147 148 float taby = 1.f/INTER_TAB_SIZE*ay; 149 float tabx = 1.f/INTER_TAB_SIZE*ax; 150 151 int dst_index = mad24(dy, dst_step, dst_offset + dx * pixsize); 152 153 #if depth <= 4 154 int itab0 = convert_short_sat_rte( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE ); 155 int itab1 = convert_short_sat_rte( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE ); 156 int itab2 = convert_short_sat_rte( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE ); 157 int itab3 = convert_short_sat_rte( taby*tabx * INTER_REMAP_COEF_SCALE ); 158 159 WT val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3; 160 storepix(convertToT((val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS), dstptr + dst_index); 161 #else 162 float tabx2 = 1.0f - tabx, taby2 = 1.0f - taby; 163 WT val = v0 * tabx2 * taby2 + v1 * tabx * taby2 + v2 * tabx2 * taby + v3 * tabx * taby; 164 storepix(convertToT(val), dstptr + dst_index); 165 #endif 166 } 167 } 168 169 #elif defined INTER_CUBIC 170 171 inline void interpolateCubic( float x, float* coeffs ) 172 { 173 const float A = -0.75f; 174 175 coeffs[0] = ((A*(x + 1.f) - 5.0f*A)*(x + 1.f) + 8.0f*A)*(x + 1.f) - 4.0f*A; 176 coeffs[1] = ((A + 2.f)*x - (A + 3.f))*x*x + 1.f; 177 coeffs[2] = ((A + 2.f)*(1.f - x) - (A + 3.f))*(1.f - x)*(1.f - x) + 1.f; 178 coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2]; 179 } 180 181 __kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, 182 __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, 183 __constant CT * M, ST scalar_) 184 { 185 int dx = get_global_id(0); 186 int dy = get_global_id(1); 187 188 if (dx < dst_cols && dy < dst_rows) 189 { 190 CT X0 = M[0] * dx + M[1] * dy + M[2]; 191 CT Y0 = M[3] * dx + M[4] * dy + M[5]; 192 CT W = M[6] * dx + M[7] * dy + M[8]; 193 W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f; 194 int X = rint(X0 * W), Y = rint(Y0 * W); 195 196 short sx = convert_short_sat(X >> INTER_BITS) - 1; 197 short sy = convert_short_sat(Y >> INTER_BITS) - 1; 198 short ay = (short)(Y & (INTER_TAB_SIZE-1)); 199 short ax = (short)(X & (INTER_TAB_SIZE-1)); 200 201 WT v[16]; 202 #pragma unroll 203 for (int y = 0; y < 4; y++) 204 #pragma unroll 205 for (int x = 0; x < 4; x++) 206 v[mad24(y, 4, x)] = (sx+x >= 0 && sx+x < src_cols && sy+y >= 0 && sy+y < src_rows) ? 207 convertToWT(loadpix(srcptr + mad24(sy+y, src_step, src_offset + (sx+x) * pixsize))) : scalar; 208 209 float tab1y[4], tab1x[4]; 210 211 float ayy = INTER_SCALE * ay; 212 float axx = INTER_SCALE * ax; 213 interpolateCubic(ayy, tab1y); 214 interpolateCubic(axx, tab1x); 215 216 int dst_index = mad24(dy, dst_step, dst_offset + dx * pixsize); 217 218 WT sum = (WT)(0); 219 #if depth <= 4 220 int itab[16]; 221 222 #pragma unroll 223 for (int i = 0; i < 16; i++) 224 itab[i] = rint(tab1y[(i>>2)] * tab1x[(i&3)] * INTER_REMAP_COEF_SCALE); 225 226 #pragma unroll 227 for (int i = 0; i < 16; i++) 228 sum += v[i] * itab[i]; 229 storepix(convertToT( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ), dstptr + dst_index); 230 #else 231 #pragma unroll 232 for (int i = 0; i < 16; i++) 233 sum += v[i] * tab1y[(i>>2)] * tab1x[(i&3)]; 234 storepix(convertToT( sum ), dstptr + dst_index); 235 #endif 236 } 237 } 238 239 #endif 240