2018-02-06 20:02:51 +08:00
|
|
|
// This file is part of OpenCV project.
|
|
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
|
|
// of this distribution and at http://opencv.org/license.html
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
#include "precomp.hpp"
|
2014-08-01 22:11:20 +08:00
|
|
|
#include "opencl_kernels_core.hpp"
|
2017-06-06 21:26:51 +08:00
|
|
|
#include "convert.hpp"
|
2016-11-12 02:47:46 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
namespace cv {
|
2015-12-03 19:43:37 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
/*namespace hal {
|
2017-04-21 19:52:45 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
void cvt16f32f( const float16_t* src, float* dst, int len )
|
2017-04-21 19:52:45 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
int j = 0;
|
|
|
|
#if CV_SIMD
|
|
|
|
const int VECSZ = v_float32::nlanes;
|
|
|
|
for( ; j < len; j += VECSZ )
|
2017-04-21 19:52:45 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j > len - VECSZ )
|
2017-04-21 19:52:45 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j == 0 )
|
|
|
|
break;
|
|
|
|
j = len - VECSZ;
|
2017-04-21 19:52:45 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
v_store(dst + j, vx_load_expand(src + j));
|
2017-04-21 19:52:45 +08:00
|
|
|
}
|
2017-12-15 05:00:35 +08:00
|
|
|
#endif
|
2018-09-07 00:36:59 +08:00
|
|
|
for( ; j < len; j++ )
|
|
|
|
dst[j] = (float)src[j];
|
|
|
|
}
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
void cvt32f16f( const float* src, float16_t* dst, int len )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
int j = 0;
|
|
|
|
#if CV_SIMD
|
|
|
|
const int VECSZ = v_float32::nlanes;
|
|
|
|
for( ; j < len; j += VECSZ )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j > len - VECSZ )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j == 0 )
|
|
|
|
break;
|
|
|
|
j = len - VECSZ;
|
2015-01-12 15:59:29 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
v_pack_store(dst + j, vx_load(src + j));
|
2015-01-12 15:59:29 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
#endif
|
|
|
|
for( ; j < len; j++ )
|
|
|
|
dst[j] = float16_t(src[j]);
|
|
|
|
}
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
/*void addRNGBias32f( float* arr, const float* scaleBiasPairs, int len )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
// the loop is simple enough, so we let the compiler to vectorize it
|
|
|
|
for( int i = 0; i < len; i++ )
|
|
|
|
arr[i] = scaleBiasPairs[i*2 + 1];
|
|
|
|
}
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
void addRNGBias64f( double* arr, const double* scaleBiasPairs, int len )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
// the loop is simple enough, so we let the compiler to vectorize it
|
|
|
|
for( int i = 0; i < len; i++ )
|
|
|
|
arr[i] = scaleBiasPairs[i*2 + 1];
|
|
|
|
}
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
}*/
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
template<typename _Ts, typename _Td, typename _Twvec> inline void
|
|
|
|
cvt_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
sstep /= sizeof(src[0]);
|
|
|
|
dstep /= sizeof(dst[0]);
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
for( int i = 0; i < size.height; i++, src += sstep, dst += dstep )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
int j = 0;
|
|
|
|
#if CV_SIMD
|
|
|
|
const int VECSZ = _Twvec::nlanes*2;
|
|
|
|
for( ; j < size.width; j += VECSZ )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j > size.width - VECSZ )
|
2017-12-15 05:00:35 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j == 0 || src == (_Ts*)dst )
|
|
|
|
break;
|
|
|
|
j = size.width - VECSZ;
|
2017-12-15 05:00:35 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
_Twvec v0, v1;
|
|
|
|
vx_load_pair_as(src + j, v0, v1);
|
|
|
|
v_store_pair_as(dst + j, v0, v1);
|
2015-01-12 15:59:29 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
#endif
|
|
|
|
for( ; j < size.width; j++ )
|
|
|
|
dst[j] = saturate_cast<_Td>(src[j]);
|
2015-01-12 15:59:29 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
}
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
// in order to reduce the code size, for (16f <-> ...) conversions
|
|
|
|
// we add a conversion function without loop unrolling
|
|
|
|
template<typename _Ts, typename _Td, typename _Twvec> inline void
|
|
|
|
cvt1_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
sstep /= sizeof(src[0]);
|
|
|
|
dstep /= sizeof(dst[0]);
|
2015-01-12 15:59:29 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
for( int i = 0; i < size.height; i++, src += sstep, dst += dstep )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
int j = 0;
|
|
|
|
#if CV_SIMD
|
|
|
|
const int VECSZ = _Twvec::nlanes;
|
|
|
|
for( ; j < size.width; j += VECSZ )
|
2015-01-12 15:59:29 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j > size.width - VECSZ )
|
2017-12-15 05:00:35 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
if( j == 0 || src == (_Ts*)dst )
|
|
|
|
break;
|
|
|
|
j = size.width - VECSZ;
|
2017-12-15 05:00:35 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
_Twvec v;
|
|
|
|
vx_load_as(src + j, v);
|
|
|
|
v_store_as(dst + j, v);
|
2015-01-12 15:59:29 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
vx_cleanup();
|
2018-02-06 20:02:51 +08:00
|
|
|
#endif
|
2018-09-07 00:36:59 +08:00
|
|
|
for( ; j < size.width; j++ )
|
|
|
|
dst[j] = saturate_cast<_Td>(src[j]);
|
2018-02-06 20:02:51 +08:00
|
|
|
}
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
static void cvtCopy( const uchar* src, size_t sstep,
|
|
|
|
uchar* dst, size_t dstep, Size size, size_t elemsize)
|
2011-04-17 21:14:45 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
size_t len = size.width*elemsize;
|
|
|
|
for( int i = 0; i < size.height; i++, src += sstep, dst += dstep )
|
2018-02-06 20:02:51 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
memcpy( dst, src, len );
|
2018-02-06 20:02:51 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-28 04:57:36 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
#define DEF_CVT_FUNC(suffix, cvtfunc, _Ts, _Td, _Twvec) \
|
|
|
|
static void cvt##suffix(const _Ts* src, size_t sstep, uchar*, size_t, \
|
|
|
|
_Td* dst, size_t dstep, Size size, void*) \
|
|
|
|
{ cvtfunc<_Ts, _Td, _Twvec>(src, sstep, dst, dstep, size); }
|
|
|
|
|
|
|
|
////////////////////// 8u -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(8u8s, cvt_, uchar, schar, v_int16)
|
|
|
|
DEF_CVT_FUNC(8u16u, cvt_, uchar, ushort, v_uint16)
|
|
|
|
DEF_CVT_FUNC(8u16s, cvt_, uchar, short, v_int16)
|
|
|
|
DEF_CVT_FUNC(8u32s, cvt_, uchar, int, v_int32)
|
|
|
|
DEF_CVT_FUNC(8u32f, cvt_, uchar, float, v_float32)
|
|
|
|
DEF_CVT_FUNC(8u64f, cvt_, uchar, double, v_int32)
|
|
|
|
//DEF_CVT_FUNC(8u16f, cvt1_, uchar, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 8s -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(8s8u, cvt_, schar, uchar, v_int16)
|
|
|
|
DEF_CVT_FUNC(8s16u, cvt_, schar, ushort, v_uint16)
|
|
|
|
DEF_CVT_FUNC(8s16s, cvt_, schar, short, v_int16)
|
|
|
|
DEF_CVT_FUNC(8s32s, cvt_, schar, int, v_int32)
|
|
|
|
DEF_CVT_FUNC(8s32f, cvt_, schar, float, v_float32)
|
|
|
|
DEF_CVT_FUNC(8s64f, cvt_, schar, double, v_int32)
|
|
|
|
//DEF_CVT_FUNC(8s16f, cvt1_, schar, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 16u -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(16u8u, cvt_, ushort, uchar, v_uint16)
|
|
|
|
DEF_CVT_FUNC(16u8s, cvt_, ushort, schar, v_uint16)
|
|
|
|
DEF_CVT_FUNC(16u16s, cvt_, ushort, short, v_int32)
|
|
|
|
DEF_CVT_FUNC(16u32s, cvt_, ushort, int, v_int32)
|
|
|
|
DEF_CVT_FUNC(16u32f, cvt_, ushort, float, v_float32)
|
|
|
|
DEF_CVT_FUNC(16u64f, cvt_, ushort, double, v_int32)
|
|
|
|
//DEF_CVT_FUNC(16u16f, cvt1_,ushort, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 16s -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(16s8u, cvt_, short, uchar, v_int16)
|
|
|
|
DEF_CVT_FUNC(16s8s, cvt_, short, schar, v_int16)
|
|
|
|
DEF_CVT_FUNC(16s16u, cvt_, short, ushort, v_int32)
|
|
|
|
DEF_CVT_FUNC(16s32s, cvt_, short, int, v_int32)
|
|
|
|
DEF_CVT_FUNC(16s32f, cvt_, short, float, v_float32)
|
|
|
|
DEF_CVT_FUNC(16s64f, cvt_, short, double, v_int32)
|
|
|
|
//DEF_CVT_FUNC(16s16f, cvt1_,short, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 32s -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(32s8u, cvt_, int, uchar, v_int32)
|
|
|
|
DEF_CVT_FUNC(32s8s, cvt_, int, schar, v_int32)
|
|
|
|
DEF_CVT_FUNC(32s16u, cvt_, int, ushort, v_int32)
|
|
|
|
DEF_CVT_FUNC(32s16s, cvt_, int, short, v_int32)
|
|
|
|
DEF_CVT_FUNC(32s32f, cvt_, int, float, v_float32)
|
|
|
|
DEF_CVT_FUNC(32s64f, cvt_, int, double, v_int32)
|
|
|
|
//DEF_CVT_FUNC(32s16f, cvt1_,int, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 32f -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(32f8u, cvt_, float, uchar, v_float32)
|
|
|
|
DEF_CVT_FUNC(32f8s, cvt_, float, schar, v_float32)
|
|
|
|
DEF_CVT_FUNC(32f16u, cvt_, float, ushort, v_float32)
|
|
|
|
DEF_CVT_FUNC(32f16s, cvt_, float, short, v_float32)
|
|
|
|
DEF_CVT_FUNC(32f32s, cvt_, float, int, v_float32)
|
|
|
|
DEF_CVT_FUNC(32f64f, cvt_, float, double, v_float32)
|
|
|
|
DEF_CVT_FUNC(32f16f, cvt1_,float, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 64f -> ... ////////////////////////
|
|
|
|
|
|
|
|
DEF_CVT_FUNC(64f8u, cvt_, double, uchar, v_int32)
|
|
|
|
DEF_CVT_FUNC(64f8s, cvt_, double, schar, v_int32)
|
|
|
|
DEF_CVT_FUNC(64f16u, cvt_, double, ushort, v_int32)
|
|
|
|
DEF_CVT_FUNC(64f16s, cvt_, double, short, v_int32)
|
|
|
|
DEF_CVT_FUNC(64f32s, cvt_, double, int, v_int32)
|
|
|
|
DEF_CVT_FUNC(64f32f, cvt_, double, float, v_float32)
|
|
|
|
//DEF_CVT_FUNC(64f16f, cvt1_,double, float16_t, v_float32)
|
|
|
|
|
|
|
|
////////////////////// 16f -> ... ////////////////////////
|
|
|
|
|
|
|
|
//DEF_CVT_FUNC(16f8u, cvt_, float16_t, uchar, v_float32)
|
|
|
|
//DEF_CVT_FUNC(16f8s, cvt_, float16_t, schar, v_float32)
|
|
|
|
//DEF_CVT_FUNC(16f16u, cvt1_, float16_t, ushort, v_float32)
|
|
|
|
//DEF_CVT_FUNC(16f16s, cvt1_, float16_t, short, v_float32)
|
|
|
|
//DEF_CVT_FUNC(16f32s, cvt1_, float16_t, int, v_float32)
|
|
|
|
DEF_CVT_FUNC(16f32f, cvt1_, float16_t, float, v_float32)
|
|
|
|
//DEF_CVT_FUNC(16f64f, cvt1_, float16_t, double, v_float32)
|
|
|
|
|
|
|
|
///////////// "conversion" w/o conversion ///////////////
|
|
|
|
|
|
|
|
static void cvt8u(const uchar* src, size_t sstep, uchar*, size_t, uchar* dst, size_t dstep, Size size, void*)
|
|
|
|
{ cvtCopy(src, sstep, dst, dstep, size, 1); }
|
|
|
|
|
|
|
|
static void cvt16u(const ushort* src, size_t sstep, uchar*, size_t, ushort* dst, size_t dstep, Size size, void*)
|
|
|
|
{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 2); }
|
|
|
|
|
|
|
|
static void cvt32s(const int* src, size_t sstep, uchar*, size_t, int* dst, size_t dstep, Size size, void*)
|
|
|
|
{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 4); }
|
|
|
|
|
|
|
|
static void cvt64s(const int64* src, size_t sstep, uchar*, size_t, int64* dst, size_t dstep, Size size, void*)
|
|
|
|
{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 8); }
|
|
|
|
|
|
|
|
|
|
|
|
/* [TODO] Recover IPP calls
|
2018-02-06 20:02:51 +08:00
|
|
|
#if defined(HAVE_IPP)
|
|
|
|
#define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \
|
|
|
|
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
|
|
|
|
dtype* dst, size_t dstep, Size size, double*) \
|
|
|
|
{ \
|
|
|
|
CV_IPP_RUN(src && dst, CV_INSTRUMENT_FUN_IPP(ippiConvert_##ippFavor, src, (int)sstep, dst, (int)dstep, ippiSize(size.width, size.height)) >= 0) \
|
|
|
|
cvt_(src, sstep, dst, dstep, size); \
|
2013-11-28 04:57:36 +08:00
|
|
|
}
|
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
#define DEF_CVT_FUNC_F2(suffix, stype, dtype, ippFavor) \
|
|
|
|
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
|
|
|
|
dtype* dst, size_t dstep, Size size, double*) \
|
|
|
|
{ \
|
|
|
|
CV_IPP_RUN(src && dst, CV_INSTRUMENT_FUN_IPP(ippiConvert_##ippFavor, src, (int)sstep, dst, (int)dstep, ippiSize(size.width, size.height), ippRndFinancial, 0) >= 0) \
|
|
|
|
cvt_(src, sstep, dst, dstep, size); \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \
|
|
|
|
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
|
|
|
|
dtype* dst, size_t dstep, Size size, double*) \
|
|
|
|
{ \
|
|
|
|
cvt_(src, sstep, dst, dstep, size); \
|
|
|
|
}
|
|
|
|
#define DEF_CVT_FUNC_F2 DEF_CVT_FUNC_F
|
2014-01-25 01:03:31 +08:00
|
|
|
#endif
|
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
#define DEF_CVT_FUNC(suffix, stype, dtype) \
|
|
|
|
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
|
|
|
|
dtype* dst, size_t dstep, Size size, double*) \
|
|
|
|
{ \
|
|
|
|
cvt_(src, sstep, dst, dstep, size); \
|
|
|
|
}
|
2016-11-02 02:14:21 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
#define DEF_CPY_FUNC(suffix, stype) \
|
|
|
|
static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
|
|
|
|
stype* dst, size_t dstep, Size size, double*) \
|
|
|
|
{ \
|
|
|
|
cpy_(src, sstep, dst, dstep, size); \
|
2016-11-02 02:14:21 +08:00
|
|
|
}
|
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CPY_FUNC(8u, uchar)
|
|
|
|
DEF_CVT_FUNC_F(8s8u, schar, uchar, 8s8u_C1Rs)
|
|
|
|
DEF_CVT_FUNC_F(16u8u, ushort, uchar, 16u8u_C1R)
|
|
|
|
DEF_CVT_FUNC_F(16s8u, short, uchar, 16s8u_C1R)
|
|
|
|
DEF_CVT_FUNC_F(32s8u, int, uchar, 32s8u_C1R)
|
|
|
|
DEF_CVT_FUNC_F2(32f8u, float, uchar, 32f8u_C1RSfs)
|
|
|
|
DEF_CVT_FUNC(64f8u, double, uchar)
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CVT_FUNC_F2(8u8s, uchar, schar, 8u8s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC_F2(16u8s, ushort, schar, 16u8s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC_F2(16s8s, short, schar, 16s8s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC_F(32s8s, int, schar, 32s8s_C1R)
|
|
|
|
DEF_CVT_FUNC_F2(32f8s, float, schar, 32f8s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC(64f8s, double, schar)
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CVT_FUNC_F(8u16u, uchar, ushort, 8u16u_C1R)
|
|
|
|
DEF_CVT_FUNC_F(8s16u, schar, ushort, 8s16u_C1Rs)
|
|
|
|
DEF_CPY_FUNC(16u, ushort)
|
|
|
|
DEF_CVT_FUNC_F(16s16u, short, ushort, 16s16u_C1Rs)
|
|
|
|
DEF_CVT_FUNC_F2(32s16u, int, ushort, 32s16u_C1RSfs)
|
|
|
|
DEF_CVT_FUNC_F2(32f16u, float, ushort, 32f16u_C1RSfs)
|
|
|
|
DEF_CVT_FUNC(64f16u, double, ushort)
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CVT_FUNC_F(8u16s, uchar, short, 8u16s_C1R)
|
|
|
|
DEF_CVT_FUNC_F(8s16s, schar, short, 8s16s_C1R)
|
|
|
|
DEF_CVT_FUNC_F2(16u16s, ushort, short, 16u16s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC_F2(32s16s, int, short, 32s16s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC(32f16s, float, short)
|
|
|
|
DEF_CVT_FUNC(64f16s, double, short)
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CVT_FUNC_F(8u32s, uchar, int, 8u32s_C1R)
|
|
|
|
DEF_CVT_FUNC_F(8s32s, schar, int, 8s32s_C1R)
|
|
|
|
DEF_CVT_FUNC_F(16u32s, ushort, int, 16u32s_C1R)
|
|
|
|
DEF_CVT_FUNC_F(16s32s, short, int, 16s32s_C1R)
|
|
|
|
DEF_CPY_FUNC(32s, int)
|
|
|
|
DEF_CVT_FUNC_F2(32f32s, float, int, 32f32s_C1RSfs)
|
|
|
|
DEF_CVT_FUNC(64f32s, double, int)
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CVT_FUNC_F(8u32f, uchar, float, 8u32f_C1R)
|
|
|
|
DEF_CVT_FUNC_F(8s32f, schar, float, 8s32f_C1R)
|
|
|
|
DEF_CVT_FUNC_F(16u32f, ushort, float, 16u32f_C1R)
|
|
|
|
DEF_CVT_FUNC_F(16s32f, short, float, 16s32f_C1R)
|
|
|
|
DEF_CVT_FUNC_F(32s32f, int, float, 32s32f_C1R)
|
|
|
|
DEF_CVT_FUNC(64f32f, double, float)
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
DEF_CVT_FUNC(8u64f, uchar, double)
|
|
|
|
DEF_CVT_FUNC(8s64f, schar, double)
|
|
|
|
DEF_CVT_FUNC(16u64f, ushort, double)
|
|
|
|
DEF_CVT_FUNC(16s64f, short, double)
|
|
|
|
DEF_CVT_FUNC(32s64f, int, double)
|
|
|
|
DEF_CVT_FUNC(32f64f, float, double)
|
|
|
|
DEF_CPY_FUNC(64s, int64)
|
2018-09-07 00:36:59 +08:00
|
|
|
*/
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
BinaryFunc getConvertFunc(int sdepth, int ddepth)
|
|
|
|
{
|
|
|
|
static BinaryFunc cvtTab[][8] =
|
|
|
|
{
|
2017-04-21 19:52:45 +08:00
|
|
|
{
|
2018-02-06 20:02:51 +08:00
|
|
|
(BinaryFunc)(cvt8u), (BinaryFunc)GET_OPTIMIZED(cvt8s8u), (BinaryFunc)GET_OPTIMIZED(cvt16u8u),
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt16s8u), (BinaryFunc)GET_OPTIMIZED(cvt32s8u), (BinaryFunc)GET_OPTIMIZED(cvt32f8u),
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt64f8u), 0 //(BinaryFunc)(cvt16f8u)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt8u8s), (BinaryFunc)cvt8u, (BinaryFunc)GET_OPTIMIZED(cvt16u8s),
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt16s8s), (BinaryFunc)GET_OPTIMIZED(cvt32s8s), (BinaryFunc)GET_OPTIMIZED(cvt32f8s),
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt64f8s), 0 //(BinaryFunc)(cvt16f8s)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt8u16u), (BinaryFunc)GET_OPTIMIZED(cvt8s16u), (BinaryFunc)cvt16u,
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt16s16u), (BinaryFunc)GET_OPTIMIZED(cvt32s16u), (BinaryFunc)GET_OPTIMIZED(cvt32f16u),
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt64f16u), 0 //(BinaryFunc)(cvt16f16u)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt8u16s), (BinaryFunc)GET_OPTIMIZED(cvt8s16s), (BinaryFunc)GET_OPTIMIZED(cvt16u16s),
|
|
|
|
(BinaryFunc)cvt16u, (BinaryFunc)GET_OPTIMIZED(cvt32s16s), (BinaryFunc)GET_OPTIMIZED(cvt32f16s),
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt64f16s), 0 //(BinaryFunc)(cvt16f16s)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt8u32s), (BinaryFunc)GET_OPTIMIZED(cvt8s32s), (BinaryFunc)GET_OPTIMIZED(cvt16u32s),
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt16s32s), (BinaryFunc)cvt32s, (BinaryFunc)GET_OPTIMIZED(cvt32f32s),
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt64f32s), 0 //(BinaryFunc)(cvt16f32s)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt8u32f), (BinaryFunc)GET_OPTIMIZED(cvt8s32f), (BinaryFunc)GET_OPTIMIZED(cvt16u32f),
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt16s32f), (BinaryFunc)GET_OPTIMIZED(cvt32s32f), (BinaryFunc)cvt32s,
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt64f32f), 0 //(BinaryFunc)(cvt16f32f)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt8u64f), (BinaryFunc)GET_OPTIMIZED(cvt8s64f), (BinaryFunc)GET_OPTIMIZED(cvt16u64f),
|
|
|
|
(BinaryFunc)GET_OPTIMIZED(cvt16s64f), (BinaryFunc)GET_OPTIMIZED(cvt32s64f), (BinaryFunc)GET_OPTIMIZED(cvt32f64f),
|
2018-09-07 00:36:59 +08:00
|
|
|
(BinaryFunc)(cvt64s), 0 //(BinaryFunc)(cvt16f64f)
|
2018-02-06 20:02:51 +08:00
|
|
|
},
|
2014-04-22 23:52:37 +08:00
|
|
|
{
|
2018-02-06 20:02:51 +08:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0
|
2018-09-07 00:36:59 +08:00
|
|
|
//(BinaryFunc)(cvt8u16f), (BinaryFunc)(cvt8s16f), (BinaryFunc)(cvt16u16f), (BinaryFunc)(cvt16s16f),
|
|
|
|
//(BinaryFunc)(cvt32s16f), (BinaryFunc)(cvt32f16f), (BinaryFunc)(cvt64f16f), (BinaryFunc)(cvt16u)
|
2014-04-22 23:52:37 +08:00
|
|
|
}
|
2018-02-06 20:02:51 +08:00
|
|
|
};
|
|
|
|
return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
|
|
|
|
}
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
static bool ocl_convertFp16( InputArray _src, OutputArray _dst, int sdepth, int ddepth )
|
2018-02-06 20:02:51 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
int type = _src.type(), cn = CV_MAT_CN(type);
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
_dst.createSameSize( _src, CV_MAKETYPE(ddepth, cn) );
|
|
|
|
int kercn = 1;
|
|
|
|
int rowsPerWI = 1;
|
|
|
|
String build_opt = format("-D HALF_SUPPORT -D srcT=%s -D dstT=%s -D rowsPerWI=%d%s",
|
|
|
|
sdepth == CV_32F ? "float" : "half",
|
|
|
|
sdepth == CV_32F ? "half" : "float",
|
|
|
|
rowsPerWI,
|
|
|
|
sdepth == CV_32F ? " -D FLOAT_TO_HALF " : "");
|
|
|
|
ocl::Kernel k("convertFp16", ocl::core::halfconvert_oclsrc, build_opt);
|
|
|
|
if (k.empty())
|
2018-02-06 20:02:51 +08:00
|
|
|
return false;
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
UMat src = _src.getUMat();
|
|
|
|
UMat dst = _dst.getUMat();
|
2015-06-24 18:50:17 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
|
|
|
|
dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn);
|
2015-06-24 18:50:17 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
k.args(srcarg, dstarg);
|
2017-04-21 19:52:45 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
size_t globalsize[2] = { (size_t)src.cols * cn / kercn, ((size_t)src.rows + rowsPerWI - 1) / rowsPerWI };
|
|
|
|
return k.run(2, globalsize, NULL, false);
|
2015-06-24 18:50:17 +08:00
|
|
|
}
|
2017-04-21 19:52:45 +08:00
|
|
|
#endif
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
} // cv::
|
2018-02-06 20:02:51 +08:00
|
|
|
|
|
|
|
void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
|
2014-04-22 23:52:37 +08:00
|
|
|
{
|
2018-02-06 20:02:51 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
|
|
|
|
2018-06-01 00:36:39 +08:00
|
|
|
if( empty() )
|
|
|
|
{
|
|
|
|
_dst.release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
if( _type < 0 )
|
|
|
|
_type = _dst.fixedType() ? _dst.type() : type();
|
|
|
|
else
|
|
|
|
_type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
|
|
|
|
if( sdepth == ddepth && noScale )
|
2014-04-22 23:52:37 +08:00
|
|
|
{
|
2018-02-06 20:02:51 +08:00
|
|
|
copyTo(_dst);
|
|
|
|
return;
|
2014-04-22 23:52:37 +08:00
|
|
|
}
|
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
Mat src = *this;
|
|
|
|
if( dims <= 2 )
|
|
|
|
_dst.create( size(), _type );
|
|
|
|
else
|
|
|
|
_dst.create( dims, size, _type );
|
|
|
|
Mat dst = _dst.getMat();
|
2014-04-22 23:52:37 +08:00
|
|
|
|
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
|
|
|
|
double scale[] = {alpha, beta};
|
|
|
|
int cn = channels();
|
|
|
|
CV_Assert( func != 0 );
|
2014-04-22 23:52:37 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
if( dims <= 2 )
|
|
|
|
{
|
|
|
|
Size sz = getContinuousSize(src, dst, cn);
|
|
|
|
func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-22 23:52:37 +08:00
|
|
|
const Mat* arrays[] = {&src, &dst, 0};
|
2018-09-04 21:44:47 +08:00
|
|
|
uchar* ptrs[2] = {};
|
2014-04-22 23:52:37 +08:00
|
|
|
NAryMatIterator it(arrays, ptrs);
|
2018-02-06 20:02:51 +08:00
|
|
|
Size sz((int)(it.size*cn), 1);
|
2014-04-22 23:52:37 +08:00
|
|
|
|
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
2018-02-06 20:02:51 +08:00
|
|
|
func(ptrs[0], 1, 0, 0, ptrs[1], 1, sz, scale);
|
2014-04-22 23:52:37 +08:00
|
|
|
}
|
2014-01-25 01:03:31 +08:00
|
|
|
}
|
2013-11-28 04:57:36 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
//==================================================================================================
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
void cv::convertFp16( InputArray _src, OutputArray _dst )
|
2018-02-06 20:02:51 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
CV_INSTRUMENT_REGION()
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
int sdepth = _src.depth(), ddepth = 0;
|
|
|
|
BinaryFunc func = 0;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2018-09-07 00:36:59 +08:00
|
|
|
switch( sdepth )
|
2018-02-06 20:02:51 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
case CV_32F:
|
|
|
|
if(_dst.fixedType())
|
2018-02-06 20:02:51 +08:00
|
|
|
{
|
2018-09-07 00:36:59 +08:00
|
|
|
ddepth = _dst.depth();
|
|
|
|
CV_Assert(ddepth == CV_16S /*|| ddepth == CV_16F*/);
|
|
|
|
CV_Assert(_dst.channels() == _src.channels());
|
2018-02-06 20:02:51 +08:00
|
|
|
}
|
2018-09-07 00:36:59 +08:00
|
|
|
else
|
|
|
|
ddepth = CV_16S;
|
|
|
|
func = (BinaryFunc)cvt32f16f;
|
2018-02-06 20:02:51 +08:00
|
|
|
break;
|
|
|
|
case CV_16S:
|
2018-09-07 00:36:59 +08:00
|
|
|
//case CV_16F:
|
2018-02-06 20:02:51 +08:00
|
|
|
ddepth = CV_32F;
|
2018-09-07 00:36:59 +08:00
|
|
|
func = (BinaryFunc)cvt16f32f;
|
2018-02-06 20:02:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error(Error::StsUnsupportedFormat, "Unsupported input depth");
|
|
|
|
return;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
|
2018-09-07 00:36:59 +08:00
|
|
|
ocl_convertFp16(_src, _dst, sdepth, ddepth))
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2015-12-25 20:33:06 +08:00
|
|
|
Mat src = _src.getMat();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
int type = CV_MAKETYPE(ddepth, src.channels());
|
|
|
|
_dst.create( src.dims, src.size, type );
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
int cn = src.channels();
|
2018-09-07 00:36:59 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
CV_Assert( func != 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
if( src.dims <= 2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-02-06 20:02:51 +08:00
|
|
|
Size sz = getContinuousSize(src, dst, cn);
|
2018-09-07 00:36:59 +08:00
|
|
|
func( src.data, src.step, 0, 0, dst.data, dst.step, sz, 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-06 20:02:51 +08:00
|
|
|
const Mat* arrays[] = {&src, &dst, 0};
|
2018-09-04 21:44:47 +08:00
|
|
|
uchar* ptrs[2] = {};
|
2018-02-06 20:02:51 +08:00
|
|
|
NAryMatIterator it(arrays, ptrs);
|
|
|
|
Size sz((int)(it.size*cn), 1);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-02-06 20:02:51 +08:00
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
2018-09-07 00:36:59 +08:00
|
|
|
func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, 0);
|
2018-02-06 20:02:51 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|