From 55af7857b903db6f07b9a61db6f391c6b3e15c36 Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Fri, 29 Nov 2013 19:16:34 +0400 Subject: [PATCH 1/3] added cv::warpPerspective to T-API --- modules/core/include/opencv2/core/ocl.hpp | 2 +- modules/core/src/ocl.cpp | 5 +- modules/imgproc/src/imgwarp.cpp | 62 +- modules/imgproc/src/opencl/warp_affine.cl | 761 ++++++++++++++++++ .../imgproc/src/opencl/warp_perspective.cl | 223 +++++ modules/imgproc/test/ocl/test_warp.cpp | 114 ++- 6 files changed, 1158 insertions(+), 9 deletions(-) create mode 100644 modules/imgproc/src/opencl/warp_affine.cl create mode 100644 modules/imgproc/src/opencl/warp_perspective.cl diff --git a/modules/core/include/opencv2/core/ocl.hpp b/modules/core/include/opencv2/core/ocl.hpp index f50ed378f..f2535940f 100644 --- a/modules/core/include/opencv2/core/ocl.hpp +++ b/modules/core/include/opencv2/core/ocl.hpp @@ -286,7 +286,7 @@ public: Kernel(); Kernel(const char* kname, const Program& prog); Kernel(const char* kname, const ProgramSource2& prog, - const String& buildopts, String* errmsg=0); + const String& buildopts = String(), String* errmsg=0); ~Kernel(); Kernel(const Kernel& k); Kernel& operator = (const Kernel& k); diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 64460efb0..8a0cc279a 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -1893,7 +1893,7 @@ Context2& Context2::getDefault() // First, try to retrieve existing context of the same type. // In its turn, Platform::getContext() may call Context2::create() // if there is no such context. - ctx.create(Device::TYPE_ACCELERATOR); + ctx.create(Device::TYPE_CPU); if(!ctx.p) ctx.create(Device::TYPE_DGPU); if(!ctx.p) @@ -2041,6 +2041,7 @@ struct Kernel::Impl cl_int retval = 0; handle = ph != 0 ? clCreateKernel(ph, kname, &retval) : 0; + printf("kernel creation error code: %d\n", retval); for( int i = 0; i < MAX_ARRS; i++ ) u[i] = 0; haveTempDstUMats = false; @@ -2218,7 +2219,7 @@ int Kernel::set(int i, const KernelArg& arg) else if( arg.m->dims <= 2 ) { UMat2D u2d(*arg.m); - clSetKernelArg(p->handle, (cl_uint)i, sizeof(h), &h); + clSetKernelArg(p->handle, (cl_uint)i, sizeof(h), &h)); clSetKernelArg(p->handle, (cl_uint)(i+1), sizeof(u2d.step), &u2d.step); clSetKernelArg(p->handle, (cl_uint)(i+2), sizeof(u2d.offset), &u2d.offset); i += 3; diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index 15d7c6a87..e5383be04 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -4030,16 +4030,76 @@ private: }; #endif +static bool ocl_warpPerspective(InputArray _src, OutputArray _dst, InputArray _M0, + Size dsize, int flags, int borderType, const Scalar& borderValue) +{ + int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), wdepth = depth; + double doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; + + int interpolation = flags & INTER_MAX; + if( interpolation == INTER_AREA ) + interpolation = INTER_LINEAR; + + if ( !(borderType == cv::BORDER_CONSTANT && + (interpolation == cv::INTER_NEAREST || interpolation == cv::INTER_LINEAR || interpolation == cv::INTER_CUBIC)) || + (!doubleSupport && depth == CV_64F) || cn > 4 || cn == 3) + return false; + + UMat src = _src.getUMat(), M0; + _dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() ); + UMat dst = _dst.getUMat(); + + double M[9]; + Mat matM(3, 3, doubleSupport ? CV_64F : CV_32F, M), M1 = _M0.getMat(); + CV_Assert( (M1.type() == CV_32F || M1.type() == CV_64F) && M1.rows == 3 && M1.cols == 3 ); + M1.convertTo(matM, matM.type()); + if( !(flags & WARP_INVERSE_MAP) ) + invert(matM, matM); + matM.copyTo(M0); + + const char * const interpolationMap[3] = { "NEAREST", "LINEAR", "CUBIC" }; + ocl::Kernel k; + + if (interpolation == INTER_NEAREST) + { + k.create("warpPerspective", ocl::imgproc::warp_perspective_oclsrc, + format("-D INTER_NEAREST -D T=%s%s", ocl::typeToStr(type), + doubleSupport ? " -D DOUBLE_SUPPORT" : "")); + } + else + { + char cvt[2][50]; + wdepth = std::max(CV_32S, depth); + k.create("warpPerspective", ocl::imgproc::warp_perspective_oclsrc, + format("-D INTER_%s -D T=%s -D WT=%s -D depth=%d -D convertToWT=%s -D convertToT=%s%s", + interpolationMap[interpolation], ocl::typeToStr(type), + ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), depth, + ocl::convertTypeStr(depth, wdepth, cn, cvt[0]), + ocl::convertTypeStr(wdepth, depth, cn, cvt[1]), + doubleSupport ? " -D DOUBLE_SUPPORT" : "")); + } + + k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst), + ocl::KernelArg::PtrOnly(M0), ocl::KernelArg::Constant(Mat(1, 1, CV_MAKE_TYPE(wdepth, cn), borderValue))); + + size_t globalThreads[2] = { dst.cols, dst.rows }; + return k.run(2, globalThreads, NULL, false); +} + } void cv::warpPerspective( InputArray _src, OutputArray _dst, InputArray _M0, Size dsize, int flags, int borderType, const Scalar& borderValue ) { + CV_Assert( _src.total() > 0 ); + + if (ocl::useOpenCL() && _dst.isUMat() && ocl_warpPerspective(_src, _dst, _M0, dsize, flags, borderType, borderValue)) + return; + Mat src = _src.getMat(), M0 = _M0.getMat(); _dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() ); Mat dst = _dst.getMat(); - CV_Assert( src.cols > 0 && src.rows > 0 ); if( dst.data == src.data ) src = src.clone(); diff --git a/modules/imgproc/src/opencl/warp_affine.cl b/modules/imgproc/src/opencl/warp_affine.cl new file mode 100644 index 000000000..27f99e005 --- /dev/null +++ b/modules/imgproc/src/opencl/warp_affine.cl @@ -0,0 +1,761 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved. +// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// @Authors +// Zhang Ying, zhangying913@gmail.com +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors as is and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + + +//warpAffine kernel +//support data types: CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4, and three interpolation methods: NN, Linear, Cubic. + +#ifdef DOUBLE_SUPPORT +#ifdef cl_amd_fp64 +#pragma OPENCL EXTENSION cl_amd_fp64:enable +#elif defined (cl_khr_fp64) +#pragma OPENCL EXTENSION cl_khr_fp64:enable +#endif +typedef double F; +typedef double4 F4; +#define convert_F4 convert_double4 +#else +typedef float F; +typedef float4 F4; +#define convert_F4 convert_float4 +#endif + +#define INTER_BITS 5 +#define INTER_TAB_SIZE (1 << INTER_BITS) +#define INTER_SCALE 1.f/INTER_TAB_SIZE +#define AB_BITS max(10, (int)INTER_BITS) +#define AB_SCALE (1 << AB_BITS) +#define INTER_REMAP_COEF_BITS 15 +#define INTER_REMAP_COEF_SCALE (1 << INTER_REMAP_COEF_BITS) + +inline void interpolateCubic( float x, float* coeffs ) +{ + const float A = -0.75f; + + coeffs[0] = ((A*(x + 1.f) - 5.0f*A)*(x + 1.f) + 8.0f*A)*(x + 1.f) - 4.0f*A; + coeffs[1] = ((A + 2.f)*x - (A + 3.f))*x*x + 1.f; + coeffs[2] = ((A + 2.f)*(1.f - x) - (A + 3.f))*(1.f - x)*(1.f - x) + 1.f; + coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2]; +} + + +/**********************************************8UC1********************************************* +***********************************************************************************************/ +__kernel void warpAffineNN_C1_D0(__global uchar const * restrict src, __global uchar * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + dx = (dx<<2) - (dst_offset&3); + + int round_delta = (AB_SCALE>>1); + + int4 X, Y; + int4 sx, sy; + int4 DX = (int4)(dx, dx+1, dx+2, dx+3); + DX = (DX << AB_BITS); + F4 M0DX, M3DX; + M0DX = M[0] * convert_F4(DX); + M3DX = M[3] * convert_F4(DX); + X = convert_int4(rint(M0DX)); + Y = convert_int4(rint(M3DX)); + int tmp1, tmp2; + tmp1 = rint((M[1]*dy + M[2]) * AB_SCALE); + tmp2 = rint((M[4]*dy + M[5]) * AB_SCALE); + + X += tmp1 + round_delta; + Y += tmp2 + round_delta; + + sx = convert_int4(convert_short4(X >> AB_BITS)); + sy = convert_int4(convert_short4(Y >> AB_BITS)); + + __global uchar4 * d = (__global uchar4 *)(dst+dst_offset+dy*dstStep+dx); + uchar4 dval = *d; + DX = (int4)(dx, dx+1, dx+2, dx+3); + int4 dcon = DX >= 0 && DX < dst_cols && dy >= 0 && dy < dst_rows; + int4 scon = sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows; + int4 spos = src_offset + sy * srcStep + sx; + uchar4 sval; + sval.s0 = scon.s0 ? src[spos.s0] : 0; + sval.s1 = scon.s1 ? src[spos.s1] : 0; + sval.s2 = scon.s2 ? src[spos.s2] : 0; + sval.s3 = scon.s3 ? src[spos.s3] : 0; + dval = convert_uchar4(dcon) != (uchar4)(0,0,0,0) ? sval : dval; + *d = dval; + } +} + +__kernel void warpAffineLinear_C1_D0(__global const uchar * restrict src, __global uchar * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + + if( dx < threadCols && dy < dst_rows) + { + dx = (dx<<2) - (dst_offset&3); + + int round_delta = ((AB_SCALE >> INTER_BITS) >> 1); + + int4 X, Y; + short4 ax, ay; + int4 sx, sy; + int4 DX = (int4)(dx, dx+1, dx+2, dx+3); + DX = (DX << AB_BITS); + F4 M0DX, M3DX; + M0DX = M[0] * convert_F4(DX); + M3DX = M[3] * convert_F4(DX); + X = convert_int4(rint(M0DX)); + Y = convert_int4(rint(M3DX)); + + int tmp1, tmp2; + tmp1 = rint((M[1]*dy + M[2]) * AB_SCALE); + tmp2 = rint((M[4]*dy + M[5]) * AB_SCALE); + + X += tmp1 + round_delta; + Y += tmp2 + round_delta; + + X = X >> (AB_BITS - INTER_BITS); + Y = Y >> (AB_BITS - INTER_BITS); + + sx = convert_int4(convert_short4(X >> INTER_BITS)); + sy = convert_int4(convert_short4(Y >> INTER_BITS)); + ax = convert_short4(X & (INTER_TAB_SIZE-1)); + ay = convert_short4(Y & (INTER_TAB_SIZE-1)); + + uchar4 v0, v1, v2,v3; + int4 scon0, scon1, scon2, scon3; + int4 spos0, spos1, spos2, spos3; + + scon0 = (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows); + scon1 = (sx+1 >= 0 && sx+1 < src_cols && sy >= 0 && sy < src_rows); + scon2 = (sx >= 0 && sx < src_cols && sy+1 >= 0 && sy+1 < src_rows); + scon3 = (sx+1 >= 0 && sx+1 < src_cols && sy+1 >= 0 && sy+1 < src_rows); + spos0 = src_offset + sy * srcStep + sx; + spos1 = src_offset + sy * srcStep + sx + 1; + spos2 = src_offset + (sy+1) * srcStep + sx; + spos3 = src_offset + (sy+1) * srcStep + sx + 1; + + v0.s0 = scon0.s0 ? src[spos0.s0] : 0; + v1.s0 = scon1.s0 ? src[spos1.s0] : 0; + v2.s0 = scon2.s0 ? src[spos2.s0] : 0; + v3.s0 = scon3.s0 ? src[spos3.s0] : 0; + + v0.s1 = scon0.s1 ? src[spos0.s1] : 0; + v1.s1 = scon1.s1 ? src[spos1.s1] : 0; + v2.s1 = scon2.s1 ? src[spos2.s1] : 0; + v3.s1 = scon3.s1 ? src[spos3.s1] : 0; + + v0.s2 = scon0.s2 ? src[spos0.s2] : 0; + v1.s2 = scon1.s2 ? src[spos1.s2] : 0; + v2.s2 = scon2.s2 ? src[spos2.s2] : 0; + v3.s2 = scon3.s2 ? src[spos3.s2] : 0; + + v0.s3 = scon0.s3 ? src[spos0.s3] : 0; + v1.s3 = scon1.s3 ? src[spos1.s3] : 0; + v2.s3 = scon2.s3 ? src[spos2.s3] : 0; + v3.s3 = scon3.s3 ? src[spos3.s3] : 0; + + short4 itab0, itab1, itab2, itab3; + float4 taby, tabx; + taby = INTER_SCALE * convert_float4(ay); + tabx = INTER_SCALE * convert_float4(ax); + + itab0 = convert_short4_sat(( (1.0f-taby)*(1.0f-tabx) * (float4)INTER_REMAP_COEF_SCALE )); + itab1 = convert_short4_sat(( (1.0f-taby)*tabx * (float4)INTER_REMAP_COEF_SCALE )); + itab2 = convert_short4_sat(( taby*(1.0f-tabx) * (float4)INTER_REMAP_COEF_SCALE )); + itab3 = convert_short4_sat(( taby*tabx * (float4)INTER_REMAP_COEF_SCALE )); + + + int4 val; + uchar4 tval; + val = convert_int4(v0) * convert_int4(itab0) + convert_int4(v1) * convert_int4(itab1) + + convert_int4(v2) * convert_int4(itab2) + convert_int4(v3) * convert_int4(itab3); + tval = convert_uchar4_sat ( (val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; + + __global uchar4 * d =(__global uchar4 *)(dst+dst_offset+dy*dstStep+dx); + uchar4 dval = *d; + DX = (int4)(dx, dx+1, dx+2, dx+3); + int4 dcon = DX >= 0 && DX < dst_cols && dy >= 0 && dy < dst_rows; + dval = convert_uchar4(dcon != 0) ? tval : dval; + *d = dval; + } +} + +__kernel void warpAffineCubic_C1_D0(__global uchar * src, __global uchar * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = ((AB_SCALE>>INTER_BITS)>>1); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + int X = X0 >> (AB_BITS - INTER_BITS); + int Y = Y0 >> (AB_BITS - INTER_BITS); + + short sx = (short)(X >> INTER_BITS) - 1; + short sy = (short)(Y >> INTER_BITS) - 1; + short ay = (short)(Y & (INTER_TAB_SIZE-1)); + short ax = (short)(X & (INTER_TAB_SIZE-1)); + + uchar v[16]; + int i, j; + +#pragma unroll 4 + for(i=0; i<4; i++) + for(j=0; j<4; j++) + { + v[i*4+j] = (sx+j >= 0 && sx+j < src_cols && sy+i >= 0 && sy+i < src_rows) ? src[src_offset+(sy+i) * srcStep + (sx+j)] : 0; + } + + short itab[16]; + float tab1y[4], tab1x[4]; + float axx, ayy; + + ayy = 1.f/INTER_TAB_SIZE * ay; + axx = 1.f/INTER_TAB_SIZE * ax; + interpolateCubic(ayy, tab1y); + interpolateCubic(axx, tab1x); + int isum = 0; + +#pragma unroll 16 + for( i=0; i<16; i++ ) + { + F v = tab1y[(i>>2)] * tab1x[(i&3)]; + isum += itab[i] = convert_short_sat( rint( v * INTER_REMAP_COEF_SCALE ) ); + } + + if( isum != INTER_REMAP_COEF_SCALE ) + { + int k1, k2; + int diff = isum - INTER_REMAP_COEF_SCALE; + int Mk1=2, Mk2=2, mk1=2, mk2=2; + for( k1 = 2; k1 < 4; k1++ ) + for( k2 = 2; k2 < 4; k2++ ) + { + if( itab[(k1<<2)+k2] < itab[(mk1<<2)+mk2] ) + mk1 = k1, mk2 = k2; + else if( itab[(k1<<2)+k2] > itab[(Mk1<<2)+Mk2] ) + Mk1 = k1, Mk2 = k2; + } + diff<0 ? (itab[(Mk1<<2)+Mk2]=(short)(itab[(Mk1<<2)+Mk2]-diff)) : (itab[(mk1<<2)+mk2]=(short)(itab[(mk1<<2)+mk2]-diff)); + } + + if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + { + int sum=0; + for ( i =0; i<16; i++ ) + { + sum += v[i] * itab[i] ; + } + dst[dst_offset+dy*dstStep+dx] = convert_uchar_sat( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; + } + } +} + +/**********************************************8UC4********************************************* +***********************************************************************************************/ + +__kernel void warpAffineNN_C4_D0(__global uchar4 const * restrict src, __global uchar4 * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = (AB_SCALE >> 1); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + + int sx0 = (short)(X0 >> AB_BITS); + int sy0 = (short)(Y0 >> AB_BITS); + + if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + dst[(dst_offset>>2)+dy*(dstStep>>2)+dx]= (sx0>=0 && sx0=0 && sy0>2)+sy0*(srcStep>>2)+sx0] : (uchar4)0; + } +} + +__kernel void warpAffineLinear_C4_D0(__global uchar4 const * restrict src, __global uchar4 * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/INTER_TAB_SIZE/2; + + src_offset = (src_offset>>2); + srcStep = (srcStep>>2); + + int tmp = (dx << AB_BITS); + int X0 = rint(M[0] * tmp); + int Y0 = rint(M[3] * tmp); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + short sx0 = (short)(X0 >> INTER_BITS); + short sy0 = (short)(Y0 >> INTER_BITS); + short ax0 = (short)(X0 & (INTER_TAB_SIZE-1)); + short ay0 = (short)(Y0 & (INTER_TAB_SIZE-1)); + + int4 v0, v1, v2, v3; + + v0 = (sx0 >= 0 && sx0 < src_cols && sy0 >= 0 && sy0 < src_rows) ? convert_int4(src[src_offset+sy0 * srcStep + sx0]) : 0; + v1 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0 >= 0 && sy0 < src_rows) ? convert_int4(src[src_offset+sy0 * srcStep + sx0+1]) : 0; + v2 = (sx0 >= 0 && sx0 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? convert_int4(src[src_offset+(sy0+1) * srcStep + sx0]) : 0; + v3 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? convert_int4(src[src_offset+(sy0+1) * srcStep + sx0+1]) : 0; + + int itab0, itab1, itab2, itab3; + float taby, tabx; + taby = 1.f/INTER_TAB_SIZE*ay0; + tabx = 1.f/INTER_TAB_SIZE*ax0; + + itab0 = convert_short_sat(rint( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE )); + itab1 = convert_short_sat(rint( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE )); + itab2 = convert_short_sat(rint( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE )); + itab3 = convert_short_sat(rint( taby*tabx * INTER_REMAP_COEF_SCALE )); + + int4 val; + val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3; + + if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + dst[(dst_offset>>2)+dy*(dstStep>>2)+dx] = convert_uchar4_sat ( (val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; + } +} + +__kernel void warpAffineCubic_C4_D0(__global uchar4 const * restrict src, __global uchar4 * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = ((AB_SCALE>>INTER_BITS)>>1); + + src_offset = (src_offset>>2); + srcStep = (srcStep>>2); + dst_offset = (dst_offset>>2); + dstStep = (dstStep>>2); + + int tmp = (dx << AB_BITS); + int X0 = rint(M[0] * tmp); + int Y0 = rint(M[3] * tmp); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + int sx = (short)(X0 >> INTER_BITS) - 1; + int sy = (short)(Y0 >> INTER_BITS) - 1; + int ay = (short)(Y0 & (INTER_TAB_SIZE-1)); + int ax = (short)(X0 & (INTER_TAB_SIZE-1)); + + uchar4 v[16]; + int i,j; +#pragma unroll 4 + for(i=0; i<4; i++) + for(j=0; j<4; j++) + { + v[i*4+j] = (sx+j >= 0 && sx+j < src_cols && sy+i >= 0 && sy+i < src_rows) ? (src[src_offset+(sy+i) * srcStep + (sx+j)]) : (uchar4)0; + } + int itab[16]; + float tab1y[4], tab1x[4]; + float axx, ayy; + + ayy = INTER_SCALE * ay; + axx = INTER_SCALE * ax; + interpolateCubic(ayy, tab1y); + interpolateCubic(axx, tab1x); + int isum = 0; + +#pragma unroll 16 + for( i=0; i<16; i++ ) + { + float tmp; + tmp = tab1y[(i>>2)] * tab1x[(i&3)] * INTER_REMAP_COEF_SCALE; + itab[i] = rint(tmp); + isum += itab[i]; + } + + if( isum != INTER_REMAP_COEF_SCALE ) + { + int k1, k2; + int diff = isum - INTER_REMAP_COEF_SCALE; + int Mk1=2, Mk2=2, mk1=2, mk2=2; + + for( k1 = 2; k1 < 4; k1++ ) + for( k2 = 2; k2 < 4; k2++ ) + { + + if( itab[(k1<<2)+k2] < itab[(mk1<<2)+mk2] ) + mk1 = k1, mk2 = k2; + else if( itab[(k1<<2)+k2] > itab[(Mk1<<2)+Mk2] ) + Mk1 = k1, Mk2 = k2; + } + + diff<0 ? (itab[(Mk1<<2)+Mk2]=(short)(itab[(Mk1<<2)+Mk2]-diff)) : (itab[(mk1<<2)+mk2]=(short)(itab[(mk1<<2)+mk2]-diff)); + } + + if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + { + int4 sum=0; + for ( i =0; i<16; i++ ) + { + sum += convert_int4(v[i]) * itab[i]; + } + dst[dst_offset+dy*dstStep+dx] = convert_uchar4_sat( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; + } + } +} + + +/**********************************************32FC1******************************************** +***********************************************************************************************/ + +__kernel void warpAffineNN_C1_D5(__global float * src, __global float * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/2; + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + + short sx0 = (short)(X0 >> AB_BITS); + short sy0 = (short)(Y0 >> AB_BITS); + + if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + dst[(dst_offset>>2)+dy*dstStep+dx]= (sx0>=0 && sx0=0 && sy0>2)+sy0*srcStep+sx0] : 0; + } +} + +__kernel void warpAffineLinear_C1_D5(__global float * src, __global float * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/INTER_TAB_SIZE/2; + + src_offset = (src_offset>>2); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + short sx0 = (short)(X0 >> INTER_BITS); + short sy0 = (short)(Y0 >> INTER_BITS); + short ax0 = (short)(X0 & (INTER_TAB_SIZE-1)); + short ay0 = (short)(Y0 & (INTER_TAB_SIZE-1)); + + float v0, v1, v2, v3; + + v0 = (sx0 >= 0 && sx0 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0] : 0; + v1 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0+1] : 0; + v2 = (sx0 >= 0 && sx0 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0] : 0; + v3 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0+1] : 0; + + float tab[4]; + float taby[2], tabx[2]; + taby[0] = 1.0f - 1.f/INTER_TAB_SIZE*ay0; + taby[1] = 1.f/INTER_TAB_SIZE*ay0; + tabx[0] = 1.0f - 1.f/INTER_TAB_SIZE*ax0; + tabx[1] = 1.f/INTER_TAB_SIZE*ax0; + + tab[0] = taby[0] * tabx[0]; + tab[1] = taby[0] * tabx[1]; + tab[2] = taby[1] * tabx[0]; + tab[3] = taby[1] * tabx[1]; + + float sum = 0; + sum += v0 * tab[0] + v1 * tab[1] + v2 * tab[2] + v3 * tab[3]; + if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + dst[(dst_offset>>2)+dy*dstStep+dx] = sum; + } +} + +__kernel void warpAffineCubic_C1_D5(__global float * src, __global float * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/INTER_TAB_SIZE/2; + + src_offset = (src_offset>>2); + dst_offset = (dst_offset>>2); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + short sx = (short)(X0 >> INTER_BITS) - 1; + short sy = (short)(Y0 >> INTER_BITS) - 1; + short ay = (short)(Y0 & (INTER_TAB_SIZE-1)); + short ax = (short)(X0 & (INTER_TAB_SIZE-1)); + + float v[16]; + int i; + + for(i=0; i<16; i++) + v[i] = (sx+(i&3) >= 0 && sx+(i&3) < src_cols && sy+(i>>2) >= 0 && sy+(i>>2) < src_rows) ? src[src_offset+(sy+(i>>2)) * srcStep + (sx+(i&3))] : 0; + + float tab[16]; + float tab1y[4], tab1x[4]; + float axx, ayy; + + ayy = 1.f/INTER_TAB_SIZE * ay; + axx = 1.f/INTER_TAB_SIZE * ax; + interpolateCubic(ayy, tab1y); + interpolateCubic(axx, tab1x); + +#pragma unroll 4 + for( i=0; i<16; i++ ) + { + tab[i] = tab1y[(i>>2)] * tab1x[(i&3)]; + } + + if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + { + float sum = 0; +#pragma unroll 4 + for ( i =0; i<16; i++ ) + { + sum += v[i] * tab[i]; + } + dst[dst_offset+dy*dstStep+dx] = sum; + + } + } +} + + +/**********************************************32FC4******************************************** +***********************************************************************************************/ + +__kernel void warpAffineNN_C4_D5(__global float4 * src, __global float4 * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/2; + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + + short sx0 = (short)(X0 >> AB_BITS); + short sy0 = (short)(Y0 >> AB_BITS); + + if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + dst[(dst_offset>>4)+dy*(dstStep>>2)+dx]= (sx0>=0 && sx0=0 && sy0>4)+sy0*(srcStep>>2)+sx0] : (float4)0; + } +} + +__kernel void warpAffineLinear_C4_D5(__global float4 * src, __global float4 * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/INTER_TAB_SIZE/2; + + src_offset = (src_offset>>4); + dst_offset = (dst_offset>>4); + srcStep = (srcStep>>2); + dstStep = (dstStep>>2); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + short sx0 = (short)(X0 >> INTER_BITS); + short sy0 = (short)(Y0 >> INTER_BITS); + short ax0 = (short)(X0 & (INTER_TAB_SIZE-1)); + short ay0 = (short)(Y0 & (INTER_TAB_SIZE-1)); + + float4 v0, v1, v2, v3; + + v0 = (sx0 >= 0 && sx0 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0] : (float4)0; + v1 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0+1] : (float4)0; + v2 = (sx0 >= 0 && sx0 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0] : (float4)0; + v3 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0+1] : (float4)0; + + float tab[4]; + float taby[2], tabx[2]; + taby[0] = 1.0f - 1.f/INTER_TAB_SIZE*ay0; + taby[1] = 1.f/INTER_TAB_SIZE*ay0; + tabx[0] = 1.0f - 1.f/INTER_TAB_SIZE*ax0; + tabx[1] = 1.f/INTER_TAB_SIZE*ax0; + + tab[0] = taby[0] * tabx[0]; + tab[1] = taby[0] * tabx[1]; + tab[2] = taby[1] * tabx[0]; + tab[3] = taby[1] * tabx[1]; + + float4 sum = 0; + sum += v0 * tab[0] + v1 * tab[1] + v2 * tab[2] + v3 * tab[3]; + if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + dst[dst_offset+dy*dstStep+dx] = sum; + } +} + +__kernel void warpAffineCubic_C4_D5(__global float4 * src, __global float4 * dst, int src_cols, int src_rows, + int dst_cols, int dst_rows, int srcStep, int dstStep, + int src_offset, int dst_offset, __constant F * M, int threadCols ) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if( dx < threadCols && dy < dst_rows) + { + int round_delta = AB_SCALE/INTER_TAB_SIZE/2; + + src_offset = (src_offset>>4); + dst_offset = (dst_offset>>4); + srcStep = (srcStep>>2); + dstStep = (dstStep>>2); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + short sx = (short)(X0 >> INTER_BITS) - 1; + short sy = (short)(Y0 >> INTER_BITS) - 1; + short ay = (short)(Y0 & (INTER_TAB_SIZE-1)); + short ax = (short)(X0 & (INTER_TAB_SIZE-1)); + + float4 v[16]; + int i; + + for(i=0; i<16; i++) + v[i] = (sx+(i&3) >= 0 && sx+(i&3) < src_cols && sy+(i>>2) >= 0 && sy+(i>>2) < src_rows) ? src[src_offset+(sy+(i>>2)) * srcStep + (sx+(i&3))] : (float4)0; + + float tab[16]; + float tab1y[4], tab1x[4]; + float axx, ayy; + + ayy = 1.f/INTER_TAB_SIZE * ay; + axx = 1.f/INTER_TAB_SIZE * ax; + interpolateCubic(ayy, tab1y); + interpolateCubic(axx, tab1x); + +#pragma unroll 4 + for( i=0; i<16; i++ ) + { + tab[i] = tab1y[(i>>2)] * tab1x[(i&3)]; + } + + if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) + { + float4 sum = 0; +#pragma unroll 4 + for ( i =0; i<16; i++ ) + { + sum += v[i] * tab[i]; + } + dst[dst_offset+dy*dstStep+dx] = sum; + + } + } +} diff --git a/modules/imgproc/src/opencl/warp_perspective.cl b/modules/imgproc/src/opencl/warp_perspective.cl new file mode 100644 index 000000000..211433e70 --- /dev/null +++ b/modules/imgproc/src/opencl/warp_perspective.cl @@ -0,0 +1,223 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved. +// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// @Authors +// Zhang Ying, zhangying913@gmail.com +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors as is and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifdef DOUBLE_SUPPORT +#ifdef cl_amd_fp64 +#pragma OPENCL EXTENSION cl_amd_fp64:enable +#elif defined (cl_khr_fp64) +#pragma OPENCL EXTENSION cl_khr_fp64:enable +#endif +#define CT double +#else +#define CT float +#endif + +#define INTER_BITS 5 +#define INTER_TAB_SIZE (1 << INTER_BITS) +#define INTER_SCALE 1.f / INTER_TAB_SIZE +#define AB_BITS max(10, (int)INTER_BITS) +#define AB_SCALE (1 << AB_BITS) +#define INTER_REMAP_COEF_BITS 15 +#define INTER_REMAP_COEF_SCALE (1 << INTER_REMAP_COEF_BITS) + +#define noconvert + +#ifdef INTER_NEAREST + +__kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __constant CT * M, T scalar) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if (dx < dst_cols && dy < dst_rows) + { + CT X0 = M[0] * dx + M[1] * dy + M[2]; + CT Y0 = M[3] * dx + M[4] * dy + M[5]; + CT W = M[6] * dx + M[7] * dy + M[8]; + W = W != 0.0f ? 1.f / W : 0.0f; + short sx = convert_short_sat_rte(X0*W); + short sy = convert_short_sat_rte(Y0*W); + + int dst_index = mad24(dy, dst_step, dx * (int)sizeof(T) + dst_offset); + __global T * dst = (__global T *)(dstptr + dst_index); + + if (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) + { + int src_index = mad24(sy, src_step, sx * (int)sizeof(T) + src_offset); + __global const T * src = (__global const T *)(srcptr + src_index); + dst[0] = src[0]; + } + else + dst[0] = scalar; + } +} + +#elif defined INTER_LINEAR + +__kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __constant CT * M, WT scalar) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if (dx < dst_cols && dy < dst_rows) + { + CT X0 = M[0] * dx + M[1] * dy + M[2]; + CT Y0 = M[3] * dx + M[4] * dy + M[5]; + CT W = M[6] * dx + M[7] * dy + M[8]; + W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f; + int X = rint(X0 * W), Y = rint(Y0 * W); + + short sx = convert_short_sat(X >> INTER_BITS); + short sy = convert_short_sat(Y >> INTER_BITS); + short ay = (short)(Y & (INTER_TAB_SIZE - 1)); + short ax = (short)(X & (INTER_TAB_SIZE - 1)); + + WT v0 = (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy, src_step, src_offset + sx * (int)sizeof(T)))) : scalar; + WT v1 = (sx+1 >= 0 && sx+1 < src_cols && sy >= 0 && sy < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy, src_step, src_offset + (sx+1) * (int)sizeof(T)))) : scalar; + WT v2 = (sx >= 0 && sx < src_cols && sy+1 >= 0 && sy+1 < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy+1, src_step, src_offset + sx * (int)sizeof(T)))) : scalar; + WT v3 = (sx+1 >= 0 && sx+1 < src_cols && sy+1 >= 0 && sy+1 < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy+1, src_step, src_offset + (sx+1) * (int)sizeof(T)))) : scalar; + + float taby = 1.f/INTER_TAB_SIZE*ay; + float tabx = 1.f/INTER_TAB_SIZE*ax; + + int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T)); + __global T * dst = (__global T *)(dstptr + dst_index); + +#if depth <= 4 + int itab0 = convert_short_sat_rte( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE ); + int itab1 = convert_short_sat_rte( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE ); + int itab2 = convert_short_sat_rte( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE ); + int itab3 = convert_short_sat_rte( taby*tabx * INTER_REMAP_COEF_SCALE ); + + WT val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3; + dst[0] = convertToT((val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS); +#else + float tabx2 = 1.0f - tabx, taby2 = 1.0f - taby; + WT val = v0 * tabx2 * taby2 + v1 * tabx * taby2 + v2 * tabx2 * taby + v3 * tabx * taby; + dst[0] = convertToT(val); +#endif + } +} + +#elif defined INTER_CUBIC + +inline void interpolateCubic( float x, float* coeffs ) +{ + const float A = -0.75f; + + coeffs[0] = ((A*(x + 1.f) - 5.0f*A)*(x + 1.f) + 8.0f*A)*(x + 1.f) - 4.0f*A; + coeffs[1] = ((A + 2.f)*x - (A + 3.f))*x*x + 1.f; + coeffs[2] = ((A + 2.f)*(1.f - x) - (A + 3.f))*(1.f - x)*(1.f - x) + 1.f; + coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2]; +} + +__kernel void warpPerspective(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __constant CT * M, WT scalar) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if (dx < dst_cols && dy < dst_rows) + { + CT X0 = M[0] * dx + M[1] * dy + M[2]; + CT Y0 = M[3] * dx + M[4] * dy + M[5]; + CT W = M[6] * dx + M[7] * dy + M[8]; + W = W != 0.0f ? INTER_TAB_SIZE / W : 0.0f; + int X = rint(X0 * W), Y = rint(Y0 * W); + + short sx = convert_short_sat(X >> INTER_BITS) - 1; + short sy = convert_short_sat(Y >> INTER_BITS) - 1; + short ay = (short)(Y & (INTER_TAB_SIZE-1)); + short ax = (short)(X & (INTER_TAB_SIZE-1)); + + WT v[16]; + #pragma unroll + for (int y = 0; y < 4; y++) + #pragma unroll + for (int x = 0; x < 4; x++) + v[mad24(y, 4, x)] = (sx+x >= 0 && sx+x < src_cols && sy+y >= 0 && sy+y < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy+y, src_step, src_offset + (sx+x) * (int)sizeof(T)))) : scalar; + + float tab1y[4], tab1x[4]; + + float ayy = INTER_SCALE * ay; + float axx = INTER_SCALE * ax; + interpolateCubic(ayy, tab1y); + interpolateCubic(axx, tab1x); + + int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T)); + __global T * dst = (__global T *)(dstptr + dst_index); + + WT sum = (WT)(0); +#if depth <= 4 + int itab[16]; + + #pragma unroll + for (int i = 0; i < 16; i++) + itab[i] = rint(tab1y[(i>>2)] * tab1x[(i&3)] * INTER_REMAP_COEF_SCALE); + + #pragma unroll + for (int i = 0; i < 16; i++) + sum += v[i] * itab[i]; + dst[0] = convertToT( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ); +#else + #pragma unroll + for (int i = 0; i < 16; i++) + sum += v[i] * tab1y[(i>>2)] * tab1x[(i&3)]; + dst[0] = convertToT( sum ); +#endif + } +} + +#endif diff --git a/modules/imgproc/test/ocl/test_warp.cpp b/modules/imgproc/test/ocl/test_warp.cpp index 6e549a4ec..fece5e7e4 100644 --- a/modules/imgproc/test/ocl/test_warp.cpp +++ b/modules/imgproc/test/ocl/test_warp.cpp @@ -61,7 +61,99 @@ namespace cvtest { namespace ocl { ///////////////////////////////////////////////////////////////////////////////////////////////// -// resize +// warpAffine & warpPerspective + +PARAM_TEST_CASE(WarpTestBase, MatType, Interpolation, bool, bool) +{ + int type, interpolation; + Size dsize; + bool useRoi, mapInverse; + + TEST_DECLARE_INPUT_PARATEMER(src) + TEST_DECLARE_OUTPUT_PARATEMER(dst) + + virtual void SetUp() + { + type = GET_PARAM(0); + interpolation = GET_PARAM(1); + mapInverse = GET_PARAM(2); + useRoi = GET_PARAM(3); + + if (mapInverse) + interpolation |= WARP_INVERSE_MAP; + } + + void random_roi() + { + dsize = randomSize(1, MAX_VALUE); + + Size roiSize = randomSize(1, MAX_VALUE); + Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0); + randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE); + + Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0); + randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE); + + UMAT_UPLOAD_INPUT_PARAMETER(src) + UMAT_UPLOAD_OUTPUT_PARAMETER(dst) + } + + void Near(double threshold = 0.0) + { + EXPECT_MAT_NEAR(dst, udst, threshold); + EXPECT_MAT_NEAR(dst_roi, udst_roi, threshold); + } +}; + +/////warpAffine + +typedef WarpTestBase WarpAffine; + +OCL_TEST_P(WarpAffine, Mat) +{ + for (int j = 0; j < test_loop_times; j++) + { + random_roi(); + + Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f), + rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f)); + + OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation)); + OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation)); + + Near(1.0); + } +} + +//// warpPerspective + +typedef WarpTestBase WarpPerspective; + +OCL_TEST_P(WarpPerspective, Mat) +{ + for (int j = 0; j < test_loop_times; j++) + { + random_roi(); + + float cols = static_cast(src_roi.cols), rows = static_cast(src_roi.rows); + float cols2 = cols / 2.0f, rows2 = rows / 2.0f; + Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) }; + Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)), + Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)), + Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)), + Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) }; + Mat M = getPerspectiveTransform(sp, dp); + + OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation)); + OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation)); + + Near(1.0); + } +} + + +///////////////////////////////////////////////////////////////////////////////////////////////// +//// resize PARAM_TEST_CASE(Resize, MatType, double, double, Interpolation, bool) { @@ -127,10 +219,22 @@ OCL_TEST_P(Resize, Mat) ///////////////////////////////////////////////////////////////////////////////////// -OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResize, Resize, Combine( - Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4), - Values(0.7, 0.4, 2.0), - Values(0.3, 0.6, 2.0), +OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine( + Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4), + Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC), + Bool(), + Bool())); + +OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine( + Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4), + Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC), + Bool(), + Bool())); + +OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine( + Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4), + Values(0.5, 1.5, 2.0), + Values(0.5, 1.5, 2.0), Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR), Bool())); From dcce9d7088f1cb9472cc75e4d3e2b62de89d33ec Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Sun, 1 Dec 2013 18:09:52 +0400 Subject: [PATCH 2/3] added cv::warpAffine to T-API --- modules/core/src/ocl.cpp | 3 +- modules/imgproc/src/imgwarp.cpp | 146 ++-- modules/imgproc/src/opencl/warp_affine.cl | 799 ++++------------------ 3 files changed, 226 insertions(+), 722 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 8a0cc279a..4ef3fdad2 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -2041,7 +2041,6 @@ struct Kernel::Impl cl_int retval = 0; handle = ph != 0 ? clCreateKernel(ph, kname, &retval) : 0; - printf("kernel creation error code: %d\n", retval); for( int i = 0; i < MAX_ARRS; i++ ) u[i] = 0; haveTempDstUMats = false; @@ -2219,7 +2218,7 @@ int Kernel::set(int i, const KernelArg& arg) else if( arg.m->dims <= 2 ) { UMat2D u2d(*arg.m); - clSetKernelArg(p->handle, (cl_uint)i, sizeof(h), &h)); + clSetKernelArg(p->handle, (cl_uint)i, sizeof(h), &h); clSetKernelArg(p->handle, (cl_uint)(i+1), sizeof(u2d.step), &u2d.step); clSetKernelArg(p->handle, (cl_uint)(i+2), sizeof(u2d.offset), &u2d.offset); i += 3; diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index e5383be04..69f7d3666 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -3789,6 +3789,87 @@ private: }; #endif +enum { OCL_OP_PERSPECTIVE = 1, OCL_OP_AFFINE = 0 }; + +static bool ocl_warpTransform(InputArray _src, OutputArray _dst, InputArray _M0, + Size dsize, int flags, int borderType, const Scalar& borderValue, + int op_type) +{ + CV_Assert(op_type == OCL_OP_AFFINE || op_type == OCL_OP_PERSPECTIVE); + + int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), wdepth = depth; + double doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; + + int interpolation = flags & INTER_MAX; + if( interpolation == INTER_AREA ) + interpolation = INTER_LINEAR; + + if ( !(borderType == cv::BORDER_CONSTANT && + (interpolation == cv::INTER_NEAREST || interpolation == cv::INTER_LINEAR || interpolation == cv::INTER_CUBIC)) || + (!doubleSupport && depth == CV_64F) || cn > 4 || cn == 3) + return false; + + UMat src = _src.getUMat(), M0; + _dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() ); + UMat dst = _dst.getUMat(); + + double M[9]; + int matRows = (op_type == OCL_OP_AFFINE ? 2 : 3); + Mat matM(matRows, 3, CV_64F, M), M1 = _M0.getMat(); + CV_Assert( (M1.type() == CV_32F || M1.type() == CV_64F) && + M1.rows == matRows && M1.cols == 3 ); + M1.convertTo(matM, matM.type()); + + if( !(flags & WARP_INVERSE_MAP) ) + { + if (op_type == OCL_OP_PERSPECTIVE) + invert(matM, matM); + else + { + double D = M[0]*M[4] - M[1]*M[3]; + D = D != 0 ? 1./D : 0; + double A11 = M[4]*D, A22=M[0]*D; + M[0] = A11; M[1] *= -D; + M[3] *= -D; M[4] = A22; + double b1 = -M[0]*M[2] - M[1]*M[5]; + double b2 = -M[3]*M[2] - M[4]*M[5]; + M[2] = b1; M[5] = b2; + } + } + matM.convertTo(M0, doubleSupport ? CV_64F : CV_32F); + + const char * const interpolationMap[3] = { "NEAREST", "LINEAR", "CUBIC" }; + ocl::ProgramSource2 program = op_type == OCL_OP_AFFINE ? + ocl::imgproc::warp_affine_oclsrc : ocl::imgproc::warp_perspective_oclsrc; + const char * const kernelName = op_type == OCL_OP_AFFINE ? "warpAffine" : "warpPerspective"; + + ocl::Kernel k; + if (interpolation == INTER_NEAREST) + { + k.create(kernelName, program, + format("-D INTER_NEAREST -D T=%s%s", ocl::typeToStr(type), + doubleSupport ? " -D DOUBLE_SUPPORT" : "")); + } + else + { + char cvt[2][50]; + wdepth = std::max(CV_32S, depth); + k.create(kernelName, program, + format("-D INTER_%s -D T=%s -D WT=%s -D depth=%d -D convertToWT=%s -D convertToT=%s%s", + interpolationMap[interpolation], ocl::typeToStr(type), + ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), depth, + ocl::convertTypeStr(depth, wdepth, cn, cvt[0]), + ocl::convertTypeStr(wdepth, depth, cn, cvt[1]), + doubleSupport ? " -D DOUBLE_SUPPORT" : "")); + } + + k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst), ocl::KernelArg::PtrOnly(M0), + ocl::KernelArg::Constant(Mat(1, 1, CV_MAKE_TYPE(wdepth, cn), borderValue))); + + size_t globalThreads[2] = { dst.cols, dst.rows }; + return k.run(2, globalThreads, NULL, false); +} + } @@ -3796,6 +3877,11 @@ void cv::warpAffine( InputArray _src, OutputArray _dst, InputArray _M0, Size dsize, int flags, int borderType, const Scalar& borderValue ) { + if (ocl::useOpenCL() && _dst.isUMat() && + ocl_warpTransform(_src, _dst, _M0, dsize, flags, borderType, + borderValue, OCL_OP_AFFINE)) + return; + Mat src = _src.getMat(), M0 = _M0.getMat(); _dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() ); Mat dst = _dst.getMat(); @@ -4030,62 +4116,6 @@ private: }; #endif -static bool ocl_warpPerspective(InputArray _src, OutputArray _dst, InputArray _M0, - Size dsize, int flags, int borderType, const Scalar& borderValue) -{ - int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), wdepth = depth; - double doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; - - int interpolation = flags & INTER_MAX; - if( interpolation == INTER_AREA ) - interpolation = INTER_LINEAR; - - if ( !(borderType == cv::BORDER_CONSTANT && - (interpolation == cv::INTER_NEAREST || interpolation == cv::INTER_LINEAR || interpolation == cv::INTER_CUBIC)) || - (!doubleSupport && depth == CV_64F) || cn > 4 || cn == 3) - return false; - - UMat src = _src.getUMat(), M0; - _dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() ); - UMat dst = _dst.getUMat(); - - double M[9]; - Mat matM(3, 3, doubleSupport ? CV_64F : CV_32F, M), M1 = _M0.getMat(); - CV_Assert( (M1.type() == CV_32F || M1.type() == CV_64F) && M1.rows == 3 && M1.cols == 3 ); - M1.convertTo(matM, matM.type()); - if( !(flags & WARP_INVERSE_MAP) ) - invert(matM, matM); - matM.copyTo(M0); - - const char * const interpolationMap[3] = { "NEAREST", "LINEAR", "CUBIC" }; - ocl::Kernel k; - - if (interpolation == INTER_NEAREST) - { - k.create("warpPerspective", ocl::imgproc::warp_perspective_oclsrc, - format("-D INTER_NEAREST -D T=%s%s", ocl::typeToStr(type), - doubleSupport ? " -D DOUBLE_SUPPORT" : "")); - } - else - { - char cvt[2][50]; - wdepth = std::max(CV_32S, depth); - k.create("warpPerspective", ocl::imgproc::warp_perspective_oclsrc, - format("-D INTER_%s -D T=%s -D WT=%s -D depth=%d -D convertToWT=%s -D convertToT=%s%s", - interpolationMap[interpolation], ocl::typeToStr(type), - ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), depth, - ocl::convertTypeStr(depth, wdepth, cn, cvt[0]), - ocl::convertTypeStr(wdepth, depth, cn, cvt[1]), - doubleSupport ? " -D DOUBLE_SUPPORT" : "")); - } - - k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst), - ocl::KernelArg::PtrOnly(M0), ocl::KernelArg::Constant(Mat(1, 1, CV_MAKE_TYPE(wdepth, cn), borderValue))); - - size_t globalThreads[2] = { dst.cols, dst.rows }; - return k.run(2, globalThreads, NULL, false); -} - } void cv::warpPerspective( InputArray _src, OutputArray _dst, InputArray _M0, @@ -4093,7 +4123,9 @@ void cv::warpPerspective( InputArray _src, OutputArray _dst, InputArray _M0, { CV_Assert( _src.total() > 0 ); - if (ocl::useOpenCL() && _dst.isUMat() && ocl_warpPerspective(_src, _dst, _M0, dsize, flags, borderType, borderValue)) + if (ocl::useOpenCL() && _dst.isUMat() && + ocl_warpTransform(_src, _dst, _M0, dsize, flags, borderType, borderValue, + OCL_OP_PERSPECTIVE)) return; Mat src = _src.getMat(), M0 = _M0.getMat(); diff --git a/modules/imgproc/src/opencl/warp_affine.cl b/modules/imgproc/src/opencl/warp_affine.cl index 27f99e005..340cfdd8e 100644 --- a/modules/imgproc/src/opencl/warp_affine.cl +++ b/modules/imgproc/src/opencl/warp_affine.cl @@ -43,23 +43,15 @@ // //M*/ - -//warpAffine kernel -//support data types: CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4, and three interpolation methods: NN, Linear, Cubic. - #ifdef DOUBLE_SUPPORT #ifdef cl_amd_fp64 #pragma OPENCL EXTENSION cl_amd_fp64:enable #elif defined (cl_khr_fp64) #pragma OPENCL EXTENSION cl_khr_fp64:enable #endif -typedef double F; -typedef double4 F4; -#define convert_F4 convert_double4 +#define CT double #else -typedef float F; -typedef float4 F4; -#define convert_F4 convert_float4 +#define CT float #endif #define INTER_BITS 5 @@ -70,6 +62,102 @@ typedef float4 F4; #define INTER_REMAP_COEF_BITS 15 #define INTER_REMAP_COEF_SCALE (1 << INTER_REMAP_COEF_BITS) +#define noconvert + +#ifdef INTER_NEAREST + +__kernel void warpAffine(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __constant CT * M, T scalar) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if (dx < dst_cols && dy < dst_rows) + { + int round_delta = (AB_SCALE >> 1); + + int X0 = rint(M[0] * dx * AB_SCALE); + int Y0 = rint(M[3] * dx * AB_SCALE); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + + short sx = convert_short_sat(X0 >> AB_BITS); + short sy = convert_short_sat(Y0 >> AB_BITS); + + int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T)); + __global T * dst = (__global T *)(dstptr + dst_index); + + if (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) + { + int src_index = mad24(sy, src_step, src_offset + sx * (int)sizeof(T)); + __global const T * src = (__global const T *)(srcptr + src_index); + dst[0] = src[0]; + } + else + dst[0] = scalar; + } +} + +#elif defined INTER_LINEAR + +__kernel void warpAffine(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __constant CT * M, WT scalar) +{ + int dx = get_global_id(0); + int dy = get_global_id(1); + + if (dx < dst_cols && dy < dst_rows) + { + int round_delta = AB_SCALE/INTER_TAB_SIZE/2; + + int tmp = (dx << AB_BITS); + int X0 = rint(M[0] * tmp); + int Y0 = rint(M[3] * tmp); + X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; + Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; + X0 = X0 >> (AB_BITS - INTER_BITS); + Y0 = Y0 >> (AB_BITS - INTER_BITS); + + short sx = convert_short_sat(X0 >> INTER_BITS); + short sy = convert_short_sat(Y0 >> INTER_BITS); + short ax = convert_short(X0 & (INTER_TAB_SIZE-1)); + short ay = convert_short(Y0 & (INTER_TAB_SIZE-1)); + + WT v0 = (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy, src_step, src_offset + sx * (int)sizeof(T)))) : scalar; + WT v1 = (sx+1 >= 0 && sx+1 < src_cols && sy >= 0 && sy < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy, src_step, src_offset + (sx+1) * (int)sizeof(T)))) : scalar; + WT v2 = (sx >= 0 && sx < src_cols && sy+1 >= 0 && sy+1 < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy+1, src_step, src_offset + sx * (int)sizeof(T)))) : scalar; + WT v3 = (sx+1 >= 0 && sx+1 < src_cols && sy+1 >= 0 && sy+1 < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy+1, src_step, src_offset + (sx+1) * (int)sizeof(T)))) : scalar; + + float taby = 1.f/INTER_TAB_SIZE*ay; + float tabx = 1.f/INTER_TAB_SIZE*ax; + + int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T)); + __global T * dst = (__global T *)(dstptr + dst_index); + +#if depth <= 4 + int itab0 = convert_short_sat_rte( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE ); + int itab1 = convert_short_sat_rte( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE ); + int itab2 = convert_short_sat_rte( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE ); + int itab3 = convert_short_sat_rte( taby*tabx * INTER_REMAP_COEF_SCALE ); + + WT val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3; + dst[0] = convertToT((val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS); +#else + float tabx2 = 1.0f - tabx, taby2 = 1.0f - taby; + WT val = v0 * tabx2 * taby2 + v1 * tabx * taby2 + v2 * tabx2 * taby + v3 * tabx * taby; + dst[0] = convertToT(val); +#endif + } +} + +#elif defined INTER_CUBIC + inline void interpolateCubic( float x, float* coeffs ) { const float A = -0.75f; @@ -80,330 +168,17 @@ inline void interpolateCubic( float x, float* coeffs ) coeffs[3] = 1.f - coeffs[0] - coeffs[1] - coeffs[2]; } - -/**********************************************8UC1********************************************* -***********************************************************************************************/ -__kernel void warpAffineNN_C1_D0(__global uchar const * restrict src, __global uchar * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) +__kernel void warpAffine(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __constant CT * M, WT scalar) { int dx = get_global_id(0); int dy = get_global_id(1); - if( dx < threadCols && dy < dst_rows) - { - dx = (dx<<2) - (dst_offset&3); - - int round_delta = (AB_SCALE>>1); - - int4 X, Y; - int4 sx, sy; - int4 DX = (int4)(dx, dx+1, dx+2, dx+3); - DX = (DX << AB_BITS); - F4 M0DX, M3DX; - M0DX = M[0] * convert_F4(DX); - M3DX = M[3] * convert_F4(DX); - X = convert_int4(rint(M0DX)); - Y = convert_int4(rint(M3DX)); - int tmp1, tmp2; - tmp1 = rint((M[1]*dy + M[2]) * AB_SCALE); - tmp2 = rint((M[4]*dy + M[5]) * AB_SCALE); - - X += tmp1 + round_delta; - Y += tmp2 + round_delta; - - sx = convert_int4(convert_short4(X >> AB_BITS)); - sy = convert_int4(convert_short4(Y >> AB_BITS)); - - __global uchar4 * d = (__global uchar4 *)(dst+dst_offset+dy*dstStep+dx); - uchar4 dval = *d; - DX = (int4)(dx, dx+1, dx+2, dx+3); - int4 dcon = DX >= 0 && DX < dst_cols && dy >= 0 && dy < dst_rows; - int4 scon = sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows; - int4 spos = src_offset + sy * srcStep + sx; - uchar4 sval; - sval.s0 = scon.s0 ? src[spos.s0] : 0; - sval.s1 = scon.s1 ? src[spos.s1] : 0; - sval.s2 = scon.s2 ? src[spos.s2] : 0; - sval.s3 = scon.s3 ? src[spos.s3] : 0; - dval = convert_uchar4(dcon) != (uchar4)(0,0,0,0) ? sval : dval; - *d = dval; - } -} - -__kernel void warpAffineLinear_C1_D0(__global const uchar * restrict src, __global uchar * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - - if( dx < threadCols && dy < dst_rows) - { - dx = (dx<<2) - (dst_offset&3); - - int round_delta = ((AB_SCALE >> INTER_BITS) >> 1); - - int4 X, Y; - short4 ax, ay; - int4 sx, sy; - int4 DX = (int4)(dx, dx+1, dx+2, dx+3); - DX = (DX << AB_BITS); - F4 M0DX, M3DX; - M0DX = M[0] * convert_F4(DX); - M3DX = M[3] * convert_F4(DX); - X = convert_int4(rint(M0DX)); - Y = convert_int4(rint(M3DX)); - - int tmp1, tmp2; - tmp1 = rint((M[1]*dy + M[2]) * AB_SCALE); - tmp2 = rint((M[4]*dy + M[5]) * AB_SCALE); - - X += tmp1 + round_delta; - Y += tmp2 + round_delta; - - X = X >> (AB_BITS - INTER_BITS); - Y = Y >> (AB_BITS - INTER_BITS); - - sx = convert_int4(convert_short4(X >> INTER_BITS)); - sy = convert_int4(convert_short4(Y >> INTER_BITS)); - ax = convert_short4(X & (INTER_TAB_SIZE-1)); - ay = convert_short4(Y & (INTER_TAB_SIZE-1)); - - uchar4 v0, v1, v2,v3; - int4 scon0, scon1, scon2, scon3; - int4 spos0, spos1, spos2, spos3; - - scon0 = (sx >= 0 && sx < src_cols && sy >= 0 && sy < src_rows); - scon1 = (sx+1 >= 0 && sx+1 < src_cols && sy >= 0 && sy < src_rows); - scon2 = (sx >= 0 && sx < src_cols && sy+1 >= 0 && sy+1 < src_rows); - scon3 = (sx+1 >= 0 && sx+1 < src_cols && sy+1 >= 0 && sy+1 < src_rows); - spos0 = src_offset + sy * srcStep + sx; - spos1 = src_offset + sy * srcStep + sx + 1; - spos2 = src_offset + (sy+1) * srcStep + sx; - spos3 = src_offset + (sy+1) * srcStep + sx + 1; - - v0.s0 = scon0.s0 ? src[spos0.s0] : 0; - v1.s0 = scon1.s0 ? src[spos1.s0] : 0; - v2.s0 = scon2.s0 ? src[spos2.s0] : 0; - v3.s0 = scon3.s0 ? src[spos3.s0] : 0; - - v0.s1 = scon0.s1 ? src[spos0.s1] : 0; - v1.s1 = scon1.s1 ? src[spos1.s1] : 0; - v2.s1 = scon2.s1 ? src[spos2.s1] : 0; - v3.s1 = scon3.s1 ? src[spos3.s1] : 0; - - v0.s2 = scon0.s2 ? src[spos0.s2] : 0; - v1.s2 = scon1.s2 ? src[spos1.s2] : 0; - v2.s2 = scon2.s2 ? src[spos2.s2] : 0; - v3.s2 = scon3.s2 ? src[spos3.s2] : 0; - - v0.s3 = scon0.s3 ? src[spos0.s3] : 0; - v1.s3 = scon1.s3 ? src[spos1.s3] : 0; - v2.s3 = scon2.s3 ? src[spos2.s3] : 0; - v3.s3 = scon3.s3 ? src[spos3.s3] : 0; - - short4 itab0, itab1, itab2, itab3; - float4 taby, tabx; - taby = INTER_SCALE * convert_float4(ay); - tabx = INTER_SCALE * convert_float4(ax); - - itab0 = convert_short4_sat(( (1.0f-taby)*(1.0f-tabx) * (float4)INTER_REMAP_COEF_SCALE )); - itab1 = convert_short4_sat(( (1.0f-taby)*tabx * (float4)INTER_REMAP_COEF_SCALE )); - itab2 = convert_short4_sat(( taby*(1.0f-tabx) * (float4)INTER_REMAP_COEF_SCALE )); - itab3 = convert_short4_sat(( taby*tabx * (float4)INTER_REMAP_COEF_SCALE )); - - - int4 val; - uchar4 tval; - val = convert_int4(v0) * convert_int4(itab0) + convert_int4(v1) * convert_int4(itab1) - + convert_int4(v2) * convert_int4(itab2) + convert_int4(v3) * convert_int4(itab3); - tval = convert_uchar4_sat ( (val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; - - __global uchar4 * d =(__global uchar4 *)(dst+dst_offset+dy*dstStep+dx); - uchar4 dval = *d; - DX = (int4)(dx, dx+1, dx+2, dx+3); - int4 dcon = DX >= 0 && DX < dst_cols && dy >= 0 && dy < dst_rows; - dval = convert_uchar4(dcon != 0) ? tval : dval; - *d = dval; - } -} - -__kernel void warpAffineCubic_C1_D0(__global uchar * src, __global uchar * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) + if (dx < dst_cols && dy < dst_rows) { int round_delta = ((AB_SCALE>>INTER_BITS)>>1); - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - int X = X0 >> (AB_BITS - INTER_BITS); - int Y = Y0 >> (AB_BITS - INTER_BITS); - - short sx = (short)(X >> INTER_BITS) - 1; - short sy = (short)(Y >> INTER_BITS) - 1; - short ay = (short)(Y & (INTER_TAB_SIZE-1)); - short ax = (short)(X & (INTER_TAB_SIZE-1)); - - uchar v[16]; - int i, j; - -#pragma unroll 4 - for(i=0; i<4; i++) - for(j=0; j<4; j++) - { - v[i*4+j] = (sx+j >= 0 && sx+j < src_cols && sy+i >= 0 && sy+i < src_rows) ? src[src_offset+(sy+i) * srcStep + (sx+j)] : 0; - } - - short itab[16]; - float tab1y[4], tab1x[4]; - float axx, ayy; - - ayy = 1.f/INTER_TAB_SIZE * ay; - axx = 1.f/INTER_TAB_SIZE * ax; - interpolateCubic(ayy, tab1y); - interpolateCubic(axx, tab1x); - int isum = 0; - -#pragma unroll 16 - for( i=0; i<16; i++ ) - { - F v = tab1y[(i>>2)] * tab1x[(i&3)]; - isum += itab[i] = convert_short_sat( rint( v * INTER_REMAP_COEF_SCALE ) ); - } - - if( isum != INTER_REMAP_COEF_SCALE ) - { - int k1, k2; - int diff = isum - INTER_REMAP_COEF_SCALE; - int Mk1=2, Mk2=2, mk1=2, mk2=2; - for( k1 = 2; k1 < 4; k1++ ) - for( k2 = 2; k2 < 4; k2++ ) - { - if( itab[(k1<<2)+k2] < itab[(mk1<<2)+mk2] ) - mk1 = k1, mk2 = k2; - else if( itab[(k1<<2)+k2] > itab[(Mk1<<2)+Mk2] ) - Mk1 = k1, Mk2 = k2; - } - diff<0 ? (itab[(Mk1<<2)+Mk2]=(short)(itab[(Mk1<<2)+Mk2]-diff)) : (itab[(mk1<<2)+mk2]=(short)(itab[(mk1<<2)+mk2]-diff)); - } - - if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - { - int sum=0; - for ( i =0; i<16; i++ ) - { - sum += v[i] * itab[i] ; - } - dst[dst_offset+dy*dstStep+dx] = convert_uchar_sat( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; - } - } -} - -/**********************************************8UC4********************************************* -***********************************************************************************************/ - -__kernel void warpAffineNN_C4_D0(__global uchar4 const * restrict src, __global uchar4 * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = (AB_SCALE >> 1); - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - - int sx0 = (short)(X0 >> AB_BITS); - int sy0 = (short)(Y0 >> AB_BITS); - - if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - dst[(dst_offset>>2)+dy*(dstStep>>2)+dx]= (sx0>=0 && sx0=0 && sy0>2)+sy0*(srcStep>>2)+sx0] : (uchar4)0; - } -} - -__kernel void warpAffineLinear_C4_D0(__global uchar4 const * restrict src, __global uchar4 * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/INTER_TAB_SIZE/2; - - src_offset = (src_offset>>2); - srcStep = (srcStep>>2); - - int tmp = (dx << AB_BITS); - int X0 = rint(M[0] * tmp); - int Y0 = rint(M[3] * tmp); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - X0 = X0 >> (AB_BITS - INTER_BITS); - Y0 = Y0 >> (AB_BITS - INTER_BITS); - - short sx0 = (short)(X0 >> INTER_BITS); - short sy0 = (short)(Y0 >> INTER_BITS); - short ax0 = (short)(X0 & (INTER_TAB_SIZE-1)); - short ay0 = (short)(Y0 & (INTER_TAB_SIZE-1)); - - int4 v0, v1, v2, v3; - - v0 = (sx0 >= 0 && sx0 < src_cols && sy0 >= 0 && sy0 < src_rows) ? convert_int4(src[src_offset+sy0 * srcStep + sx0]) : 0; - v1 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0 >= 0 && sy0 < src_rows) ? convert_int4(src[src_offset+sy0 * srcStep + sx0+1]) : 0; - v2 = (sx0 >= 0 && sx0 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? convert_int4(src[src_offset+(sy0+1) * srcStep + sx0]) : 0; - v3 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? convert_int4(src[src_offset+(sy0+1) * srcStep + sx0+1]) : 0; - - int itab0, itab1, itab2, itab3; - float taby, tabx; - taby = 1.f/INTER_TAB_SIZE*ay0; - tabx = 1.f/INTER_TAB_SIZE*ax0; - - itab0 = convert_short_sat(rint( (1.0f-taby)*(1.0f-tabx) * INTER_REMAP_COEF_SCALE )); - itab1 = convert_short_sat(rint( (1.0f-taby)*tabx * INTER_REMAP_COEF_SCALE )); - itab2 = convert_short_sat(rint( taby*(1.0f-tabx) * INTER_REMAP_COEF_SCALE )); - itab3 = convert_short_sat(rint( taby*tabx * INTER_REMAP_COEF_SCALE )); - - int4 val; - val = v0 * itab0 + v1 * itab1 + v2 * itab2 + v3 * itab3; - - if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - dst[(dst_offset>>2)+dy*(dstStep>>2)+dx] = convert_uchar4_sat ( (val + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; - } -} - -__kernel void warpAffineCubic_C4_D0(__global uchar4 const * restrict src, __global uchar4 * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = ((AB_SCALE>>INTER_BITS)>>1); - - src_offset = (src_offset>>2); - srcStep = (srcStep>>2); - dst_offset = (dst_offset>>2); - dstStep = (dstStep>>2); - int tmp = (dx << AB_BITS); int X0 = rint(M[0] * tmp); int Y0 = rint(M[3] * tmp); @@ -417,345 +192,43 @@ __kernel void warpAffineCubic_C4_D0(__global uchar4 const * restrict src, __glob int ay = (short)(Y0 & (INTER_TAB_SIZE-1)); int ax = (short)(X0 & (INTER_TAB_SIZE-1)); - uchar4 v[16]; - int i,j; -#pragma unroll 4 - for(i=0; i<4; i++) - for(j=0; j<4; j++) - { - v[i*4+j] = (sx+j >= 0 && sx+j < src_cols && sy+i >= 0 && sy+i < src_rows) ? (src[src_offset+(sy+i) * srcStep + (sx+j)]) : (uchar4)0; - } + WT v[16]; + #pragma unroll + for (int y = 0; y < 4; y++) + #pragma unroll + for (int x = 0; x < 4; x++) + v[mad24(y, 4, x)] = (sx+x >= 0 && sx+x < src_cols && sy+y >= 0 && sy+y < src_rows) ? + convertToWT(*(__global const T *)(srcptr + mad24(sy+y, src_step, src_offset + (sx+x) * (int)sizeof(T)))) : scalar; + + float tab1y[4], tab1x[4]; + + float ayy = INTER_SCALE * ay; + float axx = INTER_SCALE * ax; + interpolateCubic(ayy, tab1y); + interpolateCubic(axx, tab1x); + + int dst_index = mad24(dy, dst_step, dst_offset + dx * (int)sizeof(T)); + __global T * dst = (__global T *)(dstptr + dst_index); + + WT sum = (WT)(0); +#if depth <= 4 int itab[16]; - float tab1y[4], tab1x[4]; - float axx, ayy; - ayy = INTER_SCALE * ay; - axx = INTER_SCALE * ax; - interpolateCubic(ayy, tab1y); - interpolateCubic(axx, tab1x); - int isum = 0; + #pragma unroll + for (int i = 0; i < 16; i++) + itab[i] = rint(tab1y[(i>>2)] * tab1x[(i&3)] * INTER_REMAP_COEF_SCALE); -#pragma unroll 16 - for( i=0; i<16; i++ ) - { - float tmp; - tmp = tab1y[(i>>2)] * tab1x[(i&3)] * INTER_REMAP_COEF_SCALE; - itab[i] = rint(tmp); - isum += itab[i]; - } - - if( isum != INTER_REMAP_COEF_SCALE ) - { - int k1, k2; - int diff = isum - INTER_REMAP_COEF_SCALE; - int Mk1=2, Mk2=2, mk1=2, mk2=2; - - for( k1 = 2; k1 < 4; k1++ ) - for( k2 = 2; k2 < 4; k2++ ) - { - - if( itab[(k1<<2)+k2] < itab[(mk1<<2)+mk2] ) - mk1 = k1, mk2 = k2; - else if( itab[(k1<<2)+k2] > itab[(Mk1<<2)+Mk2] ) - Mk1 = k1, Mk2 = k2; - } - - diff<0 ? (itab[(Mk1<<2)+Mk2]=(short)(itab[(Mk1<<2)+Mk2]-diff)) : (itab[(mk1<<2)+mk2]=(short)(itab[(mk1<<2)+mk2]-diff)); - } - - if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - { - int4 sum=0; - for ( i =0; i<16; i++ ) - { - sum += convert_int4(v[i]) * itab[i]; - } - dst[dst_offset+dy*dstStep+dx] = convert_uchar4_sat( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ) ; - } + #pragma unroll + for (int i = 0; i < 16; i++) + sum += v[i] * itab[i]; + dst[0] = convertToT( (sum + (1 << (INTER_REMAP_COEF_BITS-1))) >> INTER_REMAP_COEF_BITS ); +#else + #pragma unroll + for (int i = 0; i < 16; i++) + sum += v[i] * tab1y[(i>>2)] * tab1x[(i&3)]; + dst[0] = convertToT( sum ); +#endif } } - -/**********************************************32FC1******************************************** -***********************************************************************************************/ - -__kernel void warpAffineNN_C1_D5(__global float * src, __global float * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/2; - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - - short sx0 = (short)(X0 >> AB_BITS); - short sy0 = (short)(Y0 >> AB_BITS); - - if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - dst[(dst_offset>>2)+dy*dstStep+dx]= (sx0>=0 && sx0=0 && sy0>2)+sy0*srcStep+sx0] : 0; - } -} - -__kernel void warpAffineLinear_C1_D5(__global float * src, __global float * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/INTER_TAB_SIZE/2; - - src_offset = (src_offset>>2); - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - X0 = X0 >> (AB_BITS - INTER_BITS); - Y0 = Y0 >> (AB_BITS - INTER_BITS); - - short sx0 = (short)(X0 >> INTER_BITS); - short sy0 = (short)(Y0 >> INTER_BITS); - short ax0 = (short)(X0 & (INTER_TAB_SIZE-1)); - short ay0 = (short)(Y0 & (INTER_TAB_SIZE-1)); - - float v0, v1, v2, v3; - - v0 = (sx0 >= 0 && sx0 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0] : 0; - v1 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0+1] : 0; - v2 = (sx0 >= 0 && sx0 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0] : 0; - v3 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0+1] : 0; - - float tab[4]; - float taby[2], tabx[2]; - taby[0] = 1.0f - 1.f/INTER_TAB_SIZE*ay0; - taby[1] = 1.f/INTER_TAB_SIZE*ay0; - tabx[0] = 1.0f - 1.f/INTER_TAB_SIZE*ax0; - tabx[1] = 1.f/INTER_TAB_SIZE*ax0; - - tab[0] = taby[0] * tabx[0]; - tab[1] = taby[0] * tabx[1]; - tab[2] = taby[1] * tabx[0]; - tab[3] = taby[1] * tabx[1]; - - float sum = 0; - sum += v0 * tab[0] + v1 * tab[1] + v2 * tab[2] + v3 * tab[3]; - if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - dst[(dst_offset>>2)+dy*dstStep+dx] = sum; - } -} - -__kernel void warpAffineCubic_C1_D5(__global float * src, __global float * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/INTER_TAB_SIZE/2; - - src_offset = (src_offset>>2); - dst_offset = (dst_offset>>2); - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - X0 = X0 >> (AB_BITS - INTER_BITS); - Y0 = Y0 >> (AB_BITS - INTER_BITS); - - short sx = (short)(X0 >> INTER_BITS) - 1; - short sy = (short)(Y0 >> INTER_BITS) - 1; - short ay = (short)(Y0 & (INTER_TAB_SIZE-1)); - short ax = (short)(X0 & (INTER_TAB_SIZE-1)); - - float v[16]; - int i; - - for(i=0; i<16; i++) - v[i] = (sx+(i&3) >= 0 && sx+(i&3) < src_cols && sy+(i>>2) >= 0 && sy+(i>>2) < src_rows) ? src[src_offset+(sy+(i>>2)) * srcStep + (sx+(i&3))] : 0; - - float tab[16]; - float tab1y[4], tab1x[4]; - float axx, ayy; - - ayy = 1.f/INTER_TAB_SIZE * ay; - axx = 1.f/INTER_TAB_SIZE * ax; - interpolateCubic(ayy, tab1y); - interpolateCubic(axx, tab1x); - -#pragma unroll 4 - for( i=0; i<16; i++ ) - { - tab[i] = tab1y[(i>>2)] * tab1x[(i&3)]; - } - - if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - { - float sum = 0; -#pragma unroll 4 - for ( i =0; i<16; i++ ) - { - sum += v[i] * tab[i]; - } - dst[dst_offset+dy*dstStep+dx] = sum; - - } - } -} - - -/**********************************************32FC4******************************************** -***********************************************************************************************/ - -__kernel void warpAffineNN_C4_D5(__global float4 * src, __global float4 * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/2; - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - - short sx0 = (short)(X0 >> AB_BITS); - short sy0 = (short)(Y0 >> AB_BITS); - - if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - dst[(dst_offset>>4)+dy*(dstStep>>2)+dx]= (sx0>=0 && sx0=0 && sy0>4)+sy0*(srcStep>>2)+sx0] : (float4)0; - } -} - -__kernel void warpAffineLinear_C4_D5(__global float4 * src, __global float4 * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/INTER_TAB_SIZE/2; - - src_offset = (src_offset>>4); - dst_offset = (dst_offset>>4); - srcStep = (srcStep>>2); - dstStep = (dstStep>>2); - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - X0 = X0 >> (AB_BITS - INTER_BITS); - Y0 = Y0 >> (AB_BITS - INTER_BITS); - - short sx0 = (short)(X0 >> INTER_BITS); - short sy0 = (short)(Y0 >> INTER_BITS); - short ax0 = (short)(X0 & (INTER_TAB_SIZE-1)); - short ay0 = (short)(Y0 & (INTER_TAB_SIZE-1)); - - float4 v0, v1, v2, v3; - - v0 = (sx0 >= 0 && sx0 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0] : (float4)0; - v1 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0 >= 0 && sy0 < src_rows) ? src[src_offset+sy0 * srcStep + sx0+1] : (float4)0; - v2 = (sx0 >= 0 && sx0 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0] : (float4)0; - v3 = (sx0+1 >= 0 && sx0+1 < src_cols && sy0+1 >= 0 && sy0+1 < src_rows) ? src[src_offset+(sy0+1) * srcStep + sx0+1] : (float4)0; - - float tab[4]; - float taby[2], tabx[2]; - taby[0] = 1.0f - 1.f/INTER_TAB_SIZE*ay0; - taby[1] = 1.f/INTER_TAB_SIZE*ay0; - tabx[0] = 1.0f - 1.f/INTER_TAB_SIZE*ax0; - tabx[1] = 1.f/INTER_TAB_SIZE*ax0; - - tab[0] = taby[0] * tabx[0]; - tab[1] = taby[0] * tabx[1]; - tab[2] = taby[1] * tabx[0]; - tab[3] = taby[1] * tabx[1]; - - float4 sum = 0; - sum += v0 * tab[0] + v1 * tab[1] + v2 * tab[2] + v3 * tab[3]; - if(dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - dst[dst_offset+dy*dstStep+dx] = sum; - } -} - -__kernel void warpAffineCubic_C4_D5(__global float4 * src, __global float4 * dst, int src_cols, int src_rows, - int dst_cols, int dst_rows, int srcStep, int dstStep, - int src_offset, int dst_offset, __constant F * M, int threadCols ) -{ - int dx = get_global_id(0); - int dy = get_global_id(1); - - if( dx < threadCols && dy < dst_rows) - { - int round_delta = AB_SCALE/INTER_TAB_SIZE/2; - - src_offset = (src_offset>>4); - dst_offset = (dst_offset>>4); - srcStep = (srcStep>>2); - dstStep = (dstStep>>2); - - int X0 = rint(M[0] * dx * AB_SCALE); - int Y0 = rint(M[3] * dx * AB_SCALE); - X0 += rint((M[1]*dy + M[2]) * AB_SCALE) + round_delta; - Y0 += rint((M[4]*dy + M[5]) * AB_SCALE) + round_delta; - X0 = X0 >> (AB_BITS - INTER_BITS); - Y0 = Y0 >> (AB_BITS - INTER_BITS); - - short sx = (short)(X0 >> INTER_BITS) - 1; - short sy = (short)(Y0 >> INTER_BITS) - 1; - short ay = (short)(Y0 & (INTER_TAB_SIZE-1)); - short ax = (short)(X0 & (INTER_TAB_SIZE-1)); - - float4 v[16]; - int i; - - for(i=0; i<16; i++) - v[i] = (sx+(i&3) >= 0 && sx+(i&3) < src_cols && sy+(i>>2) >= 0 && sy+(i>>2) < src_rows) ? src[src_offset+(sy+(i>>2)) * srcStep + (sx+(i&3))] : (float4)0; - - float tab[16]; - float tab1y[4], tab1x[4]; - float axx, ayy; - - ayy = 1.f/INTER_TAB_SIZE * ay; - axx = 1.f/INTER_TAB_SIZE * ax; - interpolateCubic(ayy, tab1y); - interpolateCubic(axx, tab1x); - -#pragma unroll 4 - for( i=0; i<16; i++ ) - { - tab[i] = tab1y[(i>>2)] * tab1x[(i&3)]; - } - - if( dx >= 0 && dx < dst_cols && dy >= 0 && dy < dst_rows) - { - float4 sum = 0; -#pragma unroll 4 - for ( i =0; i<16; i++ ) - { - sum += v[i] * tab[i]; - } - dst[dst_offset+dy*dstStep+dx] = sum; - - } - } -} +#endif From 803672feeac20afb2747119cd6cda8c13a66b28e Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Sun, 1 Dec 2013 18:56:28 +0400 Subject: [PATCH 3/3] added cv::remap to T-API --- modules/core/src/ocl.cpp | 2 +- modules/core/src/umatrix.cpp | 1 + modules/imgproc/src/imgwarp.cpp | 140 +++++-- modules/imgproc/src/opencl/remap.cl | 435 +++++++++++++++++++++ modules/imgproc/test/ocl/test_warp.cpp | 130 +++++- modules/ts/include/opencv2/ts/ocl_test.hpp | 1 + 6 files changed, 676 insertions(+), 33 deletions(-) create mode 100644 modules/imgproc/src/opencl/remap.cl diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 4ef3fdad2..64460efb0 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -1893,7 +1893,7 @@ Context2& Context2::getDefault() // First, try to retrieve existing context of the same type. // In its turn, Platform::getContext() may call Context2::create() // if there is no such context. - ctx.create(Device::TYPE_CPU); + ctx.create(Device::TYPE_ACCELERATOR); if(!ctx.p) ctx.create(Device::TYPE_DGPU); if(!ctx.p) diff --git a/modules/core/src/umatrix.cpp b/modules/core/src/umatrix.cpp index 9ed00ac6f..c35d4ccfa 100644 --- a/modules/core/src/umatrix.cpp +++ b/modules/core/src/umatrix.cpp @@ -578,6 +578,7 @@ Mat UMat::getMat(int accessFlags) const u->currAllocator->map(u, accessFlags | ACCESS_READ); CV_Assert(u->data != 0); Mat hdr(dims, size.p, type(), u->data + offset, step.p); + hdr.flags = flags; hdr.u = u; hdr.datastart = u->data; hdr.data = hdr.datastart + offset; diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index 69f7d3666..875813068 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -2010,6 +2010,8 @@ static bool ocl_resize( InputArray _src, OutputArray _dst, Size dsize, iscale_x, iscale_y, 1.0f / (iscale_x * iscale_y)); k.create("resizeAREA_FAST", ocl::imgproc::resize_oclsrc, buildOption); + if (k.empty()) + return false; int smap_tab_size = dst.cols * iscale_x + dst.rows * iscale_y; AutoBuffer dmap_tab(dst.cols + dst.rows), smap_tab(smap_tab_size); @@ -2026,6 +2028,8 @@ static bool ocl_resize( InputArray _src, OutputArray _dst, Size dsize, { buildOption = buildOption + format(" -D convertToT=%s", ocl::convertTypeStr(wdepth, depth, cn, cvt[0])); k.create("resizeAREA", ocl::imgproc::resize_oclsrc, buildOption); + if (k.empty()) + return false; Size ssize = src.size(); int xytab_size = (ssize.width + ssize.height) << 1; @@ -3383,6 +3387,78 @@ private: const void *ctab; }; +static bool ocl_remap(InputArray _src, OutputArray _dst, InputArray _map1, InputArray _map2, + int interpolation, int borderType, const Scalar& borderValue) +{ + int cn = _src.channels(), type = _src.type(), depth = _src.depth(); + + if (borderType == BORDER_TRANSPARENT || cn == 3 || !(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST) + || _map1.type() == CV_16SC1 || _map2.type() == CV_16SC1) + return false; + + UMat src = _src.getUMat(), map1 = _map1.getUMat(), map2 = _map2.getUMat(); + + if( (map1.type() == CV_16SC2 && (map2.type() == CV_16UC1 || map2.empty())) || + (map2.type() == CV_16SC2 && (map1.type() == CV_16UC1 || map1.empty())) ) + { + if (map1.type() != CV_16SC2) + std::swap(map1, map2); + } + else + CV_Assert( map1.type() == CV_32FC2 || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1) ); + + _dst.create(map1.size(), type); + UMat dst = _dst.getUMat(); + + String kernelName = "remap"; + if (map1.type() == CV_32FC2 && map2.empty()) + kernelName += "_32FC2"; + else if (map1.type() == CV_16SC2) + { + kernelName += "_16SC2"; + if (!map2.empty()) + kernelName += "_16UC1"; + } + else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1) + kernelName += "_2_32FC1"; + else + CV_Error(Error::StsBadArg, "Unsupported map types"); + + static const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" }; + static const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", + "BORDER_REFLECT_101", "BORDER_TRANSPARENT" }; + String buildOptions = format("-D %s -D %s -D T=%s", interMap[interpolation], borderMap[borderType], ocl::typeToStr(type)); + + if (interpolation != INTER_NEAREST) + { + char cvt[3][40]; + int wdepth = std::max(CV_32F, dst.depth()); + buildOptions = buildOptions + + format(" -D WT=%s -D convertToT=%s -D convertToWT=%s" + " -D convertToWT2=%s -D WT2=%s", + ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), + ocl::convertTypeStr(wdepth, depth, cn, cvt[0]), + ocl::convertTypeStr(depth, wdepth, cn, cvt[1]), + ocl::convertTypeStr(CV_32S, wdepth, 2, cvt[2]), + ocl::typeToStr(CV_MAKE_TYPE(wdepth, 2))); + } + + ocl::Kernel k(kernelName.c_str(), ocl::imgproc::remap_oclsrc, buildOptions); + + Mat scalar(1, 1, type, borderValue); + ocl::KernelArg srcarg = ocl::KernelArg::ReadOnly(src), dstarg = ocl::KernelArg::WriteOnly(dst), + map1arg = ocl::KernelArg::ReadOnlyNoSize(map1), + scalararg = ocl::KernelArg::Constant((void*)scalar.data, scalar.elemSize()); + + if (map2.empty()) + k.args(srcarg, dstarg, map1arg, scalararg); + else + k.args(srcarg, dstarg, map1arg, ocl::KernelArg::ReadOnlyNoSize(map2), scalararg); + + size_t globalThreads[2] = { dst.cols, dst.rows }; + return k.run(2, globalThreads, NULL, false); +} + } void cv::remap( InputArray _src, OutputArray _dst, @@ -3422,11 +3498,13 @@ void cv::remap( InputArray _src, OutputArray _dst, remapLanczos4, float, 1>, 0 }; + CV_Assert( _map1.size().area() > 0 ); + CV_Assert( _map2.empty() || (_map2.size() == _map1.size())); + + if (ocl::useOpenCL() && _dst.isUMat() && ocl_remap(_src, _dst, _map1, _map2, interpolation, borderType, borderValue)) + return; + Mat src = _src.getMat(), map1 = _map1.getMat(), map2 = _map2.getMat(); - - CV_Assert( map1.size().area() > 0 ); - CV_Assert( !map2.data || (map2.size() == map1.size())); - _dst.create( map1.size(), src.type() ); Mat dst = _dst.getMat(); if( dst.data == src.data ) @@ -3809,6 +3887,33 @@ static bool ocl_warpTransform(InputArray _src, OutputArray _dst, InputArray _M0, (!doubleSupport && depth == CV_64F) || cn > 4 || cn == 3) return false; + const char * const interpolationMap[3] = { "NEAREST", "LINEAR", "CUBIC" }; + ocl::ProgramSource2 program = op_type == OCL_OP_AFFINE ? + ocl::imgproc::warp_affine_oclsrc : ocl::imgproc::warp_perspective_oclsrc; + const char * const kernelName = op_type == OCL_OP_AFFINE ? "warpAffine" : "warpPerspective"; + + ocl::Kernel k; + if (interpolation == INTER_NEAREST) + { + k.create(kernelName, program, + format("-D INTER_NEAREST -D T=%s%s", ocl::typeToStr(type), + doubleSupport ? " -D DOUBLE_SUPPORT" : "")); + } + else + { + char cvt[2][50]; + wdepth = std::max(CV_32S, depth); + k.create(kernelName, program, + format("-D INTER_%s -D T=%s -D WT=%s -D depth=%d -D convertToWT=%s -D convertToT=%s%s", + interpolationMap[interpolation], ocl::typeToStr(type), + ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), depth, + ocl::convertTypeStr(depth, wdepth, cn, cvt[0]), + ocl::convertTypeStr(wdepth, depth, cn, cvt[1]), + doubleSupport ? " -D DOUBLE_SUPPORT" : "")); + } + if (k.empty()) + return false; + UMat src = _src.getUMat(), M0; _dst.create( dsize.area() == 0 ? src.size() : dsize, src.type() ); UMat dst = _dst.getUMat(); @@ -3838,32 +3943,7 @@ static bool ocl_warpTransform(InputArray _src, OutputArray _dst, InputArray _M0, } matM.convertTo(M0, doubleSupport ? CV_64F : CV_32F); - const char * const interpolationMap[3] = { "NEAREST", "LINEAR", "CUBIC" }; - ocl::ProgramSource2 program = op_type == OCL_OP_AFFINE ? - ocl::imgproc::warp_affine_oclsrc : ocl::imgproc::warp_perspective_oclsrc; - const char * const kernelName = op_type == OCL_OP_AFFINE ? "warpAffine" : "warpPerspective"; - - ocl::Kernel k; - if (interpolation == INTER_NEAREST) - { - k.create(kernelName, program, - format("-D INTER_NEAREST -D T=%s%s", ocl::typeToStr(type), - doubleSupport ? " -D DOUBLE_SUPPORT" : "")); - } - else - { - char cvt[2][50]; - wdepth = std::max(CV_32S, depth); - k.create(kernelName, program, - format("-D INTER_%s -D T=%s -D WT=%s -D depth=%d -D convertToWT=%s -D convertToT=%s%s", - interpolationMap[interpolation], ocl::typeToStr(type), - ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), depth, - ocl::convertTypeStr(depth, wdepth, cn, cvt[0]), - ocl::convertTypeStr(wdepth, depth, cn, cvt[1]), - doubleSupport ? " -D DOUBLE_SUPPORT" : "")); - } - - k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst), ocl::KernelArg::PtrOnly(M0), + k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst), ocl::KernelArg::PtrReadOnly(M0), ocl::KernelArg::Constant(Mat(1, 1, CV_MAKE_TYPE(wdepth, cn), borderValue))); size_t globalThreads[2] = { dst.cols, dst.rows }; diff --git a/modules/imgproc/src/opencl/remap.cl b/modules/imgproc/src/opencl/remap.cl new file mode 100644 index 000000000..aaa9dc371 --- /dev/null +++ b/modules/imgproc/src/opencl/remap.cl @@ -0,0 +1,435 @@ +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved. +// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// @Authors +// Wu Zailong, bullet@yeah.net +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors as is and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#ifdef DOUBLE_SUPPORT +#ifdef cl_amd_fp64 +#pragma OPENCL EXTENSION cl_amd_fp64:enable +#elif defined (cl_khr_fp64) +#pragma OPENCL EXTENSION cl_khr_fp64:enable +#endif +#endif + +#define noconvert + +enum +{ + INTER_BITS = 5, + INTER_TAB_SIZE = 1 << INTER_BITS, + INTER_TAB_SIZE2 = INTER_TAB_SIZE * INTER_TAB_SIZE +}; + +#ifdef INTER_NEAREST +#define convertToWT +#endif + +#ifdef BORDER_CONSTANT +#define EXTRAPOLATE(v2, v) v = scalar; +#elif defined BORDER_REPLICATE +#define EXTRAPOLATE(v2, v) \ + { \ + v2 = max(min(v2, (int2)(src_cols - 1, src_rows - 1)), (int2)(0)); \ + v = convertToWT(*((__global const T*)(srcptr + mad24(v2.y, src_step, v2.x * (int)sizeof(T) + src_offset)))); \ + } +#elif defined BORDER_WRAP +#define EXTRAPOLATE(v2, v) \ + { \ + if (v2.x < 0) \ + v2.x -= ((v2.x - src_cols + 1) / src_cols) * src_cols; \ + if (v2.x >= src_cols) \ + v2.x %= src_cols; \ + \ + if (v2.y < 0) \ + v2.y -= ((v2.y - src_rows + 1) / src_rows) * src_rows; \ + if( v2.y >= src_rows ) \ + v2.y %= src_rows; \ + v = convertToWT(*((__global const T*)(srcptr + mad24(v2.y, src_step, v2.x * (int)sizeof(T) + src_offset)))); \ + } +#elif defined(BORDER_REFLECT) || defined(BORDER_REFLECT_101) +#ifdef BORDER_REFLECT +#define DELTA int delta = 0 +#else +#define DELTA int delta = 1 +#endif +#define EXTRAPOLATE(v2, v) \ + { \ + DELTA; \ + if (src_cols == 1) \ + v2.x = 0; \ + else \ + do \ + { \ + if( v2.x < 0 ) \ + v2.x = -v2.x - 1 + delta; \ + else \ + v2.x = src_cols - 1 - (v2.x - src_cols) - delta; \ + } \ + while (v2.x >= src_cols || v2.x < 0); \ + \ + if (src_rows == 1) \ + v2.y = 0; \ + else \ + do \ + { \ + if( v2.y < 0 ) \ + v2.y = -v2.y - 1 + delta; \ + else \ + v2.y = src_rows - 1 - (v2.y - src_rows) - delta; \ + } \ + while (v2.y >= src_rows || v2.y < 0); \ + v = convertToWT(*((__global const T*)(srcptr + mad24(v2.y, src_step, v2.x * (int)sizeof(T) + src_offset)))); \ + } +#else +#error No extrapolation method +#endif + +#define NEED_EXTRAPOLATION(gx, gy) (gx >= src_cols || gy >= src_rows || gx < 0 || gy < 0) + +#ifdef INTER_NEAREST + +__kernel void remap_2_32FC1(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * map1ptr, int map1_step, int map1_offset, + __global const uchar * map2ptr, int map2_step, int map2_offset, + T scalar) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int map1_index = mad24(y, map1_step, x * (int)sizeof(float) + map1_offset); + int map2_index = mad24(y, map2_step, x * (int)sizeof(float) + map2_offset); + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + + __global const float * map1 = (__global const float *)(map1ptr + map1_index); + __global const float * map2 = (__global const float *)(map2ptr + map2_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + int gx = convert_int_sat_rte(map1[0]); + int gy = convert_int_sat_rte(map2[0]); + + if (NEED_EXTRAPOLATION(gx, gy)) + { +#ifndef BORDER_CONSTANT + int2 gxy = (int2)(gx, gy); +#endif + EXTRAPOLATE(gxy, dst[0]) + } + else + { + int src_index = mad24(gy, src_step, gx * (int)sizeof(T) + src_offset); + dst[0] = *((__global const T*)(srcptr + src_index)); + } + } +} + +__kernel void remap_32FC2(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * mapptr, int map_step, int map_offset, + T scalar) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + int map_index = mad24(y, map_step, x * (int)sizeof(float2) + map_offset); + + __global const float2 * map = (__global const float2 *)(mapptr + map_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + int2 gxy = convert_int2_sat_rte(map[0]); + int gx = gxy.x, gy = gxy.y; + + if (NEED_EXTRAPOLATION(gx, gy)) + EXTRAPOLATE(gxy, dst[0]) + else + { + int src_index = mad24(gy, src_step, gx * (int)sizeof(T) + src_offset); + dst[0] = *((__global const T *)(srcptr + src_index)); + } + } +} + +__kernel void remap_16SC2(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * mapptr, int map_step, int map_offset, + T scalar) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + int map_index = mad24(y, map_step, x * (int)sizeof(short2) + map_offset); + + __global const short2 * map = (__global const short2 *)(mapptr + map_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + int2 gxy = convert_int2(map[0]); + int gx = gxy.x, gy = gxy.y; + + if (NEED_EXTRAPOLATION(gx, gy)) + EXTRAPOLATE(gxy, dst[0]) + else + { + int src_index = mad24(gy, src_step, gx * (int)sizeof(T) + src_offset); + dst[0] = *((__global const T *)(srcptr + src_index)); + } + } +} + +__kernel void remap_16SC2_16UC1(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * map1ptr, int map1_step, int map1_offset, + __global const uchar * map2ptr, int map2_step, int map2_offset, + T scalar) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + int map1_index = mad24(y, map1_step, x * (int)sizeof(short2) + map1_offset); + int map2_index = mad24(y, map2_step, x * (int)sizeof(ushort) + map2_offset); + + __global const short2 * map1 = (__global const short2 *)(map1ptr + map1_index); + __global const ushort * map2 = (__global const ushort *)(map2ptr + map2_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + int map2Value = convert_int(map2[0]) & (INTER_TAB_SIZE2 - 1); + int dx = (map2Value & (INTER_TAB_SIZE - 1)) < (INTER_TAB_SIZE >> 1) ? 1 : 0; + int dy = (map2Value >> INTER_BITS) < (INTER_TAB_SIZE >> 1) ? 1 : 0; + int2 gxy = convert_int2(map1[0]) + (int2)(dx, dy); + int gx = gxy.x, gy = gxy.y; + + if (NEED_EXTRAPOLATION(gx, gy)) + EXTRAPOLATE(gxy, dst[0]) + else + { + int src_index = mad24(gy, src_step, gx * (int)sizeof(T) + src_offset); + dst[0] = *((__global const T *)(srcptr + src_index)); + } + } +} + +#elif INTER_LINEAR + +__kernel void remap_16SC2_16UC1(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * map1ptr, int map1_step, int map1_offset, + __global const uchar * map2ptr, int map2_step, int map2_offset, + T nVal) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + int map1_index = mad24(y, map1_step, x * (int)sizeof(short2) + map1_offset); + int map2_index = mad24(y, map2_step, x * (int)sizeof(ushort) + map2_offset); + + __global const short2 * map1 = (__global const short2 *)(map1ptr + map1_index); + __global const ushort * map2 = (__global const ushort *)(map2ptr + map2_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + int2 map_dataA = convert_int2(map1[0]); + int2 map_dataB = (int2)(map_dataA.x + 1, map_dataA.y); + int2 map_dataC = (int2)(map_dataA.x, map_dataA.y + 1); + int2 map_dataD = (int2)(map_dataA.x + 1, map_dataA.y + 1); + + ushort map2Value = (ushort)(map2[0] & (INTER_TAB_SIZE2 - 1)); + WT2 u = (WT2)(map2Value & (INTER_TAB_SIZE - 1), map2Value >> INTER_BITS) / (WT2)(INTER_TAB_SIZE); + + WT scalar = convertToWT(nVal); + WT a = scalar, b = scalar, c = scalar, d = scalar; + + if (!NEED_EXTRAPOLATION(map_dataA.x, map_dataA.y)) + a = convertToWT(*((__global const T *)(srcptr + mad24(map_dataA.y, src_step, map_dataA.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataA, a); + + if (!NEED_EXTRAPOLATION(map_dataB.x, map_dataB.y)) + b = convertToWT(*((__global const T *)(srcptr + mad24(map_dataB.y, src_step, map_dataB.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataB, b); + + if (!NEED_EXTRAPOLATION(map_dataC.x, map_dataC.y)) + c = convertToWT(*((__global const T *)(srcptr + mad24(map_dataC.y, src_step, map_dataC.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataC, c); + + if (!NEED_EXTRAPOLATION(map_dataD.x, map_dataD.y)) + d = convertToWT(*((__global const T *)(srcptr + mad24(map_dataD.y, src_step, map_dataD.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataD, d); + + WT dst_data = a * (1 - u.x) * (1 - u.y) + + b * (u.x) * (1 - u.y) + + c * (1 - u.x) * (u.y) + + d * (u.x) * (u.y); + dst[0] = convertToT(dst_data); + } +} + +__kernel void remap_2_32FC1(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * map1ptr, int map1_step, int map1_offset, + __global const uchar * map2ptr, int map2_step, int map2_offset, + T nVal) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + int map1_index = mad24(y, map1_step, x * (int)sizeof(float) + map1_offset); + int map2_index = mad24(y, map2_step, x * (int)sizeof(float) + map2_offset); + + __global const float * map1 = (__global const float *)(map1ptr + map1_index); + __global const float * map2 = (__global const float *)(map2ptr + map2_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + float2 map_data = (float2)(map1[0], map2[0]); + + int2 map_dataA = convert_int2_sat_rtn(map_data); + int2 map_dataB = (int2)(map_dataA.x + 1, map_dataA.y); + int2 map_dataC = (int2)(map_dataA.x, map_dataA.y + 1); + int2 map_dataD = (int2)(map_dataA.x + 1, map_dataA.y + 1); + + float2 _u = map_data - convert_float2(map_dataA); + WT2 u = convertToWT2(convert_int2_rte(convertToWT2(_u) * (WT2)INTER_TAB_SIZE)) / (WT2)INTER_TAB_SIZE; + WT scalar = convertToWT(nVal); + WT a = scalar, b = scalar, c = scalar, d = scalar; + + if (!NEED_EXTRAPOLATION(map_dataA.x, map_dataA.y)) + a = convertToWT(*((__global const T *)(srcptr + mad24(map_dataA.y, src_step, map_dataA.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataA, a); + + if (!NEED_EXTRAPOLATION(map_dataB.x, map_dataB.y)) + b = convertToWT(*((__global const T *)(srcptr + mad24(map_dataB.y, src_step, map_dataB.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataB, b); + + if (!NEED_EXTRAPOLATION(map_dataC.x, map_dataC.y)) + c = convertToWT(*((__global const T *)(srcptr + mad24(map_dataC.y, src_step, map_dataC.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataC, c); + + if (!NEED_EXTRAPOLATION(map_dataD.x, map_dataD.y)) + d = convertToWT(*((__global const T *)(srcptr + mad24(map_dataD.y, src_step, map_dataD.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataD, d); + + WT dst_data = a * (1 - u.x) * (1 - u.y) + + b * (u.x) * (1 - u.y) + + c * (1 - u.x) * (u.y) + + d * (u.x) * (u.y); + dst[0] = convertToT(dst_data); + } +} + +__kernel void remap_32FC2(__global const uchar * srcptr, int src_step, int src_offset, int src_rows, int src_cols, + __global uchar * dstptr, int dst_step, int dst_offset, int dst_rows, int dst_cols, + __global const uchar * mapptr, int map_step, int map_offset, + T nVal) +{ + int x = get_global_id(0); + int y = get_global_id(1); + + if (x < dst_cols && y < dst_rows) + { + int dst_index = mad24(y, dst_step, x * (int)sizeof(T) + dst_offset); + int map_index = mad24(y, map_step, x * (int)sizeof(float2) + map_offset); + + __global const float2 * map = (__global const float2 *)(mapptr + map_index); + __global T * dst = (__global T *)(dstptr + dst_index); + + float2 map_data = map[0]; + int2 map_dataA = convert_int2_sat_rtn(map_data); + int2 map_dataB = (int2)(map_dataA.x + 1, map_dataA.y); + int2 map_dataC = (int2)(map_dataA.x, map_dataA.y + 1); + int2 map_dataD = (int2)(map_dataA.x + 1, map_dataA.y + 1); + + float2 _u = map_data - convert_float2(map_dataA); + WT2 u = convertToWT2(convert_int2_rte(convertToWT2(_u) * (WT2)INTER_TAB_SIZE)) / (WT2)INTER_TAB_SIZE; + WT scalar = convertToWT(nVal); + WT a = scalar, b = scalar, c = scalar, d = scalar; + + if (!NEED_EXTRAPOLATION(map_dataA.x, map_dataA.y)) + a = convertToWT(*((__global const T *)(srcptr + mad24(map_dataA.y, src_step, map_dataA.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataA, a); + + if (!NEED_EXTRAPOLATION(map_dataB.x, map_dataB.y)) + b = convertToWT(*((__global const T *)(srcptr + mad24(map_dataB.y, src_step, map_dataB.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataB, b); + + if (!NEED_EXTRAPOLATION(map_dataC.x, map_dataC.y)) + c = convertToWT(*((__global const T *)(srcptr + mad24(map_dataC.y, src_step, map_dataC.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataC, c); + + if (!NEED_EXTRAPOLATION(map_dataD.x, map_dataD.y)) + d = convertToWT(*((__global const T *)(srcptr + mad24(map_dataD.y, src_step, map_dataD.x * (int)sizeof(T) + src_offset)))); + else + EXTRAPOLATE(map_dataD, d); + + WT dst_data = a * (1 - u.x) * (1 - u.y) + + b * (u.x) * (1 - u.y) + + c * (1 - u.x) * (u.y) + + d * (u.x) * (u.y); + dst[0] = convertToT(dst_data); + } +} + +#endif diff --git a/modules/imgproc/test/ocl/test_warp.cpp b/modules/imgproc/test/ocl/test_warp.cpp index fece5e7e4..2e176f482 100644 --- a/modules/imgproc/test/ocl/test_warp.cpp +++ b/modules/imgproc/test/ocl/test_warp.cpp @@ -60,6 +60,11 @@ namespace cvtest { namespace ocl { +enum +{ + noType = -1 +}; + ///////////////////////////////////////////////////////////////////////////////////////////////// // warpAffine & warpPerspective @@ -69,8 +74,8 @@ PARAM_TEST_CASE(WarpTestBase, MatType, Interpolation, bool, bool) Size dsize; bool useRoi, mapInverse; - TEST_DECLARE_INPUT_PARATEMER(src) - TEST_DECLARE_OUTPUT_PARATEMER(dst) + TEST_DECLARE_INPUT_PARAMETER(src) + TEST_DECLARE_OUTPUT_PARAMETER(dst) virtual void SetUp() { @@ -217,6 +222,100 @@ OCL_TEST_P(Resize, Mat) } } +///////////////////////////////////////////////////////////////////////////////////////////////// +// remap + +PARAM_TEST_CASE(Remap, MatDepth, Channels, std::pair, Border, bool) +{ + int srcType, map1Type, map2Type; + int borderType; + bool useRoi; + + Scalar val; + + TEST_DECLARE_INPUT_PARAMETER(src) + TEST_DECLARE_INPUT_PARAMETER(map1) + TEST_DECLARE_INPUT_PARAMETER(map2) + TEST_DECLARE_OUTPUT_PARAMETER(dst) + + virtual void SetUp() + { + srcType = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1)); + map1Type = GET_PARAM(2).first; + map2Type = GET_PARAM(2).second; + borderType = GET_PARAM(3); + useRoi = GET_PARAM(4); + } + + void random_roi() + { + val = randomScalar(-MAX_VALUE, MAX_VALUE); + Size srcROISize = randomSize(1, MAX_VALUE); + Size dstROISize = randomSize(1, MAX_VALUE); + + Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0); + randomSubMat(src, src_roi, srcROISize, srcBorder, srcType, 5, 256); + + Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0); + randomSubMat(dst, dst_roi, dstROISize, dstBorder, srcType, -MAX_VALUE, MAX_VALUE); + + int mapMaxValue = MAX_VALUE << 2; + Border map1Border = randomBorder(0, useRoi ? MAX_VALUE : 0); + randomSubMat(map1, map1_roi, dstROISize, map1Border, map1Type, -mapMaxValue, mapMaxValue); + + Border map2Border = randomBorder(0, useRoi ? MAX_VALUE : 0); + if (map2Type != noType) + { + int mapMinValue = -mapMaxValue; + if (map2Type == CV_16UC1 || map2Type == CV_16SC1) + mapMinValue = 0, mapMaxValue = INTER_TAB_SIZE2; + randomSubMat(map2, map2_roi, dstROISize, map2Border, map2Type, mapMinValue, mapMaxValue); + } + + UMAT_UPLOAD_INPUT_PARAMETER(src) + UMAT_UPLOAD_INPUT_PARAMETER(map1) + UMAT_UPLOAD_OUTPUT_PARAMETER(dst) + if (noType != map2Type) + UMAT_UPLOAD_INPUT_PARAMETER(map2) + } + + void Near(double threshold = 0.0) + { + EXPECT_MAT_NEAR(dst, udst, threshold); + EXPECT_MAT_NEAR(dst_roi, udst_roi, threshold); + } +}; + +typedef Remap Remap_INTER_NEAREST; + +OCL_TEST_P(Remap_INTER_NEAREST, Mat) +{ + for (int j = 0; j < test_loop_times; j++) + { + random_roi(); + + OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_NEAREST, borderType, val)); + OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_NEAREST, borderType, val)); + + Near(1.0); + } +} + +typedef Remap Remap_INTER_LINEAR; + +OCL_TEST_P(Remap_INTER_LINEAR, Mat) +{ + for (int j = 0; j < test_loop_times; j++) + { + random_roi(); + + OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_LINEAR, borderType, val)); + OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_LINEAR, borderType, val)); + + Near(2.0); + } +} + ///////////////////////////////////////////////////////////////////////////////////// OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine( @@ -245,6 +344,33 @@ OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResizeArea, Resize, Combine( Values((Interpolation)INTER_AREA), Bool())); +OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_LINEAR, Combine( + Values(CV_8U, CV_16U, CV_32F), + Values(1, 4), + Values(std::pair((MatType)CV_32FC1, (MatType)CV_32FC1), + std::pair((MatType)CV_16SC2, (MatType)CV_16UC1), + std::pair((MatType)CV_32FC2, noType)), + Values((Border)BORDER_CONSTANT, + (Border)BORDER_REPLICATE, + (Border)BORDER_WRAP, + (Border)BORDER_REFLECT, + (Border)BORDER_REFLECT_101), + Bool())); + +OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_NEAREST, Combine( + Values(CV_8U, CV_16U, CV_32F), + Values(1, 4), + Values(std::pair((MatType)CV_32FC1, (MatType)CV_32FC1), + std::pair((MatType)CV_32FC2, noType), + std::pair((MatType)CV_16SC2, (MatType)CV_16UC1), + std::pair((MatType)CV_16SC2, noType)), + Values((Border)BORDER_CONSTANT, + (Border)BORDER_REPLICATE, + (Border)BORDER_WRAP, + (Border)BORDER_REFLECT, + (Border)BORDER_REFLECT_101), + Bool())); + } } // namespace cvtest::ocl #endif // HAVE_OPENCL diff --git a/modules/ts/include/opencv2/ts/ocl_test.hpp b/modules/ts/include/opencv2/ts/ocl_test.hpp index 33520b9b2..81eae0138 100644 --- a/modules/ts/include/opencv2/ts/ocl_test.hpp +++ b/modules/ts/include/opencv2/ts/ocl_test.hpp @@ -306,6 +306,7 @@ IMPLEMENT_PARAM_CLASS(Channels, int) #define OCL_ALL_CHANNELS Values(1, 2, 3, 4) CV_ENUM(Interpolation, INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA) +CV_ENUM(Border, BORDER_CONSTANT, BORDER_REPLICATE, BORDER_WRAP, BORDER_REFLECT, BORDER_REFLECT_101) #define OCL_INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator) \ INSTANTIATE_TEST_CASE_P(OCL_ ## prefix, test_case_name, generator)