Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers out.cpp Source File

out.cpp

00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
00016 // Third party copyrights are property of their respective owners.
00017 //
00018 // Redistribution and use in source and binary forms, with or without modification,
00019 // are permitted provided that the following conditions are met:
00020 //
00021 //   * Redistribution's of source code must retain the above copyright notice,
00022 //     this list of conditions and the following disclaimer.
00023 //
00024 //   * Redistribution's in binary form must reproduce the above copyright notice,
00025 //     this list of conditions and the following disclaimer in the documentation
00026 //     and/or other materials provided with the distribution.
00027 //
00028 //   * The name of the copyright holders may not be used to endorse or promote products
00029 //     derived from this software without specific prior written permission.
00030 //
00031 // This software is provided by the copyright holders and contributors "as is" and
00032 // any express or implied warranties, including, but not limited to, the implied
00033 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00034 // In no event shall the Intel Corporation or contributors be liable for any direct,
00035 // indirect, incidental, special, exemplary, or consequential damages
00036 // (including, but not limited to, procurement of substitute goods or services;
00037 // loss of use, data, or profits; or business interruption) however caused
00038 // and on any theory of liability, whether in contract, strict liability,
00039 // or tort (including negligence or otherwise) arising in any way out of
00040 // the use of this software, even if advised of the possibility of such damage.
00041 //
00042 //M*/
00043 
00044 #include "precomp.hpp"
00045 
00046 namespace cv
00047 {
00048     class FormattedImpl : public Formatted
00049     {
00050         enum { STATE_PROLOGUE, STATE_EPILOGUE, STATE_INTERLUDE,
00051                STATE_ROW_OPEN, STATE_ROW_CLOSE, STATE_CN_OPEN, STATE_CN_CLOSE, STATE_VALUE, STATE_FINISHED,
00052                STATE_LINE_SEPARATOR, STATE_CN_SEPARATOR, STATE_VALUE_SEPARATOR };
00053         enum {BRACE_ROW_OPEN = 0, BRACE_ROW_CLOSE = 1, BRACE_ROW_SEP=2, BRACE_CN_OPEN=3, BRACE_CN_CLOSE=4 };
00054 
00055         char floatFormat[8];
00056         char buf[32];   // enough for double with precision up to 20
00057 
00058         Mat mtx;
00059         int mcn; // == mtx.channels()
00060         bool singleLine;
00061         bool alignOrder;    // true when cn first order
00062 
00063         int state;
00064         int row;
00065         int col;
00066         int cn;
00067 
00068         String prologue;
00069         String epilogue;
00070         char braces[5];
00071 
00072         void (FormattedImpl::*valueToStr)();
00073         void valueToStr8u()  { sprintf(buf, "%3d", (int)mtx.ptr<uchar>(row, col)[cn]); }
00074         void valueToStr8s()  { sprintf(buf, "%3d", (int)mtx.ptr<schar>(row, col)[cn]); }
00075         void valueToStr16u() { sprintf(buf, "%d", (int)mtx.ptr<ushort>(row, col)[cn]); }
00076         void valueToStr16s() { sprintf(buf, "%d", (int)mtx.ptr<short>(row, col)[cn]); }
00077         void valueToStr32s() { sprintf(buf, "%d", mtx.ptr<int>(row, col)[cn]); }
00078         void valueToStr32f() { sprintf(buf, floatFormat, mtx.ptr<float>(row, col)[cn]); }
00079         void valueToStr64f() { sprintf(buf, floatFormat, mtx.ptr<double>(row, col)[cn]); }
00080         void valueToStrOther() { buf[0] = 0; }
00081 
00082     public:
00083 
00084         FormattedImpl(String pl, String el, Mat m, char br[5], bool sLine, bool aOrder, int precision)
00085         {
00086             CV_Assert(m.dims <= 2);
00087 
00088             prologue = pl;
00089             epilogue = el;
00090             mtx = m;
00091             mcn = m.channels();
00092             memcpy(braces, br, 5);
00093             state = STATE_PROLOGUE;
00094             singleLine = sLine;
00095             alignOrder = aOrder;
00096             row = col = cn =0;
00097 
00098             if (precision < 0)
00099             {
00100                 floatFormat[0] = '%';
00101                 floatFormat[1] = 'a';
00102                 floatFormat[2] = 0;
00103             }
00104             else
00105             {
00106                 sprintf(floatFormat, "%%.%dg", std::min(precision, 20));
00107             }
00108 
00109             switch(mtx.depth())
00110             {
00111                 case CV_8U:  valueToStr = &FormattedImpl::valueToStr8u; break;
00112                 case CV_8S:  valueToStr = &FormattedImpl::valueToStr8s; break;
00113                 case CV_16U: valueToStr = &FormattedImpl::valueToStr16u; break;
00114                 case CV_16S: valueToStr = &FormattedImpl::valueToStr16s; break;
00115                 case CV_32S: valueToStr = &FormattedImpl::valueToStr32s; break;
00116                 case CV_32F: valueToStr = &FormattedImpl::valueToStr32f; break;
00117                 case CV_64F: valueToStr = &FormattedImpl::valueToStr64f; break;
00118                 default:     valueToStr = &FormattedImpl::valueToStrOther; break;
00119             }
00120         }
00121 
00122         void reset()
00123         {
00124             state = STATE_PROLOGUE;
00125         }
00126 
00127         const char* next()
00128         {
00129             switch(state)
00130             {
00131                 case STATE_PROLOGUE:
00132                     row = 0;
00133                     if (mtx.empty())
00134                         state = STATE_EPILOGUE;
00135                     else if (alignOrder)
00136                         state = STATE_INTERLUDE;
00137                     else
00138                         state = STATE_ROW_OPEN;
00139                     return prologue.c_str();
00140                 case STATE_INTERLUDE:
00141                     state = STATE_ROW_OPEN;
00142                     if (row >= mtx.rows)
00143                     {
00144                         if (++cn >= mcn)
00145                         {
00146                             state = STATE_EPILOGUE;
00147                             buf[0] = 0;
00148                             return buf;
00149                         }
00150                         else
00151                             row = 0;
00152                         sprintf(buf, "\n(:, :, %d) = \n", cn+1);
00153                         return buf;
00154                     }
00155                     sprintf(buf, "(:, :, %d) = \n", cn+1);
00156                     return buf;
00157                 case STATE_EPILOGUE:
00158                     state = STATE_FINISHED;
00159                     return epilogue.c_str();
00160                 case STATE_ROW_OPEN:
00161                     col = 0;
00162                     state = STATE_CN_OPEN;
00163                     {
00164                         size_t pos = 0;
00165                         if (row > 0)
00166                             while(pos < prologue.size() && pos < sizeof(buf) - 2)
00167                                 buf[pos++] = ' ';
00168                         if (braces[BRACE_ROW_OPEN])
00169                             buf[pos++] = braces[BRACE_ROW_OPEN];
00170                         if(!pos)
00171                             return next();
00172                         buf[pos] = 0;
00173                     }
00174                     return buf;
00175                 case STATE_ROW_CLOSE:
00176                     state = STATE_LINE_SEPARATOR;
00177                     ++row;
00178                     if (braces[BRACE_ROW_CLOSE])
00179                     {
00180                         buf[0] = braces[BRACE_ROW_CLOSE];
00181                         buf[1] = row < mtx.rows ? ',' : '\0';
00182                         buf[2] = 0;
00183                         return buf;
00184                     }
00185                     else if(braces[BRACE_ROW_SEP] && row < mtx.rows)
00186                     {
00187                         buf[0] = braces[BRACE_ROW_SEP];
00188                         buf[1] = 0;
00189                         return buf;
00190                     }
00191                     return next();
00192                 case STATE_CN_OPEN:
00193                     state = STATE_VALUE;
00194                     if (!alignOrder)
00195                         cn = 0;
00196                     if (mcn > 1 && braces[BRACE_CN_OPEN])
00197                     {
00198                         buf[0] = braces[BRACE_CN_OPEN];
00199                         buf[1] = 0;
00200                         return buf;
00201                     }
00202                     return next();
00203                 case STATE_CN_CLOSE:
00204                     ++col;
00205                     if (col >= mtx.cols)
00206                         state = STATE_ROW_CLOSE;
00207                     else
00208                         state = STATE_CN_SEPARATOR;
00209                     if (mcn > 1 && braces[BRACE_CN_CLOSE])
00210                     {
00211                         buf[0] = braces[BRACE_CN_CLOSE];
00212                         buf[1] = 0;
00213                         return buf;
00214                     }
00215                     return next();
00216                 case STATE_VALUE:
00217                     (this->*valueToStr)();
00218                     state = STATE_CN_CLOSE;
00219                     if (alignOrder)
00220                         return buf;
00221                     if (++cn < mcn)
00222                         state = STATE_VALUE_SEPARATOR;
00223                     return buf;
00224                 case STATE_FINISHED:
00225                     return 0;
00226                 case STATE_LINE_SEPARATOR:
00227                     if (row >= mtx.rows)
00228                     {
00229                         if (alignOrder)
00230                             state = STATE_INTERLUDE;
00231                         else
00232                             state = STATE_EPILOGUE;
00233                         return next();
00234                     }
00235                     state = STATE_ROW_OPEN;
00236                     buf[0] = singleLine ? ' ' : '\n';
00237                     buf[1] = 0;
00238                     return buf;
00239                 case STATE_CN_SEPARATOR:
00240                     state = STATE_CN_OPEN;
00241                     buf[0] = ',';
00242                     buf[1] = ' ';
00243                     buf[2] = 0;
00244                     return buf;
00245                 case STATE_VALUE_SEPARATOR:
00246                     state = STATE_VALUE;
00247                     buf[0] = ',';
00248                     buf[1] = ' ';
00249                     buf[2] = 0;
00250                     return buf;
00251             }
00252             return 0;
00253         }
00254     };
00255 
00256     class FormatterBase : public Formatter
00257     {
00258     public:
00259         FormatterBase() : prec32f(8), prec64f(16), multiline(true) {}
00260 
00261         void set32fPrecision(int p)
00262         {
00263             prec32f = p;
00264         }
00265 
00266         void set64fPrecision(int p)
00267         {
00268             prec64f = p;
00269         }
00270 
00271         void setMultiline(bool ml)
00272         {
00273             multiline = ml;
00274         }
00275 
00276     protected:
00277         int prec32f;
00278         int prec64f;
00279         int multiline;
00280     };
00281 
00282     class DefaultFormatter : public FormatterBase
00283     {
00284     public:
00285 
00286         Ptr<Formatted> format(const Mat& mtx) const
00287         {
00288             char braces[5] = {'\0', '\0', ';', '\0', '\0'};
00289             return makePtr<FormattedImpl>("[", "]", mtx, &*braces,
00290                 mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
00291         }
00292     };
00293 
00294     class MatlabFormatter : public FormatterBase
00295     {
00296     public:
00297 
00298         Ptr<Formatted> format(const Mat& mtx) const
00299         {
00300             char braces[5] = {'\0', '\0', ';', '\0', '\0'};
00301             return makePtr<FormattedImpl>("", "", mtx, &*braces,
00302                 mtx.rows == 1 || !multiline, true, mtx.depth() == CV_64F ? prec64f : prec32f );
00303         }
00304     };
00305 
00306     class PythonFormatter : public FormatterBase
00307     {
00308     public:
00309 
00310         Ptr<Formatted> format(const Mat& mtx) const
00311         {
00312             char braces[5] = {'[', ']', ',', '[', ']'};
00313             if (mtx.cols == 1)
00314                 braces[0] = braces[1] = '\0';
00315             return makePtr<FormattedImpl>("[", "]", mtx, &*braces,
00316                 mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
00317         }
00318     };
00319 
00320     class NumpyFormatter : public FormatterBase
00321     {
00322     public:
00323 
00324         Ptr<Formatted> format(const Mat& mtx) const
00325         {
00326             static const char* numpyTypes[] =
00327             {
00328                 "uint8", "int8", "uint16", "int16", "int32", "float32", "float64", "uint64"
00329             };
00330             char braces[5] = {'[', ']', ',', '[', ']'};
00331             if (mtx.cols == 1)
00332                 braces[0] = braces[1] = '\0';
00333             return makePtr<FormattedImpl>("array([",
00334                 cv::format("], dtype='%s')", numpyTypes[mtx.depth()]), mtx, &*braces,
00335                 mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
00336         }
00337     };
00338 
00339     class CSVFormatter : public FormatterBase
00340     {
00341     public:
00342 
00343         Ptr<Formatted> format(const Mat& mtx) const
00344         {
00345             char braces[5] = {'\0', '\0', '\0', '\0', '\0'};
00346             return makePtr<FormattedImpl>(String(),
00347                 mtx.rows > 1 ? String("\n") : String(), mtx, &*braces,
00348                 mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
00349         }
00350     };
00351 
00352     class CFormatter : public FormatterBase
00353     {
00354     public:
00355 
00356         Ptr<Formatted> format(const Mat& mtx) const
00357         {
00358             char braces[5] = {'\0', '\0', ',', '\0', '\0'};
00359             return makePtr<FormattedImpl>("{", "}", mtx, &*braces,
00360                 mtx.rows == 1 || !multiline, false, mtx.depth() == CV_64F ? prec64f : prec32f );
00361         }
00362     };
00363 
00364     Formatted::~Formatted() {}
00365     Formatter::~Formatter() {}
00366 
00367     Ptr<Formatter> Formatter::get(int fmt)
00368     {
00369         switch(fmt)
00370         {
00371             case FMT_DEFAULT:
00372                 return makePtr<DefaultFormatter>();
00373             case FMT_MATLAB:
00374                 return makePtr<MatlabFormatter>();
00375             case FMT_CSV:
00376                 return makePtr<CSVFormatter>();
00377             case FMT_PYTHON:
00378                 return makePtr<PythonFormatter>();
00379             case FMT_NUMPY:
00380                 return makePtr<NumpyFormatter>();
00381             case FMT_C:
00382                 return makePtr<CFormatter>();
00383         }
00384         return makePtr<DefaultFormatter>();
00385     }
00386 } // cv
00387