Skip to content

Commit 825b142

Browse files
committed
core: fix persistence with deprecated traits
1 parent b0bce60 commit 825b142

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

modules/core/include/opencv2/core/persistence.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,9 @@ namespace internal
851851
size_t remaining = it->remaining;
852852
size_t cn = DataType<_Tp>::channels;
853853
int _fmt = traits::SafeFmt<_Tp>::fmt;
854+
CV_Assert((_fmt >> 8) < 9);
854855
char fmt[] = { (char)((_fmt >> 8)+'1'), (char)_fmt, '\0' };
856+
CV_Assert((remaining % cn) == 0);
855857
size_t remaining1 = remaining / cn;
856858
count = count < remaining1 ? count : remaining1;
857859
vec.resize(count);

modules/core/src/persistence.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7367,8 +7367,18 @@ void read(const FileNode& node, std::vector<KeyPoint>& keypoints)
73677367
if (first_node.isSeq())
73687368
{
73697369
// modern scheme
7370+
#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
7371+
FileNodeIterator it = node.begin();
7372+
size_t total = (size_t)it.remaining;
7373+
keypoints.resize(total);
7374+
for (size_t i = 0; i < total; ++i, ++it)
7375+
{
7376+
(*it) >> keypoints[i];
7377+
}
7378+
#else
73707379
FileNodeIterator it = node.begin();
73717380
it >> keypoints;
7381+
#endif
73727382
return;
73737383
}
73747384
keypoints.clear();
@@ -7394,8 +7404,18 @@ void read(const FileNode& node, std::vector<DMatch>& matches)
73947404
if (first_node.isSeq())
73957405
{
73967406
// modern scheme
7407+
#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
7408+
FileNodeIterator it = node.begin();
7409+
size_t total = (size_t)it.remaining;
7410+
matches.resize(total);
7411+
for (size_t i = 0; i < total; ++i, ++it)
7412+
{
7413+
(*it) >> matches[i];
7414+
}
7415+
#else
73977416
FileNodeIterator it = node.begin();
73987417
it >> matches;
7418+
#endif
73997419
return;
74007420
}
74017421
matches.clear();

modules/core/test/test_io.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,7 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector)
11991199

12001200
EXPECT_NO_THROW(fs << "dvv" << dvv);
12011201
cv::String fs_result = fs.releaseAndGetString();
1202+
#ifndef OPENCV_TRAITS_ENABLE_DEPRECATED
12021203
#if defined _MSC_VER && _MSC_VER <= 1700 /* MSVC 2012 and older */
12031204
EXPECT_STREQ(fs_result.c_str(),
12041205
"%YAML:1.0\n"
@@ -1226,6 +1227,7 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector)
12261227
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
12271228
);
12281229
#endif
1230+
#endif // OPENCV_TRAITS_ENABLE_DEPRECATED
12291231

12301232
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
12311233

@@ -1344,6 +1346,7 @@ TEST(Core_InputOutput, FileStorage_KeyPoint_vector_vector)
13441346

13451347
EXPECT_NO_THROW(fs << "kvv" << kvv);
13461348
cv::String fs_result = fs.releaseAndGetString();
1349+
#ifndef OPENCV_TRAITS_ENABLE_DEPRECATED
13471350
EXPECT_STREQ(fs_result.c_str(),
13481351
"<?xml version=\"1.0\"?>\n"
13491352
"<opencv_storage>\n"
@@ -1362,6 +1365,7 @@ TEST(Core_InputOutput, FileStorage_KeyPoint_vector_vector)
13621365
" 1. 2. 16. 0. 100. 1 -1</_></_></kvv>\n"
13631366
"</opencv_storage>\n"
13641367
);
1368+
#endif //OPENCV_TRAITS_ENABLE_DEPRECATED
13651369

13661370
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
13671371

0 commit comments

Comments
 (0)