From 0f622206e48f6050536f00d47abf47c4a2d1824a Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Fri, 2 Nov 2018 00:27:06 +0300 Subject: [PATCH] completely new C++ persistence implementation (#13011) * integrated the new C++ persistence; removed old persistence; most of OpenCV compiles fine! the tests have not been run yet * fixed multiple bugs in the new C++ persistence * fixed raw size of the parsed empty sequences * [temporarily] excluded obsolete applications traincascade and createsamples from build * fixed several compiler warnings and multiple test failures * undo changes in cocoa window rendering (that was fixed in another PR) * fixed more compile warnings and the remaining test failures (hopefully) * trying to fix the last little warning --- apps/CMakeLists.txt | 4 +- apps/traincascade/old_ml.hpp | 22 +- modules/calib3d/include/opencv2/calib3d.hpp | 6 + modules/calib3d/src/calibinit.cpp | 2 +- modules/calib3d/src/checkchessboard.cpp | 9 +- modules/calib3d/src/precomp.hpp | 1 - modules/calib3d/test/test_cameracalibration.cpp | 4 +- modules/calib3d/test/test_chesscorners_timing.cpp | 95 +- modules/calib3d/test/test_fundam.cpp | 20 +- modules/calib3d/test/test_undistort.cpp | 4 +- modules/core/include/opencv2/core/core_c.h | 64 +- modules/core/include/opencv2/core/cvstd.inl.hpp | 20 - modules/core/include/opencv2/core/persistence.hpp | 357 +-- modules/core/include/opencv2/core/types_c.h | 25 +- modules/core/src/array.cpp | 35 +- modules/core/src/datastructs.cpp | 4 +- modules/core/src/persistence.cpp | 2853 ++++++++++++++++---- modules/core/src/persistence.hpp | 353 +-- modules/core/src/persistence_base64.cpp | 926 ------- modules/core/src/persistence_c.cpp | 1479 ---------- modules/core/src/persistence_cpp.cpp | 689 ----- modules/core/src/persistence_json.cpp | 1470 +++++----- modules/core/src/persistence_types.cpp | 1397 ++-------- modules/core/src/persistence_xml.cpp | 1552 +++++------ modules/core/src/persistence_yml.cpp | 1435 +++++----- modules/core/test/test_ds.cpp | 24 +- modules/core/test/test_io.cpp | 252 +- modules/features2d/src/matchers.cpp | 10 +- .../features2d/test/test_matchers_algorithmic.cpp | 37 + modules/imgproc/src/histogram.cpp | 2 + modules/imgproc/test/test_approxpoly.cpp | 4 +- modules/imgproc/test/test_contours.cpp | 18 +- modules/imgproc/test/test_convhull.cpp | 10 +- modules/imgproc/test/test_filter.cpp | 14 +- modules/imgproc/test/test_histograms.cpp | 12 +- modules/imgproc/test/test_imgwarp.cpp | 4 +- modules/imgproc/test/test_templmatch.cpp | 6 +- modules/imgproc/test/test_watershed.cpp | 3 + modules/ml/test/test_mltests2.cpp | 5 +- modules/ml/test/test_precomp.hpp | 2 +- modules/objdetect/src/cascadedetect.cpp | 18 +- modules/objdetect/src/cascadedetect.hpp | 6 + modules/objdetect/src/cascadedetect_convert.cpp | 26 +- modules/objdetect/src/haar.cpp | 112 +- modules/objdetect/src/hog.cpp | 56 - modules/objdetect/test/test_cascadeandhog.cpp | 31 +- modules/ts/include/opencv2/ts.hpp | 6 +- modules/ts/src/ts.cpp | 8 +- modules/ts/src/ts_arrtest.cpp | 8 +- modules/video/test/test_camshift.cpp | 10 +- modules/video/test/test_optflowpyrlk.cpp | 94 +- 51 files changed, 5352 insertions(+), 8252 deletions(-) delete mode 100644 modules/core/src/persistence_base64.cpp delete mode 100644 modules/core/src/persistence_c.cpp delete mode 100644 modules/core/src/persistence_cpp.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 260a08f..40a36b6 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -49,8 +49,8 @@ macro(ocv_add_app directory) endif() endmacro() -ocv_add_app(traincascade) -ocv_add_app(createsamples) +#ocv_add_app(traincascade) +#ocv_add_app(createsamples) ocv_add_app(annotation) ocv_add_app(visualisation) ocv_add_app(interactive-calibration) diff --git a/apps/traincascade/old_ml.hpp b/apps/traincascade/old_ml.hpp index bdfe2b4..d5b5937 100644 --- a/apps/traincascade/old_ml.hpp +++ b/apps/traincascade/old_ml.hpp @@ -144,8 +144,8 @@ public: CV_WRAP virtual void save( const char* filename, const char* name=0 ) const; CV_WRAP virtual void load( const char* filename, const char* name=0 ); - virtual void write( CvFileStorage* storage, const char* name ) const; - virtual void read( CvFileStorage* storage, CvFileNode* node ); + virtual void write( cv::FileStorage& storage, const char* name ) const; + virtual void read( const cv::FileNode& node ); protected: const char* default_model_name; @@ -210,8 +210,8 @@ public: bool update=false ); CV_WRAP virtual float predict( const cv::Mat& samples, CV_OUT cv::Mat* results=0, CV_OUT cv::Mat* results_prob=0 ) const; - virtual void write( CvFileStorage* storage, const char* name ) const; - virtual void read( CvFileStorage* storage, CvFileNode* node ); + virtual void write( cv::FileStorage& storage, const char* name ) const; + virtual void read( const cv::FileNode& node ); protected: int var_count, var_all; @@ -521,8 +521,8 @@ public: static CvParamGrid get_default_grid( int param_id ); - virtual void write( CvFileStorage* storage, const char* name ) const; - virtual void read( CvFileStorage* storage, CvFileNode* node ); + virtual void write( cv::FileStorage& storage, const char* name ) const; + virtual void read( const cv::FileNode& node ); CV_WRAP int get_var_count() const { return var_idx ? var_idx->cols : var_all; } protected: @@ -538,8 +538,8 @@ protected: virtual float predict( const float* row_sample, int row_len, bool returnDFVal=false ) const; - virtual void write_params( CvFileStorage* fs ) const; - virtual void read_params( CvFileStorage* fs, CvFileNode* node ); + virtual void write_params( cv::FileStorage& fs ) const; + virtual void read_params( const cv::FileNode& node ); void optimize_linear_svm(); @@ -673,8 +673,8 @@ struct CvDTreeTrainData virtual CvDTreeNode* subsample_data( const CvMat* _subsample_idx ); - virtual void write_params( CvFileStorage* fs ) const; - virtual void read_params( CvFileStorage* fs, CvFileNode* node ); + virtual void write_params( cv::FileStorage& fs ) const; + virtual void read_params( const cv::FileNode& node ); // release all the data virtual void clear(); @@ -1738,7 +1738,7 @@ protected: // Read parameters of the gtb model and data. // // API - // virtual void read_params( CvFileStorage* fs ); + // virtual void read_params( const cv::FileStorage& fs ); // // INPUT // fs - file storage to read parameters from. diff --git a/modules/calib3d/include/opencv2/calib3d.hpp b/modules/calib3d/include/opencv2/calib3d.hpp index 35fb612..01ea765 100644 --- a/modules/calib3d/include/opencv2/calib3d.hpp +++ b/modules/calib3d/include/opencv2/calib3d.hpp @@ -843,6 +843,12 @@ square grouping and ordering algorithm fails. CV_EXPORTS_W bool findChessboardCorners( InputArray image, Size patternSize, OutputArray corners, int flags = CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE ); +/* + Checks whether the image contains chessboard of the specific size or not. + If yes, nonzero value is returned. +*/ +CV_EXPORTS_W bool checkChessboard(InputArray img, Size size); + /** @brief Finds the positions of internal corners of the chessboard using a sector based approach. @param image Source chessboard view. It must be an 8-bit grayscale or color image. diff --git a/modules/calib3d/src/calibinit.cpp b/modules/calib3d/src/calibinit.cpp index 78405a2..c98a428 100644 --- a/modules/calib3d/src/calibinit.cpp +++ b/modules/calib3d/src/calibinit.cpp @@ -526,7 +526,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size, //image is binarised using a threshold dependent on the image histogram if (checkChessboardBinary(thresh_img_new, pattern_size) <= 0) //fall back to the old method { - if (checkChessboard(img, pattern_size) <= 0) + if (!checkChessboard(img, pattern_size)) { corners_.release(); return false; diff --git a/modules/calib3d/src/checkchessboard.cpp b/modules/calib3d/src/checkchessboard.cpp index ea34878..817fd82 100644 --- a/modules/calib3d/src/checkchessboard.cpp +++ b/modules/calib3d/src/checkchessboard.cpp @@ -163,11 +163,12 @@ static bool checkQuads(vector > & quads, const cv::Size & size) int cvCheckChessboard(IplImage* src, CvSize size) { cv::Mat img = cv::cvarrToMat(src); - return checkChessboard(img, size); + return (int)cv::checkChessboard(img, size); } -int checkChessboard(const cv::Mat & img, const cv::Size & size) +bool cv::checkChessboard(InputArray _img, Size size) { + Mat img = _img.getMat(); CV_Assert(img.channels() == 1 && img.depth() == CV_8U); const int erosion_count = 1; @@ -180,13 +181,13 @@ int checkChessboard(const cv::Mat & img, const cv::Size & size) erode(img, white, Mat(), Point(-1, -1), erosion_count); dilate(img, black, Mat(), Point(-1, -1), erosion_count); - int result = 0; + bool result = false; for(float thresh_level = black_level; thresh_level < white_level && !result; thresh_level += 20.0f) { vector > quads; fillQuads(white, black, thresh_level + black_white_gap, thresh_level, quads); if (checkQuads(quads, size)) - result = 1; + result = true; } return result; } diff --git a/modules/calib3d/src/precomp.hpp b/modules/calib3d/src/precomp.hpp index 2335599..1119107 100644 --- a/modules/calib3d/src/precomp.hpp +++ b/modules/calib3d/src/precomp.hpp @@ -153,7 +153,6 @@ static inline bool haveCollinearPoints( const Mat& m, int count ) } // namespace cv -int checkChessboard(const cv::Mat & img, const cv::Size & size); int checkChessboardBinary(const cv::Mat & img, const cv::Size & size); #endif diff --git a/modules/calib3d/test/test_cameracalibration.cpp b/modules/calib3d/test/test_cameracalibration.cpp index b22e6b9..d0dda46 100644 --- a/modules/calib3d/test/test_cameracalibration.cpp +++ b/modules/calib3d/test/test_cameracalibration.cpp @@ -51,7 +51,7 @@ public: CV_ProjectPointsTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void fill_array( int test_case_idx, int i, int j, Mat& arr ); int prepare_test_case( int test_case_idx ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); @@ -83,7 +83,7 @@ CV_ProjectPointsTest::CV_ProjectPointsTest() } -int CV_ProjectPointsTest::read_params( CvFileStorage* fs ) +int CV_ProjectPointsTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; diff --git a/modules/calib3d/test/test_chesscorners_timing.cpp b/modules/calib3d/test/test_chesscorners_timing.cpp index 1e10d96..9bd0f87 100644 --- a/modules/calib3d/test/test_chesscorners_timing.cpp +++ b/modules/calib3d/test/test_chesscorners_timing.cpp @@ -40,8 +40,8 @@ //M*/ #include "test_precomp.hpp" -#include "opencv2/imgproc/imgproc_c.h" -#include "opencv2/calib3d/calib3d_c.h" +#include "opencv2/imgproc.hpp" +#include "opencv2/calib3d.hpp" namespace opencv_test { namespace { @@ -67,117 +67,90 @@ void CV_ChessboardDetectorTimingTest::run( int start_from ) std::string filepath; std::string filename; - CvMat* _v = 0; - CvPoint2D32f* v; - - IplImage img; - IplImage* gray = 0; - IplImage* thresh = 0; + std::vector v; + Mat img, gray, thresh; int idx, max_idx; int progress = 0; filepath = cv::format("%scv/cameracalibration/", ts->get_data_path().c_str() ); filename = cv::format("%schessboard_timing_list.dat", filepath.c_str() ); - CvFileStorage* fs = cvOpenFileStorage( filename.c_str(), 0, CV_STORAGE_READ ); - CvFileNode* board_list = fs ? cvGetFileNodeByName( fs, 0, "boards" ) : 0; + cv::FileStorage fs( filename, FileStorage::READ ); + cv::FileNode board_list = fs["boards"]; + cv::FileNodeIterator bl_it = board_list.begin(); - if( !fs || !board_list || !CV_NODE_IS_SEQ(board_list->tag) || - board_list->data.seq->total % 4 != 0 ) + if( !fs.isOpened() || !board_list.isSeq() || board_list.size() % 4 != 0 ) { ts->printf( cvtest::TS::LOG, "chessboard_timing_list.dat can not be read or is not valid" ); code = cvtest::TS::FAIL_MISSING_TEST_DATA; goto _exit_; } - max_idx = board_list->data.seq->total/4; + max_idx = (int)(board_list.size()/4); + for( idx = 0; idx < start_from; idx++ ) + { + bl_it += 4; + } for( idx = start_from; idx < max_idx; idx++ ) { - int count0 = -1; - int count = 0; Size pattern_size; - int result, result1 = 0; - const char* imgname = cvReadString((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4), "dummy.txt"); - int is_chessboard = cvReadInt((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4+1), 0); - pattern_size.width = cvReadInt((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4 + 2), -1); - pattern_size.height = cvReadInt((CvFileNode*)cvGetSeqElem(board_list->data.seq,idx*4 + 3), -1); + std::string imgname; read(*bl_it++, imgname, "dummy.txt"); + int is_chessboard = 0; + read(*bl_it++, is_chessboard, 0); + read(*bl_it++, pattern_size.width, -1); + read(*bl_it++, pattern_size.height, -1); ts->update_context( this, idx-1, true ); /* read the image */ - filename = cv::format("%s%s", filepath.c_str(), imgname ); + filename = cv::format("%s%s", filepath.c_str(), imgname.c_str() ); - cv::Mat img2 = cv::imread( filename ); - img = cvIplImage(img2); - - if( img2.empty() ) + img = cv::imread( filename ); + if( img.empty() ) { ts->printf( cvtest::TS::LOG, "one of chessboard images can't be read: %s\n", filename.c_str() ); code = cvtest::TS::FAIL_MISSING_TEST_DATA; continue; } - ts->printf(cvtest::TS::LOG, "%s: chessboard %d:\n", imgname, is_chessboard); - - gray = cvCreateImage( cvSize( img.width, img.height ), IPL_DEPTH_8U, 1 ); - thresh = cvCreateImage( cvSize( img.width, img.height ), IPL_DEPTH_8U, 1 ); - cvCvtColor( &img, gray, CV_BGR2GRAY ); - + ts->printf(cvtest::TS::LOG, "%s: chessboard %d:\n", imgname.c_str(), is_chessboard); - count0 = pattern_size.width*pattern_size.height; + cvtColor(img, gray, COLOR_BGR2GRAY); - /* allocate additional buffers */ - _v = cvCreateMat(1, count0, CV_32FC2); - count = count0; + int64 _time0 = cv::getTickCount(); + bool result = cv::checkChessboard(gray, pattern_size); + int64 _time01 = cv::getTickCount(); + bool result1 = findChessboardCorners(gray, pattern_size, v, 15); + int64 _time1 = cv::getTickCount(); - v = (CvPoint2D32f*)_v->data.fl; - - int64 _time0 = cvGetTickCount(); - result = cvCheckChessboard(gray, cvSize(pattern_size)); - int64 _time01 = cvGetTickCount(); - - OPENCV_CALL( result1 = cvFindChessboardCorners( - gray, cvSize(pattern_size), v, &count, 15 )); - int64 _time1 = cvGetTickCount(); - - if( result != is_chessboard ) + if( result != (is_chessboard != 0)) { ts->printf( cvtest::TS::LOG, "Error: chessboard was %sdetected in the image %s\n", - result ? "" : "not ", imgname ); + result ? "" : "not ", imgname.c_str() ); code = cvtest::TS::FAIL_INVALID_OUTPUT; goto _exit_; } if(result != result1) { ts->printf( cvtest::TS::LOG, "Warning: results differ cvCheckChessboard %d, cvFindChessboardCorners %d\n", - result, result1); + (int)result, (int)result1); } - int num_pixels = gray->width*gray->height; - float check_chessboard_time = float(_time01 - _time0)/(float)cvGetTickFrequency(); // in us + int num_pixels = gray.cols*gray.rows; + float check_chessboard_time = float(_time01 - _time0)/(float)cv::getTickFrequency(); // in us ts->printf(cvtest::TS::LOG, " cvCheckChessboard time s: %f, us per pixel: %f\n", check_chessboard_time*1e-6, check_chessboard_time/num_pixels); - float find_chessboard_time = float(_time1 - _time01)/(float)cvGetTickFrequency(); + float find_chessboard_time = float(_time1 - _time01)/(float)cv::getTickFrequency(); ts->printf(cvtest::TS::LOG, " cvFindChessboard time s: %f, us per pixel: %f\n", find_chessboard_time*1e-6, find_chessboard_time/num_pixels); - - cvReleaseMat( &_v ); - cvReleaseImage( &gray ); - cvReleaseImage( &thresh ); progress = update_progress( progress, idx-1, max_idx, 0 ); } _exit_: - /* release occupied memory */ - cvReleaseMat( &_v ); - cvReleaseFileStorage( &fs ); - cvReleaseImage( &gray ); - cvReleaseImage( &thresh ); - if( code < 0 ) ts->set_failed_test_info( code ); } diff --git a/modules/calib3d/test/test_fundam.cpp b/modules/calib3d/test/test_fundam.cpp index 236db6e..8b03de1 100644 --- a/modules/calib3d/test/test_fundam.cpp +++ b/modules/calib3d/test/test_fundam.cpp @@ -525,7 +525,7 @@ public: CV_RodriguesTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void fill_array( int test_case_idx, int i, int j, Mat& arr ); int prepare_test_case( int test_case_idx ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); @@ -559,7 +559,7 @@ CV_RodriguesTest::CV_RodriguesTest() } -int CV_RodriguesTest::read_params( CvFileStorage* fs ) +int CV_RodriguesTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; @@ -767,7 +767,7 @@ public: CV_FundamentalMatTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void fill_array( int test_case_idx, int i, int j, Mat& arr ); int prepare_test_case( int test_case_idx ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); @@ -822,7 +822,7 @@ CV_FundamentalMatTest::CV_FundamentalMatTest() } -int CV_FundamentalMatTest::read_params( CvFileStorage* fs ) +int CV_FundamentalMatTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; @@ -1063,7 +1063,7 @@ public: CV_EssentialMatTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void fill_array( int test_case_idx, int i, int j, Mat& arr ); int prepare_test_case( int test_case_idx ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); @@ -1123,7 +1123,7 @@ CV_EssentialMatTest::CV_EssentialMatTest() } -int CV_EssentialMatTest::read_params( CvFileStorage* fs ) +int CV_EssentialMatTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; @@ -1429,7 +1429,7 @@ public: CV_ConvertHomogeneousTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void fill_array( int test_case_idx, int i, int j, Mat& arr ); double get_success_error_level( int test_case_idx, int i, int j ); @@ -1452,7 +1452,7 @@ CV_ConvertHomogeneousTest::CV_ConvertHomogeneousTest() } -int CV_ConvertHomogeneousTest::read_params( CvFileStorage* fs ) +int CV_ConvertHomogeneousTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; @@ -1562,7 +1562,7 @@ public: CV_ComputeEpilinesTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void fill_array( int test_case_idx, int i, int j, Mat& arr ); double get_success_error_level( int test_case_idx, int i, int j ); @@ -1587,7 +1587,7 @@ CV_ComputeEpilinesTest::CV_ComputeEpilinesTest() } -int CV_ComputeEpilinesTest::read_params( CvFileStorage* fs ) +int CV_ComputeEpilinesTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; diff --git a/modules/calib3d/test/test_undistort.cpp b/modules/calib3d/test/test_undistort.cpp index 80fab69..d358e5a 100644 --- a/modules/calib3d/test/test_undistort.cpp +++ b/modules/calib3d/test/test_undistort.cpp @@ -1064,7 +1064,7 @@ public: CV_ImgWarpBaseTest( bool warp_matrix ); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); int prepare_test_case( int test_case_idx ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high ); @@ -1091,7 +1091,7 @@ CV_ImgWarpBaseTest::CV_ImgWarpBaseTest( bool warp_matrix ) } -int CV_ImgWarpBaseTest::read_params( CvFileStorage* fs ) +int CV_ImgWarpBaseTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; diff --git a/modules/core/include/opencv2/core/core_c.h b/modules/core/include/opencv2/core/core_c.h index 9151397..0d07d9e 100644 --- a/modules/core/include/opencv2/core/core_c.h +++ b/modules/core/include/opencv2/core/core_c.h @@ -1576,8 +1576,8 @@ CVAPI(void) cvRestoreMemStoragePos( CvMemStorage* storage, CvMemStoragePos* pos CVAPI(void*) cvMemStorageAlloc( CvMemStorage* storage, size_t size ); /** Allocates string in memory storage */ -CVAPI(CvString) cvMemStorageAllocString( CvMemStorage* storage, const char* ptr, - int len CV_DEFAULT(-1) ); +//CVAPI(CvString) cvMemStorageAllocString( CvMemStorage* storage, const char* ptr, +// int len CV_DEFAULT(-1) ); /** Creates new empty sequence that will reside in the specified storage */ CVAPI(CvSeq*) cvCreateSeq( int seq_flags, size_t header_size, @@ -1970,6 +1970,7 @@ CVAPI(void) cvSetIPLAllocators( Cv_iplCreateImageHeader create_header, * Data Persistence * \****************************************************************************************/ +#if 0 /********************************** High-level functions ********************************/ /** @brief Opens file storage for reading or writing data. @@ -2556,10 +2557,12 @@ returns NULL. */ CVAPI(CvTypeInfo*) cvTypeOf( const void* struct_ptr ); +#endif + /** @brief Releases an object. -The function finds the type of a given object and calls release with the double pointer. -@param struct_ptr Double pointer to the object + The function finds the type of a given object and calls release with the double pointer. + @param struct_ptr Double pointer to the object */ CVAPI(void) cvRelease( void** struct_ptr ); @@ -2572,41 +2575,6 @@ function, like cvCloneMat. */ CVAPI(void*) cvClone( const void* struct_ptr ); -/** @brief Saves an object to a file. - -The function saves an object to a file. It provides a simple interface to cvWrite . -@param filename File name -@param struct_ptr Object to save -@param name Optional object name. If it is NULL, the name will be formed from filename . -@param comment Optional comment to put in the beginning of the file -@param attributes Optional attributes passed to cvWrite - */ -CVAPI(void) cvSave( const char* filename, const void* struct_ptr, - const char* name CV_DEFAULT(NULL), - const char* comment CV_DEFAULT(NULL), - CvAttrList attributes CV_DEFAULT(cvAttrList())); - -/** @brief Loads an object from a file. - -The function loads an object from a file. It basically reads the specified file, find the first -top-level node and calls cvRead for that node. If the file node does not have type information or -the type information can not be found by the type name, the function returns NULL. After the object -is loaded, the file storage is closed and all the temporary buffers are deleted. Thus, to load a -dynamic structure, such as a sequence, contour, or graph, one should pass a valid memory storage -destination to the function. -@param filename File name -@param memstorage Memory storage for dynamic structures, such as CvSeq or CvGraph . It is not used -for matrices or images. -@param name Optional object name. If it is NULL, the first top-level object in the storage will be -loaded. -@param real_name Optional output parameter that will contain the name of the loaded object -(useful if name=NULL ) - */ -CVAPI(void*) cvLoad( const char* filename, - CvMemStorage* memstorage CV_DEFAULT(NULL), - const char* name CV_DEFAULT(NULL), - const char** real_name CV_DEFAULT(NULL) ); - /*********************************** Measuring Execution Time ***************************/ /** helper functions for RNG initialization and accurate time measurement: @@ -2757,24 +2725,6 @@ static char cvFuncName[] = Name #ifdef __cplusplus -//! @addtogroup core_c_glue -//! @{ - -//! class for automatic module/RTTI data registration/unregistration -struct CV_EXPORTS CvType -{ - CvType( const char* type_name, - CvIsInstanceFunc is_instance, CvReleaseFunc release=0, - CvReadFunc read=0, CvWriteFunc write=0, CvCloneFunc clone=0 ); - ~CvType(); - CvTypeInfo* info; - - static CvTypeInfo* first; - static CvTypeInfo* last; -}; - -//! @} - #include "opencv2/core/utility.hpp" namespace cv diff --git a/modules/core/include/opencv2/core/cvstd.inl.hpp b/modules/core/include/opencv2/core/cvstd.inl.hpp index 5df48ab..f158c3b 100644 --- a/modules/core/include/opencv2/core/cvstd.inl.hpp +++ b/modules/core/include/opencv2/core/cvstd.inl.hpp @@ -73,26 +73,6 @@ public: typedef Vec vec_type; }; -inline -FileNode::operator std::string() const -{ - String value; - read(*this, value, value); - return value; -} - -template<> inline -void operator >> (const FileNode& n, std::string& value) -{ - read(n, value, std::string()); -} - -template<> inline -FileStorage& operator << (FileStorage& fs, const std::string& value) -{ - return fs << cv::String(value); -} - static inline std::ostream& operator << (std::ostream& os, const String& str) { diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index d1e66e8..2832046 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -94,10 +94,6 @@ The same functions can read and write data in both formats; the particular forma the extension of the opened file, ".xml" for XML files, ".yml" or ".yaml" for YAML and ".json" for JSON. */ -typedef struct CvFileStorage CvFileStorage; -typedef struct CvFileNode CvFileNode; -typedef struct CvMat CvMat; -typedef struct CvMatND CvMatND; //! @} core_c @@ -314,7 +310,7 @@ public: WRITE = 1, //!< value, open the file for writing APPEND = 2, //!< value, open the file for appending MEMORY = 4, //!< flag, read data from source or write data to the internal buffer (which is - //!< returned by FileStorage::release) + //!< returned by FileStorage::release) FORMAT_MASK = (7<<3), //!< mask for format flags FORMAT_AUTO = 0, //!< flag, auto format FORMAT_XML = (1<<3), //!< flag, XML format @@ -334,105 +330,77 @@ public: /** @brief The constructors. - The full constructor opens the file. Alternatively you can use the default constructor and then - call FileStorage::open. + The full constructor opens the file. Alternatively you can use the default constructor and then + call FileStorage::open. */ CV_WRAP FileStorage(); /** @overload - @copydoc open() - */ + @copydoc open() + */ CV_WRAP FileStorage(const String& filename, int flags, const String& encoding=String()); - /** @overload */ - FileStorage(CvFileStorage* fs, bool owning=true); - //! the destructor. calls release() virtual ~FileStorage(); /** @brief Opens a file. - See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release - before opening the file. - @param filename Name of the file to open or the text string to read the data from. - Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON - 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.). A file name can also contain parameters. - You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to - FileStorage::BASE64 flag. - @param flags Mode of operation. One of FileStorage::Mode - @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. + See description of parameters in FileStorage::FileStorage. The method calls FileStorage::release + before opening the file. + @param filename Name of the file to open or the text string to read the data from. + Extension of the file (.xml, .yml/.yaml or .json) determines its format (XML, YAML or JSON + 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.). A file name can also contain parameters. + You can use this format, "*?base64" (e.g. "file.json?base64" (case sensitive)), as an alternative to + FileStorage::BASE64 flag. + @param flags Mode of operation. One of FileStorage::Mode + @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. */ CV_WRAP virtual bool open(const String& filename, int flags, const String& encoding=String()); /** @brief Checks whether the file is opened. - @returns true if the object is associated with the current file and false otherwise. It is a - good practice to call this method after you tried to open a file. + @returns true if the object is associated with the current file and false otherwise. It is a + good practice to call this method after you tried to open a file. */ CV_WRAP virtual bool isOpened() const; /** @brief 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. */ CV_WRAP virtual void release(); /** @brief Closes the file and releases all the memory buffers. - 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 + 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 */ CV_WRAP virtual String releaseAndGetString(); /** @brief Returns the first element of the top-level mapping. - @returns The first element of the top-level mapping. + @returns The first element of the top-level mapping. */ CV_WRAP FileNode getFirstTopLevelNode() const; /** @brief Returns the top-level mapping - @param streamidx Zero-based index of the stream. In most cases there is only one stream in the file. - However, YAML supports multiple streams and so there can be several. - @returns The top-level mapping. + @param streamidx Zero-based index of the stream. In most cases there is only one stream in the file. + However, YAML supports multiple streams and so there can be several. + @returns The top-level mapping. */ CV_WRAP FileNode root(int streamidx=0) const; /** @brief Returns the specified element of the top-level mapping. - @param nodename Name of the file node. - @returns Node with the given name. + @param nodename Name of the file node. + @returns Node with the given name. */ FileNode operator[](const String& nodename) const; /** @overload */ CV_WRAP_AS(getNode) FileNode operator[](const char* nodename) const; - /** @brief Returns the obsolete C FileStorage structure. - @returns Pointer to the underlying C FileStorage structure - */ - CvFileStorage* operator *() { return fs.get(); } - - /** @overload */ - const CvFileStorage* operator *() const { return fs.get(); } - - /** @brief Writes multiple numbers. - - Writes one or more numbers of the specified format to the currently written structure. Usually it is - more convenient to use operator `<<` instead of this method. - @param fmt Specification of each array element, see @ref format_spec "format specification" - @param vec Pointer to the written array. - @param len Number of the uchar elements to write. - */ - void writeRaw( const String& fmt, const uchar* vec, size_t len ); - - /** @brief Writes the registered C structure (CvMat, CvMatND, CvSeq). - @param name Name of the written object. - @param obj Pointer to the object. - @see ocvWrite for details. - */ - void writeObj( const String& name, const void* obj ); - /** * @brief Simplified writing API to use with bindings. * @param name Name of the written object @@ -448,19 +416,32 @@ public: /// @overload CV_WRAP void write(const String& name, const std::vector& val); + /** @brief Writes multiple numbers. + + Writes one or more numbers of the specified format to the currently written structure. Usually it is + more convenient to use operator `<<` instead of this method. + @param fmt Specification of each array element, see @ref format_spec "format specification" + @param vec Pointer to the written array. + @param len Number of the uchar elements to write. + */ + void writeRaw( const String& fmt, const void* vec, size_t len ); + /** @brief Writes a comment. - The function writes a comment into file storage. The comments are skipped when the storage is read. - @param comment The written comment, single-line or multi-line - @param append If true, the function tries to put the comment at the end of current line. - Else if the comment is multi-line, or if it does not fit at the end of the current - line, the comment starts a new line. + The function writes a comment into file storage. The comments are skipped when the storage is read. + @param comment The written comment, single-line or multi-line + @param append If true, the function tries to put the comment at the end of current line. + Else if the comment is multi-line, or if it does not fit at the end of the current + line, the comment starts a new line. */ CV_WRAP void writeComment(const String& comment, bool append = false); + void startWriteStruct(const String& name, int flags, const String& typeName); + void endWriteStruct(); + /** @brief Returns the normalized object name for the specified name of a file. - @param filename Name of a file - @returns The normalized object name. + @param filename Name of a file + @returns The normalized object name. */ static String getDefaultObjectName(const String& filename); @@ -469,13 +450,12 @@ public: */ CV_WRAP int getFormat() const; - Ptr fs; //!< the underlying C FileStorage structure - String elname; //!< the currently written element - std::vector structs; //!< the stack of written structures - int state; //!< the writer state -}; + int state; + std::string elname; -template<> struct DefaultDeleter{ CV_EXPORTS void operator ()(CvFileStorage* obj) const; }; + class Impl; + Ptr p; +}; /** @brief File Storage Node class. @@ -493,7 +473,7 @@ class CV_EXPORTS_W_SIMPLE FileNode { public: //! type of the file storage node - enum Type + enum { NONE = 0, //!< empty node INT = 1, //!< an integer @@ -501,56 +481,53 @@ public: FLOAT = REAL, //!< synonym or REAL STR = 3, //!< text string in UTF-8 encoding STRING = STR, //!< synonym for STR - REF = 4, //!< integer of size size_t. Typically used for storing complex dynamic structures where some elements reference the others - SEQ = 5, //!< sequence - MAP = 6, //!< mapping + SEQ = 4, //!< sequence + MAP = 5, //!< mapping TYPE_MASK = 7, + FLOW = 8, //!< compact representation of a sequence or mapping. Used only by YAML writer - USER = 16, //!< a registered object (e.g. a matrix) - EMPTY = 32, //!< empty structure (sequence or mapping) - NAMED = 64 //!< the node has a name (i.e. it is element of a mapping) + UNIFORM = 8, //!< if set, means that all the collection elements are numbers of the same type (real's or int's). + //!< UNIFORM is used only when reading FileStorage; FLOW is used only when writing. So they share the same bit + EMPTY = 16, //!< empty structure (sequence or mapping) + NAMED = 32 //!< the node has a name (i.e. it is element of a mapping). }; /** @brief The constructors. - These constructors are used to create a default file node, construct it from obsolete structures or - from the another file node. + These constructors are used to create a default file node, construct it from obsolete structures or + from the another file node. */ CV_WRAP FileNode(); /** @overload - @param fs Pointer to the obsolete file storage structure. - @param node File node to be used as initialization for the created file node. - */ - FileNode(const CvFileStorage* fs, const CvFileNode* node); + @param fs Pointer to the file storage structure. + @param blockIdx Index of the memory block where the file node is stored + @param ofs Offset in bytes from the beginning of the serialized storage + */ + FileNode(const FileStorage* fs, size_t blockIdx, size_t ofs); /** @overload - @param node File node to be used as initialization for the created file node. - */ + @param node File node to be used as initialization for the created file node. + */ FileNode(const FileNode& node); /** @brief Returns element of a mapping node or a sequence node. - @param nodename Name of an element in the mapping node. - @returns Returns the element with the given identifier. + @param nodename Name of an element in the mapping node. + @returns Returns the element with the given identifier. */ FileNode operator[](const String& nodename) const; /** @overload - @param nodename Name of an element in the mapping node. - */ + @param nodename Name of an element in the mapping node. + */ CV_WRAP_AS(getNode) FileNode operator[](const char* nodename) const; /** @overload - @param i Index of an element in the sequence node. - */ - CV_WRAP_AS(at) FileNode operator[](int i) const; - - /** @brief Returns keys of a mapping node. - @returns Keys of a mapping node. + @param i Index of an element in the sequence node. */ - CV_WRAP std::vector keys() const; + CV_WRAP_AS(at) FileNode operator[](int i) const; /** @brief Returns type of the node. - @returns Type of the node. See FileNode::Type + @returns Type of the node. See FileNode::Type */ CV_WRAP int type() const; @@ -571,9 +548,11 @@ public: //! returns true if the node has a name CV_WRAP bool isNamed() const; //! returns the node name or an empty string if the node is nameless - CV_WRAP String name() const; + CV_WRAP std::string name() const; //! returns the number of elements in the node, if it is a sequence or mapping, or 1 otherwise. CV_WRAP size_t size() const; + //! returns raw size of the FileNode in bytes + CV_WRAP size_t rawSize() const; //! returns the node content as an integer. If the node stores floating-point number, it is rounded. operator int() const; //! returns the node content as float @@ -583,10 +562,14 @@ public: //! returns the node content as text string operator std::string() const; - //! returns pointer to the underlying file node - CvFileNode* operator *(); - //! returns pointer to the underlying file node - const CvFileNode* operator* () const; + static bool isMap(int flags); + static bool isSeq(int flags); + static bool isCollection(int flags); + static bool isEmptyCollection(int flags); + static bool isFlow(int flags); + + uchar* ptr(); + const uchar* ptr() const; //! returns iterator pointing to the first node element FileNodeIterator begin() const; @@ -595,16 +578,18 @@ public: /** @brief Reads node elements to the buffer with the specified format. - Usually it is more convenient to use operator `>>` instead of this method. - @param fmt Specification of each array element. See @ref format_spec "format specification" - @param vec Pointer to the destination array. - @param len Number of elements to read. If it is greater than number of remaining elements then all - of them will be read. + Usually it is more convenient to use operator `>>` instead of this method. + @param fmt Specification of each array element. See @ref format_spec "format specification" + @param vec Pointer to the destination array. + @param len Number of elements to read. If it is greater than number of remaining elements then all + of them will be read. */ - void readRaw( const String& fmt, uchar* vec, size_t len ) const; + void readRaw( const String& fmt, void* vec, size_t len ) const; - //! reads the registered object and returns pointer to it - void* readObj() const; + /** Internal method used when reading FileStorage. + Sets the type (int, real or string) and value of the previously created node. + */ + void setValue( int type, const void* value, int len=-1 ); //! Simplified reading API to use with bindings. CV_WRAP double real() const; @@ -613,37 +598,41 @@ public: //! Simplified reading API to use with bindings. CV_WRAP Mat mat() const; - // do not use wrapper pointer classes for better efficiency - const CvFileStorage* fs; - const CvFileNode* node; + //protected: + const FileStorage* fs; + size_t blockIdx; + size_t ofs; }; /** @brief used to iterate through sequences and mappings. -A standard STL notation, with node.begin(), node.end() denoting the beginning and the end of a -sequence, stored in node. See the data reading sample in the beginning of the section. + A standard STL notation, with node.begin(), node.end() denoting the beginning and the end of a + sequence, stored in node. See the data reading sample in the beginning of the section. */ class CV_EXPORTS FileNodeIterator { public: /** @brief The constructors. - These constructors are used to create a default iterator, set it to specific element in a file node - or construct it from another iterator. + These constructors are used to create a default iterator, set it to specific element in a file node + or construct it from another iterator. */ FileNodeIterator(); /** @overload - @param fs File storage for the iterator. - @param node File node for the iterator. - @param ofs Index of the element in the node. The created iterator will point to this element. - */ - FileNodeIterator(const CvFileStorage* fs, const CvFileNode* node, size_t ofs=0); + @param node File node - the collection to iterate over; + it can be a scalar (equivalent to 1-element collection) or "none" (equivalent to empty collection). + @param seekEnd - true if iterator needs to be set after the last element of the node; + that is: + * node.begin() => FileNodeIterator(node, false) + * node.end() => FileNodeIterator(node, true) + */ + FileNodeIterator(const FileNode& node, bool seekEnd); /** @overload - @param it Iterator to be used as initialization for the created iterator. - */ + @param it Iterator to be used as initialization for the created iterator. + */ FileNodeIterator(const FileNodeIterator& it); //! returns the currently observed element @@ -655,42 +644,32 @@ public: FileNodeIterator& operator ++ (); //! moves iterator to the next node FileNodeIterator operator ++ (int); - //! moves iterator to the previous node - FileNodeIterator& operator -- (); - //! moves iterator to the previous node - FileNodeIterator operator -- (int); //! moves iterator forward by the specified offset (possibly negative) FileNodeIterator& operator += (int ofs); - //! moves iterator backward by the specified offset (possibly negative) - FileNodeIterator& operator -= (int ofs); /** @brief Reads node elements to the buffer with the specified format. - Usually it is more convenient to use operator `>>` instead of this method. - @param fmt Specification of each array element. See @ref format_spec "format specification" - @param vec Pointer to the destination array. - @param maxCount Number of elements to read. If it is greater than number of remaining elements then - all of them will be read. + Usually it is more convenient to use operator `>>` instead of this method. + @param fmt Specification of each array element. See @ref format_spec "format specification" + @param vec Pointer to the destination array. + @param maxCount Number of elements to read. If it is greater than number of remaining elements then + all of them will be read. */ - FileNodeIterator& readRaw( const String& fmt, uchar* vec, + FileNodeIterator& readRaw( const String& fmt, void* vec, size_t maxCount=(size_t)INT_MAX ); - struct SeqReader - { - int header_size; - void* seq; /* sequence, beign read; CvSeq */ - void* block; /* current block; CvSeqBlock */ - schar* ptr; /* pointer to element be read next */ - schar* block_min; /* pointer to the beginning of block */ - schar* block_max; /* pointer to the end of block */ - int delta_index;/* = seq->first->start_index */ - schar* prev_elem; /* pointer to previous element */ - }; + //! returns the number of remaining (not read yet) elements + size_t remaining() const; - const CvFileStorage* fs; - const CvFileNode* container; - SeqReader reader; - size_t remaining; + bool equalTo(const FileNodeIterator& it) const; + +protected: + const FileStorage* fs; + size_t blockIdx; + size_t ofs; + size_t blockSize; + size_t nodeNElems; + size_t idx; }; //! @} core_xml @@ -840,7 +819,7 @@ namespace internal VecReaderProxy( FileNodeIterator* _it ) : it(_it) {} void operator()(std::vector<_Tp>& vec, size_t count) const { - count = std::min(count, it->remaining); + count = std::min(count, it->remaining()); vec.resize(count); for (size_t i = 0; i < count; i++, ++(*it)) read(**it, vec[i], _Tp()); @@ -855,14 +834,14 @@ namespace internal VecReaderProxy( FileNodeIterator* _it ) : it(_it) {} void operator()(std::vector<_Tp>& vec, size_t count) const { - size_t remaining = it->remaining; + 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; + count = count > remaining1 ? remaining1 : count; vec.resize(count); it->readRaw(fmt, !vec.empty() ? (uchar*)&vec[0] : 0, count*sizeof(_Tp)); } @@ -1159,7 +1138,7 @@ static inline void read(const FileNode& node, _Tp& value, const _Tp& default_val template static inline void read( const FileNode& node, std::vector<_Tp>& vec, const std::vector<_Tp>& default_value = std::vector<_Tp>() ) { - if(!node.node) + if(node.empty()) vec = default_value; else { @@ -1171,7 +1150,7 @@ void read( const FileNode& node, std::vector<_Tp>& vec, const std::vector<_Tp>& static inline void read( const FileNode& node, std::vector& vec, const std::vector& default_value ) { - if(!node.node) + if(node.empty()) vec = default_value; else read(node, vec); @@ -1180,7 +1159,7 @@ void read( const FileNode& node, std::vector& vec, const std::vector& vec, const std::vector& default_value ) { - if(!node.node) + if(node.empty()) vec = default_value; else read(node, vec); @@ -1306,73 +1285,23 @@ void operator >> (const FileNode& n, DMatch& m) //! @relates cv::FileNodeIterator //! @{ -static inline -bool operator == (const FileNodeIterator& it1, const FileNodeIterator& it2) -{ - return it1.fs == it2.fs && it1.container == it2.container && - it1.reader.ptr == it2.reader.ptr && it1.remaining == it2.remaining; -} - -static inline -bool operator != (const FileNodeIterator& it1, const FileNodeIterator& it2) -{ - return !(it1 == it2); -} +CV_EXPORTS bool operator == (const FileNodeIterator& it1, const FileNodeIterator& it2); +CV_EXPORTS bool operator != (const FileNodeIterator& it1, const FileNodeIterator& it2); static inline ptrdiff_t operator - (const FileNodeIterator& it1, const FileNodeIterator& it2) { - return it2.remaining - it1.remaining; + return it2.remaining() - it1.remaining(); } static inline bool operator < (const FileNodeIterator& it1, const FileNodeIterator& it2) { - return it1.remaining > it2.remaining; + return it1.remaining() > it2.remaining(); } //! @} FileNodeIterator -//! @cond IGNORED - -inline FileNode FileStorage::getFirstTopLevelNode() const { FileNode r = root(); FileNodeIterator it = r.begin(); return it != r.end() ? *it : FileNode(); } -inline FileNode::FileNode() : fs(0), node(0) {} -inline FileNode::FileNode(const CvFileStorage* _fs, const CvFileNode* _node) : fs(_fs), node(_node) {} -inline FileNode::FileNode(const FileNode& _node) : fs(_node.fs), node(_node.node) {} -inline bool FileNode::empty() const { return node == 0; } -inline bool FileNode::isNone() const { return type() == NONE; } -inline bool FileNode::isSeq() const { return type() == SEQ; } -inline bool FileNode::isMap() const { return type() == MAP; } -inline bool FileNode::isInt() const { return type() == INT; } -inline bool FileNode::isReal() const { return type() == REAL; } -inline bool FileNode::isString() const { return type() == STR; } -inline CvFileNode* FileNode::operator *() { return (CvFileNode*)node; } -inline const CvFileNode* FileNode::operator* () const { return node; } -inline FileNode::operator int() const { int value; read(*this, value, 0); return value; } -inline FileNode::operator float() const { float value; read(*this, value, 0.f); return value; } -inline FileNode::operator double() const { double value; read(*this, value, 0.); return value; } -inline double FileNode::real() const { return double(*this); } -inline String FileNode::string() const { return String(*this); } -inline Mat FileNode::mat() const { Mat value; read(*this, value, value); return value; } -inline FileNodeIterator FileNode::begin() const { return FileNodeIterator(fs, node); } -inline FileNodeIterator FileNode::end() const { return FileNodeIterator(fs, node, size()); } -inline void FileNode::readRaw( const String& fmt, uchar* vec, size_t len ) const { begin().readRaw( fmt, vec, len ); } -inline FileNode FileNodeIterator::operator *() const { return FileNode(fs, (const CvFileNode*)(const void*)reader.ptr); } -inline FileNode FileNodeIterator::operator ->() const { return FileNode(fs, (const CvFileNode*)(const void*)reader.ptr); } - -//! @endcond - - -CV_EXPORTS void cvStartWriteRawData_Base64(::CvFileStorage * fs, const char* name, int len, const char* dt); - -CV_EXPORTS void cvWriteRawData_Base64(::CvFileStorage * fs, const void* _data, int len); - -CV_EXPORTS void cvEndWriteRawData_Base64(::CvFileStorage * fs); - -CV_EXPORTS void cvWriteMat_Base64(::CvFileStorage* fs, const char* name, const ::CvMat* mat); - -CV_EXPORTS void cvWriteMatND_Base64(::CvFileStorage* fs, const char* name, const ::CvMatND* mat); - } // cv #endif // OPENCV_CORE_PERSISTENCE_HPP diff --git a/modules/core/include/opencv2/core/types_c.h b/modules/core/include/opencv2/core/types_c.h index 9898d20..7010f07 100644 --- a/modules/core/include/opencv2/core/types_c.h +++ b/modules/core/include/opencv2/core/types_c.h @@ -1957,6 +1957,8 @@ CvSeqReader; * Data structures for persistence (a.k.a serialization) functionality * \****************************************************************************************/ +#if 0 + /** "black box" file storage */ typedef struct CvFileStorage CvFileStorage; @@ -2109,28 +2111,7 @@ typedef struct CvTypeInfo CvCloneFunc clone; /**< creates a copy of the object */ } CvTypeInfo; - - -/**** System data types ******/ - -typedef struct CvPluginFuncInfo -{ - void** func_addr; - void* default_func_addr; - const char* func_names; - int search_modules; - int loaded_from; -} -CvPluginFuncInfo; - -typedef struct CvModuleInfo -{ - struct CvModuleInfo* next; - const char* name; - const char* version; - CvPluginFuncInfo* func_tab; -} -CvModuleInfo; +#endif /** @} */ diff --git a/modules/core/src/array.cpp b/modules/core/src/array.cpp index a1a23ec..ce6e4e8 100644 --- a/modules/core/src/array.cpp +++ b/modules/core/src/array.cpp @@ -3210,7 +3210,6 @@ void DefaultDeleter::operator ()(IplImage* obj) const { cvReleaseImage void DefaultDeleter::operator ()(CvMatND* obj) const { cvReleaseMatND(&obj); } void DefaultDeleter::operator ()(CvSparseMat* obj) const { cvReleaseSparseMat(&obj); } void DefaultDeleter::operator ()(CvMemStorage* obj) const { cvReleaseMemStorage(&obj); } -void DefaultDeleter::operator ()(CvFileStorage* obj) const { cvReleaseFileStorage(&obj); } template static inline void scalarToRawData_(const Scalar& s, T * const buf, const int cn, const int unroll_to) @@ -3262,4 +3261,38 @@ void scalarToRawData(const Scalar& s, void* _buf, int type, int unroll_to) } // cv:: +/* universal functions */ +CV_IMPL void +cvRelease( void** struct_ptr ) +{ + if( !struct_ptr ) + CV_Error( CV_StsNullPtr, "NULL double pointer" ); + + if( *struct_ptr ) + { + if( CV_IS_MAT(*struct_ptr) ) + cvReleaseMat((CvMat**)struct_ptr); + else if( CV_IS_IMAGE(*struct_ptr)) + cvReleaseImage((IplImage**)struct_ptr); + else + CV_Error( CV_StsError, "Unknown object type" ); + } +} + +void* cvClone( const void* struct_ptr ) +{ + void* ptr = 0; + if( !struct_ptr ) + CV_Error( CV_StsNullPtr, "NULL structure pointer" ); + + if( CV_IS_MAT(struct_ptr) ) + ptr = cvCloneMat((const CvMat*)struct_ptr); + else if( CV_IS_IMAGE(struct_ptr)) + ptr = cvCloneImage((const IplImage*)struct_ptr); + else + CV_Error( CV_StsError, "Unknown object type" ); + return ptr; +} + + /* End of file. */ diff --git a/modules/core/src/datastructs.cpp b/modules/core/src/datastructs.cpp index 1e374c8..e908791 100644 --- a/modules/core/src/datastructs.cpp +++ b/modules/core/src/datastructs.cpp @@ -348,7 +348,7 @@ cvMemStorageAlloc( CvMemStorage* storage, size_t size ) } -CV_IMPL CvString +/*CV_IMPL CvString cvMemStorageAllocString( CvMemStorage* storage, const char* ptr, int len ) { CvString str; @@ -360,7 +360,7 @@ cvMemStorageAllocString( CvMemStorage* storage, const char* ptr, int len ) str.ptr[str.len] = '\0'; return str; -} +}*/ /****************************************************************************************\ diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 30101dc..f869383 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -2,11 +2,36 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html - #include "precomp.hpp" #include "persistence.hpp" +#include +#include + +namespace cv +{ + +namespace fs +{ + +int strcasecmp(const char* s1, const char* s2) +{ + const char* dummy=""; + if(!s1) s1=dummy; + if(!s2) s2=dummy; + + size_t len1 = strlen(s1); + size_t len2 = strlen(s2); + size_t i, len = std::min(len1, len2); + for( i = 0; i < len; i++ ) + { + int d = tolower((int)s1[i]) - tolower((int)s2[i]); + if( d != 0 ) + return d; + } + return len1 < len2 ? -1 : len1 > len2 ? 1 : 0; +} -char* icv_itoa( int _val, char* buffer, int /*radix*/ ) +char* itoa( int _val, char* buffer, int /*radix*/ ) { const int radix = 10; char* ptr=buffer + 23 /* enough even for 64-bit integers */; @@ -27,241 +52,7 @@ char* icv_itoa( int _val, char* buffer, int /*radix*/ ) return ptr; } -void icvPuts( CvFileStorage* fs, const char* str ) -{ - if( fs->outbuf ) - std::copy(str, str + strlen(str), std::back_inserter(*fs->outbuf)); - else if( fs->file ) - fputs( str, fs->file ); -#if USE_ZLIB - else if( fs->gzfile ) - gzputs( fs->gzfile, str ); -#endif - else - CV_Error( CV_StsError, "The storage is not opened" ); -} - -char* icvGets( CvFileStorage* fs, char* str, int maxCount ) -{ - if( fs->strbuf ) - { - size_t i = fs->strbufpos, len = fs->strbufsize; - int j = 0; - const char* instr = fs->strbuf; - while( i < len && j < maxCount-1 ) - { - char c = instr[i++]; - if( c == '\0' ) - break; - str[j++] = c; - if( c == '\n' ) - break; - } - str[j++] = '\0'; - fs->strbufpos = i; - if (maxCount > 256 && !(fs->flags & cv::FileStorage::BASE64)) - CV_Assert(j < maxCount - 1 && "OpenCV persistence doesn't support very long lines"); - return j > 1 ? str : 0; - } - if( fs->file ) - { - char* ptr = fgets( str, maxCount, fs->file ); - if (ptr && maxCount > 256 && !(fs->flags & cv::FileStorage::BASE64)) - { - size_t sz = strnlen(ptr, maxCount); - CV_Assert(sz < (size_t)(maxCount - 1) && "OpenCV persistence doesn't support very long lines"); - } - return ptr; - } -#if USE_ZLIB - if( fs->gzfile ) - { - char* ptr = gzgets( fs->gzfile, str, maxCount ); - if (ptr && maxCount > 256 && !(fs->flags & cv::FileStorage::BASE64)) - { - size_t sz = strnlen(ptr, maxCount); - CV_Assert(sz < (size_t)(maxCount - 1) && "OpenCV persistence doesn't support very long lines"); - } - return ptr; - } -#endif - CV_Error(CV_StsError, "The storage is not opened"); -} - -int icvEof( CvFileStorage* fs ) -{ - if( fs->strbuf ) - return fs->strbufpos >= fs->strbufsize; - if( fs->file ) - return feof(fs->file); -#if USE_ZLIB - if( fs->gzfile ) - return gzeof(fs->gzfile); -#endif - return false; -} - -void icvCloseFile( CvFileStorage* fs ) -{ - if( fs->file ) - fclose( fs->file ); -#if USE_ZLIB - else if( fs->gzfile ) - gzclose( fs->gzfile ); -#endif - fs->file = 0; - fs->gzfile = 0; - fs->strbuf = 0; - fs->strbufpos = 0; - fs->is_opened = false; -} - -void icvRewind( CvFileStorage* fs ) -{ - if( fs->file ) - rewind(fs->file); -#if USE_ZLIB - else if( fs->gzfile ) - gzrewind(fs->gzfile); -#endif - fs->strbufpos = 0; -} - -CvGenericHash* cvCreateMap( int flags, int header_size, int elem_size, CvMemStorage* storage, int start_tab_size ) -{ - if( header_size < (int)sizeof(CvGenericHash) ) - CV_Error( CV_StsBadSize, "Too small map header_size" ); - - if( start_tab_size <= 0 ) - start_tab_size = 16; - - CvGenericHash* map = (CvGenericHash*)cvCreateSet( flags, header_size, elem_size, storage ); - - map->tab_size = start_tab_size; - start_tab_size *= sizeof(map->table[0]); - map->table = (void**)cvMemStorageAlloc( storage, start_tab_size ); - memset( map->table, 0, start_tab_size ); - - return map; -} - -void icvParseError( CvFileStorage* fs, const char* func_name, - const char* err_msg, const char* source_file, int source_line ) -{ - cv::String msg = cv::format("%s(%d): %s", fs->filename, fs->lineno, err_msg); - cv::error(cv::Error::StsParseError, func_name, msg.c_str(), source_file, source_line ); -} - -void icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection ) -{ - if( CV_NODE_IS_MAP(tag) ) - { - if( collection->tag != CV_NODE_NONE ) - { - assert( fs->fmt == CV_STORAGE_FORMAT_XML ); - CV_PARSE_ERROR( "Sequence element should not have name (use <_>)" ); - } - - collection->data.map = cvCreateMap( 0, sizeof(CvFileNodeHash), - sizeof(CvFileMapNode), fs->memstorage, 16 ); - } - else - { - CvSeq* seq; - seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvFileNode), fs->memstorage ); - - // if contains some scalar element, add it to the newly created collection - if( CV_NODE_TYPE(collection->tag) != CV_NODE_NONE ) - cvSeqPush( seq, collection ); - - collection->data.seq = seq; - } - - collection->tag = tag; - cvSetSeqBlockSize( collection->data.seq, 8 ); -} - -static char* icvFSDoResize( CvFileStorage* fs, char* ptr, int len ) -{ - char* new_ptr = 0; - int written_len = (int)(ptr - fs->buffer_start); - int new_size = (int)((fs->buffer_end - fs->buffer_start)*3/2); - new_size = MAX( written_len + len, new_size ); - new_ptr = (char*)cvAlloc( new_size + 256 ); - fs->buffer = new_ptr + (fs->buffer - fs->buffer_start); - if( written_len > 0 ) - memcpy( new_ptr, fs->buffer_start, written_len ); - fs->buffer_start = new_ptr; - fs->buffer_end = fs->buffer_start + new_size; - new_ptr += written_len; - return new_ptr; -} - -char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len ) -{ - return ptr + len < fs->buffer_end ? ptr : icvFSDoResize( fs, ptr, len ); -} - -char* icvFSFlush( CvFileStorage* fs ) -{ - char* ptr = fs->buffer; - int indent; - - if( ptr > fs->buffer_start + fs->space ) - { - ptr[0] = '\n'; - ptr[1] = '\0'; - icvPuts( fs, fs->buffer_start ); - fs->buffer = fs->buffer_start; - } - - indent = fs->struct_indent; - - if( fs->space != indent ) - { - memset( fs->buffer_start, ' ', indent ); - fs->space = indent; - } - - ptr = fs->buffer = fs->buffer_start + fs->space; - - return ptr; -} - -void icvClose( CvFileStorage* fs, cv::String* out ) -{ - if( out ) - out->clear(); - - if( !fs ) - CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" ); - - if( fs->is_opened ) - { - if( fs->write_mode && (fs->file || fs->gzfile || fs->outbuf) ) - { - if( fs->write_stack ) - { - while( fs->write_stack->total > 0 ) - cvEndWriteStruct(fs); - } - icvFSFlush(fs); - if( fs->fmt == CV_STORAGE_FORMAT_XML ) - icvPuts( fs, "\n" ); - else if ( fs->fmt == CV_STORAGE_FORMAT_JSON ) - icvPuts( fs, "}\n" ); - } - - icvCloseFile(fs); - } - - if( fs->outbuf && out ) - { - *out = cv::String(fs->outbuf->begin(), fs->outbuf->end()); - } -} - -char* icvDoubleToString( char* buf, double value ) +char* doubleToString( char* buf, double value, bool explicitZero ) { Cv64suf val; unsigned ieee754_hi; @@ -273,7 +64,12 @@ char* icvDoubleToString( char* buf, double value ) { int ivalue = cvRound(value); if( ivalue == value ) - sprintf( buf, "%d.", ivalue ); + { + if( explicitZero ) + sprintf( buf, "%d.0", ivalue ); + else + sprintf( buf, "%d.", ivalue ); + } else { static const char* fmt = "%.16e"; @@ -299,7 +95,7 @@ char* icvDoubleToString( char* buf, double value ) return buf; } -char* icvFloatToString( char* buf, float value ) +char* floatToString( char* buf, float value, bool halfprecision, bool explicitZero ) { Cv32suf val; unsigned ieee754; @@ -310,10 +106,15 @@ char* icvFloatToString( char* buf, float value ) { int ivalue = cvRound(value); if( ivalue == value ) - sprintf( buf, "%d.", ivalue ); + { + if( explicitZero ) + sprintf( buf, "%d.0", ivalue ); + else + sprintf( buf, "%d.", ivalue ); + } else { - static const char* fmt = "%.8e"; + static const char* fmt = halfprecision ? "%.4e" : "%.8e"; char* ptr = buf; sprintf( buf, fmt, value ); if( *ptr == '+' || *ptr == '-' ) @@ -335,197 +136,16 @@ char* icvFloatToString( char* buf, float value ) return buf; } -static void icvProcessSpecialDouble( CvFileStorage* fs, char* buf, double* value, char** endptr ) -{ - char c = buf[0]; - int inf_hi = 0x7ff00000; - - if( c == '-' || c == '+' ) - { - inf_hi = c == '-' ? 0xfff00000 : 0x7ff00000; - c = *++buf; - } - - if( c != '.' ) - CV_PARSE_ERROR( "Bad format of floating-point constant" ); - - union{double d; uint64 i;} v; - v.d = 0.; - if( toupper(buf[1]) == 'I' && toupper(buf[2]) == 'N' && toupper(buf[3]) == 'F' ) - v.i = (uint64)inf_hi << 32; - else if( toupper(buf[1]) == 'N' && toupper(buf[2]) == 'A' && toupper(buf[3]) == 'N' ) - v.i = (uint64)-1; - else - CV_PARSE_ERROR( "Bad format of floating-point constant" ); - *value = v.d; - - *endptr = buf + 4; -} - - -double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr ) -{ - double fval = strtod( ptr, endptr ); - if( **endptr == '.' ) - { - char* dot_pos = *endptr; - *dot_pos = ','; - double fval2 = strtod( ptr, endptr ); - *dot_pos = '.'; - if( *endptr > dot_pos ) - fval = fval2; - else - *endptr = dot_pos; - } - - if( *endptr == ptr || cv_isalpha(**endptr) ) - icvProcessSpecialDouble( fs, ptr, &fval, endptr ); - - return fval; -} - -void switch_to_Base64_state( CvFileStorage* fs, base64::fs::State state ) -{ - const char * err_unkonwn_state = "Unexpected error, unable to determine the Base64 state."; - const char * err_unable_to_switch = "Unexpected error, unable to switch to this state."; - - /* like a finite state machine */ - switch (fs->state_of_writing_base64) - { - case base64::fs::Uncertain: - switch (state) - { - case base64::fs::InUse: - CV_DbgAssert( fs->base64_writer == 0 ); - fs->base64_writer = new base64::Base64Writer( fs ); - break; - case base64::fs::Uncertain: - break; - case base64::fs::NotUse: - break; - default: - CV_Error( CV_StsError, err_unkonwn_state ); - break; - } - break; - case base64::fs::InUse: - switch (state) - { - case base64::fs::InUse: - case base64::fs::NotUse: - CV_Error( CV_StsError, err_unable_to_switch ); - break; - case base64::fs::Uncertain: - delete fs->base64_writer; - fs->base64_writer = 0; - break; - default: - CV_Error( CV_StsError, err_unkonwn_state ); - break; - } - break; - case base64::fs::NotUse: - switch (state) - { - case base64::fs::InUse: - case base64::fs::NotUse: - CV_Error( CV_StsError, err_unable_to_switch ); - break; - case base64::fs::Uncertain: - break; - default: - CV_Error( CV_StsError, err_unkonwn_state ); - break; - } - break; - default: - CV_Error( CV_StsError, err_unkonwn_state ); - break; - } - - fs->state_of_writing_base64 = state; -} - -void check_if_write_struct_is_delayed( CvFileStorage* fs, bool change_type_to_base64 ) -{ - if ( fs->is_write_struct_delayed ) - { - /* save data to prevent recursive call errors */ - std::string struct_key; - std::string type_name; - int struct_flags = fs->delayed_struct_flags; - - if ( fs->delayed_struct_key != 0 && *fs->delayed_struct_key != '\0' ) - { - struct_key.assign(fs->delayed_struct_key); - } - if ( fs->delayed_type_name != 0 && *fs->delayed_type_name != '\0' ) - { - type_name.assign(fs->delayed_type_name); - } - - /* reset */ - delete[] fs->delayed_struct_key; - delete[] fs->delayed_type_name; - fs->delayed_struct_key = 0; - fs->delayed_struct_flags = 0; - fs->delayed_type_name = 0; - - fs->is_write_struct_delayed = false; - - /* call */ - if ( change_type_to_base64 ) - { - fs->start_write_struct( fs, struct_key.c_str(), struct_flags, "binary"); - if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) - switch_to_Base64_state( fs, base64::fs::Uncertain ); - switch_to_Base64_state( fs, base64::fs::InUse ); - } - else - { - fs->start_write_struct( fs, struct_key.c_str(), struct_flags, type_name.c_str()); - if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) - switch_to_Base64_state( fs, base64::fs::Uncertain ); - switch_to_Base64_state( fs, base64::fs::NotUse ); - } - } -} - -void make_write_struct_delayed( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name ) -{ - CV_Assert( fs->is_write_struct_delayed == false ); - CV_DbgAssert( fs->delayed_struct_key == 0 ); - CV_DbgAssert( fs->delayed_struct_flags == 0 ); - CV_DbgAssert( fs->delayed_type_name == 0 ); - - fs->delayed_struct_flags = struct_flags; - - if ( key != 0 ) - { - fs->delayed_struct_key = new char[strlen(key) + 1U]; - strcpy(fs->delayed_struct_key, key); - } - - if ( type_name != 0 ) - { - fs->delayed_type_name = new char[strlen(type_name) + 1U]; - strcpy(fs->delayed_type_name, type_name); - } - - fs->is_write_struct_delayed = true; -} - -// FIXIT: conflict with 8UC8 (replacement for CV_USRTYPE1) -static const char symbols[9] = "ucwsifdr"; +static const char symbols[9] = "ucwsifdh"; -static char icvTypeSymbol(int depth) +static char typeSymbol(int depth) { CV_StaticAssert(CV_64F == 6, ""); CV_Assert(depth >=0 && depth <= CV_64F); return symbols[depth]; } -static int icvSymbolToType(char c) +static int symbolToType(char c) { const char* pos = strchr( symbols, c ); if( !pos ) @@ -535,15 +155,15 @@ static int icvSymbolToType(char c) return static_cast(pos - symbols); } -char* icvEncodeFormat(int elem_type, char* dt) +char* encodeFormat(int elem_type, char* dt) { int cn = (elem_type == CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/) ? 1 : CV_MAT_CN(elem_type); - char symbol = (elem_type == CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/) ? 'r' : icvTypeSymbol(CV_MAT_DEPTH(elem_type)); + char symbol = (elem_type == CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/) ? 'r' : typeSymbol(CV_MAT_DEPTH(elem_type)); sprintf(dt, "%d%c", cn, symbol); return dt + (cn == 1 ? 1 : 0); } -int icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len ) +int decodeFormat( const char* dt, int* fmt_pairs, int max_len ) { int fmt_pair_count = 0; int i = 0, k = 0, len = dt ? (int)strlen(dt) : 0; @@ -576,7 +196,7 @@ int icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len ) } else { - int depth = icvSymbolToType(c); + int depth = symbolToType(c); if( fmt_pairs[i] == 0 ) fmt_pairs[i] = 1; fmt_pairs[i+1] = depth; @@ -596,14 +216,13 @@ int icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len ) return fmt_pair_count; } - -int icvCalcElemSize( const char* dt, int initial_size ) +int calcElemSize( const char* dt, int initial_size ) { int size = 0; int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count; int comp_size; - fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); + fmt_pair_count = decodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); fmt_pair_count *= 2; for( i = 0, size = initial_size; i < fmt_pair_count; i += 2 ) { @@ -620,9 +239,9 @@ int icvCalcElemSize( const char* dt, int initial_size ) } -int icvCalcStructSize( const char* dt, int initial_size ) +int calcStructSize( const char* dt, int initial_size ) { - int size = icvCalcElemSize( dt, initial_size ); + int size = calcElemSize( dt, initial_size ); size_t elem_max_size = 0; for ( const char * type = dt; *type != '\0'; type++ ) { switch ( *type ) @@ -641,12 +260,12 @@ int icvCalcStructSize( const char* dt, int initial_size ) return size; } -int icvDecodeSimpleFormat( const char* dt ) +int decodeSimpleFormat( const char* dt ) { int elem_type = -1; int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count; - fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); + fmt_pair_count = decodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); if( fmt_pair_count != 1 || fmt_pairs[0] >= CV_CN_MAX) CV_Error( CV_StsError, "Too complex format for the matrix" ); @@ -655,53 +274,2337 @@ int icvDecodeSimpleFormat( const char* dt ) return elem_type; } -void icvWriteCollection( CvFileStorage* fs, const CvFileNode* node ) +} + +#if defined __i386__ || defined(_M_IX86) || defined __x86_64__ || defined(_M_X64) +#define CV_UNALIGNED_LITTLE_ENDIAN_MEM_ACCESS 1 +#else +#define CV_UNALIGNED_LITTLE_ENDIAN_MEM_ACCESS 0 +#endif + +static inline int readInt(const uchar* p) +{ +#if CV_UNALIGNED_LITTLE_ENDIAN_MEM_ACCESS + return *(const int*)p; +#else + int val = (int)(p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)); + return val; +#endif +} + +static inline double readReal(const uchar* p) +{ +#if CV_UNALIGNED_LITTLE_ENDIAN_MEM_ACCESS + return *(const double*)p; +#else + unsigned val0 = (unsigned)(p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)); + unsigned val1 = (unsigned)(p[4] | (p[5] << 8) | (p[6] << 16) | (p[7] << 24)); + Cv64suf val; + val.u = val0 | ((uint64)val1 << 32); + return val.f; +#endif +} + +static inline void writeInt(uchar* p, int ival) { - int i, total = node->data.seq->total; - int elem_size = node->data.seq->elem_size; - int is_map = CV_NODE_IS_MAP(node->tag); - CvSeqReader reader; +#if CV_UNALIGNED_LITTLE_ENDIAN_MEM_ACCESS + int* ip = (int*)p; + *ip = ival; +#else + p[0] = (uchar)ival; + p[1] = (uchar)(ival >> 8); + p[2] = (uchar)(ival >> 16); + p[3] = (uchar)(ival >> 24); +#endif +} + +static inline void writeReal(uchar* p, double fval) +{ +#if CV_UNALIGNED_LITTLE_ENDIAN_MEM_ACCESS + double* fp = (double*)p; + *fp = fval; +#else + Cv64suf v; + v.f = fval; + p[0] = (uchar)v.u; + p[1] = (uchar)(v.u >> 8); + p[2] = (uchar)(v.u >> 16); + p[3] = (uchar)(v.u >> 24); + p[4] = (uchar)(v.u >> 32); + p[5] = (uchar)(v.u >> 40); + p[6] = (uchar)(v.u >> 48); + p[7] = (uchar)(v.u >> 56); +#endif +} + +class FileStorage::Impl : public FileStorage_API +{ +public: + enum State + { + UNDEFINED = 0, + VALUE_EXPECTED = 1, + NAME_EXPECTED = 2, + INSIDE_MAP = 4 + }; + + void init() + { + buffer.clear(); + bufofs = 0; + state = UNDEFINED; + is_opened = false; + dummy_eof = false; + write_mode = false; + mem_mode = false; + space = 0; + fmt = 0; + file = 0; + gzfile = 0; + empty_stream = true; + + strbufv.clear(); + strbuf = 0; + strbufsize = strbufpos = 0; + roots.clear(); + + fs_data.clear(); + fs_data_ptrs.clear(); + fs_data_blksz.clear(); + freeSpaceOfs = 0; + + str_hash.clear(); + str_hash_data.clear(); + str_hash_data.resize(1); + str_hash_data[0] = '\0'; + + filename.clear(); + lineno = 0; + } - cvStartReadSeq( node->data.seq, &reader, 0 ); + Impl(FileStorage* _fs) + { + fs_ext = _fs; + init(); + } + + virtual ~Impl() + { + release(); + } - for( i = 0; i < total; i++ ) + void release(String* out=0) { - CvFileMapNode* elem = (CvFileMapNode*)reader.ptr; - if( !is_map || CV_IS_SET_ELEM(elem) ) + if( is_opened ) { - const char* name = is_map ? elem->key->str.ptr : 0; - icvWriteFileNode( fs, name, &elem->value ); + if(out) + out->clear(); + if( write_mode ) + { + while( write_stack.size() > 1 ) + { + endWriteStruct(); + } + flush(); + if( fmt == FileStorage::FORMAT_XML ) + puts( "\n" ); + else if ( fmt == FileStorage::FORMAT_JSON ) + puts( "}\n" ); + } + + closeFile(); + if( mem_mode && out ) + { + *out = cv::String(outbuf.begin(), outbuf.end()); + } + init(); } - CV_NEXT_SEQ_ELEM( elem_size, reader ); } -} -void icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node ) -{ - switch( CV_NODE_TYPE(node->tag) ) + void analyze_file_name( const std::string& file_name, std::vector& params ) { - case CV_NODE_INT: - fs->write_int( fs, name, node->data.i ); - break; - case CV_NODE_REAL: - fs->write_real( fs, name, node->data.f ); - break; - case CV_NODE_STR: - fs->write_string( fs, name, node->data.str.ptr, 0 ); - break; - case CV_NODE_SEQ: - case CV_NODE_MAP: - cvStartWriteStruct( fs, name, CV_NODE_TYPE(node->tag) + - (CV_NODE_SEQ_IS_SIMPLE(node->data.seq) ? CV_NODE_FLOW : 0), - node->info ? node->info->type_name : 0 ); - icvWriteCollection( fs, node ); - cvEndWriteStruct( fs ); - break; - case CV_NODE_NONE: - cvStartWriteStruct( fs, name, CV_NODE_SEQ, 0 ); - cvEndWriteStruct( fs ); - break; - default: - CV_Error( CV_StsBadFlag, "Unknown type of file node" ); + params.clear(); + static const char not_file_name = '\n'; + static const char parameter_begin = '?'; + static const char parameter_separator = '&'; + + if( file_name.find(not_file_name, (size_t)0) != std::string::npos ) + return; + + size_t beg = file_name.find_last_of(parameter_begin); + params.push_back(file_name.substr((size_t)0, beg)); + + if( beg != std::string::npos ) + { + size_t end = file_name.size(); + beg++; + for( size_t param_beg = beg, param_end = beg; + param_end < end; + param_beg = param_end + 1 ) + { + param_end = file_name.find_first_of( parameter_separator, param_beg ); + if( (param_end == std::string::npos || param_end != param_beg) && param_beg + 1 < end ) + { + params.push_back( file_name.substr( param_beg, param_end - param_beg ) ); + } + } + } } + + bool open( const char* filename_or_buf, int _flags, const char* encoding ) + { + _flags &= ~FileStorage::BASE64; + + bool ok = true; + release(); + + bool append = (_flags & 3) == FileStorage::APPEND; + mem_mode = (_flags & FileStorage::MEMORY) != 0; + + write_mode = (_flags & 3) != 0; + + bool isGZ = false; + size_t fnamelen = 0; + + std::vector params; + //if ( !mem_mode ) + { + analyze_file_name( filename_or_buf, params ); + if( !params.empty() ) + filename = params[0]; + + /*if( !write_base64 && params.size() >= 2 && + std::find(params.begin()+1, params.end(), std::string("base64")) != params.end()) + write_base64 = (write_mode || append);*/ + } + + if( filename.size() == 0 && !mem_mode && !write_mode ) + CV_Error( CV_StsNullPtr, "NULL or empty filename" ); + + if( mem_mode && append ) + CV_Error( CV_StsBadFlag, "FileStorage::APPEND and FileStorage::MEMORY are not currently compatible" ); + + flags = _flags; + + if( !mem_mode ) + { + char* dot_pos = strrchr((char*)filename.c_str(), '.'); + char compression = '\0'; + + if( dot_pos && dot_pos[1] == 'g' && dot_pos[2] == 'z' && + (dot_pos[3] == '\0' || (cv_isdigit(dot_pos[3]) && dot_pos[4] == '\0')) ) + { + if( append ) + { + CV_Error(CV_StsNotImplemented, "Appending data to compressed file is not implemented" ); + } + isGZ = true; + compression = dot_pos[3]; + if( compression ) + dot_pos[3] = '\0', fnamelen--; + } + + if( !isGZ ) + { + file = fopen(filename.c_str(), !write_mode ? "rt" : !append ? "wt" : "a+t" ); + if( !file ) + return false; + } + else + { +#if USE_ZLIB + char mode[] = { write_mode ? 'w' : 'r', 'b', compression ? compression : '3', '\0' }; + gzfile = gzopen(filename.c_str(), mode); + if( !gzfile ) + return false; +#else + CV_Error(CV_StsNotImplemented, "There is no compressed file storage support in this configuration"); +#endif + } + } + + roots.clear(); + fs_data.clear(); + wrap_margin = 71; + fmt = FileStorage::FORMAT_AUTO; + + if( write_mode ) + { + fmt = flags & FileStorage::FORMAT_MASK; + + if( mem_mode ) + outbuf.clear(); + + if( fmt == FileStorage::FORMAT_AUTO && !filename.empty() ) + { + const char* dot_pos = NULL; + const char* dot_pos2 = NULL; + // like strrchr() implementation, but save two last positions simultaneously + for (const char* pos = &filename[0]; pos[0] != 0; pos++) + { + if( pos[0] == '.' ) + { + dot_pos2 = dot_pos; + dot_pos = pos; + } + } + if (fs::strcasecmp(dot_pos, ".gz") == 0 && dot_pos2 != NULL) + { + dot_pos = dot_pos2; + } + fmt = (fs::strcasecmp(dot_pos, ".xml") == 0 || fs::strcasecmp(dot_pos, ".xml.gz") == 0 ) + ? FileStorage::FORMAT_XML + : (fs::strcasecmp(dot_pos, ".json") == 0 || fs::strcasecmp(dot_pos, ".json.gz") == 0) + ? FileStorage::FORMAT_JSON + : FileStorage::FORMAT_YAML; + } + else if( fmt == FileStorage::FORMAT_AUTO ) + { + fmt = FileStorage::FORMAT_XML; + } + + // we use factor=6 for XML (the longest characters (' and ") are encoded with 6 bytes (' and ") + // and factor=4 for YAML ( as we use 4 bytes for non ASCII characters (e.g. \xAB)) + int buf_size = CV_FS_MAX_LEN*(fmt == FileStorage::FORMAT_XML ? 6 : 4) + 1024; + + if (append) + { + fseek( file, 0, SEEK_END ); + if (ftell(file) == 0) + append = false; + } + + write_stack.clear(); + empty_stream = true; + write_stack.push_back(FStructData("", FileNode::MAP | FileNode::EMPTY, 0)); + buffer.reserve(buf_size + 1024); + buffer.resize(buf_size); + bufofs = 0; + + if( fmt == FileStorage::FORMAT_XML ) + { + size_t file_size = file ? (size_t)ftell(file) : (size_t)0; + if( !append || file_size == 0 ) + { + if( encoding && *encoding != '\0' ) + { + if( fs::strcasecmp(encoding, "UTF-16" ) == 0 ) + { + release(); + CV_Error( CV_StsBadArg, "UTF-16 XML encoding is not supported! Use 8-bit encoding\n"); + } + + CV_Assert( strlen(encoding) < 1000 ); + char buf[1100]; + sprintf(buf, "\n", encoding); + puts( buf ); + } + else + puts( "\n" ); + puts( "\n" ); + } + else + { + int xml_buf_size = 1 << 10; + char substr[] = ""; + int last_occurence = -1; + xml_buf_size = MIN(xml_buf_size, int(file_size)); + fseek( file, -xml_buf_size, SEEK_END ); + std::vector xml_buf_(xml_buf_size+2); + // find the last occurrence of + for(;;) + { + int line_offset = (int)ftell( file ); + char* ptr0 = gets( &xml_buf_[0], xml_buf_size ), *ptr; + if( !ptr0 ) + break; + ptr = ptr0; + for(;;) + { + ptr = strstr( ptr, substr ); + if( !ptr ) + break; + last_occurence = line_offset + (int)(ptr - ptr0); + ptr += strlen(substr); + } + } + if( last_occurence < 0 ) + { + release(); + CV_Error( CV_StsError, "Could not find in the end of file.\n" ); + } + closeFile(); + file = fopen( filename.c_str(), "r+t" ); + CV_Assert(file != 0); + fseek( file, last_occurence, SEEK_SET ); + // replace the last "" with " ", which has the same length + puts( " " ); + fseek( file, 0, SEEK_END ); + puts( "\n" ); + } + + emitter = createXMLEmitter(this); + } + else if( fmt == FileStorage::FORMAT_YAML ) + { + if( !append) + puts( "%YAML:1.0\n---\n" ); + else + puts( "...\n---\n" ); + + emitter = createYAMLEmitter(this); + } + else + { + CV_Assert( fmt == FileStorage::FORMAT_JSON ); + if( !append ) + puts( "{\n" ); + else + { + bool valid = false; + long roffset = 0; + for ( ; + fseek( file, roffset, SEEK_END ) == 0; + roffset -= 1 ) + { + const char end_mark = '}'; + if ( fgetc( file ) == end_mark ) + { + fseek( file, roffset, SEEK_END ); + valid = true; + break; + } + } + + if ( valid ) + { + closeFile(); + file = fopen( filename.c_str(), "r+t" ); + CV_Assert(file != 0); + fseek( file, roffset, SEEK_END ); + fputs( ",", file ); + } + else + { + CV_Error( CV_StsError, "Could not find '}' in the end of file.\n" ); + } + } + write_stack.back().indent = 4; + emitter = createJSONEmitter(this); + } + is_opened = true; + } + else + { + const size_t buf_size0 = 1 << 20; + size_t buf_size = buf_size0; + if( mem_mode ) + { + strbuf = (char*)filename_or_buf; + strbufsize = strlen(strbuf); + } + + const char* yaml_signature = "%YAML"; + const char* json_signature = "{"; + const char* xml_signature = "(); + } + + if( !parser.empty() ) + { + ok = parser->parse(ptr); + if( ok ) + { + finalizeCollection(root_nodes); + + CV_Assert( !fs_data_ptrs.empty() ); + FileNode roots_node(fs_ext, 0, 0); + size_t i, nroots = roots_node.size(); + FileNodeIterator it = roots_node.begin(); + + for( i = 0; i < nroots; i++, ++it ) + roots.push_back(*it); + } + } + } + catch(...) + { + is_opened = true; + release(); + throw; + } + + // release resources that we do not need anymore + closeFile(); + is_opened = true; + std::vector tmpbuf; + std::swap(buffer, tmpbuf); + bufofs = 0; + } + return ok; + } + + void puts( const char* str ) + { + CV_Assert( write_mode ); + if( mem_mode ) + std::copy(str, str + strlen(str), std::back_inserter(outbuf)); + else if( file ) + fputs( str, file ); +#if USE_ZLIB + else if( gzfile ) + gzputs( gzfile, str ); +#endif + else + CV_Error( CV_StsError, "The storage is not opened" ); + } + + char* gets( char* str, int maxCount ) + { + if( strbuf ) + { + size_t i = strbufpos, len = strbufsize; + int j = 0; + const char* instr = strbuf; + while( i < len && j < maxCount-1 ) + { + char c = instr[i++]; + if( c == '\0' ) + break; + str[j++] = c; + if( c == '\n' ) + break; + } + str[j++] = '\0'; + strbufpos = i; + if (maxCount > 256 && !(flags & cv::FileStorage::BASE64)) + CV_Assert(j < maxCount - 1 && "OpenCV persistence doesn't support very long lines"); + return j > 1 ? str : 0; + } + if( file ) + { + char* ptr = fgets( str, maxCount, file ); + if (ptr && maxCount > 256 && !(flags & cv::FileStorage::BASE64)) + { + size_t sz = strnlen(ptr, maxCount); + CV_Assert(sz < (size_t)(maxCount - 1) && "OpenCV persistence doesn't support very long lines"); + } + return ptr; + } +#if USE_ZLIB + if( gzfile ) + { + char* ptr = gzgets( gzfile, str, maxCount ); + if (ptr && maxCount > 256 && !(flags & cv::FileStorage::BASE64)) + { + size_t sz = strnlen(ptr, maxCount); + CV_Assert(sz < (size_t)(maxCount - 1) && "OpenCV persistence doesn't support very long lines"); + } + return ptr; + } +#endif + CV_Error(CV_StsError, "The storage is not opened"); + } + + char* gets() + { + char* ptr = gets(bufferStart(), (int)(bufferEnd() - bufferStart())); + if( !ptr ) + { + ptr = bufferStart(); // FIXIT Why do we need this hack? What is about other parsers JSON/YAML? + *ptr = '\0'; + setEof(); + return 0; + } + else + { + FileStorage_API* fs = this; + int l = (int)strlen(ptr); + if( l > 0 && ptr[l-1] != '\n' && ptr[l-1] != '\r' && !eof() ) + CV_PARSE_ERROR_CPP("Too long string or a last string w/o newline"); + } + lineno++; + return ptr; + } + + bool eof() + { + if( dummy_eof ) + return true; + if( strbuf ) + return strbufpos >= strbufsize; + if( file ) + return feof(file) != 0; +#if USE_ZLIB + if( gzfile ) + return gzeof(gzfile) != 0; +#endif + return false; + } + + void setEof() + { + dummy_eof = true; + } + + void closeFile() + { + if( file ) + fclose( file ); +#if USE_ZLIB + else if( gzfile ) + gzclose( gzfile ); +#endif + file = 0; + gzfile = 0; + strbuf = 0; + strbufpos = 0; + is_opened = false; + } + + void rewind() + { + if( file ) + ::rewind(file); +#if USE_ZLIB + else if( gzfile ) + gzrewind(gzfile); +#endif + strbufpos = 0; + } + + char* resizeWriteBuffer( char* ptr, int len ) + { + const char* buffer_end = &buffer[0] + buffer.size(); + if( ptr + len < buffer_end ) + return ptr; + + const char* buffer_start = &buffer[0]; + int written_len = (int)(ptr - buffer_start); + + CV_Assert(written_len <= (int)buffer.size()); + int new_size = (int)((buffer_end - buffer_start)*3/2); + new_size = MAX( written_len + len, new_size ); + buffer.reserve( new_size + 256 ); + buffer.resize( new_size ); + bufofs = written_len; + return &buffer[0] + bufofs; + } + + char* flush() + { + char* buffer_start = &buffer[0]; + char* ptr = buffer_start + bufofs; + + if( ptr > buffer_start + space ) + { + ptr[0] = '\n'; + ptr[1] = '\0'; + puts( buffer_start ); + bufofs = 0; + } + + int indent = write_stack.back().indent; + + if( space != indent ) + { + memset( buffer_start, ' ', indent ); + space = indent; + } + bufofs = space; + ptr = buffer_start + bufofs; + + return ptr; + } + + void endWriteStruct() + { + CV_Assert( write_mode ); + CV_Assert( !write_stack.empty() ); + + FStructData& current_struct = write_stack.back(); + if( fmt == FileStorage::FORMAT_JSON && !FileNode::isFlow(current_struct.flags) && write_stack.size() > 1 ) + current_struct.indent = write_stack[write_stack.size() - 2].indent; + + emitter->endWriteStruct(current_struct); + + write_stack.pop_back(); + if( !write_stack.empty() ) + write_stack.back().flags &= ~FileNode::EMPTY; + } + + void startWriteStruct( const char* key, int struct_flags, + const char* type_name ) + { + CV_Assert( write_mode ); + + struct_flags = (struct_flags & (FileNode::TYPE_MASK|FileNode::FLOW)) | FileNode::EMPTY; + if( !FileNode::isCollection(struct_flags)) + CV_Error( CV_StsBadArg, + "Some collection type: FileNode::SEQ or FileNode::MAP must be specified" ); + + if( type_name && type_name[0] == '\0' ) + type_name = 0; + + FStructData s = emitter->startWriteStruct( write_stack.back(), key, struct_flags, type_name ); + write_stack.push_back(s); + size_t write_stack_size = write_stack.size(); + if( write_stack_size > 1 ) + write_stack[write_stack_size-2].flags &= ~FileNode::EMPTY; + + if( !FileNode::isFlow(s.flags) ) + flush(); + + if( fmt == FileStorage::FORMAT_JSON && type_name && type_name[0] && FileNode::isMap(struct_flags)) + { + emitter->write("type_id", type_name, false); + } + } + + void writeComment( const char* comment, bool eol_comment ) + { + CV_Assert(write_mode); + emitter->writeComment( comment, eol_comment ); + } + + void startNextStream() + { + CV_Assert(write_mode); + if( !empty_stream ) + { + while( !write_stack.empty() ) + endWriteStruct(); + flush(); + emitter->startNextStream(); + empty_stream = true; + write_stack.push_back(FStructData("", FileNode::EMPTY, 0)); + bufofs = 0; + } + } + + void write( const String& key, int value ) + { + CV_Assert(write_mode); + emitter->write(key.c_str(), value); + } + + void write( const String& key, double value ) + { + CV_Assert(write_mode); + emitter->write(key.c_str(), value); + } + + void write( const String& key, const String& value ) + { + CV_Assert(write_mode); + emitter->write(key.c_str(), value.c_str(), false); + } + + void writeRawData( const std::string& dt, const void* _data, size_t len ) + { + CV_Assert(write_mode); + + size_t elemSize = fs::calcStructSize(dt.c_str(), 0); + CV_Assert( len % elemSize == 0 ); + len /= elemSize; + + bool explicitZero = fmt == FileStorage::FORMAT_JSON; + const uchar* data0 = (const uchar*)_data; + int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k, fmt_pair_count; + char buf[256] = ""; + + fmt_pair_count = fs::decodeFormat( dt.c_str(), fmt_pairs, CV_FS_MAX_FMT_PAIRS ); + + if( !len ) + return; + + if( !data0 ) + CV_Error( CV_StsNullPtr, "Null data pointer" ); + + if( fmt_pair_count == 1 ) + { + fmt_pairs[0] *= (int)len; + len = 1; + } + + for(;len--; data0 += elemSize) + { + int offset = 0; + for( k = 0; k < fmt_pair_count; k++ ) + { + int i, count = fmt_pairs[k*2]; + int elem_type = fmt_pairs[k*2+1]; + int elem_size = CV_ELEM_SIZE(elem_type); + const char *ptr; + + offset = cvAlign( offset, elem_size ); + const uchar* data = data0 + offset; + + for( i = 0; i < count; i++ ) + { + switch( elem_type ) + { + case CV_8U: + ptr = fs::itoa( *(uchar*)data, buf, 10 ); + data++; + break; + case CV_8S: + ptr = fs::itoa( *(char*)data, buf, 10 ); + data++; + break; + case CV_16U: + ptr = fs::itoa( *(ushort*)data, buf, 10 ); + data += sizeof(ushort); + break; + case CV_16S: + ptr = fs::itoa( *(short*)data, buf, 10 ); + data += sizeof(short); + break; + case CV_32S: + ptr = fs::itoa( *(int*)data, buf, 10 ); + data += sizeof(int); + break; + case CV_32F: + ptr = fs::floatToString( buf, *(float*)data, false, explicitZero ); + data += sizeof(float); + break; + case CV_64F: + ptr = fs::doubleToString( buf, *(double*)data, explicitZero ); + data += sizeof(double); + break; + case CV_16F: /* reference */ + ptr = fs::floatToString( buf, (float)*(float16_t*)data, true, explicitZero ); + data += sizeof(float16_t); + break; + default: + CV_Error( CV_StsUnsupportedFormat, "Unsupported type" ); + return; + } + + emitter->writeScalar(0, ptr); + } + + offset = (int)(data - data0); + } + } + } + + void writeRawDataBase64(const void* /*data*/, int /*len*/, const char* /*dt*/ ) + { + + } + + String releaseAndGetString(); + + FileNode getFirstTopLevelNode() const + { + return roots.empty() ? FileNode() : roots[0]; + } + + FileNode root(int streamIdx=0) const + { + return streamIdx >= 0 && streamIdx < (int)roots.size() ? roots[streamIdx] : FileNode(); + } + + FileNode operator[](const String& nodename) const + { + return this->operator[](nodename.c_str()); + } + + FileNode operator[](const char* /*nodename*/) const + { + return FileNode(); + } + + int getFormat() const { return fmt; } + + char* bufferPtr() const { return (char*)(&buffer[0] + bufofs); } + char* bufferStart() const { return (char*)&buffer[0]; } + char* bufferEnd() const { return (char*)(&buffer[0] + buffer.size()); } + void setBufferPtr(char* ptr) + { + char* bufferstart = bufferStart(); + CV_Assert( ptr >= bufferstart && ptr <= bufferEnd() ); + bufofs = ptr - bufferstart; + } + int wrapMargin() const { return wrap_margin; } + + FStructData& getCurrentStruct() + { + CV_Assert(!write_stack.empty()); + return write_stack.back(); + } + + void setNonEmpty() + { + empty_stream = false; + } + + void processSpecialDouble( char* buf, double* value, char** endptr ) + { + FileStorage_API* fs = this; + char c = buf[0]; + int inf_hi = 0x7ff00000; + + if( c == '-' || c == '+' ) + { + inf_hi = c == '-' ? 0xfff00000 : 0x7ff00000; + c = *++buf; + } + + if( c != '.' ) + CV_PARSE_ERROR_CPP( "Bad format of floating-point constant" ); + + Cv64suf v; + v.f = 0.; + if( toupper(buf[1]) == 'I' && toupper(buf[2]) == 'N' && toupper(buf[3]) == 'F' ) + v.u = (uint64)inf_hi << 32; + else if( toupper(buf[1]) == 'N' && toupper(buf[2]) == 'A' && toupper(buf[3]) == 'N' ) + v.u = (uint64)-1; + else + CV_PARSE_ERROR_CPP( "Bad format of floating-point constant" ); + *value = v.f; + *endptr = buf + 4; + } + + double strtod( char* ptr, char** endptr ) + { + double fval = ::strtod( ptr, endptr ); + if( **endptr == '.' ) + { + char* dot_pos = *endptr; + *dot_pos = ','; + double fval2 = ::strtod( ptr, endptr ); + *dot_pos = '.'; + if( *endptr > dot_pos ) + fval = fval2; + else + *endptr = dot_pos; + } + + if( *endptr == ptr || cv_isalpha(**endptr) ) + processSpecialDouble( ptr, &fval, endptr ); + + return fval; + } + + void convertToCollection(int type, FileNode& node) + { + CV_Assert( type == FileNode::SEQ || type == FileNode::MAP ); + + int node_type = node.type(); + if( node_type == type ) + return; + + bool named = node.isNamed(); + uchar* ptr = node.ptr() + 1 + (named ? 4 : 0); + + int ival = 0; + double fval = 0; + std::string sval; + bool add_first_scalar = false; + + if( node_type != FileNode::NONE ) + { + // scalar nodes can only be converted to sequences, e.g. in XML: + // 5[parser_position]... => create 5 with name "a" + // 5 6[parser_position]... => 5 is converted to [5] and then 6 is added to it + // + // otherwise we don't know where to get the element names from + CV_Assert( type == FileNode::SEQ ); + if( node_type == FileNode::INT ) + { + ival = readInt(ptr); + add_first_scalar = true; + } + else if( node_type == FileNode::REAL ) + { + fval = readReal(ptr); + add_first_scalar = true; + } + else if( node_type == FileNode::STRING ) + { + sval = std::string(node); + add_first_scalar = true; + } + else + CV_Error_(Error::StsError, ("The node of type %d cannot be converted to collection", node_type)); + } + + ptr = reserveNodeSpace(node, 1 + (named ? 4 : 0) + 4 + 4); + *ptr++ = (uchar)(type | (named ? FileNode::NAMED : 0)); + // name has been copied automatically + if( named ) + ptr += 4; + // set raw_size(collection)==4, nelems(collection)==1 + writeInt(ptr, 4); + writeInt(ptr + 4, 0); + + if( add_first_scalar ) + addNode(node, std::string(), node_type, + node_type == FileNode::INT ? (const void*)&ival : + node_type == FileNode::REAL ? (const void*)&fval : + node_type == FileNode::STRING ? (const void*)sval.c_str() : 0, + -1); + } + + // a) allocates new FileNode (for that just set blockIdx to the last block and ofs to freeSpaceOfs) or + // b) reallocates just created new node (blockIdx and ofs must be taken from FileNode). + // If there is no enough space in the current block (it should be the last block added so far), + // the last block is shrunk so that it ends immediately before the reallocated node. Then, + // a new block of sufficient size is allocated and the FileNode is placed in the beginning of it. + // The case (a) can be used to allocate the very first node by setting blockIdx == ofs == 0. + // In the case (b) the existing tag and the name are copied automatically. + uchar* reserveNodeSpace(FileNode& node, size_t sz) + { + uchar *ptr = 0, *blockEnd = 0; + + if( !fs_data_ptrs.empty() ) + { + size_t blockIdx = node.blockIdx; + size_t ofs = node.ofs; + CV_Assert( blockIdx == fs_data_ptrs.size()-1 ); + CV_Assert( ofs <= fs_data_blksz[blockIdx] ); + //CV_Assert( freeSpaceOfs <= ofs + sz ); + + ptr = fs_data_ptrs[blockIdx] + ofs; + blockEnd = fs_data_ptrs[blockIdx] + fs_data_blksz[blockIdx]; + + if( ptr + sz <= blockEnd ) + { + freeSpaceOfs = ofs + sz; + return ptr; + } + + fs_data[blockIdx]->resize(ofs); + fs_data_blksz[blockIdx] = ofs; + } + + size_t blockSize = std::max((size_t)CV_FS_MAX_LEN*4 - 256, sz) + 256; + Ptr > pv = makePtr >(blockSize); + fs_data.push_back(pv); + uchar* new_ptr = &pv->at(0); + fs_data_ptrs.push_back(new_ptr); + fs_data_blksz.push_back(blockSize); + node.blockIdx = fs_data_ptrs.size()-1; + node.ofs = 0; + freeSpaceOfs = sz; + + if( ptr && ptr + 5 <= blockEnd ) + { + new_ptr[0] = ptr[0]; + if( ptr[0] & FileNode::NAMED ) + { + new_ptr[1] = ptr[1]; + new_ptr[2] = ptr[2]; + new_ptr[3] = ptr[3]; + new_ptr[4] = ptr[4]; + } + } + + return new_ptr; + } + + unsigned getStringOfs( const std::string& key ) + { + str_hash_t::iterator it = str_hash.find(key); + return it != str_hash.end() ? it->second : 0; + } + + FileNode addNode( FileNode& collection, const std::string& key, + int elem_type, const void* value, int len ) + { + FileStorage_API* fs = this; + bool noname = key.empty() || (fmt == FileStorage::FORMAT_XML && strcmp(key.c_str(), "_") == 0); + convertToCollection( noname ? FileNode::SEQ : FileNode::MAP, collection ); + + bool isseq = collection.empty() ? false : collection.isSeq(); + if( noname != isseq ) + CV_PARSE_ERROR_CPP( noname ? "Map element should have a name" : + "Sequence element should not have name (use <_>)" ); + unsigned strofs = 0; + if( !noname ) + { + strofs = getStringOfs(key); + if( !strofs ) + { + strofs = (unsigned)str_hash_data.size(); + size_t keysize = key.size() + 1; + str_hash_data.resize(strofs + keysize); + memcpy(&str_hash_data[0] + strofs, &key[0], keysize); + str_hash.insert(std::make_pair(key, strofs)); + } + } + + uchar* cp = collection.ptr(); + + size_t blockIdx = fs_data_ptrs.size() - 1; + size_t ofs = freeSpaceOfs; + FileNode node(fs_ext, blockIdx, ofs); + + size_t sz0 = 1 + (noname ? 0 : 4) + 8; + uchar* ptr = reserveNodeSpace(node, sz0); + + *ptr++ = (uchar)(elem_type | (noname ? 0 : FileNode::NAMED)); + if( elem_type == FileNode::NONE ) + freeSpaceOfs -= 8; + + if( !noname ) + { + writeInt(ptr, (int)strofs); + ptr += 4; + } + + if( elem_type == FileNode::SEQ || elem_type == FileNode::MAP ) + { + writeInt(ptr, 4); + writeInt(ptr, 0); + } + + if( value ) + node.setValue(elem_type, value, len); + + if( collection.isNamed() ) + cp += 4; + int nelems = readInt(cp + 5); + writeInt(cp + 5, nelems + 1); + + return node; + } + + void finalizeCollection( FileNode& collection ) + { + if( !collection.isSeq() && !collection.isMap() ) + return; + uchar* ptr0 = collection.ptr(), *ptr = ptr0 + 1; + if( *ptr0 & FileNode::NAMED ) + ptr += 4; + size_t blockIdx = collection.blockIdx; + size_t ofs = collection.ofs + (size_t)(ptr + 8 - ptr0); + size_t rawSize = 4; + unsigned sz = (unsigned)readInt(ptr + 4); + if( sz > 0 ) + { + size_t lastBlockIdx = fs_data_ptrs.size() - 1; + + for( ; blockIdx < lastBlockIdx; blockIdx++ ) + { + rawSize += fs_data_blksz[blockIdx] - ofs; + ofs = 0; + } + } + rawSize += freeSpaceOfs - ofs; + writeInt(ptr, (int)rawSize); + } + + void normalizeNodeOfs(size_t& blockIdx, size_t& ofs) + { + while( ofs >= fs_data_blksz[blockIdx] ) + { + if( blockIdx == fs_data_blksz.size() - 1 ) + { + CV_Assert( ofs == fs_data_blksz[blockIdx] ); + break; + } + ofs -= fs_data_blksz[blockIdx]; + blockIdx++; + } + } + + class Base64Decoder + { + public: + Base64Decoder() { ofs = 0; ptr = 0; indent = 0; totalchars = 0; eos = true; } + void init(Ptr& _parser, char* _ptr, int _indent) + { + parser = _parser; + ptr = _ptr; + indent = _indent; + encoded.clear(); + decoded.clear(); + ofs = 0; + totalchars = 0; + eos = false; + } + + bool readMore(int needed) + { + static const uchar base64tab[] = + { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, + 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + if( eos ) + return false; + + size_t sz = decoded.size(); + CV_Assert( ofs <= sz ); + sz -= ofs; + for( size_t i = 0; i < sz; i++ ) + decoded[i] = decoded[ofs + i]; + + decoded.resize(sz); + ofs = 0; + + CV_Assert( !parser.empty() && ptr ); + char *beg = 0, *end = 0; + bool ok = parser->getBase64Row(ptr, indent, beg, end); + ptr = end; + std::copy(beg, end, std::back_inserter(encoded)); + totalchars += end - beg; + + if( !ok || beg == end ) + { + // in the end of base64 sequence pad it with '=' characters so that + // its total length is multiple of + eos = true; + size_t tc = totalchars; + for( ; tc % 4 != 0; tc++ ) + encoded.push_back('='); + } + + int i = 0, j, n = (int)encoded.size(); + if( n > 0 ) + { + const uchar* tab = base64tab; + char* src = &encoded[0]; + + for( ; i <= n - 4; i += 4 ) + { + // dddddd cccccc bbbbbb aaaaaa => ddddddcc ccccbbbb bbaaaaaa + uchar d = tab[(int)(uchar)src[i]], c = tab[(int)(uchar)src[i+1]]; + uchar b = tab[(int)(uchar)src[i+2]], a = tab[(int)(uchar)src[i+3]]; + + decoded.push_back((uchar)((d << 2) | (c >> 4))); + decoded.push_back((uchar)((c << 4) | (b >> 2))); + decoded.push_back((uchar)((b << 6) | a)); + } + } + + if( i > 0 && encoded[i-1] == '=' ) + { + if( i > 1 && encoded[i-2] == '=' && !decoded.empty() ) + decoded.pop_back(); + if( !decoded.empty() ) + decoded.pop_back(); + } + + n -= i; + for( j = 0; j < n; j++ ) + encoded[j] = encoded[i + j]; + encoded.resize(n); + + return (int)decoded.size() >= needed; + } + + uchar getUInt8() + { + size_t sz = decoded.size(); + if( ofs >= sz && !readMore(1) ) + return (uchar)0; + return decoded[ofs++]; + } + + ushort getUInt16() + { + size_t sz = decoded.size(); + if( ofs + 2 > sz && !readMore(2) ) + return (ushort)0; + ushort val = (decoded[ofs] + (decoded[ofs + 1] << 8)); + ofs += 2; + return val; + } + + int getInt32() + { + size_t sz = decoded.size(); + if( ofs + 4 > sz && !readMore(4) ) + return 0; + int ival = readInt(&decoded[ofs]); + ofs += 4; + return ival; + } + + double getFloat64() + { + size_t sz = decoded.size(); + if( ofs + 8 > sz && !readMore(8) ) + return 0; + double fval = readReal(&decoded[ofs]); + ofs += 8; + return fval; + } + + bool endOfStream() const { return eos; } + char* getPtr() const { return ptr; } + protected: + + Ptr parser; + char* ptr; + int indent; + std::vector encoded; + std::vector decoded; + size_t ofs; + size_t totalchars; + bool eos; + }; + + char* parseBase64(char* ptr, int indent, FileNode& collection) + { + const int BASE64_HDR_SIZE = 24; + char dt[BASE64_HDR_SIZE+1] = {0}; + base64decoder.init(parser, ptr, indent); + + int i, k; + + for( i = 0; i < BASE64_HDR_SIZE; i++ ) + dt[i] = (char)base64decoder.getUInt8(); + for( i = 0; i < BASE64_HDR_SIZE; i++ ) + if( isspace(dt[i])) + break; + dt[i] = '\0'; + + CV_Assert( !base64decoder.endOfStream() ); + + int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2]; + int fmt_pair_count = fs::decodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); + int ival = 0; + double fval = 0; + + for(;;) + { + for( k = 0; k < fmt_pair_count; k++ ) + { + int elem_type = fmt_pairs[k*2+1]; + int count = fmt_pairs[k*2]; + + for( i = 0; i < count; i++ ) + { + int node_type = FileNode::INT; + switch( elem_type ) + { + case CV_8U: + ival = base64decoder.getUInt8(); + break; + case CV_8S: + ival = (char)base64decoder.getUInt8(); + break; + case CV_16U: + ival = base64decoder.getUInt16(); + break; + case CV_16S: + ival = (short)base64decoder.getUInt16(); + break; + case CV_32S: + ival = base64decoder.getInt32(); + break; + case CV_32F: + { + Cv32suf v; + v.i = base64decoder.getInt32(); + fval = v.f; + node_type = FileNode::REAL; + } + break; + case CV_64F: + fval = base64decoder.getFloat64(); + node_type = FileNode::REAL; + break; + case CV_16F: + fval = (float)float16_t::fromBits(base64decoder.getUInt16()); + node_type = FileNode::REAL; + break; + default: + CV_Error( Error::StsUnsupportedFormat, "Unsupported type" ); + } + + if( base64decoder.endOfStream() ) + break; + addNode(collection, std::string(), node_type, + node_type == FileNode::INT ? (void*)&ival : (void*)&fval, -1); + } + } + if( base64decoder.endOfStream() ) + break; + } + + finalizeCollection(collection); + return base64decoder.getPtr(); + } + + void parseError( const char* func_name, const std::string& err_msg, const char* source_file, int source_line ) + { + std::string msg = format("%s(%d): %s", filename.c_str(), lineno, err_msg.c_str()); + error(Error::StsParseError, func_name, msg.c_str(), source_file, source_line ); + } + + const uchar* getNodePtr(size_t blockIdx, size_t ofs) const + { + CV_Assert( blockIdx < fs_data_ptrs.size()); + CV_Assert( ofs < fs_data_blksz[blockIdx]); + + return fs_data_ptrs[blockIdx] + ofs; + } + + std::string getName( size_t nameofs ) const + { + CV_Assert( nameofs < str_hash_data.size() ); + return std::string(&str_hash_data[nameofs]); + } + + FileStorage* getFS() { return fs_ext; } + + FileStorage* fs_ext; + + std::string filename; + int flags; + bool empty_stream; + + FILE* file; + gzFile gzfile; + + bool is_opened; + bool dummy_eof; + bool write_mode; + bool mem_mode; + int fmt; + + State state; //!< current state of the FileStorage (used only for writing) + int space, wrap_margin; + std::deque write_stack; + std::vector buffer; + size_t bufofs; + + std::deque outbuf; + + Ptr emitter; + Ptr parser; + Base64Decoder base64decoder; + + std::vector roots; + std::vector > > fs_data; + std::vector fs_data_ptrs; + std::vector fs_data_blksz; + size_t freeSpaceOfs; + typedef std::unordered_map str_hash_t; + str_hash_t str_hash; + std::vector str_hash_data; + + std::vector strbufv; + char* strbuf; + size_t strbufsize; + size_t strbufpos; + int lineno; +}; + +FileStorage::FileStorage() +{ + p = makePtr(this); +} + +FileStorage::FileStorage(const String& filename, int flags, const String& encoding) +{ + p = makePtr(this); + bool ok = p->open(filename.c_str(), flags, encoding.c_str()); + if(ok) + state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP; +} + +void FileStorage::startWriteStruct(const String& name, int struct_flags, const String& typeName) +{ + p->startWriteStruct(name.c_str(), struct_flags, typeName.c_str()); + elname = String(); + if ((struct_flags & FileNode::TYPE_MASK) == FileNode::SEQ) + state = FileStorage::VALUE_EXPECTED; + else + state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP; +} + +void FileStorage::endWriteStruct() +{ + p->endWriteStruct(); + state = p->write_stack.empty() || FileNode::isMap(p->write_stack.back().flags) ? + FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP : + FileStorage::VALUE_EXPECTED; + elname = String(); +} + +FileStorage::~FileStorage() +{ + p.release(); +} + +bool FileStorage::open(const String& filename, int flags, const String& encoding) +{ + bool ok = p->open(filename.c_str(), flags, encoding.c_str()); + if(ok) + state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP; + return ok; +} + +bool FileStorage::isOpened() const { return p->is_opened; } + +void FileStorage::release() +{ + p->release(); +} + +FileNode FileStorage::root(int i) const +{ + if( p.empty() || p->roots.empty() || i < 0 || i >= (int)p->roots.size() ) + return FileNode(); + + return p->roots[i]; +} + +FileNode FileStorage::getFirstTopLevelNode() const +{ + FileNode r = root(); + FileNodeIterator it = r.begin(); + return it != r.end() ? *it : FileNode(); +} + +std::string FileStorage::getDefaultObjectName(const std::string& _filename) +{ + static const char* stubname = "unnamed"; + const char* filename = _filename.c_str(); + const char* ptr2 = filename + _filename.size(); + const char* ptr = ptr2 - 1; + cv::AutoBuffer name_buf(_filename.size()+1); + + while( ptr >= filename && *ptr != '\\' && *ptr != '/' && *ptr != ':' ) + { + if( *ptr == '.' && (!*ptr2 || strncmp(ptr2, ".gz", 3) == 0) ) + ptr2 = ptr; + ptr--; + } + ptr++; + if( ptr == ptr2 ) + CV_Error( CV_StsBadArg, "Invalid filename" ); + + char* name = name_buf.data(); + + // name must start with letter or '_' + if( !cv_isalpha(*ptr) && *ptr!= '_' ){ + *name++ = '_'; + } + + while( ptr < ptr2 ) + { + char c = *ptr++; + if( !cv_isalnum(c) && c != '-' && c != '_' ) + c = '_'; + *name++ = c; + } + *name = '\0'; + name = name_buf.data(); + if( strcmp( name, "_" ) == 0 ) + strcpy( name, stubname ); + return name; +} + + +int FileStorage::getFormat() const +{ + return p->fmt; +} + +FileNode FileStorage::operator [](const char* key) const +{ + if( p->roots.empty() ) + return FileNode(); + + return p->roots[0][key]; +} + +FileNode FileStorage::operator [](const std::string& key) const +{ + if( p->roots.empty() ) + return FileNode(); + + return p->roots[0][key]; +} + +String FileStorage::releaseAndGetString() +{ + String buf; + p->release(&buf); + return buf; +} + +void FileStorage::writeRaw( const String& fmt, const void* vec, size_t len ) +{ + p->writeRawData(fmt, (const uchar*)vec, len); +} + +void FileStorage::writeComment( const String& comment, bool eol_comment ) +{ + p->writeComment(comment.c_str(), eol_comment); +} + +void writeScalar( FileStorage& fs, int value ) +{ + fs.p->write(String(), value); +} + +void writeScalar( FileStorage& fs, float value ) +{ + fs.p->write(String(), (double)value); +} + +void writeScalar( FileStorage& fs, double value ) +{ + fs.p->write(String(), value); +} + +void writeScalar( FileStorage& fs, const String& value ) +{ + fs.p->write(String(), value); +} + +void write( FileStorage& fs, const String& name, int value ) +{ + fs.p->write(name, value); +} + +void write( FileStorage& fs, const String& name, float value ) +{ + fs.p->write(name, (double)value); +} + +void write( FileStorage& fs, const String& name, double value ) +{ + fs.p->write(name, value); +} + +void write( FileStorage& fs, const String& name, const String& value ) +{ + fs.p->write(name, value); +} + +void FileStorage::write(const String& name, int val) { p->write(name, val); } +void FileStorage::write(const String& name, double val) { p->write(name, val); } +void FileStorage::write(const String& name, const String& val) { p->write(name, val); } +void FileStorage::write(const String& name, const Mat& val) { cv::write(*this, name, val); } +void FileStorage::write(const String& name, const std::vector& val) { cv::write(*this, name, val); } + +FileStorage& operator << (FileStorage& fs, const String& str) +{ + enum { NAME_EXPECTED = FileStorage::NAME_EXPECTED, + VALUE_EXPECTED = FileStorage::VALUE_EXPECTED, + INSIDE_MAP = FileStorage::INSIDE_MAP }; + const char* _str = str.c_str(); + if( !fs.isOpened() || !_str ) + return fs; + Ptr& fs_impl = fs.p; + char c = *_str; + + if( c == '}' || c == ']' ) + { + if( fs_impl->write_stack.empty() ) + CV_Error_( CV_StsError, ("Extra closing '%c'", *_str) ); + + int struct_flags = fs_impl->write_stack.back().flags; + char expected_bracket = FileNode::isMap(struct_flags) ? '}' : ']'; + if( c != expected_bracket ) + CV_Error_( CV_StsError, ("The closing '%c' does not match the opening '%c'", c, expected_bracket)); + fs_impl->endWriteStruct(); + CV_Assert(!fs_impl->write_stack.empty()); + struct_flags = fs_impl->write_stack.back().flags; + fs.state = FileNode::isMap(struct_flags) ? INSIDE_MAP + NAME_EXPECTED : VALUE_EXPECTED; + fs.elname = String(); + } + else if( fs.state == NAME_EXPECTED + INSIDE_MAP ) + { + if (!cv_isalpha(c) && c != '_') + CV_Error_( CV_StsError, ("Incorrect element name %s; should start with a letter or '_'", _str) ); + fs.elname = str; + fs.state = VALUE_EXPECTED + INSIDE_MAP; + } + else if( (fs.state & 3) == VALUE_EXPECTED ) + { + if( c == '{' || c == '[' ) + { + int struct_flags = c == '{' ? FileNode::MAP : FileNode::SEQ; + fs.state = struct_flags == FileNode::MAP ? INSIDE_MAP + NAME_EXPECTED : VALUE_EXPECTED; + _str++; + if( *_str == ':' ) + { + _str++; + if( !*_str ) + struct_flags |= FileNode::FLOW; + } + fs_impl->startWriteStruct(!fs.elname.empty() ? fs.elname.c_str() : 0, struct_flags, *_str ? _str : 0 ); + fs.elname = String(); + } + else + { + write( fs, fs.elname, (c == '\\' && (_str[1] == '{' || _str[1] == '}' || + _str[1] == '[' || _str[1] == ']')) ? String(_str+1) : str ); + if( fs.state == INSIDE_MAP + VALUE_EXPECTED ) + fs.state = INSIDE_MAP + NAME_EXPECTED; + } + } + else + CV_Error( CV_StsError, "Invalid fs.state" ); + return fs; +} + + +FileNode::FileNode() +{ + fs = 0; + blockIdx = ofs = 0; +} + +FileNode::FileNode(const FileStorage* _fs, size_t _blockIdx, size_t _ofs) +{ + fs = _fs; + blockIdx = _blockIdx; + ofs = _ofs; +} + +FileNode::FileNode(const FileNode& node) +{ + fs = node.fs; + blockIdx = node.blockIdx; + ofs = node.ofs; +} + +FileNode FileNode::operator[](const std::string& nodename) const +{ + if(!fs) + return FileNode(); + + CV_Assert( isMap() ); + + unsigned key = fs->p->getStringOfs(nodename); + size_t i, sz = size(); + FileNodeIterator it = begin(); + + for( i = 0; i < sz; i++, ++it ) + { + FileNode n = *it; + const uchar* p = n.ptr(); + unsigned key2 = (unsigned)readInt(p + 1); + CV_Assert( key2 < fs->p->str_hash_data.size() ); + if( key == key2 ) + return n; + } + return FileNode(); +} + +FileNode FileNode::operator[](const char* nodename) const +{ + return this->operator[](std::string(nodename)); +} + +FileNode FileNode::operator[](int i) const +{ + if(!fs) + return FileNode(); + + CV_Assert( isSeq() ); + + int sz = (int)size(); + CV_Assert( 0 <= i && i < sz ); + + FileNodeIterator it = begin(); + it += i; + + return *it; +} + +int FileNode::type() const +{ + const uchar* p = ptr(); + if(!p) + return NONE; + return (*p & TYPE_MASK); +} + +bool FileNode::isMap(int flags) { return (flags & TYPE_MASK) == MAP; } +bool FileNode::isSeq(int flags) { return (flags & TYPE_MASK) == SEQ; } +bool FileNode::isCollection(int flags) { return isMap(flags) || isSeq(flags); } +bool FileNode::isFlow(int flags) { return (flags & FLOW) != 0; } +bool FileNode::isEmptyCollection(int flags) { return (flags & EMPTY) != 0; } + +bool FileNode::empty() const { return fs == 0; } +bool FileNode::isNone() const { return type() == NONE; } +bool FileNode::isSeq() const { return type() == SEQ; } +bool FileNode::isMap() const { return type() == MAP; } +bool FileNode::isInt() const { return type() == INT; } +bool FileNode::isReal() const { return type() == REAL; } +bool FileNode::isString() const { return type() == STRING; } +bool FileNode::isNamed() const +{ + const uchar* p = ptr(); + if(!p) + return false; + return (*p & NAMED) != 0; +} + +std::string FileNode::name() const +{ + const uchar* p = ptr(); + if(!p) + return std::string(); + size_t nameofs = p[1] | (p[2]<<8) | (p[3]<<16) | (p[4]<<24); + return fs->p->getName(nameofs); +} + +FileNode::operator int() const +{ + const uchar* p = ptr(); + if(!p) + return 0; + int tag = *p; + int type = (tag & TYPE_MASK); + p += (tag & NAMED) ? 5 : 1; + + if( type == INT ) + { + return readInt(p); + } + else if( type == REAL ) + { + return cvRound(readReal(p)); + } + else + return 0x7fffffff; +} + +FileNode::operator float() const +{ + const uchar* p = ptr(); + if(!p) + return 0.f; + int tag = *p; + int type = (tag & TYPE_MASK); + p += (tag & NAMED) ? 5 : 1; + + if( type == INT ) + { + return (float)readInt(p); + } + else if( type == REAL ) + { + return (float)readReal(p); + } + else + return FLT_MAX; +} + +FileNode::operator double() const +{ + const uchar* p = ptr(); + if(!p) + return 0.f; + int tag = *p; + int type = (tag & TYPE_MASK); + p += (tag & NAMED) ? 5 : 1; + + if( type == INT ) + { + return (double)readInt(p); + } + else if( type == REAL ) + { + return readReal(p); + } + else + return DBL_MAX; +} + +FileNode::operator std::string() const +{ + const uchar* p = ptr(); + if( !p || (*p & TYPE_MASK) != STRING ) + return std::string(); + p += (*p & NAMED) ? 5 : 1; + size_t sz = (size_t)(unsigned)readInt(p); + return std::string((const char*)(p + 4), sz - 1); +} + +double FileNode::real() const { return double(*this); } +std::string FileNode::string() const { return std::string(*this); } +Mat FileNode::mat() const { Mat value; read(*this, value, Mat()); return value; } + +FileNodeIterator FileNode::begin() const { return FileNodeIterator(*this, false); } +FileNodeIterator FileNode::end() const { return FileNodeIterator(*this, true); } + +void FileNode::readRaw( const std::string& fmt, void* vec, size_t len ) const +{ + FileNodeIterator it = begin(); + it.readRaw( fmt, vec, len ); +} + +size_t FileNode::size() const +{ + const uchar* p = ptr(); + if( !p ) + return 0; + int tag = *p; + int tp = tag & TYPE_MASK; + if( tp == MAP || tp == SEQ ) + { + if( tag & NAMED ) + p += 4; + return (size_t)(unsigned)readInt(p + 5); + } + return tp != NONE; +} + +size_t FileNode::rawSize() const +{ + const uchar* p0 = ptr(), *p = p0; + if( !p ) + return 0; + int tag = *p++; + int tp = tag & TYPE_MASK; + if( tag & NAMED ) + p += 4; + size_t sz0 = (size_t)(p - p0); + if( tp == INT ) + return sz0 + 4; + if( tp == REAL ) + return sz0 + 8; + if( tp == NONE ) + return sz0; + CV_Assert( tp == STRING || tp == SEQ || tp == MAP ); + return sz0 + 4 + readInt(p); +} + +uchar* FileNode::ptr() +{ + return !fs ? 0 : (uchar*)fs->p->getNodePtr(blockIdx, ofs); +} + +const uchar* FileNode::ptr() const +{ + return !fs ? 0 : fs->p->getNodePtr(blockIdx, ofs); +} + +void FileNode::setValue( int type, const void* value, int len ) +{ + uchar *p = ptr(); + CV_Assert(p != 0); + + int tag = *p; + int current_type = tag & TYPE_MASK; + CV_Assert( current_type == NONE || current_type == type ); + + int sz = 1; + + if( tag & NAMED ) + sz += 4; + + if( type == INT ) + sz += 4; + else if( type == REAL ) + sz += 8; + else if( type == STRING ) + { + if( len < 0 ) + len = (int)strlen((const char*)value); + sz += 4 + len + 1; // besides the string content, + // take the size (4 bytes) and the final '\0' into account + } + else + CV_Error(Error::StsNotImplemented, "Only scalar types can be dynamically assigned to a file node"); + + p = fs->p->reserveNodeSpace(*this, sz); + *p++ = (uchar)(type | (tag & NAMED)); + if( tag & NAMED ) + p += 4; + + if( type == INT ) + { + int ival = *(const int*)value; + writeInt(p, ival); + } + else if( type == REAL ) + { + double dbval = *(const double*)value; + writeReal(p, dbval); + } + else if( type == STRING ) + { + const char* str = (const char*)value; + writeInt(p, len + 1); + memcpy(p + 4, str, len); + p[4 + len] = (uchar)'\0'; + } +} + +FileNodeIterator::FileNodeIterator() +{ + fs = 0; + blockIdx = 0; + ofs = 0; + blockSize = 0; + nodeNElems = 0; + idx = 0; +} + +FileNodeIterator::FileNodeIterator( const FileNode& node, bool seekEnd ) +{ + fs = node.fs; + idx = 0; + if( !fs ) + blockIdx = ofs = blockSize = nodeNElems = 0; + else + { + blockIdx = node.blockIdx; + ofs = node.ofs; + + bool collection = node.isSeq() || node.isMap(); + if( node.isNone() ) + { + nodeNElems = 0; + } + else if( !collection ) + { + nodeNElems = 1; + if( seekEnd ) + { + idx = 1; + ofs += node.rawSize(); + } + } + else + { + nodeNElems = node.size(); + const uchar* p0 = node.ptr(), *p = p0 + 1; + if(*p0 & FileNode::NAMED ) + p += 4; + if( !seekEnd ) + ofs += (p - p0) + 8; + else + { + size_t rawsz = (size_t)(unsigned)readInt(p); + ofs += (p - p0) + 4 + rawsz; + idx = nodeNElems; + } + } + fs->p->normalizeNodeOfs(blockIdx, ofs); + blockSize = fs->p->fs_data_blksz[blockIdx]; + } +} + +FileNodeIterator::FileNodeIterator(const FileNodeIterator& it) +{ + fs = it.fs; + blockIdx = it.blockIdx; + ofs = it.ofs; + blockSize = it.blockSize; + nodeNElems = it.nodeNElems; + idx = it.idx; +} + +FileNode FileNodeIterator::operator *() const +{ + return FileNode(idx < nodeNElems ? fs : 0, blockIdx, ofs); +} + +FileNode FileNodeIterator::operator ->() const +{ + return FileNode(idx < nodeNElems ? fs : 0, blockIdx, ofs); +} + +FileNodeIterator& FileNodeIterator::operator ++ () +{ + if( idx == nodeNElems || !fs ) + return *this; + idx++; + FileNode n(fs, blockIdx, ofs); + ofs += n.rawSize(); + if( ofs >= blockSize ) + { + fs->p->normalizeNodeOfs(blockIdx, ofs); + blockSize = fs->p->fs_data_blksz[blockIdx]; + } + return *this; +} + +FileNodeIterator FileNodeIterator::operator ++ (int) +{ + FileNodeIterator it = *this; + ++(*this); + return it; +} + +FileNodeIterator& FileNodeIterator::operator += (int _ofs) +{ + CV_Assert( _ofs >= 0 ); + for( ; _ofs > 0; _ofs-- ) + this->operator ++(); + return *this; +} + +FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, void* _data0, size_t maxsz) +{ + if( fs && idx < nodeNElems ) + { + uchar* data0 = (uchar*)_data0; + int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2]; + int fmt_pair_count = fs::decodeFormat( fmt.c_str(), fmt_pairs, CV_FS_MAX_FMT_PAIRS ); + size_t esz = fs::calcStructSize( fmt.c_str(), 0 ); + + CV_Assert( maxsz % esz == 0 ); + maxsz /= esz; + + for( ; maxsz > 0; maxsz--, data0 += esz ) + { + size_t offset = 0; + for( int k = 0; k < fmt_pair_count; k++ ) + { + int elem_type = fmt_pairs[k*2+1]; + int elem_size = CV_ELEM_SIZE(elem_type); + + int count = fmt_pairs[k*2]; + offset = alignSize( offset, elem_size ); + uchar* data = data0 + offset; + + for( int i = 0; i < count; i++, ++(*this) ) + { + FileNode node = *(*this); + if( node.isInt() ) + { + int ival = (int)node; + switch( elem_type ) + { + case CV_8U: + *(uchar*)data = saturate_cast(ival); + data++; + break; + case CV_8S: + *(char*)data = saturate_cast(ival); + data++; + break; + case CV_16U: + *(ushort*)data = saturate_cast(ival); + data += sizeof(ushort); + break; + case CV_16S: + *(short*)data = saturate_cast(ival); + data += sizeof(short); + break; + case CV_32S: + *(int*)data = ival; + data += sizeof(int); + break; + case CV_32F: + *(float*)data = (float)ival; + data += sizeof(float); + break; + case CV_64F: + *(double*)data = (double)ival; + data += sizeof(double); + break; + case CV_16F: + *(float16_t*)data = float16_t((float)ival); + data += sizeof(float16_t); + break; + default: + CV_Error( Error::StsUnsupportedFormat, "Unsupported type" ); + } + } + else if( node.isReal() ) + { + double fval = (double)node; + + switch( elem_type ) + { + case CV_8U: + *(uchar*)data = saturate_cast(fval); + data++; + break; + case CV_8S: + *(char*)data = saturate_cast(fval); + data++; + break; + case CV_16U: + *(ushort*)data = saturate_cast(fval); + data += sizeof(ushort); + break; + case CV_16S: + *(short*)data = saturate_cast(fval); + data += sizeof(short); + break; + case CV_32S: + *(int*)data = saturate_cast(fval); + data += sizeof(int); + break; + case CV_32F: + *(float*)data = (float)fval; + data += sizeof(float); + break; + case CV_64F: + *(double*)data = fval; + data += sizeof(double); + break; + case CV_16F: + *(float16_t*)data = float16_t((float)fval); + data += sizeof(float16_t); + break; + default: + CV_Error( Error::StsUnsupportedFormat, "Unsupported type" ); + } + } + else + CV_Error( Error::StsError, "readRawData can only be used to read plain sequences of numbers" ); + } + offset = (int)(data - data0); + } + } + } + + return *this; +} + +bool FileNodeIterator::equalTo(const FileNodeIterator& it) const +{ + return fs == it.fs && blockIdx == it.blockIdx && ofs == it.ofs && + idx == it.idx && nodeNElems == it.nodeNElems; +} + +size_t FileNodeIterator::remaining() const +{ + return nodeNElems - idx; +} + +bool operator == ( const FileNodeIterator& it1, const FileNodeIterator& it2 ) +{ + return it1.equalTo(it2); +} + +bool operator != ( const FileNodeIterator& it1, const FileNodeIterator& it2 ) +{ + return !it1.equalTo(it2); +} + +void read(const FileNode& node, int& val, int default_val) +{ + val = default_val; + if( !node.empty() ) + { + val = (int)node; + } +} + +void read(const FileNode& node, double& val, double default_val) +{ + val = default_val; + if( !node.empty() ) + { + val = (double)node; + } +} + +void read(const FileNode& node, float& val, float default_val) +{ + val = default_val; + if( !node.empty() ) + { + val = (float)node; + } +} + +void read(const FileNode& node, std::string& val, const std::string& default_val) +{ + val = default_val; + if( !node.empty() ) + { + val = (std::string)node; + } +} + +FileStorage_API::~FileStorage_API() {} + +namespace internal +{ + +WriteStructContext::WriteStructContext(FileStorage& _fs, const std::string& name, + int flags, const std::string& typeName) +{ + fs = &_fs; + fs->startWriteStruct(name, flags, typeName); +} + +WriteStructContext::~WriteStructContext() +{ + fs->endWriteStruct(); +} + +} + } diff --git a/modules/core/src/persistence.hpp b/modules/core/src/persistence.hpp index aa0acab..38ad08b 100644 --- a/modules/core/src/persistence.hpp +++ b/modules/core/src/persistence.hpp @@ -31,73 +31,10 @@ static const size_t PARSER_BASE64_BUFFER_SIZE = 1024U * 1024U / 8U; namespace base64 { -namespace fs { -enum State +enum { - Uncertain, - NotUse, - InUse, -}; -} // fs:: - -static const size_t HEADER_SIZE = 24U; -static const size_t ENCODED_HEADER_SIZE = 32U; - -size_t base64_encode(uint8_t const * src, uint8_t * dst, size_t off, size_t cnt); -size_t base64_encode( char const * src, char * dst, size_t off = 0U, size_t cnt = 0U); -size_t base64_decode(uint8_t const * src, uint8_t * dst, size_t off, size_t cnt); -size_t base64_decode( char const * src, char * dst, size_t off = 0U, size_t cnt = 0U); -bool base64_valid (uint8_t const * src, size_t off, size_t cnt); -bool base64_valid ( char const * src, size_t off = 0U, size_t cnt = 0U); -size_t base64_encode_buffer_size(size_t cnt, bool is_end_with_zero = true); -size_t base64_decode_buffer_size(size_t cnt, bool is_end_with_zero = true); -size_t base64_decode_buffer_size(size_t cnt, char const * src, bool is_end_with_zero = true); -size_t base64_decode_buffer_size(size_t cnt, uchar const * src, bool is_end_with_zero = true); -std::string make_base64_header(const char * dt); -bool read_base64_header(std::vector const & header, std::string & dt); -void make_seq(void * binary_data, int elem_cnt, const char * dt, CvSeq & seq); -void cvWriteRawDataBase64(::CvFileStorage* fs, const void* _data, int len, const char* dt); - -class Base64ContextEmitter; - -class Base64Writer -{ -public: - Base64Writer(::CvFileStorage * fs); - ~Base64Writer(); - void write(const void* _data, size_t len, const char* dt); - template void write(_to_binary_convertor_t & convertor, const char* dt); - -private: - void check_dt(const char* dt); - -private: - // disable copy and assignment - Base64Writer(const Base64Writer &); - Base64Writer & operator=(const Base64Writer &); - -private: - - Base64ContextEmitter * emitter; - std::string data_type_string; -}; - -class Base64ContextParser -{ -public: - explicit Base64ContextParser(uchar * buffer, size_t size); - ~Base64ContextParser(); - Base64ContextParser & read(const uchar * beg, const uchar * end); - bool flush(); -private: - static const size_t BUFFER_LEN = 120U; - uchar * dst_cur; - uchar * dst_end; - std::vector base64_buffer; - uchar * src_beg; - uchar * src_cur; - uchar * src_end; - std::vector binary_buffer; + HEADER_SIZE = 24, + ENCODED_HEADER_SIZE = 32 }; } // base64:: @@ -107,108 +44,6 @@ private: #define CV_FS_MAX_LEN 4096 #define CV_FS_MAX_FMT_PAIRS 128 -#define CV_FILE_STORAGE ('Y' + ('A' << 8) + ('M' << 16) + ('L' << 24)) - -#define CV_IS_FILE_STORAGE(fs) ((fs) != 0 && (fs)->flags == CV_FILE_STORAGE) - -#define CV_CHECK_FILE_STORAGE(fs) \ -{ \ - if( !CV_IS_FILE_STORAGE(fs) ) \ - CV_Error( (fs) ? CV_StsBadArg : CV_StsNullPtr, \ - "Invalid pointer to file storage" ); \ -} - -#define CV_CHECK_OUTPUT_FILE_STORAGE(fs) \ -{ \ - CV_CHECK_FILE_STORAGE(fs); \ - if( !fs->write_mode ) \ - CV_Error( CV_StsError, "The file storage is opened for reading" ); \ -} - -#define CV_PARSE_ERROR( errmsg ) \ - icvParseError( fs, CV_Func, (errmsg), __FILE__, __LINE__ ) - -typedef struct CvGenericHash -{ - CV_SET_FIELDS() - int tab_size; - void** table; -} -CvGenericHash; -typedef CvGenericHash CvStringHash; - -//typedef void (*CvParse)( struct CvFileStorage* fs ); -typedef void (*CvStartWriteStruct)( struct CvFileStorage* fs, const char* key, - int struct_flags, const char* type_name ); -typedef void (*CvEndWriteStruct)( struct CvFileStorage* fs ); -typedef void (*CvWriteInt)( struct CvFileStorage* fs, const char* key, int value ); -typedef void (*CvWriteReal)( struct CvFileStorage* fs, const char* key, double value ); -typedef void (*CvWriteString)( struct CvFileStorage* fs, const char* key, - const char* value, int quote ); -typedef void (*CvWriteComment)( struct CvFileStorage* fs, const char* comment, int eol_comment ); -typedef void (*CvStartNextStream)( struct CvFileStorage* fs ); - -typedef struct CvFileStorage -{ - int flags; - int fmt; - int write_mode; - int is_first; - CvMemStorage* memstorage; - CvMemStorage* dststorage; - CvMemStorage* strstorage; - CvStringHash* str_hash; - CvSeq* roots; - CvSeq* write_stack; - int struct_indent; - int struct_flags; - CvString struct_tag; - int space; - char* filename; - FILE* file; - gzFile gzfile; - char* buffer; - char* buffer_start; - char* buffer_end; - int wrap_margin; - int lineno; - int dummy_eof; - const char* errmsg; - char errmsgbuf[128]; - - CvStartWriteStruct start_write_struct; - CvEndWriteStruct end_write_struct; - CvWriteInt write_int; - CvWriteReal write_real; - CvWriteString write_string; - CvWriteComment write_comment; - CvStartNextStream start_next_stream; - - const char* strbuf; - size_t strbufsize, strbufpos; - std::deque* outbuf; - - base64::Base64Writer * base64_writer; - bool is_default_using_base64; - base64::fs::State state_of_writing_base64; /**< used in WriteRawData only */ - - bool is_write_struct_delayed; - char* delayed_struct_key; - int delayed_struct_flags; - char* delayed_type_name; - - bool is_opened; -} -CvFileStorage; - -typedef struct CvFileMapNode -{ - CvFileNode value; - const CvStringHashNode* key; - struct CvFileMapNode* next; -} -CvFileMapNode; - /****************************************************************************************\ * Common macros and type definitions * \****************************************************************************************/ @@ -245,83 +80,113 @@ inline char* cv_skip_BOM(char* ptr) return ptr; } -char* icv_itoa( int _val, char* buffer, int /*radix*/ ); -double icv_strtod( CvFileStorage* fs, char* ptr, char** endptr ); -char* icvFloatToString( char* buf, float value ); -char* icvDoubleToString( char* buf, double value ); - -void icvClose( CvFileStorage* fs, cv::String* out ); -void icvCloseFile( CvFileStorage* fs ); -void icvPuts( CvFileStorage* fs, const char* str ); -char* icvGets( CvFileStorage* fs, char* str, int maxCount ); -int icvEof( CvFileStorage* fs ); -void icvRewind( CvFileStorage* fs ); -char* icvFSFlush( CvFileStorage* fs ); -void icvFSCreateCollection( CvFileStorage* fs, int tag, CvFileNode* collection ); -char* icvFSResizeWriteBuffer( CvFileStorage* fs, char* ptr, int len ); -int icvCalcStructSize( const char* dt, int initial_size ); -int icvCalcElemSize( const char* dt, int initial_size ); -void CV_NORETURN icvParseError( CvFileStorage* fs, const char* func_name, const char* err_msg, const char* source_file, int source_line ); -char* icvEncodeFormat( int elem_type, char* dt ); -int icvDecodeFormat( const char* dt, int* fmt_pairs, int max_len ); -int icvDecodeSimpleFormat( const char* dt ); -void icvWriteFileNode( CvFileStorage* fs, const char* name, const CvFileNode* node ); -void icvWriteCollection( CvFileStorage* fs, const CvFileNode* node ); -void switch_to_Base64_state( CvFileStorage* fs, base64::fs::State state ); -void make_write_struct_delayed( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name ); -void check_if_write_struct_is_delayed( CvFileStorage* fs, bool change_type_to_base64 = false ); -CvGenericHash* cvCreateMap( int flags, int header_size, int elem_size, CvMemStorage* storage, int start_tab_size ); - -// -// XML -// -void icvXMLParse( CvFileStorage* fs ); -void icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name CV_DEFAULT(0)); -void icvXMLEndWriteStruct( CvFileStorage* fs ); -void icvXMLStartNextStream( CvFileStorage* fs ); -void icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len ); -void icvXMLWriteInt( CvFileStorage* fs, const char* key, int value ); -void icvXMLWriteReal( CvFileStorage* fs, const char* key, double value ); -void icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote ); -void icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ); - -typedef struct CvXMLStackRecord +namespace cv { - CvMemStoragePos pos; - CvString struct_tag; - int struct_indent; - int struct_flags; +namespace fs +{ +int strcasecmp(const char* str1, const char* str2); +char* itoa( int _val, char* buffer, int /*radix*/ ); +char* floatToString( char* buf, float value, bool halfprecision, bool explicitZero ); +char* doubleToString( char* buf, double value, bool explicitZero ); + +int calcStructSize( const char* dt, int initial_size ); +int calcElemSize( const char* dt, int initial_size ); +char* encodeFormat( int elem_type, char* dt ); +int decodeFormat( const char* dt, int* fmt_pairs, int max_len ); +int decodeSimpleFormat( const char* dt ); +} + +#define CV_PARSE_ERROR_CPP( errmsg ) \ + fs->parseError( CV_Func, (errmsg), __FILE__, __LINE__ ) + +#define CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP() \ + if((ptr)[0] == 0 && (ptr) == fs->bufferEnd() - 1) CV_PARSE_ERROR_CPP("OpenCV persistence doesn't support very long lines") + +class FileStorageParser; +class FileStorageEmitter; + +struct FStructData +{ + FStructData() { indent = flags = 0; } + FStructData( const std::string& _struct_tag, + int _struct_flags, int _struct_indent ) + { + tag = _struct_tag; + flags = _struct_flags; + indent = _struct_indent; + } + + std::string tag; + int flags; + int indent; +}; + +class FileStorage_API +{ +public: + virtual ~FileStorage_API(); + virtual FileStorage* getFS() = 0; + + virtual void puts( const char* str ) = 0; + virtual char* gets() = 0; + virtual bool eof() = 0; + virtual void setEof() = 0; + virtual void closeFile() = 0; + virtual void rewind() = 0; + virtual char* resizeWriteBuffer( char* ptr, int len ) = 0; + virtual char* bufferPtr() const = 0; + virtual char* bufferStart() const = 0; + virtual char* bufferEnd() const = 0; + virtual void setBufferPtr(char* ptr) = 0; + virtual char* flush() = 0; + virtual void setNonEmpty() = 0; + virtual int wrapMargin() const = 0; + + virtual FStructData& getCurrentStruct() = 0; + + virtual void convertToCollection( int type, FileNode& node ) = 0; + virtual FileNode addNode( FileNode& collection, const std::string& key, + int type, const void* value=0, int len=-1 ) = 0; + virtual void finalizeCollection( FileNode& collection ) = 0; + virtual double strtod(char* ptr, char** endptr) = 0; + + virtual char* parseBase64(char* ptr, int indent, FileNode& collection) = 0; + virtual void parseError(const char* funcname, const std::string& msg, + const char* filename, int lineno) = 0; +}; + +class FileStorageEmitter +{ +public: + virtual ~FileStorageEmitter() {} + + virtual FStructData startWriteStruct( const FStructData& parent, const char* key, + int struct_flags, const char* type_name=0 ) = 0; + virtual void endWriteStruct(const FStructData& current_struct) = 0; + virtual void write(const char* key, int value) = 0; + virtual void write(const char* key, double value) = 0; + virtual void write(const char* key, const char* value, bool quote) = 0; + virtual void writeScalar(const char* key, const char* value) = 0; + virtual void writeComment(const char* comment, bool eol_comment) = 0; + virtual void startNextStream() = 0; +}; + +class FileStorageParser +{ +public: + virtual ~FileStorageParser() {} + virtual bool parse(char* ptr) = 0; + virtual bool getBase64Row(char* ptr, int indent, char* &beg, char* &end) = 0; +}; + +Ptr createXMLEmitter(FileStorage_API* fs); +Ptr createYAMLEmitter(FileStorage_API* fs); +Ptr createJSONEmitter(FileStorage_API* fs); + +Ptr createXMLParser(FileStorage_API* fs); +Ptr createYAMLParser(FileStorage_API* fs); +Ptr createJSONParser(FileStorage_API* fs); + } -CvXMLStackRecord; - -// -// YML -// -void icvYMLParse( CvFileStorage* fs ); -void icvYMLWrite( CvFileStorage* fs, const char* key, const char* data ); -void icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name CV_DEFAULT(0)); -void icvYMLEndWriteStruct( CvFileStorage* fs ); -void icvYMLStartNextStream( CvFileStorage* fs ); -void icvYMLWriteInt( CvFileStorage* fs, const char* key, int value ); -void icvYMLWriteReal( CvFileStorage* fs, const char* key, double value ); -void icvYMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote CV_DEFAULT(0)); -void icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ); - -// -// JSON -// -void icvJSONParse( CvFileStorage* fs ); -void icvJSONWrite( CvFileStorage* fs, const char* key, const char* data ); -void icvJSONStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name CV_DEFAULT(0)); -void icvJSONEndWriteStruct( CvFileStorage* fs ); -void icvJSONStartNextStream( CvFileStorage* fs ); -void icvJSONWriteInt( CvFileStorage* fs, const char* key, int value ); -void icvJSONWriteReal( CvFileStorage* fs, const char* key, double value ); -void icvJSONWriteString( CvFileStorage* fs, const char* key, const char* str, int quote CV_DEFAULT(0)); -void icvJSONWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ); - -// Adding icvGets is not enough - we need to merge buffer contents (see #11061) -#define CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG() \ - CV_Assert((ptr[0] != 0 || ptr != fs->buffer_end - 1) && "OpenCV persistence doesn't support very long lines") #endif // SRC_PERSISTENCE_HPP diff --git a/modules/core/src/persistence_base64.cpp b/modules/core/src/persistence_base64.cpp deleted file mode 100644 index 8d6e6cc..0000000 --- a/modules/core/src/persistence_base64.cpp +++ /dev/null @@ -1,926 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html - - -#include "precomp.hpp" -#include "persistence.hpp" - -namespace base64 { - -typedef uchar uint8_t; - -#if CHAR_BIT != 8 -#error "`char` should be 8 bit." -#endif - -uint8_t const base64_mapping[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; - -uint8_t const base64_padding = '='; - -uint8_t const base64_demapping[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, - 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, - 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, - 49, 50, 51, 0, 0, 0, 0, -}; - -/* `base64_demapping` above is generated in this way: - * ````````````````````````````````````````````````````````````````````` - * std::string mapping((const char *)base64_mapping); - * for (auto ch = 0; ch < 127; ch++) { - * auto i = mapping.find(ch); - * printf("%3u, ", (i != std::string::npos ? i : 0)); - * } - * putchar('\n'); - * ````````````````````````````````````````````````````````````````````` - */ - -size_t base64_encode(uint8_t const * src, uint8_t * dst, size_t off, size_t cnt) -{ - if (!src || !dst || !cnt) - return 0; - - /* initialize beginning and end */ - uint8_t * dst_beg = dst; - uint8_t * dst_cur = dst_beg; - - uint8_t const * src_beg = src + off; - uint8_t const * src_cur = src_beg; - uint8_t const * src_end = src_cur + cnt / 3U * 3U; - - /* integer multiples part */ - while (src_cur < src_end) { - uint8_t _2 = *src_cur++; - uint8_t _1 = *src_cur++; - uint8_t _0 = *src_cur++; - *dst_cur++ = base64_mapping[ _2 >> 2U]; - *dst_cur++ = base64_mapping[(_1 & 0xF0U) >> 4U | (_2 & 0x03U) << 4U]; - *dst_cur++ = base64_mapping[(_0 & 0xC0U) >> 6U | (_1 & 0x0FU) << 2U]; - *dst_cur++ = base64_mapping[ _0 & 0x3FU]; - } - - /* remainder part */ - size_t rst = src_beg + cnt - src_cur; - if (rst == 1U) { - uint8_t _2 = *src_cur++; - *dst_cur++ = base64_mapping[ _2 >> 2U]; - *dst_cur++ = base64_mapping[(_2 & 0x03U) << 4U]; - } else if (rst == 2U) { - uint8_t _2 = *src_cur++; - uint8_t _1 = *src_cur++; - *dst_cur++ = base64_mapping[ _2 >> 2U]; - *dst_cur++ = base64_mapping[(_2 & 0x03U) << 4U | (_1 & 0xF0U) >> 4U]; - *dst_cur++ = base64_mapping[(_1 & 0x0FU) << 2U]; - } - - /* padding */ - switch (rst) - { - case 1U: *dst_cur++ = base64_padding; - /* fallthrough */ - case 2U: *dst_cur++ = base64_padding; - /* fallthrough */ - default: *dst_cur = 0; - break; - } - - return static_cast(dst_cur - dst_beg); -} - -size_t base64_encode(char const * src, char * dst, size_t off, size_t cnt) -{ - if (cnt == 0U) - cnt = std::strlen(src); - - return base64_encode - ( - reinterpret_cast(src), - reinterpret_cast(dst), - off, - cnt - ); -} - -size_t base64_decode(uint8_t const * src, uint8_t * dst, size_t off, size_t cnt) -{ - /* check parameters */ - if (!src || !dst || !cnt) - return 0U; - if (cnt & 0x3U) - return 0U; - - /* initialize beginning and end */ - uint8_t * dst_beg = dst; - uint8_t * dst_cur = dst_beg; - - uint8_t const * src_beg = src + off; - uint8_t const * src_cur = src_beg; - uint8_t const * src_end = src_cur + cnt; - - /* start decoding */ - while (src_cur < src_end) { - uint8_t d50 = base64_demapping[*src_cur++]; - uint8_t c50 = base64_demapping[*src_cur++]; - uint8_t b50 = base64_demapping[*src_cur++]; - uint8_t a50 = base64_demapping[*src_cur++]; - - uint8_t b10 = b50 & 0x03U; - uint8_t b52 = b50 & 0x3CU; - uint8_t c30 = c50 & 0x0FU; - uint8_t c54 = c50 & 0x30U; - - *dst_cur++ = (d50 << 2U) | (c54 >> 4U); - *dst_cur++ = (c30 << 4U) | (b52 >> 2U); - *dst_cur++ = (b10 << 6U) | (a50 >> 0U); - } - - *dst_cur = 0; - return size_t(dst_cur - dst_beg); -} - -size_t base64_decode(char const * src, char * dst, size_t off, size_t cnt) -{ - if (cnt == 0U) - cnt = std::strlen(src); - - return base64_decode - ( - reinterpret_cast(src), - reinterpret_cast(dst), - off, - cnt - ); -} - -bool base64_valid(uint8_t const * src, size_t off, size_t cnt) -{ - /* check parameters */ - if (src == 0 || src + off == 0) - return false; - if (cnt == 0U) - cnt = std::strlen(reinterpret_cast(src)); - if (cnt == 0U) - return false; - if (cnt & 0x3U) - return false; - - /* initialize beginning and end */ - uint8_t const * beg = src + off; - uint8_t const * end = beg + cnt; - - /* skip padding */ - if (*(end - 1U) == base64_padding) { - end--; - if (*(end - 1U) == base64_padding) - end--; - } - - /* find illegal characters */ - for (uint8_t const * iter = beg; iter < end; iter++) - if (*iter > 126U || (!base64_demapping[(uint8_t)*iter] && *iter != base64_mapping[0])) - return false; - - return true; -} - -bool base64_valid(char const * src, size_t off, size_t cnt) -{ - if (cnt == 0U) - cnt = std::strlen(src); - - return base64_valid(reinterpret_cast(src), off, cnt); -} - -size_t base64_encode_buffer_size(size_t cnt, bool is_end_with_zero) -{ - size_t additional = static_cast(is_end_with_zero == true); - return (cnt + 2U) / 3U * 4U + additional; -} - -size_t base64_decode_buffer_size(size_t cnt, bool is_end_with_zero) -{ - size_t additional = static_cast(is_end_with_zero == true); - return cnt / 4U * 3U + additional; -} - -size_t base64_decode_buffer_size(size_t cnt, char const * src, bool is_end_with_zero) -{ - return base64_decode_buffer_size(cnt, reinterpret_cast(src), is_end_with_zero); -} - -size_t base64_decode_buffer_size(size_t cnt, uchar const * src, bool is_end_with_zero) -{ - size_t padding_cnt = 0U; - for (uchar const * ptr = src + cnt - 1U; *ptr == base64_padding; ptr--) - padding_cnt ++; - return base64_decode_buffer_size(cnt, is_end_with_zero) - padding_cnt; -} - -/**************************************************************************** - * to_binary && binary_to - ***************************************************************************/ - -template inline size_t -to_binary(_uint_t val, uchar * cur) -{ - size_t delta = CHAR_BIT; - size_t cnt = sizeof(_uint_t); - while (cnt --> static_cast(0U)) { - *cur++ = static_cast(val); - val >>= delta; - } - return sizeof(_uint_t); -} - -template<> inline size_t to_binary(double val, uchar * cur) -{ - Cv64suf bit64; - bit64.f = val; - return to_binary(bit64.u, cur); -} - -template<> inline size_t to_binary(float val, uchar * cur) -{ - Cv32suf bit32; - bit32.f = val; - return to_binary(bit32.u, cur); -} - -template inline size_t -to_binary(uchar const * val, uchar * cur) -{ - return to_binary<_primitive_t>(*reinterpret_cast<_primitive_t const *>(val), cur); -} - - -template inline size_t -binary_to(uchar const * cur, _uint_t & val) -{ - val = static_cast<_uint_t>(0); - for (size_t i = static_cast(0U); i < sizeof(_uint_t); i++) - val |= (static_cast<_uint_t>(*cur++) << (i * CHAR_BIT)); - return sizeof(_uint_t); -} - -template<> inline size_t binary_to(uchar const * cur, double & val) -{ - Cv64suf bit64; - binary_to(cur, bit64.u); - val = bit64.f; - return sizeof(val); -} - -template<> inline size_t binary_to(uchar const * cur, float & val) -{ - Cv32suf bit32; - binary_to(cur, bit32.u); - val = bit32.f; - return sizeof(val); -} - -template inline size_t -binary_to(uchar const * cur, uchar * val) -{ - return binary_to<_primitive_t>(cur, *reinterpret_cast<_primitive_t *>(val)); -} - -/**************************************************************************** - * others - ***************************************************************************/ - -std::string make_base64_header(const char * dt) -{ - std::ostringstream oss; - oss << dt << ' '; - std::string buffer(oss.str()); - CV_Assert(buffer.size() < HEADER_SIZE); - - buffer.reserve(HEADER_SIZE); - while (buffer.size() < HEADER_SIZE) - buffer += ' '; - - return buffer; -} - -bool read_base64_header(std::vector const & header, std::string & dt) -{ - std::istringstream iss(header.data()); - return !!(iss >> dt);//the "std::basic_ios::operator bool" differs between C++98 and C++11. The "double not" syntax is portable and covers both cases with equivalent meaning -} - -/**************************************************************************** - * Parser - ***************************************************************************/ - -Base64ContextParser::Base64ContextParser(uchar * buffer, size_t size) - : dst_cur(buffer) - , dst_end(buffer + size) - , base64_buffer(BUFFER_LEN) - , src_beg(0) - , src_cur(0) - , src_end(0) - , binary_buffer(base64_encode_buffer_size(BUFFER_LEN)) -{ - src_beg = binary_buffer.data(); - src_cur = src_beg; - src_end = src_beg + BUFFER_LEN; -} - -Base64ContextParser::~Base64ContextParser() -{ - /* encode the rest binary data to base64 buffer */ - if (src_cur != src_beg) - flush(); -} - -Base64ContextParser & Base64ContextParser::read(const uchar * beg, const uchar * end) -{ - if (beg >= end) - return *this; - - while (beg < end) { - /* collect binary data and copy to binary buffer */ - size_t len = std::min(end - beg, src_end - src_cur); - std::memcpy(src_cur, beg, len); - beg += len; - src_cur += len; - - if (src_cur >= src_end) { - /* binary buffer is full. */ - /* decode it send result to dst */ - - CV_Assert(flush()); /* check for base64_valid */ - } - } - - return *this; -} - -bool Base64ContextParser::flush() -{ - if ( !base64_valid(src_beg, 0U, src_cur - src_beg) ) - return false; - - if ( src_cur == src_beg ) - return true; - - uchar * buffer = binary_buffer.data(); - size_t len = base64_decode(src_beg, buffer, 0U, src_cur - src_beg); - src_cur = src_beg; - - /* unexpected error */ - CV_Assert(len != 0); - - /* buffer is full */ - CV_Assert(dst_cur + len < dst_end); - - if (dst_cur + len < dst_end) { - /* send data to dst */ - std::memcpy(dst_cur, buffer, len); - dst_cur += len; - } - - return true; -} - -/**************************************************************************** - * Emitter - ***************************************************************************/ - -/* A decorator for CvFileStorage - * - no copyable - * - not safe for now - * - move constructor may be needed if C++11 - */ -class Base64ContextEmitter -{ -public: - explicit Base64ContextEmitter(CvFileStorage * fs) - : file_storage(fs) - , binary_buffer(BUFFER_LEN) - , base64_buffer(base64_encode_buffer_size(BUFFER_LEN)) - , src_beg(0) - , src_cur(0) - , src_end(0) - { - src_beg = binary_buffer.data(); - src_end = src_beg + BUFFER_LEN; - src_cur = src_beg; - - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - - if ( fs->fmt == CV_STORAGE_FORMAT_JSON ) - { - /* clean and break buffer */ - *fs->buffer++ = '\0'; - ::icvPuts( fs, fs->buffer_start ); - fs->buffer = fs->buffer_start; - memset( file_storage->buffer_start, 0, static_cast(file_storage->space) ); - ::icvPuts( fs, "\"$base64$" ); - } - else - { - ::icvFSFlush(file_storage); - } - } - - ~Base64ContextEmitter() - { - /* cleaning */ - if (src_cur != src_beg) - flush(); /* encode the rest binary data to base64 buffer */ - - if ( file_storage->fmt == CV_STORAGE_FORMAT_JSON ) - { - /* clean and break buffer */ - ::icvPuts(file_storage, "\""); - file_storage->buffer = file_storage->buffer_start; - ::icvFSFlush( file_storage ); - memset( file_storage->buffer_start, 0, static_cast(file_storage->space) ); - file_storage->buffer = file_storage->buffer_start; - } - } - - Base64ContextEmitter & write(const uchar * beg, const uchar * end) - { - if (beg >= end) - return *this; - - while (beg < end) { - /* collect binary data and copy to binary buffer */ - size_t len = std::min(end - beg, src_end - src_cur); - std::memcpy(src_cur, beg, len); - beg += len; - src_cur += len; - - if (src_cur >= src_end) { - /* binary buffer is full. */ - /* encode it to base64 and send result to fs */ - flush(); - } - } - - return *this; - } - - /* - * a convertor must provide : - * - `operator >> (uchar * & dst)` for writing current binary data to `dst` and moving to next data. - * - `operator bool` for checking if current loaction is valid and not the end. - */ - template inline - Base64ContextEmitter & write(_to_binary_convertor_t & convertor) - { - static const size_t BUFFER_MAX_LEN = 1024U; - - std::vector buffer(BUFFER_MAX_LEN); - uchar * beg = buffer.data(); - uchar * end = beg; - - while (convertor) { - convertor >> end; - write(beg, end); - end = beg; - } - - return *this; - } - - bool flush() - { - /* control line width, so on. */ - size_t len = base64_encode(src_beg, base64_buffer.data(), 0U, src_cur - src_beg); - if (len == 0U) - return false; - - src_cur = src_beg; - { - if ( file_storage->fmt == CV_STORAGE_FORMAT_JSON ) - { - ::icvPuts(file_storage, (const char*)base64_buffer.data()); - } - else - { - const char newline[] = "\n"; - char space[80]; - int ident = file_storage->struct_indent; - memset(space, ' ', static_cast(ident)); - space[ident] = '\0'; - - ::icvPuts(file_storage, space); - ::icvPuts(file_storage, (const char*)base64_buffer.data()); - ::icvPuts(file_storage, newline); - ::icvFSFlush(file_storage); - } - - } - - return true; - } - -private: - /* because of Base64, we must keep its length a multiple of 3 */ - static const size_t BUFFER_LEN = 48U; - // static_assert(BUFFER_LEN % 3 == 0, "BUFFER_LEN is invalid"); - -private: - CvFileStorage * file_storage; - - std::vector binary_buffer; - std::vector base64_buffer; - uchar * src_beg; - uchar * src_cur; - uchar * src_end; -}; - - -class RawDataToBinaryConvertor -{ -public: - - RawDataToBinaryConvertor(const void* src, int len, const std::string & dt) - : beg(reinterpret_cast(src)) - , cur(0) - , end(0) - { - CV_Assert(src); - CV_Assert(!dt.empty()); - CV_Assert(len > 0); - - /* calc step and to_binary_funcs */ - make_to_binary_funcs(dt); - - end = beg; - cur = beg; - - step = ::icvCalcStructSize(dt.c_str(), 0); - end = beg + step * static_cast(len); - } - - inline RawDataToBinaryConvertor & operator >>(uchar * & dst) - { - CV_DbgAssert(*this); - - for (size_t i = 0U, n = to_binary_funcs.size(); i < n; i++) { - elem_to_binary_t & pack = to_binary_funcs[i]; - pack.func(cur + pack.offset, dst + pack.offset); - } - cur += step; - dst += step; - - return *this; - } - - inline operator bool() const - { - return cur < end; - } - -private: - typedef size_t(*to_binary_t)(const uchar *, uchar *); - struct elem_to_binary_t - { - size_t offset; - to_binary_t func; - }; - -private: - void make_to_binary_funcs(const std::string &dt) - { - size_t cnt = 0; - size_t offset = 0; - char type = '\0'; - - std::istringstream iss(dt); - while (!iss.eof()) { - if (!(iss >> cnt)) { - iss.clear(); - cnt = 1; - } - CV_Assert(cnt > 0U); - if (!(iss >> type)) - break; - - while (cnt-- > 0) - { - elem_to_binary_t pack; - - size_t size = 0; - switch (type) - { - case 'u': - case 'c': - size = sizeof(uchar); - pack.func = to_binary; - break; - case 'w': - case 's': - size = sizeof(ushort); - pack.func = to_binary; - break; - case 'i': - size = sizeof(uint); - pack.func = to_binary; - break; - case 'f': - size = sizeof(float); - pack.func = to_binary; - break; - case 'd': - size = sizeof(double); - pack.func = to_binary; - break; - case 'r': - default: - CV_Error(cv::Error::StsError, "type is not supported"); - }; - - offset = static_cast(cvAlign(static_cast(offset), static_cast(size))); - pack.offset = offset; - offset += size; - - to_binary_funcs.push_back(pack); - } - } - - CV_Assert(iss.eof()); - } - -private: - const uchar * beg; - const uchar * cur; - const uchar * end; - - size_t step; - std::vector to_binary_funcs; -}; - -class BinaryToCvSeqConvertor -{ -public: - BinaryToCvSeqConvertor(const void* src, int len, const char* dt) - : cur(reinterpret_cast(src)) - , beg(reinterpret_cast(src)) - , end(reinterpret_cast(src)) - { - CV_Assert(src); - CV_Assert(dt); - CV_Assert(len >= 0); - - /* calc binary_to_funcs */ - make_funcs(dt); - functor_iter = binary_to_funcs.begin(); - - step = ::icvCalcStructSize(dt, 0); - end = beg + step * static_cast(len); - } - - inline BinaryToCvSeqConvertor & operator >> (CvFileNode & dst) - { - CV_DbgAssert(*this); - - /* get current data */ - union - { - uchar mem[sizeof(double)]; - uchar u; - char b; - ushort w; - short s; - int i; - float f; - double d; - } buffer; /* for GCC -Wstrict-aliasing */ - std::memset(buffer.mem, 0, sizeof(buffer)); - functor_iter->func(cur + functor_iter->offset, buffer.mem); - - /* set node::data */ - switch (functor_iter->cv_type) - { - case CV_8U : { dst.data.i = cv::saturate_cast (buffer.u); break;} - case CV_8S : { dst.data.i = cv::saturate_cast (buffer.b); break;} - case CV_16U: { dst.data.i = cv::saturate_cast (buffer.w); break;} - case CV_16S: { dst.data.i = cv::saturate_cast (buffer.s); break;} - case CV_32S: { dst.data.i = cv::saturate_cast (buffer.i); break;} - case CV_32F: { dst.data.f = cv::saturate_cast(buffer.f); break;} - case CV_64F: { dst.data.f = cv::saturate_cast(buffer.d); break;} - default: break; - } - - /* set node::tag */ - switch (functor_iter->cv_type) - { - case CV_8U : - case CV_8S : - case CV_16U: - case CV_16S: - case CV_32S: { dst.tag = CV_NODE_INT; /*std::printf("%i,", dst.data.i);*/ break; } - case CV_32F: - case CV_64F: { dst.tag = CV_NODE_REAL; /*std::printf("%.1f,", dst.data.f);*/ break; } - default: break; - } - - /* check if end */ - if (++functor_iter == binary_to_funcs.end()) { - functor_iter = binary_to_funcs.begin(); - cur += step; - } - - return *this; - } - - inline operator bool() const - { - return cur < end; - } - -private: - typedef size_t(*binary_to_t)(uchar const *, uchar *); - struct binary_to_filenode_t - { - size_t cv_type; - size_t offset; - binary_to_t func; - }; - -private: - void make_funcs(const char* dt) - { - size_t cnt = 0; - char type = '\0'; - size_t offset = 0; - - std::istringstream iss(dt); - while (!iss.eof()) { - if (!(iss >> cnt)) { - iss.clear(); - cnt = 1; - } - CV_Assert(cnt > 0U); - if (!(iss >> type)) - break; - - while (cnt-- > 0) - { - binary_to_filenode_t pack; - - /* set func and offset */ - size_t size = 0; - switch (type) - { - case 'u': - case 'c': - size = sizeof(uchar); - pack.func = binary_to; - break; - case 'w': - case 's': - size = sizeof(ushort); - pack.func = binary_to; - break; - case 'i': - size = sizeof(uint); - pack.func = binary_to; - break; - case 'f': - size = sizeof(float); - pack.func = binary_to; - break; - case 'd': - size = sizeof(double); - pack.func = binary_to; - break; - case 'r': - default: - CV_Error(cv::Error::StsError, "type is not supported"); - }; // need a better way for outputting error. - - offset = static_cast(cvAlign(static_cast(offset), static_cast(size))); - pack.offset = offset; - offset += size; - - /* set type */ - switch (type) - { - case 'u': { pack.cv_type = CV_8U ; break; } - case 'c': { pack.cv_type = CV_8S ; break; } - case 'w': { pack.cv_type = CV_16U; break; } - case 's': { pack.cv_type = CV_16S; break; } - case 'i': { pack.cv_type = CV_32S; break; } - case 'f': { pack.cv_type = CV_32F; break; } - case 'd': { pack.cv_type = CV_64F; break; } - case 'r': - default: - CV_Error(cv::Error::StsError, "type is not supported"); - } // need a better way for outputting error. - - binary_to_funcs.push_back(pack); - } - } - - CV_Assert(iss.eof()); - CV_Assert(binary_to_funcs.size()); - } - -private: - - const uchar * cur; - const uchar * beg; - const uchar * end; - - size_t step; - std::vector binary_to_funcs; - std::vector::iterator functor_iter; -}; - - -/**************************************************************************** - * Wrapper - ***************************************************************************/ - -Base64Writer::Base64Writer(::CvFileStorage * fs) - : emitter(new Base64ContextEmitter(fs)) - , data_type_string() -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); -} - -void Base64Writer::write(const void* _data, size_t len, const char* dt) -{ - check_dt(dt); - RawDataToBinaryConvertor convertor(_data, static_cast(len), data_type_string); - emitter->write(convertor); -} - -template inline -void Base64Writer::write(_to_binary_convertor_t & convertor, const char* dt) -{ - check_dt(dt); - emitter->write(convertor); -} - -Base64Writer::~Base64Writer() -{ - delete emitter; -} - -void Base64Writer::check_dt(const char* dt) -{ - if ( dt == 0 ) - CV_Error( CV_StsBadArg, "Invalid \'dt\'." ); - else if (data_type_string.empty()) { - data_type_string = dt; - - /* output header */ - std::string buffer = make_base64_header(dt); - const uchar * beg = reinterpret_cast(buffer.data()); - const uchar * end = beg + buffer.size(); - - emitter->write(beg, end); - } else if ( data_type_string != dt ) - CV_Error( CV_StsBadArg, "\'dt\' does not match." ); -} - - -void make_seq(void * binary, int elem_cnt, const char * dt, ::CvSeq & seq) -{ - ::CvFileNode node; - node.info = 0; - BinaryToCvSeqConvertor convertor(binary, elem_cnt, dt); - while (convertor) { - convertor >> node; - cvSeqPush(&seq, &node); - } -} - -} // base64:: - -/**************************************************************************** - * Interface - ***************************************************************************/ - -CV_IMPL void cvWriteRawDataBase64(::CvFileStorage* fs, const void* _data, int len, const char* dt) -{ - CV_Assert(fs); - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - - check_if_write_struct_is_delayed( fs, true ); - - if ( fs->state_of_writing_base64 == base64::fs::Uncertain ) - { - switch_to_Base64_state( fs, base64::fs::InUse ); - } - else if ( fs->state_of_writing_base64 != base64::fs::InUse ) - { - CV_Error( CV_StsError, "Base64 should not be used at present." ); - } - - fs->base64_writer->write(_data, len, dt); -} diff --git a/modules/core/src/persistence_c.cpp b/modules/core/src/persistence_c.cpp deleted file mode 100644 index 7980a04..0000000 --- a/modules/core/src/persistence_c.cpp +++ /dev/null @@ -1,1479 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html - - -#include "precomp.hpp" -#include "persistence.hpp" - -static inline bool cv_strcasecmp(const char * s1, const char * s2) -{ - if ( s1 == 0 && s2 == 0 ) - return true; - else if ( s1 == 0 || s2 == 0 ) - return false; - - size_t len1 = strlen(s1); - size_t len2 = strlen(s2); - if ( len1 != len2 ) - return false; - - for ( size_t i = 0U; i < len1; i++ ) - if ( tolower( static_cast(s1[i]) ) != tolower( static_cast(s2[i]) ) ) - return false; - - return true; -} - -// this function will convert "aa?bb&cc&dd" to {"aa", "bb", "cc", "dd"} -static std::vector analyze_file_name( std::string const & file_name ) -{ - static const char not_file_name = '\n'; - static const char parameter_begin = '?'; - static const char parameter_separator = '&'; - std::vector result; - - if ( file_name.find(not_file_name, 0U) != std::string::npos ) - return result; - - size_t beg = file_name.find_last_of(parameter_begin); - size_t end = file_name.size(); - result.push_back(file_name.substr(0U, beg)); - - if ( beg != std::string::npos ) - { - beg ++; - for ( size_t param_beg = beg, param_end = beg; - param_end < end; - param_beg = param_end + 1U ) - { - param_end = file_name.find_first_of( parameter_separator, param_beg ); - if ( (param_end == std::string::npos || param_end != param_beg) && param_beg + 1U < end ) - { - result.push_back( file_name.substr( param_beg, param_end - param_beg ) ); - } - } - } - - return result; -} - -static bool is_param_exist( const std::vector & params, const std::string & param ) -{ - if ( params.size() < 2U ) - return false; - - return std::find(params.begin(), params.end(), param) != params.end(); -} - -//=========================================================================================== - -CV_IMPL CvFileStorage* -cvOpenFileStorage( const char* query, CvMemStorage* dststorage, int flags, const char* encoding ) -{ - CvFileStorage* fs = 0; - int default_block_size = 1 << 18; - bool append = (flags & 3) == CV_STORAGE_APPEND; - bool mem = (flags & CV_STORAGE_MEMORY) != 0; - bool write_mode = (flags & 3) != 0; - bool write_base64 = (write_mode || append) && (flags & CV_STORAGE_BASE64) != 0; - bool isGZ = false; - size_t fnamelen = 0; - const char * filename = query; - - std::vector params; - if ( !mem ) - { - params = analyze_file_name( query ); - if ( !params.empty() ) - filename = params.begin()->c_str(); - - if ( write_base64 == false && is_param_exist( params, "base64" ) ) - write_base64 = (write_mode || append); - } - - if( !filename || filename[0] == '\0' ) - { - if( !write_mode ) - CV_Error( CV_StsNullPtr, mem ? "NULL or empty filename" : "NULL or empty buffer" ); - mem = true; - } - else - fnamelen = strlen(filename); - - if( mem && append ) - CV_Error( CV_StsBadFlag, "CV_STORAGE_APPEND and CV_STORAGE_MEMORY are not currently compatible" ); - - fs = (CvFileStorage*)cvAlloc( sizeof(*fs) ); - CV_Assert(fs); - memset( fs, 0, sizeof(*fs)); - - fs->memstorage = cvCreateMemStorage( default_block_size ); - fs->dststorage = dststorage ? dststorage : fs->memstorage; - - fs->flags = CV_FILE_STORAGE; - fs->write_mode = write_mode; - - if( !mem ) - { - fs->filename = (char*)cvMemStorageAlloc( fs->memstorage, fnamelen+1 ); - strcpy( fs->filename, filename ); - - char* dot_pos = strrchr(fs->filename, '.'); - char compression = '\0'; - - if( dot_pos && dot_pos[1] == 'g' && dot_pos[2] == 'z' && - (dot_pos[3] == '\0' || (cv_isdigit(dot_pos[3]) && dot_pos[4] == '\0')) ) - { - if( append ) - { - cvReleaseFileStorage( &fs ); - CV_Error(CV_StsNotImplemented, "Appending data to compressed file is not implemented" ); - } - isGZ = true; - compression = dot_pos[3]; - if( compression ) - dot_pos[3] = '\0', fnamelen--; - } - - if( !isGZ ) - { - fs->file = fopen(fs->filename, !fs->write_mode ? "rt" : !append ? "wt" : "a+t" ); - if( !fs->file ) - goto _exit_; - } - else - { - #if USE_ZLIB - char mode[] = { fs->write_mode ? 'w' : 'r', 'b', compression ? compression : '3', '\0' }; - fs->gzfile = gzopen(fs->filename, mode); - if( !fs->gzfile ) - goto _exit_; - #else - cvReleaseFileStorage( &fs ); - CV_Error(CV_StsNotImplemented, "There is no compressed file storage support in this configuration"); - #endif - } - } - - fs->roots = 0; - fs->struct_indent = 0; - fs->struct_flags = 0; - fs->wrap_margin = 71; - - if( fs->write_mode ) - { - int fmt = flags & CV_STORAGE_FORMAT_MASK; - - if( mem ) - fs->outbuf = new std::deque; - - if( fmt == CV_STORAGE_FORMAT_AUTO && filename ) - { - const char* dot_pos = NULL; - const char* dot_pos2 = NULL; - // like strrchr() implementation, but save two last positions simultaneously - for (const char* pos = filename; pos[0] != 0; pos++) - { - if (pos[0] == '.') - { - dot_pos2 = dot_pos; - dot_pos = pos; - } - } - if (cv_strcasecmp(dot_pos, ".gz") && dot_pos2 != NULL) - { - dot_pos = dot_pos2; - } - fs->fmt - = (cv_strcasecmp(dot_pos, ".xml") || cv_strcasecmp(dot_pos, ".xml.gz")) - ? CV_STORAGE_FORMAT_XML - : (cv_strcasecmp(dot_pos, ".json") || cv_strcasecmp(dot_pos, ".json.gz")) - ? CV_STORAGE_FORMAT_JSON - : CV_STORAGE_FORMAT_YAML - ; - } - else if ( fmt != CV_STORAGE_FORMAT_AUTO ) - { - fs->fmt = fmt; - } - else - { - fs->fmt = CV_STORAGE_FORMAT_XML; - } - - // we use factor=6 for XML (the longest characters (' and ") are encoded with 6 bytes (' and ") - // and factor=4 for YAML ( as we use 4 bytes for non ASCII characters (e.g. \xAB)) - int buf_size = CV_FS_MAX_LEN*(fs->fmt == CV_STORAGE_FORMAT_XML ? 6 : 4) + 1024; - - if (append) - { - fseek( fs->file, 0, SEEK_END ); - if (ftell(fs->file) == 0) - append = false; - } - - fs->write_stack = cvCreateSeq( 0, sizeof(CvSeq), fs->fmt == CV_STORAGE_FORMAT_XML ? - sizeof(CvXMLStackRecord) : sizeof(int), fs->memstorage ); - fs->is_first = 1; - fs->struct_indent = 0; - fs->struct_flags = CV_NODE_EMPTY; - fs->buffer_start = fs->buffer = (char*)cvAlloc( buf_size + 1024 ); - fs->buffer_end = fs->buffer_start + buf_size; - - fs->base64_writer = 0; - fs->is_default_using_base64 = write_base64; - fs->state_of_writing_base64 = base64::fs::Uncertain; - - fs->is_write_struct_delayed = false; - fs->delayed_struct_key = 0; - fs->delayed_struct_flags = 0; - fs->delayed_type_name = 0; - - if( fs->fmt == CV_STORAGE_FORMAT_XML ) - { - size_t file_size = fs->file ? (size_t)ftell( fs->file ) : (size_t)0; - fs->strstorage = cvCreateChildMemStorage( fs->memstorage ); - if( !append || file_size == 0 ) - { - if( encoding ) - { - if( strcmp( encoding, "UTF-16" ) == 0 || - strcmp( encoding, "utf-16" ) == 0 || - strcmp( encoding, "Utf-16" ) == 0 ) - { - cvReleaseFileStorage( &fs ); - CV_Error( CV_StsBadArg, "UTF-16 XML encoding is not supported! Use 8-bit encoding\n"); - } - - CV_Assert( strlen(encoding) < 1000 ); - char buf[1100]; - sprintf(buf, "\n", encoding); - icvPuts( fs, buf ); - } - else - icvPuts( fs, "\n" ); - icvPuts( fs, "\n" ); - } - else - { - int xml_buf_size = 1 << 10; - char substr[] = ""; - int last_occurence = -1; - xml_buf_size = MIN(xml_buf_size, int(file_size)); - fseek( fs->file, -xml_buf_size, SEEK_END ); - char* xml_buf = (char*)cvAlloc( xml_buf_size+2 ); - // find the last occurrence of - for(;;) - { - int line_offset = (int)ftell( fs->file ); - char* ptr0 = icvGets( fs, xml_buf, xml_buf_size ), *ptr; - if( !ptr0 ) - break; - ptr = ptr0; - for(;;) - { - ptr = strstr( ptr, substr ); - if( !ptr ) - break; - last_occurence = line_offset + (int)(ptr - ptr0); - ptr += strlen(substr); - } - } - cvFree( &xml_buf ); - if( last_occurence < 0 ) - { - cvReleaseFileStorage( &fs ); - CV_Error( CV_StsError, "Could not find in the end of file.\n" ); - } - icvCloseFile( fs ); - fs->file = fopen( fs->filename, "r+t" ); - CV_Assert(fs->file); - fseek( fs->file, last_occurence, SEEK_SET ); - // replace the last "" with " ", which has the same length - icvPuts( fs, " " ); - fseek( fs->file, 0, SEEK_END ); - icvPuts( fs, "\n" ); - } - fs->start_write_struct = icvXMLStartWriteStruct; - fs->end_write_struct = icvXMLEndWriteStruct; - fs->write_int = icvXMLWriteInt; - fs->write_real = icvXMLWriteReal; - fs->write_string = icvXMLWriteString; - fs->write_comment = icvXMLWriteComment; - fs->start_next_stream = icvXMLStartNextStream; - } - else if( fs->fmt == CV_STORAGE_FORMAT_YAML ) - { - if( !append) - icvPuts( fs, "%YAML:1.0\n---\n" ); - else - icvPuts( fs, "...\n---\n" ); - fs->start_write_struct = icvYMLStartWriteStruct; - fs->end_write_struct = icvYMLEndWriteStruct; - fs->write_int = icvYMLWriteInt; - fs->write_real = icvYMLWriteReal; - fs->write_string = icvYMLWriteString; - fs->write_comment = icvYMLWriteComment; - fs->start_next_stream = icvYMLStartNextStream; - } - else - { - if( !append ) - icvPuts( fs, "{\n" ); - else - { - bool valid = false; - long roffset = 0; - for ( ; - fseek( fs->file, roffset, SEEK_END ) == 0; - roffset -= 1 ) - { - const char end_mark = '}'; - if ( fgetc( fs->file ) == end_mark ) - { - fseek( fs->file, roffset, SEEK_END ); - valid = true; - break; - } - } - - if ( valid ) - { - icvCloseFile( fs ); - fs->file = fopen( fs->filename, "r+t" ); - CV_Assert(fs->file); - fseek( fs->file, roffset, SEEK_END ); - fputs( ",", fs->file ); - } - else - { - CV_Error( CV_StsError, "Could not find '}' in the end of file.\n" ); - } - } - fs->struct_indent = 4; - fs->start_write_struct = icvJSONStartWriteStruct; - fs->end_write_struct = icvJSONEndWriteStruct; - fs->write_int = icvJSONWriteInt; - fs->write_real = icvJSONWriteReal; - fs->write_string = icvJSONWriteString; - fs->write_comment = icvJSONWriteComment; - fs->start_next_stream = icvJSONStartNextStream; - } - } - else - { - if( mem ) - { - fs->strbuf = filename; - fs->strbufsize = fnamelen; - } - - size_t buf_size = 1 << 20; - const char* yaml_signature = "%YAML"; - const char* json_signature = "{"; - const char* xml_signature = "fmt = CV_STORAGE_FORMAT_YAML; - else if(strncmp( bufPtr, json_signature, strlen(json_signature) ) == 0) - fs->fmt = CV_STORAGE_FORMAT_JSON; - else if(strncmp( bufPtr, xml_signature, strlen(xml_signature) ) == 0) - fs->fmt = CV_STORAGE_FORMAT_XML; - else if(fs->strbufsize == bufOffset) - CV_Error(CV_BADARG_ERR, "Input file is empty"); - else - CV_Error(CV_BADARG_ERR, "Unsupported file storage format"); - - if( !isGZ ) - { - if( !mem ) - { - fseek( fs->file, 0, SEEK_END ); - buf_size = ftell( fs->file ); - } - else - buf_size = fs->strbufsize; - buf_size = MIN( buf_size, (size_t)(1 << 20) ); - buf_size = MAX( buf_size, (size_t)(CV_FS_MAX_LEN*2 + 1024) ); - } - icvRewind(fs); - fs->strbufpos = bufOffset; - - fs->str_hash = cvCreateMap( 0, sizeof(CvStringHash), - sizeof(CvStringHashNode), fs->memstorage, 256 ); - - fs->roots = cvCreateSeq( 0, sizeof(CvSeq), - sizeof(CvFileNode), fs->memstorage ); - - fs->buffer = fs->buffer_start = (char*)cvAlloc( buf_size + 256 ); - fs->buffer_end = fs->buffer_start + buf_size; - fs->buffer[0] = '\n'; - fs->buffer[1] = '\0'; - - //mode = cvGetErrMode(); - //cvSetErrMode( CV_ErrModeSilent ); - CV_TRY - { - switch (fs->fmt) - { - case CV_STORAGE_FORMAT_XML : { icvXMLParse ( fs ); break; } - case CV_STORAGE_FORMAT_YAML: { icvYMLParse ( fs ); break; } - case CV_STORAGE_FORMAT_JSON: { icvJSONParse( fs ); break; } - default: break; - } - } - CV_CATCH_ALL - { - fs->is_opened = true; - cvReleaseFileStorage( &fs ); - CV_RETHROW(); - } - //cvSetErrMode( mode ); - - // release resources that we do not need anymore - cvFree( &fs->buffer_start ); - fs->buffer = fs->buffer_end = 0; - } - fs->is_opened = true; - -_exit_: - if( fs ) - { - if( cvGetErrStatus() < 0 || (!fs->file && !fs->gzfile && !fs->outbuf && !fs->strbuf) ) - { - cvReleaseFileStorage( &fs ); - } - else if( !fs->write_mode ) - { - icvCloseFile(fs); - // we close the file since it's not needed anymore. But icvCloseFile() resets is_opened, - // which may be misleading. Since we restore the value of is_opened. - fs->is_opened = true; - } - } - - return fs; -} - - -/* closes file storage and deallocates buffers */ -CV_IMPL void -cvReleaseFileStorage( CvFileStorage** p_fs ) -{ - if( !p_fs ) - CV_Error( CV_StsNullPtr, "NULL double pointer to file storage" ); - - if( *p_fs ) - { - CvFileStorage* fs = *p_fs; - *p_fs = 0; - - icvClose(fs, 0); - - cvReleaseMemStorage( &fs->strstorage ); - cvFree( &fs->buffer_start ); - cvReleaseMemStorage( &fs->memstorage ); - - delete fs->outbuf; - delete fs->base64_writer; - delete[] fs->delayed_struct_key; - delete[] fs->delayed_type_name; - - memset( fs, 0, sizeof(*fs) ); - cvFree( &fs ); - } -} - - -CV_IMPL void* -cvLoad( const char* filename, CvMemStorage* memstorage, - const char* name, const char** _real_name ) -{ - void* ptr = 0; - const char* real_name = 0; - cv::FileStorage fs(cvOpenFileStorage(filename, memstorage, CV_STORAGE_READ)); - - CvFileNode* node = 0; - - if( !fs.isOpened() ) - return 0; - - if( name ) - { - node = cvGetFileNodeByName( *fs, 0, name ); - } - else - { - int i, k; - for( k = 0; k < (*fs)->roots->total; k++ ) - { - CvSeq* seq; - CvSeqReader reader; - - node = (CvFileNode*)cvGetSeqElem( (*fs)->roots, k ); - CV_Assert(node != NULL); - if( !CV_NODE_IS_MAP( node->tag )) - return 0; - seq = node->data.seq; - node = 0; - - cvStartReadSeq( seq, &reader, 0 ); - - // find the first element in the map - for( i = 0; i < seq->total; i++ ) - { - if( CV_IS_SET_ELEM( reader.ptr )) - { - node = (CvFileNode*)reader.ptr; - goto stop_search; - } - CV_NEXT_SEQ_ELEM( seq->elem_size, reader ); - } - } - -stop_search: - ; - } - - if( !node ) - CV_Error( CV_StsObjectNotFound, "Could not find the/an object in file storage" ); - - real_name = cvGetFileNodeName( node ); - ptr = cvRead( *fs, node, 0 ); - - // sanity check - if( !memstorage && (CV_IS_SEQ( ptr ) || CV_IS_SET( ptr )) ) - CV_Error( CV_StsNullPtr, - "NULL memory storage is passed - the loaded dynamic structure can not be stored" ); - - if( cvGetErrStatus() < 0 ) - { - cvRelease( (void**)&ptr ); - real_name = 0; - } - - if( _real_name) - { - if (real_name) - { - *_real_name = (const char*)cvAlloc(strlen(real_name)); - memcpy((void*)*_real_name, real_name, strlen(real_name)); - } else { - *_real_name = 0; - } - } - - return ptr; -} - -CV_IMPL const char* -cvAttrValue( const CvAttrList* attr, const char* attr_name ) -{ - while( attr && attr->attr ) - { - int i; - for( i = 0; attr->attr[i*2] != 0; i++ ) - { - if( strcmp( attr_name, attr->attr[i*2] ) == 0 ) - return attr->attr[i*2+1]; - } - attr = attr->next; - } - - return 0; -} - - -#define CV_HASHVAL_SCALE 33 - -CV_IMPL CvStringHashNode* -cvGetHashedKey( CvFileStorage* fs, const char* str, int len, int create_missing ) -{ - CvStringHashNode* node = 0; - unsigned hashval = 0; - int i, tab_size; - - if( !fs ) - return 0; - - CvStringHash* map = fs->str_hash; - - if( len < 0 ) - { - for( i = 0; str[i] != '\0'; i++ ) - hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i]; - len = i; - } - else for( i = 0; i < len; i++ ) - hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i]; - - hashval &= INT_MAX; - tab_size = map->tab_size; - if( (tab_size & (tab_size - 1)) == 0 ) - i = (int)(hashval & (tab_size - 1)); - else - i = (int)(hashval % tab_size); - - for( node = (CvStringHashNode*)(map->table[i]); node != 0; node = node->next ) - { - if( node->hashval == hashval && - node->str.len == len && - memcmp( node->str.ptr, str, len ) == 0 ) - break; - } - - if( !node && create_missing ) - { - node = (CvStringHashNode*)cvSetNew( (CvSet*)map ); - node->hashval = hashval; - node->str = cvMemStorageAllocString( map->storage, str, len ); - node->next = (CvStringHashNode*)(map->table[i]); - map->table[i] = node; - } - - return node; -} - -CV_IMPL CvFileNode* -cvGetFileNode( CvFileStorage* fs, CvFileNode* _map_node, - const CvStringHashNode* key, - int create_missing ) -{ - CvFileNode* value = 0; - int k = 0, attempts = 1; - - if( !fs ) - return 0; - - CV_CHECK_FILE_STORAGE(fs); - - if( !key ) - CV_Error( CV_StsNullPtr, "Null key element" ); - - if( _map_node ) - { - if( !fs->roots ) - return 0; - attempts = fs->roots->total; - } - - for( k = 0; k < attempts; k++ ) - { - int i, tab_size; - CvFileNode* map_node = _map_node; - CvFileMapNode* another; - CvFileNodeHash* map; - - if( !map_node ) - map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k ); - CV_Assert(map_node != NULL); - if( !CV_NODE_IS_MAP(map_node->tag) ) - { - if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) && - CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE ) - CV_Error( CV_StsError, "The node is neither a map nor an empty collection" ); - return 0; - } - - map = map_node->data.map; - tab_size = map->tab_size; - - if( (tab_size & (tab_size - 1)) == 0 ) - i = (int)(key->hashval & (tab_size - 1)); - else - i = (int)(key->hashval % tab_size); - - for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next ) - if( another->key == key ) - { - if( !create_missing ) - { - value = &another->value; - return value; - } - CV_PARSE_ERROR( "Duplicated key" ); - } - - if( k == attempts - 1 && create_missing ) - { - CvFileMapNode* node = (CvFileMapNode*)cvSetNew( (CvSet*)map ); - node->key = key; - - node->next = (CvFileMapNode*)(map->table[i]); - map->table[i] = node; - value = (CvFileNode*)node; - } - } - - return value; -} - -CV_IMPL CvFileNode* -cvGetFileNodeByName( const CvFileStorage* fs, const CvFileNode* _map_node, const char* str ) -{ - CvFileNode* value = 0; - int i, len, tab_size; - unsigned hashval = 0; - int k = 0, attempts = 1; - - if( !fs ) - return 0; - - CV_CHECK_FILE_STORAGE(fs); - - if( !str ) - CV_Error( CV_StsNullPtr, "Null element name" ); - - for( i = 0; str[i] != '\0'; i++ ) - hashval = hashval*CV_HASHVAL_SCALE + (unsigned char)str[i]; - hashval &= INT_MAX; - len = i; - - if( !_map_node ) - { - if( !fs->roots ) - return 0; - attempts = fs->roots->total; - } - - for( k = 0; k < attempts; k++ ) - { - CvFileNodeHash* map; - const CvFileNode* map_node = _map_node; - CvFileMapNode* another; - - if( !map_node ) - map_node = (CvFileNode*)cvGetSeqElem( fs->roots, k ); - - if( !CV_NODE_IS_MAP(map_node->tag) ) - { - if( (!CV_NODE_IS_SEQ(map_node->tag) || map_node->data.seq->total != 0) && - CV_NODE_TYPE(map_node->tag) != CV_NODE_NONE ) - CV_Error( CV_StsError, "The node is neither a map nor an empty collection" ); - return 0; - } - - map = map_node->data.map; - tab_size = map->tab_size; - - if( (tab_size & (tab_size - 1)) == 0 ) - i = (int)(hashval & (tab_size - 1)); - else - i = (int)(hashval % tab_size); - - for( another = (CvFileMapNode*)(map->table[i]); another != 0; another = another->next ) - { - const CvStringHashNode* key = another->key; - - if( key->hashval == hashval && - key->str.len == len && - memcmp( key->str.ptr, str, len ) == 0 ) - { - value = &another->value; - return value; - } - } - } - - return value; -} - -CV_IMPL CvFileNode* -cvGetRootFileNode( const CvFileStorage* fs, int stream_index ) -{ - CV_CHECK_FILE_STORAGE(fs); - - if( !fs->roots || (unsigned)stream_index >= (unsigned)fs->roots->total ) - return 0; - - return (CvFileNode*)cvGetSeqElem( fs->roots, stream_index ); -} - -CV_IMPL void -cvStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, - const char* type_name, CvAttrList /*attributes*/ ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - check_if_write_struct_is_delayed( fs ); - if ( fs->state_of_writing_base64 == base64::fs::NotUse ) - switch_to_Base64_state( fs, base64::fs::Uncertain ); - - if ( fs->state_of_writing_base64 == base64::fs::Uncertain - && - CV_NODE_IS_SEQ(struct_flags) - && - fs->is_default_using_base64 - && - type_name == 0 - ) - { - /* Uncertain whether output Base64 data */ - make_write_struct_delayed( fs, key, struct_flags, type_name ); - } - else if ( type_name && memcmp(type_name, "binary", 6) == 0 ) - { - /* Must output Base64 data */ - if ( !CV_NODE_IS_SEQ(struct_flags) ) - CV_Error( CV_StsBadArg, "must set 'struct_flags |= CV_NODE_SEQ' if using Base64."); - else if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) - CV_Error( CV_StsError, "function \'cvStartWriteStruct\' calls cannot be nested if using Base64."); - - fs->start_write_struct( fs, key, struct_flags, type_name ); - - if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) - switch_to_Base64_state( fs, base64::fs::Uncertain ); - switch_to_Base64_state( fs, base64::fs::InUse ); - } - else - { - /* Won't output Base64 data */ - if ( fs->state_of_writing_base64 == base64::fs::InUse ) - CV_Error( CV_StsError, "At the end of the output Base64, `cvEndWriteStruct` is needed."); - - fs->start_write_struct( fs, key, struct_flags, type_name ); - - if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) - switch_to_Base64_state( fs, base64::fs::Uncertain ); - switch_to_Base64_state( fs, base64::fs::NotUse ); - } -} - - -CV_IMPL void -cvEndWriteStruct( CvFileStorage* fs ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - check_if_write_struct_is_delayed( fs ); - - if ( fs->state_of_writing_base64 != base64::fs::Uncertain ) - switch_to_Base64_state( fs, base64::fs::Uncertain ); - - fs->end_write_struct( fs ); -} - - -CV_IMPL void -cvWriteInt( CvFileStorage* fs, const char* key, int value ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - fs->write_int( fs, key, value ); -} - - -CV_IMPL void -cvWriteReal( CvFileStorage* fs, const char* key, double value ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - fs->write_real( fs, key, value ); -} - - -CV_IMPL void -cvWriteString( CvFileStorage* fs, const char* key, const char* value, int quote ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - fs->write_string( fs, key, value, quote ); -} - - -CV_IMPL void -cvWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - fs->write_comment( fs, comment, eol_comment ); -} - - -CV_IMPL void -cvStartNextStream( CvFileStorage* fs ) -{ - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - fs->start_next_stream( fs ); -} - -CV_IMPL void -cvWriteRawData( CvFileStorage* fs, const void* _data, int len, const char* dt ) -{ - if (fs->is_default_using_base64 || - fs->state_of_writing_base64 == base64::fs::InUse ) - { - cvWriteRawDataBase64( fs, _data, len, dt ); - return; - } - else if ( fs->state_of_writing_base64 == base64::fs::Uncertain ) - { - switch_to_Base64_state( fs, base64::fs::NotUse ); - } - - const char* data0 = (const char*)_data; - int offset = 0; - int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k, fmt_pair_count; - char buf[256] = ""; - - CV_CHECK_OUTPUT_FILE_STORAGE( fs ); - - if( len < 0 ) - CV_Error( CV_StsOutOfRange, "Negative number of elements" ); - - fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); - - if( !len ) - return; - - if( !data0 ) - CV_Error( CV_StsNullPtr, "Null data pointer" ); - - if( fmt_pair_count == 1 ) - { - fmt_pairs[0] *= len; - len = 1; - } - - for(;len--;) - { - for( k = 0; k < fmt_pair_count; k++ ) - { - int i, count = fmt_pairs[k*2]; - int elem_type = fmt_pairs[k*2+1]; - int elem_size = CV_ELEM_SIZE(elem_type); - const char* data, *ptr; - - offset = cvAlign( offset, elem_size ); - data = data0 + offset; - - for( i = 0; i < count; i++ ) - { - switch( elem_type ) - { - case CV_8U: - ptr = icv_itoa( *(uchar*)data, buf, 10 ); - data++; - break; - case CV_8S: - ptr = icv_itoa( *(char*)data, buf, 10 ); - data++; - break; - case CV_16U: - ptr = icv_itoa( *(ushort*)data, buf, 10 ); - data += sizeof(ushort); - break; - case CV_16S: - ptr = icv_itoa( *(short*)data, buf, 10 ); - data += sizeof(short); - break; - case CV_32S: - ptr = icv_itoa( *(int*)data, buf, 10 ); - data += sizeof(int); - break; - case CV_32F: - ptr = icvFloatToString( buf, *(float*)data ); - data += sizeof(float); - break; - case CV_64F: - ptr = icvDoubleToString( buf, *(double*)data ); - data += sizeof(double); - break; - case CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/: /* reference */ - ptr = icv_itoa( (int)*(size_t*)data, buf, 10 ); - data += sizeof(size_t); - break; - default: - CV_Error( CV_StsUnsupportedFormat, "Unsupported type" ); - return; - } - - if( fs->fmt == CV_STORAGE_FORMAT_XML ) - { - int buf_len = (int)strlen(ptr); - icvXMLWriteScalar( fs, 0, ptr, buf_len ); - } - else if ( fs->fmt == CV_STORAGE_FORMAT_YAML ) - { - icvYMLWrite( fs, 0, ptr ); - } - else - { - if( elem_type == CV_32F || elem_type == CV_64F ) - { - size_t buf_len = strlen(ptr); - if( buf_len > 0 && ptr[buf_len-1] == '.' ) - { - // append zero if CV_32F or CV_64F string ends with decimal place to match JSON standard - // ptr will point to buf, so can write to buf given ptr is const - buf[buf_len] = '0'; - buf[buf_len+1] = '\0'; - } - } - icvJSONWrite( fs, 0, ptr ); - } - } - - offset = (int)(data - data0); - } - } -} - -CV_IMPL void -cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, CvSeqReader* reader ) -{ - int node_type; - CV_CHECK_FILE_STORAGE( fs ); - - if( !src || !reader ) - CV_Error( CV_StsNullPtr, "Null pointer to source file node or reader" ); - - node_type = CV_NODE_TYPE(src->tag); - if( node_type == CV_NODE_INT || node_type == CV_NODE_REAL ) - { - // emulate reading from 1-element sequence - reader->ptr = (schar*)src; - reader->block_max = reader->ptr + sizeof(*src)*2; - reader->block_min = reader->ptr; - reader->seq = 0; - } - else if( node_type == CV_NODE_SEQ ) - { - cvStartReadSeq( src->data.seq, reader, 0 ); - } - else if( node_type == CV_NODE_NONE ) - { - memset( reader, 0, sizeof(*reader) ); - } - else - CV_Error( CV_StsBadArg, "The file node should be a numerical scalar or a sequence" ); -} - - -CV_IMPL void -cvReadRawDataSlice( const CvFileStorage* fs, CvSeqReader* reader, - int len, void* _data, const char* dt ) -{ - char* data0 = (char*)_data; - int fmt_pairs[CV_FS_MAX_FMT_PAIRS*2], k = 0, fmt_pair_count; - int i = 0, count = 0; - - CV_CHECK_FILE_STORAGE( fs ); - - if( !reader || !data0 ) - CV_Error( CV_StsNullPtr, "Null pointer to reader or destination array" ); - - if( !reader->seq && len != 1 ) - CV_Error( CV_StsBadSize, "The read sequence is a scalar, thus len must be 1" ); - - fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); - size_t step = ::icvCalcStructSize(dt, 0); - - for(;;) - { - int offset = 0; - for( k = 0; k < fmt_pair_count; k++ ) - { - int elem_type = fmt_pairs[k*2+1]; - int elem_size = CV_ELEM_SIZE(elem_type); - char* data; - - count = fmt_pairs[k*2]; - offset = cvAlign( offset, elem_size ); - data = data0 + offset; - - for( i = 0; i < count; i++ ) - { - CvFileNode* node = (CvFileNode*)reader->ptr; - if( CV_NODE_IS_INT(node->tag) ) - { - int ival = node->data.i; - - switch( elem_type ) - { - case CV_8U: - *(uchar*)data = cv::saturate_cast(ival); - data++; - break; - case CV_8S: - *(char*)data = cv::saturate_cast(ival); - data++; - break; - case CV_16U: - *(ushort*)data = cv::saturate_cast(ival); - data += sizeof(ushort); - break; - case CV_16S: - *(short*)data = cv::saturate_cast(ival); - data += sizeof(short); - break; - case CV_32S: - *(int*)data = ival; - data += sizeof(int); - break; - case CV_32F: - *(float*)data = (float)ival; - data += sizeof(float); - break; - case CV_64F: - *(double*)data = (double)ival; - data += sizeof(double); - break; - case CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/: /* reference */ - *(size_t*)data = ival; - data += sizeof(size_t); - break; - default: - CV_Error( CV_StsUnsupportedFormat, "Unsupported type" ); - return; - } - } - else if( CV_NODE_IS_REAL(node->tag) ) - { - double fval = node->data.f; - int ival; - - switch( elem_type ) - { - case CV_8U: - ival = cvRound(fval); - *(uchar*)data = cv::saturate_cast(ival); - data++; - break; - case CV_8S: - ival = cvRound(fval); - *(char*)data = cv::saturate_cast(ival); - data++; - break; - case CV_16U: - ival = cvRound(fval); - *(ushort*)data = cv::saturate_cast(ival); - data += sizeof(ushort); - break; - case CV_16S: - ival = cvRound(fval); - *(short*)data = cv::saturate_cast(ival); - data += sizeof(short); - break; - case CV_32S: - ival = cvRound(fval); - *(int*)data = ival; - data += sizeof(int); - break; - case CV_32F: - *(float*)data = (float)fval; - data += sizeof(float); - break; - case CV_64F: - *(double*)data = fval; - data += sizeof(double); - break; - case CV_SEQ_ELTYPE_PTR/*CV_USRTYPE1*/: /* reference */ - ival = cvRound(fval); - *(size_t*)data = ival; - data += sizeof(size_t); - break; - default: - CV_Error( CV_StsUnsupportedFormat, "Unsupported type" ); - return; - } - } - else - CV_Error( CV_StsError, - "The sequence element is not a numerical scalar" ); - - CV_NEXT_SEQ_ELEM( sizeof(CvFileNode), *reader ); - if( !--len ) - goto end_loop; - } - - offset = (int)(data - data0); - } - data0 += step; - } - -end_loop: - if( i != count - 1 || k != fmt_pair_count - 1 ) - CV_Error( CV_StsBadSize, - "The sequence slice does not fit an integer number of records" ); - - if( !reader->seq ) - reader->ptr -= sizeof(CvFileNode); -} - - -CV_IMPL void -cvReadRawData( const CvFileStorage* fs, const CvFileNode* src, - void* data, const char* dt ) -{ - CvSeqReader reader; - - if( !src || !data ) - CV_Error( CV_StsNullPtr, "Null pointers to source file node or destination array" ); - - cvStartReadRawData( fs, src, &reader ); - cvReadRawDataSlice( fs, &reader, CV_NODE_IS_SEQ(src->tag) ? - src->data.seq->total : 1, data, dt ); -} - - -CV_IMPL void -cvWriteFileNode( CvFileStorage* fs, const char* new_node_name, - const CvFileNode* node, int embed ) -{ - CvFileStorage* dst = 0; - CV_CHECK_OUTPUT_FILE_STORAGE(fs); - - if( !node ) - return; - - if( CV_NODE_IS_COLLECTION(node->tag) && embed ) - { - icvWriteCollection( fs, node ); - } - else - { - icvWriteFileNode( fs, new_node_name, node ); - } - /* - int i, stream_count; - stream_count = fs->roots->total; - for( i = 0; i < stream_count; i++ ) - { - CvFileNode* node = (CvFileNode*)cvGetSeqElem( fs->roots, i, 0 ); - icvDumpCollection( dst, node ); - if( i < stream_count - 1 ) - dst->start_next_stream( dst ); - }*/ - cvReleaseFileStorage( &dst ); -} - - -CV_IMPL const char* -cvGetFileNodeName( const CvFileNode* file_node ) -{ - return file_node && CV_NODE_HAS_NAME(file_node->tag) ? - ((CvFileMapNode*)file_node)->key->str.ptr : 0; -} - - - -CV_IMPL void -cvRegisterType( const CvTypeInfo* _info ) -{ - CvTypeInfo* info = 0; - int i, len; - char c; - - //if( !CvType::first ) - // icvCreateStandardTypes(); - - if( !_info || _info->header_size != sizeof(CvTypeInfo) ) - CV_Error( CV_StsBadSize, "Invalid type info" ); - - if( !_info->is_instance || !_info->release || - !_info->read || !_info->write ) - CV_Error( CV_StsNullPtr, - "Some of required function pointers " - "(is_instance, release, read or write) are NULL"); - - c = _info->type_name[0]; - if( !cv_isalpha(c) && c != '_' ) - CV_Error( CV_StsBadArg, "Type name should start with a letter or _" ); - - len = (int)strlen(_info->type_name); - - for( i = 0; i < len; i++ ) - { - c = _info->type_name[i]; - if( !cv_isalnum(c) && c != '-' && c != '_' ) - CV_Error( CV_StsBadArg, - "Type name should contain only letters, digits, - and _" ); - } - - info = (CvTypeInfo*)cvAlloc( sizeof(*info) + len + 1 ); - - *info = *_info; - info->type_name = (char*)(info + 1); - memcpy( (char*)info->type_name, _info->type_name, len + 1 ); - - info->flags = 0; - info->next = CvType::first; - info->prev = 0; - if( CvType::first ) - CvType::first->prev = info; - else - CvType::last = info; - CvType::first = info; -} - - -CV_IMPL void -cvUnregisterType( const char* type_name ) -{ - CvTypeInfo* info; - - info = cvFindType( type_name ); - if( info ) - { - if( info->prev ) - info->prev->next = info->next; - else - CvType::first = info->next; - - if( info->next ) - info->next->prev = info->prev; - else - CvType::last = info->prev; - - if( !CvType::first || !CvType::last ) - CvType::first = CvType::last = 0; - - cvFree( &info ); - } -} - - -CV_IMPL CvTypeInfo* -cvFirstType( void ) -{ - return CvType::first; -} - - -CV_IMPL CvTypeInfo* -cvFindType( const char* type_name ) -{ - CvTypeInfo* info = 0; - - if (type_name) - for( info = CvType::first; info != 0; info = info->next ) - if( strcmp( info->type_name, type_name ) == 0 ) - break; - - return info; -} - - -CV_IMPL CvTypeInfo* -cvTypeOf( const void* struct_ptr ) -{ - CvTypeInfo* info = 0; - - if( struct_ptr ) - { - for( info = CvType::first; info != 0; info = info->next ) - if( info->is_instance( struct_ptr )) - break; - } - - return info; -} - - -/* universal functions */ -CV_IMPL void -cvRelease( void** struct_ptr ) -{ - CvTypeInfo* info; - - if( !struct_ptr ) - CV_Error( CV_StsNullPtr, "NULL double pointer" ); - - if( *struct_ptr ) - { - info = cvTypeOf( *struct_ptr ); - if( !info ) - CV_Error( CV_StsError, "Unknown object type" ); - if( !info->release ) - CV_Error( CV_StsError, "release function pointer is NULL" ); - - info->release( struct_ptr ); - *struct_ptr = 0; - } -} - - -void* cvClone( const void* struct_ptr ) -{ - void* struct_copy = 0; - CvTypeInfo* info; - - if( !struct_ptr ) - CV_Error( CV_StsNullPtr, "NULL structure pointer" ); - - info = cvTypeOf( struct_ptr ); - if( !info ) - CV_Error( CV_StsError, "Unknown object type" ); - if( !info->clone ) - CV_Error( CV_StsError, "clone function pointer is NULL" ); - - struct_copy = info->clone( struct_ptr ); - return struct_copy; -} - - -/* reads matrix, image, sequence, graph etc. */ -CV_IMPL void* -cvRead( CvFileStorage* fs, CvFileNode* node, CvAttrList* list ) -{ - void* obj = 0; - CV_CHECK_FILE_STORAGE( fs ); - - if( !node ) - return 0; - - if( !CV_NODE_IS_USER(node->tag) || !node->info ) - CV_Error( CV_StsError, "The node does not represent a user object (unknown type?)" ); - - obj = node->info->read( fs, node ); - if( list ) - *list = cvAttrList(0,0); - - return obj; -} - - -/* writes matrix, image, sequence, graph etc. */ -CV_IMPL void -cvWrite( CvFileStorage* fs, const char* name, - const void* ptr, CvAttrList attributes ) -{ - CvTypeInfo* info; - - CV_CHECK_OUTPUT_FILE_STORAGE( fs ); - - if( !ptr ) - CV_Error( CV_StsNullPtr, "Null pointer to the written object" ); - - info = cvTypeOf( ptr ); - if( !info ) - CV_Error( CV_StsBadArg, "Unknown object" ); - - if( !info->write ) - CV_Error( CV_StsBadArg, "The object does not have write function" ); - - info->write( fs, name, ptr, attributes ); -} - - -/* simple API for reading/writing data */ -CV_IMPL void -cvSave( const char* filename, const void* struct_ptr, - const char* _name, const char* comment, CvAttrList attributes ) -{ - CvFileStorage* fs = 0; - - if( !struct_ptr ) - CV_Error( CV_StsNullPtr, "NULL object pointer" ); - - fs = cvOpenFileStorage( filename, 0, CV_STORAGE_WRITE ); - if( !fs ) - CV_Error( CV_StsError, "Could not open the file storage. Check the path and permissions" ); - - cv::String name = _name ? cv::String(_name) : cv::FileStorage::getDefaultObjectName(filename); - - if( comment ) - cvWriteComment( fs, comment, 0 ); - cvWrite( fs, name.c_str(), struct_ptr, attributes ); - cvReleaseFileStorage( &fs ); -} diff --git a/modules/core/src/persistence_cpp.cpp b/modules/core/src/persistence_cpp.cpp deleted file mode 100644 index dc94c09..0000000 --- a/modules/core/src/persistence_cpp.cpp +++ /dev/null @@ -1,689 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html - - -#include "precomp.hpp" -#include "persistence.hpp" - -///////////////////////// new C++ interface for CvFileStorage /////////////////////////// - -namespace cv -{ - -static void getElemSize( const String& fmt, size_t& elemSize, size_t& cn ) -{ - const char* dt = fmt.c_str(); - cn = 1; - if( cv_isdigit(dt[0]) ) - { - cn = dt[0] - '0'; - dt++; - } - char c = dt[0]; - elemSize = cn*(c == 'u' || c == 'c' ? sizeof(uchar) : c == 'w' || c == 's' ? sizeof(ushort) : - c == 'i' ? sizeof(int) : c == 'f' ? sizeof(float) : c == 'd' ? sizeof(double) : - c == 'r' ? sizeof(void*) : (size_t)0); -} - -FileStorage::FileStorage() -{ - state = UNDEFINED; -} - -FileStorage::FileStorage(const String& filename, int flags, const String& encoding) -{ - state = UNDEFINED; - open( filename, flags, encoding ); -} - -FileStorage::FileStorage(CvFileStorage* _fs, bool owning) -{ - if (owning) fs.reset(_fs); - else fs = Ptr(Ptr(), _fs); - - state = _fs ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED; -} - -FileStorage::~FileStorage() -{ - while( structs.size() > 0 ) - { - cvEndWriteStruct(fs); - structs.pop_back(); - } -} - -bool FileStorage::open(const String& filename, int flags, const String& encoding) -{ - CV_INSTRUMENT_REGION(); - - release(); - fs.reset(cvOpenFileStorage( filename.c_str(), 0, flags, - !encoding.empty() ? encoding.c_str() : 0)); - bool ok = isOpened(); - state = ok ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED; - return ok; -} - -bool FileStorage::isOpened() const -{ - return fs && fs->is_opened; -} - -void FileStorage::release() -{ - fs.release(); - structs.clear(); - state = UNDEFINED; -} - -String FileStorage::releaseAndGetString() -{ - String buf; - if( fs && fs->outbuf ) - icvClose(fs, &buf); - - release(); - return buf; -} - -FileNode FileStorage::root(int streamidx) const -{ - return isOpened() ? FileNode(fs, cvGetRootFileNode(fs, streamidx)) : FileNode(); -} - -int FileStorage::getFormat() const -{ - CV_Assert(!fs.empty()); - return fs->fmt & FORMAT_MASK; -} - -FileStorage& operator << (FileStorage& fs, const String& str) -{ - CV_TRACE_REGION_VERBOSE(); - - enum { NAME_EXPECTED = FileStorage::NAME_EXPECTED, - VALUE_EXPECTED = FileStorage::VALUE_EXPECTED, - INSIDE_MAP = FileStorage::INSIDE_MAP }; - const char* _str = str.c_str(); - if( !fs.isOpened() || !_str ) - return fs; - if( *_str == '}' || *_str == ']' ) - { - if( fs.structs.empty() ) - CV_Error_( CV_StsError, ("Extra closing '%c'", *_str) ); - if( (*_str == ']' ? '[' : '{') != fs.structs.back() ) - CV_Error_( CV_StsError, - ("The closing '%c' does not match the opening '%c'", *_str, fs.structs.back())); - fs.structs.pop_back(); - fs.state = fs.structs.empty() || fs.structs.back() == '{' ? - INSIDE_MAP + NAME_EXPECTED : VALUE_EXPECTED; - cvEndWriteStruct( *fs ); - fs.elname = String(); - } - else if( fs.state == NAME_EXPECTED + INSIDE_MAP ) - { - if (!cv_isalpha(*_str) && *_str != '_') - CV_Error_( CV_StsError, ("Incorrect element name %s", _str) ); - fs.elname = str; - fs.state = VALUE_EXPECTED + INSIDE_MAP; - } - else if( (fs.state & 3) == VALUE_EXPECTED ) - { - if( *_str == '{' || *_str == '[' ) - { - fs.structs.push_back(*_str); - int flags = *_str++ == '{' ? CV_NODE_MAP : CV_NODE_SEQ; - fs.state = flags == CV_NODE_MAP ? INSIDE_MAP + - NAME_EXPECTED : VALUE_EXPECTED; - if( *_str == ':' ) - { - flags |= CV_NODE_FLOW; - _str++; - } - cvStartWriteStruct( *fs, fs.elname.size() > 0 ? fs.elname.c_str() : 0, - flags, *_str ? _str : 0 ); - fs.elname = String(); - } - else - { - write( fs, fs.elname, (_str[0] == '\\' && (_str[1] == '{' || _str[1] == '}' || - _str[1] == '[' || _str[1] == ']')) ? String(_str+1) : str ); - if( fs.state == INSIDE_MAP + VALUE_EXPECTED ) - fs.state = INSIDE_MAP + NAME_EXPECTED; - } - } - else - CV_Error( CV_StsError, "Invalid fs.state" ); - return fs; -} - - -void FileStorage::writeRaw( const String& fmt, const uchar* vec, size_t len ) -{ - if( !isOpened() ) - return; - size_t elemSize, cn; - getElemSize( fmt, elemSize, cn ); - CV_Assert( len % elemSize == 0 ); - cvWriteRawData( fs, vec, (int)(len/elemSize), fmt.c_str()); -} - - -void FileStorage::writeObj( const String& name, const void* obj ) -{ - if( !isOpened() ) - return; - cvWrite( fs, name.size() > 0 ? name.c_str() : 0, obj ); -} - - -void FileStorage::write( const String& name, int val ) -{ - *this << name << val; -} - -void FileStorage::write( const String& name, double val ) -{ - *this << name << val; -} - -void FileStorage::write( const String& name, const String& val ) -{ - *this << name << val; -} - -void FileStorage::write( const String& name, const Mat& val ) -{ - *this << name << val; -} - -void FileStorage::write( const String& name, const std::vector& val ) -{ - *this << name << val; -} - -void FileStorage::writeComment( const String& comment, bool append ) -{ - cvWriteComment(fs, comment.c_str(), append ? 1 : 0); -} - -String FileStorage::getDefaultObjectName(const String& _filename) -{ - static const char* stubname = "unnamed"; - const char* filename = _filename.c_str(); - const char* ptr2 = filename + _filename.size(); - const char* ptr = ptr2 - 1; - cv::AutoBuffer name_buf(_filename.size()+1); - - while( ptr >= filename && *ptr != '\\' && *ptr != '/' && *ptr != ':' ) - { - if( *ptr == '.' && (!*ptr2 || strncmp(ptr2, ".gz", 3) == 0) ) - ptr2 = ptr; - ptr--; - } - ptr++; - if( ptr == ptr2 ) - CV_Error( CV_StsBadArg, "Invalid filename" ); - - char* name = name_buf.data(); - - // name must start with letter or '_' - if( !cv_isalpha(*ptr) && *ptr!= '_' ){ - *name++ = '_'; - } - - while( ptr < ptr2 ) - { - char c = *ptr++; - if( !cv_isalnum(c) && c != '-' && c != '_' ) - c = '_'; - *name++ = c; - } - *name = '\0'; - name = name_buf.data(); - if( strcmp( name, "_" ) == 0 ) - strcpy( name, stubname ); - return String(name); -} - -FileNode FileStorage::operator[](const String& nodename) const -{ - return FileNode(fs, cvGetFileNodeByName(fs, 0, nodename.c_str())); -} - -FileNode FileStorage::operator[](const char* nodename) const -{ - return FileNode(fs, cvGetFileNodeByName(fs, 0, nodename)); -} - -FileNode FileNode::operator[](const String& nodename) const -{ - return FileNode(fs, cvGetFileNodeByName(fs, node, nodename.c_str())); -} - -FileNode FileNode::operator[](const char* nodename) const -{ - return FileNode(fs, cvGetFileNodeByName(fs, node, nodename)); -} - -FileNode FileNode::operator[](int i) const -{ - return isSeq() ? FileNode(fs, (CvFileNode*)cvGetSeqElem(node->data.seq, i)) : - i == 0 ? *this : FileNode(); -} - -std::vector FileNode::keys() const -{ - std::vector res; - if (isMap()) - { - res.reserve(size()); - for (FileNodeIterator it = begin(); it != end(); ++it) - { - res.push_back((*it).name()); - } - } - return res; -} - -String FileNode::name() const -{ - const char* str; - return !node || (str = cvGetFileNodeName(node)) == 0 ? String() : String(str); -} - -void* FileNode::readObj() const -{ - if( !fs || !node ) - return 0; - return cvRead( (CvFileStorage*)fs, (CvFileNode*)node ); -} - -static const FileNodeIterator::SeqReader emptyReader = {0, 0, 0, 0, 0, 0, 0, 0}; - -FileNodeIterator::FileNodeIterator() -{ - fs = 0; - container = 0; - reader = emptyReader; - remaining = 0; -} - -FileNodeIterator::FileNodeIterator(const CvFileStorage* _fs, - const CvFileNode* _node, size_t _ofs) -{ - reader = emptyReader; - if( _fs && _node && CV_NODE_TYPE(_node->tag) != CV_NODE_NONE ) - { - int node_type = _node->tag & FileNode::TYPE_MASK; - fs = _fs; - container = _node; - if( !(_node->tag & FileNode::USER) && (node_type == FileNode::SEQ || node_type == FileNode::MAP) ) - { - cvStartReadSeq( _node->data.seq, (CvSeqReader*)&reader ); - remaining = FileNode(_fs, _node).size(); - } - else - { - reader.ptr = (schar*)_node; - reader.seq = 0; - remaining = 1; - } - (*this) += (int)_ofs; - } - else - { - fs = 0; - container = 0; - remaining = 0; - } -} - -FileNodeIterator::FileNodeIterator(const FileNodeIterator& it) -{ - fs = it.fs; - container = it.container; - reader = it.reader; - remaining = it.remaining; -} - -FileNodeIterator& FileNodeIterator::operator ++() -{ - if( remaining > 0 ) - { - if( reader.seq ) - { - if( ((reader).ptr += (((CvSeq*)reader.seq)->elem_size)) >= (reader).block_max ) - { - cvChangeSeqBlock( (CvSeqReader*)&(reader), 1 ); - } - } - remaining--; - } - return *this; -} - -FileNodeIterator FileNodeIterator::operator ++(int) -{ - FileNodeIterator it = *this; - ++(*this); - return it; -} - -FileNodeIterator& FileNodeIterator::operator --() -{ - if( remaining < FileNode(fs, container).size() ) - { - if( reader.seq ) - { - if( ((reader).ptr -= (((CvSeq*)reader.seq)->elem_size)) < (reader).block_min ) - { - cvChangeSeqBlock( (CvSeqReader*)&(reader), -1 ); - } - } - remaining++; - } - return *this; -} - -FileNodeIterator FileNodeIterator::operator --(int) -{ - FileNodeIterator it = *this; - --(*this); - return it; -} - -FileNodeIterator& FileNodeIterator::operator += (int ofs) -{ - if( ofs == 0 ) - return *this; - if( ofs > 0 ) - ofs = std::min(ofs, (int)remaining); - else - { - size_t count = FileNode(fs, container).size(); - ofs = (int)(remaining - std::min(remaining - ofs, count)); - } - remaining -= ofs; - if( reader.seq ) - cvSetSeqReaderPos( (CvSeqReader*)&reader, ofs, 1 ); - return *this; -} - -FileNodeIterator& FileNodeIterator::operator -= (int ofs) -{ - return operator += (-ofs); -} - - -FileNodeIterator& FileNodeIterator::readRaw( const String& fmt, uchar* vec, size_t maxCount ) -{ - if( fs && container && remaining > 0 ) - { - size_t elem_size, cn; - getElemSize( fmt, elem_size, cn ); - CV_Assert( elem_size > 0 ); - size_t count = std::min(remaining, maxCount); - - if( reader.seq ) - { - cvReadRawDataSlice( fs, (CvSeqReader*)&reader, (int)count, vec, fmt.c_str() ); - remaining -= count*cn; - } - else - { - cvReadRawData( fs, container, vec, fmt.c_str() ); - remaining = 0; - } - } - return *this; -} - - -void write( FileStorage& fs, const String& name, int value ) -{ cvWriteInt( *fs, name.size() ? name.c_str() : 0, value ); } - -void write( FileStorage& fs, const String& name, float value ) -{ cvWriteReal( *fs, name.size() ? name.c_str() : 0, value ); } - -void write( FileStorage& fs, const String& name, double value ) -{ cvWriteReal( *fs, name.size() ? name.c_str() : 0, value ); } - -void write( FileStorage& fs, const String& name, const String& value ) -{ cvWriteString( *fs, name.size() ? name.c_str() : 0, value.c_str() ); } - -void writeScalar(FileStorage& fs, int value ) -{ cvWriteInt( *fs, 0, value ); } - -void writeScalar(FileStorage& fs, float value ) -{ cvWriteReal( *fs, 0, value ); } - -void writeScalar(FileStorage& fs, double value ) -{ cvWriteReal( *fs, 0, value ); } - -void writeScalar(FileStorage& fs, const String& value ) -{ cvWriteString( *fs, 0, value.c_str() ); } - - -void write( FileStorage& fs, const String& name, const Mat& value ) -{ - if( value.dims <= 2 ) - { - CvMat mat = cvMat(value); - cvWrite( *fs, name.size() ? name.c_str() : 0, &mat ); - } - else - { - CvMatND mat = cvMatND(value); - cvWrite( *fs, name.size() ? name.c_str() : 0, &mat ); - } -} - -// TODO: the 4 functions below need to be implemented more efficiently -void write( FileStorage& fs, const String& name, const SparseMat& value ) -{ - Ptr mat(cvCreateSparseMat(value)); - cvWrite( *fs, name.size() ? name.c_str() : 0, mat ); -} - - -internal::WriteStructContext::WriteStructContext(FileStorage& _fs, - const String& name, int flags, const String& typeName) : fs(&_fs) -{ - cvStartWriteStruct(**fs, !name.empty() ? name.c_str() : 0, flags, - !typeName.empty() ? typeName.c_str() : 0); - fs->elname = String(); - if ((flags & FileNode::TYPE_MASK) == FileNode::SEQ) - { - fs->state = FileStorage::VALUE_EXPECTED; - fs->structs.push_back('['); - } - else - { - fs->state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP; - fs->structs.push_back('{'); - } -} - -internal::WriteStructContext::~WriteStructContext() -{ - cvEndWriteStruct(**fs); - fs->structs.pop_back(); - fs->state = fs->structs.empty() || fs->structs.back() == '{' ? - FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP : - FileStorage::VALUE_EXPECTED; - fs->elname = String(); -} - - -void read( const FileNode& node, Mat& mat, const Mat& default_mat ) -{ - if( node.empty() ) - { - default_mat.copyTo(mat); - return; - } - void* obj = cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node); - if(CV_IS_MAT_HDR_Z(obj)) - { - cvarrToMat(obj).copyTo(mat); - cvReleaseMat((CvMat**)&obj); - } - else if(CV_IS_MATND_HDR(obj)) - { - cvarrToMat(obj).copyTo(mat); - cvReleaseMatND((CvMatND**)&obj); - } - else - { - cvRelease(&obj); - CV_Error(CV_StsBadArg, "Unknown array type"); - } -} - -void read( const FileNode& node, SparseMat& mat, const SparseMat& default_mat ) -{ - if( node.empty() ) - { - default_mat.copyTo(mat); - return; - } - Ptr m((CvSparseMat*)cvRead((CvFileStorage*)node.fs, (CvFileNode*)*node)); - CV_Assert(CV_IS_SPARSE_MAT(m.get())); - m->copyToSparseMat(mat); -} - -CV_EXPORTS void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value) -{ - if( node.empty() ) - { - value = default_value; - return; - } - node >> value; -} - -CV_EXPORTS void read(const FileNode& node, DMatch& value, const DMatch& default_value) -{ - if( node.empty() ) - { - value = default_value; - return; - } - node >> value; -} - -#ifdef CV__LEGACY_PERSISTENCE -void write( FileStorage& fs, const String& name, const std::vector& vec) -{ - // from template implementation - cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ); - write(fs, vec); -} - -void read(const FileNode& node, std::vector& keypoints) -{ - FileNode first_node = *(node.begin()); - 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(); - FileNodeIterator it = node.begin(), it_end = node.end(); - for( ; it != it_end; ) - { - KeyPoint kpt; - it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id; - keypoints.push_back(kpt); - } -} - -void write( FileStorage& fs, const String& name, const std::vector& vec) -{ - // from template implementation - cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ); - write(fs, vec); -} - -void read(const FileNode& node, std::vector& matches) -{ - FileNode first_node = *(node.begin()); - 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(); - FileNodeIterator it = node.begin(), it_end = node.end(); - for( ; it != it_end; ) - { - DMatch m; - it >> m.queryIdx >> m.trainIdx >> m.imgIdx >> m.distance; - matches.push_back(m); - } -} -#endif - -int FileNode::type() const { return !node ? NONE : (node->tag & TYPE_MASK); } -bool FileNode::isNamed() const { return !node ? false : (node->tag & NAMED) != 0; } - -size_t FileNode::size() const -{ - int t = type(); - return t == MAP ? (size_t)((CvSet*)node->data.map)->active_count : - t == SEQ ? (size_t)node->data.seq->total : (size_t)!isNone(); -} - -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 : 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) ? 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 : std::numeric_limits::max(); -} - -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; -} - -} // cv:: diff --git a/modules/core/src/persistence_json.cpp b/modules/core/src/persistence_json.cpp index abbd292..ace7394 100644 --- a/modules/core/src/persistence_json.cpp +++ b/modules/core/src/persistence_json.cpp @@ -2,909 +2,859 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html - #include "precomp.hpp" #include "persistence.hpp" -/****************************************************************************************\ -* JSON Parser * -\****************************************************************************************/ - -static char* -icvJSONSkipSpaces( CvFileStorage* fs, char* ptr ) +namespace cv { - bool is_eof = false; - bool is_completed = false; - while ( is_eof == false && is_completed == false ) +class JSONEmitter : public FileStorageEmitter +{ +public: + JSONEmitter(FileStorage_API* _fs) : fs(_fs) { - switch ( *ptr ) - { - /* comment */ - case '/' : { - ptr++; - if ( *ptr == '\0' ) - { - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) { is_eof = true; break; } - } - - if ( *ptr == '/' ) - { - while ( *ptr != '\n' && *ptr != '\r' ) - { - if ( *ptr == '\0' ) - { - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) { is_eof = true; break; } - } - else - { - ptr++; - } - } - } - else if ( *ptr == '*' ) - { - ptr++; - for (;;) - { - if ( *ptr == '\0' ) - { - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) { is_eof = true; break; } - } - else if ( *ptr == '*' ) - { - ptr++; - if ( *ptr == '\0' ) - { - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) { is_eof = true; break; } - } - if ( *ptr == '/' ) - { - ptr++; - break; - } - } - else - { - ptr++; - } - } - } - else - { - CV_PARSE_ERROR( "Not supported escape character" ); - } - } break; - /* whitespace */ - case '\t': - case ' ' : { - ptr++; - } break; - /* newline || end mark */ - case '\0': - case '\n': - case '\r': { - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) { is_eof = true; break; } - } break; - /* other character */ - default: { - if ( !cv_isprint(*ptr) ) - CV_PARSE_ERROR( "Invalid character in the stream" ); - is_completed = true; - } break; - } } + virtual ~JSONEmitter() {} - if ( is_eof ) - { - ptr = fs->buffer_start; - *ptr = '\0'; - fs->dummy_eof = 1; - } - else if ( !is_completed ) + FStructData startWriteStruct( const FStructData& parent, const char* key, + int struct_flags, const char* type_name=0 ) { - /* should not be executed */ - ptr = 0; - fs->dummy_eof = 1; - CV_PARSE_ERROR( "Abort at parse time" ); - } - return ptr; -} + char data[CV_FS_MAX_LEN + 1024]; + struct_flags = (struct_flags & (FileNode::TYPE_MASK|FileNode::FLOW)) | FileNode::EMPTY; + if( !FileNode::isCollection(struct_flags)) + CV_Error( CV_StsBadArg, + "Some collection type - FileNode::SEQ or FileNode::MAP, must be specified" ); -static char* icvJSONParseKey( CvFileStorage* fs, char* ptr, CvFileNode* map, CvFileNode** value_placeholder ) -{ - if( *ptr != '"' ) - CV_PARSE_ERROR( "Key must start with \'\"\'" ); + if( type_name && *type_name == '\0' ) + type_name = 0; - char * beg = ptr + 1; + bool is_real_collection = true; + if (type_name && memcmp(type_name, "binary", 6) == 0) + { + struct_flags = FileNode::STR; + data[0] = '\0'; + is_real_collection = false; + } - do { - ++ptr; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - } while( cv_isprint(*ptr) && *ptr != '"' ); + if ( is_real_collection ) + { + char c = FileNode::isMap(struct_flags) ? '{' : '['; + data[0] = c; + data[1] = '\0'; + } - if( *ptr != '"' ) - CV_PARSE_ERROR( "Key must end with \'\"\'" ); + writeScalar( key, data ); + FStructData current_struct("", struct_flags, parent.indent + 4); - const char * end = ptr; - ptr++; - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - return 0; + return current_struct; + } - if( *ptr != ':' ) - CV_PARSE_ERROR( "Missing \':\' between key and value" ); + void endWriteStruct(const FStructData& current_struct) + { + int struct_flags = current_struct.flags; + CV_Assert( FileNode::isCollection(struct_flags) ); - /* [beg, end) */ - if( end <= beg ) - CV_PARSE_ERROR( "Key is empty" ); + if( !FileNode::isFlow(struct_flags) ) + { +#if 0 + if ( fs->bufferPtr() <= fs->bufferStart() + fs->space ) + { + /* some bad code for base64_writer... */ + ptr = fs->bufferPtr(); + *ptr++ = '\n'; + *ptr++ = '\0'; + fs->puts( fs->bufferStart() ); + fs->setBufferPtr(fs->bufferStart()); + } +#endif + fs->flush(); + } - if ( end - beg == 7u && memcmp(beg, "type_id", 7u) == 0 ) - { - *value_placeholder = 0; + char* ptr = fs->bufferPtr(); + if( ptr > fs->bufferStart() + current_struct.indent && !FileNode::FileNode::isEmptyCollection(struct_flags) ) + *ptr++ = ' '; + *ptr++ = FileNode::isMap(struct_flags) ? '}' : ']'; + fs->setBufferPtr(ptr); } - else + + void write(const char* key, int value) { - CvStringHashNode* str_hash_node = cvGetHashedKey( fs, beg, static_cast(end - beg), 1 ); - *value_placeholder = cvGetFileNode( fs, map, str_hash_node, 1 ); + char buf[128]; + writeScalar( key, fs::itoa( value, buf, 10 )); } - ptr++; - return ptr; -} - -static char* icvJSONParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node ) -{ - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - CV_PARSE_ERROR( "Unexpected End-Of-File" ); - - memset( node, 0, sizeof(*node) ); + void write( const char* key, double value ) + { + char buf[128]; + writeScalar( key, fs::doubleToString( buf, value, true )); + } - if ( *ptr == '"' ) - { /* must be string or Base64 string */ - ptr++; - char * beg = ptr; - size_t len = 0u; - for ( ; (cv_isalnum(*ptr) || *ptr == '$' ) && len <= 9u; ptr++ ) - len++; + void write(const char* key, const char* str, bool quote) + { + char buf[CV_FS_MAX_LEN*4+16]; + char* data = (char*)str; + int i, len; - if ( len >= 8u && memcmp( beg, "$base64$", 8u ) == 0 ) - { /**************** Base64 string ****************/ - ptr = beg += 8; + if( !str ) + CV_Error( CV_StsNullPtr, "Null string pointer" ); - std::string base64_buffer; - base64_buffer.reserve( PARSER_BASE64_BUFFER_SIZE ); + len = (int)strlen(str); + if( len > CV_FS_MAX_LEN ) + CV_Error( CV_StsBadArg, "The written string is too long" ); - bool is_matching = false; - while ( !is_matching ) + if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') ) + { + int need_quote = 1; + data = buf; + *data++ = '\"'; + for( i = 0; i < len; i++ ) { - switch ( *ptr ) - { - case '\0': - { - base64_buffer.append( beg, ptr ); + char c = str[i]; - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) - CV_PARSE_ERROR( "'\"' - right-quote of string is missing" ); - - beg = ptr; - break; - } - case '\"': + switch ( c ) { - base64_buffer.append( beg, ptr ); - beg = ptr; - is_matching = true; - break; - } - case '\n': - case '\r': - { - CV_PARSE_ERROR( "'\"' - right-quote of string is missing" ); - break; - } - default: - { - ptr++; - break; - } + case '\\': + case '\"': + case '\'': { *data++ = '\\'; *data++ = c; break; } + case '\n': { *data++ = '\\'; *data++ = 'n'; break; } + case '\r': { *data++ = '\\'; *data++ = 'r'; break; } + case '\t': { *data++ = '\\'; *data++ = 't'; break; } + case '\b': { *data++ = '\\'; *data++ = 'b'; break; } + case '\f': { *data++ = '\\'; *data++ = 'f'; break; } + default : { *data++ = c; } } } - if ( *ptr != '\"' ) - CV_PARSE_ERROR( "'\"' - right-quote of string is missing" ); - else - ptr++; + *data++ = '\"'; + *data++ = '\0'; + data = buf + !need_quote; + } - if ( base64_buffer.size() >= base64::ENCODED_HEADER_SIZE ) - { - const char * base64_beg = base64_buffer.data(); - const char * base64_end = base64_beg + base64_buffer.size(); + writeScalar( key, data); + } - /* get dt from header */ - std::string dt; - { - std::vector header(base64::HEADER_SIZE + 1, ' '); - base64::base64_decode(base64_beg, header.data(), 0U, base64::ENCODED_HEADER_SIZE); - if ( !base64::read_base64_header(header, dt) || dt.empty() ) - CV_PARSE_ERROR("Invalid `dt` in Base64 header"); - } + void writeScalar(const char* key, const char* data) + { + size_t key_len = 0u; + if( key && *key == '\0' ) + key = 0; + if ( key ) + { + key_len = strlen(key); + if ( key_len == 0u ) + CV_Error( CV_StsBadArg, "The key is an empty" ); + else if ( static_cast(key_len) > CV_FS_MAX_LEN ) + CV_Error( CV_StsBadArg, "The key is too long" ); + } + size_t data_len = 0u; + if ( data ) + data_len = strlen(data); - if ( base64_buffer.size() > base64::ENCODED_HEADER_SIZE ) - { - /* set base64_beg to beginning of base64 data */ - base64_beg = &base64_buffer.at( base64::ENCODED_HEADER_SIZE ); - if ( !base64::base64_valid( base64_beg, 0U, base64_end - base64_beg ) ) - CV_PARSE_ERROR( "Invalid Base64 data." ); - - /* buffer for decoded data(exclude header) */ - std::vector binary_buffer( base64::base64_decode_buffer_size(base64_end - base64_beg) ); - int total_byte_size = static_cast( - base64::base64_decode_buffer_size( base64_end - base64_beg, base64_beg, false ) - ); - { - base64::Base64ContextParser parser(binary_buffer.data(), binary_buffer.size() ); - const uchar * binary_beg = reinterpret_cast( base64_beg ); - const uchar * binary_end = binary_beg + (base64_end - base64_beg); - parser.read( binary_beg, binary_end ); - parser.flush(); - } + FStructData& current_struct = fs->getCurrentStruct(); + int struct_flags = current_struct.flags; + if( FileNode::isCollection(struct_flags) ) + { + if ( (FileNode::isMap(struct_flags) ^ (key != 0)) ) + CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, " + "or add element with key to sequence" ); + } else { + fs->setNonEmpty(); + struct_flags = FileNode::EMPTY | (key ? FileNode::MAP : FileNode::SEQ); + } - /* save as CvSeq */ - int elem_size = ::icvCalcStructSize(dt.c_str(), 0); - if (total_byte_size % elem_size != 0) - CV_PARSE_ERROR("Byte size not match elememt size"); - int elem_cnt = total_byte_size / elem_size; + // start to write + char* ptr = 0; - /* after icvFSCreateCollection, node->tag == struct_flags */ - icvFSCreateCollection(fs, CV_NODE_FLOW | CV_NODE_SEQ, node); - base64::make_seq(binary_buffer.data(), elem_cnt, dt.c_str(), *node->data.seq); - } - else - { - /* empty */ - icvFSCreateCollection(fs, CV_NODE_FLOW | CV_NODE_SEQ, node); - } - } - else if ( base64_buffer.empty() ) + if( FileNode::isFlow(struct_flags) ) + { + int new_offset; + ptr = fs->bufferPtr(); + if( !FileNode::FileNode::isEmptyCollection(struct_flags) ) + *ptr++ = ','; + new_offset = static_cast(ptr - fs->bufferStart() + key_len + data_len); + if( new_offset > fs->wrapMargin() && new_offset - current_struct.indent > 10 ) { - /* empty */ - icvFSCreateCollection(fs, CV_NODE_FLOW | CV_NODE_SEQ, node); + fs->setBufferPtr(ptr); + ptr = fs->flush(); } else + *ptr++ = ' '; + } + else + { + if ( !FileNode::FileNode::isEmptyCollection(struct_flags) ) { - CV_PARSE_ERROR("Unrecognized Base64 header"); + ptr = fs->bufferPtr(); + *ptr++ = ','; + *ptr++ = '\n'; + *ptr++ = '\0'; + fs->puts( fs->bufferStart() ); + fs->setBufferPtr(fs->bufferStart()); } + ptr = fs->flush(); } - else - { /**************** normal string ****************/ - std::string string_buffer; - string_buffer.reserve( PARSER_BASE64_BUFFER_SIZE ); - ptr = beg; - bool is_matching = false; - while ( !is_matching ) - { - switch ( *ptr ) - { - case '\\': - { - string_buffer.append( beg, ptr ); - ptr++; - switch ( *ptr ) - { - case '\\': - case '\"': - case '\'': { string_buffer.append( 1u, *ptr ); break; } - case 'n' : { string_buffer.append( 1u, '\n' ); break; } - case 'r' : { string_buffer.append( 1u, '\r' ); break; } - case 't' : { string_buffer.append( 1u, '\t' ); break; } - case 'b' : { string_buffer.append( 1u, '\b' ); break; } - case 'f' : { string_buffer.append( 1u, '\f' ); break; } - case 'u' : { CV_PARSE_ERROR( "'\\uXXXX' currently not supported" ); } - default : { CV_PARSE_ERROR( "Invalid escape character" ); } - break; - } - ptr++; - beg = ptr; - break; - } - case '\0': - { - string_buffer.append( beg, ptr ); + if( key ) + { + if( !cv_isalpha(key[0]) && key[0] != '_' ) + CV_Error( CV_StsBadArg, "Key must start with a letter or _" ); - ptr = icvGets( fs, fs->buffer_start, static_cast(fs->buffer_end - fs->buffer_start) ); - if ( !ptr ) - CV_PARSE_ERROR( "'\"' - right-quote of string is missing" ); + ptr = fs->resizeWriteBuffer( ptr, static_cast(key_len) ); + *ptr++ = '\"'; - beg = ptr; - break; - } - case '\"': - { - string_buffer.append( beg, ptr ); - beg = ptr; - is_matching = true; - break; - } - case '\n': - case '\r': - { - CV_PARSE_ERROR( "'\"' - right-quote of string is missing" ); - break; - } - default: - { - ptr++; - break; - } - } - } + for( size_t i = 0u; i < key_len; i++ ) + { + char c = key[i]; - if ( *ptr != '\"' ) - CV_PARSE_ERROR( "'\"' - right-quote of string is missing" ); - else - ptr++; + ptr[i] = c; + if( !cv_isalnum(c) && c != '-' && c != '_' && c != ' ' ) + CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" ); + } - node->data.str = cvMemStorageAllocString - ( - fs->memstorage, - string_buffer.c_str(), - static_cast(string_buffer.size()) - ); - node->tag = CV_NODE_STRING; - } - } - else if ( cv_isdigit(*ptr) || *ptr == '-' || *ptr == '+' || *ptr == '.' ) - { /**************** number ****************/ - char * beg = ptr; - if ( *ptr == '+' || *ptr == '-' ) - { - ptr++; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - } - while( cv_isdigit(*ptr) ) - { - ptr++; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - } - if (*ptr == '.' || *ptr == 'e') - { - node->data.f = icv_strtod( fs, beg, &ptr ); - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - node->tag = CV_NODE_REAL; - } - else - { - node->data.i = static_cast(strtol( beg, &ptr, 0 )); - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - node->tag = CV_NODE_INT; + ptr += key_len; + *ptr++ = '\"'; + *ptr++ = ':'; + *ptr++ = ' '; } - if ( beg >= ptr ) - CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" ); - } - else - { /**************** other data ****************/ - const char * beg = ptr; - size_t len = 0u; - for ( ; cv_isalpha(*ptr) && len <= 6u; ) + if( data ) { - len++; - ptr++; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); + ptr = fs->resizeWriteBuffer( ptr, static_cast(data_len) ); + memcpy( ptr, data, data_len ); + ptr += data_len; } - if ( len >= 4u && memcmp( beg, "null", 4u ) == 0 ) - { - CV_PARSE_ERROR( "Value 'null' is not supported by this parser" ); - } - else if ( len >= 4u && memcmp( beg, "true", 4u ) == 0 ) - { - node->data.i = 1; - node->tag = CV_NODE_INT; - } - else if ( len >= 5u && memcmp( beg, "false", 5u ) == 0 ) - { - node->data.i = 0; - node->tag = CV_NODE_INT; - } - else - { - CV_PARSE_ERROR( "Unrecognized value" ); - } + fs->setBufferPtr(ptr); + current_struct.flags &= ~FileNode::EMPTY; } - return ptr; -} - -static char* icvJSONParseSeq( CvFileStorage* fs, char* ptr, CvFileNode* node ); -static char* icvJSONParseMap( CvFileStorage* fs, char* ptr, CvFileNode* node ); - -static char* icvJSONParseSeq( CvFileStorage* fs, char* ptr, CvFileNode* node ) -{ - if (!ptr) - CV_PARSE_ERROR( "ptr is NULL" ); - - if ( *ptr != '[' ) - CV_PARSE_ERROR( "'[' - left-brace of seq is missing" ); - else - ptr++; + void writeComment(const char* comment, bool eol_comment) + { + if( !comment ) + CV_Error( CV_StsNullPtr, "Null comment" ); - memset( node, 0, sizeof(*node) ); - icvFSCreateCollection( fs, CV_NODE_SEQ, node ); + int len = static_cast(strlen(comment)); + char* ptr = fs->bufferPtr(); + const char* eol = strchr(comment, '\n'); + bool multiline = eol != 0; - for (;;) - { - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - break; + if( !eol_comment || multiline || fs->bufferEnd() - ptr < len || ptr == fs->bufferStart() ) + ptr = fs->flush(); + else + *ptr++ = ' '; - if ( *ptr != ']' ) + while( comment ) { - CvFileNode* child = (CvFileNode*)cvSeqPush( node->data.seq, 0 ); - - if ( *ptr == '[' ) - ptr = icvJSONParseSeq( fs, ptr, child ); - else if ( *ptr == '{' ) - ptr = icvJSONParseMap( fs, ptr, child ); + *ptr++ = '/'; + *ptr++ = '/'; + *ptr++ = ' '; + if( eol ) + { + ptr = fs->resizeWriteBuffer( ptr, (int)(eol - comment) + 1 ); + memcpy( ptr, comment, eol - comment + 1 ); + fs->setBufferPtr(ptr + (eol - comment)); + comment = eol + 1; + eol = strchr( comment, '\n' ); + } else - ptr = icvJSONParseValue( fs, ptr, child ); + { + len = (int)strlen(comment); + ptr = fs->resizeWriteBuffer( ptr, len ); + memcpy( ptr, comment, len ); + fs->setBufferPtr(ptr + len); + comment = 0; + } + ptr = fs->flush(); } - - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - break; - - if ( *ptr == ',' ) - ptr++; - else if ( *ptr == ']' ) - break; - else - CV_PARSE_ERROR( "Unexpected character" ); } - if (!ptr) - CV_PARSE_ERROR("ptr is NULL"); - - if ( *ptr != ']' ) - CV_PARSE_ERROR( "']' - right-brace of seq is missing" ); - else - ptr++; + void startNextStream() + { + fs->puts( "...\n" ); + fs->puts( "---\n" ); + } - return ptr; -} +protected: + FileStorage_API* fs; +}; -static char* icvJSONParseMap( CvFileStorage* fs, char* ptr, CvFileNode* node ) +class JSONParser : public FileStorageParser { - if (!ptr) - CV_PARSE_ERROR("ptr is NULL"); - - if ( *ptr != '{' ) - CV_PARSE_ERROR( "'{' - left-brace of map is missing" ); - else - ptr++; +public: + JSONParser(FileStorage_API* _fs) : fs(_fs) + { + } - memset( node, 0, sizeof(*node) ); - icvFSCreateCollection( fs, CV_NODE_MAP, node ); + virtual ~JSONParser() {} - for ( ;; ) + char* skipSpaces( char* ptr ) { - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - break; + bool is_eof = false; + bool is_completed = false; - if ( *ptr == '"' ) + while ( is_eof == false && is_completed == false ) { - CvFileNode* child = 0; - ptr = icvJSONParseKey( fs, ptr, node, &child ); - if ( ptr == 0 || fs->dummy_eof ) - break; - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - break; + switch ( *ptr ) + { + /* comment */ + case '/' : { + ptr++; + if ( *ptr == '\0' ) + { + ptr = fs->gets(); + if( !ptr || !*ptr ) { is_eof = true; break; } + } - if ( child == 0 ) - { /* type_id */ - CvFileNode tmp; - ptr = icvJSONParseValue( fs, ptr, &tmp ); - if ( CV_NODE_IS_STRING(tmp.tag) ) - { - node->info = cvFindType( tmp.data.str.ptr ); - if ( node->info ) - node->tag |= CV_NODE_USER; - // delete tmp.data.str - } - else - { - CV_PARSE_ERROR( "\"type_id\" should be of type string" ); - } - } - else - { /* normal */ - if ( *ptr == '[' ) - ptr = icvJSONParseSeq( fs, ptr, child ); - else if ( *ptr == '{' ) - ptr = icvJSONParseMap( fs, ptr, child ); - else - ptr = icvJSONParseValue( fs, ptr, child ); - child->tag |= CV_NODE_NAMED; + if ( *ptr == '/' ) + { + while ( *ptr != '\n' && *ptr != '\r' ) + { + if ( *ptr == '\0' ) + { + ptr = fs->gets(); + if( !ptr || !*ptr ) { is_eof = true; break; } + } + else + { + ptr++; + } + } + } + else if ( *ptr == '*' ) + { + ptr++; + for (;;) + { + if ( *ptr == '\0' ) + { + ptr = fs->gets(); + if( !ptr || !*ptr ) { is_eof = true; break; } + } + else if ( *ptr == '*' ) + { + ptr++; + if ( *ptr == '\0' ) + { + ptr = fs->gets(); + if( !ptr || !*ptr ) { is_eof = true; break; } + } + if ( *ptr == '/' ) + { + ptr++; + break; + } + } + else + { + ptr++; + } + } + } + else + { + CV_PARSE_ERROR_CPP( "Not supported escape character" ); + } + } break; + /* whitespace */ + case '\t': + case ' ' : { + ptr++; + } break; + /* newline || end mark */ + case '\0': + case '\n': + case '\r': { + ptr = fs->gets(); + if( !ptr || !*ptr ) { is_eof = true; break; } + } break; + /* other character */ + default: { + if( !cv_isprint(*ptr) ) + CV_PARSE_ERROR_CPP( "Invalid character in the stream" ); + is_completed = true; + } break; } } - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - break; + if ( is_eof || !is_completed ) + { + ptr = fs->bufferStart(); + *ptr = '\0'; + fs->setEof(); + if( !is_completed ) + CV_PARSE_ERROR_CPP( "Abort at parse time" ); + } - if ( *ptr == ',' ) - ptr++; - else if ( *ptr == '}' ) - break; - else - CV_PARSE_ERROR( "Unexpected character" ); + return ptr; } - if (!ptr) - CV_PARSE_ERROR("ptr is NULL"); - - if ( *ptr != '}' ) - CV_PARSE_ERROR( "'}' - right-brace of map is missing" ); - else - ptr++; - - return ptr; -} - + char* parseKey( char* ptr, FileNode& collection, FileNode& value_placeholder ) + { + if( *ptr != '"' ) + CV_PARSE_ERROR_CPP( "Key must start with \'\"\'" ); -void icvJSONParse( CvFileStorage* fs ) -{ - char* ptr = fs->buffer_start; - ptr = icvJSONSkipSpaces( fs, ptr ); - if ( ptr == 0 || fs->dummy_eof ) - return; + char * beg = ptr + 1; - if ( *ptr == '{' ) - { - CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 ); - icvJSONParseMap( fs, ptr, root_node ); - } - else if ( *ptr == '[' ) - { - CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 ); - icvJSONParseSeq( fs, ptr, root_node ); - } - else - { - CV_PARSE_ERROR( "left-brace of top level is missing" ); - } + do { + ++ptr; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } while( cv_isprint(*ptr) && *ptr != '"' ); - if ( fs->dummy_eof != 0 ) - CV_PARSE_ERROR( "Unexpected End-Of-File" ); -} + if( *ptr != '"' ) + CV_PARSE_ERROR_CPP( "Key must end with \'\"\'" ); + const char * end = ptr; + ptr++; + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + return 0; -/****************************************************************************************\ -* JSON Emitter * -\****************************************************************************************/ + if( *ptr != ':' ) + CV_PARSE_ERROR_CPP( "Missing \':\' between key and value" ); -void icvJSONWrite( CvFileStorage* fs, const char* key, const char* data ) -{ - /* check write_struct */ + /* [beg, end) */ + if( end <= beg ) + CV_PARSE_ERROR_CPP( "Key is empty" ); - check_if_write_struct_is_delayed( fs ); - if ( fs->state_of_writing_base64 == base64::fs::Uncertain ) - { - switch_to_Base64_state( fs, base64::fs::NotUse ); + value_placeholder = fs->addNode(collection, std::string(beg, (size_t)(end - beg)), FileNode::NONE); + return ++ptr; } - else if ( fs->state_of_writing_base64 == base64::fs::InUse ) + + bool getBase64Row(char*, int /*indent*/, char*&, char*&) { - CV_Error( CV_StsError, "At present, output Base64 data only." ); + CV_PARSE_ERROR_CPP("Currently, JSON parser does not support base64 data"); + return false; } - /* check parameters */ - - size_t key_len = 0u; - if( key && *key == '\0' ) - key = 0; - if ( key ) + char* parseValue( char* ptr, FileNode& node ) { - key_len = strlen(key); - if ( key_len == 0u ) - CV_Error( CV_StsBadArg, "The key is an empty" ); - else if ( static_cast(key_len) > CV_FS_MAX_LEN ) - CV_Error( CV_StsBadArg, "The key is too long" ); - } + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + CV_PARSE_ERROR_CPP( "Unexpected End-Of-File" ); - size_t data_len = 0u; - if ( data ) - data_len = strlen(data); + if( *ptr == '"' ) + { /* must be string or Base64 string */ + ptr++; + char * beg = ptr; + size_t len = 0u; + for ( ; (cv_isalnum(*ptr) || *ptr == '$' ) && len <= 9u; ptr++ ) + len++; + + if ( len >= 8u && memcmp( beg, "$base64$", 8u ) == 0 ) + { /**************** Base64 string ****************/ + CV_PARSE_ERROR_CPP("base64 data is not supported"); +#if 0 + ptr = beg += 8; + + std::string base64_buffer; + base64_buffer.reserve( PARSER_BASE64_BUFFER_SIZE ); + + bool is_matching = false; + while ( !is_matching ) + { + switch ( *ptr ) + { + case '\0': + { + base64_buffer.append( beg, ptr ); - int struct_flags = fs->struct_flags; - if( CV_NODE_IS_COLLECTION(struct_flags) ) - { - if ( (CV_NODE_IS_MAP(struct_flags) ^ (key != 0)) ) - CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, " - "or add element with key to sequence" ); - } else { - fs->is_first = 0; - struct_flags = CV_NODE_EMPTY | (key ? CV_NODE_MAP : CV_NODE_SEQ); - } + ptr = fs->gets(); + if( !ptr || !*ptr ) + CV_PARSE_ERROR_CPP( "'\"' - right-quote of string is missing" ); + beg = ptr; + break; + } + case '\"': + { + base64_buffer.append( beg, ptr ); + beg = ptr; + is_matching = true; + break; + } + case '\n': + case '\r': + { + CV_PARSE_ERROR_CPP( "'\"' - right-quote of string is missing" ); + break; + } + default: + { + ptr++; + break; + } + } + } - /* start to write */ + if ( *ptr != '\"' ) + CV_PARSE_ERROR_CPP( "'\"' - right-quote of string is missing" ); + else + ptr++; - char* ptr = 0; + if ( base64_buffer.size() >= base64::ENCODED_HEADER_SIZE ) + { + const char * base64_beg = base64_buffer.data(); + const char * base64_end = base64_beg + base64_buffer.size(); - if( CV_NODE_IS_FLOW(struct_flags) ) - { - int new_offset; - ptr = fs->buffer; - if( !CV_NODE_IS_EMPTY(struct_flags) ) - *ptr++ = ','; - new_offset = static_cast(ptr - fs->buffer_start + key_len + data_len); - if( new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10 ) - { - fs->buffer = ptr; - ptr = icvFSFlush(fs); - } - else - *ptr++ = ' '; - } - else - { - if ( !CV_NODE_IS_EMPTY(struct_flags) ) - { - ptr = fs->buffer; - *ptr++ = ','; - *ptr++ = '\n'; - *ptr++ = '\0'; - ::icvPuts( fs, fs->buffer_start ); - fs->buffer = fs->buffer_start; - } - ptr = icvFSFlush(fs); - } + /* get dt from header */ + std::string dt; + { + std::vector header(base64::HEADER_SIZE + 1, ' '); + base64::base64_decode(base64_beg, header.data(), 0U, base64::ENCODED_HEADER_SIZE); + if ( !base64::read_base64_header(header, dt) || dt.empty() ) + CV_PARSE_ERROR_CPP("Invalid `dt` in Base64 header"); + } - if( key ) - { - if( !cv_isalpha(key[0]) && key[0] != '_' ) - CV_Error( CV_StsBadArg, "Key must start with a letter or _" ); - ptr = icvFSResizeWriteBuffer( fs, ptr, static_cast(key_len) ); - *ptr++ = '\"'; + if ( base64_buffer.size() > base64::ENCODED_HEADER_SIZE ) + { + /* set base64_beg to beginning of base64 data */ + base64_beg = &base64_buffer.at( base64::ENCODED_HEADER_SIZE ); + if ( !base64::base64_valid( base64_beg, 0U, base64_end - base64_beg ) ) + CV_PARSE_ERROR_CPP( "Invalid Base64 data." ); + + /* buffer for decoded data(exclude header) */ + std::vector binary_buffer( base64::base64_decode_buffer_size(base64_end - base64_beg) ); + int total_byte_size = static_cast( + base64::base64_decode_buffer_size( base64_end - base64_beg, base64_beg, false ) + ); + { + base64::Base64ContextParser parser(binary_buffer.data(), binary_buffer.size() ); + const uchar * binary_beg = reinterpret_cast( base64_beg ); + const uchar * binary_end = binary_beg + (base64_end - base64_beg); + parser.read( binary_beg, binary_end ); + parser.flush(); + } - for( size_t i = 0u; i < key_len; i++ ) - { - char c = key[i]; + /* save as CvSeq */ + int elem_size = ::icvCalcStructSize(dt.c_str(), 0); + if (total_byte_size % elem_size != 0) + CV_PARSE_ERROR_CPP("Byte size not match elememt size"); + int elem_cnt = total_byte_size / elem_size; - ptr[i] = c; - if( !cv_isalnum(c) && c != '-' && c != '_' && c != ' ' ) - CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" ); - } + /* after icvFSCreateCollection, node->tag == struct_flags */ + icvFSCreateCollection(fs, FileNode::FLOW | FileNode::SEQ, node); + base64::make_seq(binary_buffer.data(), elem_cnt, dt.c_str(), *node->data.seq); + } + else + { + /* empty */ + icvFSCreateCollection(fs, FileNode::FLOW | FileNode::SEQ, node); + } + } + else if ( base64_buffer.empty() ) + { + /* empty */ + icvFSCreateCollection(fs, FileNode::FLOW | FileNode::SEQ, node); + } + else + { + CV_PARSE_ERROR("Unrecognized Base64 header"); + } +#endif + } + else + { /**************** normal string ****************/ + int i = 0, sz; - ptr += key_len; - *ptr++ = '\"'; - *ptr++ = ':'; - *ptr++ = ' '; - } + ptr = beg; + bool is_matching = false; + while ( !is_matching ) + { + switch ( *ptr ) + { + case '\\': + { + sz = (int)(ptr - beg); + if( sz > 0 ) + { + memcpy(buf + i, beg, sz); + i += sz; + } + ptr++; + switch ( *ptr ) + { + case '\\': + case '\"': + case '\'': { buf[i++] = *ptr; break; } + case 'n' : { buf[i++] = '\n'; break; } + case 'r' : { buf[i++] = '\r'; break; } + case 't' : { buf[i++] = '\t'; break; } + case 'b' : { buf[i++] = '\b'; break; } + case 'f' : { buf[i++] = '\f'; break; } + case 'u' : { CV_PARSE_ERROR_CPP( "'\\uXXXX' currently not supported" ); break; } + default : { CV_PARSE_ERROR_CPP( "Invalid escape character" ); } + break; + } + ptr++; + beg = ptr; + break; + } + case '\0': + { + sz = (int)(ptr - beg); + if( sz > 0 ) + { + memcpy(buf + i, beg, sz); + i += sz; + } + ptr = fs->gets(); + if ( !ptr || !*ptr ) + CV_PARSE_ERROR_CPP( "'\"' - right-quote of string is missing" ); + + beg = ptr; + break; + } + case '\"': + { + sz = (int)(ptr - beg); + if( sz > 0 ) + { + memcpy(buf + i, beg, sz); + i += sz; + } + beg = ptr; + is_matching = true; + break; + } + case '\n': + case '\r': + { + CV_PARSE_ERROR_CPP( "'\"' - right-quote of string is missing" ); + break; + } + default: + { + ptr++; + break; + } + } + } - if( data ) - { - ptr = icvFSResizeWriteBuffer( fs, ptr, static_cast(data_len) ); - memcpy( ptr, data, data_len ); - ptr += data_len; - } + if ( *ptr != '\"' ) + CV_PARSE_ERROR_CPP( "'\"' - right-quote of string is missing" ); + else + ptr++; - fs->buffer = ptr; - fs->struct_flags = struct_flags & ~CV_NODE_EMPTY; -} + node.setValue(FileNode::STRING, buf, i); + } + } + else if ( cv_isdigit(*ptr) || *ptr == '-' || *ptr == '+' || *ptr == '.' ) + { /**************** number ****************/ + char * beg = ptr; + if ( *ptr == '+' || *ptr == '-' ) + { + ptr++; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } + while( cv_isdigit(*ptr) ) + { + ptr++; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } + if (*ptr == '.' || *ptr == 'e') + { + double fval = fs->strtod( beg, &ptr ); + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + node.setValue(FileNode::REAL, &fval); + } + else + { + int ival = (int)strtol( beg, &ptr, 0 ); + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); -void icvJSONStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name) -{ - int parent_flags; - char data[CV_FS_MAX_LEN + 1024]; + node.setValue(FileNode::INT, &ival); + } - struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY; - if( !CV_NODE_IS_COLLECTION(struct_flags)) - CV_Error( CV_StsBadArg, - "Some collection type - CV_NODE_SEQ or CV_NODE_MAP, must be specified" ); + if ( beg >= ptr ) + CV_PARSE_ERROR_CPP( "Invalid numeric value (inconsistent explicit type specification?)" ); + } + else + { /**************** other data ****************/ + const char* beg = ptr; + int len = 0; + for ( ; cv_isalpha(*ptr) && len <= 6; ) + { + len++; + ptr++; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } - if ( type_name && *type_name == '\0' ) - type_name = 0; + if( len == 4 && memcmp( beg, "null", 4 ) == 0 ) + { + CV_PARSE_ERROR_CPP( "Value 'null' is not supported by this parser" ); + } + else if( (len == 4 && memcmp( beg, "true", 4 ) == 0) || + (len == 5 && memcmp( beg, "false", 5 ) == 0) ) + { + int ival = *beg == 't' ? 1 : 0; + node.setValue(FileNode::INT, &ival); + } + else + { + CV_PARSE_ERROR_CPP( "Unrecognized value" ); + } + } - bool has_type_id = false; - bool is_real_collection = true; - if (type_name && memcmp(type_name, "binary", 6) == 0) - { - struct_flags = CV_NODE_STR; - data[0] = '\0'; - is_real_collection = false; - } - else if( type_name ) - { - has_type_id = true; + return ptr; } - if ( is_real_collection ) + char* parseSeq( char* ptr, FileNode& node ) { - char c = CV_NODE_IS_MAP(struct_flags) ? '{' : '['; - data[0] = c; - data[1] = '\0'; - } + if (!ptr) + CV_PARSE_ERROR_CPP( "ptr is NULL" ); - icvJSONWrite( fs, key, data ); + if ( *ptr != '[' ) + CV_PARSE_ERROR_CPP( "'[' - left-brace of seq is missing" ); + else + ptr++; - parent_flags = fs->struct_flags; - cvSeqPush( fs->write_stack, &parent_flags ); - fs->struct_flags = struct_flags; - fs->struct_indent += 4; + fs->convertToCollection(FileNode::SEQ, node); - if ( has_type_id ) - fs->write_string( fs, "type_id", type_name, 1 ); -} + for (;;) + { + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + break; + if ( *ptr != ']' ) + { + FileNode child = fs->addNode(node, std::string(), FileNode::NONE ); -void icvJSONEndWriteStruct( CvFileStorage* fs ) -{ - if( fs->write_stack->total == 0 ) - CV_Error( CV_StsError, "EndWriteStruct w/o matching StartWriteStruct" ); + if ( *ptr == '[' ) + ptr = parseSeq( ptr, child ); + else if ( *ptr == '{' ) + ptr = parseMap( ptr, child ); + else + ptr = parseValue( ptr, child ); + } - int parent_flags = 0; - int struct_flags = fs->struct_flags; - cvSeqPop( fs->write_stack, &parent_flags ); - fs->struct_indent -= 4; - fs->struct_flags = parent_flags & ~CV_NODE_EMPTY; - assert( fs->struct_indent >= 0 ); + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + break; - if ( CV_NODE_IS_COLLECTION(struct_flags) ) - { - if ( !CV_NODE_IS_FLOW(struct_flags) ) - { - if ( fs->buffer <= fs->buffer_start + fs->space ) - { - /* some bad code for base64_writer... */ - *fs->buffer++ = '\n'; - *fs->buffer++ = '\0'; - icvPuts( fs, fs->buffer_start ); - fs->buffer = fs->buffer_start; - } - icvFSFlush(fs); + if ( *ptr == ',' ) + ptr++; + else if ( *ptr == ']' ) + break; + else + CV_PARSE_ERROR_CPP( "Unexpected character" ); } - char* ptr = fs->buffer; - if( ptr > fs->buffer_start + fs->struct_indent && !CV_NODE_IS_EMPTY(struct_flags) ) - *ptr++ = ' '; - *ptr++ = CV_NODE_IS_MAP(struct_flags) ? '}' : ']'; - fs->buffer = ptr; - } -} - + if (!ptr) + CV_PARSE_ERROR_CPP("ptr is NULL"); -void icvJSONStartNextStream( CvFileStorage* fs ) -{ - if( !fs->is_first ) - { - while( fs->write_stack->total > 0 ) - icvJSONEndWriteStruct(fs); + if ( *ptr != ']' ) + CV_PARSE_ERROR_CPP( "']' - right-brace of seq is missing" ); + else + ptr++; - fs->struct_indent = 4; - icvFSFlush(fs); - fs->buffer = fs->buffer_start; + fs->finalizeCollection(node); + return ptr; } -} - -void icvJSONWriteInt( CvFileStorage* fs, const char* key, int value ) -{ - char buf[128]; - icvJSONWrite( fs, key, icv_itoa( value, buf, 10 )); -} - - -void icvJSONWriteReal( CvFileStorage* fs, const char* key, double value ) -{ - char buf[128]; - size_t len = strlen( icvDoubleToString( buf, value ) ); - if( len > 0 && buf[len-1] == '.' ) + char* parseMap( char* ptr, FileNode& node ) { - // append zero if string ends with decimal place to match JSON standard - buf[len] = '0'; - buf[len+1] = '\0'; - } - icvJSONWrite( fs, key, buf ); -} + if (!ptr) + CV_PARSE_ERROR_CPP("ptr is NULL"); + if ( *ptr != '{' ) + CV_PARSE_ERROR_CPP( "'{' - left-brace of map is missing" ); + else + ptr++; -void icvJSONWriteString( CvFileStorage* fs, const char* key, const char* str, int quote) -{ - char buf[CV_FS_MAX_LEN*4+16]; - char* data = (char*)str; - int i, len; - - if( !str ) - CV_Error( CV_StsNullPtr, "Null string pointer" ); - - len = (int)strlen(str); - if( len > CV_FS_MAX_LEN ) - CV_Error( CV_StsBadArg, "The written string is too long" ); + fs->convertToCollection(FileNode::MAP, node); - if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') ) - { - int need_quote = 1; - data = buf; - *data++ = '\"'; - for( i = 0; i < len; i++ ) + for( ;; ) { - char c = str[i]; + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + break; - switch ( c ) + if ( *ptr == '"' ) { - case '\\': - case '\"': - case '\'': { *data++ = '\\'; *data++ = c; break; } - case '\n': { *data++ = '\\'; *data++ = 'n'; break; } - case '\r': { *data++ = '\\'; *data++ = 'r'; break; } - case '\t': { *data++ = '\\'; *data++ = 't'; break; } - case '\b': { *data++ = '\\'; *data++ = 'b'; break; } - case '\f': { *data++ = '\\'; *data++ = 'f'; break; } - default : { *data++ = c; } - break; - } - } + FileNode child; + ptr = parseKey( ptr, node, child ); + if( !ptr || !*ptr ) + break; + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + break; - *data++ = '\"'; - *data++ = '\0'; - data = buf + !need_quote; - } + if ( *ptr == '[' ) + ptr = parseSeq( ptr, child ); + else if ( *ptr == '{' ) + ptr = parseMap( ptr, child ); + else + ptr = parseValue( ptr, child ); + } - icvJSONWrite( fs, key, data ); -} + ptr = skipSpaces( ptr ); + if( !ptr || !*ptr ) + break; + if ( *ptr == ',' ) + ptr++; + else if ( *ptr == '}' ) + break; + else + CV_PARSE_ERROR_CPP( "Unexpected character" ); + } -void icvJSONWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ) -{ - if( !comment ) - CV_Error( CV_StsNullPtr, "Null comment" ); + if (!ptr) + CV_PARSE_ERROR_CPP("ptr is NULL"); - int len = static_cast(strlen(comment)); - char* ptr = fs->buffer; - const char* eol = strchr(comment, '\n'); - bool multiline = eol != 0; + if ( *ptr != '}' ) + CV_PARSE_ERROR_CPP( "'}' - right-brace of map is missing" ); + else + ptr++; - if( !eol_comment || multiline || fs->buffer_end - ptr < len || ptr == fs->buffer_start ) - ptr = icvFSFlush( fs ); - else - *ptr++ = ' '; + fs->finalizeCollection(node); + return ptr; + } - while( comment ) + bool parse( char* ptr ) { - *ptr++ = '/'; - *ptr++ = '/'; - *ptr++ = ' '; - if( eol ) + ptr = skipSpaces( ptr ); + if ( !ptr || !*ptr ) + return false; + + FileNode root_collection(fs->getFS(), 0, 0); + + if( *ptr == '{' ) { - ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 ); - memcpy( ptr, comment, eol - comment + 1 ); - fs->buffer = ptr + (eol - comment); - comment = eol + 1; - eol = strchr( comment, '\n' ); + FileNode root_node = fs->addNode(root_collection, std::string(), FileNode::MAP); + parseMap( ptr, root_node ); + } + else if ( *ptr == '[' ) + { + FileNode root_node = fs->addNode(root_collection, std::string(), FileNode::SEQ); + parseSeq( ptr, root_node ); } else { - len = (int)strlen(comment); - ptr = icvFSResizeWriteBuffer( fs, ptr, len ); - memcpy( ptr, comment, len ); - fs->buffer = ptr + len; - comment = 0; + CV_PARSE_ERROR_CPP( "left-brace of top level is missing" ); } - ptr = icvFSFlush( fs ); + + if( !ptr || !*ptr ) + CV_PARSE_ERROR_CPP( "Unexpected End-Of-File" ); + return true; } + + FileStorage_API* fs; + char buf[CV_FS_MAX_LEN+1024]; +}; + +Ptr createJSONEmitter(FileStorage_API* fs) +{ + return makePtr(fs); +} + +Ptr createJSONParser(FileStorage_API* fs) +{ + return makePtr(fs); +} + } diff --git a/modules/core/src/persistence_types.cpp b/modules/core/src/persistence_types.cpp index 2f4cc25..eeebdaf 100644 --- a/modules/core/src/persistence_types.cpp +++ b/modules/core/src/persistence_types.cpp @@ -2,343 +2,196 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html - #include "precomp.hpp" #include "persistence.hpp" - - -/****************************************************************************************\ -* Reading/Writing etc. for standard types * -\****************************************************************************************/ - -/*#define CV_TYPE_NAME_MAT "opencv-matrix" -#define CV_TYPE_NAME_MATND "opencv-nd-matrix" -#define CV_TYPE_NAME_SPARSE_MAT "opencv-sparse-matrix" -#define CV_TYPE_NAME_IMAGE "opencv-image" -#define CV_TYPE_NAME_SEQ "opencv-sequence" -#define CV_TYPE_NAME_SEQ_TREE "opencv-sequence-tree" -#define CV_TYPE_NAME_GRAPH "opencv-graph"*/ - -/******************************* CvMat ******************************/ - -static int icvIsMat( const void* ptr ) +namespace cv { - return CV_IS_MAT_HDR_Z(ptr); -} -static void icvWriteMat( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList /*attr*/ ) +void write( FileStorage& fs, const String& name, const Mat& m ) { - const CvMat* mat = (const CvMat*)struct_ptr; char dt[16]; - cv::Size size; - int y; - - assert( CV_IS_MAT_HDR_Z(mat) ); - - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MAT ); - cvWriteInt( fs, "rows", mat->rows ); - cvWriteInt( fs, "cols", mat->cols ); - cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 ); - cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW ); - size = cvGetSize(mat); - if( size.height > 0 && size.width > 0 && mat->data.ptr ) + if( m.dims <= 2 ) { - if( CV_IS_MAT_CONT(mat->type) ) - { - size.width *= size.height; - size.height = 1; - } - - for( y = 0; y < size.height; y++ ) - cvWriteRawData( fs, mat->data.ptr + (size_t)y*mat->step, size.width, dt ); + fs.startWriteStruct(name, FileNode::MAP, String("opencv-matrix")); + fs << "rows" << m.rows; + fs << "cols" << m.cols; + fs << "dt" << fs::encodeFormat( m.type(), dt ); + fs << "data" << "[:"; + for( int i = 0; i < m.rows; i++ ) + fs.writeRaw(dt, m.ptr(i), m.cols*m.elemSize()); + fs << "]"; + fs.endWriteStruct(); } - cvEndWriteStruct( fs ); - cvEndWriteStruct( fs ); -} - - -static int icvFileNodeSeqLen( CvFileNode* node ) -{ - return CV_NODE_IS_COLLECTION(node->tag) ? node->data.seq->total : - CV_NODE_TYPE(node->tag) != CV_NODE_NONE; -} - - -static void* icvReadMat( CvFileStorage* fs, CvFileNode* node ) -{ - void* ptr = 0; - CvMat* mat; - const char* dt; - CvFileNode* data; - int rows, cols, elem_type; - - rows = cvReadIntByName( fs, node, "rows", -1 ); - cols = cvReadIntByName( fs, node, "cols", -1 ); - dt = cvReadStringByName( fs, node, "dt", 0 ); - - if( rows < 0 || cols < 0 || !dt ) - CV_Error( CV_StsError, "Some of essential matrix attributes are absent" ); - - elem_type = icvDecodeSimpleFormat( dt ); - - data = cvGetFileNodeByName( fs, node, "data" ); - if( !data ) - CV_Error( CV_StsError, "The matrix data is not found in file storage" ); - - int nelems = icvFileNodeSeqLen( data ); - if( nelems > 0 && nelems != rows*cols*CV_MAT_CN(elem_type) ) - CV_Error( CV_StsUnmatchedSizes, - "The matrix size does not match to the number of stored elements" ); - - if( nelems > 0 ) + else { - mat = cvCreateMat( rows, cols, elem_type ); - cvReadRawData( fs, data, mat->data.ptr, dt ); + fs.startWriteStruct(name, FileNode::MAP, String("opencv-nd-matrix")); + fs << "sizes" << "[:"; + fs.writeRaw( "i", m.size.p, m.dims*sizeof(int) ); + fs << "]"; + fs << "dt" << fs::encodeFormat( m.type(), dt ); + fs << "data" << "[:"; + const Mat* arrays[] = {&m, 0}; + uchar* ptrs[1] = {}; + NAryMatIterator it(arrays, ptrs); + size_t total = it.size*m.elemSize(); + + for( size_t i = 0; i < it.nplanes; i++, ++it ) + fs.writeRaw( dt, ptrs[0], total ); + fs << "]"; + fs.endWriteStruct(); } - else - mat = cvCreateMatHeader( rows, cols, elem_type ); - - ptr = mat; - return ptr; -} - - -/******************************* CvMatND ******************************/ - -static int icvIsMatND( const void* ptr ) -{ - return CV_IS_MATND_HDR(ptr); } - -static void icvWriteMatND( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList /*attr*/ ) +struct SparseNodeCmp { - CvMatND* mat = (CvMatND*)struct_ptr; - CvMatND stub; - CvNArrayIterator iterator; - int dims, sizes[CV_MAX_DIM]; - char dt[16]; - - assert( CV_IS_MATND_HDR(mat) ); - - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MATND ); - dims = cvGetDims( mat, sizes ); - cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW ); - cvWriteRawData( fs, sizes, dims, "i" ); - cvEndWriteStruct( fs ); - cvWriteString( fs, "dt", icvEncodeFormat( cvGetElemType(mat), dt ), 0 ); - cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW ); - - if( mat->dim[0].size > 0 && mat->data.ptr ) + SparseNodeCmp(int _dims) : dims(_dims) {} + bool operator()(const SparseMat::Node* a, const SparseMat::Node* b) { - cvInitNArrayIterator( 1, (CvArr**)&mat, 0, &stub, &iterator ); - - do - cvWriteRawData( fs, iterator.ptr[0], iterator.size.width, dt ); - while( cvNextNArraySlice( &iterator )); + for( int i = 0; i < dims; i++ ) + { + int d = a->idx[i] - b->idx[i]; + if(d) + return d < 0; + } + return false; } - cvEndWriteStruct( fs ); - cvEndWriteStruct( fs ); -} + int dims; +}; -static void* icvReadMatND( CvFileStorage* fs, CvFileNode* node ) +void write( FileStorage& fs, const String& name, const SparseMat& m ) { - void* ptr = 0; - CvMatND* mat; - const char* dt; - CvFileNode* data; - CvFileNode* sizes_node; - int sizes[CV_MAX_DIM] = {0}, dims, elem_type; - int i, total_size; - - sizes_node = cvGetFileNodeByName( fs, node, "sizes" ); - dt = cvReadStringByName( fs, node, "dt", 0 ); - - if( !sizes_node || !dt ) - CV_Error( CV_StsError, "Some of essential matrix attributes are absent" ); - - dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total : - CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1; - - if( dims <= 0 || dims > CV_MAX_DIM ) - CV_Error( CV_StsParseError, "Could not determine the matrix dimensionality" ); - - cvReadRawData( fs, sizes_node, sizes, "i" ); - elem_type = icvDecodeSimpleFormat( dt ); - - data = cvGetFileNodeByName( fs, node, "data" ); - if( !data ) - CV_Error( CV_StsError, "The matrix data is not found in file storage" ); + char dt[16]; + fs.startWriteStruct(name, FileNode::MAP, String("opencv-sparse-matrix")); + fs << "sizes" << "[:"; + int dims = m.dims(); + if( dims > 0 ) + fs.writeRaw("i", m.hdr->size, dims*sizeof(int) ); + fs << "]"; + fs << "dt" << fs::encodeFormat( m.type(), dt ); + fs << "data" << "[:"; + size_t i = 0, n = m.nzcount(); + std::vector elems(n); + SparseMatConstIterator it = m.begin(), it_end = m.end(); - for( total_size = CV_MAT_CN(elem_type), i = 0; i < dims; i++ ) + for( ; it != it_end; ++it ) { - CV_Assert(sizes[i]); - total_size *= sizes[i]; + CV_Assert(it.node() != 0); + elems[i++] = it.node(); } - int nelems = icvFileNodeSeqLen( data ); + std::sort(elems.begin(), elems.end(), SparseNodeCmp(dims)); + const SparseMat::Node* prev_node = 0; + size_t esz = m.elemSize(); - if( nelems > 0 && nelems != total_size ) - CV_Error( CV_StsUnmatchedSizes, - "The matrix size does not match to the number of stored elements" ); - - if( nelems > 0 ) + for( i = 0; i < n; i++ ) { - mat = cvCreateMatND( dims, sizes, elem_type ); - cvReadRawData( fs, data, mat->data.ptr, dt ); - } - else - mat = cvCreateMatNDHeader( dims, sizes, elem_type ); - - ptr = mat; - return ptr; -} + const SparseMat::Node* node = elems[i]; + int k = 0; + if( prev_node ) + { + for( ; k < dims; k++ ) + if( node->idx[k] != prev_node->idx[k] ) + break; + CV_Assert( k < dims ); + if( k < dims - 1 ) + writeScalar( fs, k - dims + 1 ); + } + for( ; k < dims; k++ ) + writeScalar( fs, node->idx[k] ); + prev_node = node; -/******************************* CvSparseMat ******************************/ + const uchar* value = &m.value(node); + fs.writeRaw(dt, value, esz); + } -static int icvIsSparseMat( const void* ptr ) -{ - return CV_IS_SPARSE_MAT(ptr); + fs << "]" << "}"; } - -static int icvSortIdxCmpFunc( const void* _a, const void* _b, void* userdata ) +void read(const FileNode& node, Mat& m, const Mat& default_mat) { - int i, dims = *(int*)userdata; - const int* a = *(const int**)_a; - const int* b = *(const int**)_b; - - for( i = 0; i < dims; i++ ) + if( node.empty() ) { - int delta = a[i] - b[i]; - if( delta ) - return delta; + default_mat.copyTo(m); + return; } - return 0; -} - - -static void icvWriteSparseMat( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList /*attr*/ ) -{ - CvMemStorage* memstorage = 0; - const CvSparseMat* mat = (const CvSparseMat*)struct_ptr; - CvSparseMatIterator iterator; - CvSparseNode* node; - CvSeq* elements; - CvSeqReader reader; - int i, dims; - int *prev_idx = 0; - char dt[16]; - - assert( CV_IS_SPARSE_MAT(mat) ); - - memstorage = cvCreateMemStorage(); - - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SPARSE_MAT ); - dims = cvGetDims( mat, 0 ); + std::string dt; + int rows, cols, elem_type; - cvStartWriteStruct( fs, "sizes", CV_NODE_SEQ + CV_NODE_FLOW ); - cvWriteRawData( fs, mat->size, dims, "i" ); - cvEndWriteStruct( fs ); - cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 ); - cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW ); + read(node["dt"], dt, std::string()); + CV_Assert( !dt.empty() ); - elements = cvCreateSeq( CV_SEQ_ELTYPE_PTR, sizeof(CvSeq), sizeof(int*), memstorage ); + elem_type = fs::decodeSimpleFormat( dt.c_str() ); - node = cvInitSparseMatIterator( mat, &iterator ); - while( node ) + read(node["rows"], rows, -1); + if( rows >= 0 ) { - int* idx = CV_NODE_IDX( mat, node ); - cvSeqPush( elements, &idx ); - node = cvGetNextSparseNode( &iterator ); + read(node["cols"], cols, -1); + m.create(rows, cols, elem_type); } - - cvSeqSort( elements, icvSortIdxCmpFunc, &dims ); - cvStartReadSeq( elements, &reader, 0 ); - - for( i = 0; i < elements->total; i++ ) + else { - int* idx; - void* val; - int k = 0; - - CV_READ_SEQ_ELEM( idx, reader ); - if( i > 0 ) - { - for( ; idx[k] == prev_idx[k]; k++ ) - assert( k < dims ); - if( k < dims - 1 ) - fs->write_int( fs, 0, k - dims + 1 ); - } - for( ; k < dims; k++ ) - fs->write_int( fs, 0, idx[k] ); - prev_idx = idx; + int sizes[CV_MAX_DIM] = {0}, dims; + FileNode sizes_node = node["sizes"]; + CV_Assert( !sizes_node.empty() ); - node = (CvSparseNode*)((uchar*)idx - mat->idxoffset ); - val = CV_NODE_VAL( mat, node ); + dims = (int)sizes_node.size(); + sizes_node.readRaw("i", sizes, dims*sizeof(sizes[0])); - cvWriteRawData( fs, val, 1, dt ); + m.create(dims, sizes, elem_type); } - cvEndWriteStruct( fs ); - cvEndWriteStruct( fs ); - cvReleaseMemStorage( &memstorage ); -} + FileNode data_node = node["data"]; + CV_Assert(!data_node.empty()); + size_t nelems = data_node.size(); + CV_Assert(nelems == m.total()*m.channels()); -static void* icvReadSparseMat( CvFileStorage* fs, CvFileNode* node ) -{ - void* ptr = 0; - CvSparseMat* mat; - const char* dt; - CvFileNode* data; - CvFileNode* sizes_node; - CvSeqReader reader; - CvSeq* elements; - int sizes[CV_MAX_DIM], dims, elem_type, cn; - int i; + data_node.readRaw(dt, (uchar*)m.ptr(), m.total()*m.elemSize()); +} - sizes_node = cvGetFileNodeByName( fs, node, "sizes" ); - dt = cvReadStringByName( fs, node, "dt", 0 ); +void read( const FileNode& node, SparseMat& m, const SparseMat& default_mat ) +{ + if( node.empty() ) + { + default_mat.copyTo(m); + return; + } - if( !sizes_node || !dt ) - CV_Error( CV_StsError, "Some of essential matrix attributes are absent" ); + std::string dt; + read(node["dt"], dt, std::string()); + CV_Assert( !dt.empty() ); - dims = CV_NODE_IS_SEQ(sizes_node->tag) ? sizes_node->data.seq->total : - CV_NODE_IS_INT(sizes_node->tag) ? 1 : -1; + int elem_type = fs::decodeSimpleFormat( dt.c_str() ); - if( dims <= 0 || dims > CV_MAX_DIM) - CV_Error( CV_StsParseError, "Could not determine sparse matrix dimensionality" ); + int sizes[CV_MAX_DIM] = {0}; + FileNode sizes_node = node["sizes"]; + CV_Assert( !sizes_node.empty() ); - cvReadRawData( fs, sizes_node, sizes, "i" ); - elem_type = icvDecodeSimpleFormat( dt ); + int dims = (int)sizes_node.size(); + sizes_node.readRaw("i", sizes, dims*sizeof(sizes[0])); - data = cvGetFileNodeByName( fs, node, "data" ); - if( !data || !CV_NODE_IS_SEQ(data->tag) ) - CV_Error( CV_StsError, "The matrix data is not found in file storage" ); + m.create(dims, sizes, elem_type); - mat = cvCreateSparseMat( dims, sizes, elem_type ); + FileNode data = node["data"]; + CV_Assert( data.isSeq() ); - cn = CV_MAT_CN(elem_type); - int idx[CV_MAX_DIM]; - elements = data->data.seq; - cvStartReadRawData( fs, data, &reader ); + int cn = CV_MAT_CN(elem_type); + int idx[CV_MAX_DIM] = {0}; + size_t i, sz = data.size(); + size_t esz = m.elemSize(); + FileNodeIterator it = data.begin(); - for( i = 0; i < elements->total; ) + for( i = 0; i < sz; ) { - CvFileNode* elem = (CvFileNode*)reader.ptr; - uchar* val; - int k; - if( !CV_NODE_IS_INT(elem->tag )) - CV_Error( CV_StsParseError, "Sparse matrix data is corrupted" ); - k = elem->data.i; + FileNode n = *it; + int k = (int)n; if( i > 0 && k >= 0 ) idx[dims-1] = k; else @@ -349,960 +202,118 @@ static void* icvReadSparseMat( CvFileStorage* fs, CvFileNode* node ) idx[0] = k, k = 1; for( ; k < dims; k++ ) { - CV_NEXT_SEQ_ELEM( elements->elem_size, reader ); + ++it; i++; - elem = (CvFileNode*)reader.ptr; - if( !CV_NODE_IS_INT(elem->tag ) || elem->data.i < 0 ) - CV_Error( CV_StsParseError, "Sparse matrix data is corrupted" ); - idx[k] = elem->data.i; + n = *it; + CV_Assert( n.isInt() ); + int idx_k = (int)n; + CV_Assert( idx_k >= 0 ); + idx[k] = idx_k; } } - CV_NEXT_SEQ_ELEM( elements->elem_size, reader ); + ++it; i++; - val = cvPtrND( mat, idx, 0, 1, 0 ); - cvReadRawDataSlice( fs, &reader, cn, val, dt ); + uchar* valptr = m.ptr(idx, true); + it.readRaw(dt, valptr, esz); i += cn; } - - ptr = mat; - return ptr; } - -/******************************* IplImage ******************************/ - -static int icvIsImage( const void* ptr ) +void read(const FileNode& node, KeyPoint& value, const KeyPoint& default_value) { - return CV_IS_IMAGE_HDR(ptr); -} - -static void icvWriteImage( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList /*attr*/ ) -{ - const IplImage* image = (const IplImage*)struct_ptr; - char dt_buf[16], *dt; - cv::Size size; - int y, depth; - - assert( CV_IS_IMAGE(image) ); - - if( image->dataOrder == IPL_DATA_ORDER_PLANE ) - CV_Error( CV_StsUnsupportedFormat, - "Images with planar data layout are not supported" ); - - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_IMAGE ); - cvWriteInt( fs, "width", image->width ); - cvWriteInt( fs, "height", image->height ); - cvWriteString( fs, "origin", image->origin == IPL_ORIGIN_TL - ? "top-left" : "bottom-left", 0 ); - cvWriteString( fs, "layout", image->dataOrder == IPL_DATA_ORDER_PLANE - ? "planar" : "interleaved", 0 ); - if( image->roi ) - { - cvStartWriteStruct( fs, "roi", CV_NODE_MAP + CV_NODE_FLOW ); - cvWriteInt( fs, "x", image->roi->xOffset ); - cvWriteInt( fs, "y", image->roi->yOffset ); - cvWriteInt( fs, "width", image->roi->width ); - cvWriteInt( fs, "height", image->roi->height ); - cvWriteInt( fs, "coi", image->roi->coi ); - cvEndWriteStruct( fs ); - } - - depth = IPL2CV_DEPTH(image->depth); - dt = icvEncodeFormat(depth, dt_buf); - cvWriteString( fs, "dt", dt, 0 ); - - size = cvSize(image->width, image->height); - if( size.width*image->nChannels*CV_ELEM_SIZE(depth) == image->widthStep ) + if( node.empty() ) { - size.width *= size.height; - size.height = 1; + value = default_value; + return; } - - cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW ); - for( y = 0; y < size.height; y++ ) - cvWriteRawData( fs, image->imageData + y*image->widthStep, size.width, dt ); - cvEndWriteStruct( fs ); - cvEndWriteStruct( fs ); + node >> value; } - -static void* icvReadImage( CvFileStorage* fs, CvFileNode* node ) +void read(const FileNode& node, DMatch& value, const DMatch& default_value) { - void* ptr = 0; - IplImage* image; - const char* dt; - CvFileNode* data; - CvFileNode* roi_node; - CvSeqReader reader; - cv::Rect roi; - int y, width, height, elem_type, coi, depth; - const char* origin, *data_order; - - width = cvReadIntByName( fs, node, "width", 0 ); - height = cvReadIntByName( fs, node, "height", 0 ); - dt = cvReadStringByName( fs, node, "dt", 0 ); - origin = cvReadStringByName( fs, node, "origin", 0 ); - - if( width == 0 || height == 0 || dt == 0 || origin == 0 ) - CV_Error( CV_StsError, "Some of essential image attributes are absent" ); - - elem_type = icvDecodeSimpleFormat( dt ); - data_order = cvReadStringByName( fs, node, "layout", "interleaved" ); - if( !data_order || strcmp( data_order, "interleaved" ) != 0 ) - CV_Error( CV_StsError, "Only interleaved images can be read" ); - - data = cvGetFileNodeByName( fs, node, "data" ); - if( !data ) - CV_Error( CV_StsError, "The image data is not found in file storage" ); - - if( icvFileNodeSeqLen( data ) != width*height*CV_MAT_CN(elem_type) ) - CV_Error( CV_StsUnmatchedSizes, - "The matrix size does not match to the number of stored elements" ); - - depth = cvIplDepth(elem_type); - image = cvCreateImage( cvSize(width,height), depth, CV_MAT_CN(elem_type) ); - - roi_node = cvGetFileNodeByName( fs, node, "roi" ); - if( roi_node ) - { - roi.x = cvReadIntByName( fs, roi_node, "x", 0 ); - roi.y = cvReadIntByName( fs, roi_node, "y", 0 ); - roi.width = cvReadIntByName( fs, roi_node, "width", 0 ); - roi.height = cvReadIntByName( fs, roi_node, "height", 0 ); - coi = cvReadIntByName( fs, roi_node, "coi", 0 ); - - cvSetImageROI( image, cvRect(roi) ); - cvSetImageCOI( image, coi ); - } - - if( width*CV_ELEM_SIZE(elem_type) == image->widthStep ) + if( node.empty() ) { - width *= height; - height = 1; + value = default_value; + return; } - - width *= CV_MAT_CN(elem_type); - cvStartReadRawData( fs, data, &reader ); - for( y = 0; y < height; y++ ) - { - cvReadRawDataSlice( fs, &reader, width, - image->imageData + y*image->widthStep, dt ); - } - - ptr = image; - return ptr; -} - - -/******************************* CvSeq ******************************/ - -static int icvIsSeq( const void* ptr ) -{ - return CV_IS_SEQ(ptr); + node >> value; } - -static void -icvReleaseSeq( void** ptr ) +#ifdef CV__LEGACY_PERSISTENCE +void write( FileStorage& fs, const std::string& name, const std::vector& vec) { - if( !ptr ) - CV_Error( CV_StsNullPtr, "NULL double pointer" ); - *ptr = 0; // it's impossible now to release seq, so just clear the pointer + // from template implementation + cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ); + write(fs, vec); } - -static void* icvCloneSeq( const void* ptr ) +void read(const FileNode& node, std::vector& keypoints) { - return cvSeqSlice( (CvSeq*)ptr, CV_WHOLE_SEQ, - 0 /* use the same storage as for the original sequence */, 1 ); -} - - -static void icvWriteHeaderData( CvFileStorage* fs, const CvSeq* seq, CvAttrList* attr, int initial_header_size ) -{ - char header_dt_buf[128]; - const char* header_dt = cvAttrValue( attr, "header_dt" ); - - if( header_dt ) - { - int dt_header_size; - dt_header_size = icvCalcElemSize( header_dt, initial_header_size ); - if( dt_header_size > seq->header_size ) - CV_Error( CV_StsUnmatchedSizes, - "The size of header calculated from \"header_dt\" is greater than header_size" ); - } - else if( seq->header_size > initial_header_size ) - { - if( CV_IS_SEQ(seq) && CV_IS_SEQ_POINT_SET(seq) && - seq->header_size == sizeof(CvPoint2DSeq) && - seq->elem_size == sizeof(int)*2 ) - { - CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq; - - cvStartWriteStruct( fs, "rect", CV_NODE_MAP + CV_NODE_FLOW ); - cvWriteInt( fs, "x", point_seq->rect.x ); - cvWriteInt( fs, "y", point_seq->rect.y ); - cvWriteInt( fs, "width", point_seq->rect.width ); - cvWriteInt( fs, "height", point_seq->rect.height ); - cvEndWriteStruct( fs ); - cvWriteInt( fs, "color", point_seq->color ); - } - else if( CV_IS_SEQ(seq) && CV_IS_SEQ_CHAIN(seq) && - CV_MAT_TYPE(seq->flags) == CV_8UC1 ) + FileNode first_node = *(node.begin()); + 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) { - CvChain* chain = (CvChain*)seq; - - cvStartWriteStruct( fs, "origin", CV_NODE_MAP + CV_NODE_FLOW ); - cvWriteInt( fs, "x", chain->origin.x ); - cvWriteInt( fs, "y", chain->origin.y ); - cvEndWriteStruct( fs ); + (*it) >> keypoints[i]; } - else - { - unsigned extra_size = seq->header_size - initial_header_size; - // a heuristic to provide nice defaults for sequences of int's & float's - if( extra_size % sizeof(int) == 0 ) - sprintf( header_dt_buf, "%ui", (unsigned)(extra_size/sizeof(int)) ); - else - sprintf( header_dt_buf, "%uu", extra_size ); - header_dt = header_dt_buf; - } - } - - if( header_dt ) - { - cvWriteString( fs, "header_dt", header_dt, 0 ); - cvStartWriteStruct( fs, "header_user_data", CV_NODE_SEQ + CV_NODE_FLOW ); - cvWriteRawData( fs, (uchar*)seq + sizeof(CvSeq), 1, header_dt ); - cvEndWriteStruct( fs ); - } -} - - -static char* icvGetFormat( const CvSeq* seq, const char* dt_key, CvAttrList* attr, int initial_elem_size, char* dt_buf ) -{ - char* dt = 0; - dt = (char*)cvAttrValue( attr, dt_key ); - - if( dt ) - { - int dt_elem_size; - dt_elem_size = icvCalcElemSize( dt, initial_elem_size ); - if( dt_elem_size != seq->elem_size ) - CV_Error( CV_StsUnmatchedSizes, - "The size of element calculated from \"dt\" and " - "the elem_size do not match" ); - } - else if( CV_MAT_TYPE(seq->flags) != 0 || seq->elem_size == 1 ) - { - if( CV_ELEM_SIZE(seq->flags) != seq->elem_size ) - CV_Error( CV_StsUnmatchedSizes, - "Size of sequence element (elem_size) is inconsistent with seq->flags" ); - dt = icvEncodeFormat( CV_MAT_TYPE(seq->flags), dt_buf ); +#else + FileNodeIterator it = node.begin(); + it >> keypoints; +#endif + return; } - else if( seq->elem_size > initial_elem_size ) + keypoints.clear(); + FileNodeIterator it = node.begin(), it_end = node.end(); + for( ; it != it_end; ) { - unsigned extra_elem_size = seq->elem_size - initial_elem_size; - // a heuristic to provide nice defaults for sequences of int's & float's - if( extra_elem_size % sizeof(int) == 0 ) - sprintf( dt_buf, "%ui", (unsigned)(extra_elem_size/sizeof(int)) ); - else - sprintf( dt_buf, "%uu", extra_elem_size ); - dt = dt_buf; + KeyPoint kpt; + it >> kpt.pt.x >> kpt.pt.y >> kpt.size >> kpt.angle >> kpt.response >> kpt.octave >> kpt.class_id; + keypoints.push_back(kpt); } - - return dt; } - -static void icvWriteSeq( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList attr, int level ) +void write( FileStorage& fs, const std::string& name, const std::vector& vec) { - const CvSeq* seq = (CvSeq*)struct_ptr; - CvSeqBlock* block; - char buf[128]; - char dt_buf[128], *dt; - - assert( CV_IS_SEQ( seq )); - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ ); - - if( level >= 0 ) - cvWriteInt( fs, "level", level ); - - dt = icvGetFormat( seq, "dt", &attr, 0, dt_buf ); - - strcpy(buf, ""); - if( CV_IS_SEQ_CLOSED(seq) ) - strcat(buf, " closed"); - if( CV_IS_SEQ_HOLE(seq) ) - strcat(buf, " hole"); - if( CV_IS_SEQ_CURVE(seq) ) - strcat(buf, " curve"); - if( CV_SEQ_ELTYPE(seq) == 0 && seq->elem_size != 1 ) - strcat(buf, " untyped"); - - cvWriteString( fs, "flags", buf + (buf[0] ? 1 : 0), 1 ); - - cvWriteInt( fs, "count", seq->total ); - - cvWriteString( fs, "dt", dt, 0 ); - - icvWriteHeaderData( fs, seq, &attr, sizeof(CvSeq) ); - cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW ); - - for( block = seq->first; block; block = block->next ) - { - cvWriteRawData( fs, block->data, block->count, dt ); - if( block == seq->first->prev ) - break; - } - cvEndWriteStruct( fs ); - cvEndWriteStruct( fs ); + // from template implementation + cv::internal::WriteStructContext ws(fs, name, FileNode::SEQ); + write(fs, vec); } - -static void icvWriteSeqTree( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList attr ) +void read(const FileNode& node, std::vector& matches) { - const CvSeq* seq = (CvSeq*)struct_ptr; - const char* recursive_value = cvAttrValue( &attr, "recursive" ); - int is_recursive = recursive_value && - strcmp(recursive_value,"0") != 0 && - strcmp(recursive_value,"false") != 0 && - strcmp(recursive_value,"False") != 0 && - strcmp(recursive_value,"FALSE") != 0; - - assert( CV_IS_SEQ( seq )); - - if( !is_recursive ) - { - icvWriteSeq( fs, name, seq, attr, -1 ); - } - else - { - CvTreeNodeIterator tree_iterator; - - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_SEQ_TREE ); - cvStartWriteStruct( fs, "sequences", CV_NODE_SEQ ); - cvInitTreeNodeIterator( &tree_iterator, seq, INT_MAX ); - - for(;;) - { - if( !tree_iterator.node ) - break; - icvWriteSeq( fs, 0, tree_iterator.node, attr, tree_iterator.level ); - cvNextTreeNode( &tree_iterator ); - } - - cvEndWriteStruct( fs ); - cvEndWriteStruct( fs ); - } -} - - -static void* icvReadSeq( CvFileStorage* fs, CvFileNode* node ) -{ - void* ptr = 0; - CvSeq* seq; - CvSeqBlock* block; - CvFileNode *data, *header_node, *rect_node, *origin_node; - CvSeqReader reader; - int total, flags; - int elem_size, header_size = sizeof(CvSeq); - int fmt_pairs[CV_FS_MAX_FMT_PAIRS], i, fmt_pair_count; - int items_per_elem = 0; - const char* flags_str; - const char* header_dt; - const char* dt; - char* endptr = 0; - - flags_str = cvReadStringByName( fs, node, "flags", 0 ); - total = cvReadIntByName( fs, node, "count", -1 ); - dt = cvReadStringByName( fs, node, "dt", 0 ); - - if( !flags_str || total == -1 || !dt ) - CV_Error( CV_StsError, "Some of essential sequence attributes are absent" ); - - flags = CV_SEQ_MAGIC_VAL; - - if( cv_isdigit(flags_str[0]) ) - { - const int OLD_SEQ_ELTYPE_BITS = 9; - const int OLD_SEQ_ELTYPE_MASK = (1 << OLD_SEQ_ELTYPE_BITS) - 1; - const int OLD_SEQ_KIND_BITS = 3; - const int OLD_SEQ_KIND_MASK = ((1 << OLD_SEQ_KIND_BITS) - 1) << OLD_SEQ_ELTYPE_BITS; - const int OLD_SEQ_KIND_CURVE = 1 << OLD_SEQ_ELTYPE_BITS; - const int OLD_SEQ_FLAG_SHIFT = OLD_SEQ_KIND_BITS + OLD_SEQ_ELTYPE_BITS; - const int OLD_SEQ_FLAG_CLOSED = 1 << OLD_SEQ_FLAG_SHIFT; - const int OLD_SEQ_FLAG_HOLE = 8 << OLD_SEQ_FLAG_SHIFT; - - int flags0 = (int)strtol( flags_str, &endptr, 16 ); - if( endptr == flags_str || (flags0 & CV_MAGIC_MASK) != CV_SEQ_MAGIC_VAL ) - CV_Error( CV_StsError, "The sequence flags are invalid" ); - if( (flags0 & OLD_SEQ_KIND_MASK) == OLD_SEQ_KIND_CURVE ) - flags |= CV_SEQ_KIND_CURVE; - if( flags0 & OLD_SEQ_FLAG_CLOSED ) - flags |= CV_SEQ_FLAG_CLOSED; - if( flags0 & OLD_SEQ_FLAG_HOLE ) - flags |= CV_SEQ_FLAG_HOLE; - flags |= flags0 & OLD_SEQ_ELTYPE_MASK; - } - else - { - if( strstr(flags_str, "curve") ) - flags |= CV_SEQ_KIND_CURVE; - if( strstr(flags_str, "closed") ) - flags |= CV_SEQ_FLAG_CLOSED; - if( strstr(flags_str, "hole") ) - flags |= CV_SEQ_FLAG_HOLE; - if( !strstr(flags_str, "untyped") ) - { - CV_TRY - { - flags |= icvDecodeSimpleFormat(dt); - } - CV_CATCH_ALL - { - } - } - } - - header_dt = cvReadStringByName( fs, node, "header_dt", 0 ); - header_node = cvGetFileNodeByName( fs, node, "header_user_data" ); - - if( (header_dt != 0) ^ (header_node != 0) ) - CV_Error( CV_StsError, - "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" ); - - rect_node = cvGetFileNodeByName( fs, node, "rect" ); - origin_node = cvGetFileNodeByName( fs, node, "origin" ); - - if( (header_node != 0) + (rect_node != 0) + (origin_node != 0) > 1 ) - CV_Error( CV_StsError, "Only one of \"header_user_data\", \"rect\" and \"origin\" tags may occur" ); - - if( header_dt ) - { - header_size = icvCalcElemSize( header_dt, header_size ); - } - else if( rect_node ) - header_size = sizeof(CvPoint2DSeq); - else if( origin_node ) - header_size = sizeof(CvChain); - - elem_size = icvCalcElemSize( dt, 0 ); - seq = cvCreateSeq( flags, header_size, elem_size, fs->dststorage ); - - if( header_node ) - { - CV_Assert(header_dt); - cvReadRawData( fs, header_node, (char*)seq + sizeof(CvSeq), header_dt ); - } - else if( rect_node ) - { - CvPoint2DSeq* point_seq = (CvPoint2DSeq*)seq; - point_seq->rect.x = cvReadIntByName( fs, rect_node, "x", 0 ); - point_seq->rect.y = cvReadIntByName( fs, rect_node, "y", 0 ); - point_seq->rect.width = cvReadIntByName( fs, rect_node, "width", 0 ); - point_seq->rect.height = cvReadIntByName( fs, rect_node, "height", 0 ); - point_seq->color = cvReadIntByName( fs, node, "color", 0 ); - } - else if( origin_node ) - { - CvChain* chain = (CvChain*)seq; - chain->origin.x = cvReadIntByName( fs, origin_node, "x", 0 ); - chain->origin.y = cvReadIntByName( fs, origin_node, "y", 0 ); - } - - cvSeqPushMulti( seq, 0, total, 0 ); - fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); - fmt_pair_count *= 2; - for( i = 0; i < fmt_pair_count; i += 2 ) - items_per_elem += fmt_pairs[i]; - - data = cvGetFileNodeByName( fs, node, "data" ); - if( !data ) - CV_Error( CV_StsError, "The image data is not found in file storage" ); - - if( icvFileNodeSeqLen( data ) != total*items_per_elem ) - CV_Error( CV_StsError, "The number of stored elements does not match to \"count\"" ); - - cvStartReadRawData( fs, data, &reader ); - for( block = seq->first; block; block = block->next ) - { - int delta = block->count*items_per_elem; - cvReadRawDataSlice( fs, &reader, delta, block->data, dt ); - if( block == seq->first->prev ) - break; - } - - ptr = seq; - return ptr; -} - - -static void* icvReadSeqTree( CvFileStorage* fs, CvFileNode* node ) -{ - void* ptr = 0; - CvFileNode *sequences_node = cvGetFileNodeByName( fs, node, "sequences" ); - CvSeq* sequences; - CvSeq* root = 0; - CvSeq* parent = 0; - CvSeq* prev_seq = 0; - CvSeqReader reader; - int i, total; - int prev_level = 0; - - if( !sequences_node || !CV_NODE_IS_SEQ(sequences_node->tag) ) - CV_Error( CV_StsParseError, - "opencv-sequence-tree instance should contain a field \"sequences\" that should be a sequence" ); - - sequences = sequences_node->data.seq; - total = sequences->total; - - cvStartReadSeq( sequences, &reader, 0 ); - for( i = 0; i < total; i++ ) - { - CvFileNode* elem = (CvFileNode*)reader.ptr; - CvSeq* seq; - int level; - seq = (CvSeq*)cvRead( fs, elem ); - CV_Assert(seq); - level = cvReadIntByName( fs, elem, "level", -1 ); - if( level < 0 ) - CV_Error( CV_StsParseError, "All the sequence tree nodes should contain \"level\" field" ); - if( !root ) - root = seq; - if( level > prev_level ) - { - assert( level == prev_level + 1 ); - parent = prev_seq; - prev_seq = 0; - if( parent ) - parent->v_next = seq; - } - else if( level < prev_level ) - { - for( ; prev_level > level; prev_level-- ) - prev_seq = prev_seq->v_prev; - parent = prev_seq->v_prev; - } - seq->h_prev = prev_seq; - if( prev_seq ) - prev_seq->h_next = seq; - seq->v_prev = parent; - prev_seq = seq; - prev_level = level; - CV_NEXT_SEQ_ELEM( sequences->elem_size, reader ); - } - - ptr = root; - return ptr; -} - -/******************************* CvGraph ******************************/ - -static int icvIsGraph( const void* ptr ) -{ - return CV_IS_GRAPH(ptr); -} - - -static void icvReleaseGraph( void** ptr ) -{ - if( !ptr ) - CV_Error( CV_StsNullPtr, "NULL double pointer" ); - - *ptr = 0; // it's impossible now to release graph, so just clear the pointer -} - - -static void* icvCloneGraph( const void* ptr ) -{ - return cvCloneGraph( (const CvGraph*)ptr, 0 ); -} - - -static void icvWriteGraph( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList attr ) -{ - int* flag_buf = 0; - char* write_buf = 0; - const CvGraph* graph = (const CvGraph*)struct_ptr; - CvSeqReader reader; - char buf[128]; - int i, k, vtx_count, edge_count; - char vtx_dt_buf[128], *vtx_dt; - char edge_dt_buf[128], *edge_dt; - int write_buf_size; - - assert( CV_IS_GRAPH(graph) ); - vtx_count = cvGraphGetVtxCount( graph ); - edge_count = cvGraphGetEdgeCount( graph ); - flag_buf = (int*)cvAlloc( vtx_count*sizeof(flag_buf[0])); - - // count vertices - cvStartReadSeq( (CvSeq*)graph, &reader ); - for( i = 0, k = 0; i < graph->total; i++ ) - { - if( CV_IS_SET_ELEM( reader.ptr )) - { - CvGraphVtx* vtx = (CvGraphVtx*)reader.ptr; - flag_buf[k] = vtx->flags; - vtx->flags = k++; - } - CV_NEXT_SEQ_ELEM( graph->elem_size, reader ); - } - - // write header - cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_GRAPH ); - - cvWriteString(fs, "flags", CV_IS_GRAPH_ORIENTED(graph) ? "oriented" : "", 1); - - cvWriteInt( fs, "vertex_count", vtx_count ); - vtx_dt = icvGetFormat( (CvSeq*)graph, "vertex_dt", - &attr, sizeof(CvGraphVtx), vtx_dt_buf ); - if( vtx_dt ) - cvWriteString( fs, "vertex_dt", vtx_dt, 0 ); - - cvWriteInt( fs, "edge_count", edge_count ); - edge_dt = icvGetFormat( (CvSeq*)graph->edges, "edge_dt", - &attr, sizeof(CvGraphEdge), buf ); - sprintf( edge_dt_buf, "2if%s", edge_dt ? edge_dt : "" ); - edge_dt = edge_dt_buf; - cvWriteString( fs, "edge_dt", edge_dt, 0 ); - - icvWriteHeaderData( fs, (CvSeq*)graph, &attr, sizeof(CvGraph) ); - - write_buf_size = MAX( 3*graph->elem_size, 1 << 16 ); - write_buf_size = MAX( 3*graph->edges->elem_size, write_buf_size ); - write_buf = (char*)cvAlloc( write_buf_size ); - - // as vertices and edges are written in similar way, - // do it as a parametrized 2-iteration loop - for( k = 0; k < 2; k++ ) - { - const char* dt = k == 0 ? vtx_dt : edge_dt; - if( dt ) - { - CvSet* data = k == 0 ? (CvSet*)graph : graph->edges; - int elem_size = data->elem_size; - int write_elem_size = icvCalcElemSize( dt, 0 ); - char* src_ptr = write_buf; - int write_max = write_buf_size / write_elem_size, write_count = 0; - - // alignment of user part of the edge data following 2if - int edge_user_align = sizeof(float); - - if( k == 1 ) - { - int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count; - fmt_pair_count = icvDecodeFormat( dt, fmt_pairs, CV_FS_MAX_FMT_PAIRS ); - if( fmt_pair_count > 2 && CV_ELEM_SIZE(fmt_pairs[2*2+1]) >= (int)sizeof(double)) - edge_user_align = sizeof(double); - } - - cvStartWriteStruct( fs, k == 0 ? "vertices" : "edges", - CV_NODE_SEQ + CV_NODE_FLOW ); - cvStartReadSeq( (CvSeq*)data, &reader ); - for( i = 0; i < data->total; i++ ) - { - if( CV_IS_SET_ELEM( reader.ptr )) - { - if( k == 0 ) // vertices - memcpy( src_ptr, reader.ptr + sizeof(CvGraphVtx), write_elem_size ); - else - { - CvGraphEdge* edge = (CvGraphEdge*)reader.ptr; - src_ptr = (char*)cvAlignPtr( src_ptr, sizeof(int) ); - ((int*)src_ptr)[0] = edge->vtx[0]->flags; - ((int*)src_ptr)[1] = edge->vtx[1]->flags; - *(float*)(src_ptr + sizeof(int)*2) = edge->weight; - if( elem_size > (int)sizeof(CvGraphEdge) ) - { - char* src_ptr2 = (char*)cvAlignPtr( src_ptr + 2*sizeof(int) - + sizeof(float), edge_user_align ); - memcpy( src_ptr2, edge + 1, elem_size - sizeof(CvGraphEdge) ); - } - } - src_ptr += write_elem_size; - if( ++write_count >= write_max ) - { - cvWriteRawData( fs, write_buf, write_count, dt ); - write_count = 0; - src_ptr = write_buf; - } - } - CV_NEXT_SEQ_ELEM( data->elem_size, reader ); - } - - if( write_count > 0 ) - cvWriteRawData( fs, write_buf, write_count, dt ); - cvEndWriteStruct( fs ); - } - } - - cvEndWriteStruct( fs ); - - // final stage. restore the graph flags - cvStartReadSeq( (CvSeq*)graph, &reader ); - vtx_count = 0; - for( i = 0; i < graph->total; i++ ) - { - if( CV_IS_SET_ELEM( reader.ptr )) - ((CvGraphVtx*)reader.ptr)->flags = flag_buf[vtx_count++]; - CV_NEXT_SEQ_ELEM( graph->elem_size, reader ); - } - - cvFree( &write_buf ); - cvFree( &flag_buf ); -} - - -static void* icvReadGraph( CvFileStorage* fs, CvFileNode* node ) -{ - void* ptr = 0; - char* read_buf = 0; - CvGraphVtx** vtx_buf = 0; - CvGraph* graph; - CvFileNode *header_node, *vtx_node, *edge_node; - int flags, vtx_count, edge_count; - int vtx_size = sizeof(CvGraphVtx), edge_size, header_size = sizeof(CvGraph); - int src_vtx_size = 0, src_edge_size; - int fmt_pairs[CV_FS_MAX_FMT_PAIRS], fmt_pair_count; - int vtx_items_per_elem = 0, edge_items_per_elem = 0; - int edge_user_align = sizeof(float); - int read_buf_size; - int i, k; - const char* flags_str; - const char* header_dt; - const char* vtx_dt; - const char* edge_dt; - char* endptr = 0; - - flags_str = cvReadStringByName( fs, node, "flags", 0 ); - vtx_dt = cvReadStringByName( fs, node, "vertex_dt", 0 ); - edge_dt = cvReadStringByName( fs, node, "edge_dt", 0 ); - vtx_count = cvReadIntByName( fs, node, "vertex_count", -1 ); - edge_count = cvReadIntByName( fs, node, "edge_count", -1 ); - - if( !flags_str || vtx_count == -1 || edge_count == -1 || !edge_dt ) - CV_Error( CV_StsError, "Some of essential graph attributes are absent" ); - - flags = CV_SET_MAGIC_VAL + CV_GRAPH; - - if( isxdigit(flags_str[0]) ) - { - const int OLD_SEQ_ELTYPE_BITS = 9; - const int OLD_SEQ_KIND_BITS = 3; - const int OLD_SEQ_FLAG_SHIFT = OLD_SEQ_KIND_BITS + OLD_SEQ_ELTYPE_BITS; - const int OLD_GRAPH_FLAG_ORIENTED = 1 << OLD_SEQ_FLAG_SHIFT; - - int flags0 = (int)strtol( flags_str, &endptr, 16 ); - if( endptr == flags_str || (flags0 & CV_MAGIC_MASK) != CV_SET_MAGIC_VAL ) - CV_Error( CV_StsError, "The sequence flags are invalid" ); - if( flags0 & OLD_GRAPH_FLAG_ORIENTED ) - flags |= CV_GRAPH_FLAG_ORIENTED; - } - else - { - if( strstr(flags_str, "oriented") ) - flags |= CV_GRAPH_FLAG_ORIENTED; - } - - header_dt = cvReadStringByName( fs, node, "header_dt", 0 ); - header_node = cvGetFileNodeByName( fs, node, "header_user_data" ); - - if( (header_dt != 0) ^ (header_node != 0) ) - CV_Error( CV_StsError, - "One of \"header_dt\" and \"header_user_data\" is there, while the other is not" ); - - if( header_dt ) - header_size = icvCalcElemSize( header_dt, header_size ); - - if( vtx_dt ) - { - src_vtx_size = icvCalcElemSize( vtx_dt, 0 ); - vtx_size = icvCalcElemSize( vtx_dt, vtx_size ); - fmt_pair_count = icvDecodeFormat( edge_dt, - fmt_pairs, CV_FS_MAX_FMT_PAIRS ); - fmt_pair_count *= 2; - for( i = 0; i < fmt_pair_count; i += 2 ) - vtx_items_per_elem += fmt_pairs[i]; - } - - { - char dst_edge_dt_buf[128]; - const char* dst_edge_dt = 0; - - fmt_pair_count = icvDecodeFormat( edge_dt, - fmt_pairs, CV_FS_MAX_FMT_PAIRS ); - if( fmt_pair_count < 2 || - fmt_pairs[0] != 2 || fmt_pairs[1] != CV_32S || - fmt_pairs[2] < 1 || fmt_pairs[3] != CV_32F ) - CV_Error( CV_StsBadArg, - "Graph edges should start with 2 integers and a float" ); - - // alignment of user part of the edge data following 2if - if( fmt_pair_count > 2 && CV_ELEM_SIZE(fmt_pairs[5]) >= (int)sizeof(double)) - edge_user_align = sizeof(double); - - fmt_pair_count *= 2; - for( i = 0; i < fmt_pair_count; i += 2 ) - edge_items_per_elem += fmt_pairs[i]; - - if( edge_dt[2] == 'f' || (edge_dt[2] == '1' && edge_dt[3] == 'f') ) - dst_edge_dt = edge_dt + 3 + cv_isdigit(edge_dt[2]); - else + FileNode first_node = *(node.begin()); + 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) { - int val = (int)strtol( edge_dt + 2, &endptr, 10 ); - sprintf( dst_edge_dt_buf, "%df%s", val-1, endptr ); - dst_edge_dt = dst_edge_dt_buf; + (*it) >> matches[i]; } - - edge_size = icvCalcElemSize( dst_edge_dt, sizeof(CvGraphEdge) ); - src_edge_size = icvCalcElemSize( edge_dt, 0 ); +#else + FileNodeIterator it = node.begin(); + it >> matches; +#endif + return; } - - graph = cvCreateGraph( flags, header_size, vtx_size, edge_size, fs->dststorage ); - - if( header_node ) - { - CV_Assert(header_dt); - cvReadRawData( fs, header_node, (char*)graph + sizeof(CvGraph), header_dt ); - } - - read_buf_size = MAX( src_vtx_size*3, 1 << 16 ); - read_buf_size = MAX( src_edge_size*3, read_buf_size ); - read_buf = (char*)cvAlloc( read_buf_size ); - vtx_buf = (CvGraphVtx**)cvAlloc( vtx_count * sizeof(vtx_buf[0]) ); - - vtx_node = cvGetFileNodeByName( fs, node, "vertices" ); - edge_node = cvGetFileNodeByName( fs, node, "edges" ); - if( !edge_node ) - CV_Error( CV_StsBadArg, "No edges data" ); - if( vtx_dt && !vtx_node ) - CV_Error( CV_StsBadArg, "No vertices data" ); - - // as vertices and edges are read in similar way, - // do it as a parametrized 2-iteration loop - for( k = 0; k < 2; k++ ) + matches.clear(); + FileNodeIterator it = node.begin(), it_end = node.end(); + for( ; it != it_end; ) { - const char* dt = k == 0 ? vtx_dt : edge_dt; - int elem_size = k == 0 ? vtx_size : edge_size; - int src_elem_size = k == 0 ? src_vtx_size : src_edge_size; - int items_per_elem = k == 0 ? vtx_items_per_elem : edge_items_per_elem; - int elem_count = k == 0 ? vtx_count : edge_count; - char* dst_ptr = read_buf; - int read_max = read_buf_size /MAX(src_elem_size, 1), read_count = 0; - CvSeqReader reader; - if(dt) - cvStartReadRawData( fs, k == 0 ? vtx_node : edge_node, &reader ); - - for( i = 0; i < elem_count; i++ ) - { - if( read_count == 0 && dt ) - { - int count = MIN( elem_count - i, read_max )*items_per_elem; - cvReadRawDataSlice( fs, &reader, count, read_buf, dt ); - read_count = count; - dst_ptr = read_buf; - } - - if( k == 0 ) - { - CvGraphVtx* vtx; - cvGraphAddVtx( graph, 0, &vtx ); - vtx_buf[i] = vtx; - if( dt ) - memcpy( vtx + 1, dst_ptr, src_elem_size ); - } - else - { - CvGraphEdge* edge = 0; - int vtx1 = ((int*)dst_ptr)[0]; - int vtx2 = ((int*)dst_ptr)[1]; - int result; - - if( (unsigned)vtx1 >= (unsigned)vtx_count || - (unsigned)vtx2 >= (unsigned)vtx_count ) - CV_Error( CV_StsOutOfRange, - "Some of stored vertex indices are out of range" ); - - result = cvGraphAddEdgeByPtr( graph, - vtx_buf[vtx1], vtx_buf[vtx2], 0, &edge ); - - if( result == 0 ) - CV_Error( CV_StsBadArg, "Duplicated edge has occurred" ); - - edge->weight = *(float*)(dst_ptr + sizeof(int)*2); - if( elem_size > (int)sizeof(CvGraphEdge) ) - { - char* dst_ptr2 = (char*)cvAlignPtr( dst_ptr + sizeof(int)*2 + - sizeof(float), edge_user_align ); - memcpy( edge + 1, dst_ptr2, elem_size - sizeof(CvGraphEdge) ); - } - } - - dst_ptr += src_elem_size; - read_count--; - } + DMatch m; + it >> m.queryIdx >> m.trainIdx >> m.imgIdx >> m.distance; + matches.push_back(m); } - - ptr = graph; - cvFree( &read_buf ); - cvFree( &vtx_buf ); - - return ptr; -} - -/****************************************************************************************\ -* RTTI Functions * -\****************************************************************************************/ - -CvTypeInfo *CvType::first = 0, *CvType::last = 0; - -CvType::CvType( const char* type_name, - CvIsInstanceFunc is_instance, CvReleaseFunc release, - CvReadFunc read, CvWriteFunc write, CvCloneFunc clone ) -{ - CvTypeInfo _info; - _info.flags = 0; - _info.header_size = sizeof(_info); - _info.type_name = type_name; - _info.prev = _info.next = 0; - _info.is_instance = is_instance; - _info.release = release; - _info.clone = clone; - _info.read = read; - _info.write = write; - - cvRegisterType( &_info ); - info = first; } +#endif -CvType::~CvType() -{ - cvUnregisterType( info->type_name ); } - - -CvType seq_type( CV_TYPE_NAME_SEQ, icvIsSeq, icvReleaseSeq, icvReadSeq, - icvWriteSeqTree /* this is the entry point for - writing a single sequence too */, icvCloneSeq ); - -CvType seq_tree_type( CV_TYPE_NAME_SEQ_TREE, icvIsSeq, icvReleaseSeq, - icvReadSeqTree, icvWriteSeqTree, icvCloneSeq ); - -CvType seq_graph_type( CV_TYPE_NAME_GRAPH, icvIsGraph, icvReleaseGraph, - icvReadGraph, icvWriteGraph, icvCloneGraph ); - -CvType sparse_mat_type( CV_TYPE_NAME_SPARSE_MAT, icvIsSparseMat, - (CvReleaseFunc)cvReleaseSparseMat, icvReadSparseMat, - icvWriteSparseMat, (CvCloneFunc)cvCloneSparseMat ); - -CvType image_type( CV_TYPE_NAME_IMAGE, icvIsImage, (CvReleaseFunc)cvReleaseImage, - icvReadImage, icvWriteImage, (CvCloneFunc)cvCloneImage ); - -CvType mat_type( CV_TYPE_NAME_MAT, icvIsMat, (CvReleaseFunc)cvReleaseMat, - icvReadMat, icvWriteMat, (CvCloneFunc)cvCloneMat ); - -CvType matnd_type( CV_TYPE_NAME_MATND, icvIsMatND, (CvReleaseFunc)cvReleaseMatND, - icvReadMatND, icvWriteMatND, (CvCloneFunc)cvCloneMatND ); diff --git a/modules/core/src/persistence_xml.cpp b/modules/core/src/persistence_xml.cpp index 8928a83..fb30d90 100644 --- a/modules/core/src/persistence_xml.cpp +++ b/modules/core/src/persistence_xml.cpp @@ -6,1038 +6,822 @@ #include "precomp.hpp" #include "persistence.hpp" -#define CV_XML_INDENT 2 -#define CV_XML_INSIDE_COMMENT 1 -#define CV_XML_INSIDE_TAG 2 -#define CV_XML_INSIDE_DIRECTIVE 3 -#define CV_XML_OPENING_TAG 1 -#define CV_XML_CLOSING_TAG 2 -#define CV_XML_EMPTY_TAG 3 -#define CV_XML_HEADER_TAG 4 -#define CV_XML_DIRECTIVE_TAG 5 - -/****************************************************************************************\ -* XML Parser * -\****************************************************************************************/ - -static char* -icvXMLSkipSpaces( CvFileStorage* fs, char* ptr, int mode ) +enum +{ + CV_XML_INDENT = 2, + CV_XML_INSIDE_COMMENT = 1, + CV_XML_INSIDE_TAG = 2, + CV_XML_INSIDE_DIRECTIVE = 3, + CV_XML_OPENING_TAG = 1, + CV_XML_CLOSING_TAG = 2, + CV_XML_EMPTY_TAG = 3, + CV_XML_HEADER_TAG = 4, + CV_XML_DIRECTIVE_TAG = 5 +}; + +namespace cv { - int level = 0; - for(;;) +class XMLEmitter : public FileStorageEmitter +{ +public: + XMLEmitter(FileStorage_API* _fs) : fs(_fs) { - char c; - ptr--; - - if( mode == CV_XML_INSIDE_COMMENT ) - { - do c = *++ptr; - while( cv_isprint_or_tab(c) && (c != '-' || ptr[1] != '-' || ptr[2] != '>') ); + } + virtual ~XMLEmitter() {} - if( c == '-' ) - { - assert( ptr[1] == '-' && ptr[2] == '>' ); - mode = 0; - ptr += 3; - } - } - else if( mode == CV_XML_INSIDE_DIRECTIVE ) - { - // !!!NOTE!!! This is not quite correct, but should work in most cases - do - { - c = *++ptr; - level += c == '<'; - level -= c == '>'; - if( level < 0 ) - return ptr; - } while( cv_isprint_or_tab(c) ); - } - else - { - do c = *++ptr; - while( c == ' ' || c == '\t' ); + void writeTag( const char* key, int tag_type, const std::vector& attrlist=std::vector() ) + { + char* ptr = fs->bufferPtr(); + int i, len = 0; + FStructData& current_struct = fs->getCurrentStruct(); + int struct_flags = current_struct.flags; - if( c == '<' && ptr[1] == '!' && ptr[2] == '-' && ptr[3] == '-' ) - { - if( mode != 0 ) - CV_PARSE_ERROR( "Comments are not allowed here" ); - mode = CV_XML_INSIDE_COMMENT; - ptr += 4; - } - else if( cv_isprint(c) ) - break; - } + if( key && key[0] == '\0' ) + key = 0; - if( !cv_isprint(*ptr) ) + if( tag_type == CV_XML_OPENING_TAG || tag_type == CV_XML_EMPTY_TAG ) { - int max_size = (int)(fs->buffer_end - fs->buffer_start); - if( *ptr != '\0' && *ptr != '\n' && *ptr != '\r' ) - CV_PARSE_ERROR( "Invalid character in the stream" ); - ptr = icvGets( fs, fs->buffer_start, max_size ); - if( !ptr ) + if( FileNode::isCollection(struct_flags) ) { - ptr = fs->buffer_start; // FIXIT Why do we need this hack? What is about other parsers JSON/YAML? - *ptr = '\0'; - fs->dummy_eof = 1; - break; + if( FileNode::isMap(struct_flags) ^ (key != 0) ) + CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, " + "or add element with key to sequence" ); } else { - int l = (int)strlen(ptr); - if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) ) - CV_PARSE_ERROR( "Too long string or a last string w/o newline" ); + struct_flags = FileNode::EMPTY + (key ? FileNode::MAP : FileNode::SEQ); + //fs->is_first = 0; } - fs->lineno++; // FIXIT doesn't really work with long lines. It must be counted via '\n' or '\r' symbols, not the number of icvGets() calls. + + if( !FileNode::isEmptyCollection(struct_flags) ) + ptr = fs->flush(); } - } - return ptr; -} + if( !key ) + key = "_"; + else if( key[0] == '_' && key[1] == '\0' ) + CV_Error( CV_StsBadArg, "A single _ is a reserved tag name" ); -static void icvXMLGetMultilineStringContent(CvFileStorage* fs, - char* ptr, char* &beg, char* &end) -{ - ptr = icvXMLSkipSpaces(fs, ptr, CV_XML_INSIDE_TAG); - beg = ptr; - end = ptr; - if ( fs->dummy_eof ) - return ; /* end of file */ - - if ( *beg == '<' ) - return; /* end of string */ - - /* find end */ - while( cv_isprint(*ptr) ) /* no check for base64 string */ - ++ ptr; - if ( *ptr == '\0' ) - CV_PARSE_ERROR( "Unexpected end of line" ); - - end = ptr; -} - + len = (int)strlen( key ); + *ptr++ = '<'; + if( tag_type == CV_XML_CLOSING_TAG ) + { + if( !attrlist.empty() ) + CV_Error( CV_StsBadArg, "Closing tag should not include any attributes" ); + *ptr++ = '/'; + } -static char* icvXMLParseBase64(CvFileStorage* fs, char* ptr, CvFileNode * node) -{ - char * beg = 0; - char * end = 0; + if( !cv_isalpha(key[0]) && key[0] != '_' ) + CV_Error( CV_StsBadArg, "Key should start with a letter or _" ); - icvXMLGetMultilineStringContent(fs, ptr, beg, end); - if (beg >= end) - return end; // CV_PARSE_ERROR("Empty Binary Data"); + ptr = fs->resizeWriteBuffer( ptr, len ); + for( i = 0; i < len; i++ ) + { + char c = key[i]; + if( !cv_isalnum(c) && c != '_' && c != '-' ) + CV_Error( CV_StsBadArg, "Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_'" ); + ptr[i] = c; + } + ptr += len; - /* calc (decoded) total_byte_size from header */ - std::string dt; - { - if (end - beg < static_cast(base64::ENCODED_HEADER_SIZE)) - CV_PARSE_ERROR("Unrecognized Base64 header"); + int nattr = (int)attrlist.size(); + CV_Assert( nattr % 2 == 0 ); - std::vector header(base64::HEADER_SIZE + 1, ' '); - base64::base64_decode(beg, header.data(), 0U, base64::ENCODED_HEADER_SIZE); - if ( !base64::read_base64_header(header, dt) || dt.empty() ) - CV_PARSE_ERROR("Invalid `dt` in Base64 header"); + for( i = 0; i < nattr; i += 2 ) + { + size_t len0 = attrlist[i].size(); + size_t len1 = attrlist[i+1].size(); + CV_Assert( len0 > 0 ); - beg += base64::ENCODED_HEADER_SIZE; - } + ptr = fs->resizeWriteBuffer( ptr, (int)(len0 + len1 + 4) ); + *ptr++ = ' '; - /* get all Base64 data */ - std::string base64_buffer; // not an efficient way. - base64_buffer.reserve( PARSER_BASE64_BUFFER_SIZE ); - while( beg < end ) - { - base64_buffer.append( beg, end ); - beg = end; - icvXMLGetMultilineStringContent( fs, beg, beg, end ); - } - if ( base64_buffer.empty() || - !base64::base64_valid(base64_buffer.data(), 0U, base64_buffer.size()) ) - CV_PARSE_ERROR( "Invalid Base64 data." ); - - /* alloc buffer for all decoded data(include header) */ - std::vector binary_buffer( base64::base64_decode_buffer_size(base64_buffer.size()) ); - int total_byte_size = static_cast( - base64::base64_decode_buffer_size( base64_buffer.size(), base64_buffer.data(), false ) - ); - { - base64::Base64ContextParser parser(binary_buffer.data(), binary_buffer.size() ); - const uchar * buffer_beg = reinterpret_cast( base64_buffer.data() ); - const uchar * buffer_end = buffer_beg + base64_buffer.size(); - parser.read( buffer_beg, buffer_end ); - parser.flush(); - } + memcpy( ptr, attrlist[i].c_str(), len0 ); + ptr += len0; + *ptr++ = '='; + *ptr++ = '\"'; + if( len1 > 0 ) + memcpy( ptr, attrlist[i+1].c_str(), len1 ); + ptr += len1; + *ptr++ = '\"'; + } - /* save as CvSeq */ - int elem_size = ::icvCalcStructSize(dt.c_str(), 0); - if (total_byte_size % elem_size != 0) - CV_PARSE_ERROR("data size not matches elememt size"); - int elem_cnt = total_byte_size / elem_size; - - node->tag = CV_NODE_NONE; - int struct_flags = CV_NODE_SEQ; - /* after icvFSCreateCollection, node->tag == struct_flags */ - icvFSCreateCollection(fs, struct_flags, node); - base64::make_seq(binary_buffer.data(), elem_cnt, dt.c_str(), *node->data.seq); - - if (fs->dummy_eof) { - /* end of file */ - return fs->buffer_start; - } else { - /* end of line */ - return end; + if( tag_type == CV_XML_EMPTY_TAG ) + *ptr++ = '/'; + *ptr++ = '>'; + fs->setBufferPtr(ptr); + current_struct.flags = struct_flags & ~FileNode::EMPTY; } -} - -static char* -icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag, - CvAttrList** _list, int* _tag_type ); - -static char* -icvXMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node, - int value_type CV_DEFAULT(CV_NODE_NONE)) -{ - CvFileNode *elem = node; - bool have_space = true, is_simple = true; - int is_user_type = CV_NODE_IS_USER(value_type); - memset( node, 0, sizeof(*node) ); - - value_type = CV_NODE_TYPE(value_type); - - for(;;) + FStructData startWriteStruct(const FStructData& parent, const char* key, + int struct_flags, const char* type_name=0) { - char c = *ptr, d; - char* endptr; - - if( cv_isspace(c) || c == '\0' || (c == '<' && ptr[1] == '!' && ptr[2] == '-') ) // FIXIT ptr[1], ptr[2] - out of bounds read without check or data fetch (#11061) + std::vector attrlist; + if( type_name && *type_name ) { - ptr = icvXMLSkipSpaces( fs, ptr, 0 ); - have_space = true; - c = *ptr; + attrlist.push_back("type_id"); + attrlist.push_back(type_name); } - d = ptr[1]; // FIXIT ptr[1] - out of bounds read without check or data fetch (#11061) + writeTag( key, CV_XML_OPENING_TAG, attrlist ); - if( c =='<' || c == '\0' ) - { - CvStringHashNode *key = 0, *key2 = 0; - CvAttrList* list = 0; - CvTypeInfo* info = 0; - int tag_type = 0; - int is_noname = 0; - const char* type_name = 0; - int elem_type = CV_NODE_NONE; - - if( d == '/' || c == '\0' ) - break; + FStructData current_struct; + current_struct.tag = key ? std::string(key) : std::string(); + current_struct.flags = struct_flags; + current_struct.indent = parent.indent + CV_XML_INDENT; - ptr = icvXMLParseTag( fs, ptr, &key, &list, &tag_type ); + return current_struct; + } - if( tag_type == CV_XML_DIRECTIVE_TAG ) - CV_PARSE_ERROR( "Directive tags are not allowed here" ); - if( tag_type == CV_XML_EMPTY_TAG ) - CV_PARSE_ERROR( "Empty tags are not supported" ); + void endWriteStruct(const FStructData& current_struct) + { + writeTag( current_struct.tag.c_str(), CV_XML_CLOSING_TAG ); + } - CV_Assert(tag_type == CV_XML_OPENING_TAG); + void write(const char* key, int value) + { + char buf[128], *ptr = fs::itoa( value, buf, 10 ); + writeScalar( key, ptr); + } - /* for base64 string */ - bool is_binary_string = false; + void write( const char* key, double value ) + { + char buf[128]; + writeScalar( key, fs::doubleToString( buf, value, false ) ); + } - type_name = list ? cvAttrValue( list, "type_id" ) : 0; - if( type_name ) - { - if( strcmp( type_name, "str" ) == 0 ) - elem_type = CV_NODE_STRING; - else if( strcmp( type_name, "map" ) == 0 ) - elem_type = CV_NODE_MAP; - else if( strcmp( type_name, "seq" ) == 0 ) - elem_type = CV_NODE_SEQ; - else if (strcmp(type_name, "binary") == 0) - { - elem_type = CV_NODE_NONE; - is_binary_string = true; - } - else - { - info = cvFindType( type_name ); - if( info ) - elem_type = CV_NODE_USER; - } - } + void write(const char* key, const char* str, bool quote) + { + char buf[CV_FS_MAX_LEN*6+16]; + char* data = (char*)str; + int i, len; - is_noname = key->str.len == 1 && key->str.ptr[0] == '_'; - if( !CV_NODE_IS_COLLECTION(node->tag) ) - { - icvFSCreateCollection( fs, is_noname ? CV_NODE_SEQ : CV_NODE_MAP, node ); - } - else if( is_noname ^ CV_NODE_IS_SEQ(node->tag) ) - CV_PARSE_ERROR( is_noname ? "Map element should have a name" : - "Sequence element should not have name (use <_>)" ); + if( !str ) + CV_Error( CV_StsNullPtr, "Null string pointer" ); - if( is_noname ) - elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 ); - else - elem = cvGetFileNode( fs, node, key, 1 ); - CV_Assert(elem); - if (!is_binary_string) - ptr = icvXMLParseValue( fs, ptr, elem, elem_type); - else { - /* for base64 string */ - ptr = icvXMLParseBase64( fs, ptr, elem); - ptr = icvXMLSkipSpaces( fs, ptr, 0 ); - } + len = (int)strlen(str); + if( len > CV_FS_MAX_LEN ) + CV_Error( CV_StsBadArg, "The written string is too long" ); - if( !is_noname ) - elem->tag |= CV_NODE_NAMED; - is_simple = is_simple && !CV_NODE_IS_COLLECTION(elem->tag); - elem->info = info; - ptr = icvXMLParseTag( fs, ptr, &key2, &list, &tag_type ); - if( tag_type != CV_XML_CLOSING_TAG || key2 != key ) - CV_PARSE_ERROR( "Mismatched closing tag" ); - have_space = true; - } - else + if( quote || len == 0 || str[0] != '\"' || str[0] != str[len-1] ) { - if( !have_space ) - CV_PARSE_ERROR( "There should be space between literals" ); - - elem = node; - if( node->tag != CV_NODE_NONE ) + bool need_quote = quote || len == 0; + data = buf; + *data++ = '\"'; + for( i = 0; i < len; i++ ) { - if( !CV_NODE_IS_COLLECTION(node->tag) ) - icvFSCreateCollection( fs, CV_NODE_SEQ, node ); + char c = str[i]; - elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 ); - elem->info = 0; - } - - if( value_type != CV_NODE_STRING && - (cv_isdigit(c) || ((c == '-' || c == '+') && - (cv_isdigit(d) || d == '.')) || (c == '.' && cv_isalnum(d))) ) // a number - { - double fval; - int ival; - endptr = ptr + (c == '-' || c == '+'); - while( cv_isdigit(*endptr) ) - endptr++; - if( *endptr == '.' || *endptr == 'e' ) - { - fval = icv_strtod( fs, ptr, &endptr ); - /*if( endptr == ptr || cv_isalpha(*endptr) ) - icvProcessSpecialDouble( fs, ptr, &fval, &endptr ));*/ - elem->tag = CV_NODE_REAL; - elem->data.f = fval; - } - else + if( (uchar)c >= 128 || c == ' ' ) { - ival = (int)strtol( ptr, &endptr, 0 ); - elem->tag = CV_NODE_INT; - elem->data.i = ival; + *data++ = c; + need_quote = true; } - - if( endptr == ptr ) - CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" ); - - ptr = endptr; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - } - else - { - // string - char buf[CV_FS_MAX_LEN+16] = {0}; - int i = 0, len, is_quoted = 0; - elem->tag = CV_NODE_STRING; - if( c == '\"' ) - is_quoted = 1; - else - --ptr; - - for( ;; ) + else if( !cv_isprint(c) || c == '<' || c == '>' || c == '&' || c == '\'' || c == '\"' ) { - c = *++ptr; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - if( !cv_isalnum(c) ) + *data++ = '&'; + if( c == '<' ) { - if( c == '\"' ) - { - if( !is_quoted ) - CV_PARSE_ERROR( "Literal \" is not allowed within a string. Use "" ); - ++ptr; - break; - } - else if( !cv_isprint(c) || c == '<' || (!is_quoted && cv_isspace(c))) - { - if( is_quoted ) - CV_PARSE_ERROR( "Closing \" is expected" ); - break; - } - else if( c == '\'' || c == '>' ) - { - CV_PARSE_ERROR( "Literal \' or > are not allowed. Use ' or >" ); - } - else if( c == '&' ) - { - if( *++ptr == '#' ) - { - int val, base = 10; - ptr++; - if( *ptr == 'x' ) - { - base = 16; - ptr++; - } - val = (int)strtol( ptr, &endptr, base ); - if( (unsigned)val > (unsigned)255 || - !endptr || *endptr != ';' ) - CV_PARSE_ERROR( "Invalid numeric value in the string" ); - c = (char)val; - } - else - { - endptr = ptr; - do c = *++endptr; - while( cv_isalnum(c) ); - if( c != ';' ) - CV_PARSE_ERROR( "Invalid character in the symbol entity name" ); - len = (int)(endptr - ptr); - if( len == 2 && memcmp( ptr, "lt", len ) == 0 ) - c = '<'; - else if( len == 2 && memcmp( ptr, "gt", len ) == 0 ) - c = '>'; - else if( len == 3 && memcmp( ptr, "amp", len ) == 0 ) - c = '&'; - else if( len == 4 && memcmp( ptr, "apos", len ) == 0 ) - c = '\''; - else if( len == 4 && memcmp( ptr, "quot", len ) == 0 ) - c = '\"'; - else - { - memcpy( buf + i, ptr-1, len + 2 ); - i += len + 2; - } - } - ptr = endptr; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - } + memcpy(data, "lt", 2); + data += 2; } - buf[i++] = c; - if( i >= CV_FS_MAX_LEN ) - CV_PARSE_ERROR( "Too long string literal" ); + else if( c == '>' ) + { + memcpy(data, "gt", 2); + data += 2; + } + else if( c == '&' ) + { + memcpy(data, "amp", 3); + data += 3; + } + else if( c == '\'' ) + { + memcpy(data, "apos", 4); + data += 4; + } + else if( c == '\"' ) + { + memcpy( data, "quot", 4); + data += 4; + } + else + { + sprintf( data, "#x%02x", (uchar)c ); + data += 4; + } + *data++ = ';'; + need_quote = 1; } - elem->data.str = cvMemStorageAllocString( fs->memstorage, buf, i ); + else + *data++ = c; } - - if( !CV_NODE_IS_COLLECTION(value_type) && value_type != CV_NODE_NONE ) - break; - have_space = false; + if( !need_quote && (cv_isdigit(str[0]) || + str[0] == '+' || str[0] == '-' || str[0] == '.' )) + need_quote = true; + + if( need_quote ) + *data++ = '\"'; + len = (int)(data - buf) - !need_quote; + *data++ = '\0'; + data = buf + (int)!need_quote; } - } - if( (CV_NODE_TYPE(node->tag) == CV_NODE_NONE || - (CV_NODE_TYPE(node->tag) != value_type && - !CV_NODE_IS_COLLECTION(node->tag))) && - CV_NODE_IS_COLLECTION(value_type) ) - { - icvFSCreateCollection( fs, CV_NODE_IS_MAP(value_type) ? - CV_NODE_MAP : CV_NODE_SEQ, node ); + writeScalar( key, data ); } - if( value_type != CV_NODE_NONE && - value_type != CV_NODE_TYPE(node->tag) ) - CV_PARSE_ERROR( "The actual type is different from the specified type" ); - - if( CV_NODE_IS_COLLECTION(node->tag) && is_simple ) - node->data.seq->flags |= CV_NODE_SEQ_SIMPLE; - - node->tag |= is_user_type ? CV_NODE_USER : 0; - return ptr; -} - - -static char* -icvXMLParseTag( CvFileStorage* fs, char* ptr, CvStringHashNode** _tag, - CvAttrList** _list, int* _tag_type ) -{ - int tag_type = 0; - CvStringHashNode* tagname = 0; - CvAttrList *first = 0, *last = 0; - int count = 0, max_count = 4; - int attr_buf_size = (max_count*2 + 1)*sizeof(char*) + sizeof(CvAttrList); - char* endptr; - char c; - int have_space; - - if( *ptr == '\0' ) - CV_PARSE_ERROR( "Preliminary end of the stream" ); - - if( *ptr != '<' ) - CV_PARSE_ERROR( "Tag should start with \'<\'" ); - - ptr++; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - if( cv_isalnum(*ptr) || *ptr == '_' ) - tag_type = CV_XML_OPENING_TAG; - else if( *ptr == '/' ) + void writeScalar(const char* key, const char* data) { - tag_type = CV_XML_CLOSING_TAG; - ptr++; - } - else if( *ptr == '?' ) - { - tag_type = CV_XML_HEADER_TAG; - ptr++; - } - else if( *ptr == '!' ) - { - tag_type = CV_XML_DIRECTIVE_TAG; - assert( ptr[1] != '-' || ptr[2] != '-' ); - ptr++; - } - else - CV_PARSE_ERROR( "Unknown tag type" ); - - for(;;) - { - CvStringHashNode* attrname; - - if( !cv_isalpha(*ptr) && *ptr != '_' ) - CV_PARSE_ERROR( "Name should start with a letter or underscore" ); + int len = (int)strlen(data); + if( key && *key == '\0' ) + key = 0; - endptr = ptr - 1; - do c = *++endptr; - while( cv_isalnum(c) || c == '_' || c == '-' ); + FStructData& current_struct = fs->getCurrentStruct(); + int struct_flags = current_struct.flags; - attrname = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 ); - CV_Assert(attrname); - ptr = endptr; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - - if( !tagname ) - tagname = attrname; - else + if( FileNode::isMap(struct_flags) || + (!FileNode::isCollection(struct_flags) && key) ) { - if( tag_type == CV_XML_CLOSING_TAG ) - CV_PARSE_ERROR( "Closing tag should not contain any attributes" ); - - if( !last || count >= max_count ) - { - CvAttrList* chunk; - - chunk = (CvAttrList*)cvMemStorageAlloc( fs->memstorage, attr_buf_size ); - memset( chunk, 0, attr_buf_size ); - chunk->attr = (const char**)(chunk + 1); - count = 0; - if( !last ) - first = last = chunk; - else - last = last->next = chunk; - } - last->attr[count*2] = attrname->str.ptr; + writeTag( key, CV_XML_OPENING_TAG ); + char* ptr = fs->resizeWriteBuffer( fs->bufferPtr(), len ); + memcpy( ptr, data, len ); + fs->setBufferPtr( ptr + len ); + writeTag( key, CV_XML_CLOSING_TAG ); } - - if( last ) + else { - CvFileNode stub; + char* ptr = fs->bufferPtr(); + int new_offset = (int)(ptr - fs->bufferStart()) + len; - if( *ptr != '=' ) - { - ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ); - if( *ptr != '=' ) - CV_PARSE_ERROR( "Attribute name should be followed by \'=\'" ); - } + if( key ) + CV_Error( CV_StsBadArg, "elements with keys can not be written to sequence" ); + + current_struct.flags = FileNode::SEQ; - c = *++ptr; - if( c != '\"' && c != '\'' ) + if( (new_offset > fs->wrapMargin() && new_offset - current_struct.indent > 10) || + (ptr > fs->bufferStart() && ptr[-1] == '>') ) { - ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ); - if( *ptr != '\"' && *ptr != '\'' ) - CV_PARSE_ERROR( "Attribute value should be put into single or double quotes" ); + ptr = fs->flush(); } + else if( ptr > fs->bufferStart() + current_struct.indent && ptr[-1] != '>' ) + *ptr++ = ' '; - ptr = icvXMLParseValue( fs, ptr, &stub, CV_NODE_STRING ); - assert( stub.tag == CV_NODE_STRING ); - last->attr[count*2+1] = stub.data.str.ptr; - count++; + memcpy( ptr, data, len ); + fs->setBufferPtr(ptr + len); } + } - c = *ptr; - have_space = cv_isspace(c) || c == '\0'; - - if( c != '>' ) - { - ptr = icvXMLSkipSpaces( fs, ptr, CV_XML_INSIDE_TAG ); - c = *ptr; - } + void writeComment(const char* comment, bool eol_comment) + { + FStructData& current_struct = fs->getCurrentStruct(); + int len; + int multiline; + const char* eol; + char* ptr; + + if( !comment ) + CV_Error( CV_StsNullPtr, "Null comment" ); + + if( strstr(comment, "--") != 0 ) + CV_Error( CV_StsBadArg, "Double hyphen \'--\' is not allowed in the comments" ); + + len = (int)strlen(comment); + eol = strchr(comment, '\n'); + multiline = eol != 0; + ptr = fs->bufferPtr(); + + if( multiline || !eol_comment || fs->bufferEnd() - ptr < len + 5 ) + ptr = fs->flush(); + else if( ptr > fs->bufferStart() + current_struct.indent ) + *ptr++ = ' '; - if( c == '>' ) - { - if( tag_type == CV_XML_HEADER_TAG ) - CV_PARSE_ERROR( "Invalid closing tag for ' ) // FIXIT ptr[1] - out of bounds read without check - CV_PARSE_ERROR( "Invalid closing tag for resizeWriteBuffer( ptr, len + 9 ); + sprintf( ptr, "", comment ); + len = (int)strlen(ptr); } - else if( c == '/' && ptr[1] == '>' && tag_type == CV_XML_OPENING_TAG ) // FIXIT ptr[1] - out of bounds read without check + else { - tag_type = CV_XML_EMPTY_TAG; - ptr += 2; - break; + strcpy( ptr, "" ); + fs->setBufferPtr(ptr + 3); + fs->flush(); } } - CV_Assert( fs->dummy_eof != 0 ); -} - -/****************************************************************************************\ -* XML Emitter * -\****************************************************************************************/ + void startNextStream() + { + fs->puts( "\n\n" ); + } -#define icvXMLFlush icvFSFlush +protected: + FileStorage_API* fs; +}; -static void -icvXMLWriteTag( CvFileStorage* fs, const char* key, int tag_type, CvAttrList list ) +class XMLParser : public FileStorageParser { - char* ptr = fs->buffer; - int i, len = 0; - int struct_flags = fs->struct_flags; +public: + XMLParser(FileStorage_API* _fs) : fs(_fs) + { + } - if( key && key[0] == '\0' ) - key = 0; + virtual ~XMLParser() {} - if( tag_type == CV_XML_OPENING_TAG || tag_type == CV_XML_EMPTY_TAG ) + char* skipSpaces( char* ptr, int mode ) { - if( CV_NODE_IS_COLLECTION(struct_flags) ) - { - if( CV_NODE_IS_MAP(struct_flags) ^ (key != 0) ) - CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, " - "or add element with key to sequence" ); - } - else + int level = 0; + + for(;;) { - struct_flags = CV_NODE_EMPTY + (key ? CV_NODE_MAP : CV_NODE_SEQ); - fs->is_first = 0; - } + char c; + ptr--; - if( !CV_NODE_IS_EMPTY(struct_flags) ) - ptr = icvXMLFlush(fs); - } + if( mode == CV_XML_INSIDE_COMMENT ) + { + do c = *++ptr; + while( cv_isprint_or_tab(c) && (c != '-' || ptr[1] != '-' || ptr[2] != '>') ); - if( !key ) - key = "_"; - else if( key[0] == '_' && key[1] == '\0' ) - CV_Error( CV_StsBadArg, "A single _ is a reserved tag name" ); + if( c == '-' ) + { + assert( ptr[1] == '-' && ptr[2] == '>' ); + mode = 0; + ptr += 3; + } + } + else if( mode == CV_XML_INSIDE_DIRECTIVE ) + { + // !!!NOTE!!! This is not quite correct, but should work in most cases + do + { + c = *++ptr; + level += c == '<'; + level -= c == '>'; + if( level < 0 ) + return ptr; + } while( cv_isprint_or_tab(c) ); + } + else + { + do c = *++ptr; + while( c == ' ' || c == '\t' ); - len = (int)strlen( key ); - *ptr++ = '<'; - if( tag_type == CV_XML_CLOSING_TAG ) - { - if( list.attr ) - CV_Error( CV_StsBadArg, "Closing tag should not include any attributes" ); - *ptr++ = '/'; - } + if( c == '<' && ptr[1] == '!' && ptr[2] == '-' && ptr[3] == '-' ) + { + if( mode != 0 ) + CV_PARSE_ERROR_CPP( "Comments are not allowed here" ); + mode = CV_XML_INSIDE_COMMENT; + ptr += 4; + } + else if( cv_isprint(c) ) + break; + } - if( !cv_isalpha(key[0]) && key[0] != '_' ) - CV_Error( CV_StsBadArg, "Key should start with a letter or _" ); + if( !cv_isprint(*ptr) ) + { + if( *ptr != '\0' && *ptr != '\n' && *ptr != '\r' ) + CV_PARSE_ERROR_CPP( "Invalid character in the stream" ); + ptr = fs->gets(); + if( !ptr || *ptr == '\0' ) + break; + } + } + return ptr; + } - ptr = icvFSResizeWriteBuffer( fs, ptr, len ); - for( i = 0; i < len; i++ ) + bool getBase64Row(char* ptr, int /*indent*/, char* &beg, char* &end) { - char c = key[i]; - if( !cv_isalnum(c) && c != '_' && c != '-' ) - CV_Error( CV_StsBadArg, "Key name may only contain alphanumeric characters [a-zA-Z0-9], '-' and '_'" ); - ptr[i] = c; + beg = end = ptr = skipSpaces(ptr, CV_XML_INSIDE_TAG); + if( !ptr || !*ptr ) + return false; + + // closing XML tag + if ( *beg == '<' ) + return false; + + // find end of the row + while( cv_isprint(*ptr) ) + ++ptr; + if ( *ptr == '\0' ) + CV_PARSE_ERROR_CPP( "Unexpected end of line" ); + + end = ptr; + return true; } - ptr += len; - for(;;) + char* parseValue( char* ptr, FileNode& node ) { - const char** attr = list.attr; + FileNode new_elem; + bool have_space = true; + int value_type = node.type(); + std::string key, key2, type_name; - for( ; attr && attr[0] != 0; attr += 2 ) + for(;;) { - int len0 = (int)strlen(attr[0]); - int len1 = (int)strlen(attr[1]); + char c = *ptr, d; + char* endptr; - ptr = icvFSResizeWriteBuffer( fs, ptr, len0 + len1 + 4 ); - *ptr++ = ' '; - memcpy( ptr, attr[0], len0 ); - ptr += len0; - *ptr++ = '='; - *ptr++ = '\"'; - memcpy( ptr, attr[1], len1 ); - ptr += len1; - *ptr++ = '\"'; - } - if( !list.next ) - break; - list = *list.next; - } - - if( tag_type == CV_XML_EMPTY_TAG ) - *ptr++ = '/'; - *ptr++ = '>'; - fs->buffer = ptr; - fs->struct_flags = struct_flags & ~CV_NODE_EMPTY; -} + // FIXIT ptr[1], ptr[2] - out of bounds read without check or data fetch (#11061) + if( cv_isspace(c) || c == '\0' || + (c == '<' && ptr[1] == '!' && ptr[2] == '-') ) + { + ptr = skipSpaces( ptr, 0 ); + have_space = true; + c = *ptr; + } + d = ptr[1]; // FIXIT ptr[1] - out of bounds read without check or data fetch (#11061) -void icvXMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name) -{ - CvXMLStackRecord parent; - const char* attr[10]; - int idx = 0; + if( c =='<' || c == '\0' ) + { + int tag_type = 0; + int elem_type = FileNode::NONE; - struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY; - if( !CV_NODE_IS_COLLECTION(struct_flags)) - CV_Error( CV_StsBadArg, - "Some collection type: CV_NODE_SEQ or CV_NODE_MAP must be specified" ); + if( d == '/' || c == '\0' ) + break; - if ( type_name && *type_name == '\0' ) - type_name = 0; + ptr = parseTag( ptr, key, type_name, tag_type ); - if( type_name ) - { - attr[idx++] = "type_id"; - attr[idx++] = type_name; - } - attr[idx++] = 0; + if( tag_type == CV_XML_DIRECTIVE_TAG ) + CV_PARSE_ERROR_CPP( "Directive tags are not allowed here" ); + if( tag_type == CV_XML_EMPTY_TAG ) + CV_PARSE_ERROR_CPP( "Empty tags are not supported" ); - icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(attr,0) ); + CV_Assert(tag_type == CV_XML_OPENING_TAG); - parent.struct_flags = fs->struct_flags & ~CV_NODE_EMPTY; - parent.struct_indent = fs->struct_indent; - parent.struct_tag = fs->struct_tag; - cvSaveMemStoragePos( fs->strstorage, &parent.pos ); - cvSeqPush( fs->write_stack, &parent ); - - fs->struct_indent += CV_XML_INDENT; - if( !CV_NODE_IS_FLOW(struct_flags) ) - icvXMLFlush( fs ); + /* for base64 string */ + bool binary_string = false; - fs->struct_flags = struct_flags; - if( key ) - { - fs->struct_tag = cvMemStorageAllocString( fs->strstorage, (char*)key, -1 ); - } - else - { - fs->struct_tag.ptr = 0; - fs->struct_tag.len = 0; - } -} + if( !type_name.empty() ) + { + const char* tn = type_name.c_str(); + if( strcmp(tn, "str") == 0 ) + elem_type = FileNode::STRING; + else if( strcmp( tn, "map" ) == 0 ) + elem_type = FileNode::MAP; + else if( strcmp( tn, "seq" ) == 0 ) + elem_type = FileNode::SEQ; + else if( strcmp( tn, "binary") == 0) + binary_string = true; + } + new_elem = fs->addNode(node, key, elem_type, 0); + if (!binary_string) + ptr = parseValue(ptr, new_elem); + else + { + ptr = fs->parseBase64( ptr, 0, new_elem); + ptr = skipSpaces( ptr, 0 ); + } -void icvXMLEndWriteStruct( CvFileStorage* fs ) -{ - CvXMLStackRecord parent; + ptr = parseTag( ptr, key2, type_name, tag_type ); + if( tag_type != CV_XML_CLOSING_TAG || key2 != key ) + CV_PARSE_ERROR_CPP( "Mismatched closing tag" ); + have_space = true; + } + else + { + if( !have_space ) + CV_PARSE_ERROR_CPP( "There should be space between literals" ); - if( fs->write_stack->total == 0 ) - CV_Error( CV_StsError, "An extra closing tag" ); + FileNode* elem = &node; + if( node.type() != FileNode::NONE ) + { + fs->convertToCollection( FileNode::SEQ, node ); + new_elem = fs->addNode(node, std::string(), FileNode::NONE, 0); + elem = &new_elem; + } - icvXMLWriteTag( fs, fs->struct_tag.ptr, CV_XML_CLOSING_TAG, cvAttrList(0,0) ); - cvSeqPop( fs->write_stack, &parent ); + if( value_type != FileNode::STRING && + (cv_isdigit(c) || ((c == '-' || c == '+') && + (cv_isdigit(d) || d == '.')) || (c == '.' && cv_isalnum(d))) ) // a number + { + endptr = ptr + (c == '-' || c == '+'); + while( cv_isdigit(*endptr) ) + endptr++; + if( *endptr == '.' || *endptr == 'e' ) + { + double fval = fs->strtod( ptr, &endptr ); + elem->setValue(FileNode::REAL, &fval); + } + else + { + int ival = (int)strtol( ptr, &endptr, 0 ); + elem->setValue(FileNode::INT, &ival); + } - fs->struct_indent = parent.struct_indent; - fs->struct_flags = parent.struct_flags; - fs->struct_tag = parent.struct_tag; - cvRestoreMemStoragePos( fs->strstorage, &parent.pos ); -} + if( endptr == ptr ) + CV_PARSE_ERROR_CPP( "Invalid numeric value (inconsistent explicit type specification?)" ); + ptr = endptr; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } + else + { + // string + int i = 0, len, is_quoted = 0; + if( c == '\"' ) + is_quoted = 1; + else + --ptr; + strbuf[0] = '\0'; + + for( ;; ) + { + c = *++ptr; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); -void icvXMLStartNextStream( CvFileStorage* fs ) -{ - if( !fs->is_first ) - { - while( fs->write_stack->total > 0 ) - icvXMLEndWriteStruct(fs); - - fs->struct_indent = 0; - icvXMLFlush(fs); - /* XML does not allow multiple top-level elements, - so we just put a comment and continue - the current (and the only) "stream" */ - icvPuts( fs, "\n\n" ); - /*fputs( "\n", fs->file ); - fputs( "\n", fs->file );*/ - fs->buffer = fs->buffer_start; - } -} + if( !cv_isalnum(c) ) + { + if( c == '\"' ) + { + if( !is_quoted ) + CV_PARSE_ERROR_CPP( "Literal \" is not allowed within a string. Use "" ); + ++ptr; + break; + } + else if( !cv_isprint(c) || c == '<' || (!is_quoted && cv_isspace(c))) + { + if( is_quoted ) + CV_PARSE_ERROR_CPP( "Closing \" is expected" ); + break; + } + else if( c == '\'' || c == '>' ) + { + CV_PARSE_ERROR_CPP( "Literal \' or > are not allowed. Use ' or >" ); + } + else if( c == '&' ) + { + if( *++ptr == '#' ) + { + int val, base = 10; + ptr++; + if( *ptr == 'x' ) + { + base = 16; + ptr++; + } + val = (int)strtol( ptr, &endptr, base ); + if( (unsigned)val > (unsigned)255 || + !endptr || *endptr != ';' ) + CV_PARSE_ERROR_CPP( "Invalid numeric value in the string" ); + c = (char)val; + } + else + { + endptr = ptr; + do c = *++endptr; + while( cv_isalnum(c) ); + if( c != ';' ) + CV_PARSE_ERROR_CPP( "Invalid character in the symbol entity name" ); + len = (int)(endptr - ptr); + if( len == 2 && memcmp( ptr, "lt", len ) == 0 ) + c = '<'; + else if( len == 2 && memcmp( ptr, "gt", len ) == 0 ) + c = '>'; + else if( len == 3 && memcmp( ptr, "amp", len ) == 0 ) + c = '&'; + else if( len == 4 && memcmp( ptr, "apos", len ) == 0 ) + c = '\''; + else if( len == 4 && memcmp( ptr, "quot", len ) == 0 ) + c = '\"'; + else + { + memcpy( strbuf + i, ptr-1, len + 2 ); + i += len + 2; + } + } + ptr = endptr; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } + } + strbuf[i++] = c; + if( i >= CV_FS_MAX_LEN ) + CV_PARSE_ERROR_CPP( "Too long string literal" ); + } + elem->setValue(FileNode::STRING, strbuf, i); + } + if( value_type != FileNode::NONE && value_type != FileNode::SEQ && value_type != FileNode::MAP ) + break; + have_space = false; + } + } + fs->finalizeCollection(node); -void icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, int len ) -{ - check_if_write_struct_is_delayed( fs ); - if ( fs->state_of_writing_base64 == base64::fs::Uncertain ) - { - switch_to_Base64_state( fs, base64::fs::NotUse ); - } - else if ( fs->state_of_writing_base64 == base64::fs::InUse ) - { - CV_Error( CV_StsError, "Currently only Base64 data is allowed." ); + return ptr; } - if( CV_NODE_IS_MAP(fs->struct_flags) || - (!CV_NODE_IS_COLLECTION(fs->struct_flags) && key) ) - { - icvXMLWriteTag( fs, key, CV_XML_OPENING_TAG, cvAttrList(0,0) ); - char* ptr = icvFSResizeWriteBuffer( fs, fs->buffer, len ); - memcpy( ptr, data, len ); - fs->buffer = ptr + len; - icvXMLWriteTag( fs, key, CV_XML_CLOSING_TAG, cvAttrList(0,0) ); - } - else + char* parseTag( char* ptr, std::string& tag_name, + std::string& type_name, int& tag_type ) { - char* ptr = fs->buffer; - int new_offset = (int)(ptr - fs->buffer_start) + len; + if( *ptr == '\0' ) + CV_PARSE_ERROR_CPP( "Unexpected end of the stream" ); - if( key ) - CV_Error( CV_StsBadArg, "elements with keys can not be written to sequence" ); + if( *ptr != '<' ) + CV_PARSE_ERROR_CPP( "Tag should start with \'<\'" ); - fs->struct_flags = CV_NODE_SEQ; + ptr++; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); - if( (new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10) || - (ptr > fs->buffer_start && ptr[-1] == '>' && !CV_NODE_IS_EMPTY(fs->struct_flags)) ) + if( cv_isalnum(*ptr) || *ptr == '_' ) + tag_type = CV_XML_OPENING_TAG; + else if( *ptr == '/' ) { - ptr = icvXMLFlush(fs); + tag_type = CV_XML_CLOSING_TAG; + ptr++; } - else if( ptr > fs->buffer_start + fs->struct_indent && ptr[-1] != '>' ) - *ptr++ = ' '; - - memcpy( ptr, data, len ); - fs->buffer = ptr + len; - } -} - - -void icvXMLWriteInt( CvFileStorage* fs, const char* key, int value ) -{ - char buf[128], *ptr = icv_itoa( value, buf, 10 ); - int len = (int)strlen(ptr); - icvXMLWriteScalar( fs, key, ptr, len ); -} - - -void icvXMLWriteReal( CvFileStorage* fs, const char* key, double value ) -{ - char buf[128]; - int len = (int)strlen( icvDoubleToString( buf, value )); - icvXMLWriteScalar( fs, key, buf, len ); -} + else if( *ptr == '?' ) + { + tag_type = CV_XML_HEADER_TAG; + ptr++; + } + else if( *ptr == '!' ) + { + tag_type = CV_XML_DIRECTIVE_TAG; + assert( ptr[1] != '-' || ptr[2] != '-' ); + ptr++; + } + else + CV_PARSE_ERROR_CPP( "Unknown tag type" ); + tag_name.clear(); + type_name.clear(); -void icvXMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote ) -{ - char buf[CV_FS_MAX_LEN*6+16]; - char* data = (char*)str; - int i, len; + for(;;) + { + char c, *endptr; + if( !cv_isalpha(*ptr) && *ptr != '_' ) + CV_PARSE_ERROR_CPP( "Name should start with a letter or underscore" ); - if( !str ) - CV_Error( CV_StsNullPtr, "Null string pointer" ); + endptr = ptr - 1; + do c = *++endptr; + while( cv_isalnum(c) || c == '_' || c == '-' ); - len = (int)strlen(str); - if( len > CV_FS_MAX_LEN ) - CV_Error( CV_StsBadArg, "The written string is too long" ); + std::string attrname(ptr, (size_t)(endptr - ptr)); + ptr = endptr; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); - if( quote || len == 0 || str[0] != '\"' || str[0] != str[len-1] ) - { - int need_quote = quote || len == 0; - data = buf; - *data++ = '\"'; - for( i = 0; i < len; i++ ) - { - char c = str[i]; - - if( (uchar)c >= 128 || c == ' ' ) - { - *data++ = c; - need_quote = 1; - } - else if( !cv_isprint(c) || c == '<' || c == '>' || c == '&' || c == '\'' || c == '\"' ) + if( tag_name.empty() ) + tag_name = attrname; + else { - *data++ = '&'; - if( c == '<' ) - { - memcpy(data, "lt", 2); - data += 2; - } - else if( c == '>' ) - { - memcpy(data, "gt", 2); - data += 2; - } - else if( c == '&' ) + if( tag_type == CV_XML_CLOSING_TAG ) + CV_PARSE_ERROR_CPP( "Closing tag should not contain any attributes" ); + + if( *ptr != '=' ) { - memcpy(data, "amp", 3); - data += 3; + ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG ); + if( *ptr != '=' ) + CV_PARSE_ERROR_CPP( "Attribute name should be followed by \'=\'" ); } - else if( c == '\'' ) + + c = *++ptr; + if( c != '\"' && c != '\'' ) { - memcpy(data, "apos", 4); - data += 4; + ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG ); + if( *ptr != '\"' && *ptr != '\'' ) + CV_PARSE_ERROR_CPP( "Attribute value should be put into single or double quotes" ); } - else if( c == '\"' ) + + char quote = *ptr++; + endptr = ptr; + for(;;) { - memcpy( data, "quot", 4); - data += 4; + c = *endptr++; + if( c == quote ) + break; + if( c == '\0' ) + CV_PARSE_ERROR_CPP( "Unexpected end of line" ); } - else + + if( attrname == "type_id" ) { - sprintf( data, "#x%02x", (uchar)c ); - data += 4; + CV_Assert( type_name.empty() ); + type_name = std::string(ptr, (size_t)(endptr - 1 - ptr)); } - *data++ = ';'; - need_quote = 1; + + ptr = endptr; } - else - *data++ = c; - } - if( !need_quote && (cv_isdigit(str[0]) || - str[0] == '+' || str[0] == '-' || str[0] == '.' )) - need_quote = 1; - if( need_quote ) - *data++ = '\"'; - len = (int)(data - buf) - !need_quote; - *data++ = '\0'; - data = buf + !need_quote; - } + c = *ptr; + bool have_space = cv_isspace(c) || c == '\0'; - icvXMLWriteScalar( fs, key, data, len ); -} + if( c != '>' ) + { + ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG ); + c = *ptr; + } + if( c == '>' ) + { + if( tag_type == CV_XML_HEADER_TAG ) + CV_PARSE_ERROR_CPP( "Invalid closing tag for ' ) // FIXIT ptr[1] - out of bounds read without check + CV_PARSE_ERROR_CPP( "Invalid closing tag for ' && tag_type == CV_XML_OPENING_TAG ) // FIXIT ptr[1] - out of bounds read without check + { + tag_type = CV_XML_EMPTY_TAG; + ptr += 2; + break; + } -void icvXMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ) -{ - int len; - int multiline; - const char* eol; - char* ptr; + if( !have_space ) + CV_PARSE_ERROR_CPP( "There should be space between attributes" ); + } - if( !comment ) - CV_Error( CV_StsNullPtr, "Null comment" ); + return ptr; + } - if( strstr(comment, "--") != 0 ) - CV_Error( CV_StsBadArg, "Double hyphen \'--\' is not allowed in the comments" ); + bool parse(char* ptr) + { + CV_Assert( fs != 0 ); - len = (int)strlen(comment); - eol = strchr(comment, '\n'); - multiline = eol != 0; - ptr = fs->buffer; + std::string key, key2, type_name; + int tag_type = 0; + bool ok = false; - if( multiline || !eol_comment || fs->buffer_end - ptr < len + 5 ) - ptr = icvXMLFlush( fs ); - else if( ptr > fs->buffer_start + fs->struct_indent ) - *ptr++ = ' '; + // CV_XML_INSIDE_TAG is used to prohibit leading comments + ptr = skipSpaces( ptr, CV_XML_INSIDE_TAG ); - if( !multiline ) - { - ptr = icvFSResizeWriteBuffer( fs, ptr, len + 9 ); - sprintf( ptr, "", comment ); - len = (int)strlen(ptr); - } - else - { - strcpy( ptr, "" ); - fs->buffer = ptr + 3; - icvXMLFlush( fs ); + CV_Assert( fs->eof() ); + return ok; } + + FileStorage_API* fs; + char strbuf[CV_FS_MAX_LEN+16]; +}; + +Ptr createXMLEmitter(FileStorage_API* fs) +{ + return makePtr(fs); +} + +Ptr createXMLParser(FileStorage_API* fs) +{ + return makePtr(fs); +} + } diff --git a/modules/core/src/persistence_yml.cpp b/modules/core/src/persistence_yml.cpp index ab6ebf6..4129ca1 100644 --- a/modules/core/src/persistence_yml.cpp +++ b/modules/core/src/persistence_yml.cpp @@ -2,944 +2,837 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html - #include "precomp.hpp" #include "persistence.hpp" -#define CV_YML_INDENT 3 -#define CV_YML_INDENT_FLOW 1 - -/****************************************************************************************\ -* YAML Parser * -\****************************************************************************************/ - -static char* icvYMLSkipSpaces( CvFileStorage* fs, char* ptr, int min_indent, int max_comment_indent ) +enum { - for(;;) - { - while( *ptr == ' ' ) - ptr++; - if( *ptr == '#' ) - { - if( ptr - fs->buffer_start > max_comment_indent ) - return ptr; - *ptr = '\0'; - } - else if( cv_isprint(*ptr) ) - { - if( ptr - fs->buffer_start < min_indent ) - CV_PARSE_ERROR( "Incorrect indentation" ); - break; - } - else if( *ptr == '\0' || *ptr == '\n' || *ptr == '\r' ) - { - int max_size = (int)(fs->buffer_end - fs->buffer_start); - ptr = icvGets( fs, fs->buffer_start, max_size ); - if( !ptr ) - { - // emulate end of stream - ptr = fs->buffer_start; - ptr[0] = ptr[1] = ptr[2] = '.'; - ptr[3] = '\0'; - fs->dummy_eof = 1; - break; - } - else - { - int l = (int)strlen(ptr); - if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !icvEof(fs) ) - CV_PARSE_ERROR( "Too long string or a last string w/o newline" ); - } + CV_YML_INDENT = 3, + CV_YML_INDENT_FLOW = 1 +}; - fs->lineno++; // FIXIT doesn't really work with long lines. It must be counted via '\n' or '\r' symbols, not the number of icvGets() calls. - } - else - CV_PARSE_ERROR( *ptr == '\t' ? "Tabs are prohibited in YAML!" : "Invalid character" ); - } - - return ptr; -} - - -static void icvYMLGetMultilineStringContent(CvFileStorage* fs, char* ptr, int indent, char* &beg, char* &end) +namespace cv { - ptr = icvYMLSkipSpaces(fs, ptr, 0, INT_MAX); - beg = ptr; - end = ptr; - if (fs->dummy_eof) - return ; /* end of file */ - - if (ptr - fs->buffer_start != indent) - return ; /* end of string */ - - /* find end */ - while(cv_isprint(*ptr)) /* no check for base64 string */ - ++ ptr; - if (*ptr == '\0') - CV_PARSE_ERROR("Unexpected end of line"); - - end = ptr; -} -static char* icvYMLParseBase64(CvFileStorage* fs, char* ptr, int indent, CvFileNode * node) +class YAMLEmitter : public FileStorageEmitter { - char * beg = 0; - char * end = 0; - - icvYMLGetMultilineStringContent(fs, ptr, indent, beg, end); - if (beg >= end) - return end; // CV_PARSE_ERROR("Empty Binary Data"); - - /* calc (decoded) total_byte_size from header */ - std::string dt; +public: + YAMLEmitter(FileStorage_API* _fs) : fs(_fs) { - if (end - beg < static_cast(base64::ENCODED_HEADER_SIZE)) - CV_PARSE_ERROR("Unrecognized Base64 header"); - - std::vector header(base64::HEADER_SIZE + 1, ' '); - base64::base64_decode(beg, header.data(), 0U, base64::ENCODED_HEADER_SIZE); - if ( !base64::read_base64_header(header, dt) || dt.empty() ) - CV_PARSE_ERROR("Invalid `dt` in Base64 header"); - - beg += base64::ENCODED_HEADER_SIZE; } + virtual ~YAMLEmitter() {} - /* get all Base64 data */ - std::string base64_buffer; - base64_buffer.reserve( PARSER_BASE64_BUFFER_SIZE ); - while( beg < end ) - { - base64_buffer.append( beg, end ); - beg = end; - icvYMLGetMultilineStringContent( fs, beg, indent, beg, end ); - } - if ( base64_buffer.empty() || - !base64::base64_valid(base64_buffer.data(), 0U, base64_buffer.size()) ) - CV_PARSE_ERROR( "Invalid Base64 data." ); - - /* buffer for decoded data(exclude header) */ - std::vector binary_buffer( base64::base64_decode_buffer_size(base64_buffer.size()) ); - int total_byte_size = static_cast( - base64::base64_decode_buffer_size( base64_buffer.size(), base64_buffer.data(), false ) - ); + FStructData startWriteStruct(const FStructData& parent, const char* key, + int struct_flags, const char* type_name=0) { - base64::Base64ContextParser parser(binary_buffer.data(), binary_buffer.size() ); - const uchar * buffer_beg = reinterpret_cast( base64_buffer.data() ); - const uchar * buffer_end = buffer_beg + base64_buffer.size(); - parser.read( buffer_beg, buffer_end ); - parser.flush(); - } - - /* save as CvSeq */ - int elem_size = ::icvCalcStructSize(dt.c_str(), 0); - if (total_byte_size % elem_size != 0) - CV_PARSE_ERROR("Byte size not match elememt size"); - int elem_cnt = total_byte_size / elem_size; - - node->tag = CV_NODE_NONE; - int struct_flags = CV_NODE_FLOW | CV_NODE_SEQ; - /* after icvFSCreateCollection, node->tag == struct_flags */ - icvFSCreateCollection(fs, struct_flags, node); - base64::make_seq(binary_buffer.data(), elem_cnt, dt.c_str(), *node->data.seq); - - if (fs->dummy_eof) { - /* end of file */ - return fs->buffer_start; - } else { - /* end of line */ - return end; - } -} - + char buf[CV_FS_MAX_LEN + 1024]; + const char* data = 0; -static char* icvYMLParseKey( CvFileStorage* fs, char* ptr, CvFileNode* map_node, CvFileNode** value_placeholder ) -{ - char c; - char *endptr = ptr - 1, *saveptr; - CvStringHashNode* str_hash_node; - - if( *ptr == '-' ) - CV_PARSE_ERROR( "Key may not start with \'-\'" ); - - do c = *++endptr; - while( cv_isprint(c) && c != ':' ); - - if( c != ':' ) - CV_PARSE_ERROR( "Missing \':\'" ); - - saveptr = endptr + 1; - do c = *--endptr; - while( c == ' ' ); - - ++endptr; - if( endptr == ptr ) - CV_PARSE_ERROR( "An empty key" ); + if ( type_name && *type_name == '\0' ) + type_name = 0; - str_hash_node = cvGetHashedKey( fs, ptr, (int)(endptr - ptr), 1 ); - *value_placeholder = cvGetFileNode( fs, map_node, str_hash_node, 1 ); - ptr = saveptr; - - return ptr; -} - - -static char* -icvYMLParseValue( CvFileStorage* fs, char* ptr, CvFileNode* node, - int parent_flags, int min_indent ) -{ - char buf[CV_FS_MAX_LEN + 1024] = {0}; - char* endptr = 0; - char c = ptr[0], d = ptr[1]; - int is_parent_flow = CV_NODE_IS_FLOW(parent_flags); - int value_type = CV_NODE_NONE; - int len; - bool is_binary_string = false; + struct_flags = (struct_flags & (FileNode::TYPE_MASK|FileNode::FLOW)) | FileNode::EMPTY; + if( !FileNode::isCollection(struct_flags)) + CV_Error( CV_StsBadArg, + "Some collection type - FileNode::SEQ or FileNode::MAP, must be specified" ); - memset( node, 0, sizeof(*node) ); - - if( c == '!' ) // handle explicit type specification - { - if( d == '!' || d == '^' ) + if (type_name && memcmp(type_name, "binary", 6) == 0) { - ptr++; - value_type |= CV_NODE_USER; + /* reset struct flag. in order not to print ']' */ + struct_flags = FileNode::SEQ; + sprintf(buf, "!!binary |"); + data = buf; } - if ( d == '<') //support of full type heading from YAML 1.2 + else if( FileNode::isFlow(struct_flags)) { - const char* yamlTypeHeading = "' && (size_t)(typeEndPtr - ptr) > headingLenght ) + if( type_name ) + sprintf( buf, "!!%s %c", type_name, c ); + else { - if ( memcmp(ptr, yamlTypeHeading, headingLenght) == 0 ) - { - value_type |= CV_NODE_USER; - *typeEndPtr = ' '; - ptr += headingLenght - 1; - } + buf[0] = c; + buf[1] = '\0'; } + data = buf; } - - endptr = ptr++; - do d = *++endptr; - while( cv_isprint(d) && d != ' ' ); - len = (int)(endptr - ptr); - if( len == 0 ) - CV_PARSE_ERROR( "Empty type name" ); - d = *endptr; - *endptr = '\0'; - - if( len == 3 && !CV_NODE_IS_USER(value_type) ) - { - if( memcmp( ptr, "str", 3 ) == 0 ) - value_type = CV_NODE_STRING; - else if( memcmp( ptr, "int", 3 ) == 0 ) - value_type = CV_NODE_INT; - else if( memcmp( ptr, "seq", 3 ) == 0 ) - value_type = CV_NODE_SEQ; - else if( memcmp( ptr, "map", 3 ) == 0 ) - value_type = CV_NODE_MAP; - } - else if( len == 5 && !CV_NODE_IS_USER(value_type) ) + else if( type_name ) { - if( memcmp( ptr, "float", 5 ) == 0 ) - value_type = CV_NODE_REAL; + sprintf( buf, "!!%s", type_name ); + data = buf; } - else if (len == 6 && CV_NODE_IS_USER(value_type)) - { - if( memcmp( ptr, "binary", 6 ) == 0 ) { - value_type = CV_NODE_SEQ; - is_binary_string = true; - /* for ignore '|' */ + writeScalar( key, data ); - /**** operation with endptr ****/ - *endptr = d; + FStructData fsd; + fsd.indent = parent.indent; + fsd.flags = struct_flags; - do { - d = *++endptr; - if (d == '|') - break; - } while (d == ' '); + if( !FileNode::isFlow(parent.flags) ) + fsd.indent += CV_YML_INDENT + FileNode::isFlow(struct_flags); - d = *++endptr; - *endptr = '\0'; - } - } - else if( CV_NODE_IS_USER(value_type) ) - { - node->info = cvFindType( ptr ); - if( !node->info ) - node->tag &= ~CV_NODE_USER; - } + return fsd; + } - *endptr = d; - ptr = icvYMLSkipSpaces( fs, endptr, min_indent, INT_MAX ); + void endWriteStruct(const FStructData& current_struct) + { + char* ptr; - c = *ptr; + int struct_flags = current_struct.flags; - if( !CV_NODE_IS_USER(value_type) ) + if( FileNode::isFlow(struct_flags) ) + { + ptr = fs->bufferPtr(); + if( ptr > fs->bufferStart() + current_struct.indent && !FileNode::isEmptyCollection(struct_flags) ) + *ptr++ = ' '; + *ptr++ = FileNode::isMap(struct_flags) ? '}' : ']'; + fs->setBufferPtr(ptr); + } + else if( FileNode::isEmptyCollection(struct_flags) ) { - if (value_type == CV_NODE_STRING && c != '\'' && c != '\"') - goto force_string; - if( value_type == CV_NODE_INT ) - goto force_int; - if( value_type == CV_NODE_REAL ) - goto force_real; + ptr = fs->flush(); + memcpy( ptr, FileNode::isMap(struct_flags) ? "{}" : "[]", 2 ); + fs->setBufferPtr(ptr + 2); } + /* + if( !FileNode::isFlow(parent_flags) ) + fs->struct_indent -= CV_YML_INDENT + FileNode::isFlow(struct_flags); + assert( fs->struct_indent >= 0 );*/ } - if (is_binary_string) + void write(const char* key, int value) { - /* for base64 string */ - int indent = static_cast(ptr - fs->buffer_start); - ptr = icvYMLParseBase64(fs, ptr, indent, node); + char buf[128]; + writeScalar( key, fs::itoa( value, buf, 10 )); } - else if( cv_isdigit(c) || - ((c == '-' || c == '+') && (cv_isdigit(d) || d == '.')) || - (c == '.' && cv_isalnum(d))) // a number + + void write( const char* key, double value ) { - double fval; - int ival; - endptr = ptr + (c == '-' || c == '+'); - while( cv_isdigit(*endptr) ) - endptr++; - if( *endptr == '.' || *endptr == 'e' ) - { -force_real: - fval = icv_strtod( fs, ptr, &endptr ); - /*if( endptr == ptr || cv_isalpha(*endptr) ) - icvProcessSpecialDouble( fs, endptr, &fval, &endptr ));*/ + char buf[128]; + writeScalar( key, fs::doubleToString( buf, value, false )); + } - node->tag = CV_NODE_REAL; - node->data.f = fval; - } - else - { -force_int: - ival = (int)strtol( ptr, &endptr, 0 ); - node->tag = CV_NODE_INT; - node->data.i = ival; - } + void write(const char* key, const char* str, bool quote) + { + char buf[CV_FS_MAX_LEN*4+16]; + char* data = (char*)str; + int i, len; - if( !endptr || endptr == ptr ) - CV_PARSE_ERROR( "Invalid numeric value (inconsistent explicit type specification?)" ); + if( !str ) + CV_Error( CV_StsNullPtr, "Null string pointer" ); - ptr = endptr; - CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG(); - } - else if( c == '\'' || c == '\"' ) // an explicit string - { - node->tag = CV_NODE_STRING; - if( c == '\'' ) - for( len = 0; len < CV_FS_MAX_LEN; ) - { - c = *++ptr; - if( cv_isalnum(c) || (c != '\'' && cv_isprint(c))) - buf[len++] = c; - else if( c == '\'' ) - { - c = *++ptr; - if( c != '\'' ) - break; - buf[len++] = c; - } - else - CV_PARSE_ERROR( "Invalid character" ); - } - else - for( len = 0; len < CV_FS_MAX_LEN; ) + len = (int)strlen(str); + if( len > CV_FS_MAX_LEN ) + CV_Error( CV_StsBadArg, "The written string is too long" ); + + if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') ) + { + int need_quote = quote || len == 0 || str[0] == ' '; + data = buf; + *data++ = '\"'; + for( i = 0; i < len; i++ ) { - c = *++ptr; - if( cv_isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c))) - buf[len++] = c; - else if( c == '\"' ) - { - ++ptr; - break; - } - else if( c == '\\' ) + char c = str[i]; + + if( !need_quote && !cv_isalnum(c) && c != '_' && c != ' ' && c != '-' && + c != '(' && c != ')' && c != '/' && c != '+' && c != ';' ) + need_quote = 1; + + if( !cv_isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') ) { - d = *++ptr; - if( d == '\'' ) - buf[len++] = d; - else if( d == '\"' || d == '\\' || d == '\'' ) - buf[len++] = d; - else if( d == 'n' ) - buf[len++] = '\n'; - else if( d == 'r' ) - buf[len++] = '\r'; - else if( d == 't' ) - buf[len++] = '\t'; - else if( d == 'x' || (cv_isdigit(d) && d < '8') ) + *data++ = '\\'; + if( cv_isprint(c) ) + *data++ = c; + else if( c == '\n' ) + *data++ = 'n'; + else if( c == '\r' ) + *data++ = 'r'; + else if( c == '\t' ) + *data++ = 't'; + else { - int val, is_hex = d == 'x'; - c = ptr[3]; - ptr[3] = '\0'; - val = (int)strtol( ptr + is_hex, &endptr, is_hex ? 8 : 16 ); - ptr[3] = c; - if( endptr == ptr + is_hex ) - buf[len++] = 'x'; - else - { - buf[len++] = (char)val; - ptr = endptr; - } + sprintf( data, "x%02x", c ); + data += 3; } } else - CV_PARSE_ERROR( "Invalid character" ); + *data++ = c; } + if( !need_quote && (cv_isdigit(str[0]) || + str[0] == '+' || str[0] == '-' || str[0] == '.' )) + need_quote = 1; - if( len >= CV_FS_MAX_LEN ) - CV_PARSE_ERROR( "Too long string literal" ); + if( need_quote ) + *data++ = '\"'; + *data++ = '\0'; + data = buf + !need_quote; + } - node->data.str = cvMemStorageAllocString( fs->memstorage, buf, len ); + writeScalar( key, data); } - else if( c == '[' || c == '{' ) // collection as a flow + + void writeScalar(const char* key, const char* data) { - int new_min_indent = min_indent + !is_parent_flow; - int struct_flags = CV_NODE_FLOW + (c == '{' ? CV_NODE_MAP : CV_NODE_SEQ); - bool is_simple = true; + int i, keylen = 0; + int datalen = 0; + char* ptr; - icvFSCreateCollection( fs, CV_NODE_TYPE(struct_flags) + - (node->info ? CV_NODE_USER : 0), node ); + FStructData& current_struct = fs->getCurrentStruct(); - d = c == '[' ? ']' : '}'; + int struct_flags = current_struct.flags; - for( ++ptr ;;) + if( key && key[0] == '\0' ) + key = 0; + + if( FileNode::isCollection(struct_flags) ) + { + if( (FileNode::isMap(struct_flags) ^ (key != 0)) ) + CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, " + "or add element with key to sequence" ); + } + else { - CvFileNode* elem = 0; + fs->setNonEmpty(); + struct_flags = FileNode::EMPTY | (key ? FileNode::MAP : FileNode::SEQ); + } - ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX ); - if( *ptr == '}' || *ptr == ']' ) - { - if( *ptr != d ) - CV_PARSE_ERROR( "The wrong closing bracket" ); - ptr++; - break; - } + if( key ) + { + keylen = (int)strlen(key); + if( keylen == 0 ) + CV_Error( CV_StsBadArg, "The key is an empty" ); - if( node->data.seq->total != 0 ) - { - if( *ptr != ',' ) - CV_PARSE_ERROR( "Missing , between the elements" ); - ptr = icvYMLSkipSpaces( fs, ptr + 1, new_min_indent, INT_MAX ); - } + if( keylen > CV_FS_MAX_LEN ) + CV_Error( CV_StsBadArg, "The key is too long" ); + } + + if( data ) + datalen = (int)strlen(data); - if( CV_NODE_IS_MAP(struct_flags) ) + if( FileNode::isFlow(struct_flags) ) + { + ptr = fs->bufferPtr(); + if( !FileNode::isEmptyCollection(struct_flags) ) + *ptr++ = ','; + int new_offset = (int)(ptr - fs->bufferStart()) + keylen + datalen; + if( new_offset > fs->wrapMargin() && new_offset - current_struct.indent > 10 ) { - ptr = icvYMLParseKey( fs, ptr, node, &elem ); - ptr = icvYMLSkipSpaces( fs, ptr, new_min_indent, INT_MAX ); + fs->setBufferPtr(ptr); + ptr = fs->flush(); } else + *ptr++ = ' '; + } + else + { + ptr = fs->flush(); + if( !FileNode::isMap(struct_flags) ) { - if( *ptr == ']' ) - break; - elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 ); + *ptr++ = '-'; + if( data ) + *ptr++ = ' '; } - CV_Assert(elem); - ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, new_min_indent ); - if( CV_NODE_IS_MAP(struct_flags) ) - elem->tag |= CV_NODE_NAMED; - is_simple = is_simple && !CV_NODE_IS_COLLECTION(elem->tag); } - node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0; - } - else - { - int indent, struct_flags; - bool is_simple; - if( is_parent_flow || c != '-' ) + if( key ) { - // implicit (one-line) string or nested block-style collection - if( !is_parent_flow ) + if( !cv_isalpha(key[0]) && key[0] != '_' ) + CV_Error( CV_StsBadArg, "Key must start with a letter or _" ); + + ptr = fs->resizeWriteBuffer( ptr, keylen ); + + for( i = 0; i < keylen; i++ ) { - if( c == '?' ) - CV_PARSE_ERROR( "Complex keys are not supported" ); - if( c == '|' || c == '>' ) - CV_PARSE_ERROR( "Multi-line text literals are not supported" ); + char c = key[i]; + + ptr[i] = c; + if( !cv_isalnum(c) && c != '-' && c != '_' && c != ' ' ) + CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" ); } -force_string: - endptr = ptr - 1; + ptr += keylen; + *ptr++ = ':'; + if( !FileNode::isFlow(struct_flags) && data ) + *ptr++ = ' '; + } - do c = *++endptr; - while( cv_isprint(c) && - (!is_parent_flow || (c != ',' && c != '}' && c != ']')) && - (is_parent_flow || c != ':' || value_type == CV_NODE_STRING)); + if( data ) + { + ptr = fs->resizeWriteBuffer( ptr, datalen ); + memcpy( ptr, data, datalen ); + ptr += datalen; + } - if( endptr == ptr ) - CV_PARSE_ERROR( "Invalid character" ); + fs->setBufferPtr(ptr); + current_struct.flags &= ~FileNode::EMPTY; + } - if( is_parent_flow || c != ':' ) - { - char* str_end = endptr; - node->tag = CV_NODE_STRING; - // strip spaces in the end of string - do c = *--str_end; - while( str_end > ptr && c == ' ' ); - str_end++; - node->data.str = cvMemStorageAllocString( fs->memstorage, ptr, (int)(str_end - ptr) ); - ptr = endptr; - return ptr; - } - struct_flags = CV_NODE_MAP; - } - else - struct_flags = CV_NODE_SEQ; + void writeComment(const char* comment, bool eol_comment) + { + if( !comment ) + CV_Error( CV_StsNullPtr, "Null comment" ); - icvFSCreateCollection( fs, struct_flags + - (node->info ? CV_NODE_USER : 0), node ); + int len = (int)strlen(comment); + const char* eol = strchr(comment, '\n'); + bool multiline = eol != 0; + char* ptr = fs->bufferPtr(); - indent = (int)(ptr - fs->buffer_start); - is_simple = true; + if( !eol_comment || multiline || + fs->bufferEnd() - ptr < len || ptr == fs->bufferStart() ) + ptr = fs->flush(); + else + *ptr++ = ' '; - for(;;) + while( comment ) { - CvFileNode* elem = 0; - - if( CV_NODE_IS_MAP(struct_flags) ) + *ptr++ = '#'; + *ptr++ = ' '; + if( eol ) { - ptr = icvYMLParseKey( fs, ptr, node, &elem ); + ptr = fs->resizeWriteBuffer( ptr, (int)(eol - comment) + 1 ); + memcpy( ptr, comment, eol - comment + 1 ); + fs->setBufferPtr(ptr + (eol - comment)); + comment = eol + 1; + eol = strchr( comment, '\n' ); } else { - c = *ptr++; - if( c != '-' ) - CV_PARSE_ERROR( "Block sequence elements must be preceded with \'-\'" ); - - elem = (CvFileNode*)cvSeqPush( node->data.seq, 0 ); - } - CV_Assert(elem); - ptr = icvYMLSkipSpaces( fs, ptr, indent + 1, INT_MAX ); - ptr = icvYMLParseValue( fs, ptr, elem, struct_flags, indent + 1 ); - if( CV_NODE_IS_MAP(struct_flags) ) - elem->tag |= CV_NODE_NAMED; - is_simple = is_simple && !CV_NODE_IS_COLLECTION(elem->tag); - - ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ); - if( ptr - fs->buffer_start != indent ) - { - if( ptr - fs->buffer_start < indent ) - break; - else - CV_PARSE_ERROR( "Incorrect indentation" ); + len = (int)strlen(comment); + ptr = fs->resizeWriteBuffer( ptr, len ); + memcpy( ptr, comment, len ); + fs->setBufferPtr(ptr + len); + comment = 0; } - if( memcmp( ptr, "...", 3 ) == 0 ) - break; + ptr = fs->flush(); } + } - node->data.seq->flags |= is_simple ? CV_NODE_SEQ_SIMPLE : 0; + void startNextStream() + { + fs->puts( "...\n" ); + fs->puts( "---\n" ); } - return ptr; -} +protected: + FileStorage_API* fs; +}; -void icvYMLParse( CvFileStorage* fs ) +class YAMLParser : public FileStorageParser { - char* ptr = fs->buffer_start; - int is_first = 1; +public: + YAMLParser(FileStorage_API* _fs) : fs(_fs) + { + } + + virtual ~YAMLParser() {} - for(;;) + char* skipSpaces( char* ptr, int min_indent, int max_comment_indent ) { - // 0. skip leading comments and directives and ... - // 1. reach the first item for(;;) { - ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ); - if( !ptr ) - return; - - if( *ptr == '%' ) + while( *ptr == ' ' ) + ptr++; + if( *ptr == '#' ) { - if( memcmp( ptr, "%YAML", 5 ) == 0 && - memcmp( ptr, "%YAML:1.", 8 ) != 0 && - memcmp( ptr, "%YAML 1.", 8 ) != 0) - CV_PARSE_ERROR( "Unsupported YAML version (it must be 1.x)" ); + if( ptr - fs->bufferStart() > max_comment_indent ) + return ptr; *ptr = '\0'; } - else if( *ptr == '-' ) + else if( cv_isprint(*ptr) ) + { + if( ptr - fs->bufferStart() < min_indent ) + CV_PARSE_ERROR_CPP( "Incorrect indentation" ); + break; + } + else if( *ptr == '\0' || *ptr == '\n' || *ptr == '\r' ) { - if( memcmp(ptr, "---", 3) == 0 ) + ptr = fs->gets(); + if( !ptr ) { - ptr += 3; + // emulate end of stream + ptr = fs->bufferStart(); + ptr[0] = ptr[1] = ptr[2] = '.'; + ptr[3] = '\0'; + fs->setEof(); break; } - else if( is_first ) - break; - } - else if( cv_isalnum(*ptr) || *ptr=='_') - { - if( !is_first ) - CV_PARSE_ERROR( "The YAML streams must start with '---', except the first one" ); - break; + else + { + int l = (int)strlen(ptr); + if( ptr[l-1] != '\n' && ptr[l-1] != '\r' && !fs->eof() ) + CV_PARSE_ERROR_CPP( "Too long string or a last string w/o newline" ); + } } - else if( fs->dummy_eof ) - break; else - CV_PARSE_ERROR( "Invalid or unsupported syntax" ); + CV_PARSE_ERROR_CPP( *ptr == '\t' ? "Tabs are prohibited in YAML!" : "Invalid character" ); } - ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ); - if( memcmp( ptr, "...", 3 ) != 0 ) - { - // 2. parse the collection - CvFileNode* root_node = (CvFileNode*)cvSeqPush( fs->roots, 0 ); + return ptr; + } - ptr = icvYMLParseValue( fs, ptr, root_node, CV_NODE_NONE, 0 ); - if( !CV_NODE_IS_COLLECTION(root_node->tag) ) - CV_PARSE_ERROR( "Only collections as YAML streams are supported by this parser" ); + bool getBase64Row(char* ptr, int indent, char* &beg, char* &end) + { + beg = end = ptr = skipSpaces(ptr, 0, INT_MAX); + if (!ptr || !*ptr) + return false; // end of file - // 3. parse until the end of file or next collection - ptr = icvYMLSkipSpaces( fs, ptr, 0, INT_MAX ); - if( !ptr ) - return; - } + if (ptr - fs->bufferStart() != indent) + return false; // end of base64 data - if( fs->dummy_eof ) - break; - ptr += 3; - is_first = 0; - } -} + /* find end */ + while(cv_isprint(*ptr)) /* no check for base64 string */ + ++ptr; + if (*ptr == '\0') + CV_PARSE_ERROR_CPP("Unexpected end of line"); + end = ptr; + return true; + } -/****************************************************************************************\ -* YAML Emitter * -\****************************************************************************************/ -void icvYMLWrite( CvFileStorage* fs, const char* key, const char* data ) -{ - check_if_write_struct_is_delayed( fs ); - if ( fs->state_of_writing_base64 == base64::fs::Uncertain ) - { - switch_to_Base64_state( fs, base64::fs::NotUse ); - } - else if ( fs->state_of_writing_base64 == base64::fs::InUse ) + char* parseKey( char* ptr, FileNode& map_node, FileNode& value_placeholder ) { - CV_Error( CV_StsError, "At present, output Base64 data only." ); - } + char c; + char *endptr = ptr - 1, *saveptr; - int i, keylen = 0; - int datalen = 0; - int struct_flags; - char* ptr; + if( *ptr == '-' ) + CV_PARSE_ERROR_CPP( "Key may not start with \'-\'" ); - struct_flags = fs->struct_flags; + do c = *++endptr; + while( cv_isprint(c) && c != ':' ); - if( key && key[0] == '\0' ) - key = 0; + if( c != ':' ) + CV_PARSE_ERROR_CPP( "Missing \':\'" ); - if( CV_NODE_IS_COLLECTION(struct_flags) ) - { - if( (CV_NODE_IS_MAP(struct_flags) ^ (key != 0)) ) - CV_Error( CV_StsBadArg, "An attempt to add element without a key to a map, " - "or add element with key to sequence" ); - } - else - { - fs->is_first = 0; - struct_flags = CV_NODE_EMPTY | (key ? CV_NODE_MAP : CV_NODE_SEQ); - } + saveptr = endptr + 1; + do c = *--endptr; + while( c == ' ' ); - if( key ) - { - keylen = (int)strlen(key); - if( keylen == 0 ) - CV_Error( CV_StsBadArg, "The key is an empty" ); - - if( keylen > CV_FS_MAX_LEN ) - CV_Error( CV_StsBadArg, "The key is too long" ); - } + ++endptr; + if( endptr == ptr ) + CV_PARSE_ERROR_CPP( "An empty key" ); - if( data ) - datalen = (int)strlen(data); + value_placeholder = fs->addNode(map_node, std::string(ptr, endptr - ptr), FileNode::NONE); + ptr = saveptr; - if( CV_NODE_IS_FLOW(struct_flags) ) - { - int new_offset; - ptr = fs->buffer; - if( !CV_NODE_IS_EMPTY(struct_flags) ) - *ptr++ = ','; - new_offset = (int)(ptr - fs->buffer_start) + keylen + datalen; - if( new_offset > fs->wrap_margin && new_offset - fs->struct_indent > 10 ) - { - fs->buffer = ptr; - ptr = icvFSFlush(fs); - } - else - *ptr++ = ' '; + return ptr; } - else + + char* parseValue( char* ptr, FileNode& node, int min_indent, bool is_parent_flow ) { - ptr = icvFSFlush(fs); - if( !CV_NODE_IS_MAP(struct_flags) ) + char* endptr = 0; + char c = ptr[0], d = ptr[1]; + int value_type = FileNode::NONE; + int len; + bool is_binary_string = false; + bool is_user = false; + + if( c == '!' ) // handle explicit type specification { - *ptr++ = '-'; - if( data ) - *ptr++ = ' '; - } - } + if( d == '!' || d == '^' ) + { + ptr++; + is_user = true; + //value_type |= FileNode::USER; + } + if ( d == '<') //support of full type heading from YAML 1.2 + { + const char* yamlTypeHeading = "' && (size_t)(typeEndPtr - ptr) > headingLenght ) + { + if ( memcmp(ptr, yamlTypeHeading, headingLenght) == 0 ) + { + *typeEndPtr = ' '; + ptr += headingLenght - 1; + is_user = true; + //value_type |= FileNode::USER; + } + } + } - ptr[i] = c; - if( !cv_isalnum(c) && c != '-' && c != '_' && c != ' ' ) - CV_Error( CV_StsBadArg, "Key names may only contain alphanumeric characters [a-zA-Z0-9], '-', '_' and ' '" ); - } + endptr = ptr++; + do d = *++endptr; + while( cv_isprint(d) && d != ' ' ); + len = (int)(endptr - ptr); + if( len == 0 ) + CV_PARSE_ERROR_CPP( "Empty type name" ); + d = *endptr; + *endptr = '\0'; - ptr += keylen; - *ptr++ = ':'; - if( !CV_NODE_IS_FLOW(struct_flags) && data ) - *ptr++ = ' '; - } + if( len == 3 && !is_user ) + { + if( memcmp( ptr, "str", 3 ) == 0 ) + value_type = FileNode::STRING; + else if( memcmp( ptr, "int", 3 ) == 0 ) + value_type = FileNode::INT; + else if( memcmp( ptr, "seq", 3 ) == 0 ) + value_type = FileNode::SEQ; + else if( memcmp( ptr, "map", 3 ) == 0 ) + value_type = FileNode::MAP; + } + else if( len == 5 && !is_user ) + { + if( memcmp( ptr, "float", 5 ) == 0 ) + value_type = FileNode::REAL; + } + else if (len == 6 && is_user) + { + if( memcmp( ptr, "binary", 6 ) == 0 ) { + value_type = FileNode::SEQ; + is_binary_string = true; - if( data ) - { - ptr = icvFSResizeWriteBuffer( fs, ptr, datalen ); - memcpy( ptr, data, datalen ); - ptr += datalen; - } + /* for ignore '|' */ - fs->buffer = ptr; - fs->struct_flags = struct_flags & ~CV_NODE_EMPTY; -} + /**** operation with endptr ****/ + *endptr = d; + do { + d = *++endptr; + if (d == '|') + break; + } while (d == ' '); -void icvYMLStartWriteStruct( CvFileStorage* fs, const char* key, int struct_flags, const char* type_name) -{ - int parent_flags; - char buf[CV_FS_MAX_LEN + 1024]; - const char* data = 0; + d = *++endptr; + *endptr = '\0'; + } + } - if ( type_name && *type_name == '\0' ) - type_name = 0; + *endptr = d; + ptr = skipSpaces( endptr, min_indent, INT_MAX ); - struct_flags = (struct_flags & (CV_NODE_TYPE_MASK|CV_NODE_FLOW)) | CV_NODE_EMPTY; - if( !CV_NODE_IS_COLLECTION(struct_flags)) - CV_Error( CV_StsBadArg, - "Some collection type - CV_NODE_SEQ or CV_NODE_MAP, must be specified" ); + c = *ptr; - if (type_name && memcmp(type_name, "binary", 6) == 0) - { - /* reset struct flag. in order not to print ']' */ - struct_flags = CV_NODE_SEQ; - sprintf(buf, "!!binary |"); - data = buf; - } - else if( CV_NODE_IS_FLOW(struct_flags)) - { - char c = CV_NODE_IS_MAP(struct_flags) ? '{' : '['; - struct_flags |= CV_NODE_FLOW; + if( !is_user ) + { + if (value_type == FileNode::STRING && c != '\'' && c != '\"') + goto force_string; + if( value_type == FileNode::INT ) + goto force_int; + if( value_type == FileNode::REAL ) + goto force_real; + } + } - if( type_name ) - sprintf( buf, "!!%s %c", type_name, c ); - else + if (is_binary_string) { - buf[0] = c; - buf[1] = '\0'; + int indent = static_cast(ptr - fs->bufferStart()); + ptr = fs->parseBase64(ptr, indent, node); } - data = buf; - } - else if( type_name ) - { - sprintf( buf, "!!%s", type_name ); - data = buf; - } - - icvYMLWrite( fs, key, data ); + else if( cv_isdigit(c) || + ((c == '-' || c == '+') && (cv_isdigit(d) || d == '.')) || + (c == '.' && cv_isalnum(d))) // a number + { + endptr = ptr + (c == '-' || c == '+'); + while( cv_isdigit(*endptr) ) + endptr++; + if( *endptr == '.' || *endptr == 'e' ) + { + force_real: + double fval = fs->strtod( ptr, &endptr ); + node.setValue(FileNode::REAL, &fval); + } + else + { + force_int: + int ival = (int)strtol( ptr, &endptr, 0 ); + node.setValue(FileNode::INT, &ival); + } - parent_flags = fs->struct_flags; - cvSeqPush( fs->write_stack, &parent_flags ); - fs->struct_flags = struct_flags; + if( !endptr || endptr == ptr ) + CV_PARSE_ERROR_CPP( "Invalid numeric value (inconsistent explicit type specification?)" ); - if( !CV_NODE_IS_FLOW(parent_flags) ) - fs->struct_indent += CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags); -} + ptr = endptr; + CV_PERSISTENCE_CHECK_END_OF_BUFFER_BUG_CPP(); + } + else if( c == '\'' || c == '\"' ) // an explicit string + { + if( c == '\'' ) + for( len = 0; len < CV_FS_MAX_LEN; ) + { + c = *++ptr; + if( cv_isalnum(c) || (c != '\'' && cv_isprint(c))) + buf[len++] = c; + else if( c == '\'' ) + { + c = *++ptr; + if( c != '\'' ) + break; + buf[len++] = c; + } + else + CV_PARSE_ERROR_CPP( "Invalid character" ); + } + else + for( len = 0; len < CV_FS_MAX_LEN; ) + { + c = *++ptr; + if( cv_isalnum(c) || (c != '\\' && c != '\"' && cv_isprint(c))) + buf[len++] = c; + else if( c == '\"' ) + { + ++ptr; + break; + } + else if( c == '\\' ) + { + d = *++ptr; + if( d == '\'' ) + buf[len++] = d; + else if( d == '\"' || d == '\\' || d == '\'' ) + buf[len++] = d; + else if( d == 'n' ) + buf[len++] = '\n'; + else if( d == 'r' ) + buf[len++] = '\r'; + else if( d == 't' ) + buf[len++] = '\t'; + else if( d == 'x' || (cv_isdigit(d) && d < '8') ) + { + int val, is_hex = d == 'x'; + c = ptr[3]; + ptr[3] = '\0'; + val = (int)strtol( ptr + is_hex, &endptr, is_hex ? 8 : 16 ); + ptr[3] = c; + if( endptr == ptr + is_hex ) + buf[len++] = 'x'; + else + { + buf[len++] = (char)val; + ptr = endptr; + } + } + } + else + CV_PARSE_ERROR_CPP( "Invalid character" ); + } + if( len >= CV_FS_MAX_LEN ) + CV_PARSE_ERROR_CPP( "Too long string literal" ); -void icvYMLEndWriteStruct( CvFileStorage* fs ) -{ - int parent_flags = 0, struct_flags; - char* ptr; + node.setValue(FileNode::STRING, buf, len); + } + else if( c == '[' || c == '{' ) // collection as a flow + { + int new_min_indent = min_indent + !is_parent_flow; + int struct_type = c == '{' ? FileNode::MAP : FileNode::SEQ; + int nelems = 0; - struct_flags = fs->struct_flags; - if( fs->write_stack->total == 0 ) - CV_Error( CV_StsError, "EndWriteStruct w/o matching StartWriteStruct" ); + fs->convertToCollection(struct_type, node); + d = c == '[' ? ']' : '}'; - cvSeqPop( fs->write_stack, &parent_flags ); + for( ++ptr ;; nelems++ ) + { + FileNode elem; - if( CV_NODE_IS_FLOW(struct_flags) ) - { - ptr = fs->buffer; - if( ptr > fs->buffer_start + fs->struct_indent && !CV_NODE_IS_EMPTY(struct_flags) ) - *ptr++ = ' '; - *ptr++ = CV_NODE_IS_MAP(struct_flags) ? '}' : ']'; - fs->buffer = ptr; - } - else if( CV_NODE_IS_EMPTY(struct_flags) ) - { - ptr = icvFSFlush(fs); - memcpy( ptr, CV_NODE_IS_MAP(struct_flags) ? "{}" : "[]", 2 ); - fs->buffer = ptr + 2; - } + ptr = skipSpaces( ptr, new_min_indent, INT_MAX ); + if( *ptr == '}' || *ptr == ']' ) + { + if( *ptr != d ) + CV_PARSE_ERROR_CPP( "The wrong closing bracket" ); + ptr++; + break; + } - if( !CV_NODE_IS_FLOW(parent_flags) ) - fs->struct_indent -= CV_YML_INDENT + CV_NODE_IS_FLOW(struct_flags); - assert( fs->struct_indent >= 0 ); + if( nelems != 0 ) + { + if( *ptr != ',' ) + CV_PARSE_ERROR_CPP( "Missing , between the elements" ); + ptr = skipSpaces( ptr + 1, new_min_indent, INT_MAX ); + } - fs->struct_flags = parent_flags; -} + if( struct_type == FileNode::MAP ) + { + ptr = parseKey( ptr, node, elem ); + ptr = skipSpaces( ptr, new_min_indent, INT_MAX ); + } + else + { + if( *ptr == ']' ) + break; + elem = fs->addNode(node, std::string(), FileNode::NONE); + } + ptr = parseValue( ptr, elem, new_min_indent, true ); + } + fs->finalizeCollection(node); + } + else + { + int indent, struct_type; + if( is_parent_flow || c != '-' ) + { + // implicit (one-line) string or nested block-style collection + if( !is_parent_flow ) + { + if( c == '?' ) + CV_PARSE_ERROR_CPP( "Complex keys are not supported" ); + if( c == '|' || c == '>' ) + CV_PARSE_ERROR_CPP( "Multi-line text literals are not supported" ); + } -void icvYMLStartNextStream( CvFileStorage* fs ) -{ - if( !fs->is_first ) - { - while( fs->write_stack->total > 0 ) - icvYMLEndWriteStruct(fs); - - fs->struct_indent = 0; - icvFSFlush(fs); - icvPuts( fs, "...\n" ); - icvPuts( fs, "---\n" ); - fs->buffer = fs->buffer_start; - } -} + force_string: + endptr = ptr - 1; + do c = *++endptr; + while( cv_isprint(c) && + (!is_parent_flow || (c != ',' && c != '}' && c != ']')) && + (is_parent_flow || c != ':' || value_type == FileNode::STRING)); -void icvYMLWriteInt( CvFileStorage* fs, const char* key, int value ) -{ - char buf[128]; - icvYMLWrite( fs, key, icv_itoa( value, buf, 10 )); -} + if( endptr == ptr ) + CV_PARSE_ERROR_CPP( "Invalid character" ); + if( is_parent_flow || c != ':' ) + { + char* str_end = endptr; + // strip spaces in the end of string + do c = *--str_end; + while( str_end > ptr && c == ' ' ); + str_end++; + node.setValue(FileNode::STRING, ptr, (int)(str_end - ptr)); + ptr = endptr; + return ptr; + } + struct_type = FileNode::MAP; + } + else + struct_type = FileNode::SEQ; -void icvYMLWriteReal( CvFileStorage* fs, const char* key, double value ) -{ - char buf[128]; - icvYMLWrite( fs, key, icvDoubleToString( buf, value )); -} + fs->convertToCollection( struct_type, node ); + indent = (int)(ptr - fs->bufferStart()); + for(;;) + { + FileNode elem; -void icvYMLWriteString( CvFileStorage* fs, const char* key, const char* str, int quote) -{ - char buf[CV_FS_MAX_LEN*4+16]; - char* data = (char*)str; - int i, len; + if( struct_type == FileNode::MAP ) + { + ptr = parseKey( ptr, node, elem ); + } + else + { + c = *ptr++; + if( c != '-' ) + CV_PARSE_ERROR_CPP( "Block sequence elements must be preceded with \'-\'" ); - if( !str ) - CV_Error( CV_StsNullPtr, "Null string pointer" ); + elem = fs->addNode(node, std::string(), FileNode::NONE); + } + ptr = skipSpaces( ptr, indent + 1, INT_MAX ); + ptr = parseValue( ptr, elem, indent + 1, false ); + ptr = skipSpaces( ptr, 0, INT_MAX ); + if( ptr - fs->bufferStart() != indent ) + { + if( ptr - fs->bufferStart() < indent ) + break; + else + CV_PARSE_ERROR_CPP( "Incorrect indentation" ); + } + if( memcmp( ptr, "...", 3 ) == 0 ) + break; + } + fs->finalizeCollection(node); + } - len = (int)strlen(str); - if( len > CV_FS_MAX_LEN ) - CV_Error( CV_StsBadArg, "The written string is too long" ); + return ptr; + } - if( quote || len == 0 || str[0] != str[len-1] || (str[0] != '\"' && str[0] != '\'') ) + bool parse( char* ptr ) { - int need_quote = quote || len == 0 || str[0] == ' '; - data = buf; - *data++ = '\"'; - for( i = 0; i < len; i++ ) - { - char c = str[i]; + bool first = true; + bool ok = true; + FileNode root_collection(fs->getFS(), 0, 0); - if( !need_quote && !cv_isalnum(c) && c != '_' && c != ' ' && c != '-' && - c != '(' && c != ')' && c != '/' && c != '+' && c != ';' ) - need_quote = 1; - - if( !cv_isalnum(c) && (!cv_isprint(c) || c == '\\' || c == '\'' || c == '\"') ) + for(;;) + { + // 0. skip leading comments and directives and ... + // 1. reach the first item + for(;;) { - *data++ = '\\'; - if( cv_isprint(c) ) - *data++ = c; - else if( c == '\n' ) - *data++ = 'n'; - else if( c == '\r' ) - *data++ = 'r'; - else if( c == '\t' ) - *data++ = 't'; - else + ptr = skipSpaces( ptr, 0, INT_MAX ); + if( !ptr || *ptr == '\0' ) + { + ok = !first; + break; + } + + if( *ptr == '%' ) + { + if( memcmp( ptr, "%YAML", 5 ) == 0 && + memcmp( ptr, "%YAML:1.", 8 ) != 0 && + memcmp( ptr, "%YAML 1.", 8 ) != 0) + CV_PARSE_ERROR_CPP( "Unsupported YAML version (it must be 1.x)" ); + *ptr = '\0'; + } + else if( *ptr == '-' ) { - sprintf( data, "x%02x", c ); - data += 3; + if( memcmp(ptr, "---", 3) == 0 ) + { + ptr += 3; + break; + } + else if( first ) + break; } + else if( cv_isalnum(*ptr) || *ptr=='_') + { + if( !first ) + CV_PARSE_ERROR_CPP( "The YAML streams must start with '---', except the first one" ); + break; + } + else if( fs->eof() ) + break; + else + CV_PARSE_ERROR_CPP( "Invalid or unsupported syntax" ); } - else - *data++ = c; + + if( ptr ) + ptr = skipSpaces( ptr, 0, INT_MAX ); + if( !ptr || !ptr[0] ) + break; + if( memcmp( ptr, "...", 3 ) != 0 ) + { + // 2. parse the collection + FileNode root_node = fs->addNode(root_collection, std::string(), FileNode::NONE); + + ptr = parseValue( ptr, root_node, 0, false ); + if( !root_node.isMap() && !root_node.isSeq() ) + CV_PARSE_ERROR_CPP( "Only collections as YAML streams are supported by this parser" ); + + // 3. parse until the end of file or next collection + ptr = skipSpaces( ptr, 0, INT_MAX ); + if( !ptr ) + break; + } + + if( fs->eof() ) + break; + ptr += 3; + first = false; } - if( !need_quote && (cv_isdigit(str[0]) || - str[0] == '+' || str[0] == '-' || str[0] == '.' )) - need_quote = 1; - if( need_quote ) - *data++ = '\"'; - *data++ = '\0'; - data = buf + !need_quote; + return ok; } - icvYMLWrite( fs, key, data ); -} + FileStorage_API* fs; + char buf[CV_FS_MAX_LEN+1024]; +}; +Ptr createYAMLEmitter(FileStorage_API* fs) +{ + return makePtr(fs); +} -void icvYMLWriteComment( CvFileStorage* fs, const char* comment, int eol_comment ) +Ptr createYAMLParser(FileStorage_API* fs) { - int len; //, indent; - int multiline; - const char* eol; - char* ptr; - - if( !comment ) - CV_Error( CV_StsNullPtr, "Null comment" ); - - len = (int)strlen(comment); - eol = strchr(comment, '\n'); - multiline = eol != 0; - ptr = fs->buffer; - - if( !eol_comment || multiline || - fs->buffer_end - ptr < len || ptr == fs->buffer_start ) - ptr = icvFSFlush( fs ); - else - *ptr++ = ' '; - - while( comment ) - { - *ptr++ = '#'; - *ptr++ = ' '; - if( eol ) - { - ptr = icvFSResizeWriteBuffer( fs, ptr, (int)(eol - comment) + 1 ); - memcpy( ptr, comment, eol - comment + 1 ); - fs->buffer = ptr + (eol - comment); - comment = eol + 1; - eol = strchr( comment, '\n' ); - } - else - { - len = (int)strlen(comment); - ptr = icvFSResizeWriteBuffer( fs, ptr, len ); - memcpy( ptr, comment, len ); - fs->buffer = ptr + len; - comment = 0; - } - ptr = icvFSFlush( fs ); - } + return makePtr(fs); +} + } diff --git a/modules/core/test/test_ds.cpp b/modules/core/test/test_ds.cpp index 64d54c5..6577b8e 100644 --- a/modules/core/test/test_ds.cpp +++ b/modules/core/test/test_ds.cpp @@ -327,7 +327,7 @@ public: void clear(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void run_func(void); void set_error_context( const char* condition, const char* err_msg, @@ -385,26 +385,26 @@ void Core_DynStructBaseTest::clear() } -int Core_DynStructBaseTest::read_params( CvFileStorage* fs ) +int Core_DynStructBaseTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::BaseTest::read_params( fs ); double sqrt_scale = sqrt(ts->get_test_case_count_scale()); if( code < 0 ) return code; - struct_count = cvReadInt( find_param( fs, "struct_count" ), struct_count ); - max_struct_size = cvReadInt( find_param( fs, "max_struct_size" ), max_struct_size ); - generations = cvReadInt( find_param( fs, "generations" ), generations ); - iterations = cvReadInt( find_param( fs, "iterations" ), iterations ); + read( find_param( fs, "struct_count" ), struct_count, struct_count ); + read( find_param( fs, "max_struct_size" ), max_struct_size, max_struct_size ); + read( find_param( fs, "generations" ), generations, generations ); + read( find_param( fs, "iterations" ), iterations, iterations ); generations = cvRound(generations*sqrt_scale); iterations = cvRound(iterations*sqrt_scale); - min_log_storage_block_size = cvReadInt( find_param( fs, "min_log_storage_block_size" ), - min_log_storage_block_size ); - max_log_storage_block_size = cvReadInt( find_param( fs, "max_log_storage_block_size" ), - max_log_storage_block_size ); - min_log_elem_size = cvReadInt( find_param( fs, "min_log_elem_size" ), min_log_elem_size ); - max_log_elem_size = cvReadInt( find_param( fs, "max_log_elem_size" ), max_log_elem_size ); + read( find_param( fs, "min_log_storage_block_size" ), + min_log_storage_block_size, min_log_storage_block_size ); + read( find_param( fs, "max_log_storage_block_size" ), + max_log_storage_block_size, max_log_storage_block_size ); + read( find_param( fs, "min_log_elem_size" ), min_log_elem_size, min_log_elem_size ); + read( find_param( fs, "max_log_elem_size" ), max_log_elem_size, max_log_elem_size ); struct_count = cvtest::clipInt( struct_count, 1, 100 ); max_struct_size = cvtest::clipInt( max_struct_size, 1, 1<<20 ); diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 07a9648..282ccce 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -37,44 +37,42 @@ static SparseMat cvTsGetRandomSparseMat(int dims, const int* sz, int type, return m; } -static bool cvTsCheckSparse(const CvSparseMat* m1, const CvSparseMat* m2, double eps) +static bool cvTsCheckSparse(const cv::SparseMat& m1, const cv::SparseMat& m2, double eps) { - CvSparseMatIterator it1; - CvSparseNode* node1; - int depth = CV_MAT_DEPTH(m1->type); + cv::SparseMatConstIterator it1, it1_end = m1.end(); + int depth = m1.depth(); - if( m1->heap->active_count != m2->heap->active_count || - m1->dims != m2->dims || CV_MAT_TYPE(m1->type) != CV_MAT_TYPE(m2->type) ) + if( m1.nzcount() != m2.nzcount() || + m1.dims() != m2.dims() || m1.type() != m2.type() ) return false; - for( node1 = cvInitSparseMatIterator( m1, &it1 ); - node1 != 0; node1 = cvGetNextSparseNode( &it1 )) + for( it1 = m1.begin(); it1 != it1_end; ++it1 ) { - uchar* v1 = (uchar*)CV_NODE_VAL(m1,node1); - uchar* v2 = cvPtrND( m2, CV_NODE_IDX(m1,node1), 0, 0, &node1->hashval ); + const cv::SparseMat::Node* node1 = it1.node(); + const uchar* v2 = m2.find(node1->idx, (size_t*)&node1->hashval); if( !v2 ) return false; if( depth == CV_8U || depth == CV_8S ) { - if( *v1 != *v2 ) + if( m1.value(node1) != *v2 ) return false; } else if( depth == CV_16U || depth == CV_16S ) { - if( *(ushort*)v1 != *(ushort*)v2 ) + if( m1.value(node1) != *(ushort*)v2 ) return false; } else if( depth == CV_32S ) { - if( *(int*)v1 != *(int*)v2 ) + if( m1.value(node1) != *(int*)v2 ) return false; } else if( depth == CV_32F ) { - if( fabs(*(float*)v1 - *(float*)v2) > eps*(fabs(*(float*)v2) + 1) ) + if( fabs(m1.value(node1) - *(float*)v2) > eps*(fabs(*(float*)v2) + 1) ) return false; } - else if( fabs(*(double*)v1 - *(double*)v2) > eps*(fabs(*(double*)v2) + 1) ) + else if( fabs(m1.value(node1) - *(double*)v2) > eps*(fabs(*(double*)v2) + 1) ) return false; } @@ -127,24 +125,6 @@ protected: cv::multiply(test_mat, test_mat_scale, test_mat); } - CvSeq* seq = cvCreateSeq(test_mat.type(), (int)sizeof(CvSeq), - (int)test_mat.elemSize(), storage); - cvSeqPushMulti(seq, test_mat.ptr(), test_mat.cols*test_mat.rows); - - CvGraph* graph = cvCreateGraph( CV_ORIENTED_GRAPH, - sizeof(CvGraph), sizeof(CvGraphVtx), - sizeof(CvGraphEdge), storage ); - int edges[][2] = {{0,1},{1,2},{2,0},{0,3},{3,4},{4,1}}; - int i, vcount = 5, ecount = 6; - for( i = 0; i < vcount; i++ ) - cvGraphAddVtx(graph); - for( i = 0; i < ecount; i++ ) - { - CvGraphEdge* edge; - cvGraphAddEdge(graph, edges[i][0], edges[i][1], 0, &edge); - edge->weight = (float)(i+1); - } - depth = cvtest::randInt(rng) % (CV_64F+1); cn = cvtest::randInt(rng) % 4 + 1; int sz[] = { @@ -182,14 +162,10 @@ protected: fs << "test_map" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:"; const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1}; - fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0]))); + fs.writeRaw("u", arr, sizeof(arr)); fs << "]" << "}"; - cvWriteComment(*fs, "test comment", 0); - - fs.writeObj("test_seq", seq); - fs.writeObj("test_graph",graph); - CvGraph* graph2 = (CvGraph*)cvClone(graph); + fs.writeComment("test comment", false); string content = fs.releaseAndGetString(); @@ -213,84 +189,51 @@ protected: return; } - CvMat* m = (CvMat*)fs["test_mat"].readObj(); - CvMat _test_mat = cvMat(test_mat); + Mat m; + fs["test_mat"] >> m; double max_diff = 0; - CvMat stub1, _test_stub1; - cvReshape(m, &stub1, 1, 0); - cvReshape(&_test_mat, &_test_stub1, 1, 0); + Mat stub1 = m.reshape(1, 0); + Mat test_stub1 = test_mat.reshape(1, 0); vector pt; - if( !m || !CV_IS_MAT(m) || m->rows != test_mat.rows || m->cols != test_mat.cols || - cvtest::cmpEps( cv::cvarrToMat(&stub1), cv::cvarrToMat(&_test_stub1), &max_diff, 0, &pt, true) < 0 ) + if( m.empty() || m.rows != test_mat.rows || m.cols != test_mat.cols || + cvtest::cmpEps( stub1, test_stub1, &max_diff, 0, &pt, true) < 0 ) { - ts->printf( cvtest::TS::LOG, "the read matrix is not correct: (%.20g vs %.20g) at (%d,%d)\n", - cvGetReal2D(&stub1, pt[0], pt[1]), cvGetReal2D(&_test_stub1, pt[0], pt[1]), + ts->printf( cvtest::TS::LOG, "the read matrix is not correct at (%d, %d)\n", pt[0], pt[1] ); ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); return; } - if( m && CV_IS_MAT(m)) - cvReleaseMat(&m); + m.release(); - CvMatND* m_nd = (CvMatND*)fs["test_mat_nd"].readObj(); - CvMatND _test_mat_nd = cvMatND(test_mat_nd); + Mat m_nd; + fs["test_mat_nd"] >> m_nd; - if( !m_nd || !CV_IS_MATND(m_nd) ) + if( m_nd.empty() ) { ts->printf( cvtest::TS::LOG, "the read nd-matrix is not correct\n" ); ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); return; } - CvMat stub, _test_stub; - cvGetMat(m_nd, &stub, 0, 1); - cvGetMat(&_test_mat_nd, &_test_stub, 0, 1); - cvReshape(&stub, &stub1, 1, 0); - cvReshape(&_test_stub, &_test_stub1, 1, 0); - - if( !CV_ARE_TYPES_EQ(&stub, &_test_stub) || - !CV_ARE_SIZES_EQ(&stub, &_test_stub) || - //cvNorm(&stub, &_test_stub, CV_L2) != 0 ) - cvtest::cmpEps( cv::cvarrToMat(&stub1), cv::cvarrToMat(&_test_stub1), &max_diff, 0, &pt, true) < 0 ) - { - ts->printf( cvtest::TS::LOG, "readObj method: the read nd matrix is not correct: (%.20g vs %.20g) vs at (%d,%d)\n", - cvGetReal2D(&stub1, pt[0], pt[1]), cvGetReal2D(&_test_stub1, pt[0], pt[1]), - pt[0], pt[1] ); - ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); - return; - } - - MatND mat_nd2; - fs["test_mat_nd"] >> mat_nd2; - CvMatND m_nd2 = cvMatND(mat_nd2); - cvGetMat(&m_nd2, &stub, 0, 1); - cvReshape(&stub, &stub1, 1, 0); + stub1 = m_nd.reshape(1, 0); + test_stub1 = test_mat_nd.reshape(1, 0); - if( !CV_ARE_TYPES_EQ(&stub, &_test_stub) || - !CV_ARE_SIZES_EQ(&stub, &_test_stub) || - //cvNorm(&stub, &_test_stub, CV_L2) != 0 ) - cvtest::cmpEps( cv::cvarrToMat(&stub1), cv::cvarrToMat(&_test_stub1), &max_diff, 0, &pt, true) < 0 ) + if( stub1.type() != test_stub1.type() || + stub1.size != test_stub1.size || + cvtest::cmpEps( stub1, test_stub1, &max_diff, 0, &pt, true) < 0 ) { - ts->printf( cvtest::TS::LOG, "C++ method: the read nd matrix is not correct: (%.20g vs %.20g) vs at (%d,%d)\n", - cvGetReal2D(&stub1, pt[0], pt[1]), cvGetReal2D(&_test_stub1, pt[1], pt[0]), + ts->printf( cvtest::TS::LOG, "readObj method: the read nd matrix is not correct at (%d,%d)\n", pt[0], pt[1] ); ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); return; } + m_nd.release(); - cvRelease((void**)&m_nd); - - Ptr m_s((CvSparseMat*)fs["test_sparse_mat"].readObj()); - Ptr _test_sparse_(cvCreateSparseMat(test_sparse_mat)); - Ptr _test_sparse((CvSparseMat*)cvClone(_test_sparse_)); - SparseMat m_s2; - fs["test_sparse_mat"] >> m_s2; - Ptr _m_s2(cvCreateSparseMat(m_s2)); + SparseMat m_s; + fs["test_sparse_mat"] >> m_s; - if( !m_s || !CV_IS_SPARSE_MAT(m_s.get()) || - !cvTsCheckSparse(m_s.get(), _test_sparse.get(), 0) || - !cvTsCheckSparse(_m_s2.get(), _test_sparse.get(), 0)) + if( m_s.nzcount() == 0 || !cvTsCheckSparse(m_s, test_sparse_mat, 0)) { ts->printf( cvtest::TS::LOG, "the read sparse matrix is not correct\n" ); ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); @@ -333,16 +276,16 @@ protected: it += 1; real_lbp_val |= (int)*it << 3; FileNodeIterator it2(it); - it2 += 4; - real_lbp_val |= (int)*it2 << 7; - --it2; - real_lbp_val |= (int)*it2 << 6; - it2--; - real_lbp_val |= (int)*it2 << 5; - it2 -= 1; + it2++; real_lbp_val |= (int)*it2 << 4; - it2 += -1; - CV_Assert( it == it2 ); + ++it2; + real_lbp_val |= (int)*it2 << 5; + it2 += 1; + real_lbp_val |= (int)*it2 << 6; + it2++; + real_lbp_val |= (int)*it2 << 7; + ++it2; + CV_Assert( it2 == tm_lbp.end() ); if( tm.type() != FileNode::MAP || tm.size() != 5 || real_x != 1 || @@ -358,28 +301,6 @@ protected: return; } - CvGraph* graph3 = (CvGraph*)fs["test_graph"].readObj(); - if(graph2->active_count != vcount || graph3->active_count != vcount || - graph2->edges->active_count != ecount || graph3->edges->active_count != ecount) - { - ts->printf( cvtest::TS::LOG, "the cloned or read graph have wrong number of vertices or edges\n" ); - ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); - return; - } - - for( i = 0; i < ecount; i++ ) - { - CvGraphEdge* edge2 = cvFindGraphEdge(graph2, edges[i][0], edges[i][1]); - CvGraphEdge* edge3 = cvFindGraphEdge(graph3, edges[i][0], edges[i][1]); - if( !edge2 || edge2->weight != (float)(i+1) || - !edge3 || edge3->weight != (float)(i+1) ) - { - ts->printf( cvtest::TS::LOG, "the cloned or read graph do not have the edge (%d, %d)\n", edges[i][0], edges[i][1] ); - ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); - return; - } - } - fs.release(); if( !mem ) remove(filename.c_str()); @@ -431,13 +352,14 @@ public: protected: void run(int) { - const char * suffix[3] = { + const char * suffix[] = { ".yml", ".xml", ".json" }; + int ncases = (int)(sizeof(suffix)/sizeof(suffix[0])); - for ( size_t i = 0u; i < 3u; i++ ) + for ( int i = 0; i < ncases; i++ ) { try { @@ -685,8 +607,6 @@ TEST(Core_InputOutput, filestorage_base64_basic) cv::Mat _nd_out, _nd_in; cv::Mat _rd_out(64, 64, CV_64FC1), _rd_in; - bool no_type_id = true; - { /* init */ /* a normal mat */ @@ -729,11 +649,12 @@ TEST(Core_InputOutput, filestorage_base64_basic) fs << "normal_nd_mat" << _nd_out; fs << "empty_2d_mat" << _em_out; fs << "random_mat" << _rd_out; + fs << "rawdata" << "[:"; + size_t esz = sizeof(data_t); - cvStartWriteStruct( *fs, "rawdata", CV_NODE_SEQ | CV_NODE_FLOW, "binary" ); for (int i = 0; i < 10; i++) - cvWriteRawDataBase64(*fs, rawdata.data() + i * 100, 100, data_t::signature()); - cvEndWriteStruct( *fs ); + fs.writeRaw(data_t::signature(), rawdata.data() + i * 100, 100*esz ); + fs << "]"; fs.release(); } @@ -747,30 +668,25 @@ TEST(Core_InputOutput, filestorage_base64_basic) fs["normal_nd_mat"] >> _nd_in; fs["random_mat"] >> _rd_in; - if ( !fs["empty_2d_mat"]["type_id"].empty() || - !fs["normal_2d_mat"]["type_id"].empty() || - !fs["normal_nd_mat"]["type_id"].empty() || - !fs[ "random_mat"]["type_id"].empty() ) - no_type_id = false; - /* raw data */ std::vector(1000).swap(rawdata); - cvReadRawData(*fs, fs["rawdata"].node, rawdata.data(), data_t::signature()); + fs["rawdata"].readRaw(data_t::signature(), &rawdata[0], 1000*sizeof(rawdata[0])); fs.release(); } int errors = 0; + const data_t* rawdata_ptr = &rawdata[0]; for (int i = 0; i < 1000; i++) { - EXPECT_EQ((int)rawdata[i].u1, 1); - EXPECT_EQ((int)rawdata[i].u2, 2); - EXPECT_EQ((int)rawdata[i].i1, 1); - EXPECT_EQ((int)rawdata[i].i2, 2); - EXPECT_EQ((int)rawdata[i].i3, 3); - EXPECT_EQ(rawdata[i].d1, 0.1); - EXPECT_EQ(rawdata[i].d2, 0.2); - EXPECT_EQ((int)rawdata[i].i4, i); + EXPECT_EQ((int)rawdata_ptr[i].u1, 1); + EXPECT_EQ((int)rawdata_ptr[i].u2, 2); + EXPECT_EQ((int)rawdata_ptr[i].i1, 1); + EXPECT_EQ((int)rawdata_ptr[i].i2, 2); + EXPECT_EQ((int)rawdata_ptr[i].i3, 3); + EXPECT_EQ(rawdata_ptr[i].d1, 0.1); + EXPECT_EQ(rawdata_ptr[i].d2, 0.2); + EXPECT_EQ((int)rawdata_ptr[i].i4, i); if (::testing::Test::HasNonfatalFailure()) { printf("i = %d\n", i); @@ -780,8 +696,6 @@ TEST(Core_InputOutput, filestorage_base64_basic) break; } - EXPECT_TRUE(no_type_id); - EXPECT_EQ(_em_in.rows , _em_out.rows); EXPECT_EQ(_em_in.cols , _em_out.cols); EXPECT_EQ(_em_in.depth(), _em_out.depth()); @@ -865,13 +779,13 @@ TEST(Core_InputOutput, filestorage_base64_valid_call) { cv::FileStorage fs(name, cv::FileStorage::WRITE_BASE64); - cvStartWriteStruct(*fs, "manydata", CV_NODE_SEQ); - cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW); + fs << "manydata" << "["; + fs << "[:"; for (int i = 0; i < 10; i++) - cvWriteRawData(*fs, rawdata.data(), static_cast(rawdata.size()), "i"); - cvEndWriteStruct(*fs); - cvWriteString(*fs, 0, str_out.c_str(), 1); - cvEndWriteStruct(*fs); + fs.writeRaw( "i", rawdata.data(), rawdata.size()*sizeof(rawdata[0])); + fs << "]"; + fs << str_out; + fs << "]"; fs.release(); }); @@ -879,7 +793,7 @@ TEST(Core_InputOutput, filestorage_base64_valid_call) { cv::FileStorage fs(name, cv::FileStorage::READ); std::vector data_in(rawdata.size()); - fs["manydata"][0].readRaw("i", (uchar *)data_in.data(), data_in.size()); + fs["manydata"][0].readRaw("i", data_in.data(), data_in.size()*sizeof(data_in[0])); EXPECT_TRUE(fs["manydata"][0].isSeq()); EXPECT_TRUE(std::equal(rawdata.begin(), rawdata.end(), data_in.begin())); cv::String str_in; @@ -893,13 +807,13 @@ TEST(Core_InputOutput, filestorage_base64_valid_call) { cv::FileStorage fs(name, cv::FileStorage::WRITE); - cvStartWriteStruct(*fs, "manydata", CV_NODE_SEQ); - cvWriteString(*fs, 0, str_out.c_str(), 1); - cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW, "binary"); + fs << "manydata" << "["; + fs << str_out; + fs << "["; for (int i = 0; i < 10; i++) - cvWriteRawData(*fs, rawdata.data(), static_cast(rawdata.size()), "i"); - cvEndWriteStruct(*fs); - cvEndWriteStruct(*fs); + fs.writeRaw("i", rawdata.data(), rawdata.size()*sizeof(rawdata[0])); + fs << "]"; + fs << "]"; fs.release(); }); @@ -911,7 +825,7 @@ TEST(Core_InputOutput, filestorage_base64_valid_call) EXPECT_TRUE(fs["manydata"][0].isString()); EXPECT_EQ(str_in, str_out); std::vector data_in(rawdata.size()); - fs["manydata"][1].readRaw("i", (uchar *)data_in.data(), data_in.size()); + fs["manydata"][1].readRaw("i", (uchar *)data_in.data(), data_in.size()*sizeof(data_in[0])); EXPECT_TRUE(fs["manydata"][1].isSeq()); EXPECT_TRUE(std::equal(rawdata.begin(), rawdata.end(), data_in.begin())); fs.release(); @@ -940,17 +854,17 @@ TEST(Core_InputOutput, filestorage_base64_invalid_call) char const * suffix_name = *ptr; std::string name = basename + '_' + suffix_name; - EXPECT_ANY_THROW({ + EXPECT_NO_THROW({ cv::FileStorage fs(name, cv::FileStorage::WRITE); - cvStartWriteStruct(*fs, "rawdata", CV_NODE_SEQ, "binary"); - cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW); + fs << "rawdata" << "["; + fs << "[:"; }); - EXPECT_ANY_THROW({ + EXPECT_NO_THROW({ cv::FileStorage fs(name, cv::FileStorage::WRITE); - cvStartWriteStruct(*fs, "rawdata", CV_NODE_SEQ); - cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW); - cvWriteRawDataBase64(*fs, name.c_str(), 1, "u"); + fs << "rawdata" << "["; + fs << "[:"; + fs.writeRaw("u", name.c_str(), 1); }); remove(name.c_str()); diff --git a/modules/features2d/src/matchers.cpp b/modules/features2d/src/matchers.cpp index ca137e2..758b4fa 100644 --- a/modules/features2d/src/matchers.cpp +++ b/modules/features2d/src/matchers.cpp @@ -1238,19 +1238,19 @@ void FlannBasedMatcher::read( const FileNode& fn) searchParams->setInt(_name, (int) sp[i]["value"]); break; case FLANN_INDEX_TYPE_32F: - searchParams->setFloat(_name, (float) ip[i]["value"]); + searchParams->setFloat(_name, (float) sp[i]["value"]); break; case FLANN_INDEX_TYPE_64F: - searchParams->setDouble(_name, (double) ip[i]["value"]); + searchParams->setDouble(_name, (double) sp[i]["value"]); break; case FLANN_INDEX_TYPE_STRING: - searchParams->setString(_name, (String) ip[i]["value"]); + searchParams->setString(_name, (String) sp[i]["value"]); break; case FLANN_INDEX_TYPE_BOOL: - searchParams->setBool(_name, (int) ip[i]["value"] != 0); + searchParams->setBool(_name, (int) sp[i]["value"] != 0); break; case FLANN_INDEX_TYPE_ALGORITHM: - searchParams->setAlgorithm((int) ip[i]["value"]); + searchParams->setAlgorithm((int) sp[i]["value"]); break; // don't default: - compiler warning is here }; diff --git a/modules/features2d/test/test_matchers_algorithmic.cpp b/modules/features2d/test/test_matchers_algorithmic.cpp index 9692151..f8a1799 100644 --- a/modules/features2d/test/test_matchers_algorithmic.cpp +++ b/modules/features2d/test/test_matchers_algorithmic.cpp @@ -558,4 +558,41 @@ TEST( Features2d_DMatch, read_write ) ASSERT_NE( strstr(str.c_str(), "4.5"), (char*)0 ); } +TEST( Features2d_FlannBasedMatcher, read_write ) +{ + static const char* ymlfile = "%YAML:1.0\n---\n" + "format: 3\n" + "indexParams:\n" + " -\n" + " name: algorithm\n" + " type: 9\n" // FLANN_INDEX_TYPE_ALGORITHM + " value: 6\n"// this line is changed! + " -\n" + " name: trees\n" + " type: 4\n" + " value: 4\n" + "searchParams:\n" + " -\n" + " name: checks\n" + " type: 4\n" + " value: 32\n" + " -\n" + " name: eps\n" + " type: 5\n" + " value: 4.\n"// this line is changed! + " -\n" + " name: sorted\n" + " type: 8\n" // FLANN_INDEX_TYPE_BOOL + " value: 1\n"; + + Ptr matcher = FlannBasedMatcher::create(); + FileStorage fs_in(ymlfile, FileStorage::READ + FileStorage::MEMORY); + matcher->read(fs_in.root()); + FileStorage fs_out(".yml", FileStorage::WRITE + FileStorage::MEMORY); + matcher->write(fs_out); + std::string out = fs_out.releaseAndGetString(); + + EXPECT_EQ(ymlfile, out); +} + }} // namespace diff --git a/modules/imgproc/src/histogram.cpp b/modules/imgproc/src/histogram.cpp index c116a98..6e7d2e6 100644 --- a/modules/imgproc/src/histogram.cpp +++ b/modules/imgproc/src/histogram.cpp @@ -3386,6 +3386,7 @@ void cv::equalizeHist( InputArray _src, OutputArray _dst ) lutBody(heightRange); } +#if 0 // ---------------------------------------------------------------------- /* Implementation of RTTI and Generic Functions for CvHistogram */ @@ -3537,5 +3538,6 @@ static void icvWriteHist( CvFileStorage* fs, const char* name, CvType hist_type( CV_TYPE_NAME_HIST, icvIsHist, (CvReleaseFunc)cvReleaseHist, icvReadHist, icvWriteHist, (CvCloneFunc)icvCloneHist ); +#endif /* End of file. */ diff --git a/modules/imgproc/test/test_approxpoly.cpp b/modules/imgproc/test/test_approxpoly.cpp index 69511a6..7a3df9f 100644 --- a/modules/imgproc/test/test_approxpoly.cpp +++ b/modules/imgproc/test/test_approxpoly.cpp @@ -64,7 +64,7 @@ public: //int write_default_params(CvFileStorage* fs); protected: - //int read_params( CvFileStorage* fs ); + //int read_params( const cv::FileStorage& fs ); int check_slice( CvPoint StartPt, CvPoint EndPt, CvSeqReader* SrcReader, float Eps, @@ -106,7 +106,7 @@ void CV_ApproxPolyTest::clear() } -int CV_ApproxPolyTest::read_params( CvFileStorage* fs ) +int CV_ApproxPolyTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::BaseTest::read_params( fs ); if( code < 0 ) diff --git a/modules/imgproc/test/test_contours.cpp b/modules/imgproc/test/test_contours.cpp index a5c9248..5043e8f 100644 --- a/modules/imgproc/test/test_contours.cpp +++ b/modules/imgproc/test/test_contours.cpp @@ -54,7 +54,7 @@ public: void clear(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); int prepare_test_case( int test_case_idx ); int validate_test_results( int test_case_idx ); void run_func(); @@ -125,7 +125,7 @@ void CV_FindContourTest::clear() } -int CV_FindContourTest::read_params( CvFileStorage* fs ) +int CV_FindContourTest::read_params( const cv::FileStorage& fs ) { int t; int code = cvtest::BaseTest::read_params( fs ); @@ -133,13 +133,13 @@ int CV_FindContourTest::read_params( CvFileStorage* fs ) if( code < 0 ) return code; - min_blob_size = cvReadInt( find_param( fs, "min_blob_size" ), min_blob_size ); - max_blob_size = cvReadInt( find_param( fs, "max_blob_size" ), max_blob_size ); - max_log_blob_count = cvReadInt( find_param( fs, "max_log_blob_count" ), max_log_blob_count ); - min_log_img_width = cvReadInt( find_param( fs, "min_log_img_width" ), min_log_img_width ); - max_log_img_width = cvReadInt( find_param( fs, "max_log_img_width" ), max_log_img_width ); - min_log_img_height = cvReadInt( find_param( fs, "min_log_img_height"), min_log_img_height ); - max_log_img_height = cvReadInt( find_param( fs, "max_log_img_height"), max_log_img_height ); + read( find_param( fs, "min_blob_size" ), min_blob_size, min_blob_size ); + read( find_param( fs, "max_blob_size" ), max_blob_size, max_blob_size ); + read( find_param( fs, "max_log_blob_count" ), max_log_blob_count, max_log_blob_count ); + read( find_param( fs, "min_log_img_width" ), min_log_img_width, min_log_img_width ); + read( find_param( fs, "max_log_img_width" ), max_log_img_width, max_log_img_width ); + read( find_param( fs, "min_log_img_height"), min_log_img_height, min_log_img_height ); + read( find_param( fs, "max_log_img_height"), max_log_img_height, max_log_img_height ); min_blob_size = cvtest::clipInt( min_blob_size, 1, 100 ); max_blob_size = cvtest::clipInt( max_blob_size, 1, 100 ); diff --git a/modules/imgproc/test/test_convhull.cpp b/modules/imgproc/test/test_convhull.cpp index 3f82e04..ccd7e81 100644 --- a/modules/imgproc/test/test_convhull.cpp +++ b/modules/imgproc/test/test_convhull.cpp @@ -196,7 +196,7 @@ public: void clear(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void run_func(void); int prepare_test_case( int test_case_idx ); int validate_test_results( int test_case_idx ); @@ -254,15 +254,15 @@ void CV_BaseShapeDescrTest::clear() } -int CV_BaseShapeDescrTest::read_params( CvFileStorage* fs ) +int CV_BaseShapeDescrTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::BaseTest::read_params( fs ); if( code < 0 ) return code; - test_case_count = cvReadInt( find_param( fs, "struct_count" ), test_case_count ); - min_log_size = cvReadInt( find_param( fs, "min_log_size" ), min_log_size ); - max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size ); + read( find_param( fs, "struct_count" ), test_case_count, test_case_count ); + read( find_param( fs, "min_log_size" ), min_log_size, min_log_size ); + read( find_param( fs, "max_log_size" ), max_log_size, max_log_size ); min_log_size = cvtest::clipInt( min_log_size, 0, 8 ); max_log_size = cvtest::clipInt( max_log_size, 0, 10 ); diff --git a/modules/imgproc/test/test_filter.cpp b/modules/imgproc/test/test_filter.cpp index b345994..10b77ca 100644 --- a/modules/imgproc/test/test_filter.cpp +++ b/modules/imgproc/test/test_filter.cpp @@ -50,7 +50,7 @@ public: protected: int prepare_test_case( int test_case_idx ); - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high ); Size aperture_size; @@ -76,13 +76,13 @@ CV_FilterBaseTest::CV_FilterBaseTest( bool _fp_kernel ) : fp_kernel(_fp_kernel) } -int CV_FilterBaseTest::read_params( CvFileStorage* fs ) +int CV_FilterBaseTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); if( code < 0 ) return code; - max_aperture_size = cvReadInt( find_param( fs, "max_aperture_size" ), max_aperture_size ); + read( find_param( fs, "max_aperture_size" ), max_aperture_size, max_aperture_size ); max_aperture_size = cvtest::clipInt( max_aperture_size, 1, 100 ); return code; @@ -1265,7 +1265,7 @@ public: CV_FeatureSelBaseTest( int width_factor ); protected: - int read_params( CvFileStorage* fs ); + int read_params( const FileStorage& fs ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high ); double get_success_error_level( int test_case_idx, int i, int j ); @@ -1289,15 +1289,15 @@ CV_FeatureSelBaseTest::CV_FeatureSelBaseTest( int _width_factor ) } -int CV_FeatureSelBaseTest::read_params( CvFileStorage* fs ) +int CV_FeatureSelBaseTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::BaseTest::read_params( fs ); if( code < 0 ) return code; - max_aperture_size = cvReadInt( find_param( fs, "max_aperture_size" ), max_aperture_size ); + read( find_param( fs, "max_aperture_size" ), max_aperture_size, max_aperture_size ); max_aperture_size = cvtest::clipInt( max_aperture_size, 1, 9 ); - max_block_size = cvReadInt( find_param( fs, "max_block_size" ), max_block_size ); + read( find_param( fs, "max_block_size" ), max_block_size, max_block_size ); max_block_size = cvtest::clipInt( max_aperture_size, 1, 100 ); return code; diff --git a/modules/imgproc/test/test_histograms.cpp b/modules/imgproc/test/test_histograms.cpp index 4214892..4f2490e 100644 --- a/modules/imgproc/test/test_histograms.cpp +++ b/modules/imgproc/test/test_histograms.cpp @@ -53,7 +53,7 @@ public: void clear(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void run_func(void); int prepare_test_case( int test_case_idx ); int validate_test_results( int test_case_idx ); @@ -115,19 +115,19 @@ void CV_BaseHistTest::clear() } -int CV_BaseHistTest::read_params( CvFileStorage* fs ) +int CV_BaseHistTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::BaseTest::read_params( fs ); if( code < 0 ) return code; - test_case_count = cvReadInt( find_param( fs, "struct_count" ), test_case_count ); - max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size ); + read( find_param( fs, "struct_count" ), test_case_count, test_case_count ); + read( find_param( fs, "max_log_size" ), max_log_size, max_log_size ); max_log_size = cvtest::clipInt( max_log_size, 1, 20 ); - img_max_log_size = cvReadInt( find_param( fs, "max_log_array_size" ), img_max_log_size ); + read( find_param( fs, "max_log_array_size" ), img_max_log_size, img_max_log_size ); img_max_log_size = cvtest::clipInt( img_max_log_size, 1, 9 ); - max_cdims = cvReadInt( find_param( fs, "max_cdims" ), max_cdims ); + read( find_param( fs, "max_cdims" ), max_cdims, max_cdims ); max_cdims = cvtest::clipInt( max_cdims, 1, 6 ); return 0; diff --git a/modules/imgproc/test/test_imgwarp.cpp b/modules/imgproc/test/test_imgwarp.cpp index 07a7511..7f6caa4 100644 --- a/modules/imgproc/test/test_imgwarp.cpp +++ b/modules/imgproc/test/test_imgwarp.cpp @@ -49,7 +49,7 @@ public: CV_ImgWarpBaseTest( bool warp_matrix ); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); int prepare_test_case( int test_case_idx ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high ); @@ -76,7 +76,7 @@ CV_ImgWarpBaseTest::CV_ImgWarpBaseTest( bool warp_matrix ) } -int CV_ImgWarpBaseTest::read_params( CvFileStorage* fs ) +int CV_ImgWarpBaseTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); return code; diff --git a/modules/imgproc/test/test_templmatch.cpp b/modules/imgproc/test/test_templmatch.cpp index d1389f4..d38aa24 100644 --- a/modules/imgproc/test/test_templmatch.cpp +++ b/modules/imgproc/test/test_templmatch.cpp @@ -49,7 +49,7 @@ public: CV_TemplMatchTest(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void get_test_array_types_and_sizes( int test_case_idx, vector >& sizes, vector >& types ); void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high ); double get_success_error_level( int test_case_idx, int i, int j ); @@ -75,13 +75,13 @@ CV_TemplMatchTest::CV_TemplMatchTest() } -int CV_TemplMatchTest::read_params( CvFileStorage* fs ) +int CV_TemplMatchTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::ArrayTest::read_params( fs ); if( code < 0 ) return code; - max_template_size = cvReadInt( find_param( fs, "max_template_size" ), max_template_size ); + read( find_param( fs, "max_template_size" ), max_template_size, max_template_size ); max_template_size = cvtest::clipInt( max_template_size, 1, 100 ); return code; diff --git a/modules/imgproc/test/test_watershed.cpp b/modules/imgproc/test/test_watershed.cpp index dbd3eae..88518aa 100644 --- a/modules/imgproc/test/test_watershed.cpp +++ b/modules/imgproc/test/test_watershed.cpp @@ -42,6 +42,7 @@ #include "test_precomp.hpp" +#if 0 namespace opencv_test { namespace { class CV_WatershedTest : public cvtest::BaseTest @@ -125,3 +126,5 @@ void CV_WatershedTest::run( int /* start_from */) TEST(Imgproc_Watershed, regression) { CV_WatershedTest test; test.safe_run(); } }} // namespace + +#endif diff --git a/modules/ml/test/test_mltests2.cpp b/modules/ml/test/test_mltests2.cpp index 44d5aeb..8ca6177 100644 --- a/modules/ml/test/test_mltests2.cpp +++ b/modules/ml/test/test_mltests2.cpp @@ -421,10 +421,9 @@ CV_MLBaseTest::~CV_MLBaseTest() theRNG().state = initSeed; } -int CV_MLBaseTest::read_params( CvFileStorage* __fs ) +int CV_MLBaseTest::read_params( const cv::FileStorage& _fs ) { CV_TRACE_FUNCTION(); - FileStorage _fs(__fs, false); if( !_fs.isOpened() ) test_case_count = -1; else @@ -452,7 +451,7 @@ void CV_MLBaseTest::run( int ) string filename = ts->get_data_path(); filename += get_validation_filename(); validationFS.open( filename, FileStorage::READ ); - read_params( *validationFS ); + read_params( validationFS ); int code = cvtest::TS::OK; for (int i = 0; i < test_case_count; i++) diff --git a/modules/ml/test/test_precomp.hpp b/modules/ml/test/test_precomp.hpp index 142bf6a..3cc2c22 100644 --- a/modules/ml/test/test_precomp.hpp +++ b/modules/ml/test/test_precomp.hpp @@ -40,7 +40,7 @@ public: CV_MLBaseTest( const char* _modelName ); virtual ~CV_MLBaseTest(); protected: - virtual int read_params( CvFileStorage* fs ); + virtual int read_params( const cv::FileStorage& fs ); virtual void run( int startFrom ); virtual int prepare_test_case( int testCaseIdx ); virtual std::string& get_validation_filename(); diff --git a/modules/objdetect/src/cascadedetect.cpp b/modules/objdetect/src/cascadedetect.cpp index 00e49a4..c46c3e3 100644 --- a/modules/objdetect/src/cascadedetect.cpp +++ b/modules/objdetect/src/cascadedetect.cpp @@ -913,13 +913,23 @@ bool CascadeClassifierImpl::load(const String& filename) if( !fs.isOpened() ) return false; - if( read_(fs.getFirstTopLevelNode()) ) + FileNode fs_root = fs.getFirstTopLevelNode(); + + if( read_(fs_root) ) return true; - fs.release(); + // probably, it's the cascade in the old format; + // let's try to convert it to the new format + FileStorage newfs(".yml", FileStorage::WRITE+FileStorage::MEMORY); + haar_cvt::convert(fs_root, newfs); + std::string newfs_content = newfs.releaseAndGetString(); + newfs.open(newfs_content, FileStorage::READ+FileStorage::MEMORY); + fs_root = newfs.getFirstTopLevelNode(); - oldCascade.reset((CvHaarClassifierCascade*)cvLoad(filename.c_str(), 0, 0, 0)); - return !oldCascade.empty(); + if( read_(fs_root) ) + return true; + + return false; } void CascadeClassifierImpl::read(const FileNode& node) diff --git a/modules/objdetect/src/cascadedetect.hpp b/modules/objdetect/src/cascadedetect.hpp index f991053..a011ed4 100644 --- a/modules/objdetect/src/cascadedetect.hpp +++ b/modules/objdetect/src/cascadedetect.hpp @@ -647,4 +647,10 @@ inline int predictCategoricalStump( CascadeClassifierImpl& cascade, sum = (double)tmp; return 1; } + +namespace haar_cvt +{ +bool convert(const FileNode& oldcascade_root, FileStorage& newfs); +} + } diff --git a/modules/objdetect/src/cascadedetect_convert.cpp b/modules/objdetect/src/cascadedetect_convert.cpp index 28dc019..02eafe0 100644 --- a/modules/objdetect/src/cascadedetect_convert.cpp +++ b/modules/objdetect/src/cascadedetect_convert.cpp @@ -42,6 +42,7 @@ /* Haar features calculation */ #include "precomp.hpp" +#include "cascadedetect.hpp" #include namespace cv @@ -111,13 +112,8 @@ struct HaarStageClassifier std::vector weaks; }; -static bool convert(const String& oldcascade, const String& newcascade) +bool convert(const FileNode& oldroot, FileStorage& newfs) { - FileStorage oldfs(oldcascade, FileStorage::READ); - if( !oldfs.isOpened() ) - return false; - FileNode oldroot = oldfs.getFirstTopLevelNode(); - FileNode sznode = oldroot[ICV_HAAR_SIZE_NAME]; if( sznode.empty() ) return false; @@ -194,10 +190,6 @@ static bool convert(const String& oldcascade, const String& newcascade) } } - FileStorage newfs(newcascade, FileStorage::WRITE); - if( !newfs.isOpened() ) - return false; - int maxWeakCount = 0, nfeatures = (int)features.size(); for( i = 0; i < nstages; i++ ) maxWeakCount = std::max(maxWeakCount, (int)stages[i].weaks.size()); @@ -225,12 +217,12 @@ static bool convert(const String& oldcascade, const String& newcascade) for( j = 0; j < nweaks; j++ ) { const HaarClassifier& c = stages[i].weaks[j]; - newfs << "{" << "internalNodes" << "["; + newfs << "{" << "internalNodes" << "[:"; int nnodes = (int)c.nodes.size(), nleaves = (int)c.leaves.size(); for( k = 0; k < nnodes; k++ ) newfs << c.nodes[k].left << c.nodes[k].right << c.nodes[k].f << c.nodes[k].threshold; - newfs << "]" << "leafValues" << "["; + newfs << "]" << "leafValues" << "[:"; for( k = 0; k < nleaves; k++ ) newfs << c.leaves[k]; newfs << "]" << "}"; @@ -249,7 +241,7 @@ static bool convert(const String& oldcascade, const String& newcascade) { if( j >= 2 && fabs(f.rect[j].weight) < FLT_EPSILON ) break; - newfs << "[" << f.rect[j].r.x << f.rect[j].r.y << + newfs << "[:" << f.rect[j].r.x << f.rect[j].r.y << f.rect[j].r.width << f.rect[j].r.height << f.rect[j].weight << "]"; } newfs << "]"; @@ -266,7 +258,13 @@ static bool convert(const String& oldcascade, const String& newcascade) bool CascadeClassifier::convert(const String& oldcascade, const String& newcascade) { - bool ok = haar_cvt::convert(oldcascade, newcascade); + FileStorage oldfs(oldcascade, FileStorage::READ); + FileStorage newfs(newcascade, FileStorage::WRITE); + if( !oldfs.isOpened() || !newfs.isOpened() ) + return false; + FileNode oldroot = oldfs.getFirstTopLevelNode(); + + bool ok = haar_cvt::convert(oldroot, newfs); if( !ok && newcascade.size() > 0 ) remove(newcascade.c_str()); return ok; diff --git a/modules/objdetect/src/haar.cpp b/modules/objdetect/src/haar.cpp index 9e20111..d0f09f8 100644 --- a/modules/objdetect/src/haar.cpp +++ b/modules/objdetect/src/haar.cpp @@ -102,26 +102,6 @@ typedef struct CvHidHaarClassifierCascade const int icv_object_win_border = 1; const float icv_stage_threshold_bias = 0.0001f; -static CvHaarClassifierCascade* -icvCreateHaarClassifierCascade( int stage_count ) -{ - CvHaarClassifierCascade* cascade = 0; - - int block_size = sizeof(*cascade) + stage_count*sizeof(*cascade->stage_classifier); - - if( stage_count <= 0 ) - CV_Error( CV_StsOutOfRange, "Number of stages should be positive" ); - - cascade = (CvHaarClassifierCascade*)cvAlloc( block_size ); - memset( cascade, 0, block_size ); - - cascade->stage_classifier = (CvHaarStageClassifier*)(cascade + 1); - cascade->flags = CV_HAAR_MAGIC_VAL; - cascade->count = stage_count; - - return cascade; -} - static void icvReleaseHidHaarClassifierCascade( CvHidHaarClassifierCascade** _cascade ) { @@ -1057,7 +1037,6 @@ public: } - CvSeq* cvHaarDetectObjectsForROC( const CvArr* _img, CvHaarClassifierCascade* cascade, CvMemStorage* storage, @@ -1373,6 +1352,32 @@ cvHaarDetectObjects( const CvArr* _img, } +CV_IMPL void +cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** _cascade ) +{ + if( _cascade && *_cascade ) + { + int i, j; + CvHaarClassifierCascade* cascade = *_cascade; + + for( i = 0; i < cascade->count; i++ ) + { + for( j = 0; j < cascade->stage_classifier[i].count; j++ ) + cvFree( &cascade->stage_classifier[i].classifier[j].haar_feature ); + cvFree( &cascade->stage_classifier[i].classifier ); + } + icvReleaseHidHaarClassifierCascade( &cascade->hid_cascade ); + cvFree( _cascade ); + } +} + +CV_IMPL CvHaarClassifierCascade* +cvLoadHaarClassifierCascade( const char*, CvSize ) +{ + return 0; +} + +#if 0 static CvHaarClassifierCascade* icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size ) @@ -1398,7 +1403,7 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size ) CV_Assert( count > 0 && count < CV_HAAR_STAGE_MAX); cascade->stage_classifier[i].count = count; cascade->stage_classifier[i].classifier = - (CvHaarClassifier*)cvAlloc( count*sizeof(cascade->stage_classifier[i].classifier[0])); + (CvHaarClassifier*)cvAlloc( count*sizeof(cascade->stage_classifier[i].classifier[0])); for( j = 0; j < count; j++ ) { @@ -1411,11 +1416,11 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size ) CV_Assert( classifier->count > 0 && classifier->count< CV_HAAR_STAGE_MAX); classifier->haar_feature = (CvHaarFeature*) cvAlloc( - classifier->count * ( sizeof( *classifier->haar_feature ) + - sizeof( *classifier->threshold ) + - sizeof( *classifier->left ) + - sizeof( *classifier->right ) ) + - (classifier->count + 1) * sizeof( *classifier->alpha ) ); + classifier->count * ( sizeof( *classifier->haar_feature ) + + sizeof( *classifier->threshold ) + + sizeof( *classifier->left ) + + sizeof( *classifier->right ) ) + + (classifier->count + 1) * sizeof( *classifier->alpha ) ); classifier->threshold = (float*) (classifier->haar_feature+classifier->count); classifier->left = (int*) (classifier->threshold + classifier->count); classifier->right = (int*) (classifier->left + classifier->count); @@ -1433,8 +1438,8 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size ) cv::Rect r; int band = 0; sscanf( stage, "%d%d%d%d%d%f%n", - &r.x, &r.y, &r.width, &r.height, &band, - &(classifier->haar_feature[l].rect[k].weight), &dl ); + &r.x, &r.y, &r.width, &r.height, &band, + &(classifier->haar_feature[l].rect[k].weight), &dl ); stage += dl; classifier->haar_feature[l].rect[k].r = cvRect(r); } @@ -1446,12 +1451,12 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size ) for( k = rects; k < CV_HAAR_FEATURE_MAX; k++ ) { memset( classifier->haar_feature[l].rect + k, 0, - sizeof(classifier->haar_feature[l].rect[k]) ); + sizeof(classifier->haar_feature[l].rect[k]) ); } sscanf( stage, "%f%d%d%n", &(classifier->threshold[l]), - &(classifier->left[l]), - &(classifier->right[l]), &dl ); + &(classifier->left[l]), + &(classifier->right[l]), &dl ); stage += dl; } for( l = 0; l <= classifier->count; l++ ) @@ -1557,27 +1562,6 @@ cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size ) return cascade; } - -CV_IMPL void -cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** _cascade ) -{ - if( _cascade && *_cascade ) - { - int i, j; - CvHaarClassifierCascade* cascade = *_cascade; - - for( i = 0; i < cascade->count; i++ ) - { - for( j = 0; j < cascade->stage_classifier[i].count; j++ ) - cvFree( &cascade->stage_classifier[i].classifier[j].haar_feature ); - cvFree( &cascade->stage_classifier[i].classifier ); - } - icvReleaseHidHaarClassifierCascade( &cascade->hid_cascade ); - cvFree( _cascade ); - } -} - - /****************************************************************************************\ * Persistence functions * \****************************************************************************************/ @@ -1605,6 +1589,26 @@ icvIsHaarClassifier( const void* struct_ptr ) return CV_IS_HAAR_CLASSIFIER( struct_ptr ); } +static CvHaarClassifierCascade* +icvCreateHaarClassifierCascade( int stage_count ) +{ + CvHaarClassifierCascade* cascade = 0; + + int block_size = sizeof(*cascade) + stage_count*sizeof(*cascade->stage_classifier); + + if( stage_count <= 0 ) + CV_Error( CV_StsOutOfRange, "Number of stages should be positive" ); + + cascade = (CvHaarClassifierCascade*)cvAlloc( block_size ); + memset( cascade, 0, block_size ); + + cascade->stage_classifier = (CvHaarStageClassifier*)(cascade + 1); + cascade->flags = CV_HAAR_MAGIC_VAL; + cascade->count = stage_count; + + return cascade; +} + static void* icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node ) { @@ -2124,4 +2128,6 @@ CvType haar_type( CV_TYPE_NAME_HAAR, icvIsHaarClassifier, icvReadHaarClassifier, icvWriteHaarClassifier, icvCloneHaarClassifier ); +#endif + /* End of file. */ diff --git a/modules/objdetect/src/hog.cpp b/modules/objdetect/src/hog.cpp index 963937c..e3e43bb 100644 --- a/modules/objdetect/src/hog.cpp +++ b/modules/objdetect/src/hog.cpp @@ -2118,62 +2118,6 @@ void HOGDescriptor::detectMultiScale(InputArray img, std::vector& foundLoc padding, scale0, finalThreshold, useMeanshiftGrouping); } -template struct RTTIImpl -{ -public: - static int isInstance(const void* ptr) - { - static _ClsName dummy; - static void* dummyp = &dummy; - union - { - const void* p; - const void** pp; - } a, b; - a.p = dummyp; - b.p = ptr; - return *a.pp == *b.pp; - } - static void release(void** dbptr) - { - if(dbptr && *dbptr) - { - delete (_ClsName*)*dbptr; - *dbptr = 0; - } - } - static void* read(CvFileStorage* fs, CvFileNode* n) - { - FileNode fn(fs, n); - _ClsName* obj = new _ClsName; - if(obj->read(fn)) - return obj; - delete obj; - return 0; - } - - static void write(CvFileStorage* _fs, const char* name, const void* ptr, CvAttrList) - { - if(ptr && _fs) - { - FileStorage fs(_fs, false); - ((const _ClsName*)ptr)->write(fs, String(name)); - } - } - - static void* clone(const void* ptr) - { - if(!ptr) - return 0; - return new _ClsName(*(const _ClsName*)ptr); - } -}; - -typedef RTTIImpl HOGRTTI; - -CvType hog_type( CV_TYPE_NAME_HOG_DESCRIPTOR, HOGRTTI::isInstance, - HOGRTTI::release, HOGRTTI::read, HOGRTTI::write, HOGRTTI::clone); - std::vector HOGDescriptor::getDefaultPeopleDetector() { static const float detector[] = { diff --git a/modules/objdetect/test/test_cascadeandhog.cpp b/modules/objdetect/test/test_cascadeandhog.cpp index f6a98cb..8164614 100644 --- a/modules/objdetect/test/test_cascadeandhog.cpp +++ b/modules/objdetect/test/test_cascadeandhog.cpp @@ -115,10 +115,11 @@ int CV_DetectorTest::prepareData( FileStorage& _fs ) // fn[TOTAL_NO_PAIR_E] >> eps.totalNoPair; // read detectors - if( fn[DETECTOR_NAMES].size() != 0 ) + FileNode fn_names = fn[DETECTOR_NAMES]; + if( fn_names.size() != 0 ) { - FileNodeIterator it = fn[DETECTOR_NAMES].begin(); - for( ; it != fn[DETECTOR_NAMES].end(); ) + FileNodeIterator it = fn_names.begin(), it_end = fn_names.end(); + for( ; it != it_end; ) { String _name; it >> _name; @@ -357,6 +358,26 @@ int CV_DetectorTest::validate( int detectorIdx, vector >& objects ) noPair += (int)count_if( map.begin(), map.end(), isZero ); totalNoPair += noPair; + /*if( noPair > cvRound(valRects.size()*eps.noPair)+1 ) + { + printf("Problem discovered: imageIdx = %d, cascade=%s: %d vs %d rects\n", imageIdx, detectorNames[detectorIdx].c_str(), (int)it->size(), (int)valRects.size()); + Mat image = images[imageIdx].clone(); + for( int k = 0; k < 2; k++ ) + { + const std::vector& imgObjects = k == 0 ? *it : valRects; + Scalar color = k == 0 ? Scalar(0, 255, 0) : Scalar(0, 0, 255); + for( size_t i = 0; i < imgObjects.size(); i++ ) + { + Rect r = imgObjects[i]; + rectangle(image, r, color, 3); + if( k == 1 ) + putText(image, format("%d", (int)i), Point(r.x + r.width/4, r.y + r.height*3/4), FONT_HERSHEY_PLAIN, 2, Scalar(0, 0, 255), 3); + } + } + imshow("results", image); + waitKey(); + }*/ + EXPECT_LE(noPair, cvRound(valRects.size()*eps.noPair)+1) << "detector " << detectorNames[detectorIdx] << " has overrated count of rectangles without pair on " << imageFilenames[imageIdx] << " image"; @@ -366,7 +387,7 @@ int CV_DetectorTest::validate( int detectorIdx, vector >& objects ) } EXPECT_LE(totalNoPair, cvRound(totalValRectCount*eps./*total*/noPair)+1) - << "detector " << detectorNames[detectorIdx] << " has overrated count of rectangles without pair on all images set"; + << "In total, detector " << detectorNames[detectorIdx] << " has overrated count of rectangles without pair on the whole image set"; if (::testing::Test::HasFailure()) return cvtest::TS::FAIL_BAD_ACCURACY; @@ -527,7 +548,7 @@ TEST(Objdetect_HOGDetectorReadWrite, regression) fs.open(tempfilename, FileStorage::READ); remove(tempfilename.c_str()); - FileNode n = fs["opencv_storage"]["myHOG"]; + FileNode n = fs["myHOG"]; ASSERT_NO_THROW(hog.read(n)); } diff --git a/modules/ts/include/opencv2/ts.hpp b/modules/ts/include/opencv2/ts.hpp index be08238..1fa7f28 100644 --- a/modules/ts/include/opencv2/ts.hpp +++ b/modules/ts/include/opencv2/ts.hpp @@ -317,7 +317,7 @@ protected: int test_case_count; // the total number of test cases // read test params - virtual int read_params( CvFileStorage* fs ); + virtual int read_params( const cv::FileStorage& fs ); // returns the number of tests or -1 if it is unknown a-priori virtual int get_test_case_count(); @@ -335,7 +335,7 @@ protected: virtual int update_progress( int progress, int test_case_idx, int count, double dt ); // finds test parameter - const CvFileNode* find_param( CvFileStorage* fs, const char* param_name ); + cv::FileNode find_param( const cv::FileStorage& fs, const char* param_name ); // name of the test (it is possible to locate a test by its name) string name; @@ -542,7 +542,7 @@ public: protected: - virtual int read_params( CvFileStorage* fs ) CV_OVERRIDE; + virtual int read_params( const cv::FileStorage& fs ) CV_OVERRIDE; virtual int prepare_test_case( int test_case_idx ) CV_OVERRIDE; virtual int validate_test_results( int test_case_idx ) CV_OVERRIDE; diff --git a/modules/ts/src/ts.cpp b/modules/ts/src/ts.cpp index 67f48d5..be23d5b 100644 --- a/modules/ts/src/ts.cpp +++ b/modules/ts/src/ts.cpp @@ -227,14 +227,14 @@ void BaseTest::clear() } -const CvFileNode* BaseTest::find_param( CvFileStorage* fs, const char* param_name ) +cv::FileNode BaseTest::find_param( const cv::FileStorage& fs, const char* param_name ) { - CvFileNode* node = cvGetFileNodeByName(fs, 0, get_name().c_str()); - return node ? cvGetFileNodeByName( fs, node, param_name ) : 0; + cv::FileNode node = fs[get_name()]; + return node[param_name]; } -int BaseTest::read_params( CvFileStorage* ) +int BaseTest::read_params( const cv::FileStorage& ) { return 0; } diff --git a/modules/ts/src/ts_arrtest.cpp b/modules/ts/src/ts_arrtest.cpp index 365cf15..c3df63a 100644 --- a/modules/ts/src/ts_arrtest.cpp +++ b/modules/ts/src/ts_arrtest.cpp @@ -80,15 +80,15 @@ void ArrayTest::clear() } -int ArrayTest::read_params( CvFileStorage* fs ) +int ArrayTest::read_params( const cv::FileStorage& fs ) { int code = BaseTest::read_params( fs ); if( code < 0 ) return code; - min_log_array_size = cvReadInt( find_param( fs, "min_log_array_size" ), min_log_array_size ); - max_log_array_size = cvReadInt( find_param( fs, "max_log_array_size" ), max_log_array_size ); - test_case_count = cvReadInt( find_param( fs, "test_case_count" ), test_case_count ); + read( find_param( fs, "min_log_array_size" ), min_log_array_size, min_log_array_size ); + read( find_param( fs, "max_log_array_size" ), max_log_array_size, max_log_array_size ); + read( find_param( fs, "test_case_count" ), test_case_count, test_case_count ); test_case_count = cvRound( test_case_count*ts->get_test_case_count_scale() ); min_log_array_size = clipInt( min_log_array_size, 0, 20 ); diff --git a/modules/video/test/test_camshift.cpp b/modules/video/test/test_camshift.cpp index 4aa0fa6..dc6275f 100644 --- a/modules/video/test/test_camshift.cpp +++ b/modules/video/test/test_camshift.cpp @@ -52,7 +52,7 @@ public: void clear(); protected: - int read_params( CvFileStorage* fs ); + int read_params( const cv::FileStorage& fs ); void run_func(void); int prepare_test_case( int test_case_idx ); int validate_test_results( int test_case_idx ); @@ -89,15 +89,15 @@ void CV_TrackBaseTest::clear() } -int CV_TrackBaseTest::read_params( CvFileStorage* fs ) +int CV_TrackBaseTest::read_params( const cv::FileStorage& fs ) { int code = cvtest::BaseTest::read_params( fs ); if( code < 0 ) return code; - test_case_count = cvReadInt( find_param( fs, "test_case_count" ), test_case_count ); - min_log_size = cvReadInt( find_param( fs, "min_log_size" ), min_log_size ); - max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size ); + read( find_param( fs, "test_case_count" ), test_case_count, test_case_count ); + read( find_param( fs, "min_log_size" ), min_log_size, min_log_size ); + read( find_param( fs, "max_log_size" ), max_log_size, max_log_size ); min_log_size = cvtest::clipInt( min_log_size, 1, 10 ); max_log_size = cvtest::clipInt( max_log_size, 1, 10 ); diff --git a/modules/video/test/test_optflowpyrlk.cpp b/modules/video/test/test_optflowpyrlk.cpp index 1f08270..e1fe5b1 100644 --- a/modules/video/test/test_optflowpyrlk.cpp +++ b/modules/video/test/test_optflowpyrlk.cpp @@ -71,83 +71,82 @@ void CV_OptFlowPyrLKTest::run( int ) int merr_i = 0, merr_j = 0, merr_k = 0, merr_nan = 0; char filename[1000]; - CvPoint2D32f *u = 0, *v = 0, *v2 = 0; - CvMat *_u = 0, *_v = 0, *_v2 = 0; - char* status = 0; + CvPoint2D32f *v = 0, *v2 = 0; + cv::Mat _u, _v, _v2; - IplImage imgI; - IplImage imgJ; - cv::Mat imgI2, imgJ2; + cv::Mat imgI, imgJ; int n = 0, i = 0; + for(;;) + { sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" ); - _u = (CvMat*)cvLoad( filename ); - if( !_u ) { - ts->printf( cvtest::TS::LOG, "could not read %s\n", filename ); - code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + FileStorage fs(filename, FileStorage::READ); + fs["points"] >> _u; + if( _u.empty() ) + { + ts->printf( cvtest::TS::LOG, "could not read %s\n", filename ); + code = cvtest::TS::FAIL_MISSING_TEST_DATA; + break; + } } sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" ); - _v = (CvMat*)cvLoad( filename ); - if( !_v ) { - ts->printf( cvtest::TS::LOG, "could not read %s\n", filename ); - code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + FileStorage fs(filename, FileStorage::READ); + fs["points"] >> _v; + if( _v.empty() ) + { + ts->printf( cvtest::TS::LOG, "could not read %s\n", filename ); + code = cvtest::TS::FAIL_MISSING_TEST_DATA; + break; + } } - if( _u->cols != 2 || CV_MAT_TYPE(_u->type) != CV_32F || - _v->cols != 2 || CV_MAT_TYPE(_v->type) != CV_32F || _v->rows != _u->rows ) + if( _u.cols != 2 || _u.type() != CV_32F || + _v.cols != 2 || _v.type() != CV_32F || + _v.rows != _u.rows ) { ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" ); code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + break; } - u = (CvPoint2D32f*)_u->data.fl; - v = (CvPoint2D32f*)_v->data.fl; - - /* allocate adidtional buffers */ - _v2 = cvCloneMat( _u ); - v2 = (CvPoint2D32f*)_v2->data.fl; - /* read first image */ sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" ); - imgI2 = cv::imread( filename, cv::IMREAD_UNCHANGED ); - imgI = cvIplImage(imgI2); + imgI = cv::imread( filename, cv::IMREAD_UNCHANGED ); - if( imgI2.empty() ) + if( imgI.empty() ) { ts->printf( cvtest::TS::LOG, "could not read %s\n", filename ); code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + break; } /* read second image */ sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" ); - imgJ2 = cv::imread( filename, cv::IMREAD_UNCHANGED ); - imgJ = cvIplImage(imgJ2); + imgJ = cv::imread( filename, cv::IMREAD_UNCHANGED ); - if( imgJ2.empty() ) + if( imgJ.empty() ) { ts->printf( cvtest::TS::LOG, "could not read %s\n", filename ); code = cvtest::TS::FAIL_MISSING_TEST_DATA; - goto _exit_; + break; } - n = _u->rows; - status = (char*)cvAlloc(n*sizeof(status[0])); + n = _u.rows; + std::vector status(n, (uchar)0); /* calculate flow */ - cvCalcOpticalFlowPyrLK( &imgI, &imgJ, 0, 0, u, v2, n, cvSize( 41, 41 ), - 4, status, 0, cvTermCriteria( CV_TERMCRIT_ITER| - CV_TERMCRIT_EPS, 30, 0.01f ), 0 ); + calcOpticalFlowPyrLK(imgI, imgJ, _u, _v2, status, cv::noArray(), Size( 41, 41 ), 4, + TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0.01f ), 0 ); + + v = (CvPoint2D32f*)_v.ptr(); + v2 = (CvPoint2D32f*)_v2.ptr(); /* compare results */ for( i = 0; i < n; i++ ) @@ -186,39 +185,36 @@ void CV_OptFlowPyrLKTest::run( int ) merr_k++; ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i ); code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; + break; } } } + if( i < n ) + break; if( pt_exceed > bad_points_max ) { ts->printf( cvtest::TS::LOG, "The number of poorly tracked points is too big (>=%d)\n", pt_exceed ); code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; + break; } if( max_err > 1 ) { ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i ); code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; + break; } if( merr_nan > 0 ) { ts->printf( cvtest::TS::LOG, "NAN tracking result with status != 0 (%d times)\n", merr_nan ); code = cvtest::TS::FAIL_BAD_ACCURACY; - goto _exit_; } -_exit_: - - cvFree( &status ); - cvReleaseMat( &_u ); - cvReleaseMat( &_v ); - cvReleaseMat( &_v2 ); + break; + } if( code < 0 ) ts->set_failed_test_info( code ); -- 2.7.4