snark
serialization.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_IMAGING_CVMAT_SERIALIZATION_H_
20 #define SNARK_IMAGING_CVMAT_SERIALIZATION_H_
21 
22 #include <iostream>
23 #include <string>
24 #include <boost/date_time/posix_time/posix_time.hpp>
25 #include <opencv2/core/core.hpp>
26 #include <comma/base/types.h>
27 #include <comma/csv/binary.h>
28 #include <comma/visiting/traits.h>
29 
30 
31 namespace snark{ namespace cv_mat {
32 
33 class serialization
34 {
35  public:
37  struct header
38  {
39  boost::posix_time::ptime timestamp;
40  comma::uint32 rows;
41  comma::uint32 cols;
42  comma::uint32 type;
43  comma::uint32 size;
44 
45  header();
46  header( const cv::Mat& m );
47  header( const std::pair< boost::posix_time::ptime, cv::Mat >& p );
48  };
49 
51  struct options
52  {
53  std::string fields;
54  comma::uint32 rows;
55  comma::uint32 cols;
56  std::string type;
57  bool no_header;
58  bool header_only;
59 
60  options() : no_header( false ), header_only( false ) {}
61  header get_header() const;
62  static std::string usage();
63  static std::string type_usage();
64  };
65 
67  serialization();
68 
70  serialization( const std::string& fields, const comma::csv::format& format, bool headerOnly = false, const header& default_header = header() );
71 
73  serialization( const options& options );
74 
76  std::size_t put( const std::pair< boost::posix_time::ptime, cv::Mat >& m, char* buf ) const;
77 
80  std::size_t get( std::pair< boost::posix_time::ptime, cv::Mat >& m, const char* buf ) const;
81 
83  std::pair< boost::posix_time::ptime, cv::Mat > get( const char* buf ) const;
84 
86  header get_header( const char* buf ) const;
87 
89  static const std::string& usage();
90 
92  std::size_t size( const cv::Mat& m ) const;
93 
95  std::size_t size( const std::pair< boost::posix_time::ptime, cv::Mat >& m ) const;
96 
98  std::pair< boost::posix_time::ptime, cv::Mat > read( std::istream& is );
99 
101  void write( std::ostream& os, const std::pair< boost::posix_time::ptime, cv::Mat >& m );
102 
103  private:
104  boost::scoped_ptr< comma::csv::binary< header > > m_binary;
105  std::vector< char > m_buffer;
106  bool m_headerOnly;
107  header m_header;
108 };
109 
110 } } // namespace snark{ namespace cv_mat {
111 
112 namespace comma { namespace visiting {
113 
114 template <> struct traits< snark::cv_mat::serialization::header >
115 {
116  template < typename K, typename V >
117  static void visit( const K&, snark::cv_mat::serialization::header& h, V& v )
118  {
119  v.apply( "t", h.timestamp );
120  v.apply( "rows", h.rows );
121  v.apply( "cols", h.cols );
122  v.apply( "type", h.type );
123  v.apply( "size", h.size );
124  }
125 
126  template < typename K, typename V >
127  static void visit( const K&, const snark::cv_mat::serialization::header& h, V& v )
128  {
129  v.apply( "t", h.timestamp );
130  v.apply( "rows", h.rows );
131  v.apply( "cols", h.cols );
132  v.apply( "type", h.type );
133  v.apply( "size", h.size );
134  }
135 };
136 
137 template <> struct traits< snark::cv_mat::serialization::options >
138 {
139  template < typename K, typename V >
140  static void visit( const K&, snark::cv_mat::serialization::options& h, V& v )
141  {
142  v.apply( "fields", h.fields );
143  v.apply( "rows", h.rows );
144  v.apply( "cols", h.cols );
145  v.apply( "type", h.type );
146  v.apply( "no-header", h.no_header );
147  v.apply( "header-only", h.header_only );
148  }
149 
150  template < typename K, typename V >
151  static void visit( const K&, const snark::cv_mat::serialization::options& h, V& v )
152  {
153  v.apply( "fields", h.fields );
154  v.apply( "rows", h.rows );
155  v.apply( "cols", h.cols );
156  v.apply( "type", h.type );
157  v.apply( "no-header", h.no_header );
158  v.apply( "header-only", h.header_only );
159  }
160 };
161 
162 } } // namespace comma { namespace visiting {
163 
164 #endif // SNARK_IMAGING_CVMAT_SERIALIZATION_H_