Commit bc929a7d authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

added API for storing OpenCV data structures to text string and reading them back

parent 18a8721f
...@@ -156,9 +156,9 @@ The constructors. ...@@ -156,9 +156,9 @@ The constructors.
.. ocv:function:: FileStorage::FileStorage() .. ocv:function:: FileStorage::FileStorage()
.. ocv:function:: FileStorage::FileStorage(const string& filename, int flags, const string& encoding=string()) .. ocv:function:: FileStorage::FileStorage(const string& source, int flags, const string& encoding=string())
:param filename: Name of the file to open. Extension of the file (``.xml`` or ``.yml``/``.yaml``) determines its format (XML or YAML respectively). Also you can append ``.gz`` to work with compressed files, for example ``myHugeMatrix.xml.gz``. :param source: Name of the file to open or the text string to read the data from. Extension of the file (``.xml`` or ``.yml``/``.yaml``) determines its format (XML or YAML respectively). Also you can append ``.gz`` to work with compressed files, for example ``myHugeMatrix.xml.gz``. If both ``FileStorage::WRITE`` and ``FileStorage::MEMORY`` flags are specified, ``source`` is used just to specify the output file format (e.g. ``mydata.xml``, ``.yml`` etc.).
:param flags: Mode of operation. Possible values are: :param flags: Mode of operation. Possible values are:
...@@ -167,6 +167,8 @@ The constructors. ...@@ -167,6 +167,8 @@ The constructors.
* **FileStorage::WRITE** Open the file for writing. * **FileStorage::WRITE** Open the file for writing.
* **FileStorage::APPEND** Open the file for appending. * **FileStorage::APPEND** Open the file for appending.
* **FileStorage::MEMORY** Read data from ``source`` or write data to the internal buffer (which is returned by ``FileStorage::release``)
:param encoding: Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it. :param encoding: Encoding of the file. Note that UTF-16 XML encoding is not supported currently and you should use 8-bit encoding instead of it.
...@@ -197,9 +199,9 @@ FileStorage::release ...@@ -197,9 +199,9 @@ FileStorage::release
-------------------- --------------------
Closes the file and releases all the memory buffers. Closes the file and releases all the memory buffers.
.. ocv:function:: void FileStorage::release() .. ocv:function:: string FileStorage::release()
Call this method after all I/O operations with the storage are finished. Call this method after all I/O operations with the storage are finished. If the storage was opened for writing data and ``FileStorage::WRITE`` was specified
FileStorage::getFirstTopLevelNode FileStorage::getFirstTopLevelNode
......
...@@ -3941,7 +3941,12 @@ public: ...@@ -3941,7 +3941,12 @@ public:
{ {
READ=0, //! read mode READ=0, //! read mode
WRITE=1, //! write mode WRITE=1, //! write mode
APPEND=2 //! append mode APPEND=2, //! append mode
MEMORY=4,
FORMAT_MASK=(7<<3),
FORMAT_AUTO=0,
FORMAT_XML=(1<<3),
FORMAT_YAML=(2<<3)
}; };
enum enum
{ {
...@@ -3953,7 +3958,7 @@ public: ...@@ -3953,7 +3958,7 @@ public:
//! the default constructor //! the default constructor
CV_WRAP FileStorage(); CV_WRAP FileStorage();
//! the full constructor that opens file storage for reading or writing //! the full constructor that opens file storage for reading or writing
CV_WRAP FileStorage(const string& filename, int flags, const string& encoding=string()); CV_WRAP FileStorage(const string& source, int flags, const string& encoding=string());
//! the constructor that takes pointer to the C FileStorage structure //! the constructor that takes pointer to the C FileStorage structure
FileStorage(CvFileStorage* fs); FileStorage(CvFileStorage* fs);
//! the destructor. calls release() //! the destructor. calls release()
...@@ -3964,7 +3969,7 @@ public: ...@@ -3964,7 +3969,7 @@ public:
//! returns true if the object is associated with currently opened file. //! returns true if the object is associated with currently opened file.
CV_WRAP virtual bool isOpened() const; CV_WRAP virtual bool isOpened() const;
//! closes the file and releases all the memory buffers //! closes the file and releases all the memory buffers
CV_WRAP virtual void release(); CV_WRAP virtual string release();
//! returns the first element of the top-level mapping //! returns the first element of the top-level mapping
CV_WRAP FileNode getFirstTopLevelNode() const; CV_WRAP FileNode getFirstTopLevelNode() const;
......
...@@ -1740,6 +1740,11 @@ typedef struct CvFileStorage CvFileStorage; ...@@ -1740,6 +1740,11 @@ typedef struct CvFileStorage CvFileStorage;
#define CV_STORAGE_WRITE_TEXT CV_STORAGE_WRITE #define CV_STORAGE_WRITE_TEXT CV_STORAGE_WRITE
#define CV_STORAGE_WRITE_BINARY CV_STORAGE_WRITE #define CV_STORAGE_WRITE_BINARY CV_STORAGE_WRITE
#define CV_STORAGE_APPEND 2 #define CV_STORAGE_APPEND 2
#define CV_STORAGE_MEMORY 4
#define CV_STORAGE_FORMAT_MASK (7<<3)
#define CV_STORAGE_FORMAT_AUTO 0
#define CV_STORAGE_FORMAT_XML 8
#define CV_STORAGE_FORMAT_YAML 16
/* List of attributes: */ /* List of attributes: */
typedef struct CvAttrList typedef struct CvAttrList
......
This diff is collapsed.
...@@ -91,7 +91,7 @@ protected: ...@@ -91,7 +91,7 @@ protected:
{-1000000, 1000000}, {-10, 10}, {-10, 10}}; {-1000000, 1000000}, {-10, 10}, {-10, 10}};
RNG& rng = ts->get_rng(); RNG& rng = ts->get_rng();
RNG rng0; RNG rng0;
test_case_count = 2; test_case_count = 4;
int progress = 0; int progress = 0;
MemStorage storage(cvCreateMemStorage(0)); MemStorage storage(cvCreateMemStorage(0));
...@@ -102,9 +102,10 @@ protected: ...@@ -102,9 +102,10 @@ protected:
cvClearMemStorage(storage); cvClearMemStorage(storage);
bool mem = (idx % 4) >= 2;
string filename = tempfile(idx % 2 ? ".yml" : ".xml"); string filename = tempfile(idx % 2 ? ".yml" : ".xml");
FileStorage fs(filename.c_str(), FileStorage::WRITE); FileStorage fs(filename, FileStorage::WRITE + (mem ? FileStorage::MEMORY : 0));
int test_int = (int)cvtest::randInt(rng); int test_int = (int)cvtest::randInt(rng);
double test_real = (cvtest::randInt(rng)%2?1:-1)*exp(cvtest::randReal(rng)*18-9); double test_real = (cvtest::randInt(rng)%2?1:-1)*exp(cvtest::randReal(rng)*18-9);
...@@ -179,11 +180,11 @@ protected: ...@@ -179,11 +180,11 @@ protected:
fs.writeObj("test_graph",graph); fs.writeObj("test_graph",graph);
CvGraph* graph2 = (CvGraph*)cvClone(graph); CvGraph* graph2 = (CvGraph*)cvClone(graph);
fs.release(); string content = fs.release();
if(!fs.open(filename.c_str(), FileStorage::READ)) if(!fs.open(mem ? content : filename, FileStorage::READ + (mem ? FileStorage::MEMORY : 0)))
{ {
ts->printf( cvtest::TS::LOG, "filename %s can not be read\n", filename.c_str() ); ts->printf( cvtest::TS::LOG, "filename %s can not be read\n", !mem ? filename.c_str() : content.c_str());
ts->set_failed_test_info( cvtest::TS::FAIL_MISSING_TEST_DATA ); ts->set_failed_test_info( cvtest::TS::FAIL_MISSING_TEST_DATA );
return; return;
} }
...@@ -310,7 +311,6 @@ protected: ...@@ -310,7 +311,6 @@ protected:
int real_width = (int)tm["width"]; int real_width = (int)tm["width"];
int real_height = (int)tm["height"]; int real_height = (int)tm["height"];
int real_lbp_val = 0; int real_lbp_val = 0;
FileNodeIterator it; FileNodeIterator it;
it = tm_lbp.begin(); it = tm_lbp.begin();
...@@ -370,7 +370,8 @@ protected: ...@@ -370,7 +370,8 @@ protected:
} }
fs.release(); fs.release();
remove(filename.c_str()); if( !mem )
remove(filename.c_str());
} }
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment