out.cpp 9.05 KB
Newer Older
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                          License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
#include <iterator>
45 46 47 48

namespace cv
{

49
static inline char getCloseBrace(char c)
50
{
51
    return c == '[' ? ']' : c == '(' ? ')' : c == '{' ? '}' : '\0';
52 53 54
}


55 56
template<typename _Tp> static void writeElems(std::ostream& out, const _Tp* data,
                                              int nelems, int cn, char obrace, char cbrace)
57
{
58 59 60 61 62 63 64 65 66 67 68 69 70 71
    typedef typename DataType<_Tp>::work_type _WTp;
    nelems *= cn;
    for(int i = 0; i < nelems; i += cn)
    {
        if(cn == 1)
        {
            out << (_WTp)data[i] << (i+1 < nelems ? ", " : "");
            continue;
        }
        out << obrace;
        for(int j = 0; j < cn; j++)
            out << (_WTp)data[i + j] << (j+1 < cn ? ", " : "");
        out << cbrace << (i+cn < nelems ? ", " : "");
    }
72
}
73 74 75


static void writeElems(std::ostream& out, const void* data, int nelems, int type, char brace)
76
{
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
    char cbrace = ' ';
    if(!brace || isspace(brace))
    {
        nelems *= cn;
        cn = 1;
    }
    else
        cbrace = getCloseBrace(brace);
    if(depth == CV_8U)
        writeElems(out, (const uchar*)data, nelems, cn, brace, cbrace);
    else if(depth == CV_8S)
        writeElems(out, (const schar*)data, nelems, cn, brace, cbrace);
    else if(depth == CV_16U)
        writeElems(out, (const ushort*)data, nelems, cn, brace, cbrace);
    else if(depth == CV_16S)
        writeElems(out, (const short*)data, nelems, cn, brace, cbrace);
    else if(depth == CV_32S)
        writeElems(out, (const int*)data, nelems, cn, brace, cbrace);
    else if(depth == CV_32F)
    {
        std::streamsize pp = out.precision();
        out.precision(8);
        writeElems(out, (const float*)data, nelems, cn, brace, cbrace);
        out.precision(pp);
    }
    else if(depth == CV_64F)
    {
        std::streamsize pp = out.precision();
        out.precision(16);
        writeElems(out, (const double*)data, nelems, cn, brace, cbrace);
        out.precision(pp);
    }
    else
        CV_Error(CV_StsUnsupportedFormat, "");
112 113
}

114 115

static void writeMat(std::ostream& out, const Mat& m, char rowsep, char elembrace, bool singleLine)
116
{
117 118
    CV_Assert(m.dims <= 2);
    int type = m.type();
119

120 121
    char crowbrace = getCloseBrace(rowsep);
    char orowbrace = crowbrace ? rowsep : '\0';
122

123 124
    if( orowbrace || isspace(rowsep) )
        rowsep = '\0';
125

126 127 128 129
    for( int i = 0; i < m.rows; i++ )
    {
        if(orowbrace)
            out << orowbrace;
130 131
        if( m.data )
            writeElems(out, m.ptr(i), m.cols, type, elembrace);
132 133 134 135 136 137 138 139 140 141
        if(orowbrace)
            out << crowbrace << (i+1 < m.rows ? ", " : "");
        if(i+1 < m.rows)
        {
            if(rowsep)
                out << rowsep << (singleLine ? " " : "");
            if(!singleLine)
                out << "\n  ";
        }
    }
142 143
}

144
class MatlabFormatter : public Formatter
145
{
146 147 148 149 150 151 152 153
public:
    virtual ~MatlabFormatter() {}
    void write(std::ostream& out, const Mat& m, const int*, int) const
    {
        out << "[";
        writeMat(out, m, ';', ' ', m.cols == 1);
        out << "]";
    }
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
    {
        writeElems(out, data, nelems, type, ' ');
    }
};

class PythonFormatter : public Formatter
{
public:
    virtual ~PythonFormatter() {}
    void write(std::ostream& out, const Mat& m, const int*, int) const
    {
        out << "[";
        writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.cols*m.channels() == 1);
        out << "]";
    }
171

172 173 174 175 176 177
    void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
    {
        writeElems(out, data, nelems, type, '[');
    }
};

178

179
class NumpyFormatter : public Formatter
180
{
181 182 183 184 185 186 187 188 189 190 191 192
public:
    virtual ~NumpyFormatter() {}
    void write(std::ostream& out, const Mat& m, const int*, int) const
    {
        static const char* numpyTypes[] =
        {
            "uint8", "int8", "uint16", "int16", "int32", "float32", "float64", "uint64"
        };
        out << "array([";
        writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.cols*m.channels() == 1);
        out << "], type='" << numpyTypes[m.depth()] << "')";
    }
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
    {
        writeElems(out, data, nelems, type, '[');
    }
};


class CSVFormatter : public Formatter
{
public:
    virtual ~CSVFormatter() {}
    void write(std::ostream& out, const Mat& m, const int*, int) const
    {
        writeMat(out, m, ' ', ' ', m.cols*m.channels() == 1);
        if(m.rows > 1)
            out << "\n";
    }
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
    {
        writeElems(out, data, nelems, type, ' ');
    }
};


class CFormatter : public Formatter
{
public:
    virtual ~CFormatter() {}
    void write(std::ostream& out, const Mat& m, const int*, int) const
    {
        out << "{";
        writeMat(out, m, ',', ' ', m.cols==1);
        out << "}";
    }
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const
    {
        writeElems(out, data, nelems, type, ' ');
    }
};


static MatlabFormatter matlabFormatter;
static PythonFormatter pythonFormatter;
static NumpyFormatter numpyFormatter;
static CSVFormatter csvFormatter;
static CFormatter cFormatter;

static const Formatter* g_defaultFormatter0 = &matlabFormatter;
static const Formatter* g_defaultFormatter = &matlabFormatter;

246
static bool my_streq(const char* a, const char* b)
247 248 249 250 251 252 253 254
{
    size_t i, alen = strlen(a), blen = strlen(b);
    if( alen != blen )
        return false;
    for( i = 0; i < alen; i++ )
        if( a[i] != b[i] && a[i] - 32 != b[i] )
            return false;
    return true;
255 256
}

257
const Formatter* Formatter::get(const char* fmt)
258
{
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if(!fmt || my_streq(fmt, ""))
        return g_defaultFormatter;
    if( my_streq(fmt, "MATLAB"))
        return &matlabFormatter;
    if( my_streq(fmt, "CSV"))
        return &csvFormatter;
    if( my_streq(fmt, "PYTHON"))
        return &pythonFormatter;
    if( my_streq(fmt, "NUMPY"))
        return &numpyFormatter;
    if( my_streq(fmt, "C"))
        return &cFormatter;
    CV_Error(CV_StsBadArg, "Unknown formatter");
    return g_defaultFormatter;
}
274

275 276 277 278 279 280 281 282
const Formatter* Formatter::setDefault(const Formatter* fmt)
{
    const Formatter* prevFmt = g_defaultFormatter;
    if(!fmt)
        fmt = g_defaultFormatter0;
    g_defaultFormatter = fmt;
    return prevFmt;
}
283

284 285 286 287 288 289 290
Formatted::Formatted(const Mat& _m, const Formatter* _fmt,
                     const vector<int>& _params)
{
    mtx = _m;
    fmt = _fmt ? _fmt : Formatter::get();
    std::copy(_params.begin(), _params.end(), back_inserter(params));
}
291

292 293 294 295
Formatted::Formatted(const Mat& _m, const Formatter* _fmt, const int* _params)
{
    mtx = _m;
    fmt = _fmt ? _fmt : Formatter::get();
296

297
    if( _params )
298
    {
299 300 301 302
        int i, maxParams = 100;
        for(i = 0; i < maxParams && _params[i] != 0; i+=2)
            ;
        std::copy(_params, _params + i, back_inserter(params));
303 304 305 306
    }
}

}