out.cpp 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*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.
14 15
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
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
// 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"
45

46
namespace cv
47
{
48
    class FormattedImpl CV_FINAL : public Formatted
49
    {
KayKwon's avatar
KayKwon committed
50 51
        enum { STATE_PROLOGUE, STATE_EPILOGUE, STATE_INTERLUDE,
               STATE_ROW_OPEN, STATE_ROW_CLOSE, STATE_CN_OPEN, STATE_CN_CLOSE, STATE_VALUE, STATE_FINISHED,
52 53
               STATE_LINE_SEPARATOR, STATE_CN_SEPARATOR, STATE_VALUE_SEPARATOR };
        enum {BRACE_ROW_OPEN = 0, BRACE_ROW_CLOSE = 1, BRACE_ROW_SEP=2, BRACE_CN_OPEN=3, BRACE_CN_CLOSE=4 };
54

55 56
        char floatFormat[8];
        char buf[32];   // enough for double with precision up to 20
57

58
        Mat mtx;
59
        int mcn; // == mtx.channels()
60
        bool singleLine;
KayKwon's avatar
KayKwon committed
61
        bool alignOrder;    // true when cn first order
62

63 64 65 66
        int state;
        int row;
        int col;
        int cn;
67

68 69
        String prologue;
        String epilogue;
70
        char braces[5];
71

72 73 74 75 76 77 78 79
        void (FormattedImpl::*valueToStr)();
        void valueToStr8u()  { sprintf(buf, "%3d", (int)mtx.ptr<uchar>(row, col)[cn]); }
        void valueToStr8s()  { sprintf(buf, "%3d", (int)mtx.ptr<schar>(row, col)[cn]); }
        void valueToStr16u() { sprintf(buf, "%d", (int)mtx.ptr<ushort>(row, col)[cn]); }
        void valueToStr16s() { sprintf(buf, "%d", (int)mtx.ptr<short>(row, col)[cn]); }
        void valueToStr32s() { sprintf(buf, "%d", mtx.ptr<int>(row, col)[cn]); }
        void valueToStr32f() { sprintf(buf, floatFormat, mtx.ptr<float>(row, col)[cn]); }
        void valueToStr64f() { sprintf(buf, floatFormat, mtx.ptr<double>(row, col)[cn]); }
80
        void valueToStr16f() { sprintf(buf, floatFormat, (float)mtx.ptr<float16_t>(row, col)[cn]); }
81
        void valueToStrOther() { buf[0] = 0; }
82

83
    public:
84

85
        FormattedImpl(String pl, String el, Mat m, char br[5], bool sLine, bool aOrder, int precision)
86
        {
KayKwon's avatar
KayKwon committed
87 88
            CV_Assert(m.dims <= 2);

89 90 91 92 93 94 95
            prologue = pl;
            epilogue = el;
            mtx = m;
            mcn = m.channels();
            memcpy(braces, br, 5);
            state = STATE_PROLOGUE;
            singleLine = sLine;
KayKwon's avatar
KayKwon committed
96 97
            alignOrder = aOrder;
            row = col = cn =0;
98 99 100 101 102 103 104 105 106

            if (precision < 0)
            {
                floatFormat[0] = '%';
                floatFormat[1] = 'a';
                floatFormat[2] = 0;
            }
            else
            {
107
                cv_snprintf(floatFormat, sizeof(floatFormat), "%%.%dg", std::min(precision, 20));
108 109 110 111 112 113 114 115 116 117 118
            }

            switch(mtx.depth())
            {
                case CV_8U:  valueToStr = &FormattedImpl::valueToStr8u; break;
                case CV_8S:  valueToStr = &FormattedImpl::valueToStr8s; break;
                case CV_16U: valueToStr = &FormattedImpl::valueToStr16u; break;
                case CV_16S: valueToStr = &FormattedImpl::valueToStr16s; break;
                case CV_32S: valueToStr = &FormattedImpl::valueToStr32s; break;
                case CV_32F: valueToStr = &FormattedImpl::valueToStr32f; break;
                case CV_64F: valueToStr = &FormattedImpl::valueToStr64f; break;
119 120
                default:     CV_Assert(mtx.depth() == CV_16F);
                             valueToStr = &FormattedImpl::valueToStr16f;
121 122
            }
        }
123

124
        void reset() CV_OVERRIDE
125 126 127
        {
            state = STATE_PROLOGUE;
        }
128

129
        const char* next() CV_OVERRIDE
130
        {
131 132 133 134 135 136
            switch(state)
            {
                case STATE_PROLOGUE:
                    row = 0;
                    if (mtx.empty())
                        state = STATE_EPILOGUE;
KayKwon's avatar
KayKwon committed
137 138
                    else if (alignOrder)
                        state = STATE_INTERLUDE;
139 140 141
                    else
                        state = STATE_ROW_OPEN;
                    return prologue.c_str();
KayKwon's avatar
KayKwon committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                case STATE_INTERLUDE:
                    state = STATE_ROW_OPEN;
                    if (row >= mtx.rows)
                    {
                        if (++cn >= mcn)
                        {
                            state = STATE_EPILOGUE;
                            buf[0] = 0;
                            return buf;
                        }
                        else
                            row = 0;
                        sprintf(buf, "\n(:, :, %d) = \n", cn+1);
                        return buf;
                    }
                    sprintf(buf, "(:, :, %d) = \n", cn+1);
                    return buf;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
                case STATE_EPILOGUE:
                    state = STATE_FINISHED;
                    return epilogue.c_str();
                case STATE_ROW_OPEN:
                    col = 0;
                    state = STATE_CN_OPEN;
                    {
                        size_t pos = 0;
                        if (row > 0)
                            while(pos < prologue.size() && pos < sizeof(buf) - 2)
                                buf[pos++] = ' ';
                        if (braces[BRACE_ROW_OPEN])
                            buf[pos++] = braces[BRACE_ROW_OPEN];
                        if(!pos)
                            return next();
                        buf[pos] = 0;
                    }
                    return buf;
                case STATE_ROW_CLOSE:
                    state = STATE_LINE_SEPARATOR;
                    ++row;
                    if (braces[BRACE_ROW_CLOSE])
                    {
                        buf[0] = braces[BRACE_ROW_CLOSE];
                        buf[1] = row < mtx.rows ? ',' : '\0';
                        buf[2] = 0;
                        return buf;
                    }
                    else if(braces[BRACE_ROW_SEP] && row < mtx.rows)
                    {
                        buf[0] = braces[BRACE_ROW_SEP];
                        buf[1] = 0;
                        return buf;
                    }
                    return next();
                case STATE_CN_OPEN:
                    state = STATE_VALUE;
KayKwon's avatar
KayKwon committed
196 197
                    if (!alignOrder)
                        cn = 0;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
                    if (mcn > 1 && braces[BRACE_CN_OPEN])
                    {
                        buf[0] = braces[BRACE_CN_OPEN];
                        buf[1] = 0;
                        return buf;
                    }
                    return next();
                case STATE_CN_CLOSE:
                    ++col;
                    if (col >= mtx.cols)
                        state = STATE_ROW_CLOSE;
                    else
                        state = STATE_CN_SEPARATOR;
                    if (mcn > 1 && braces[BRACE_CN_CLOSE])
                    {
                        buf[0] = braces[BRACE_CN_CLOSE];
                        buf[1] = 0;
                        return buf;
                    }
                    return next();
                case STATE_VALUE:
                    (this->*valueToStr)();
KayKwon's avatar
KayKwon committed
220 221 222 223
                    state = STATE_CN_CLOSE;
                    if (alignOrder)
                        return buf;
                    if (++cn < mcn)
224 225 226 227 228 229 230
                        state = STATE_VALUE_SEPARATOR;
                    return buf;
                case STATE_FINISHED:
                    return 0;
                case STATE_LINE_SEPARATOR:
                    if (row >= mtx.rows)
                    {
KayKwon's avatar
KayKwon committed
231 232 233 234
                        if (alignOrder)
                            state = STATE_INTERLUDE;
                        else
                            state = STATE_EPILOGUE;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                        return next();
                    }
                    state = STATE_ROW_OPEN;
                    buf[0] = singleLine ? ' ' : '\n';
                    buf[1] = 0;
                    return buf;
                case STATE_CN_SEPARATOR:
                    state = STATE_CN_OPEN;
                    buf[0] = ',';
                    buf[1] = ' ';
                    buf[2] = 0;
                    return buf;
                case STATE_VALUE_SEPARATOR:
                    state = STATE_VALUE;
                    buf[0] = ',';
                    buf[1] = ' ';
                    buf[2] = 0;
                    return buf;
            }
            return 0;
255
        }
256
    };
257

258
    class FormatterBase : public Formatter
259
    {
260
    public:
261 262 263 264 265 266
        FormatterBase() : prec16f(4), prec32f(8), prec64f(16), multiline(true) {}

        void set16fPrecision(int p) CV_OVERRIDE
        {
            prec16f = p;
        }
267

268
        void set32fPrecision(int p) CV_OVERRIDE
269 270 271
        {
            prec32f = p;
        }
272

273
        void set64fPrecision(int p) CV_OVERRIDE
274 275 276
        {
            prec64f = p;
        }
277

278
        void setMultiline(bool ml) CV_OVERRIDE
279 280 281
        {
            multiline = ml;
        }
282

283
    protected:
284
        int prec16f;
285 286 287 288
        int prec32f;
        int prec64f;
        int multiline;
    };
289

290
    class DefaultFormatter CV_FINAL : public FormatterBase
KayKwon's avatar
KayKwon committed
291 292 293
    {
    public:

294
        Ptr<Formatted> format(const Mat& mtx) const CV_OVERRIDE
KayKwon's avatar
KayKwon committed
295 296
        {
            char braces[5] = {'\0', '\0', ';', '\0', '\0'};
297
            return makePtr<FormattedImpl>("[", "]", mtx, &*braces,
KayKwon's avatar
KayKwon committed
298 299 300
                mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
        }
    };
301

302
    class MatlabFormatter CV_FINAL : public FormatterBase
303
    {
304 305
    public:

306
        Ptr<Formatted> format(const Mat& mtx) const CV_OVERRIDE
307
        {
308
            char braces[5] = {'\0', '\0', ';', '\0', '\0'};
309
            return makePtr<FormattedImpl>("", "", mtx, &*braces,
KayKwon's avatar
KayKwon committed
310
                mtx.rows == 1 || !multiline, true, mtx.depth() == CV_64F ? prec64f : prec32f );
311 312
        }
    };
313

314
    class PythonFormatter CV_FINAL : public FormatterBase
315
    {
316
    public:
317

318
        Ptr<Formatted> format(const Mat& mtx) const CV_OVERRIDE
319
        {
320
            char braces[5] = {'[', ']', ',', '[', ']'};
321 322
            if (mtx.cols == 1)
                braces[0] = braces[1] = '\0';
323
            return makePtr<FormattedImpl>("[", "]", mtx, &*braces,
KayKwon's avatar
KayKwon committed
324
                mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
325 326
        }
    };
327

328
    class NumpyFormatter CV_FINAL : public FormatterBase
329
    {
330
    public:
331

332
        Ptr<Formatted> format(const Mat& mtx) const CV_OVERRIDE
333 334 335
        {
            static const char* numpyTypes[] =
            {
336
                "uint8", "int8", "uint16", "int16", "int32", "float32", "float64", "float16"
337
            };
338
            char braces[5] = {'[', ']', ',', '[', ']'};
339 340
            if (mtx.cols == 1)
                braces[0] = braces[1] = '\0';
341
            return makePtr<FormattedImpl>("array([",
342
                cv::format("], dtype='%s')", numpyTypes[mtx.depth()]), mtx, &*braces,
KayKwon's avatar
KayKwon committed
343
                mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
344 345
        }
    };
346

347
    class CSVFormatter CV_FINAL : public FormatterBase
348
    {
349
    public:
350

351
        Ptr<Formatted> format(const Mat& mtx) const CV_OVERRIDE
352 353
        {
            char braces[5] = {'\0', '\0', '\0', '\0', '\0'};
354 355
            return makePtr<FormattedImpl>(String(),
                mtx.rows > 1 ? String("\n") : String(), mtx, &*braces,
KayKwon's avatar
KayKwon committed
356
                mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
357 358 359
        }
    };

360
    class CFormatter CV_FINAL : public FormatterBase
361
    {
362
    public:
363

364
        Ptr<Formatted> format(const Mat& mtx) const CV_OVERRIDE
365 366
        {
            char braces[5] = {'\0', '\0', ',', '\0', '\0'};
367
            return makePtr<FormattedImpl>("{", "}", mtx, &*braces,
KayKwon's avatar
KayKwon committed
368
                mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
369 370
        }
    };
371

372 373
    Formatted::~Formatted() {}
    Formatter::~Formatter() {}
374

375
    Ptr<Formatter> Formatter::get(Formatter::FormatType fmt)
376
    {
377 378
        switch(fmt)
        {
KayKwon's avatar
KayKwon committed
379 380
            case FMT_DEFAULT:
                return makePtr<DefaultFormatter>();
381
            case FMT_MATLAB:
Roman Donchenko's avatar
Roman Donchenko committed
382
                return makePtr<MatlabFormatter>();
383
            case FMT_CSV:
Roman Donchenko's avatar
Roman Donchenko committed
384
                return makePtr<CSVFormatter>();
385
            case FMT_PYTHON:
Roman Donchenko's avatar
Roman Donchenko committed
386
                return makePtr<PythonFormatter>();
387
            case FMT_NUMPY:
Roman Donchenko's avatar
Roman Donchenko committed
388
                return makePtr<NumpyFormatter>();
389
            case FMT_C:
Roman Donchenko's avatar
Roman Donchenko committed
390
                return makePtr<CFormatter>();
391
        }
KayKwon's avatar
KayKwon committed
392
        return makePtr<DefaultFormatter>();
393
    }
394
} // cv