From 5b833db558a670bd8b5d501f4070dfe1cacc63a9 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Fri, 23 Jun 2017 17:03:13 +0300 Subject: [PATCH 1/2] core: forbid conversion real->int in some cases in FileStorage --- modules/core/src/persistence.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index ca4615e7de..90ebb77865 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -7374,22 +7374,21 @@ size_t FileNode::size() const void read(const FileNode& node, int& value, int default_value) { value = !node.node ? default_value : - CV_NODE_IS_INT(node.node->tag) ? node.node->data.i : - CV_NODE_IS_REAL(node.node->tag) ? cvRound(node.node->data.f) : 0x7fffffff; + CV_NODE_IS_INT(node.node->tag) ? node.node->data.i : std::numeric_limits::max(); } void read(const FileNode& node, float& value, float default_value) { value = !node.node ? default_value : CV_NODE_IS_INT(node.node->tag) ? (float)node.node->data.i : - CV_NODE_IS_REAL(node.node->tag) ? (float)node.node->data.f : 1e30f; + CV_NODE_IS_REAL(node.node->tag) ? saturate_cast(node.node->data.f) : std::numeric_limits::max(); } void read(const FileNode& node, double& value, double default_value) { value = !node.node ? default_value : CV_NODE_IS_INT(node.node->tag) ? (double)node.node->data.i : - CV_NODE_IS_REAL(node.node->tag) ? node.node->data.f : 1e300; + CV_NODE_IS_REAL(node.node->tag) ? node.node->data.f : std::numeric_limits::max(); } void read(const FileNode& node, String& value, const String& default_value) From 6dd9e18b2e1cde257b89111ed8b3869f5edafaca Mon Sep 17 00:00:00 2001 From: "PkLab.net" Date: Fri, 26 May 2017 19:14:58 +0200 Subject: [PATCH 2/2] add std::string overload for cv::read() --- modules/core/include/opencv2/core/cvstd.inl.hpp | 4 +--- modules/core/include/opencv2/core/persistence.hpp | 1 + modules/core/src/persistence.cpp | 5 +++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/core/include/opencv2/core/cvstd.inl.hpp b/modules/core/include/opencv2/core/cvstd.inl.hpp index c8c7ba998e..c77f02a98b 100644 --- a/modules/core/include/opencv2/core/cvstd.inl.hpp +++ b/modules/core/include/opencv2/core/cvstd.inl.hpp @@ -156,9 +156,7 @@ FileNode::operator std::string() const template<> inline void operator >> (const FileNode& n, std::string& value) { - String val; - read(n, val, val); - value = val; + read(n, value, std::string()); } template<> inline diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 90da60d821..ac53ca3160 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -714,6 +714,7 @@ CV_EXPORTS void read(const FileNode& node, int& value, int default_value); CV_EXPORTS void read(const FileNode& node, float& value, float default_value); CV_EXPORTS void read(const FileNode& node, double& value, double default_value); CV_EXPORTS void read(const FileNode& node, String& value, const String& default_value); +CV_EXPORTS void read(const FileNode& node, std::string& value, const std::string& default_value); CV_EXPORTS void read(const FileNode& node, Mat& mat, const Mat& default_mat = Mat() ); CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat = SparseMat() ); CV_EXPORTS void read(const FileNode& node, std::vector& keypoints); diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 90ebb77865..bb4d953d4a 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -7396,6 +7396,11 @@ void read(const FileNode& node, String& value, const String& default_value) value = !node.node ? default_value : CV_NODE_IS_STRING(node.node->tag) ? String(node.node->data.str.ptr) : String(); } +void read(const FileNode& node, std::string& value, const std::string& default_value) +{ + value = !node.node ? default_value : CV_NODE_IS_STRING(node.node->tag) ? std::string(node.node->data.str.ptr) : default_value; +} + }