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

Merge pull request #23055 from seanm:sprintf2

* Replaced most remaining sprintf with snprintf
* Deprecated encodeFormat and introduced new method that takes the buffer length
* Also increased buffer size at call sites to be a little bigger, in case int is 64 bit
This commit is contained in:
Sean McBride
2023-04-18 02:22:59 -04:00
committed by GitHub
parent 8512deb3cc
commit 47bea69322
32 changed files with 82 additions and 71 deletions
+2 -2
View File
@@ -489,7 +489,7 @@ static bool ocl_arithm_op(InputArray _src1, InputArray _src2, OutputArray _dst,
int kercn = haveMask || haveScalar ? cn : ocl::predictOptimalVectorWidth(_src1, _src2, _dst);
int scalarcn = kercn == 3 ? 4 : kercn, rowsPerWI = d.isIntel() ? 4 : 1;
char cvtstr[4][32], opts[1024];
char cvtstr[4][50], opts[1024];
snprintf(opts, sizeof(opts), "-D %s%s -D %s -D srcT1=%s -D srcT1_C1=%s -D srcT2=%s -D srcT2_C1=%s "
"-D dstT=%s -D DEPTH_dst=%d -D dstT_C1=%s -D workT=%s -D workST=%s -D scaleT=%s -D wdepth=%d -D convertToWT1=%s "
"-D convertToWT2=%s -D convertToDT=%s%s -D cn=%d -D rowsPerWI=%d -D convertFromU=%s",
@@ -1098,7 +1098,7 @@ static bool ocl_compare(InputArray _src1, InputArray _src2, OutputArray _dst, in
int scalarcn = kercn == 3 ? 4 : kercn;
const char * const operationMap[] = { "==", ">", ">=", "<", "<=", "!=" };
char cvt[40];
char cvt[50];
String opts = format("-D %s -D srcT1=%s -D dstT=%s -D DEPTH_dst=%d -D workT=srcT1 -D cn=%d"
" -D convertToDT=%s -D OP_CMP -D CMP_OPERATOR=%s -D srcT1_C1=%s"
+1 -1
View File
@@ -1041,7 +1041,7 @@ static bool ocl_dot( InputArray _src1, InputArray _src2, double & res )
wgs2_aligned <<= 1;
wgs2_aligned >>= 1;
char cvt[40];
char cvt[50];
ocl::Kernel k("reduce", ocl::core::reduce_oclsrc,
format("-D srcT=%s -D srcT1=%s -D dstT=%s -D dstTK=%s -D ddepth=%d -D convertToDT=%s -D OP_DOT "
"-D WGS=%d -D WGS2_ALIGNED=%d%s%s%s -D kercn=%d",
+2 -2
View File
@@ -633,7 +633,7 @@ static bool ocl_reduce(InputArray _src, OutputArray _dst,
static const size_t maxItemInGroupCount = 16;
tileHeight = min(tileHeight, defDev.localMemSize() / buf_cols / CV_ELEM_SIZE(CV_MAKETYPE(wdepth, cn)) / maxItemInGroupCount);
}
char cvt[3][40];
char cvt[3][50];
cv::String build_opt = format("-D OP_REDUCE_PRE -D BUF_COLS=%d -D TILE_HEIGHT=%zu -D %s -D dim=1"
" -D cn=%d -D ddepth=%d"
" -D srcT=%s -D bufT=%s -D dstT=%s"
@@ -667,7 +667,7 @@ static bool ocl_reduce(InputArray _src, OutputArray _dst,
}
else
{
char cvt[2][40];
char cvt[2][50];
cv::String build_opt = format("-D %s -D dim=%d -D cn=%d -D ddepth=%d"
" -D srcT=%s -D dstT=%s -D dstT0=%s -D convertToWT=%s"
" -D convertToDT=%s -D convertToDT0=%s%s",
+1 -1
View File
@@ -227,7 +227,7 @@ static bool ocl_meanStdDev( InputArray _src, OutputArray _mean, OutputArray _sdv
if ( (!doubleSupport && depth == CV_64F) )
return false;
char cvt[2][40];
char cvt[2][50];
String opts = format("-D srcT=%s -D srcT1=%s -D dstT=%s -D dstT1=%s -D sqddepth=%d"
" -D sqdstT=%s -D sqdstT1=%s -D convertToSDT=%s -D cn=%d%s%s"
" -D convertToDT=%s -D WGS=%d -D WGS2_ALIGNED=%d%s%s",
+1 -1
View File
@@ -1029,7 +1029,7 @@ bool ocl_minMaxIdx( InputArray _src, double* minVal, double* maxVal, int* minLoc
needMaxLoc = true;
}
char cvt[2][40];
char cvt[2][50];
String opts = format("-D DEPTH_%d -D srcT1=%s%s -D WGS=%d -D srcT=%s"
" -D WGS2_ALIGNED=%d%s%s%s -D kercn=%d%s%s%s%s"
" -D dstT1=%s -D dstT=%s -D convertToDT=%s%s%s%s%s -D wdepth=%d -D convertFromU=%s"
+1 -1
View File
@@ -1319,7 +1319,7 @@ static bool ocl_normalize( InputArray _src, InputOutputArray _dst, InputArray _m
if ((sdepth == CV_64F || ddepth == CV_64F) && !doubleSupport)
return false;
char cvt[2][40];
char cvt[2][50];
String opts = format("-D srcT=%s -D dstT=%s -D convertToWT=%s -D cn=%d -D rowsPerWI=%d"
" -D convertToDT=%s -D workT=%s%s%s%s -D srcT1=%s -D dstT1=%s",
ocl::typeToStr(stype), ocl::typeToStr(dtype),
+10 -7
View File
@@ -6983,7 +6983,7 @@ const char* typeToStr(int type)
{
"uchar", "uchar2", "uchar3", "uchar4", 0, 0, 0, "uchar8", 0, 0, 0, 0, 0, 0, 0, "uchar16",
"char", "char2", "char3", "char4", 0, 0, 0, "char8", 0, 0, 0, 0, 0, 0, 0, "char16",
"ushort", "ushort2", "ushort3", "ushort4",0, 0, 0, "ushort8", 0, 0, 0, 0, 0, 0, 0, "ushort16",
"ushort", "ushort2", "ushort3", "ushort4", 0, 0, 0, "ushort8", 0, 0, 0, 0, 0, 0, 0, "ushort16",
"short", "short2", "short3", "short4", 0, 0, 0, "short8", 0, 0, 0, 0, 0, 0, 0, "short16",
"int", "int2", "int3", "int4", 0, 0, 0, "int8", 0, 0, 0, 0, 0, 0, 0, "int16",
"float", "float2", "float3", "float4", 0, 0, 0, "float8", 0, 0, 0, 0, 0, 0, 0, "float16",
@@ -6992,7 +6992,7 @@ const char* typeToStr(int type)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int cn = CV_MAT_CN(type), depth = CV_MAT_DEPTH(type);
const char* result = cn > 16 ? 0 : tab[depth*16 + cn-1];
const char* result = cn > 16 ? nullptr : tab[depth*16 + cn-1];
CV_Assert(result);
return result;
}
@@ -7003,7 +7003,7 @@ const char* memopTypeToStr(int type)
{
"uchar", "uchar2", "uchar3", "uchar4", 0, 0, 0, "uchar8", 0, 0, 0, 0, 0, 0, 0, "uchar16",
"char", "char2", "char3", "char4", 0, 0, 0, "char8", 0, 0, 0, 0, 0, 0, 0, "char16",
"ushort", "ushort2", "ushort3", "ushort4",0, 0, 0, "ushort8", 0, 0, 0, 0, 0, 0, 0, "ushort16",
"ushort", "ushort2", "ushort3", "ushort4", 0, 0, 0, "ushort8", 0, 0, 0, 0, 0, 0, 0, "ushort16",
"short", "short2", "short3", "short4", 0, 0, 0, "short8", 0, 0, 0, 0, 0, 0, 0, "short16",
"int", "int2", "int3", "int4", 0, 0, 0, "int8", 0, 0, 0, 0, 0, 0, 0, "int16",
"int", "int2", "int3", "int4", 0, 0, 0, "int8", 0, 0, 0, 0, 0, 0, 0, "int16",
@@ -7012,7 +7012,7 @@ const char* memopTypeToStr(int type)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
int cn = CV_MAT_CN(type), depth = CV_MAT_DEPTH(type);
const char* result = cn > 16 ? 0 : tab[depth*16 + cn-1];
const char* result = cn > 16 ? nullptr : tab[depth*16 + cn-1];
CV_Assert(result);
return result;
}
@@ -7039,6 +7039,9 @@ const char* vecopTypeToStr(int type)
const char* convertTypeStr(int sdepth, int ddepth, int cn, char* buf)
{
// Since the size of buf is not given, we assume 50 because that's what all callers use.
constexpr size_t buf_max = 50;
if( sdepth == ddepth )
return "noconvert";
const char *typestr = typeToStr(CV_MAKETYPE(ddepth, cn));
@@ -7047,12 +7050,12 @@ const char* convertTypeStr(int sdepth, int ddepth, int cn, char* buf)
(ddepth == CV_16S && sdepth <= CV_8S) ||
(ddepth == CV_16U && sdepth == CV_8U))
{
sprintf(buf, "convert_%s", typestr);
snprintf(buf, buf_max, "convert_%s", typestr);
}
else if( sdepth >= CV_32F )
sprintf(buf, "convert_%s%s_rte", typestr, (ddepth < CV_32S ? "_sat" : ""));
snprintf(buf, buf_max, "convert_%s%s_rte", typestr, (ddepth < CV_32S ? "_sat" : ""));
else
sprintf(buf, "convert_%s_sat", typestr);
snprintf(buf, buf_max, "convert_%s_sat", typestr);
return buf;
}
+9 -2
View File
@@ -161,14 +161,21 @@ static int symbolToType(char c)
return static_cast<int>(pos - symbols);
}
char* encodeFormat(int elem_type, char* dt)
char* encodeFormat(int elem_type, char* dt, size_t dt_len)
{
int cn = (elem_type == CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/) ? 1 : CV_MAT_CN(elem_type);
char symbol = (elem_type == CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/) ? 'r' : typeSymbol(CV_MAT_DEPTH(elem_type));
sprintf(dt, "%d%c", cn, symbol);
snprintf(dt, dt_len, "%d%c", cn, symbol);
return dt + (cn == 1 ? 1 : 0);
}
// Deprecated due to size of dt buffer being unknowable.
char* encodeFormat(int elem_type, char* dt)
{
constexpr size_t max = 20+1+1; // UINT64_MAX + one char + nul termination.
return encodeFormat(elem_type, dt, max);
}
int decodeFormat( const char* dt, int* fmt_pairs, int max_len )
{
int fmt_pair_count = 0;
+2 -1
View File
@@ -91,7 +91,8 @@ char* doubleToString( char* buf, size_t bufSize, double value, bool explicitZero
int calcStructSize( const char* dt, int initial_size );
int calcElemSize( const char* dt, int initial_size );
char* encodeFormat( int elem_type, char* dt );
CV_DEPRECATED char* encodeFormat( int elem_type, char* dt );
char* encodeFormat( int elem_type, char* dt, size_t dt_len );
int decodeFormat( const char* dt, int* fmt_pairs, int max_len );
int decodeSimpleFormat( const char* dt );
}
+5 -5
View File
@@ -10,14 +10,14 @@ namespace cv
void write( FileStorage& fs, const String& name, const Mat& m )
{
char dt[16];
char dt[22];
if( m.dims <= 2 )
{
fs.startWriteStruct(name, FileNode::MAP, String("opencv-matrix"));
fs << "rows" << m.rows;
fs << "cols" << m.cols;
fs << "dt" << fs::encodeFormat( m.type(), dt );
fs << "dt" << fs::encodeFormat( m.type(), dt, sizeof(dt) );
fs << "data" << "[:";
for( int i = 0; i < m.rows; i++ )
fs.writeRaw(dt, m.ptr(i), m.cols*m.elemSize());
@@ -30,7 +30,7 @@ void write( FileStorage& fs, const String& name, const Mat& m )
fs << "sizes" << "[:";
fs.writeRaw( "i", m.size.p, m.dims*sizeof(int) );
fs << "]";
fs << "dt" << fs::encodeFormat( m.type(), dt );
fs << "dt" << fs::encodeFormat( m.type(), dt, sizeof(dt) );
fs << "data" << "[:";
const Mat* arrays[] = {&m, 0};
uchar* ptrs[1] = {};
@@ -63,7 +63,7 @@ struct SparseNodeCmp
void write( FileStorage& fs, const String& name, const SparseMat& m )
{
char dt[16];
char dt[22];
fs.startWriteStruct(name, FileNode::MAP, String("opencv-sparse-matrix"));
fs << "sizes" << "[:";
@@ -71,7 +71,7 @@ void write( FileStorage& fs, const String& name, const SparseMat& m )
if( dims > 0 )
fs.writeRaw("i", m.hdr->size, dims*sizeof(int) );
fs << "]";
fs << "dt" << fs::encodeFormat( m.type(), dt );
fs << "dt" << fs::encodeFormat( m.type(), dt, sizeof(dt) );
fs << "data" << "[:";
size_t i = 0, n = m.nzcount();
+1 -1
View File
@@ -208,7 +208,7 @@ public:
}
else
{
sprintf( data, "#x%02x", (uchar)c );
snprintf( data, sizeof(buf) - (data - buf), "#x%02x", (uchar)c );
data += 4;
}
*data++ = ';';
+1 -1
View File
@@ -152,7 +152,7 @@ public:
*data++ = 't';
else
{
sprintf( data, "x%02x", c );
snprintf( data, sizeof(buf) - (data - buf), "x%02x", c );
data += 3;
}
}
+1 -1
View File
@@ -62,7 +62,7 @@ bool ocl_sum( InputArray _src, Scalar & res, int sum_op, InputArray _mask,
wgs2_aligned >>= 1;
static const char * const opMap[3] = { "OP_SUM", "OP_SUM_ABS", "OP_SUM_SQR" };
char cvt[2][40];
char cvt[2][50];
String opts = format("-D srcT=%s -D srcT1=%s -D dstT=%s -D dstTK=%s -D dstT1=%s -D ddepth=%d -D cn=%d"
" -D convertToDT=%s -D %s -D WGS=%d -D WGS2_ALIGNED=%d%s%s%s%s -D kercn=%d%s%s%s -D convertFromU=%s",
ocl::typeToStr(CV_MAKE_TYPE(depth, mcn)), ocl::typeToStr(depth),
+1 -1
View File
@@ -1259,7 +1259,7 @@ void UMat::convertTo(OutputArray _dst, int _type, double alpha, double beta) con
{
int wdepth = std::max(CV_32F, sdepth), rowsPerWI = 4;
char cvt[2][40];
char cvt[2][50];
ocl::Kernel k("convertTo", ocl::core::convert_oclsrc,
format("-D srcT=%s -D WT=%s -D dstT=%s -D convertToWT=%s -D convertToDT=%s%s%s",
ocl::typeToStr(sdepth), ocl::typeToStr(wdepth), ocl::typeToStr(ddepth),