1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
49 static inline char getCloseBrace(char c)
51 return c == '[' ? ']' : c == '(' ? ')' : c == '{' ? '}' : '\0';
55 template<typename _Tp> static void writeElems(std::ostream& out, const _Tp* data,
56 int nelems, int cn, char obrace, char cbrace)
58 typedef typename DataType<_Tp>::work_type _WTp;
60 for(int i = 0; i < nelems; i += cn)
64 out << (_WTp)data[i] << (i+1 < nelems ? ", " : "");
68 for(int j = 0; j < cn; j++)
69 out << (_WTp)data[i + j] << (j+1 < cn ? ", " : "");
70 out << cbrace << (i+cn < nelems ? ", " : "");
75 static void writeElems(std::ostream& out, const void* data, int nelems, int type, char brace)
77 int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
79 if(!brace || isspace(brace))
85 cbrace = getCloseBrace(brace);
87 writeElems(out, (const uchar*)data, nelems, cn, brace, cbrace);
88 else if(depth == CV_8S)
89 writeElems(out, (const schar*)data, nelems, cn, brace, cbrace);
90 else if(depth == CV_16U)
91 writeElems(out, (const ushort*)data, nelems, cn, brace, cbrace);
92 else if(depth == CV_16S)
93 writeElems(out, (const short*)data, nelems, cn, brace, cbrace);
94 else if(depth == CV_32S)
95 writeElems(out, (const int*)data, nelems, cn, brace, cbrace);
96 else if(depth == CV_32F)
98 std::streamsize pp = out.precision();
100 writeElems(out, (const float*)data, nelems, cn, brace, cbrace);
103 else if(depth == CV_64F)
105 std::streamsize pp = out.precision();
107 writeElems(out, (const double*)data, nelems, cn, brace, cbrace);
111 CV_Error(CV_StsUnsupportedFormat, "");
115 static void writeMat(std::ostream& out, const Mat& m, char rowsep, char elembrace, bool singleLine)
117 CV_Assert(m.dims <= 2);
120 char crowbrace = getCloseBrace(rowsep);
121 char orowbrace = crowbrace ? rowsep : '\0';
123 if( orowbrace || isspace(rowsep) )
126 for( int i = 0; i < m.rows; i++ )
131 writeElems(out, m.ptr(i), m.cols, type, elembrace);
133 out << crowbrace << (i+1 < m.rows ? ", " : "");
137 out << rowsep << (singleLine ? " " : "");
144 class MatlabFormatter : public Formatter
147 virtual ~MatlabFormatter() {}
148 void write(std::ostream& out, const Mat& m, const int*, int) const
151 writeMat(out, m, ';', ' ', m.rows == 1);
155 void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
157 writeElems(out, data, nelems, type, ' ');
161 class PythonFormatter : public Formatter
164 virtual ~PythonFormatter() {}
165 void write(std::ostream& out, const Mat& m, const int*, int) const
168 writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.rows*m.channels() == 1);
172 void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
174 writeElems(out, data, nelems, type, '[');
179 class NumpyFormatter : public Formatter
182 virtual ~NumpyFormatter() {}
183 void write(std::ostream& out, const Mat& m, const int*, int) const
185 static const char* numpyTypes[] =
187 "uint8", "int8", "uint16", "int16", "int32", "float32", "float64", "uint64"
190 writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.rows*m.channels() == 1);
191 out << "], type='" << numpyTypes[m.depth()] << "')";
194 void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
196 writeElems(out, data, nelems, type, '[');
201 class CSVFormatter : public Formatter
204 virtual ~CSVFormatter() {}
205 void write(std::ostream& out, const Mat& m, const int*, int) const
207 writeMat(out, m, ' ', ' ', m.rows*m.channels() == 1);
212 void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
214 writeElems(out, data, nelems, type, ' ');
219 class CFormatter : public Formatter
222 virtual ~CFormatter() {}
223 void write(std::ostream& out, const Mat& m, const int*, int) const
226 writeMat(out, m, ',', ' ', m.rows==1);
230 void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
232 writeElems(out, data, nelems, type, ' ');
237 static MatlabFormatter matlabFormatter;
238 static PythonFormatter pythonFormatter;
239 static NumpyFormatter numpyFormatter;
240 static CSVFormatter csvFormatter;
241 static CFormatter cFormatter;
243 static const Formatter* g_defaultFormatter0 = &matlabFormatter;
244 static const Formatter* g_defaultFormatter = &matlabFormatter;
246 static bool my_streq(const char* a, const char* b)
248 size_t i, alen = strlen(a), blen = strlen(b);
251 for( i = 0; i < alen; i++ )
252 if( a[i] != b[i] && a[i] - 32 != b[i] )
257 const Formatter* Formatter::get(const char* fmt)
259 if(!fmt || my_streq(fmt, ""))
260 return g_defaultFormatter;
261 if( my_streq(fmt, "MATLAB"))
262 return &matlabFormatter;
263 if( my_streq(fmt, "CSV"))
264 return &csvFormatter;
265 if( my_streq(fmt, "PYTHON"))
266 return &pythonFormatter;
267 if( my_streq(fmt, "NUMPY"))
268 return &numpyFormatter;
269 if( my_streq(fmt, "C"))
271 CV_Error(CV_StsBadArg, "Unknown formatter");
272 return g_defaultFormatter;
275 const Formatter* Formatter::setDefault(const Formatter* fmt)
277 const Formatter* prevFmt = g_defaultFormatter;
279 fmt = g_defaultFormatter0;
280 g_defaultFormatter = fmt;
284 Formatted::Formatted(const Mat& _m, const Formatter* _fmt,
285 const vector<int>& _params)
288 fmt = _fmt ? _fmt : Formatter::get();
289 std::copy(_params.begin(), _params.end(), back_inserter(params));
292 Formatted::Formatted(const Mat& _m, const Formatter* _fmt, const int* _params)
295 fmt = _fmt ? _fmt : Formatter::get();
299 int i, maxParams = 100;
300 for(i = 0; i < maxParams && _params[i] != 0; i+=2)
302 std::copy(_params, _params + i, back_inserter(params));