1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Added new data types to cv::Mat & UMat (#23865)

* started working on adding 32u, 64u, 64s, bool and 16bf types to OpenCV

* core & imgproc tests seem to pass

* fixed a few compile errors and test failures on macOS x86

* hopefully fixed some compile problems and test failures

* fixed some more warnings and test failures

* trying to fix small deviations in perf_core & perf_imgproc by revering randf_64f to exact version used before

* trying to fix behavior of the new OpenCV with old plugins; there is (quite strong) assumption that video capture would give us frames with depth == CV_8U (0) or CV_16U (2). If depth is > 7 then it means that the plugin is built with the old OpenCV. It needs to be recompiled, of course and then this hack can be removed.

* try to repair the case when target arch does not have FP64 SIMD

* 1. fixed bug in itoa() found by alalek
2. restored ==, !=, > and < univ. intrinsics on ARM32/ARM64.
This commit is contained in:
Vadim Pisarevsky
2023-08-04 10:50:03 +03:00
committed by GitHub
parent fa91c1445e
commit 518486ed3d
52 changed files with 2363 additions and 859 deletions
+87 -4
View File
@@ -56,6 +56,28 @@ char* itoa( int _val, char* buffer, int /*radix*/ )
return ptr;
}
char* itoa( int64_t _val, char* buffer, int /*radix*/, bool _signed)
{
const int radix = 10;
char* ptr=buffer + 23 /* enough even for 64-bit integers */;
int sign = _signed && _val < 0 ? -1 : 1;
uint64_t val = !_signed ? (uint64_t)_val : abs(_val);
*ptr = '\0';
do
{
uint64_t r = val / radix;
*--ptr = (char)(val - (r*radix) + '0');
val = r;
}
while( val != 0 );
if( sign < 0 )
*--ptr = '-';
return ptr;
}
char* doubleToString( char* buf, size_t bufSize, double value, bool explicitZero )
{
Cv64suf val;
@@ -142,12 +164,12 @@ char* floatToString( char* buf, size_t bufSize, float value, bool halfprecision,
return buf;
}
static const char symbols[9] = "ucwsifdh";
static const char symbols[] = "ucwsifdhHbLUn";
static char typeSymbol(int depth)
{
CV_StaticAssert(CV_64F == 6, "");
CV_CheckDepth(depth, depth >=0 && depth <= CV_16F, "");
CV_CheckDepth(depth, depth >= 0 && depth <= CV_32U, "");
return symbols[depth];
}
@@ -264,13 +286,18 @@ int calcStructSize( const char* dt, int initial_size )
switch (v)
{
case 'u': { elem_max_size = std::max( elem_max_size, sizeof(uchar ) ); break; }
case 'b': { elem_max_size = std::max( elem_max_size, sizeof(bool ) ); break; }
case 'c': { elem_max_size = std::max( elem_max_size, sizeof(schar ) ); break; }
case 'w': { elem_max_size = std::max( elem_max_size, sizeof(ushort) ); break; }
case 's': { elem_max_size = std::max( elem_max_size, sizeof(short ) ); break; }
case 'i': { elem_max_size = std::max( elem_max_size, sizeof(int ) ); break; }
case 'n': { elem_max_size = std::max( elem_max_size, sizeof(unsigned) ); break; }
case 'f': { elem_max_size = std::max( elem_max_size, sizeof(float ) ); break; }
case 'd': { elem_max_size = std::max( elem_max_size, sizeof(double) ); break; }
case 'h': { elem_max_size = std::max(elem_max_size, sizeof(float16_t)); break; }
case 'h': { elem_max_size = std::max( elem_max_size, sizeof(float16_t)); break; }
case 'H': { elem_max_size = std::max( elem_max_size, sizeof(bfloat16_t)); break; }
case 'I': { elem_max_size = std::max( elem_max_size, sizeof(int64_t)); break; }
case 'U': { elem_max_size = std::max( elem_max_size, sizeof(uint64_t)); break; }
default:
CV_Error_(Error::StsNotImplemented, ("Unknown type identifier: '%c' in '%s'", (char)(*type), dt));
}
@@ -1097,6 +1124,10 @@ void FileStorage::Impl::writeRawData(const std::string &dt, const void *_data, s
ptr = fs::itoa(*(uchar *) data, buf, 10);
data++;
break;
case CV_Bool:
ptr = fs::itoa(*(uchar *) data != 0, buf, 10);
data++;
break;
case CV_8S:
ptr = fs::itoa(*(char *) data, buf, 10);
data++;
@@ -1109,10 +1140,22 @@ void FileStorage::Impl::writeRawData(const std::string &dt, const void *_data, s
ptr = fs::itoa(*(short *) data, buf, 10);
data += sizeof(short);
break;
case CV_32U:
ptr = fs::itoa((int64_t)*(unsigned*) data, buf, 10, false);
data += sizeof(unsigned);
break;
case CV_32S:
ptr = fs::itoa(*(int *) data, buf, 10);
data += sizeof(int);
break;
case CV_64U:
ptr = fs::itoa(*(uint64_t*) data, buf, 10, false);
data += sizeof(uint64_t);
break;
case CV_64S:
ptr = fs::itoa(*(int64_t*) data, buf, 10, true);
data += sizeof(int64_t);
break;
case CV_32F:
ptr = fs::floatToString(buf, sizeof(buf), *(float *) data, false, explicitZero);
data += sizeof(float);
@@ -1121,10 +1164,14 @@ void FileStorage::Impl::writeRawData(const std::string &dt, const void *_data, s
ptr = fs::doubleToString(buf, sizeof(buf), *(double *) data, explicitZero);
data += sizeof(double);
break;
case CV_16F: /* reference */
case CV_16F:
ptr = fs::floatToString(buf, sizeof(buf), (float) *(float16_t *) data, true, explicitZero);
data += sizeof(float16_t);
break;
case CV_16BF:
ptr = fs::floatToString(buf, sizeof(buf), (float) *(bfloat16_t *) data, true, explicitZero);
data += sizeof(bfloat16_t);
break;
default:
CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported type");
return;
@@ -2572,6 +2619,10 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(char*)data = saturate_cast<schar>(ival);
data++;
break;
case CV_Bool:
*(bool*)data = ival != 0;
data++;
break;
case CV_16U:
*(ushort*)data = saturate_cast<ushort>(ival);
data += sizeof(ushort);
@@ -2580,6 +2631,10 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(short*)data = saturate_cast<short>(ival);
data += sizeof(short);
break;
case CV_32U:
*(unsigned*)data = (unsigned)std::max(ival, 0);
data += sizeof(unsigned);
break;
case CV_32S:
*(int*)data = ival;
data += sizeof(int);
@@ -2588,6 +2643,14 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(float*)data = (float)ival;
data += sizeof(float);
break;
case CV_64U:
*(uint64_t*)data = (uint64_t)ival;
data += sizeof(uint64_t);
break;
case CV_64S:
*(int64_t*)data = (int64_t)ival;
data += sizeof(int64_t);
break;
case CV_64F:
*(double*)data = (double)ival;
data += sizeof(double);
@@ -2596,6 +2659,10 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(float16_t*)data = float16_t((float)ival);
data += sizeof(float16_t);
break;
case CV_16BF:
*(bfloat16_t*)data = bfloat16_t((float)ival);
data += sizeof(bfloat16_t);
break;
default:
CV_Error( Error::StsUnsupportedFormat, "Unsupported type" );
}
@@ -2622,6 +2689,10 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(short*)data = saturate_cast<short>(fval);
data += sizeof(short);
break;
case CV_32U:
*(int*)data = saturate_cast<unsigned>(fval);
data += sizeof(int);
break;
case CV_32S:
*(int*)data = saturate_cast<int>(fval);
data += sizeof(int);
@@ -2630,6 +2701,14 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(float*)data = (float)fval;
data += sizeof(float);
break;
case CV_64U:
*(uint64_t*)data = (uint64_t)round(std::max(fval, 0.));
data += sizeof(uint64_t);
break;
case CV_64S:
*(int64_t*)data = (int64_t)round(std::max(fval, 0.));
data += sizeof(int64_t);
break;
case CV_64F:
*(double*)data = fval;
data += sizeof(double);
@@ -2638,6 +2717,10 @@ FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, si
*(float16_t*)data = float16_t((float)fval);
data += sizeof(float16_t);
break;
case CV_16BF:
*(bfloat16_t*)data = bfloat16_t((float)fval);
data += sizeof(bfloat16_t);
break;
default:
CV_Error( Error::StsUnsupportedFormat, "Unsupported type" );
}