Commit 9ad470ba authored by Andrey Kamaev's avatar Andrey Kamaev

backported API for storing OpenCV data structures to text string and reading…

backported API for storing OpenCV data structures to text string and reading them back (r8481, r8516, r8518, r8522)
parent 4e4b96e3
...@@ -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.
...@@ -202,6 +204,15 @@ Closes the file and releases all the memory buffers. ...@@ -202,6 +204,15 @@ Closes the file and releases all the memory buffers.
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.
FileStorage::releaseAndGetString
--------------------------------
Closes the file and releases all the memory buffers.
.. ocv:function:: string FileStorage::releaseAndGetString()
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
--------------------------------- ---------------------------------
Returns the first element of the top-level mapping. Returns the first element of the top-level mapping.
......
...@@ -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()
...@@ -3965,6 +3970,8 @@ public: ...@@ -3965,6 +3970,8 @@ public:
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 void release();
//! closes the file, releases all the memory buffers and returns the text string
CV_WRAP string releaseAndGetString();
//! 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.releaseAndGetString();
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());
} }
} }
}; };
......
...@@ -22,4 +22,21 @@ TEST(Core_Drawing, _914) ...@@ -22,4 +22,21 @@ TEST(Core_Drawing, _914)
int pixelsDrawn = rows*cols - countNonZero(img); int pixelsDrawn = rows*cols - countNonZero(img);
ASSERT_EQ( (3*rows + cols)*3 - 3*9, pixelsDrawn); ASSERT_EQ( (3*rows + cols)*3 - 3*9, pixelsDrawn);
}
TEST(Core_OutputArraySreate, _1997)
{
struct local {
static void create(OutputArray arr, Size submatSize, int type)
{
int sizes[] = {submatSize.width, submatSize.height};
arr.create(sizeof(sizes)/sizeof(sizes[0]), sizes, type);
}
};
Mat mat(Size(512, 512), CV_8U);
Size submatSize = Size(256, 256);
ASSERT_NO_THROW(local::create( mat(Rect(Point(), submatSize)), submatSize, mat.type() ));
} }
\ No newline at end of file
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