[PP GAPI] - Generic precision conversion kernel; support for U8 (#2076)

- added U8 support
- tests are extended
This commit is contained in:
Anton Potapov 2020-09-09 15:30:08 +03:00 committed by GitHub
parent 8b87e1a477
commit f86d930e3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 10 deletions

View File

@ -2247,25 +2247,27 @@ GAPI_FLUID_KERNEL(FConvertDepth, ConvertDepth, false) {
static const int Window = 1; static const int Window = 1;
static void run(const cv::gapi::fluid::View& src, int depth, cv::gapi::fluid::Buffer& dst) { static void run(const cv::gapi::fluid::View& src, int depth, cv::gapi::fluid::Buffer& dst) {
GAPI_Assert(src.meta().depth == CV_16U || src.meta().depth == CV_32F); GAPI_Assert(src.meta().depth == CV_8U || src.meta().depth == CV_32F || src.meta().depth == CV_16U);
GAPI_Assert(dst.meta().depth == CV_32F || dst.meta().depth == CV_16U); GAPI_Assert(dst.meta().depth == CV_8U || dst.meta().depth == CV_32F || dst.meta().depth == CV_16U);
GAPI_Assert(src.meta().chan == 1); GAPI_Assert(src.meta().chan == 1);
GAPI_Assert(dst.meta().chan == 1); GAPI_Assert(dst.meta().chan == 1);
GAPI_Assert(src.length() == dst.length()); GAPI_Assert(src.length() == dst.length());
constexpr unsigned supported_types_n = 2; constexpr unsigned supported_types_n = 3;
using p_f = void (*)( const uint8_t* src, uint8_t* dst, const int width); using p_f = void (*)( const uint8_t* src, uint8_t* dst, const int width);
using table_string_t = std::array<p_f, supported_types_n>; using table_string_t = std::array<p_f, supported_types_n>;
constexpr std::array<table_string_t, supported_types_n> func_table = { constexpr std::array<table_string_t, supported_types_n> func_table = {
table_string_t{convert_precision<uint16_t, uint16_t>, convert_precision<uint16_t, float>}, table_string_t{convert_precision<uint16_t, uint16_t>, convert_precision<uint16_t, float>, convert_precision<uint16_t, uint8_t>},
table_string_t{convert_precision<float, uint16_t>, convert_precision<float, float>} table_string_t{convert_precision<float, uint16_t>, convert_precision<float, float>, convert_precision<float, uint8_t>},
table_string_t{convert_precision<uint8_t, uint16_t>, convert_precision<uint8_t, float>, convert_precision<uint8_t, uint8_t>}
}; };
auto depth_to_index = [](int depth){ auto depth_to_index = [](int depth){
switch (depth) { switch (depth) {
case CV_16U: return 0; case CV_16U: return 0;
case CV_32F: return 1; case CV_32F: return 1;
case CV_8U: return 2;
default: GAPI_Assert(!"not supported depth"); return -1; default: GAPI_Assert(!"not supported depth"); return -1;
} }
}; };

View File

@ -145,8 +145,8 @@ namespace gapi {
G_TYPED_KERNEL(ConvertDepth, <cv::GMat(cv::GMat, int depth)>, "com.intel.ie.ConvertDepth") { G_TYPED_KERNEL(ConvertDepth, <cv::GMat(cv::GMat, int depth)>, "com.intel.ie.ConvertDepth") {
static cv::GMatDesc outMeta(const cv::GMatDesc& in, int depth) { static cv::GMatDesc outMeta(const cv::GMatDesc& in, int depth) {
GAPI_Assert(in.depth == CV_16U || in.depth == CV_32F); GAPI_Assert(in.depth == CV_8U || in.depth == CV_16U || in.depth == CV_32F);
GAPI_Assert(depth == CV_32F || depth == CV_16U); GAPI_Assert(depth == CV_8U || depth == CV_32F || depth == CV_16U);
return in.withDepth(depth); return in.withDepth(depth);
} }

View File

@ -24,6 +24,7 @@
#include <climits> #include <climits>
#include <cstdint> #include <cstdint>
#include <limits>
#if defined(__GNUC__) && (__GNUC__ <= 5) #if defined(__GNUC__) && (__GNUC__ <= 5)
#include <cmath> #include <cmath>
@ -38,12 +39,20 @@ template<> inline short saturate_cast(int x) { return (std::min)(SHRT_MAX, (std:
template<> inline short saturate_cast(float x) { return saturate_cast<short>(static_cast<int>(std::rint(x))); } template<> inline short saturate_cast(float x) { return saturate_cast<short>(static_cast<int>(std::rint(x))); }
template<> inline float saturate_cast(float x) { return x; } template<> inline float saturate_cast(float x) { return x; }
template<> inline short saturate_cast(short x) { return x; } template<> inline short saturate_cast(short x) { return x; }
template<> inline uint16_t saturate_cast(uint16_t x) { return x; } template<> inline uint16_t saturate_cast(uint16_t x) { return x; }
template<> inline float saturate_cast(uint16_t x) { return x; } template<> inline float saturate_cast(uint16_t x) { return x; }
template<> inline uint16_t saturate_cast(int x) { return (std::min)(USHRT_MAX, (std::max)(0, x)); } template<> inline uint16_t saturate_cast(int x) { return (std::min)(USHRT_MAX, (std::max)(0, x)); }
template<> inline uint16_t saturate_cast(float x) { return saturate_cast<uint16_t>(static_cast<int>(std::rint(x))); } template<> inline uint16_t saturate_cast(float x) { return saturate_cast<uint16_t>(static_cast<int>(std::rint(x))); }
template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); } template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
template<> inline uint16_t saturate_cast(uint8_t x) { return x; }
template<> inline float saturate_cast(uint8_t x) { return x; }
template<> inline uint8_t saturate_cast(uint8_t x) { return x; }
template<> inline uint8_t saturate_cast(uint16_t x) { using lim = std::numeric_limits<uint8_t>; return std::min(static_cast<uint16_t>(lim::max()), std::max(static_cast<uint16_t>(lim::min()), x));}
template<> inline uint8_t saturate_cast(float x) { return saturate_cast<uint8_t>(static_cast<int>(std::rint(x))); }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
constexpr static const int ONE = 1 << 15; constexpr static const int ONE = 1 << 15;

View File

@ -171,8 +171,8 @@ INSTANTIATE_TEST_CASE_P(I420toRGBTestFluid, I420toRGBTestGAPI,
Values(0))); Values(0)));
INSTANTIATE_TEST_CASE_P(ConvertDepthFluid, ConvertDepthTestGAPI, INSTANTIATE_TEST_CASE_P(ConvertDepthFluid, ConvertDepthTestGAPI,
Combine(Values(CV_16U, CV_32F), Combine(Values(CV_16U, CV_32F, CV_8U),
Values(CV_32F, CV_16U), Values(CV_32F, CV_16U, CV_8U),
Values(cv::Size(3840, 2160), Values(cv::Size(3840, 2160),
cv::Size(1920, 1080), cv::Size(1920, 1080),
cv::Size(1280, 720), cv::Size(1280, 720),