From 5e155571553ea5e8212126069d32d17b62dec899 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Tue, 3 Apr 2012 12:23:46 +0000 Subject: [PATCH] fixed a few bugs in XML/YAML input/output. --- modules/core/include/opencv2/core/operations.hpp | 25 ++++++-------- modules/core/src/persistence.cpp | 32 +++-------------- modules/core/test/test_io.cpp | 44 ++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 42 deletions(-) diff --git a/modules/core/include/opencv2/core/operations.hpp b/modules/core/include/opencv2/core/operations.hpp index 6e6db9f..9337912 100644 --- a/modules/core/include/opencv2/core/operations.hpp +++ b/modules/core/include/opencv2/core/operations.hpp @@ -2834,29 +2834,26 @@ public: { int _fmt = DataType<_Tp>::fmt; char fmt[] = { (char)((_fmt>>8)+'1'), (char)_fmt, '\0' }; - fs->writeRaw( string(fmt), (uchar*)&vec[0], vec.size()*sizeof(_Tp) ); + fs->writeRaw( string(fmt), !vec.empty() ? (uchar*)&vec[0] : 0, vec.size()*sizeof(_Tp) ); } FileStorage* fs; }; - template static inline void write( FileStorage& fs, const vector<_Tp>& vec ) { VecWriterProxy<_Tp, DataType<_Tp>::fmt != 0> w(&fs); w(vec); } -template static inline FileStorage& -operator << ( FileStorage& fs, const vector<_Tp>& vec ) +template static inline void write( FileStorage& fs, const string& name, + const vector<_Tp>& vec ) { - VecWriterProxy<_Tp, DataType<_Tp>::generic_type == 0> w(&fs); - w(vec); - return fs; -} - + WriteStructContext ws(fs, name, CV_NODE_SEQ+(DataType<_Tp>::fmt != 0 ? CV_NODE_FLOW : 0)); + write(fs, vec); +} + CV_EXPORTS_W void write( FileStorage& fs, const string& name, const Mat& value ); CV_EXPORTS void write( FileStorage& fs, const string& name, const SparseMat& value ); -CV_EXPORTS void write( FileStorage& fs, const string& name, const vector& value ); template static inline FileStorage& operator << (FileStorage& fs, const _Tp& value) { @@ -2894,7 +2891,7 @@ inline size_t FileNode::size() const { int t = type(); return t == MAP ? ((CvSet*)node->data.map)->active_count : - t == SEQ ? node->data.seq->total : node != 0; + t == SEQ ? node->data.seq->total : (size_t)!isNone(); } inline CvFileNode* FileNode::operator *() { return (CvFileNode*)node; } @@ -2958,7 +2955,6 @@ static inline void read(const FileNode& node, string& value, const string& defau CV_EXPORTS_W 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, vector& mat, const vector& default_mat_vector=vector() ); inline FileNode::operator int() const { @@ -3029,9 +3025,10 @@ read( FileNodeIterator& it, vector<_Tp>& vec, size_t maxCount=(size_t)INT_MAX ) } template static inline void -read( FileNode& node, vector<_Tp>& vec, const vector<_Tp>& default_value=vector<_Tp>() ) +read( const FileNode& node, vector<_Tp>& vec, const vector<_Tp>& default_value=vector<_Tp>() ) { - read( node.begin(), vec ); + FileNodeIterator it = node.begin(); + read( it, vec ); } inline FileNodeIterator FileNode::begin() const diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 5e90a53..d73e911 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -2987,9 +2987,6 @@ cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt ) CV_CHECK_OUTPUT_FILE_STORAGE( fs ); - if( !data0 ) - CV_Error( CV_StsNullPtr, "Null data pointer" ); - if( len < 0 ) CV_Error( CV_StsOutOfRange, "Negative number of elements" ); @@ -2997,6 +2994,9 @@ cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt ) if( !len ) return; + + if( !data0 ) + CV_Error( CV_StsNullPtr, "Null data pointer" ); if( fmt_pair_count == 1 ) { @@ -5195,7 +5195,7 @@ FileNodeIterator::FileNodeIterator() FileNodeIterator::FileNodeIterator(const CvFileStorage* _fs, const CvFileNode* _node, size_t _ofs) { - if( _fs && _node ) + if( _fs && _node && CV_NODE_TYPE(_node->tag) != CV_NODE_NONE ) { int node_type = _node->tag & FileNode::TYPE_MASK; fs = _fs; @@ -5359,12 +5359,6 @@ void write( FileStorage& fs, const string& name, const SparseMat& value ) cvWrite( *fs, name.size() ? name.c_str() : 0, mat ); } -void write( FileStorage& fs, const string& name, const vector& value ) -{ - WriteStructContext ws(fs, name, CV_NODE_SEQ); - for( size_t i = 0; i < value.size(); i++ ) - write(fs, string(), value[i]); -} WriteStructContext::WriteStructContext(FileStorage& _fs, const string& name, int flags, const string& typeName) : fs(&_fs) @@ -5412,24 +5406,6 @@ void read( const FileNode& node, SparseMat& mat, const SparseMat& default_mat ) CV_Assert(CV_IS_SPARSE_MAT(m)); SparseMat(m).copyTo(mat); } - -void read( const FileNode& node, vector& mat_vector, const vector& default_mat_vector ) -{ - if( node.empty() ) - { - mat_vector = default_mat_vector; - return; - } - - FileNodeIterator it = node.begin(), it_end = node.end(); - mat_vector.clear(); - for( ; it != it_end; ++it ) - { - Mat m; - *it >> m; - mat_vector.push_back(m); - } -} } diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 40aab53..a380a60 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -377,6 +377,50 @@ protected: TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run(); } + +class CV_MiscIOTest : public cvtest::BaseTest +{ +public: + CV_MiscIOTest() {} + ~CV_MiscIOTest() {} +protected: + void run(int) + { + //try + { + FileStorage fs("test.xml", FileStorage::WRITE); + vector mi, mi2, mi3, mi4; + vector mv, mv2, mv3, mv4; + Mat m(10, 9, CV_32F); + randu(m, 0, 1); + mi3.push_back(5); + mv3.push_back(m); + fs << "mi" << mi; + fs << "mv" << mv; + fs << "mi3" << mi3; + fs << "mv3" << mv3; + fs.release(); + fs.open("test.xml", FileStorage::READ); + fs["mi"] >> mi2; + fs["mv"] >> mv2; + fs["mi3"] >> mi4; + fs["mv3"] >> mv4; + CV_Assert( mi2.empty() ); + CV_Assert( mv2.empty() ); + CV_Assert( norm(mi3, mi4, CV_C) == 0 ); + CV_Assert( mv4.size() == 1 ); + double n = norm(mv3[0], mv4[0], CV_C); + CV_Assert( n == 0 ); + } + /*catch(...) + { + ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH); + }*/ + } +}; + +TEST(Core_InputOutput, misc) { CV_MiscIOTest test; test.safe_run(); } + /*class CV_BigMatrixIOTest : public cvtest::BaseTest { public: -- 2.7.4