mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53: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:
@@ -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"
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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++ = ';';
|
||||
|
||||
@@ -152,7 +152,7 @@ public:
|
||||
*data++ = 't';
|
||||
else
|
||||
{
|
||||
sprintf( data, "x%02x", c );
|
||||
snprintf( data, sizeof(buf) - (data - buf), "x%02x", c );
|
||||
data += 3;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -497,7 +497,7 @@ static string idx2string(const int* idx, int dims)
|
||||
char* ptr = buf;
|
||||
for( int k = 0; k < dims; k++ )
|
||||
{
|
||||
sprintf(ptr, "%4d ", idx[k]);
|
||||
snprintf(ptr, sizeof(buf) - (ptr - buf), "%4d ", idx[k]);
|
||||
ptr += strlen(ptr);
|
||||
}
|
||||
ptr[-1] = '\0';
|
||||
|
||||
Reference in New Issue
Block a user