used new device layer for cv::gpu::subtract

This commit is contained in:
Vladislav Vinogradov
2013-07-29 15:17:38 +04:00
parent 9c5da2ea22
commit 156f86ea0b
3 changed files with 301 additions and 446 deletions

View File

@@ -348,248 +348,9 @@ void cv::cuda::add(InputArray src1, InputArray src2, OutputArray dst, InputArray
////////////////////////////////////////////////////////////////////////
// subtract
namespace arithm
{
void subMat_v4(PtrStepSz<unsigned int> src1, PtrStepSz<unsigned int> src2, PtrStepSz<unsigned int> dst, cudaStream_t stream);
void subMat_v2(PtrStepSz<unsigned int> src1, PtrStepSz<unsigned int> src2, PtrStepSz<unsigned int> dst, cudaStream_t stream);
void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int);
template <typename T, typename D>
void subMat(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
}
static void subMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int)
{
typedef void (*func_t)(PtrStepSzb src1, PtrStepSzb src2, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
static const func_t funcs[7][7] =
{
{
arithm::subMat<unsigned char, unsigned char>,
arithm::subMat<unsigned char, signed char>,
arithm::subMat<unsigned char, unsigned short>,
arithm::subMat<unsigned char, short>,
arithm::subMat<unsigned char, int>,
arithm::subMat<unsigned char, float>,
arithm::subMat<unsigned char, double>
},
{
arithm::subMat<signed char, unsigned char>,
arithm::subMat<signed char, signed char>,
arithm::subMat<signed char, unsigned short>,
arithm::subMat<signed char, short>,
arithm::subMat<signed char, int>,
arithm::subMat<signed char, float>,
arithm::subMat<signed char, double>
},
{
0 /*arithm::subMat<unsigned short, unsigned char>*/,
0 /*arithm::subMat<unsigned short, signed char>*/,
arithm::subMat<unsigned short, unsigned short>,
arithm::subMat<unsigned short, short>,
arithm::subMat<unsigned short, int>,
arithm::subMat<unsigned short, float>,
arithm::subMat<unsigned short, double>
},
{
0 /*arithm::subMat<short, unsigned char>*/,
0 /*arithm::subMat<short, signed char>*/,
arithm::subMat<short, unsigned short>,
arithm::subMat<short, short>,
arithm::subMat<short, int>,
arithm::subMat<short, float>,
arithm::subMat<short, double>
},
{
0 /*arithm::subMat<int, unsigned char>*/,
0 /*arithm::subMat<int, signed char>*/,
0 /*arithm::subMat<int, unsigned short>*/,
0 /*arithm::subMat<int, short>*/,
arithm::subMat<int, int>,
arithm::subMat<int, float>,
arithm::subMat<int, double>
},
{
0 /*arithm::subMat<float, unsigned char>*/,
0 /*arithm::subMat<float, signed char>*/,
0 /*arithm::subMat<float, unsigned short>*/,
0 /*arithm::subMat<float, short>*/,
0 /*arithm::subMat<float, int>*/,
arithm::subMat<float, float>,
arithm::subMat<float, double>
},
{
0 /*arithm::subMat<double, unsigned char>*/,
0 /*arithm::subMat<double, signed char>*/,
0 /*arithm::subMat<double, unsigned short>*/,
0 /*arithm::subMat<double, short>*/,
0 /*arithm::subMat<double, int>*/,
0 /*arithm::subMat<double, float>*/,
arithm::subMat<double, double>
}
};
const int sdepth = src1.depth();
const int ddepth = dst.depth();
const int cn = src1.channels();
cudaStream_t stream = StreamAccessor::getStream(_stream);
PtrStepSzb src1_(src1.rows, src1.cols * cn, src1.data, src1.step);
PtrStepSzb src2_(src1.rows, src1.cols * cn, src2.data, src2.step);
PtrStepSzb dst_(src1.rows, src1.cols * cn, dst.data, dst.step);
if (mask.empty() && (sdepth == CV_8U || sdepth == CV_16U) && ddepth == sdepth)
{
const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
if (isAllAligned)
{
if (sdepth == CV_8U && (src1_.cols & 3) == 0)
{
const int vcols = src1_.cols >> 2;
arithm::subMat_v4(PtrStepSz<unsigned int>(src1_.rows, vcols, (unsigned int*) src1_.data, src1_.step),
PtrStepSz<unsigned int>(src1_.rows, vcols, (unsigned int*) src2_.data, src2_.step),
PtrStepSz<unsigned int>(src1_.rows, vcols, (unsigned int*) dst_.data, dst_.step),
stream);
return;
}
else if (sdepth == CV_16U && (src1_.cols & 1) == 0)
{
const int vcols = src1_.cols >> 1;
arithm::subMat_v2(PtrStepSz<unsigned int>(src1_.rows, vcols, (unsigned int*) src1_.data, src1_.step),
PtrStepSz<unsigned int>(src1_.rows, vcols, (unsigned int*) src2_.data, src2_.step),
PtrStepSz<unsigned int>(src1_.rows, vcols, (unsigned int*) dst_.data, dst_.step),
stream);
return;
}
}
}
const func_t func = funcs[sdepth][ddepth];
if (!func)
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
func(src1_, src2_, dst_, mask, stream);
}
namespace arithm
{
template <typename T, typename S, typename D>
void subScalar(PtrStepSzb src1, double val, bool inv, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
}
static void subScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& _stream, int)
{
typedef void (*func_t)(PtrStepSzb src1, double val, bool inv, PtrStepSzb dst, PtrStepb mask, cudaStream_t stream);
static const func_t funcs[7][7] =
{
{
arithm::subScalar<unsigned char, float, unsigned char>,
arithm::subScalar<unsigned char, float, signed char>,
arithm::subScalar<unsigned char, float, unsigned short>,
arithm::subScalar<unsigned char, float, short>,
arithm::subScalar<unsigned char, float, int>,
arithm::subScalar<unsigned char, float, float>,
arithm::subScalar<unsigned char, double, double>
},
{
arithm::subScalar<signed char, float, unsigned char>,
arithm::subScalar<signed char, float, signed char>,
arithm::subScalar<signed char, float, unsigned short>,
arithm::subScalar<signed char, float, short>,
arithm::subScalar<signed char, float, int>,
arithm::subScalar<signed char, float, float>,
arithm::subScalar<signed char, double, double>
},
{
0 /*arithm::subScalar<unsigned short, float, unsigned char>*/,
0 /*arithm::subScalar<unsigned short, float, signed char>*/,
arithm::subScalar<unsigned short, float, unsigned short>,
arithm::subScalar<unsigned short, float, short>,
arithm::subScalar<unsigned short, float, int>,
arithm::subScalar<unsigned short, float, float>,
arithm::subScalar<unsigned short, double, double>
},
{
0 /*arithm::subScalar<short, float, unsigned char>*/,
0 /*arithm::subScalar<short, float, signed char>*/,
arithm::subScalar<short, float, unsigned short>,
arithm::subScalar<short, float, short>,
arithm::subScalar<short, float, int>,
arithm::subScalar<short, float, float>,
arithm::subScalar<short, double, double>
},
{
0 /*arithm::subScalar<int, float, unsigned char>*/,
0 /*arithm::subScalar<int, float, signed char>*/,
0 /*arithm::subScalar<int, float, unsigned short>*/,
0 /*arithm::subScalar<int, float, short>*/,
arithm::subScalar<int, float, int>,
arithm::subScalar<int, float, float>,
arithm::subScalar<int, double, double>
},
{
0 /*arithm::subScalar<float, float, unsigned char>*/,
0 /*arithm::subScalar<float, float, signed char>*/,
0 /*arithm::subScalar<float, float, unsigned short>*/,
0 /*arithm::subScalar<float, float, short>*/,
0 /*arithm::subScalar<float, float, int>*/,
arithm::subScalar<float, float, float>,
arithm::subScalar<float, double, double>
},
{
0 /*arithm::subScalar<double, double, unsigned char>*/,
0 /*arithm::subScalar<double, double, signed char>*/,
0 /*arithm::subScalar<double, double, unsigned short>*/,
0 /*arithm::subScalar<double, double, short>*/,
0 /*arithm::subScalar<double, double, int>*/,
0 /*arithm::subScalar<double, double, float>*/,
arithm::subScalar<double, double, double>
}
};
typedef void (*npp_func_t)(const PtrStepSzb src, Scalar sc, PtrStepb dst, cudaStream_t stream);
static const npp_func_t npp_funcs[7][4] =
{
{NppArithmScalar<CV_8U , 1, nppiSubC_8u_C1RSfs >::call, 0 , NppArithmScalar<CV_8U , 3, nppiSubC_8u_C3RSfs >::call, NppArithmScalar<CV_8U , 4, nppiSubC_8u_C4RSfs >::call},
{0 , 0 , 0 , 0 },
{NppArithmScalar<CV_16U, 1, nppiSubC_16u_C1RSfs>::call, 0 , NppArithmScalar<CV_16U, 3, nppiSubC_16u_C3RSfs>::call, NppArithmScalar<CV_16U, 4, nppiSubC_16u_C4RSfs>::call},
{NppArithmScalar<CV_16S, 1, nppiSubC_16s_C1RSfs>::call, NppArithmScalar<CV_16S, 2, nppiSubC_16sc_C1RSfs>::call, NppArithmScalar<CV_16S, 3, nppiSubC_16s_C3RSfs>::call, NppArithmScalar<CV_16S, 4, nppiSubC_16s_C4RSfs>::call},
{NppArithmScalar<CV_32S, 1, nppiSubC_32s_C1RSfs>::call, NppArithmScalar<CV_32S, 2, nppiSubC_32sc_C1RSfs>::call, NppArithmScalar<CV_32S, 3, nppiSubC_32s_C3RSfs>::call, 0 },
{NppArithmScalar<CV_32F, 1, nppiSubC_32f_C1R >::call, NppArithmScalar<CV_32F, 2, nppiSubC_32fc_C1R >::call, NppArithmScalar<CV_32F, 3, nppiSubC_32f_C3R >::call, NppArithmScalar<CV_32F, 4, nppiSubC_32f_C4R >::call},
{0 , 0 , 0 , 0 }
};
const int sdepth = src.depth();
const int ddepth = dst.depth();
const int cn = src.channels();
cudaStream_t stream = StreamAccessor::getStream(_stream);
const npp_func_t npp_func = npp_funcs[sdepth][cn - 1];
if (ddepth == sdepth && cn > 1 && npp_func != 0 && !inv)
{
npp_func(src, val, dst, stream);
return;
}
CV_Assert( cn == 1 );
const func_t func = funcs[sdepth][ddepth];
if (!func)
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
func(src, val[0], inv, dst, mask, stream);
}
void subScalar(const GpuMat& src, Scalar val, bool inv, GpuMat& dst, const GpuMat& mask, double, Stream& stream, int);
void cv::cuda::subtract(InputArray src1, InputArray src2, OutputArray dst, InputArray mask, int dtype, Stream& stream)
{