From 825b14278e832083b51319e0f9ff9610f997a6c3 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 12 Dec 2017 17:07:03 +0300 Subject: [PATCH] core: fix persistence with deprecated traits --- .../core/include/opencv2/core/persistence.hpp | 2 ++ modules/core/src/persistence.cpp | 20 +++++++++++++++++++ modules/core/test/test_io.cpp | 4 ++++ 3 files changed, 26 insertions(+) diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 8665e59cdf..6d8ad20c14 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -851,7 +851,9 @@ namespace internal size_t remaining = it->remaining; size_t cn = DataType<_Tp>::channels; int _fmt = traits::SafeFmt<_Tp>::fmt; + CV_Assert((_fmt >> 8) < 9); char fmt[] = { (char)((_fmt >> 8)+'1'), (char)_fmt, '\0' }; + CV_Assert((remaining % cn) == 0); size_t remaining1 = remaining / cn; count = count < remaining1 ? count : remaining1; vec.resize(count); diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index fc11d8da28..450297e963 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -7367,8 +7367,18 @@ void read(const FileNode& node, std::vector& keypoints) if (first_node.isSeq()) { // modern scheme +#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED + FileNodeIterator it = node.begin(); + size_t total = (size_t)it.remaining; + keypoints.resize(total); + for (size_t i = 0; i < total; ++i, ++it) + { + (*it) >> keypoints[i]; + } +#else FileNodeIterator it = node.begin(); it >> keypoints; +#endif return; } keypoints.clear(); @@ -7394,8 +7404,18 @@ void read(const FileNode& node, std::vector& matches) if (first_node.isSeq()) { // modern scheme +#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED + FileNodeIterator it = node.begin(); + size_t total = (size_t)it.remaining; + matches.resize(total); + for (size_t i = 0; i < total; ++i, ++it) + { + (*it) >> matches[i]; + } +#else FileNodeIterator it = node.begin(); it >> matches; +#endif return; } matches.clear(); diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 8d1bb324b8..046b85d818 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -1199,6 +1199,7 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector) EXPECT_NO_THROW(fs << "dvv" << dvv); cv::String fs_result = fs.releaseAndGetString(); +#ifndef OPENCV_TRAITS_ENABLE_DEPRECATED #if defined _MSC_VER && _MSC_VER <= 1700 /* MSVC 2012 and older */ EXPECT_STREQ(fs_result.c_str(), "%YAML:1.0\n" @@ -1226,6 +1227,7 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector) " - [ 1, 2, 3, -1.5000000000000000e+00 ]\n" ); #endif +#endif // OPENCV_TRAITS_ENABLE_DEPRECATED cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY); @@ -1344,6 +1346,7 @@ TEST(Core_InputOutput, FileStorage_KeyPoint_vector_vector) EXPECT_NO_THROW(fs << "kvv" << kvv); cv::String fs_result = fs.releaseAndGetString(); +#ifndef OPENCV_TRAITS_ENABLE_DEPRECATED EXPECT_STREQ(fs_result.c_str(), "\n" "\n" @@ -1362,6 +1365,7 @@ TEST(Core_InputOutput, FileStorage_KeyPoint_vector_vector) " 1. 2. 16. 0. 100. 1 -1\n" "\n" ); +#endif //OPENCV_TRAITS_ENABLE_DEPRECATED cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);