]> rtime.felk.cvut.cz Git - opencv.git/commitdiff
added C++ video capturing API to highgui; modified image.cpp example; cleaned up...
authorvp153 <vp153@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Tue, 4 Aug 2009 15:23:20 +0000 (15:23 +0000)
committervp153 <vp153@73c94f0f-984f-4a5f-82bc-2d8db8d8ee08>
Tue, 4 Aug 2009 15:23:20 +0000 (15:23 +0000)
git-svn-id: https://code.ros.org/svn/opencv/trunk@2014 73c94f0f-984f-4a5f-82bc-2d8db8d8ee08

opencv/include/opencv/cv.hpp
opencv/include/opencv/cxcore.hpp
opencv/include/opencv/cxmat.hpp
opencv/include/opencv/cxmisc.h
opencv/include/opencv/cxoperations.hpp
opencv/include/opencv/highgui.hpp
opencv/samples/c/image.cpp
opencv/src/cv/cvsamplers.cpp
opencv/src/cxcore/_cxcore.h
opencv/src/highgui/cvcap.cpp

index 0d1f02ad69828c16eb1868dd2fdf9904f5020d5e..f6370b425091deb037f97aaa0d6dd8eeba8db4e1 100644 (file)
@@ -399,6 +399,9 @@ CV_EXPORTS void calcOpticalFlowFarneback( const Mat& prev0, const Mat& next0,
                                Mat& flow0, double pyr_scale, int levels, int winsize,
                                int iterations, int poly_n, double poly_sigma, int flags );
     
+
+template<> inline void Ptr<CvHistogram>::delete_obj()
+{ cvReleaseHist(&obj); }
     
 CV_EXPORTS void calcHist( const Vector<Mat>& images, const Vector<int>& channels,
                           const Mat& mask, MatND& hist, const Vector<int>& histSize,
index 883837e02564b9807e256ba9efc867c99ed21a39..f02d041dd7c8a914bce24dc0b5a104aa4df36fc4 100644 (file)
@@ -859,6 +859,10 @@ public:
     template<typename _Tp> const _Tp* ptr(int y=0) const;
     template<typename _Tp> _Tp& at(int y, int x);
     template<typename _Tp> const _Tp& at(int y, int x) const;
+    template<typename _Tp> MatIterator_<_Tp> begin();
+    template<typename _Tp> MatIterator_<_Tp> end();
+    template<typename _Tp> MatConstIterator_<_Tp> begin() const;
+    template<typename _Tp> MatConstIterator_<_Tp> end() const;
 
     enum { MAGIC_VAL=0x42FF0000, AUTO_STEP=0, CONTINUOUS_FLAG=CV_MAT_CONT_FLAG };
 
@@ -1278,7 +1282,6 @@ public:
     MatConstIterator_ operator ++(int);
     Point pos() const;
 
-protected:
     const Mat_<_Tp>* m;
     _Tp* ptr;
     _Tp* sliceEnd;
@@ -1332,7 +1335,6 @@ public:
     operator Vector<_Tp>() const;
     Vector<_Tp> operator *() const;
 
-protected:
     Vector<_Tp>* vec;
     int idx;
 };
@@ -1948,9 +1950,6 @@ public:
 
 template<typename _Tp> class SeqIterator;
 
-template<> inline void Ptr<CvMemStorage>::delete_obj()
-{ cvReleaseMemStorage(&obj); }
-
 typedef Ptr<CvMemStorage> MemStorage;
 
 template<typename _Tp> class CV_EXPORTS Seq
index a2f42dfa7a268d21cd160247442b1c79cff24128..7e181fb7409e00352a7c8c89ac6a59138f2fe781 100644 (file)
-/*M///////////////////////////////////////////////////////////////////////////////////////\r
-//\r
-//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
-//\r
-//  By downloading, copying, installing or using the software you agree to this license.\r
-//  If you do not agree to this license, do not download, install,\r
-//  copy or use the software.\r
-//\r
-//\r
-//                           License Agreement\r
-//                For Open Source Computer Vision Library\r
-//\r
-// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
-// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
-// Third party copyrights are property of their respective owners.\r
-//\r
-// Redistribution and use in source and binary forms, with or without modification,\r
-// are permitted provided that the following conditions are met:\r
-//\r
-//   * Redistribution's of source code must retain the above copyright notice,\r
-//     this list of conditions and the following disclaimer.\r
-//\r
-//   * Redistribution's in binary form must reproduce the above copyright notice,\r
-//     this list of conditions and the following disclaimer in the documentation\r
-//     and/or other materials provided with the distribution.\r
-//\r
-//   * The name of the copyright holders may not be used to endorse or promote products\r
-//     derived from this software without specific prior written permission.\r
-//\r
-// This software is provided by the copyright holders and contributors "as is" and\r
-// any express or implied warranties, including, but not limited to, the implied\r
-// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
-// In no event shall the Intel Corporation or contributors be liable for any direct,\r
-// indirect, incidental, special, exemplary, or consequential damages\r
-// (including, but not limited to, procurement of substitute goods or services;\r
-// loss of use, data, or profits; or business interruption) however caused\r
-// and on any theory of liability, whether in contract, strict liability,\r
-// or tort (including negligence or otherwise) arising in any way out of\r
-// the use of this software, even if advised of the possibility of such damage.\r
-//\r
-//M*/\r
-\r
-#ifndef _OPENCV_CORE_MAT_OPERATIONS_H_\r
-#define _OPENCV_CORE_MAT_OPERATIONS_H_\r
-\r
-#ifndef SKIP_INCLUDES\r
-#include <limits.h>\r
-#endif // SKIP_INCLUDES\r
-\r
-#ifdef __cplusplus\r
-\r
-namespace cv\r
-{\r
-\r
-//////////////////////////////// Mat ////////////////////////////////\r
-\r
-inline Mat::Mat()\r
-    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0) {}\r
-\r
-inline Mat::Mat(int _rows, int _cols, int _type)\r
-    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)\r
-{\r
-    if( _rows > 0 && _cols > 0 )\r
-        create( _rows, _cols, _type );\r
-}\r
-\r
-inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s)\r
-    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0),\r
-    datastart(0), dataend(0)\r
-{\r
-    create(_rows, _cols, _type);\r
-    *this = _s;\r
-}\r
-\r
-inline Mat::Mat(Size _size, int _type)\r
-    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0),\r
-    datastart(0), dataend(0)\r
-{\r
-    if( _size.height > 0 && _size.width > 0 )\r
-        create( _size.height, _size.width, _type );\r
-}\r
-\r
-inline Mat::Mat(const Mat& m)\r
-    : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data),\r
-    refcount(m.refcount), datastart(m.datastart), dataend(m.dataend)\r
-{\r
-    if( refcount )\r
-        ++*refcount;\r
-}\r
-\r
-inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)\r
-    : flags(MAGIC_VAL + (_type & TYPE_MASK)), rows(_rows), cols(_cols),\r
-    step(_step), data((uchar*)_data), refcount(0),\r
-    datastart((uchar*)_data), dataend((uchar*)_data)\r
-{\r
-    size_t minstep = cols*elemSize();\r
-    if( step == AUTO_STEP )\r
-    {\r
-        step = minstep;\r
-        flags |= CONTINUOUS_FLAG;\r
-    }\r
-    else\r
-    {\r
-        if( rows == 1 ) step = minstep;\r
-        CV_DbgAssert( step >= minstep );\r
-        flags |= step == minstep ? CONTINUOUS_FLAG : 0;\r
-    }\r
-    dataend += step*(rows-1) + minstep;\r
-}\r
-\r
-inline Mat::Mat(Size _size, int _type, void* _data, size_t _step)\r
-    : flags(MAGIC_VAL + (_type & TYPE_MASK)), rows(_size.height), cols(_size.width),\r
-    step(_step), data((uchar*)_data), refcount(0),\r
-    datastart((uchar*)_data), dataend((uchar*)_data)\r
-{\r
-    size_t minstep = cols*elemSize();\r
-    if( step == AUTO_STEP )\r
-    {\r
-        step = minstep;\r
-        flags |= CONTINUOUS_FLAG;\r
-    }\r
-    else\r
-    {\r
-        if( rows == 1 ) step = minstep;\r
-        CV_DbgAssert( step >= minstep );\r
-        flags |= step == minstep ? CONTINUOUS_FLAG : 0;\r
-    }\r
-    dataend += step*(rows-1) + minstep;\r
-}\r
-\r
-inline Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange)\r
-{\r
-    flags = m.flags;\r
-    step = m.step; refcount = m.refcount;\r
-    data = m.data; datastart = m.datastart; dataend = m.dataend;\r
-\r
-    if( rowRange == Range::all() )\r
-        rows = m.rows;\r
-    else\r
-    {\r
-        CV_Assert( 0 <= rowRange.start && rowRange.start <= rowRange.end && rowRange.end <= m.rows );\r
-        rows = rowRange.size();\r
-        data += step*rowRange.start;\r
-    }\r
-\r
-    if( colRange == Range::all() )\r
-        cols = m.cols;\r
-    else\r
-    {\r
-        CV_Assert( 0 <= colRange.start && colRange.start <= colRange.end && colRange.end <= m.cols );\r
-        cols = colRange.size();\r
-        data += colRange.start*elemSize();\r
-        flags &= cols < m.cols ? ~CONTINUOUS_FLAG : -1;\r
-    }\r
-\r
-    if( rows == 1 )\r
-        flags |= CONTINUOUS_FLAG;\r
-\r
-    if( refcount )\r
-        ++*refcount;\r
-    if( rows <= 0 || cols <= 0 )\r
-        rows = cols = 0;\r
-}\r
-\r
-inline Mat::Mat(const Mat& m, const Rect& roi)\r
-    : flags(m.flags), rows(roi.height), cols(roi.width),\r
-    step(m.step), data(m.data + roi.y*step), refcount(m.refcount),\r
-    datastart(m.datastart), dataend(m.dataend)\r
-{\r
-    flags &= roi.width < m.cols ? ~CONTINUOUS_FLAG : -1;\r
-    data += roi.x*elemSize();\r
-    CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols &&\r
-        0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows );\r
-    if( refcount )\r
-        ++*refcount;\r
-    if( rows <= 0 || cols <= 0 )\r
-        rows = cols = 0;\r
-}\r
-\r
-inline Mat::Mat(const CvMat* m, bool copyData)\r
-    : flags(MAGIC_VAL + (m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG))),\r
-    rows(m->rows), cols(m->cols), step(m->step), data(m->data.ptr), refcount(0),\r
-    datastart(m->data.ptr), dataend(m->data.ptr)\r
-{\r
-    if( step == 0 )\r
-        step = cols*elemSize();\r
-    size_t minstep = cols*elemSize();\r
-    dataend += step*(rows-1) + minstep;\r
-    if( copyData )\r
-    {\r
-        data = datastart = dataend = 0;\r
-        Mat(m->rows, m->cols, m->type, m->data.ptr, m->step).copyTo(*this);\r
-    }\r
-}\r
-\r
-inline Mat::~Mat()\r
-{\r
-    release();\r
-}\r
-\r
-inline Mat& Mat::operator = (const Mat& m)\r
-{\r
-    if( this != &m )\r
-    {\r
-        if( m.refcount )\r
-            ++*m.refcount;\r
-        release();\r
-        flags = m.flags;\r
-        rows = m.rows; cols = m.cols;\r
-        step = m.step; data = m.data;\r
-        datastart = m.datastart; dataend = m.dataend;\r
-        refcount = m.refcount;\r
-    }\r
-    return *this;\r
-}\r
-\r
-inline Mat Mat::row(int y) const { return Mat(*this, Range(y, y+1), Range::all()); }\r
-inline Mat Mat::col(int x) const { return Mat(*this, Range::all(), Range(x, x+1)); }\r
-inline Mat Mat::rowRange(int startrow, int endrow) const\r
-    { return Mat(*this, Range(startrow, endrow), Range::all()); }\r
-inline Mat Mat::rowRange(const Range& r) const\r
-    { return Mat(*this, r, Range::all()); }\r
-inline Mat Mat::colRange(int startcol, int endcol) const\r
-    { return Mat(*this, Range::all(), Range(startcol, endcol)); }\r
-inline Mat Mat::colRange(const Range& r) const\r
-    { return Mat(*this, Range::all(), r); }\r
-\r
-inline Mat Mat::diag(int d) const\r
-{\r
-    Mat m = *this;\r
-    size_t esz = elemSize();\r
-    int len;\r
-\r
-    if( d >= 0 )\r
-    {\r
-        len = std::min(cols - d, rows);\r
-        m.data += esz*d;\r
-    }\r
-    else\r
-    {\r
-        len = std::min(rows + d, cols);\r
-        m.data -= step*d;\r
-    }\r
-    CV_DbgAssert( len > 0 );\r
-    m.rows = len;\r
-    m.cols = 1;\r
-    m.step += esz;\r
-    if( m.rows > 1 )\r
-        m.flags &= ~CONTINUOUS_FLAG;\r
-    else\r
-        m.flags |= CONTINUOUS_FLAG;\r
-    return m;\r
-}\r
-\r
-inline Mat Mat::diag(const Mat& d)\r
-{\r
-    Mat m(d.rows, d.rows, d.type(), Scalar(0)), md = m.diag();\r
-    d.copyTo(md);\r
-    return m;\r
-}\r
-\r
-inline Mat Mat::clone() const\r
-{\r
-    Mat m;\r
-    copyTo(m);\r
-    return m;\r
-}\r
-\r
-inline void Mat::assignTo( Mat& m, int type ) const\r
-{\r
-    if( type < 0 )\r
-        m = *this;\r
-    else\r
-        convertTo(m, type);\r
-}\r
-\r
-inline void Mat::create(int _rows, int _cols, int _type)\r
-{\r
-    _type &= TYPE_MASK;\r
-    if( rows == _rows && cols == _cols && type() == _type && data )\r
-        return;\r
-    if( data )\r
-        release();\r
-    CV_DbgAssert( _rows >= 0 && _cols >= 0 );\r
-    if( _rows > 0 && _cols > 0 )\r
-    {\r
-        flags = MAGIC_VAL + CONTINUOUS_FLAG + _type;\r
-        rows = _rows;\r
-        cols = _cols;\r
-        step = elemSize()*cols;\r
-        int64 _nettosize = (int64)step*rows;\r
-        size_t nettosize = (size_t)_nettosize;\r
-        if( _nettosize != (int64)nettosize )\r
-            CV_Error(CV_StsNoMem, "Too big buffer is allocated");\r
-        size_t datasize = alignSize(nettosize, (int)sizeof(*refcount));\r
-        datastart = data = (uchar*)fastMalloc(datasize + sizeof(*refcount));\r
-        dataend = data + nettosize;\r
-        refcount = (int*)(data + datasize);\r
-        *refcount = 1;\r
-    }\r
-}\r
-\r
-inline void Mat::create(Size _size, int _type)\r
-{\r
-    create(_size.height, _size.width, _type);\r
-}\r
-\r
-inline void Mat::addref()\r
-{ if( refcount ) ++*refcount; }\r
-\r
-inline void Mat::release()\r
-{\r
-    if( refcount && --*refcount == 0 )\r
-        fastFree(datastart);\r
-    data = datastart = dataend = 0;\r
-    step = rows = cols = 0;\r
-    refcount = 0;\r
-}\r
-\r
-inline void Mat::locateROI( Size& wholeSize, Point& ofs ) const\r
-{\r
-    size_t esz = elemSize(), minstep;\r
-    ptrdiff_t delta1 = data - datastart, delta2 = dataend - datastart;\r
-    CV_DbgAssert( step > 0 );\r
-    if( delta1 == 0 )\r
-        ofs.x = ofs.y = 0;\r
-    else\r
-    {\r
-        ofs.y = (int)(delta1/step);\r
-        ofs.x = (int)((delta1 - step*ofs.y)/esz);\r
-        CV_DbgAssert( data == datastart + ofs.y*step + ofs.x*esz );\r
-    }\r
-    minstep = (ofs.x + cols)*esz;\r
-    wholeSize.height = (int)((delta2 - minstep)/step + 1);\r
-    wholeSize.height = std::max(wholeSize.height, ofs.y + rows);\r
-    wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);\r
-    wholeSize.width = std::max(wholeSize.width, ofs.x + cols);\r
-}\r
-\r
-inline Mat& Mat::adjustROI( int dtop, int dbottom, int dleft, int dright )\r
-{\r
-    Size wholeSize; Point ofs;\r
-    size_t esz = elemSize();\r
-    locateROI( wholeSize, ofs );\r
-    int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);\r
-    int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);\r
-    data += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;\r
-    rows = row2 - row1; cols = col2 - col1;\r
-    if( esz*cols == step || rows == 1 )\r
-        flags |= CONTINUOUS_FLAG;\r
-    else\r
-        flags &= ~CONTINUOUS_FLAG;\r
-    return *this;\r
-}\r
-\r
-inline Mat Mat::operator()( Range rowRange, Range colRange ) const\r
-{\r
-    return Mat(*this, rowRange, colRange);\r
-}\r
-\r
-inline Mat Mat::operator()( const Rect& roi ) const\r
-{ return Mat(*this, roi); }\r
-\r
-inline Mat::operator CvMat() const\r
-{\r
-    CvMat m = cvMat(rows, cols, type(), data);\r
-    m.step = (int)step;\r
-    m.type = (m.type & ~CONTINUOUS_FLAG) | (flags & CONTINUOUS_FLAG);\r
-    return m;\r
-}\r
-\r
-inline Mat::operator IplImage() const\r
-{\r
-    IplImage img;\r
-    cvInitImageHeader(&img, size(), cvIplDepth(flags), channels());\r
-    cvSetData(&img, data, (int)step);\r
-    return img;\r
-}\r
-\r
-inline bool Mat::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; }\r
-inline size_t Mat::elemSize() const { return CV_ELEM_SIZE(flags); }\r
-inline size_t Mat::elemSize1() const { return CV_ELEM_SIZE1(flags); }\r
-inline int Mat::type() const { return CV_MAT_TYPE(flags); }\r
-inline int Mat::depth() const { return CV_MAT_DEPTH(flags); }\r
-inline int Mat::channels() const { return CV_MAT_CN(flags); }\r
-inline size_t Mat::step1() const { return step/elemSize1(); }\r
-inline Size Mat::size() const { return Size(cols, rows); }\r
-inline bool Mat::empty() const { return data == 0; }\r
-\r
-inline uchar* Mat::ptr(int y)\r
-{\r
-    CV_DbgAssert( (unsigned)y < (unsigned)rows );\r
-    return data + step*y;\r
-}\r
-\r
-inline const uchar* Mat::ptr(int y) const\r
-{\r
-    CV_DbgAssert( (unsigned)y < (unsigned)rows );\r
-    return data + step*y;\r
-}\r
-\r
-template<typename _Tp> inline _Tp* Mat::ptr(int y)\r
-{\r
-    CV_DbgAssert( (unsigned)y < (unsigned)rows );\r
-    return (_Tp*)(data + step*y);\r
-}\r
-\r
-template<typename _Tp> inline const _Tp* Mat::ptr(int y) const\r
-{\r
-    CV_DbgAssert( (unsigned)y < (unsigned)rows );\r
-    return (const _Tp*)(data + step*y);\r
-}\r
-\r
-template<typename _Tp> inline _Tp& Mat::at(int y, int x)\r
-{\r
-    CV_DbgAssert( (unsigned)y < (unsigned)rows && (unsigned)x < (unsigned)cols &&\r
-        sizeof(_Tp) == elemSize() );\r
-    return ((_Tp*)(data + step*y))[x];\r
-}\r
-\r
-template<typename _Tp> inline const _Tp& Mat::at(int y, int x) const\r
-{\r
-    CV_DbgAssert( (unsigned)y < (unsigned)rows && (unsigned)x < (unsigned)cols &&\r
-        sizeof(_Tp) == elemSize() );\r
-    return ((_Tp*)(data + step*y))[x];\r
-}\r
-\r
-static inline void swap( Mat& a, Mat& b )\r
-{\r
-    std::swap( a.flags, b.flags );\r
-    std::swap( a.rows, b.rows ); std::swap( a.cols, b.cols );\r
-    std::swap( a.step, b.step ); std::swap( a.data, b.data );\r
-    std::swap( a.datastart, b.datastart );\r
-    std::swap( a.dataend, b.dataend );\r
-    std::swap( a.refcount, b.refcount );\r
-}\r
-\r
-inline SVD::SVD() {}\r
-inline SVD::SVD( const Mat& m, int flags ) { operator ()(m, flags); }\r
-inline void SVD::solveZ( const Mat& m, Mat& dst )\r
-{\r
-    SVD svd(m);\r
-    svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);\r
-}\r
-\r
-///////////////////////////////// Mat_<_Tp> ////////////////////////////////////\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_() :\r
-    Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; }\r
-    \r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols) :\r
-    Mat(_rows, _cols, DataType<_Tp>::type) {}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value) :\r
-    Mat(_rows, _cols, DataType<_Tp>::type) { *this = value; }\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _size) :\r
-    Mat(_size.height, _size.width, DataType<_Tp>::type) {}\r
-    \r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _size, const _Tp& value) :\r
-    Mat(_size.height, _size.width, DataType<_Tp>::type) { *this = value; }\r
-    \r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat& m) : Mat()\r
-{ flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; *this = m; }\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m) : Mat(m) {}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t _step)\r
-    : Mat(_rows, _cols, DataType<_Tp>::type, _data, _step) {}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Range& rowRange, const Range& colRange)\r
-    : Mat(m, rowRange, colRange) {}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)\r
-    : Mat(m, roi) {}\r
-\r
-template<typename _Tp> template<int n> inline Mat_<_Tp>::Mat_(const Vec<_Tp, n>& vec)\r
-    : Mat(n, 1, DataType<_Tp>::type)\r
-{\r
-    _Tp* d = (_Tp*)data;\r
-    for( int i = 0; i < n; i++ )\r
-        d[i] = vec[i];\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(const Vector<_Tp>& vec)\r
-    : Mat((int)vec.size(), 1, DataType<_Tp>::type, (void*)&vec[0])\r
-{}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)\r
-{\r
-    if( DataType<_Tp>::type == m.type() )\r
-    {\r
-        Mat::operator = (m);\r
-        return *this;\r
-    }\r
-    if( DataType<_Tp>::depth == m.depth() )\r
-    {\r
-        return (*this = m.reshape(DataType<_Tp>::channels));\r
-    }\r
-    CV_DbgAssert(DataType<_Tp>::channels == m.channels());\r
-    m.convertTo(*this, type());\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)\r
-{\r
-    Mat::operator=(m);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)\r
-{\r
-    Mat::operator=(Scalar(s));\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline void Mat_<_Tp>::create(int _rows, int _cols)\r
-{\r
-    Mat::create(_rows, _cols, DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline void Mat_<_Tp>::create(Size _size)\r
-{\r
-    Mat::create(_size, DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const\r
-{ return Mat_<_Tp>(Mat::cross(m)); }\r
-\r
-template<typename _Tp> template<typename T2> inline Mat_<_Tp>::operator Mat_<T2>() const\r
-{ return Mat_<T2>(*this); }\r
-\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::row(int y) const\r
-{ return Mat_(*this, Range(y, y+1), Range::all()); }\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::col(int x) const\r
-{ return Mat_(*this, Range::all(), Range(x, x+1)); }\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::diag(int d) const\r
-{ return Mat_(Mat::diag(d)); }\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::clone() const\r
-{ return Mat_(Mat::clone()); }\r
-\r
-template<typename _Tp> inline size_t Mat_<_Tp>::elemSize() const\r
-{\r
-    CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );\r
-    return sizeof(_Tp);\r
-}\r
-\r
-template<typename _Tp> inline size_t Mat_<_Tp>::elemSize1() const\r
-{\r
-    CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp)/DataType<_Tp>::channels );\r
-    return sizeof(_Tp)/DataType<_Tp>::channels;\r
-}\r
-template<typename _Tp> inline int Mat_<_Tp>::type() const\r
-{\r
-    CV_DbgAssert( Mat::type() == DataType<_Tp>::type );\r
-    return DataType<_Tp>::type;\r
-}\r
-template<typename _Tp> inline int Mat_<_Tp>::depth() const\r
-{\r
-    CV_DbgAssert( Mat::depth() == DataType<_Tp>::depth );\r
-    return DataType<_Tp>::depth;\r
-}\r
-template<typename _Tp> inline int Mat_<_Tp>::channels() const\r
-{\r
-    CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );\r
-    return DataType<_Tp>::channels;\r
-}\r
-template<typename _Tp> inline size_t Mat_<_Tp>::stepT() const { return step/elemSize(); }\r
-template<typename _Tp> inline size_t Mat_<_Tp>::step1() const { return step/elemSize1(); }\r
-\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::reshape(int _rows) const\r
-{ return Mat_<_Tp>(Mat::reshape(0,_rows)); }\r
-\r
-template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )\r
-{ return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright));  }\r
-\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range& rowRange, const Range& colRange ) const\r
-{ return Mat_<_Tp>(*this, rowRange, colRange); }\r
-\r
-template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const\r
-{ return Mat_<_Tp>(roi); }\r
-\r
-template<typename _Tp> inline _Tp* Mat_<_Tp>::operator [](int y)\r
-{ return (_Tp*)ptr(y); }\r
-template<typename _Tp> inline const _Tp* Mat_<_Tp>::operator [](int y) const\r
-{ return (const _Tp*)ptr(y); }\r
-\r
-template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int row, int col)\r
-{\r
-    CV_DbgAssert( (unsigned)row < (unsigned)rows && (unsigned)col < (unsigned)cols );\r
-    return ((_Tp*)(data + step*row))[col];\r
-}\r
-\r
-template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int row, int col) const\r
-{\r
-    CV_DbgAssert( (unsigned)row < (unsigned)rows && (unsigned)col < (unsigned)cols );\r
-    return ((const _Tp*)(data + step*row))[col];\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::operator Vector<_Tp>() const\r
-{\r
-    CV_Assert( rows == 1 || cols == 1 );\r
-    return isContinuous() ? Vector<_Tp>((size_t)(rows + cols - 1), (_Tp*)data) :\r
-        (Vector<_Tp>)((Mat_<_Tp>)this->t());\r
-}\r
-\r
-template<typename T1, typename T2, typename Op> inline void\r
-process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op )\r
-{\r
-    int y, x, rows = m1.rows, cols = m1.cols;\r
-    int c1 = m1.channels(), c2 = m2.channels();\r
-\r
-    CV_DbgAssert( m1.size() == m2.size() );\r
-\r
-    for( y = 0; y < rows; y++ )\r
-    {\r
-        const T1* src = m1[y];\r
-        T2* dst = m2[y];\r
-\r
-        for( x = 0; x < cols; x++ )\r
-            dst[x] = op(src[x]);\r
-    }\r
-}\r
-\r
-template<typename T1, typename T2, typename T3, typename Op> inline void\r
-process( const Mat_<T1>& m1, const Mat_<T2>& m2, Mat_<T3>& m3, Op op )\r
-{\r
-    int y, x, rows = m1.rows, cols = m1.cols;\r
-\r
-    CV_DbgAssert( m1.size() == m2.size() );\r
-\r
-    for( y = 0; y < rows; y++ )\r
-    {\r
-        const T1* src1 = m1[y];\r
-        const T2* src2 = m2[y];\r
-        T3* dst = m3[y];\r
-\r
-        for( x = 0; x < cols; x++ )\r
-            dst[x] = op( src1[x], src2[x] );\r
-    }\r
-}\r
-\r
-template<typename M> class CV_EXPORTS MatExpr_Base_\r
-{\r
-public:\r
-    MatExpr_Base_() {}\r
-    virtual ~MatExpr_Base_() {}\r
-    virtual void assignTo(M& m, int type=-1) const = 0;\r
-};\r
-\r
-template<typename E, typename M> class CV_EXPORTS MatExpr_ : public MatExpr_Base_<M>\r
-{\r
-public:\r
-    MatExpr_(const E& _e) : e(_e) {}\r
-    ~MatExpr_() {}\r
-    operator M() const { return (M)e; }\r
-    void assignTo(M& m, int type=-1) const { e.assignTo(m, type); }\r
-\r
-    M row(int y) const { return ((M)e).row(y); }\r
-    M col(int x) const { return ((M)e).col(x); }\r
-    M diag(int d=0) const { return ((M)e).diag(d); }\r
-\r
-    M operator()( const Range& rowRange, const Range& colRange ) const\r
-    { return ((M)e)(rowRange, colRange); }\r
-    M operator()( const Rect& roi ) const { return ((M)e)(roi); }\r
-\r
-    M cross(const M& m) const { return ((M)e).cross(m); }\r
-    double dot(const M& m) const { return ((M)e).dot(m); }\r
-\r
-    MatExpr_<MatExpr_Op2_<M, double, M, MatOp_T_<Mat> >, M> t() const\r
-    { return ((M)e).t(); }\r
-    MatExpr_<MatExpr_Op2_<M, int, M, MatOp_Inv_<Mat> >, M> inv(int method=DECOMP_LU) const\r
-    { return ((M)e).inv(method); }\r
-\r
-    MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-    mul(const M& m, double scale=1) const\r
-    { return ((M)e).mul(m, scale); }\r
-    template<typename A> MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M >\r
-    mul(const MatExpr_<A, M>& m, double scale=1) const\r
-    { return ((M)e).mul(m, scale); }\r
-\r
-    E e;\r
-};\r
-\r
-\r
-inline Mat::Mat(const MatExpr_Base& expr)\r
- : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)\r
-{\r
-    expr.assignTo(*this);\r
-}\r
-\r
-inline Mat& Mat::operator = (const MatExpr_Base& expr)\r
-{\r
-    expr.assignTo(*this);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatExpr_Base& e) : Mat()\r
-{\r
-    e.assignTo(*this, DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr_Base& e)\r
-{\r
-    e.assignTo(*this, DataType<_Tp>::type);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp>::operator MatExpr_<Mat_<_Tp>, Mat_<_Tp> >() const\r
-{ return MatExpr_<Mat_<_Tp>, Mat_<_Tp> >(*this); }\r
-\r
-inline Mat::operator MatExpr_<Mat, Mat>() const\r
-{ return MatExpr_<Mat, Mat>(*this); }\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Sub_\r
-{\r
-public:\r
-    MatOp_Sub_() {}\r
-\r
-    static void apply(const M& a, const M& b, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            subtract( a, b, c );\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Scale_\r
-{\r
-public:\r
-    MatOp_Scale_() {}\r
-\r
-    static void apply(const M& a, double alpha, M& c, int type=-1)\r
-    {\r
-        a.convertTo(c, type, alpha, 0);\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_ScaleAddS_\r
-{\r
-public:\r
-    MatOp_ScaleAddS_() {}\r
-\r
-    static void apply(const M& a, double alpha, double beta, M& c, int type=-1)\r
-    {\r
-        a.convertTo(c, type, alpha, beta);\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_AddS_\r
-{\r
-public:\r
-    MatOp_AddS_() {}\r
-\r
-    static void apply(const M& a, const Scalar& s, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            add(a, s, c);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, s, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_AddEx_\r
-{\r
-public:\r
-    MatOp_AddEx_() {}\r
-\r
-    static void apply(const M& a, double alpha, const M& b,\r
-                      double beta, double gamma, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            addWeighted(a, alpha, b, beta, gamma, c);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, alpha, b, beta, gamma, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Bin_\r
-{\r
-public:\r
-    MatOp_Bin_() {}\r
-\r
-    static void apply(const M& a, const M& b, int _op, M& c, int type=-1)\r
-    {\r
-        char op = (char)_op;\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            if( op == '&' )\r
-                bitwise_and( a, b, c );\r
-            else if( op == '|' )\r
-                bitwise_or( a, b, c );\r
-            else if( op == '^' )\r
-                bitwise_xor( a, b, c );\r
-            else if( op == 'm' )\r
-                min( a, b, c );\r
-            else if( op == 'M' )\r
-                max( a, b, c );\r
-            else if( op == 'a' )\r
-                absdiff( a, b, c );\r
-            else\r
-                assert(0);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, op, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_BinS_\r
-{\r
-public:\r
-    MatOp_BinS_() {}\r
-\r
-    static void apply(const M& a, const Scalar& s, int _op, M& c, int type=-1)\r
-    {\r
-        char op = (char)_op;\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            if( op == '&' )\r
-                bitwise_and( a, s, c );\r
-            else if( op == '|' )\r
-                bitwise_or( a, s, c );\r
-            else if( op == '^' )\r
-                bitwise_xor( a, s, c );\r
-            else if( op == 'm' )\r
-                min( a, s[0], c );\r
-            else if( op == 'M' )\r
-                max( a, s[0], c );\r
-            else if( op == 'a' )\r
-                absdiff( a, s, c );\r
-            else if( op == '~' )\r
-                bitwise_not( a, c );\r
-            else\r
-                assert(0);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, s, op, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_T_\r
-{\r
-public:\r
-    MatOp_T_() {}\r
-\r
-    static void apply(const M& a, double scale, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            transpose(a, c);\r
-            if( fabs(scale - 1) > DBL_EPSILON )\r
-                c.convertTo(c, -1, scale, 0);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, scale, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-\r
-template<typename M> class CV_EXPORTS MatOp_MatMul_\r
-{\r
-public:\r
-    MatOp_MatMul_() {}\r
-\r
-    static void apply(const M& a, const M& b, double scale, int flags, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            gemm(a, b, scale, Mat(), 0, c, flags);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, scale, flags, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-\r
-template<typename M> class CV_EXPORTS MatOp_MatMulAdd_\r
-{\r
-public:\r
-    MatOp_MatMulAdd_() {}\r
-\r
-    static void apply(const M& a, const M& b, double alpha,\r
-        const M& c, double beta, int flags, M& d, int type=-1)\r
-    {\r
-        if( type == a.type() || type < 0 )\r
-        {\r
-            gemm(a, b, alpha, c, beta, d, flags);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, alpha, c, beta, flags, temp);\r
-            temp.convertTo(d, type);\r
-        }\r
-    }\r
-};\r
-\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Cmp_\r
-{\r
-public:\r
-    MatOp_Cmp_() {}\r
-\r
-    static void apply(const M& a, const M& b, int op, M& c, int type=-1)\r
-    {\r
-        if( type == CV_8UC1 || type == -1 )\r
-        {\r
-            compare(a, b, c, op);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, op, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_CmpS_\r
-{\r
-public:\r
-    MatOp_CmpS_() {}\r
-\r
-    static void apply(const M& a, double alpha, int op, M& c, int type=-1)\r
-    {\r
-        if( type == CV_8UC1 || type == -1 )\r
-        {\r
-            compare(a, alpha, c, op);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, alpha, op, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_MulDiv_\r
-{\r
-public:\r
-    MatOp_MulDiv_() {}\r
-\r
-    static void apply(const M& a, const M& b, double alpha, char op, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type == -1 )\r
-        {\r
-            if( op == '*' )\r
-                multiply( a, b, c, alpha );\r
-            else\r
-                divide( a, b, c, alpha );\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, alpha, op, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_DivRS_\r
-{\r
-public:\r
-    MatOp_DivRS_() {}\r
-\r
-    static void apply(const M& a, double alpha, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type == -1 )\r
-        {\r
-            c.create(a.rows, a.cols, a.type());\r
-            divide( alpha, a, c );\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, alpha, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Inv_\r
-{\r
-public:\r
-    MatOp_Inv_() {}\r
-\r
-    static void apply(const M& a, int method, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type == -1 )\r
-        {\r
-            invert(a, c, method);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, method, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Solve_\r
-{\r
-public:\r
-    MatOp_Solve_() {}\r
-\r
-    static void apply(const M& a, const M& b, int method, M& c, int type=-1)\r
-    {\r
-        if( type == a.type() || type == -1 )\r
-        {\r
-            solve(a, b, c, method);\r
-        }\r
-        else\r
-        {\r
-            Mat temp;\r
-            apply(a, b, method, temp);\r
-            temp.convertTo(c, type);\r
-        }\r
-    }\r
-};\r
-\r
-template<typename M> class CV_EXPORTS MatOp_Set_\r
-{\r
-public:\r
-    MatOp_Set_() {}\r
-\r
-    static void apply(Size size, int type0, const Scalar& s, int mtype, M& c, int type=-1)\r
-    {\r
-        if( type < 0 )\r
-            type = type0;\r
-        c.create(size.height, size.width, type);\r
-        if( mtype == 0 )\r
-            c = Scalar(0);\r
-        else if( mtype == 1 )\r
-            c = s;\r
-        else if( mtype == 2 )\r
-            setIdentity(c, s);\r
-    }\r
-};\r
-\r
-template<typename A1, typename M, typename Op>\r
-class CV_EXPORTS MatExpr_Op1_\r
-{\r
-public:\r
-    MatExpr_Op1_(const A1& _a1) : a1(_a1) {}\r
-    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, (M&)m, type); }\r
-    operator M() const { M result; assignTo(result); return result; }\r
-\r
-    A1 a1;\r
-};\r
-\r
-template<typename A1, typename A2, typename M, typename Op>\r
-class CV_EXPORTS MatExpr_Op2_\r
-{\r
-public:\r
-    MatExpr_Op2_(const A1& _a1, const A2& _a2) : a1(_a1), a2(_a2) {}\r
-    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, (M&)m, type); }\r
-    operator M() const { M result; assignTo(result); return result; }\r
-\r
-    A1 a1; A2 a2;\r
-};\r
-\r
-template<typename A1, typename A2, typename A3, typename M, typename Op>\r
-class CV_EXPORTS MatExpr_Op3_\r
-{\r
-public:\r
-    MatExpr_Op3_(const A1& _a1, const A2& _a2, const A3& _a3) : a1(_a1), a2(_a2), a3(_a3) {}\r
-    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, (M&)m, type); }\r
-    operator M() const { M result; assignTo(result); return result; }\r
-\r
-    A1 a1; A2 a2; A3 a3;\r
-};\r
-\r
-template<typename A1, typename A2, typename A3, typename A4, typename M, typename Op>\r
-class CV_EXPORTS MatExpr_Op4_\r
-{\r
-public:\r
-    MatExpr_Op4_(const A1& _a1, const A2& _a2, const A3& _a3, const A4& _a4)\r
-        : a1(_a1), a2(_a2), a3(_a3), a4(_a4) {}\r
-    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, a4, (M&)m, type); }\r
-    operator M() const { M result; assignTo(result); return result; }\r
-\r
-    A1 a1; A2 a2; A3 a3; A4 a4;\r
-};\r
-\r
-template<typename A1, typename A2, typename A3, typename A4, typename A5, typename M, typename Op>\r
-class CV_EXPORTS MatExpr_Op5_\r
-{\r
-public:\r
-    MatExpr_Op5_(const A1& _a1, const A2& _a2, const A3& _a3, const A4& _a4, const A5& _a5)\r
-        : a1(_a1), a2(_a2), a3(_a3), a4(_a4), a5(_a5) {}\r
-    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, a4, a5, (M&)m, type); }\r
-    operator M() const { M result; assignTo(result); return result; }\r
-\r
-    A1 a1; A2 a2; A3 a3; A4 a4; A5 a5;\r
-};\r
-\r
-template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename M, typename Op>\r
-class CV_EXPORTS MatExpr_Op6_\r
-{\r
-public:\r
-    MatExpr_Op6_(const A1& _a1, const A2& _a2, const A3& _a3,\r
-                    const A4& _a4, const A5& _a5, const A6& _a6)\r
-        : a1(_a1), a2(_a2), a3(_a3), a4(_a4), a5(_a5), a6(_a6) {}\r
-    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, a4, a5, a6, (M&)m, type); }\r
-    operator M() const { M result; assignTo(result); return result; }\r
-\r
-    A1 a1; A2 a2; A3 a3; A4 a4; A5 a5; A6 a6;\r
-};\r
-\r
-///////////////////////////////// Arithmetical Operations ///////////////////////////////////\r
-\r
-// A + B\r
-static inline MatExpr_<MatExpr_Op5_<Mat, double, Mat, double, double, Mat, MatOp_AddEx_<Mat> >, Mat>\r
-operator + (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op5_<Mat, double, Mat, double, double, Mat, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, 1, b, 1, 0));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op5_<Mat_<_Tp>, double, Mat_<_Tp>,\r
-double, double, Mat_<_Tp>, MatOp_AddEx_<Mat> >, Mat_<_Tp> >\r
-operator + (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op5_<Mat_<_Tp>, double, Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, 1, b, 1, 0));\r
-}\r
-\r
-// E1 + E2\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<M, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<M, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, 1, (M)b, 1, 0));\r
-}\r
-\r
-// A - B\r
-static inline MatExpr_<MatExpr_Op2_<Mat, Mat, Mat, MatOp_Sub_<Mat> >, Mat>\r
-operator - (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op2_<Mat, Mat, Mat, MatOp_Sub_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Mat_<_Tp>, Mat_<_Tp>, MatOp_Sub_<Mat> >, Mat_<_Tp> >\r
-operator - (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op2_<Mat_<_Tp>, Mat_<_Tp>, Mat_<_Tp>, MatOp_Sub_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, b));\r
-}\r
-\r
-// E1 - E2\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, M, M, MatOp_Sub_<Mat> >, M>\r
-operator - (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b )\r
-{\r
-    typedef MatExpr_Op2_<M, M, M, MatOp_Sub_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, (M)b));\r
-}\r
-\r
-// -(E1 - E2)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<B, A, M, MatOp_Sub_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op2_<B, A, M, MatOp_Sub_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a2, a.e.a1));\r
-}\r
-\r
-// (A - B)*alpha\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a,\r
-            double alpha)\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, alpha, a.e.a2, -alpha, 0));\r
-}\r
-\r
-// alpha*(A - B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator * (double alpha,\r
-            const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a)\r
-{ return a*alpha; }\r
-\r
-\r
-// A*alpha\r
-static inline\r
-MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>\r
-operator * (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha));\r
-}\r
-\r
-// A*alpha\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >\r
-operator * (const Mat_<_Tp>& a, double alpha)\r
-{\r
-    typedef MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, alpha));\r
-}\r
-\r
-// alpha*A\r
-static inline\r
-MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>\r
-operator * (double alpha, const Mat& a)\r
-{ return a*alpha; }\r
-\r
-// alpha*A\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >\r
-operator * (double alpha, const Mat_<_Tp>& a)\r
-{ return a*alpha; }\r
-\r
-// A/alpha\r
-static inline\r
-MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>\r
-operator / (const Mat& a, double alpha)\r
-{ return a*(1./alpha); }\r
-\r
-// A/alpha\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >\r
-operator / (const Mat_<_Tp>& a, double alpha)\r
-{ return a*(1./alpha); }\r
-\r
-// -A\r
-static inline\r
-MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>\r
-operator - (const Mat& a)\r
-{ return a*(-1); }\r
-\r
-// -A\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >\r
-operator - (const Mat_<_Tp>& a)\r
-{ return a*(-1); }\r
-\r
-// E*alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>\r
-operator * (const MatExpr_<A, M>& a, double alpha)\r
-{\r
-    typedef MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, alpha));\r
-}\r
-\r
-// alpha*E\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>\r
-operator * (double alpha, const MatExpr_<A, M>& a)\r
-{ return a*alpha; }\r
-\r
-// E/alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>\r
-operator / (const MatExpr_<A, M>& a, double alpha)\r
-{ return a*(1./alpha); }\r
-\r
-// (E*alpha)*beta ~ E*(alpha*beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a.e.a1*(a.e.a2*beta); }\r
-\r
-// beta*(E*alpha) ~ E*(alpha*beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>\r
-operator * (double beta,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return a.e.a1*(a.e.a2*beta); }\r
-\r
-// (E*alpha)/beta ~ E*(alpha/beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>\r
-operator / (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a.e.a1*(a.e.a2/beta); }\r
-\r
-// -E ~ E*(-1)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<MatExpr_<A, M>, double, M, MatOp_Scale_<Mat> >, M>\r
-operator - (const MatExpr_<A, M>& a)\r
-{ return a*(-1); }\r
-\r
-// -(E*alpha) ~ E*(-alpha)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return a.e.a1*(-a.e.a2); }\r
-\r
-// A + alpha\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >\r
-operator + (const Mat_<_Tp>& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>,\r
-        MatOp_ScaleAddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, 1, alpha));\r
-}\r
-\r
-// A + alpha\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>, MatOp_AddS_<Mat> >, Mat_<_Tp> >\r
-operator + (const Mat_<_Tp>& a, const Scalar& alpha)\r
-{\r
-    typedef MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>,\r
-        MatOp_AddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, alpha));\r
-}\r
-\r
-// alpha + A\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >\r
-operator + (double alpha, const Mat_<_Tp>& a)\r
-{ return a + alpha; }\r
-\r
-// alpha + A\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>, MatOp_AddS_<Mat> >, Mat_<_Tp> >\r
-operator + (const Scalar& alpha, const Mat_<_Tp>& a)\r
-{ return a + alpha; }\r
-\r
-// A - alpha\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >\r
-operator - (const Mat_<_Tp>& a, double alpha)\r
-{ return a + (-alpha); }\r
-\r
-// A - alpha\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>, MatOp_AddS_<Mat> >, Mat_<_Tp> >\r
-operator - (const Mat_<_Tp>& a, const Scalar& alpha)\r
-{ return a + (-alpha); }\r
-\r
-// alpha - A\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >\r
-operator - (double alpha, const Mat_<_Tp>& a)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>,\r
-        MatOp_ScaleAddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, -1, alpha));\r
-}\r
-\r
-// E + alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator + (const MatExpr_<A, M>& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, 1, alpha));\r
-}\r
-\r
-// E + alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> >, M>\r
-operator + (const MatExpr_<A, M>& a, const Scalar& alpha)\r
-{\r
-    typedef MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, alpha));\r
-}\r
-\r
-// alpha + E\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator + (double alpha, const MatExpr_<A, M>& a)\r
-{ return a + alpha; }\r
-\r
-// alpha + E\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> >, M>\r
-operator + (const Scalar& alpha, const MatExpr_<A, M>& a)\r
-{ return a + alpha; }\r
-\r
-// E - alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (const MatExpr_<A, M>& a, double alpha)\r
-{ return a + (-alpha); }\r
-\r
-// E - alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> >, M>\r
-operator - (const MatExpr_<A, M>& a, const Scalar& alpha)\r
-{ return a + (-alpha); }\r
-\r
-// alpha - E\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (double alpha, const MatExpr_<A, M>& a)\r
-{\r
-    typedef MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a, -1, alpha));\r
-}\r
-\r
-// E*alpha + beta\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            double beta)\r
-{\r
-    typedef MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, beta));\r
-}\r
-\r
-// beta + E*alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator + (double beta,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return a + beta; }\r
-\r
-// E*alpha - beta\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a + (-beta); }\r
-\r
-// beta - E*alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (double beta,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return (a.e.a1*(-a.e.a2)) + beta; }\r
-\r
-// (E*alpha + gamma) + beta ~ E*alpha + (gamma + beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a.e.a1*a.e.a2 + (a.e.a3 + beta); }\r
-\r
-// beta + (E*alpha + gamma)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator + (double beta, const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return a + beta; }\r
-\r
-// (E*alpha + gamma) - beta\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a + (-beta); }\r
-\r
-// beta - (E*alpha + gamma)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (double beta, const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return a.e.a1*(-a.e.a2) + (beta - a.e.a3); }\r
-\r
-// (E*alpha + gamma)*beta\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a.e.a1*(a.e.a2*beta) + (a.e.a3*beta); }\r
-\r
-// beta*(E*alpha + gamma)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator * (double beta, const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return a*beta; }\r
-\r
-// -(E*alpha + beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return a*(-1); }\r
-\r
-// (A*u + B*v + w) + beta\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a,\r
-            double beta )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, a.e.a3, a.e.a4, a.e.a5 + beta));\r
-}\r
-\r
-// beta + (A*u + B*v + w)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (double beta,\r
-            const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)\r
-{ return a + beta; }\r
-\r
-// (A*u + B*v + w) - beta\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a,\r
-            double beta)\r
-{ return a + (-beta); }\r
-\r
-// beta - (A*u + B*v + w)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (double beta,\r
-            const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, -a.e.a2, a.e.a3, -a.e.a4, -a.e.a5 + beta));\r
-}\r
-\r
-// (A*u + B*v + w)*beta\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a,\r
-            double beta )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1,\r
-        a.e.a2*beta, a.e.a3, a.e.a4*beta, a.e.a5*beta));\r
-}\r
-\r
-// beta*(A*u + B*v + w)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator * (double beta,\r
-            const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)\r
-{ return a * beta; }\r
-\r
-// -(A*u + B*v + w)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)\r
-{ return a*(-1); }\r
-\r
-// A*alpha + B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const M& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, 1, 0));\r
-}\r
-\r
-// B + A*alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const M& b,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return a + b; }\r
-\r
-// (A*alpha + beta) + B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const M& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, 1, a.e.a3));\r
-}\r
-\r
-// B + (A*alpha + beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const M& b,\r
-            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return a + b; }\r
-\r
-\r
-// A*alpha + E\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const MatExpr_<B, M>& b )\r
-{ return a + (M)b; }\r
-\r
-// E + A*alpha\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<B, M>& b,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return a + (M)b; }\r
-\r
-// (A*alpha + beta) + E\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const MatExpr_<B, M>& b )\r
-{ return a + (M)b; }\r
-\r
-// E + (A*alpha + beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<B, M>& b,\r
-            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return a + b; }\r
-\r
-// A*alpha + B*beta\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, b.e.a2, 0));\r
-}\r
-\r
-// (A*alpha + beta) + B*gamma\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, b.e.a2, a.e.a3));\r
-}\r
-\r
-// B*gamma + (A*alpha + beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b,\r
-            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a )\r
-{ return a + b; }\r
-\r
-// (A*alpha + beta) + (B*gamma + theta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, b.e.a2, a.e.a3 + b.e.a3));\r
-}\r
-\r
-// A*alpha - B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const M& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, -1, 0));\r
-}\r
-\r
-// B - A*alpha\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const M& b,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{\r
-    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, -a.e.a2, b, 1, 0));\r
-}\r
-\r
-// (A*alpha + beta) - B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const M& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, -1, a.e.a3));\r
-}\r
-\r
-// B - (A*alpha + beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const M& b,\r
-            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{\r
-    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, -1, a.e.a3));\r
-}\r
-\r
-// A*alpha - E\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const MatExpr_<B, M>& b )\r
-{ return a - (M)b; }\r
-\r
-// E - A*alpha\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<B, M>& b,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return (M)b - a; }\r
-\r
-// (A*alpha + beta) - E\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const MatExpr_<B, M>& b )\r
-{ return a - (M)b; }\r
-\r
-// E - (A*alpha + beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<B, M>& b,\r
-            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)\r
-{ return (M)b - a; }\r
-\r
-// A*alpha - B*beta\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, -b.e.a2, 0));\r
-}\r
-\r
-// (A*alpha + beta) - B*gamma\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, -b.e.a2, a.e.a3));\r
-}\r
-\r
-// B*gamma - (A*alpha + beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b,\r
-            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, -a.e.a2, b.e.a1, b.e.a2, -a.e.a3));\r
-}\r
-\r
-// (A*alpha + beta) - (B*gamma + theta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>\r
-operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, -b.e.a2, a.e.a3 - b.e.a3));\r
-}\r
-\r
-/////////////////////////////// Mat Multiplication ///////////////////////////////////\r
-\r
-// A^t\r
-inline MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_T_<Mat> >, Mat>\r
-Mat::t() const\r
-{\r
-    typedef MatExpr_Op2_<Mat, double, Mat, MatOp_T_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, 1));\r
-}\r
-\r
-template<typename _Tp> inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_T_<Mat> >, Mat_<_Tp> >\r
-Mat_<_Tp>::t() const\r
-{\r
-    typedef MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_T_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, 1));\r
-}\r
-\r
-// A*B\r
-static inline\r
-MatExpr_<MatExpr_Op4_<Mat, Mat, double, int, Mat, MatOp_MatMul_<Mat> >, Mat>\r
-operator * ( const Mat& a, const Mat& b )\r
-{\r
-    typedef MatExpr_Op4_<Mat, Mat, double, int, Mat, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 1, 0));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, int, Mat_<_Tp>,\r
-MatOp_MatMul_<Mat> >, Mat_<_Tp> >\r
-operator * ( const Mat_<_Tp>& a, const Mat_<_Tp>& b )\r
-{\r
-    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, int, Mat_<_Tp>,\r
-        MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, b, 1, 0));\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<A, M>& a, const MatExpr_<B, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, (M)b, 1, 0));\r
-}\r
-\r
-// (A*alpha)*B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a, const M& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2, 0));\r
-}\r
-\r
-// A*(B*alpha)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const M& b, const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(b, (M)a.e.a1, a.e.a2, 0));\r
-}\r
-\r
-// A^t*B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& a, const M& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2, GEMM_1_T));\r
-}\r
-\r
-// A*B^t\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const M& a, const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a, (M)b.e.a1, b.e.a2, GEMM_2_T));\r
-}\r
-\r
-// (A*alpha)*(B*beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1, a.e.a2*b.e.a2, 0));\r
-}\r
-\r
-// A^t*(B*alpha)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1, a.e.a2*b.e.a2, GEMM_1_T));\r
-}\r
-\r
-// (A*alpha)*B^t\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1, a.e.a2*b.e.a2, GEMM_2_T));\r
-}\r
-\r
-// A^t*B^t\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1,\r
-        (M)b.e.a1, a.e.a2*b.e.a2, GEMM_1_T+GEMM_2_T));\r
-}\r
-\r
-// (A*B)*alpha\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             double alpha )\r
-{\r
-    typedef MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, a.e.a3*alpha, a.e.a4));\r
-}\r
-\r
-// alpha*(A*B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator * ( double alpha,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{\r
-    return a*alpha;\r
-}\r
-\r
-// -(A*B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{\r
-    return a*(-1);\r
-}\r
-\r
-// (A*alpha + beta)*B\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& a, const M& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2, b, a.e.a3, 0));\r
-}\r
-\r
-// A*(B*alpha + beta)\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const M& a, const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a, (M)b.e.a1, b.e.a2, a, b.e.a3, 0));\r
-}\r
-\r
-// (A*alpha + beta)*(B*gamma)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,\r
-        a.e.a2*b.e.a2, (M)b.e.a1, a.e.a3*b.e.a2, 0));\r
-}\r
-\r
-// (A*gamma)*(B*alpha + beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,\r
-        a.e.a2*b.e.a2, (M)a.e.a1, a.e.a2*b.e.a3, 0));\r
-}\r
-\r
-// (A*alpha + beta)*B^t\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,\r
-        a.e.a2*b.e.a2, (M)b.e.a1, a.e.a3*b.e.a2, GEMM_2_T));\r
-}\r
-\r
-// A^t*(B*alpha + beta)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,\r
-        a.e.a2*b.e.a2, (M)a.e.a1, a.e.a2*b.e.a3, GEMM_1_T));\r
-}\r
-\r
-// (A*B + C)*alpha\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( const MatExpr_<MatExpr_Op6_<A, B, double, C,\r
-             double, int, M, MatOp_MatMulAdd_<Mat> >, M>& a, double alpha )\r
-{\r
-    typedef MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2,\r
-        a.e.a3*alpha, a.e.a4, a.e.a5*alpha, a.e.a6));\r
-}\r
-\r
-// alpha*(A*B + C)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator * ( double alpha, const MatExpr_<MatExpr_Op6_<A, B, double, C,\r
-             double, int, M, MatOp_MatMulAdd_<Mat> >, M>& a )\r
-{ return a*alpha; }\r
-\r
-// -(A*B + C)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op6_<A, B, double, C,\r
-             double, int, M, MatOp_MatMulAdd_<Mat> >, M>& a )\r
-{ return a*(-1); }\r
-\r
-\r
-// (A*B) + C\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const M& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, b, 1, a.e.a4));\r
-}\r
-\r
-// C + (A*B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const M& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{ return a + b; }\r
-\r
-\r
-// (A*B) - C\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const M& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, b, -1, a.e.a4));\r
-}\r
-\r
-// C - (A*B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const M& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, -a.e.a3, b, 1, a.e.a4));\r
-}\r
-\r
-\r
-// (A*B) + C\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const MatExpr_<C, M>& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b, 1, a.e.a4));\r
-}\r
-\r
-// C + (A*B)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<C, M>& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{ return a + b; }\r
-\r
-\r
-// (A*B) - C\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const MatExpr_<C, M>& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b, -1, a.e.a4));\r
-}\r
-\r
-// C - (A*B)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<C, M>& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, -a.e.a3, (M)b, 1, a.e.a4));\r
-}\r
-\r
-\r
-// (A*B) + C*alpha\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4));\r
-}\r
-\r
-// C*alpha + (A*B)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{ return a + b; }\r
-\r
-\r
-// (A*B) - (C*alpha)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, -b.e.a2, a.e.a4));\r
-}\r
-\r
-// (C*alpha) - (A*B)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, -a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4));\r
-}\r
-\r
-\r
-// (A*B) + C^t\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4 + GEMM_3_T));\r
-}\r
-\r
-// C^t + (A*B)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator + ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{ return a + b; }\r
-\r
-\r
-// (A*B) - C^t\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,\r
-             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, -b.e.a2, a.e.a4+GEMM_3_T));\r
-}\r
-\r
-// C^t - (A*B)\r
-template<typename A, typename B, typename C, typename M> static inline\r
-MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>\r
-operator - ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b,\r
-             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )\r
-{\r
-    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(\r
-        (M)a.e.a1, (M)a.e.a2, -a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4+GEMM_3_T));\r
-}\r
-\r
-\r
-////////////////////////////// Augmenting algebraic operations //////////////////////////////////\r
-\r
-static inline Mat& operator += (const Mat& a, const Mat& b)\r
-{\r
-    add(a, b, (Mat&)a);\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator -= (const Mat& a, const Mat& b)\r
-{\r
-    subtract(a, b, (Mat&)a);\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator *= (const Mat& a, const Mat& b)\r
-{\r
-    gemm(a, b, 1, Mat(), 0, (Mat&)a, 0);\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator *= (const Mat& a, double alpha)\r
-{\r
-    a.convertTo((Mat&)a, -1, alpha);\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator += (const Mat& a, const Scalar& s)\r
-{\r
-    add(a, s, (Mat&)a);\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator -= (const Mat& a, const Scalar& s)\r
-{ return (a += -s); }\r
-\r
-template<typename _Tp> static inline\r
-Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    (Mat&)a += (const Mat&)b;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline\r
-Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    (Mat&)a -= (const Mat&)b;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline\r
-Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    (Mat&)a *= (const Mat&)b;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline\r
-Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    (Mat&)a += s;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline\r
-Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    (Mat&)a -= s;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator += (const M& a, const MatExpr_<A, M>& b)\r
-{ return (a += (M)b); }\r
-\r
-template<typename A, typename M> static inline\r
-M& operator -= (const M& a, const MatExpr_<A, M>& b)\r
-{ return (a -= (M)b); }\r
-\r
-template<typename A, typename M> static inline\r
-M& operator *= (const M& a, const MatExpr_<A, M>& b)\r
-{ return (a *= (M)b); }\r
-\r
-template<typename A, typename M> static inline\r
-M& operator += (const M& a,\r
-                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    scaleAdd( b.e.a1, Scalar(b.e.a2), _a, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator -= (const M& a,\r
-                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    scaleAdd( b.e.a1, -Scalar(b.e.a2), _a, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator += (const M& a,\r
-                const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_AddEx_<Mat>::apply( a, 1, (M)b.e.a1, b.e.a2, b.e.a3, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator -= (const M& a,\r
-                const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_AddEx_<Mat>::apply( a, 1, (M)b.e.a1, -b.e.a2, -b.e.a3, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-M& operator += (const M& a,\r
-                const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_MatMulAdd_<Mat>::apply( (M)b.e.a1, (M)b.e.a2, b.e.a3, a, 1, b.e.a4, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-M& operator -= (const M& a,\r
-                const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_MatMulAdd_<Mat>::apply( (M)b.e.a1, (M)b.e.a2, -b.e.a3, a, 1, b.e.a4, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator *= (const M& a,\r
-                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_MatMul_<Mat>::apply( a, (M)b.e.a1, b.e.a2, 0, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator *= (const M& a,\r
-                const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_MatMulAdd_<Mat>::apply( a, (M)b.e.a1, b.e.a2, a, b.e.a3, 0, _a );\r
-    return _a;\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-M& operator *= (const M& a,\r
-                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& b)\r
-{\r
-    M& _a = (M&)a;\r
-    MatOp_MatMul_<Mat>::apply( a, (M)b.e.a1, b.e.a2, GEMM_2_T, _a );\r
-    return _a;\r
-}\r
-\r
-////////////////////////////// Logical operations ///////////////////////////////\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>\r
-operator & (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, '&'));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>\r
-operator | (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, '|'));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>\r
-operator ^ (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, '^'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-            MatOp_Bin_<Mat> >, Mat_<_Tp> >\r
-operator & (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-        MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(\r
-        a, b, '&'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-            MatOp_Bin_<Mat> >, Mat_<_Tp> >\r
-operator | (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-        MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(\r
-        a, b, '|'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-            MatOp_Bin_<Mat> >, Mat_<_Tp> >\r
-operator ^ (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-        MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(\r
-        a, b, '^'));\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator & (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)\r
-{ return (M)a & (M)b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator & (const MatExpr_<A, M>& a, const M& b)\r
-{ return (M)a & b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator & (const M& a, const MatExpr_<A, M>& b)\r
-{ return a & (M)b; }\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator | (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)\r
-{ return (M)a | (M)b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator | (const MatExpr_<A, M>& a, const M& b)\r
-{ return (M)a | b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator | (const M& a, const MatExpr_<A, M>& b)\r
-{ return a | (M)b; }\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator ^ (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)\r
-{ return (M)a ^ (M)b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator ^ (const MatExpr_<A, M>& a, const M& b)\r
-{ return (M)a ^ b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-operator ^ (const M& a, const MatExpr_<A, M>& b)\r
-{ return a ^ (M)b; }\r
-\r
-static inline Mat& operator &= (const Mat& a, const Mat& b)\r
-{\r
-    MatOp_Bin_<Mat>::apply( a, b, '&', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator |= (const Mat& a, const Mat& b)\r
-{\r
-    MatOp_Bin_<Mat>::apply( a, b, '|', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator ^= (const Mat& a, const Mat& b)\r
-{\r
-    MatOp_Bin_<Mat>::apply( a, b, '^', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-template<typename _Tp> static inline Mat_<_Tp>&\r
-operator &= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    (Mat&)a &= (const Mat&)b;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline Mat_<_Tp>&\r
-operator |= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    (Mat&)a |= (const Mat&)b;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline Mat_<_Tp>&\r
-operator ^= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    (Mat&)a ^= (const Mat&)b;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename A, typename M> static inline M&\r
-operator &= (const M& a, const MatExpr_<A, M>& b)\r
-{ return (a &= (M)b); }\r
-\r
-template<typename A, typename M> static inline M&\r
-operator |= (const M& a, const MatExpr_<A, M>& b)\r
-{ return (a |= (M)b); }\r
-\r
-template<typename A, typename M> static inline M&\r
-operator ^= (const M& a, const MatExpr_<A, M>& b)\r
-{ return (a ^= (M)b); }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator & (const Mat& a, const Scalar& s)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, s, '&'));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator & (const Scalar& s, const Mat& a)\r
-{ return a & s; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator | (const Mat& a, const Scalar& s)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, s, '|'));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator | (const Scalar& s, const Mat& a)\r
-{ return a | s; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator ^ (const Mat& a, const Scalar& s)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, s, '^'));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator ^ (const Scalar& s, const Mat& a)\r
-{ return a ^ s; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-operator ~ (const Mat& a)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, Scalar(), '~'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator & (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, s, '&'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator & (const Scalar& s, const Mat_<_Tp>& a)\r
-{ return a & s; }\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator | (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, s, '|'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator | (const Scalar& s, const Mat_<_Tp>& a)\r
-{ return a | s; }\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator ^ (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, s, '^'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator ^ (const Scalar& s, const Mat_<_Tp>& a)\r
-{ return a ^ s; }\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-operator ~ (const Mat_<_Tp>& a)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, Scalar(), '~'));\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator & (const MatExpr_<A, M>& a, const Scalar& s)\r
-{ return (M)a & s; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator & (const Scalar& s, const MatExpr_<A, M>& a)\r
-{ return (M)a & s; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator | (const MatExpr_<A, M>& a, const Scalar& s)\r
-{ return (M)a | s; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator | (const Scalar& s, const MatExpr_<A, M>& a)\r
-{ return (M)a | s; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator ^ (const MatExpr_<A, M>& a, const Scalar& s)\r
-{ return (M)a ^ s; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator ^ (const Scalar& s, const MatExpr_<A, M>& a)\r
-{ return (M)a ^ s; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >\r
-operator ~ (const MatExpr_<A, M>& a)\r
-{ return ~(M)a; }\r
-\r
-static inline Mat& operator &= (const Mat& a, const Scalar& s)\r
-{\r
-    MatOp_BinS_<Mat>::apply( a, s, '&', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator |= (const Mat& a, const Scalar& s)\r
-{\r
-    MatOp_BinS_<Mat>::apply( a, s, '|', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-static inline Mat& operator ^= (const Mat& a, const Scalar& s)\r
-{\r
-    MatOp_BinS_<Mat>::apply( a, s, '^', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-template<typename _Tp> static inline Mat_<_Tp>&\r
-operator &= (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    (Mat&)a &= s;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline Mat_<_Tp>&\r
-operator |= (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    (Mat&)a |= s;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline Mat_<_Tp>&\r
-operator ^= (const Mat_<_Tp>& a, const Scalar& s)\r
-{\r
-    (Mat&)a ^= s;\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-////////////////////////////// Comparison operations ///////////////////////////////\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>\r
-operator == (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_EQ));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>\r
-operator >= (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_GE));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>\r
-operator > (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_GT));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>\r
-operator <= (const Mat& a, const Mat& b)\r
-{ return b >= a; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>\r
-operator < (const Mat& a, const Mat& b)\r
-{ return b > a; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>\r
-operator != (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_NE));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator == (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_EQ));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator >= (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_GE));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator > (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_GT));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator <= (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_LE));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator < (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_LT));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator != (const Mat& a, double alpha)\r
-{\r
-    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_NE));\r
-}\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator == (double alpha, const Mat& a)\r
-{ return a == alpha; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator >= (double alpha, const Mat& a)\r
-{ return a <= alpha; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator > (double alpha, const Mat& a)\r
-{ return a < alpha; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator <= (double alpha, const Mat& a)\r
-{ return a >= alpha; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator < (double alpha, const Mat& a)\r
-{ return a > alpha; }\r
-\r
-static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>\r
-operator != (double alpha, const Mat& a)\r
-{ return a != alpha; }\r
-\r
-/////////////////////////////// Miscellaneous operations //////////////////////////////\r
-\r
-// max(A, B)\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>\r
-max(const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 'M'));\r
-}\r
-\r
-// min(A, B)\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>\r
-min(const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 'm'));\r
-}\r
-\r
-// abs(A)\r
-static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>\r
-abs(const Mat& a)\r
-{\r
-    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, Scalar(0), 'a'));\r
-}\r
-\r
-// max(A, B)\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-            MatOp_Bin_<Mat> >, Mat_<_Tp> >\r
-max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-        MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(\r
-        a, b, 'M'));\r
-}\r
-\r
-// min(A, B)\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-            MatOp_Bin_<Mat> >, Mat_<_Tp> >\r
-min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,\r
-        MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(\r
-        a, b, 'm'));\r
-}\r
-\r
-// abs(A)\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>,\r
-            MatOp_BinS_<Mat> >, Mat_<_Tp> >\r
-abs(const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>,\r
-        MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(\r
-        a, Scalar(0), 'a'));\r
-}\r
-\r
-// max(A, B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-max(const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)\r
-{ return max((M)a, (M)b); }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-max(const MatExpr_<A, M>& a, const M& b)\r
-{ return max((M)a, b); }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-max(const M& a, const MatExpr_<A, M>& b)\r
-{ return max(a, (M)b); }\r
-\r
-// min(A, B)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-min(const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)\r
-{ return min((M)a, (M)b); }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-min(const MatExpr_<A, M>& a, const M& b)\r
-{ return min((M)a, b); }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-min(const M& a, const MatExpr_<A, M>& b)\r
-{ return min(a, (M)b); }\r
-\r
-// abs(A)\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>\r
-abs(const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a)\r
-{\r
-    typedef MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)a.e.a2, 'a'));\r
-}\r
-\r
-template<typename _Tp> void merge(const Vector<Mat_<_Tp> >& mv, Mat& dst)\r
-{ merge( (const Vector<Mat>&)mv, dst ); }\r
-\r
-template<typename _Tp> void split(const Mat& src, Vector<Mat_<_Tp> >& mv)\r
-{ split(src, (Vector<Mat>&)mv ); }\r
-\r
-///// Element-wise multiplication\r
-\r
-inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>\r
-Mat::mul(const Mat& m, double scale) const\r
-{\r
-    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, m, scale, '*'));\r
-}\r
-\r
-inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>\r
-Mat::mul(const MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>& m, double scale) const\r
-{\r
-    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, m.e.a1, m.e.a2*scale, '*'));\r
-}\r
-\r
-inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>\r
-Mat::mul(const MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_DivRS_<Mat> >, Mat>& m, double scale) const\r
-{\r
-    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, m.e.a1, scale/m.e.a2, '/'));\r
-}\r
-\r
-template<typename _Tp> inline\r
-MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >\r
-Mat_<_Tp>::mul(const Mat_<_Tp>& m, double scale) const\r
-{\r
-    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, m, scale, '*'));\r
-}\r
-\r
-template<typename _Tp> inline\r
-MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >\r
-Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >& m, double scale) const\r
-{\r
-    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, m.e.a1, m.e.a2*scale, '*'));\r
-}\r
-\r
-template<typename _Tp> inline\r
-MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >\r
-Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_DivRS_<Mat> >, Mat_<_Tp> >& m, double scale) const\r
-{\r
-    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, m.e.a1, scale/m.e.a2, '/'));\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op4_<A, B, double, char, M, MatOp_MulDiv_<Mat> >, M>& a,\r
-            double alpha)\r
-{\r
-    typedef MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)a.e.a2, a.e.a3*alpha, a.e.a4));\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator * (double alpha,\r
-            const MatExpr_<MatExpr_Op4_<A, B, double, char, M, MatOp_MulDiv_<Mat> >, M>& a)\r
-{ return a*alpha; }\r
-\r
-\r
-////// Element-wise division\r
-\r
-static inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>\r
-operator / (const Mat& a, const Mat& b)\r
-{\r
-    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 1, '/'));\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double,\r
-char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >\r
-operator / (const Mat_<_Tp>& a, const Mat_<_Tp>& b)\r
-{\r
-    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double,\r
-        char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, b, 1, '/'));\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)\r
-{ return (M)a/(M)b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const MatExpr_<A, M>& a, const M& b)\r
-{ return (M)a/b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const M& a, const MatExpr_<A, M>& b)\r
-{ return a/(M)b; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const M& b)\r
-{ return ((M)a.e.a1/b)*a.e.a2; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const M& a,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)\r
-{ return (a/(M)b.e.a1)*(1./b.e.a2); }\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,\r
-            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b)\r
-{ return ((M)a.e.a1/(M)b.e.a1)*(a.e.a2/b.e.a2); }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const M& a,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_DivRS_<Mat> >, M>& b)\r
-{ return a.mul((M)b.e.a1, 1./b.e.a2); }\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>\r
-operator / (const MatExpr_<A, M>& a,\r
-            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_DivRS_<Mat> >, M>& b)\r
-{ return ((M)a).mul((M)b.e.a1, 1./b.e.a2); }\r
-\r
-static inline\r
-MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_DivRS_<Mat> >, Mat >\r
-operator / (double alpha, const Mat& a)\r
-{\r
-    typedef MatExpr_Op2_<Mat, double, Mat, MatOp_DivRS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha));\r
-}\r
-\r
-static inline Mat& operator /= (const Mat& a, double alpha)\r
-{\r
-    MatOp_Scale_<Mat>::apply( a, 1./alpha, (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-template<typename _Tp>\r
-static inline Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, double alpha)\r
-{\r
-    MatOp_Scale_<Mat>::apply( a, 1./alpha, (Mat&)a );\r
-    return (Mat_<_Tp>&)a;\r
-}\r
-\r
-template<typename _Tp> static inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_DivRS_<Mat> >, Mat_<_Tp> >\r
-operator / (double alpha, const Mat_<_Tp>& a)\r
-{\r
-    typedef MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>,\r
-        MatOp_DivRS_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, alpha));\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, double, M, MatOp_DivRS_<Mat> >, M>\r
-operator / (double alpha, const MatExpr_<A, M>& a)\r
-{ return alpha/(M)a; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, double, M, MatOp_DivRS_<Mat> >, M>\r
-operator / (double alpha,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)\r
-{ return (alpha/a.e.a2)/(M)a.e.a1; }\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>\r
-operator / (double alpha,\r
-            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_DivRS_<Mat> >, M>& a)\r
-{ return (M)a.e.a1*(alpha/a.e.a2); }\r
-\r
-static inline Mat& operator /= (const Mat& a, const Mat& b)\r
-{\r
-    MatOp_MulDiv_<Mat>::apply( a, b, 1, '/', (Mat&)a );\r
-    return (Mat&)a;\r
-}\r
-\r
-template<typename A, typename M>\r
-static inline M& operator /= (const M& a, const MatExpr_<MatExpr_Op2_<A, double,\r
-                              M, MatOp_Scale_<Mat> >, M>& b)\r
-{\r
-    MatOp_MulDiv_<Mat>::apply( a, (M)b.e.a1, 1./b.e.a2, '/', (M&)a );\r
-    return (M&)a;\r
-}\r
-\r
-template<typename A, typename M>\r
-static inline M& operator /= (const M& a, const MatExpr_<MatExpr_Op2_<A, double,\r
-                              M, MatOp_DivRS_<Mat> >, M>& b)\r
-{\r
-    MatOp_MulDiv_<Mat>::apply( a, (M)b.e.a1, 1./b.e.a2, '*', (M&)a );\r
-    return (M&)a;\r
-}\r
-\r
-// Mat Inversion and solving linear systems\r
-\r
-inline MatExpr_<MatExpr_Op2_<Mat, int, Mat, MatOp_Inv_<Mat> >, Mat>\r
-Mat::inv(int method) const\r
-{\r
-    typedef MatExpr_Op2_<Mat, int, Mat, MatOp_Inv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, method));\r
-}\r
-\r
-template<typename _Tp> inline\r
-MatExpr_<MatExpr_Op2_<Mat_<_Tp>, int, Mat_<_Tp>, MatOp_Inv_<Mat> >, Mat_<_Tp> >\r
-Mat_<_Tp>::inv(int method) const\r
-{\r
-    typedef MatExpr_Op2_<Mat_<_Tp>, int, Mat_<_Tp>, MatOp_Inv_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, method));\r
-}\r
-\r
-template<typename A, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Solve_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op2_<A, int, M, MatOp_Inv_<Mat> >, M>& a,\r
-            const M& b)\r
-{\r
-    typedef MatExpr_Op3_<M, M, int, M, MatOp_Solve_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2));\r
-}\r
-\r
-template<typename A, typename B, typename M> static inline\r
-MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Solve_<Mat> >, M>\r
-operator * (const MatExpr_<MatExpr_Op2_<A, int, M, MatOp_Inv_<Mat> >, M>& a,\r
-            const MatExpr_<B, M>& b)\r
-{ return a*(M)b; }\r
-\r
-\r
-/////////////////////////////// Initialization ////////////////////////////////////////\r
-\r
-inline MatExpr_Initializer Mat::zeros(int rows, int cols, int type)\r
-{\r
-    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(Size(cols, rows), type, 0, 0));\r
-}\r
-\r
-inline MatExpr_Initializer Mat::zeros(Size size, int type)\r
-{\r
-    return zeros(size.height, size.width, type);\r
-}\r
-\r
-inline MatExpr_Initializer Mat::ones(int rows, int cols, int type)\r
-{\r
-    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;\r
-    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(Size(cols, rows), type, 1, 1));\r
-}\r
-\r
-inline MatExpr_Initializer Mat::ones(Size size, int type)\r
-{\r
-    return ones(size.height, size.width, type);\r
-}\r
-\r
-inline MatExpr_Initializer Mat::eye(int rows, int cols, int type)\r
-{\r
-    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;\r
-    return MatExpr_Initializer(MatExpr_Temp(Size(cols, rows), type, 1, 2));\r
-}\r
-\r
-inline MatExpr_Initializer Mat::eye(Size size, int type)\r
-{\r
-    return eye(size.height, size.width, type);\r
-}\r
-\r
-static inline MatExpr_Initializer operator * (const MatExpr_Initializer& a, double alpha)\r
-{\r
-    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;\r
-    return MatExpr_Initializer(MatExpr_Temp(a.e.a1, a.e.a2, a.e.a3*alpha, a.e.a4));\r
-}\r
-\r
-static inline MatExpr_Initializer operator * (double alpha, MatExpr_Initializer& a)\r
-{ return a*alpha; }\r
-\r
-template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::zeros(int rows, int cols)\r
-{ return Mat::zeros(rows, cols, DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::zeros(Size size)\r
-{ return Mat::zeros(size, DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::ones(int rows, int cols)\r
-{ return Mat::ones(rows, cols, DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::ones(Size size)\r
-{ return Mat::ones(size, DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::eye(int rows, int cols)\r
-{ return Mat::eye(rows, cols, DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::eye(Size size)\r
-{ return Mat::eye(size, DataType<_Tp>::type); }\r
-\r
-\r
-//////////// Iterators & Comma initializers //////////////////\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_()\r
-    : m(0), ptr(0), sliceEnd(0) {}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m) : m(_m)\r
-{\r
-    if( !_m )\r
-        ptr = sliceEnd = 0;\r
-    else\r
-    {\r
-        ptr = (_Tp*)_m->data;\r
-        sliceEnd = ptr + (_m->isContinuous() ? _m->rows*_m->cols : _m->cols);\r
-    }\r
-}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>::\r
-    MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col) : m(_m)\r
-{\r
-    if( !_m )\r
-        ptr = sliceEnd = 0;\r
-    else\r
-    {\r
-        CV_DbgAssert( (unsigned)_row < _m->rows && (unsigned)_col < _m->cols );\r
-        ptr = (_Tp*)(_m->data + _m->step*_row);\r
-        sliceEnd = _m->isContinuous() ? (_Tp*)_m->data + _m->rows*_m->cols : ptr + _m->cols;\r
-        ptr += _col;\r
-    }\r
-}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>::\r
-    MatConstIterator_(const Mat_<_Tp>* _m, Point _pt) : m(_m)\r
-{\r
-    if( !_m )\r
-        ptr = sliceEnd = 0;\r
-    else\r
-    {\r
-        CV_DbgAssert( (unsigned)_pt.y < (unsigned)_m->rows && (unsigned)_pt.x < (unsigned)_m->cols );\r
-        ptr = (_Tp*)(_m->data + _m->step*_pt.y);\r
-        sliceEnd = _m->isContinuous() ? (_Tp*)_m->data + _m->rows*_m->cols : ptr + _m->cols;\r
-        ptr += _pt.x;\r
-    }\r
-}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>::\r
-    MatConstIterator_(const MatConstIterator_& it)\r
-    : m(it.m), ptr(it.ptr), sliceEnd(it.sliceEnd) {}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>&\r
-    MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )\r
-{\r
-    m = it.m; ptr = it.ptr; sliceEnd = it.sliceEnd;\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator *() const { return *ptr; }\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (int ofs)\r
-{\r
-    if( !m || ofs == 0 )\r
-        return *this;\r
-    ptr += ofs;\r
-    if( m->isContinuous() )\r
-    {\r
-        if( ptr > sliceEnd )\r
-            ptr = sliceEnd;\r
-        else if( ptr < (_Tp*)m->data )\r
-            ptr = (_Tp*)m->data;\r
-    }\r
-    else if( ptr >= sliceEnd || ptr < sliceEnd - m->cols )\r
-    {\r
-        ptr -= ofs;\r
-        Point pt = pos();\r
-        int cols = m->cols;\r
-        ofs += pt.y*cols + pt.x;\r
-        if( ofs > cols*m->rows )\r
-            ofs = cols*m->rows;\r
-        else if( ofs < 0 )\r
-            ofs = 0;\r
-        pt.y = ofs/cols;\r
-        pt.x = ofs - pt.y*cols;\r
-        ptr = (_Tp*)(m->data + m->step*pt.y);\r
-        sliceEnd = ptr + cols;\r
-        ptr += pt.x;\r
-    }\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (int ofs)\r
-{ return (*this += -ofs); }\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()\r
-{ return (*this += -1); }\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)\r
-{\r
-    MatConstIterator_ b = *this;\r
-    *this += -1;\r
-    return b;\r
-}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()\r
-{\r
-    if( m && ++ptr >= sliceEnd )\r
-    {\r
-        --ptr;\r
-        *this += 1;\r
-    }\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)\r
-{\r
-    MatConstIterator_ b = *this;\r
-    if( m && ++ptr >= sliceEnd )\r
-    {\r
-        --ptr;\r
-        *this += 1;\r
-    }\r
-    return b;\r
-}\r
-\r
-template<typename _Tp> inline Point MatConstIterator_<_Tp>::pos() const\r
-{\r
-    if( !m )\r
-        return Point();\r
-    if( m->isContinuous() )\r
-    {\r
-        int ofs = ptr - (_Tp*)m->data, y = ofs / m->cols, x = ofs - y*m->cols;\r
-        return Point(x,y);\r
-    }\r
-    else\r
-    {\r
-        int stepT = m->stepT(), y = (ptr - (_Tp*)m->data)/stepT, x = (ptr - (_Tp*)m->data) - y*stepT;\r
-        return Point(x,y);\r
-    }\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_() : MatConstIterator_<_Tp>() {}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)\r
-    : MatConstIterator_<_Tp>(_m) {}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)\r
-    : MatConstIterator_<_Tp>(_m, _row, _col) {}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, Point _pt)\r
-    : MatConstIterator_<_Tp>(_m, _pt) {}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)\r
-    : MatConstIterator_<_Tp>(it) {}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )\r
-{\r
-    this->m = it.m; this->ptr = it.ptr; this->sliceEnd = it.sliceEnd;\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator *() const { return *(this->ptr); }\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (int ofs)\r
-{\r
-    MatConstIterator_<_Tp>::operator += (ofs);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (int ofs)\r
-{\r
-    MatConstIterator_<_Tp>::operator += (-ofs);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()\r
-{\r
-    MatConstIterator_<_Tp>::operator += (-1);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)\r
-{\r
-    MatIterator_ b = *this;\r
-    MatConstIterator_<_Tp>::operator += (-1);\r
-    return b;\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()\r
-{\r
-    if( this->m && ++this->ptr >= this->sliceEnd )\r
-    {\r
-        --this->ptr;\r
-        MatConstIterator_<_Tp>::operator += (1);\r
-    }\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)\r
-{\r
-    MatIterator_ b = *this;\r
-    if( this->m && ++this->ptr >= this->sliceEnd )\r
-    {\r
-        --this->ptr;\r
-        MatConstIterator_<_Tp>::operator += (1);\r
-    }\r
-    return b;\r
-}\r
-\r
-template<typename _Tp> static inline bool\r
-operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)\r
-{ return a.m == b.m && a.ptr == b.ptr; }\r
-\r
-template<typename _Tp> static inline bool\r
-operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)\r
-{ return !(a == b); }\r
-\r
-template<typename _Tp> static inline bool\r
-operator < (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)\r
-{ return a.ptr < b.ptr; }\r
-\r
-template<typename _Tp> static inline bool\r
-operator > (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)\r
-{ return a.ptr > b.ptr; }\r
-\r
-template<typename _Tp> static inline bool\r
-operator <= (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)\r
-{ return a.ptr <= b.ptr; }\r
-\r
-template<typename _Tp> static inline bool\r
-operator >= (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)\r
-{ return a.ptr >= b.ptr; }\r
-\r
-template<typename _Tp> static inline int\r
-operator - (const MatConstIterator_<_Tp>& b, const MatConstIterator_<_Tp>& a)\r
-{\r
-    if( a.m != b.m )\r
-        return INT_MAX;\r
-    if( a.sliceEnd == b.sliceEnd )\r
-        return b.ptr - a.ptr;\r
-    {\r
-        Point ap = a.pos(), bp = b.pos();\r
-        if( bp.y > ap.y )\r
-            return (bp.y - ap.y - 1)*a.m->cols + (a.m->cols - ap.x) + bp.x;\r
-        if( bp.y < ap.y )\r
-            return -((ap.y - bp.y - 1)*a.m->cols + (a.m->cols - bp.x) + ap.x);\r
-        return bp.x - ap.x;\r
-    }\r
-}\r
-\r
-template<typename _Tp> static inline MatConstIterator_<_Tp>\r
-operator + (const MatConstIterator_<_Tp>& a, int ofs)\r
-{ MatConstIterator_<_Tp> b = a; return b += ofs; }\r
-\r
-template<typename _Tp> static inline MatConstIterator_<_Tp>\r
-operator + (int ofs, const MatConstIterator_<_Tp>& a)\r
-{ MatConstIterator_<_Tp> b = a; return b += ofs; }\r
-\r
-template<typename _Tp> static inline MatConstIterator_<_Tp>\r
-operator - (const MatConstIterator_<_Tp>& a, int ofs)\r
-{ MatConstIterator_<_Tp> b = a; return b += -ofs; }\r
-\r
-template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator [](int i) const\r
-{ return *(*this + i); }\r
-\r
-template<typename _Tp> static inline MatIterator_<_Tp>\r
-operator + (const MatIterator_<_Tp>& a, int ofs)\r
-{ MatIterator_<_Tp> b = a; return b += ofs; }\r
-\r
-template<typename _Tp> static inline MatIterator_<_Tp>\r
-operator + (int ofs, const MatIterator_<_Tp>& a)\r
-{ MatIterator_<_Tp> b = a; return b += ofs; }\r
-\r
-template<typename _Tp> static inline MatIterator_<_Tp>\r
-operator - (const MatIterator_<_Tp>& a, int ofs)\r
-{ MatIterator_<_Tp> b = a; return b += -ofs; }\r
-\r
-template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator [](int i) const\r
-{ return *(*this + i); }\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::begin() const\r
-{ return MatConstIterator_<_Tp>(this); }\r
-\r
-template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::end() const\r
-{\r
-    MatConstIterator_<_Tp> it(this);\r
-    it.ptr = it.sliceEnd = (_Tp*)(data + step*(rows-1)) + cols;\r
-    return it;\r
-}\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::begin()\r
-{ return MatIterator_<_Tp>(this); }\r
-\r
-template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::end()\r
-{\r
-    MatIterator_<_Tp> it(this);\r
-    it.ptr = it.sliceEnd = (_Tp*)(data + step*(rows-1)) + cols;\r
-    return it;\r
-}\r
-\r
-template<typename _Tp> class CV_EXPORTS MatOp_Iter_\r
-{\r
-    MatOp_Iter_() {}\r
-\r
-    static void apply(const MatIterator_<_Tp>& a, Mat& c, int type=-1)\r
-    {\r
-        if( type < 0 )\r
-            c = *a.m;\r
-        else\r
-            a.m->convertTo(c, type);\r
-    }\r
-};\r
-\r
-template<typename _Tp> inline MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m) :\r
-    MatExpr_<MatExpr_Op1_<MatIterator_<_Tp>, Mat_<_Tp>,\r
-        MatOp_Iter_<_Tp> >, Mat_<_Tp> >(MatIterator_<_Tp>(_m)) {}\r
-\r
-template<typename _Tp> template<typename T2> inline MatCommaInitializer_<_Tp>&\r
-MatCommaInitializer_<_Tp>::operator , (T2 v)\r
-{\r
-    CV_DbgAssert( this->e.a1 < this->e.a1.m->end() );\r
-    *this->e.a1 = _Tp(v); ++this->e.a1;\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const\r
-{\r
-    CV_DbgAssert( this->e.a1 == this->e.a1.m->end() );\r
-    return *this->e.a1.m;\r
-}\r
-\r
-template<typename _Tp> inline Mat_<_Tp> MatCommaInitializer_<_Tp>::operator *() const\r
-{\r
-    CV_DbgAssert( this->e.a1 == this->e.a1.m->end() );\r
-    return *this->e.a1.m;\r
-}\r
-\r
-template<typename _Tp> inline void\r
-MatCommaInitializer_<_Tp>::assignTo(Mat& m, int type) const\r
-{\r
-    Mat_<_Tp>(*this).assignTo(m, type);\r
-}\r
-\r
-template<typename _Tp, typename T2> static inline MatCommaInitializer_<_Tp>\r
-operator << (const Mat_<_Tp>& m, T2 val)\r
-{\r
-    MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);\r
-    return (commaInitializer, val);\r
-}\r
-\r
-//////////////////////////////// MatND ////////////////////////////////\r
-\r
-inline MatND::MatND()\r
- : flags(MAGIC_VAL), dims(0), refcount(0), data(0), datastart(0), dataend(0)\r
-{\r
-}\r
-\r
-inline MatND::MatND(const Vector<int>& _sizes, int _type)\r
- : flags(MAGIC_VAL), dims(0), refcount(0), data(0), datastart(0), dataend(0)\r
-{\r
-    create(_sizes, _type);\r
-}\r
-\r
-inline MatND::MatND(const Vector<int>& _sizes, int _type, const Scalar& _s)\r
- : flags(MAGIC_VAL), dims(0), refcount(0), data(0), datastart(0), dataend(0)\r
-{\r
-    create(_sizes, _type);\r
-    *this = _s;\r
-}\r
-\r
-inline MatND::MatND(const MatND& m)\r
- : flags(m.flags), dims(m.dims), refcount(m.refcount),\r
- data(m.data), datastart(m.datastart), dataend(m.dataend)\r
-{\r
-    int i, d = dims;\r
-    for( i = 0; i < d; i++ )\r
-    {\r
-        dim[i].size = m.dim[i].size;\r
-        dim[i].step = m.dim[i].step;\r
-    }\r
-    if( refcount )\r
-        ++*refcount;\r
-}\r
-\r
-inline MatND::MatND(const CvMatND* m, bool copyData)\r
-  : flags(MAGIC_VAL|(m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG))),\r
-  dims(m->dims), refcount(0), data(m->data.ptr)\r
-{\r
-    int i, d = dims;\r
-    for( i = 0; i < d; i++ )\r
-    {\r
-        dim[i].size = m->dim[i].size;\r
-        dim[i].step = m->dim[i].step;\r
-    }\r
-    datastart = data;\r
-    dataend = datastart + dim[0].size*dim[0].step;\r
-    if( copyData )\r
-    {\r
-        MatND temp(*this);\r
-        temp.copyTo(*this);\r
-    }\r
-}\r
-\r
-inline MatND::~MatND() { release(); }\r
-\r
-inline MatND& MatND::operator = (const MatND& m)\r
-{\r
-    if( this != &m )\r
-    {\r
-        if( m.refcount )\r
-            ++*m.refcount;\r
-        release();\r
-        flags = m.flags;\r
-        dims = m.dims;\r
-        data = m.data;\r
-        datastart = m.datastart;\r
-        dataend = m.dataend;\r
-        refcount = m.refcount;\r
-        int i, d = dims;\r
-        for( i = 0; i < d; i++ )\r
-        {\r
-            dim[i].size = m.dim[i].size;\r
-            dim[i].step = m.dim[i].step;\r
-        }\r
-    }\r
-    return *this;\r
-}\r
-\r
-inline MatND MatND::clone() const\r
-{\r
-    MatND temp;\r
-    this->copyTo(temp);\r
-    return temp;\r
-}\r
-\r
-inline MatND MatND::operator()(const Vector<Range>& ranges) const\r
-{\r
-    return MatND(*this, ranges);\r
-}\r
-\r
-inline void MatND::assignTo( MatND& m, int type ) const\r
-{\r
-    if( type < 0 )\r
-        m = *this;\r
-    else\r
-        convertTo(m, type);\r
-}\r
-\r
-inline void MatND::addref()\r
-{\r
-    if( refcount ) ++*refcount;\r
-}\r
-\r
-inline void MatND::release()\r
-{\r
-    if( refcount && --*refcount == 0 )\r
-        fastFree(datastart);\r
-    dims = 0;\r
-    data = datastart = dataend = 0;\r
-    refcount = 0;\r
-}\r
-\r
-inline bool MatND::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; }\r
-inline size_t MatND::elemSize() const { return getElemSize(flags); }\r
-inline size_t MatND::elemSize1() const { return CV_ELEM_SIZE1(flags); }\r
-inline int MatND::type() const { return CV_MAT_TYPE(flags); }\r
-inline int MatND::depth() const { return CV_MAT_DEPTH(flags); }\r
-inline int MatND::channels() const { return CV_MAT_CN(flags); }\r
-\r
-inline size_t MatND::step(int i) const { CV_DbgAssert((unsigned)i < (unsigned)dims); return dim[i].step; }\r
-inline size_t MatND::step1(int i) const\r
-{ CV_DbgAssert((unsigned)i < (unsigned)dims); return dim[i].step/elemSize1(); }\r
-inline Vector<int> MatND::size() const\r
-{\r
-    int i, d = dims;\r
-    Vector<int> sz(d);\r
-    for( i = 0; i < d; i++ )\r
-        sz[i] = dim[i].size;\r
-    return sz;\r
-}\r
-inline int MatND::size(int i) const  { CV_DbgAssert((unsigned)i < (unsigned)dims); return dim[i].size; }\r
-\r
-inline uchar* MatND::ptr(int i0)\r
-{\r
-    CV_DbgAssert( dims == 1 && data &&\r
-        (unsigned)i0 < (unsigned)dim[0].size );\r
-    return data + i0*dim[0].step;\r
-}\r
-\r
-inline const uchar* MatND::ptr(int i0) const\r
-{\r
-    CV_DbgAssert( dims == 1 && data &&\r
-        (unsigned)i0 < (unsigned)dim[0].size );\r
-    return data + i0*dim[0].step;\r
-}\r
-\r
-inline uchar* MatND::ptr(int i0, int i1)\r
-{\r
-    CV_DbgAssert( dims == 2 && data &&\r
-        (unsigned)i0 < (unsigned)dim[0].size &&\r
-        (unsigned)i1 < (unsigned)dim[1].size );\r
-    return data + i0*dim[0].step + i1*dim[1].step;\r
-}\r
-\r
-inline const uchar* MatND::ptr(int i0, int i1) const\r
-{\r
-    CV_DbgAssert( dims == 2 && data &&\r
-        (unsigned)i0 < (unsigned)dim[0].size &&\r
-        (unsigned)i1 < (unsigned)dim[1].size );\r
-    return data + i0*dim[0].step + i1*dim[1].step;\r
-}\r
-\r
-inline uchar* MatND::ptr(int i0, int i1, int i2)\r
-{\r
-    CV_DbgAssert( dims == 3 && data &&\r
-        (unsigned)i0 < (unsigned)dim[0].size &&\r
-        (unsigned)i1 < (unsigned)dim[1].size &&\r
-        (unsigned)i2 < (unsigned)dim[2].size );\r
-    return data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step;\r
-}\r
-\r
-inline const uchar* MatND::ptr(int i0, int i1, int i2) const\r
-{\r
-    CV_DbgAssert( dims == 3 && data &&\r
-        (unsigned)i0 < (unsigned)dim[0].size &&\r
-        (unsigned)i1 < (unsigned)dim[1].size &&\r
-        (unsigned)i2 < (unsigned)dim[2].size );\r
-    return data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step;\r
-}\r
-\r
-inline uchar* MatND::ptr(const int* idx)\r
-{\r
-    int i, d = dims;\r
-    uchar* p = data;\r
-    CV_DbgAssert( data );\r
-    for( i = 0; i < d; i++ )\r
-    {\r
-        CV_DbgAssert( (unsigned)idx[i] < (unsigned)dim[i].size );\r
-        p += idx[i]*dim[i].step;\r
-    }\r
-    return p;\r
-}\r
-\r
-inline const uchar* MatND::ptr(const int* idx) const\r
-{\r
-    int i, d = dims;\r
-    uchar* p = data;\r
-    CV_DbgAssert( data );\r
-    for( i = 0; i < d; i++ )\r
-    {\r
-        CV_DbgAssert( (unsigned)idx[i] < (unsigned)dim[i].size );\r
-        p += idx[i]*dim[i].step;\r
-    }\r
-    return p;\r
-}\r
-\r
-template<typename _Tp> inline _Tp& MatND::at(int i0)\r
-{ return *(_Tp*)ptr(i0); }\r
-template<typename _Tp> inline const _Tp& MatND::at(int i0) const\r
-{ return *(const _Tp*)ptr(i0); }\r
-template<typename _Tp> inline _Tp& MatND::at(int i0, int i1)\r
-{ return *(_Tp*)ptr(i0, i1); }\r
-template<typename _Tp> inline const _Tp& MatND::at(int i0, int i1) const\r
-{ return *(const _Tp*)ptr(i0, i1); }\r
-template<typename _Tp> inline _Tp& MatND::at(int i0, int i1, int i2)\r
-{ return *(_Tp*)ptr(i0, i1, i2); }\r
-template<typename _Tp> inline const _Tp& MatND::at(int i0, int i1, int i2) const\r
-{ return *(const _Tp*)ptr(i0, i1, i2); }\r
-template<typename _Tp> inline _Tp& MatND::at(const int* idx)\r
-{ return *(_Tp*)ptr(idx); }\r
-template<typename _Tp> inline const _Tp& MatND::at(const int* idx) const\r
-{ return *(const _Tp*)ptr(idx); }\r
-\r
-inline NAryMatNDIterator::NAryMatNDIterator()\r
-{\r
-}\r
-\r
-inline void subtract(const MatND& a, const Scalar& s, MatND& c, const MatND& mask=MatND())\r
-{\r
-    add(a, -s, c, mask);\r
-}\r
-\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_()\r
-{\r
-    flags = MAGIC_VAL | DataType<_Tp>::type;\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_(const Vector<int>& _sizes)\r
-: MatND(_sizes, DataType<_Tp>::type)\r
-{\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_(const Vector<int>& _sizes, const _Tp& _s)\r
-: MatND(_sizes, DataType<_Tp>::type, Scalar(_s))\r
-{\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_(const MatND& m)\r
-{\r
-    if( m.type() == DataType<_Tp>::type )\r
-        *this = (const MatND_<_Tp>&)m;\r
-    else\r
-        m.convertTo(this, DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_(const MatND_<_Tp>& m) : MatND(m)\r
-{\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_(const MatND_<_Tp>& m, const Vector<Range>& ranges)\r
-: MatND(m, ranges)\r
-{\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>::MatND_(const CvMatND* m, bool copyData)\r
-{\r
-    *this = MatND(m, copyData || CV_MAT_TYPE(m->type) != DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>& MatND_<_Tp>::operator = (const MatND& m)\r
-{\r
-    if( DataType<_Tp>::type == m.type() )\r
-    {\r
-        Mat::operator = (m);\r
-        return *this;\r
-    }\r
-    if( DataType<_Tp>::depth == m.depth() )\r
-    {\r
-        return (*this = m.reshape(DataType<_Tp>::channels));\r
-    }\r
-    CV_DbgAssert(DataType<_Tp>::channels == m.channels());\r
-    m.convertTo(*this, DataType<_Tp>::type);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>& MatND_<_Tp>::operator = (const MatND_<_Tp>& m)\r
-{\r
-    return ((MatND&)*this = m);\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>& MatND_<_Tp>::operator = (const _Tp& s)\r
-{\r
-    return (MatND&)*this = Scalar(s);\r
-}\r
-\r
-template<typename _Tp> inline void MatND_<_Tp>::create(const Vector<int>& _sizes)\r
-{\r
-    MatND::create(_sizes);\r
-}\r
-\r
-template<typename _Tp> template<typename _Tp2> inline MatND_<_Tp>::operator MatND_<_Tp2>() const\r
-{\r
-    return MatND_<_Tp2>((const MatND&)*this);\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp> MatND_<_Tp>::clone() const\r
-{\r
-    MatND_<_Tp> temp;\r
-    this->copyTo(temp);\r
-    return temp;\r
-}\r
-\r
-template<typename _Tp> inline MatND_<_Tp>\r
-MatND_<_Tp>::operator()(const Vector<Range>& ranges) const\r
-{ return MatND_<_Tp>(*this, ranges); }\r
-\r
-template<typename _Tp> inline size_t MatND_<_Tp>::elemSize() const\r
-{ return CV_ELEM_SIZE(DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline size_t MatND_<_Tp>::elemSize1() const\r
-{ return CV_ELEM_SIZE1(DataType<_Tp>::type); }\r
-\r
-template<typename _Tp> inline int MatND_<_Tp>::type() const\r
-{ return DataType<_Tp>::type; }\r
-\r
-template<typename _Tp> inline int MatND_<_Tp>::depth() const\r
-{ return DataType<_Tp>::depth; }\r
-\r
-template<typename _Tp> inline int MatND_<_Tp>::channels() const\r
-{ return DataType<_Tp>::channels; }\r
-\r
-template<typename _Tp> inline size_t MatND_<_Tp>::stepT(int i) const\r
-{\r
-    CV_DbgAssert( (unsigned)i < (unsigned)dims );\r
-    return dim[i].step/elemSize();\r
-}\r
-\r
-template<typename _Tp> inline size_t MatND_<_Tp>::step1(int i) const\r
-{\r
-    CV_DbgAssert( (unsigned)i < (unsigned)dims );\r
-    return dim[i].step/elemSize1();\r
-}\r
-\r
-template<typename _Tp> inline _Tp& MatND_<_Tp>::operator ()(const int* idx)\r
-{\r
-    uchar* ptr = data;\r
-    int i, d = dims;\r
-    for( i = 0; i < d; i++ )\r
-    {\r
-        int ii = idx[i];\r
-        CV_DbgAssert( (unsigned)ii < (unsigned)dim[i].size );\r
-        ptr += ii*dim[i].step;\r
-    }\r
-    return *(_Tp*)ptr;\r
-}\r
-\r
-template<typename _Tp> inline const _Tp& MatND_<_Tp>::operator ()(const int* idx) const\r
-{\r
-    const uchar* ptr = data;\r
-    int i, d = dims;\r
-    for( i = 0; i < d; i++ )\r
-    {\r
-        int ii = idx[i];\r
-        CV_DbgAssert( (unsigned)ii < (unsigned)dim[i].size );\r
-        ptr += ii*dim[i].step;\r
-    }\r
-    return *(const _Tp*)ptr;\r
-}\r
-\r
-template<typename _Tp> inline _Tp& MatND_<_Tp>::operator ()(int i0, int i1, int i2)\r
-{\r
-    CV_DbgAssert( dims == 3 &&\r
-        (unsigned)i0 < (unsigned)dim[0].size &&\r
-        (unsigned)i1 < (unsigned)dim[1].size &&\r
-        (unsigned)i2 < (unsigned)dim[2].size );\r
-\r
-    return *(_Tp*)(data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step);\r
-}\r
-\r
-template<typename _Tp> inline const _Tp& MatND_<_Tp>::operator ()(int i0, int i1, int i2) const\r
-{\r
-    CV_DbgAssert( dims == 3 &&\r
-        (unsigned)i0 < (unsigned)dim[0].size &&\r
-        (unsigned)i1 < (unsigned)dim[1].size &&\r
-        (unsigned)i2 < (unsigned)dim[2].size );\r
-\r
-    return *(const _Tp*)(data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step);\r
-}\r
-\r
-\r
-//////////////////////////////// SparseMat ////////////////////////////////\r
-\r
-inline SparseMat::SparseMat()\r
-: flags(MAGIC_VAL), hdr(0)\r
-{\r
-}\r
-\r
-inline SparseMat::SparseMat(const Vector<int>& _sizes, int _type)\r
-: flags(MAGIC_VAL), hdr(0)\r
-{\r
-    create(_sizes, _type);\r
-}\r
-\r
-inline SparseMat::SparseMat(const SparseMat& m)\r
-: flags(m.flags), hdr(m.hdr)\r
-{\r
-    addref();\r
-}\r
-\r
-inline SparseMat::~SparseMat()\r
-{\r
-    release();\r
-}\r
-\r
-inline SparseMat& SparseMat::operator = (const SparseMat& m)\r
-{\r
-    if( this != &m )\r
-    {\r
-        if( m.hdr )\r
-            ++m.hdr->refcount;\r
-        release();\r
-        flags = m.flags;\r
-        hdr = m.hdr;\r
-    }\r
-    return *this;\r
-}\r
-\r
-inline SparseMat& SparseMat::operator = (const Mat& m)\r
-{ return (*this = SparseMat(m)); }\r
-\r
-inline SparseMat& SparseMat::operator = (const MatND& m)\r
-{ return (*this = SparseMat(m)); }\r
-\r
-inline SparseMat SparseMat::clone() const\r
-{\r
-    SparseMat temp;\r
-    this->copyTo(temp);\r
-    return temp;\r
-}\r
-\r
-\r
-inline void SparseMat::assignTo( SparseMat& m, int type ) const\r
-{\r
-    if( type < 0 )\r
-        m = *this;\r
-    else\r
-        convertTo(m, type);\r
-}\r
-\r
-inline void SparseMat::addref()\r
-{ if( hdr ) ++hdr->refcount; }\r
-\r
-inline void SparseMat::release()\r
-{\r
-    if( hdr && --hdr->refcount == 0 )\r
-        delete hdr;\r
-    hdr = 0;\r
-}\r
-\r
-inline size_t SparseMat::elemSize() const\r
-{ return CV_ELEM_SIZE(flags); }\r
-\r
-inline size_t SparseMat::elemSize1() const\r
-{ return CV_ELEM_SIZE1(flags); }\r
-\r
-inline int SparseMat::type() const\r
-{ return CV_MAT_TYPE(flags); }\r
-\r
-inline int SparseMat::depth() const\r
-{ return CV_MAT_DEPTH(flags); }\r
-\r
-inline int SparseMat::channels() const\r
-{ return CV_MAT_CN(flags); }\r
-\r
-inline Vector<int> SparseMat::size() const\r
-{\r
-    if( !hdr )\r
-        return Vector<int>();\r
-    return Vector<int>(hdr->size, hdr->dims, true);\r
-}\r
-\r
-inline int SparseMat::size(int i) const\r
-{\r
-    if( hdr )\r
-    {\r
-        CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);\r
-        return hdr->size[i];\r
-    }\r
-    return 0;\r
-}\r
-\r
-inline int SparseMat::dims() const\r
-{\r
-    return hdr ? hdr->dims : 0;\r
-}\r
-\r
-inline size_t SparseMat::nzcount() const\r
-{\r
-    return hdr ? hdr->nodeCount : 0;\r
-}\r
-\r
-inline size_t SparseMat::hash(int i0) const\r
-{\r
-    return (size_t)i0;\r
-}\r
-\r
-inline size_t SparseMat::hash(int i0, int i1) const\r
-{\r
-    return (size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1;\r
-}\r
-\r
-inline size_t SparseMat::hash(int i0, int i1, int i2) const\r
-{\r
-    return ((size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1)*HASH_SCALE + (unsigned)i2;\r
-}\r
-\r
-inline size_t SparseMat::hash(const int* idx) const\r
-{\r
-    size_t h = (unsigned)idx[0];\r
-    if( !hdr )\r
-        return 0;\r
-    int i, d = hdr->dims;\r
-    for( i = 1; i < d; i++ )\r
-        h = h*HASH_SCALE + (unsigned)idx[i];\r
-    return h;\r
-}\r
-\r
-template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, size_t* hashval)\r
-{ return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); }\r
-\r
-template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)\r
-{ return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); }\r
-\r
-template<typename _Tp> inline _Tp& SparseMat::ref(const int* idx, size_t* hashval)\r
-{ return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval); }\r
-\r
-template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, size_t* hashval) const\r
-{\r
-    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);\r
-    return p ? *p : _Tp();\r
-}\r
-\r
-template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const\r
-{\r
-    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);\r
-    return p ? *p : _Tp();\r
-}\r
-\r
-template<typename _Tp> inline _Tp SparseMat::value(const int* idx, size_t* hashval) const\r
-{\r
-    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);\r
-    return p ? *p : _Tp();\r
-}\r
-\r
-template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const\r
-{ return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); }\r
-\r
-template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const\r
-{ return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); }\r
-\r
-template<typename _Tp> inline const _Tp* SparseMat::find(const int* idx, size_t* hashval) const\r
-{ return (const _Tp*)((SparseMat*)this)->ptr(idx, true, hashval); }\r
-\r
-template<typename _Tp> inline _Tp& SparseMat::value(Node* n)\r
-{ return *(_Tp*)((uchar*)n + hdr->valueOffset); }\r
-\r
-template<typename _Tp> inline const _Tp& SparseMat::value(const Node* n) const\r
-{ return *(const _Tp*)((const uchar*)n + hdr->valueOffset); }\r
-\r
-inline SparseMat::Node* SparseMat::node(size_t nidx)\r
-{ return (Node*)&hdr->pool[nidx]; }\r
-\r
-inline const SparseMat::Node* SparseMat::node(size_t nidx) const\r
-{ return (const Node*)&hdr->pool[nidx]; }\r
-\r
-inline SparseMatIterator SparseMat::begin()\r
-{ return SparseMatIterator(this); }\r
-\r
-inline SparseMatConstIterator SparseMat::begin() const\r
-{ return SparseMatConstIterator(this); }\r
-\r
-inline SparseMatIterator SparseMat::end()\r
-{\r
-    SparseMatIterator it;\r
-    it.m = this;\r
-    if( hdr )\r
-    {\r
-        it.hashidx = hdr->hashtab.size();\r
-        it.ptr = 0;\r
-    }\r
-    return it;\r
-}\r
-\r
-inline SparseMatConstIterator SparseMat::end() const\r
-{\r
-    SparseMatConstIterator it;\r
-    it.m = this;\r
-    if( hdr )\r
-    {\r
-        it.hashidx = hdr->hashtab.size();\r
-        it.ptr = 0;\r
-    }\r
-    return it;\r
-}\r
-\r
-inline SparseMatConstIterator::SparseMatConstIterator()\r
-: m(0), hashidx(0), ptr(0)\r
-{\r
-}\r
-\r
-inline SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)\r
-: m(it.m), hashidx(it.hashidx), ptr(it.ptr)\r
-{\r
-}\r
-\r
-static inline bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)\r
-{ return it1.m == it2.m && it1.hashidx == it2.hashidx && it1.ptr == it2.ptr; }\r
-\r
-static inline bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)\r
-{ return !(it1 == it2); }\r
-\r
-\r
-inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)\r
-{\r
-    if( this != &it )\r
-    {\r
-        m = it.m;\r
-        hashidx = it.hashidx;\r
-        ptr = it.ptr;\r
-    }\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline const _Tp& SparseMatConstIterator::value() const\r
-{ return *(_Tp*)ptr; }\r
-\r
-inline const SparseMat::Node* SparseMatConstIterator::node() const\r
-{\r
-    return ptr && m && m->hdr ?\r
-        (const SparseMat::Node*)(ptr - m->hdr->valueOffset) : 0;\r
-}\r
-\r
-inline SparseMatConstIterator SparseMatConstIterator::operator ++(int)\r
-{\r
-    SparseMatConstIterator it = *this;\r
-    ++*this;\r
-    return it;\r
-}\r
-\r
-inline SparseMatIterator::SparseMatIterator()\r
-{}\r
-\r
-inline SparseMatIterator::SparseMatIterator(SparseMat* _m)\r
-: SparseMatConstIterator(_m)\r
-{}\r
-\r
-inline SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)\r
-: SparseMatConstIterator(it)\r
-{\r
-}\r
-\r
-inline SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)\r
-{\r
-    (SparseMatConstIterator&)*this = it;\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline _Tp& SparseMatIterator::value() const\r
-{ return *(_Tp*)ptr; }\r
-\r
-inline SparseMat::Node* SparseMatIterator::node() const\r
-{\r
-    return (SparseMat::Node*)SparseMatConstIterator::node();\r
-}\r
-\r
-inline SparseMatIterator& SparseMatIterator::operator ++()\r
-{\r
-    SparseMatConstIterator::operator ++();\r
-    return *this;\r
-}\r
-\r
-inline SparseMatIterator SparseMatIterator::operator ++(int)\r
-{\r
-    SparseMatIterator it = *this;\r
-    ++*this;\r
-    return it;\r
-}\r
-\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_()\r
-{ flags = MAGIC_VAL | DataType<_Tp>::type; }\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Vector<int>& _sizes)\r
-: SparseMat(_sizes, DataType<_Tp>::type)\r
-{}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat& m)\r
-{\r
-    if( m.type() == DataType<_Tp>::type )\r
-        *this = (const SparseMat_<_Tp>&)m;\r
-    else\r
-        m.convertTo(this, DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)\r
-{\r
-    this->flags = m.flags;\r
-    this->hdr = m.hdr;\r
-    if( this->hdr )\r
-        ++this->hdr->refcount;\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Mat& m)\r
-{\r
-    SparseMat sm(m);\r
-    *this = sm;\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const MatND& m)\r
-{\r
-    SparseMat sm(m);\r
-    *this = sm;\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const CvSparseMat* m)\r
-{\r
-    SparseMat sm(m);\r
-    *this = sm;\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>&\r
-SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)\r
-{\r
-    if( this != &m )\r
-    {\r
-        if( m.hdr ) ++m.hdr->refcount;\r
-        release();\r
-        flags = m.flags;\r
-        hdr = m.hdr;\r
-    }\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>&\r
-SparseMat_<_Tp>::operator = (const SparseMat& m)\r
-{\r
-    if( m.type() == DataType<_Tp>::type )\r
-        return (*this = (const SparseMat_<_Tp>&)m);\r
-    m.convertTo(*this, DataType<_Tp>::type);\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>&\r
-SparseMat_<_Tp>::operator = (const Mat& m)\r
-{ return (*this = SparseMat(m)); }\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>&\r
-SparseMat_<_Tp>::operator = (const MatND& m)\r
-{ return (*this = SparseMat(m)); }\r
-\r
-template<typename _Tp> inline SparseMat_<_Tp>\r
-SparseMat_<_Tp>::clone() const\r
-{\r
-    SparseMat_<_Tp> m;\r
-    this->copyTo(m);\r
-    return m;\r
-}\r
-\r
-template<typename _Tp> inline void\r
-SparseMat_<_Tp>::create(const Vector<int>& _sizes)\r
-{\r
-    SparseMat::create(_sizes, DataType<_Tp>::type);\r
-}\r
-\r
-template<typename _Tp> inline\r
-SparseMat_<_Tp>::operator CvSparseMat*() const\r
-{\r
-    return SparseMat::operator CvSparseMat*();\r
-}\r
-\r
-template<typename _Tp> inline int SparseMat_<_Tp>::type() const\r
-{ return DataType<_Tp>::type; }\r
-\r
-template<typename _Tp> inline int SparseMat_<_Tp>::depth() const\r
-{ return DataType<_Tp>::depth; }\r
-\r
-template<typename _Tp> inline int SparseMat_<_Tp>::channels() const\r
-{ return DataType<_Tp>::channels; }\r
-\r
-template<typename _Tp> inline _Tp&\r
-SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)\r
-{ return SparseMat::ref<_Tp>(i0, i1, hashval); }\r
-\r
-template<typename _Tp> inline _Tp\r
-SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const\r
-{ return SparseMat::value<_Tp>(i0, i1, hashval); }\r
-\r
-template<typename _Tp> inline _Tp&\r
-SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)\r
-{ return SparseMat::ref<_Tp>(i0, i1, i2, hashval); }\r
-\r
-template<typename _Tp> inline _Tp\r
-SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const\r
-{ return SparseMat::value<_Tp>(i0, i1, i2, hashval); }\r
-\r
-template<typename _Tp> inline _Tp&\r
-SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)\r
-{ return SparseMat::ref<_Tp>(idx, hashval); }\r
-\r
-template<typename _Tp> inline _Tp\r
-SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const\r
-{ return SparseMat::value<_Tp>(idx, hashval); }\r
-\r
-template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()\r
-{ return SparseMatIterator_<_Tp>(this); }\r
-\r
-template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const\r
-{ return SparseMatConstIterator_<_Tp>(this); }\r
-\r
-template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()\r
-{\r
-    SparseMatIterator_<_Tp> it;\r
-    it.m = this;\r
-    if( hdr )\r
-    {\r
-        it.hashidx = hdr->hashtab.size();\r
-        it.ptr = 0;\r
-    }\r
-    return it;\r
-}\r
-\r
-template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const\r
-{\r
-    SparseMatConstIterator_<_Tp> it;\r
-    it.m = this;\r
-    if( hdr )\r
-    {\r
-        it.hashidx = hdr->hashtab.size();\r
-        it.ptr = 0;\r
-    }\r
-    return it;\r
-}\r
-\r
-\r
-template<typename _Tp> inline\r
-SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()\r
-{}\r
-\r
-template<typename _Tp> inline\r
-SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)\r
-: SparseMatConstIterator(_m)\r
-{}\r
-\r
-template<typename _Tp> inline\r
-SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)\r
-: SparseMatConstIterator(it)\r
-{}\r
-\r
-template<typename _Tp> inline SparseMatConstIterator_<_Tp>&\r
-SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)\r
-{ return ((SparseMatConstIterator&)*this = it); }\r
-\r
-template<typename _Tp> inline const _Tp&\r
-SparseMatConstIterator_<_Tp>::operator *() const\r
-{ return *(const _Tp*)this->ptr; }\r
-\r
-template<typename _Tp> inline SparseMatConstIterator_<_Tp>&\r
-SparseMatConstIterator_<_Tp>::operator ++()\r
-{\r
-    SparseMatConstIterator::operator ++();\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline SparseMatConstIterator_<_Tp>\r
-SparseMatConstIterator_<_Tp>::operator ++(int)\r
-{\r
-    SparseMatConstIterator it = *this;\r
-    SparseMatConstIterator::operator ++();\r
-    return it;\r
-}\r
-\r
-template<typename _Tp> inline\r
-SparseMatIterator_<_Tp>::SparseMatIterator_()\r
-{}\r
-\r
-template<typename _Tp> inline\r
-SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)\r
-: SparseMatConstIterator_<_Tp>(_m)\r
-{}\r
-\r
-template<typename _Tp> inline\r
-SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)\r
-: SparseMatConstIterator_<_Tp>(it)\r
-{}\r
-\r
-template<typename _Tp> inline SparseMatIterator_<_Tp>&\r
-SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)\r
-{ return ((SparseMatIterator&)*this = it); }\r
-\r
-template<typename _Tp> inline _Tp&\r
-SparseMatIterator_<_Tp>::operator *() const\r
-{ return *(_Tp*)this->ptr; }\r
-\r
-template<typename _Tp> inline SparseMatIterator_<_Tp>&\r
-SparseMatIterator_<_Tp>::operator ++()\r
-{\r
-    SparseMatConstIterator::operator ++();\r
-    return *this;\r
-}\r
-\r
-template<typename _Tp> inline SparseMatIterator_<_Tp>\r
-SparseMatIterator_<_Tp>::operator ++(int)\r
-{\r
-    SparseMatIterator it = *this;\r
-    SparseMatConstIterator::operator ++();\r
-    return it;\r
-}\r
-    \r
-}\r
-\r
-#endif\r
-#endif\r
+/*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, 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*/
+
+#ifndef _OPENCV_CORE_MAT_OPERATIONS_H_
+#define _OPENCV_CORE_MAT_OPERATIONS_H_
+
+#ifndef SKIP_INCLUDES
+#include <limits.h>
+#endif // SKIP_INCLUDES
+
+#ifdef __cplusplus
+
+namespace cv
+{
+
+//////////////////////////////// Mat ////////////////////////////////
+
+inline Mat::Mat()
+    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0) {}
+
+inline Mat::Mat(int _rows, int _cols, int _type)
+    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
+{
+    if( _rows > 0 && _cols > 0 )
+        create( _rows, _cols, _type );
+}
+
+inline Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s)
+    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0),
+    datastart(0), dataend(0)
+{
+    create(_rows, _cols, _type);
+    *this = _s;
+}
+
+inline Mat::Mat(Size _size, int _type)
+    : flags(0), rows(0), cols(0), step(0), data(0), refcount(0),
+    datastart(0), dataend(0)
+{
+    if( _size.height > 0 && _size.width > 0 )
+        create( _size.height, _size.width, _type );
+}
+
+inline Mat::Mat(const Mat& m)
+    : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data),
+    refcount(m.refcount), datastart(m.datastart), dataend(m.dataend)
+{
+    if( refcount )
+        ++*refcount;
+}
+
+inline Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)
+    : flags(MAGIC_VAL + (_type & TYPE_MASK)), rows(_rows), cols(_cols),
+    step(_step), data((uchar*)_data), refcount(0),
+    datastart((uchar*)_data), dataend((uchar*)_data)
+{
+    size_t minstep = cols*elemSize();
+    if( step == AUTO_STEP )
+    {
+        step = minstep;
+        flags |= CONTINUOUS_FLAG;
+    }
+    else
+    {
+        if( rows == 1 ) step = minstep;
+        CV_DbgAssert( step >= minstep );
+        flags |= step == minstep ? CONTINUOUS_FLAG : 0;
+    }
+    dataend += step*(rows-1) + minstep;
+}
+
+inline Mat::Mat(Size _size, int _type, void* _data, size_t _step)
+    : flags(MAGIC_VAL + (_type & TYPE_MASK)), rows(_size.height), cols(_size.width),
+    step(_step), data((uchar*)_data), refcount(0),
+    datastart((uchar*)_data), dataend((uchar*)_data)
+{
+    size_t minstep = cols*elemSize();
+    if( step == AUTO_STEP )
+    {
+        step = minstep;
+        flags |= CONTINUOUS_FLAG;
+    }
+    else
+    {
+        if( rows == 1 ) step = minstep;
+        CV_DbgAssert( step >= minstep );
+        flags |= step == minstep ? CONTINUOUS_FLAG : 0;
+    }
+    dataend += step*(rows-1) + minstep;
+}
+
+inline Mat::Mat(const Mat& m, const Range& rowRange, const Range& colRange)
+{
+    flags = m.flags;
+    step = m.step; refcount = m.refcount;
+    data = m.data; datastart = m.datastart; dataend = m.dataend;
+
+    if( rowRange == Range::all() )
+        rows = m.rows;
+    else
+    {
+        CV_Assert( 0 <= rowRange.start && rowRange.start <= rowRange.end && rowRange.end <= m.rows );
+        rows = rowRange.size();
+        data += step*rowRange.start;
+    }
+
+    if( colRange == Range::all() )
+        cols = m.cols;
+    else
+    {
+        CV_Assert( 0 <= colRange.start && colRange.start <= colRange.end && colRange.end <= m.cols );
+        cols = colRange.size();
+        data += colRange.start*elemSize();
+        flags &= cols < m.cols ? ~CONTINUOUS_FLAG : -1;
+    }
+
+    if( rows == 1 )
+        flags |= CONTINUOUS_FLAG;
+
+    if( refcount )
+        ++*refcount;
+    if( rows <= 0 || cols <= 0 )
+        rows = cols = 0;
+}
+
+inline Mat::Mat(const Mat& m, const Rect& roi)
+    : flags(m.flags), rows(roi.height), cols(roi.width),
+    step(m.step), data(m.data + roi.y*step), refcount(m.refcount),
+    datastart(m.datastart), dataend(m.dataend)
+{
+    flags &= roi.width < m.cols ? ~CONTINUOUS_FLAG : -1;
+    data += roi.x*elemSize();
+    CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols &&
+        0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows );
+    if( refcount )
+        ++*refcount;
+    if( rows <= 0 || cols <= 0 )
+        rows = cols = 0;
+}
+
+inline Mat::Mat(const CvMat* m, bool copyData)
+    : flags(MAGIC_VAL + (m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG))),
+    rows(m->rows), cols(m->cols), step(m->step), data(m->data.ptr), refcount(0),
+    datastart(m->data.ptr), dataend(m->data.ptr)
+{
+    if( step == 0 )
+        step = cols*elemSize();
+    size_t minstep = cols*elemSize();
+    dataend += step*(rows-1) + minstep;
+    if( copyData )
+    {
+        data = datastart = dataend = 0;
+        Mat(m->rows, m->cols, m->type, m->data.ptr, m->step).copyTo(*this);
+    }
+}
+
+inline Mat::~Mat()
+{
+    release();
+}
+
+inline Mat& Mat::operator = (const Mat& m)
+{
+    if( this != &m )
+    {
+        if( m.refcount )
+            ++*m.refcount;
+        release();
+        flags = m.flags;
+        rows = m.rows; cols = m.cols;
+        step = m.step; data = m.data;
+        datastart = m.datastart; dataend = m.dataend;
+        refcount = m.refcount;
+    }
+    return *this;
+}
+
+inline Mat Mat::row(int y) const { return Mat(*this, Range(y, y+1), Range::all()); }
+inline Mat Mat::col(int x) const { return Mat(*this, Range::all(), Range(x, x+1)); }
+inline Mat Mat::rowRange(int startrow, int endrow) const
+    { return Mat(*this, Range(startrow, endrow), Range::all()); }
+inline Mat Mat::rowRange(const Range& r) const
+    { return Mat(*this, r, Range::all()); }
+inline Mat Mat::colRange(int startcol, int endcol) const
+    { return Mat(*this, Range::all(), Range(startcol, endcol)); }
+inline Mat Mat::colRange(const Range& r) const
+    { return Mat(*this, Range::all(), r); }
+
+inline Mat Mat::diag(int d) const
+{
+    Mat m = *this;
+    size_t esz = elemSize();
+    int len;
+
+    if( d >= 0 )
+    {
+        len = std::min(cols - d, rows);
+        m.data += esz*d;
+    }
+    else
+    {
+        len = std::min(rows + d, cols);
+        m.data -= step*d;
+    }
+    CV_DbgAssert( len > 0 );
+    m.rows = len;
+    m.cols = 1;
+    m.step += esz;
+    if( m.rows > 1 )
+        m.flags &= ~CONTINUOUS_FLAG;
+    else
+        m.flags |= CONTINUOUS_FLAG;
+    return m;
+}
+
+inline Mat Mat::diag(const Mat& d)
+{
+    Mat m(d.rows, d.rows, d.type(), Scalar(0)), md = m.diag();
+    d.copyTo(md);
+    return m;
+}
+
+inline Mat Mat::clone() const
+{
+    Mat m;
+    copyTo(m);
+    return m;
+}
+
+inline void Mat::assignTo( Mat& m, int type ) const
+{
+    if( type < 0 )
+        m = *this;
+    else
+        convertTo(m, type);
+}
+
+inline void Mat::create(int _rows, int _cols, int _type)
+{
+    _type &= TYPE_MASK;
+    if( rows == _rows && cols == _cols && type() == _type && data )
+        return;
+    if( data )
+        release();
+    CV_DbgAssert( _rows >= 0 && _cols >= 0 );
+    if( _rows > 0 && _cols > 0 )
+    {
+        flags = MAGIC_VAL + CONTINUOUS_FLAG + _type;
+        rows = _rows;
+        cols = _cols;
+        step = elemSize()*cols;
+        int64 _nettosize = (int64)step*rows;
+        size_t nettosize = (size_t)_nettosize;
+        if( _nettosize != (int64)nettosize )
+            CV_Error(CV_StsNoMem, "Too big buffer is allocated");
+        size_t datasize = alignSize(nettosize, (int)sizeof(*refcount));
+        datastart = data = (uchar*)fastMalloc(datasize + sizeof(*refcount));
+        dataend = data + nettosize;
+        refcount = (int*)(data + datasize);
+        *refcount = 1;
+    }
+}
+
+inline void Mat::create(Size _size, int _type)
+{
+    create(_size.height, _size.width, _type);
+}
+
+inline void Mat::addref()
+{ if( refcount ) ++*refcount; }
+
+inline void Mat::release()
+{
+    if( refcount && --*refcount == 0 )
+        fastFree(datastart);
+    data = datastart = dataend = 0;
+    step = rows = cols = 0;
+    refcount = 0;
+}
+
+inline void Mat::locateROI( Size& wholeSize, Point& ofs ) const
+{
+    size_t esz = elemSize(), minstep;
+    ptrdiff_t delta1 = data - datastart, delta2 = dataend - datastart;
+    CV_DbgAssert( step > 0 );
+    if( delta1 == 0 )
+        ofs.x = ofs.y = 0;
+    else
+    {
+        ofs.y = (int)(delta1/step);
+        ofs.x = (int)((delta1 - step*ofs.y)/esz);
+        CV_DbgAssert( data == datastart + ofs.y*step + ofs.x*esz );
+    }
+    minstep = (ofs.x + cols)*esz;
+    wholeSize.height = (int)((delta2 - minstep)/step + 1);
+    wholeSize.height = std::max(wholeSize.height, ofs.y + rows);
+    wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);
+    wholeSize.width = std::max(wholeSize.width, ofs.x + cols);
+}
+
+inline Mat& Mat::adjustROI( int dtop, int dbottom, int dleft, int dright )
+{
+    Size wholeSize; Point ofs;
+    size_t esz = elemSize();
+    locateROI( wholeSize, ofs );
+    int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
+    int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
+    data += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
+    rows = row2 - row1; cols = col2 - col1;
+    if( esz*cols == step || rows == 1 )
+        flags |= CONTINUOUS_FLAG;
+    else
+        flags &= ~CONTINUOUS_FLAG;
+    return *this;
+}
+
+inline Mat Mat::operator()( Range rowRange, Range colRange ) const
+{
+    return Mat(*this, rowRange, colRange);
+}
+
+inline Mat Mat::operator()( const Rect& roi ) const
+{ return Mat(*this, roi); }
+
+inline Mat::operator CvMat() const
+{
+    CvMat m = cvMat(rows, cols, type(), data);
+    m.step = (int)step;
+    m.type = (m.type & ~CONTINUOUS_FLAG) | (flags & CONTINUOUS_FLAG);
+    return m;
+}
+
+inline Mat::operator IplImage() const
+{
+    IplImage img;
+    cvInitImageHeader(&img, size(), cvIplDepth(flags), channels());
+    cvSetData(&img, data, (int)step);
+    return img;
+}
+
+inline bool Mat::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; }
+inline size_t Mat::elemSize() const { return CV_ELEM_SIZE(flags); }
+inline size_t Mat::elemSize1() const { return CV_ELEM_SIZE1(flags); }
+inline int Mat::type() const { return CV_MAT_TYPE(flags); }
+inline int Mat::depth() const { return CV_MAT_DEPTH(flags); }
+inline int Mat::channels() const { return CV_MAT_CN(flags); }
+inline size_t Mat::step1() const { return step/elemSize1(); }
+inline Size Mat::size() const { return Size(cols, rows); }
+inline bool Mat::empty() const { return data == 0; }
+
+inline uchar* Mat::ptr(int y)
+{
+    CV_DbgAssert( (unsigned)y < (unsigned)rows );
+    return data + step*y;
+}
+
+inline const uchar* Mat::ptr(int y) const
+{
+    CV_DbgAssert( (unsigned)y < (unsigned)rows );
+    return data + step*y;
+}
+
+template<typename _Tp> inline _Tp* Mat::ptr(int y)
+{
+    CV_DbgAssert( (unsigned)y < (unsigned)rows );
+    return (_Tp*)(data + step*y);
+}
+
+template<typename _Tp> inline const _Tp* Mat::ptr(int y) const
+{
+    CV_DbgAssert( (unsigned)y < (unsigned)rows );
+    return (const _Tp*)(data + step*y);
+}
+
+template<typename _Tp> inline _Tp& Mat::at(int y, int x)
+{
+    CV_DbgAssert( (unsigned)y < (unsigned)rows && (unsigned)x < (unsigned)cols &&
+        sizeof(_Tp) == elemSize() );
+    return ((_Tp*)(data + step*y))[x];
+}
+
+template<typename _Tp> inline const _Tp& Mat::at(int y, int x) const
+{
+    CV_DbgAssert( (unsigned)y < (unsigned)rows && (unsigned)x < (unsigned)cols &&
+        sizeof(_Tp) == elemSize() );
+    return ((_Tp*)(data + step*y))[x];
+}
+
+    
+template<typename _Tp> inline MatConstIterator_<_Tp> Mat::begin() const
+{
+    CV_DbgAssert( elemSize() == sizeof(_Tp) );
+    return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp> Mat::end() const
+{
+    CV_DbgAssert( elemSize() == sizeof(_Tp) );
+    MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
+    it.ptr = it.sliceEnd = (_Tp*)(data + step*(rows-1)) + cols;
+    return it;
+}
+
+template<typename _Tp> inline MatIterator_<_Tp> Mat::begin()
+{
+    CV_DbgAssert( elemSize() == sizeof(_Tp) );
+    return MatIterator_<_Tp>((Mat_<_Tp>*)this);
+}
+
+template<typename _Tp> inline MatIterator_<_Tp> Mat::end()
+{
+    CV_DbgAssert( elemSize() == sizeof(_Tp) );
+    MatIterator_<_Tp> it((Mat_<_Tp>*)this);
+    it.ptr = it.sliceEnd = (_Tp*)(data + step*(rows-1)) + cols;
+    return it;
+}
+    
+    
+static inline void swap( Mat& a, Mat& b )
+{
+    std::swap( a.flags, b.flags );
+    std::swap( a.rows, b.rows ); std::swap( a.cols, b.cols );
+    std::swap( a.step, b.step ); std::swap( a.data, b.data );
+    std::swap( a.datastart, b.datastart );
+    std::swap( a.dataend, b.dataend );
+    std::swap( a.refcount, b.refcount );
+}
+
+inline SVD::SVD() {}
+inline SVD::SVD( const Mat& m, int flags ) { operator ()(m, flags); }
+inline void SVD::solveZ( const Mat& m, Mat& dst )
+{
+    SVD svd(m);
+    svd.vt.row(svd.vt.rows-1).reshape(1,svd.vt.cols).copyTo(dst);
+}
+
+///////////////////////////////// Mat_<_Tp> ////////////////////////////////////
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_() :
+    Mat() { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; }
+    
+template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols) :
+    Mat(_rows, _cols, DataType<_Tp>::type) {}
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value) :
+    Mat(_rows, _cols, DataType<_Tp>::type) { *this = value; }
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _size) :
+    Mat(_size.height, _size.width, DataType<_Tp>::type) {}
+    
+template<typename _Tp> inline Mat_<_Tp>::Mat_(Size _size, const _Tp& value) :
+    Mat(_size.height, _size.width, DataType<_Tp>::type) { *this = value; }
+    
+template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat& m) : Mat()
+{ flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type; *this = m; }
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m) : Mat(m) {}
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t _step)
+    : Mat(_rows, _cols, DataType<_Tp>::type, _data, _step) {}
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Range& rowRange, const Range& colRange)
+    : Mat(m, rowRange, colRange) {}
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
+    : Mat(m, roi) {}
+
+template<typename _Tp> template<int n> inline Mat_<_Tp>::Mat_(const Vec<_Tp, n>& vec)
+    : Mat(n, 1, DataType<_Tp>::type)
+{
+    _Tp* d = (_Tp*)data;
+    for( int i = 0; i < n; i++ )
+        d[i] = vec[i];
+}
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(const Vector<_Tp>& vec)
+    : Mat((int)vec.size(), 1, DataType<_Tp>::type, (void*)&vec[0])
+{}
+
+template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
+{
+    if( DataType<_Tp>::type == m.type() )
+    {
+        Mat::operator = (m);
+        return *this;
+    }
+    if( DataType<_Tp>::depth == m.depth() )
+    {
+        return (*this = m.reshape(DataType<_Tp>::channels));
+    }
+    CV_DbgAssert(DataType<_Tp>::channels == m.channels());
+    m.convertTo(*this, type());
+    return *this;
+}
+
+template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
+{
+    Mat::operator=(m);
+    return *this;
+}
+
+template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
+{
+    Mat::operator=(Scalar(s));
+    return *this;
+}
+
+template<typename _Tp> inline void Mat_<_Tp>::create(int _rows, int _cols)
+{
+    Mat::create(_rows, _cols, DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline void Mat_<_Tp>::create(Size _size)
+{
+    Mat::create(_size, DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
+{ return Mat_<_Tp>(Mat::cross(m)); }
+
+template<typename _Tp> template<typename T2> inline Mat_<_Tp>::operator Mat_<T2>() const
+{ return Mat_<T2>(*this); }
+
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::row(int y) const
+{ return Mat_(*this, Range(y, y+1), Range::all()); }
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::col(int x) const
+{ return Mat_(*this, Range::all(), Range(x, x+1)); }
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::diag(int d) const
+{ return Mat_(Mat::diag(d)); }
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::clone() const
+{ return Mat_(Mat::clone()); }
+
+template<typename _Tp> inline size_t Mat_<_Tp>::elemSize() const
+{
+    CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
+    return sizeof(_Tp);
+}
+
+template<typename _Tp> inline size_t Mat_<_Tp>::elemSize1() const
+{
+    CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp)/DataType<_Tp>::channels );
+    return sizeof(_Tp)/DataType<_Tp>::channels;
+}
+template<typename _Tp> inline int Mat_<_Tp>::type() const
+{
+    CV_DbgAssert( Mat::type() == DataType<_Tp>::type );
+    return DataType<_Tp>::type;
+}
+template<typename _Tp> inline int Mat_<_Tp>::depth() const
+{
+    CV_DbgAssert( Mat::depth() == DataType<_Tp>::depth );
+    return DataType<_Tp>::depth;
+}
+template<typename _Tp> inline int Mat_<_Tp>::channels() const
+{
+    CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
+    return DataType<_Tp>::channels;
+}
+template<typename _Tp> inline size_t Mat_<_Tp>::stepT() const { return step/elemSize(); }
+template<typename _Tp> inline size_t Mat_<_Tp>::step1() const { return step/elemSize1(); }
+
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::reshape(int _rows) const
+{ return Mat_<_Tp>(Mat::reshape(0,_rows)); }
+
+template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
+{ return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright));  }
+
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Range& rowRange, const Range& colRange ) const
+{ return Mat_<_Tp>(*this, rowRange, colRange); }
+
+template<typename _Tp> inline Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
+{ return Mat_<_Tp>(roi); }
+
+template<typename _Tp> inline _Tp* Mat_<_Tp>::operator [](int y)
+{ return (_Tp*)ptr(y); }
+template<typename _Tp> inline const _Tp* Mat_<_Tp>::operator [](int y) const
+{ return (const _Tp*)ptr(y); }
+
+template<typename _Tp> inline _Tp& Mat_<_Tp>::operator ()(int row, int col)
+{
+    CV_DbgAssert( (unsigned)row < (unsigned)rows && (unsigned)col < (unsigned)cols );
+    return ((_Tp*)(data + step*row))[col];
+}
+
+template<typename _Tp> inline const _Tp& Mat_<_Tp>::operator ()(int row, int col) const
+{
+    CV_DbgAssert( (unsigned)row < (unsigned)rows && (unsigned)col < (unsigned)cols );
+    return ((const _Tp*)(data + step*row))[col];
+}
+
+template<typename _Tp> inline Mat_<_Tp>::operator Vector<_Tp>() const
+{
+    CV_Assert( rows == 1 || cols == 1 );
+    return isContinuous() ? Vector<_Tp>((size_t)(rows + cols - 1), (_Tp*)data) :
+        (Vector<_Tp>)((Mat_<_Tp>)this->t());
+}
+
+template<typename T1, typename T2, typename Op> inline void
+process( const Mat_<T1>& m1, Mat_<T2>& m2, Op op )
+{
+    int y, x, rows = m1.rows, cols = m1.cols;
+    int c1 = m1.channels(), c2 = m2.channels();
+
+    CV_DbgAssert( m1.size() == m2.size() );
+
+    for( y = 0; y < rows; y++ )
+    {
+        const T1* src = m1[y];
+        T2* dst = m2[y];
+
+        for( x = 0; x < cols; x++ )
+            dst[x] = op(src[x]);
+    }
+}
+
+template<typename T1, typename T2, typename T3, typename Op> inline void
+process( const Mat_<T1>& m1, const Mat_<T2>& m2, Mat_<T3>& m3, Op op )
+{
+    int y, x, rows = m1.rows, cols = m1.cols;
+
+    CV_DbgAssert( m1.size() == m2.size() );
+
+    for( y = 0; y < rows; y++ )
+    {
+        const T1* src1 = m1[y];
+        const T2* src2 = m2[y];
+        T3* dst = m3[y];
+
+        for( x = 0; x < cols; x++ )
+            dst[x] = op( src1[x], src2[x] );
+    }
+}
+
+template<typename M> class CV_EXPORTS MatExpr_Base_
+{
+public:
+    MatExpr_Base_() {}
+    virtual ~MatExpr_Base_() {}
+    virtual void assignTo(M& m, int type=-1) const = 0;
+};
+
+template<typename E, typename M> class CV_EXPORTS MatExpr_ : public MatExpr_Base_<M>
+{
+public:
+    MatExpr_(const E& _e) : e(_e) {}
+    ~MatExpr_() {}
+    operator M() const { return (M)e; }
+    void assignTo(M& m, int type=-1) const { e.assignTo(m, type); }
+
+    M row(int y) const { return ((M)e).row(y); }
+    M col(int x) const { return ((M)e).col(x); }
+    M diag(int d=0) const { return ((M)e).diag(d); }
+
+    M operator()( const Range& rowRange, const Range& colRange ) const
+    { return ((M)e)(rowRange, colRange); }
+    M operator()( const Rect& roi ) const { return ((M)e)(roi); }
+
+    M cross(const M& m) const { return ((M)e).cross(m); }
+    double dot(const M& m) const { return ((M)e).dot(m); }
+
+    MatExpr_<MatExpr_Op2_<M, double, M, MatOp_T_<Mat> >, M> t() const
+    { return ((M)e).t(); }
+    MatExpr_<MatExpr_Op2_<M, int, M, MatOp_Inv_<Mat> >, M> inv(int method=DECOMP_LU) const
+    { return ((M)e).inv(method); }
+
+    MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+    mul(const M& m, double scale=1) const
+    { return ((M)e).mul(m, scale); }
+    template<typename A> MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M >
+    mul(const MatExpr_<A, M>& m, double scale=1) const
+    { return ((M)e).mul(m, scale); }
+
+    E e;
+};
+
+
+inline Mat::Mat(const MatExpr_Base& expr)
+ : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
+{
+    expr.assignTo(*this);
+}
+
+inline Mat& Mat::operator = (const MatExpr_Base& expr)
+{
+    expr.assignTo(*this);
+    return *this;
+}
+
+template<typename _Tp> inline Mat_<_Tp>::Mat_(const MatExpr_Base& e) : Mat()
+{
+    e.assignTo(*this, DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr_Base& e)
+{
+    e.assignTo(*this, DataType<_Tp>::type);
+    return *this;
+}
+
+template<typename _Tp> inline Mat_<_Tp>::operator MatExpr_<Mat_<_Tp>, Mat_<_Tp> >() const
+{ return MatExpr_<Mat_<_Tp>, Mat_<_Tp> >(*this); }
+
+inline Mat::operator MatExpr_<Mat, Mat>() const
+{ return MatExpr_<Mat, Mat>(*this); }
+
+template<typename M> class CV_EXPORTS MatOp_Sub_
+{
+public:
+    MatOp_Sub_() {}
+
+    static void apply(const M& a, const M& b, M& c, int type=-1)
+    {
+        if( type == a.type() || type < 0 )
+        {
+            subtract( a, b, c );
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_Scale_
+{
+public:
+    MatOp_Scale_() {}
+
+    static void apply(const M& a, double alpha, M& c, int type=-1)
+    {
+        a.convertTo(c, type, alpha, 0);
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_ScaleAddS_
+{
+public:
+    MatOp_ScaleAddS_() {}
+
+    static void apply(const M& a, double alpha, double beta, M& c, int type=-1)
+    {
+        a.convertTo(c, type, alpha, beta);
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_AddS_
+{
+public:
+    MatOp_AddS_() {}
+
+    static void apply(const M& a, const Scalar& s, M& c, int type=-1)
+    {
+        if( type == a.type() || type < 0 )
+        {
+            add(a, s, c);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, s, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_AddEx_
+{
+public:
+    MatOp_AddEx_() {}
+
+    static void apply(const M& a, double alpha, const M& b,
+                      double beta, double gamma, M& c, int type=-1)
+    {
+        if( type == a.type() || type < 0 )
+        {
+            addWeighted(a, alpha, b, beta, gamma, c);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, alpha, b, beta, gamma, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_Bin_
+{
+public:
+    MatOp_Bin_() {}
+
+    static void apply(const M& a, const M& b, int _op, M& c, int type=-1)
+    {
+        char op = (char)_op;
+        if( type == a.type() || type < 0 )
+        {
+            if( op == '&' )
+                bitwise_and( a, b, c );
+            else if( op == '|' )
+                bitwise_or( a, b, c );
+            else if( op == '^' )
+                bitwise_xor( a, b, c );
+            else if( op == 'm' )
+                min( a, b, c );
+            else if( op == 'M' )
+                max( a, b, c );
+            else if( op == 'a' )
+                absdiff( a, b, c );
+            else
+                assert(0);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, op, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_BinS_
+{
+public:
+    MatOp_BinS_() {}
+
+    static void apply(const M& a, const Scalar& s, int _op, M& c, int type=-1)
+    {
+        char op = (char)_op;
+        if( type == a.type() || type < 0 )
+        {
+            if( op == '&' )
+                bitwise_and( a, s, c );
+            else if( op == '|' )
+                bitwise_or( a, s, c );
+            else if( op == '^' )
+                bitwise_xor( a, s, c );
+            else if( op == 'm' )
+                min( a, s[0], c );
+            else if( op == 'M' )
+                max( a, s[0], c );
+            else if( op == 'a' )
+                absdiff( a, s, c );
+            else if( op == '~' )
+                bitwise_not( a, c );
+            else
+                assert(0);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, s, op, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_T_
+{
+public:
+    MatOp_T_() {}
+
+    static void apply(const M& a, double scale, M& c, int type=-1)
+    {
+        if( type == a.type() || type < 0 )
+        {
+            transpose(a, c);
+            if( fabs(scale - 1) > DBL_EPSILON )
+                c.convertTo(c, -1, scale, 0);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, scale, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+
+template<typename M> class CV_EXPORTS MatOp_MatMul_
+{
+public:
+    MatOp_MatMul_() {}
+
+    static void apply(const M& a, const M& b, double scale, int flags, M& c, int type=-1)
+    {
+        if( type == a.type() || type < 0 )
+        {
+            gemm(a, b, scale, Mat(), 0, c, flags);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, scale, flags, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+
+template<typename M> class CV_EXPORTS MatOp_MatMulAdd_
+{
+public:
+    MatOp_MatMulAdd_() {}
+
+    static void apply(const M& a, const M& b, double alpha,
+        const M& c, double beta, int flags, M& d, int type=-1)
+    {
+        if( type == a.type() || type < 0 )
+        {
+            gemm(a, b, alpha, c, beta, d, flags);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, alpha, c, beta, flags, temp);
+            temp.convertTo(d, type);
+        }
+    }
+};
+
+
+template<typename M> class CV_EXPORTS MatOp_Cmp_
+{
+public:
+    MatOp_Cmp_() {}
+
+    static void apply(const M& a, const M& b, int op, M& c, int type=-1)
+    {
+        if( type == CV_8UC1 || type == -1 )
+        {
+            compare(a, b, c, op);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, op, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_CmpS_
+{
+public:
+    MatOp_CmpS_() {}
+
+    static void apply(const M& a, double alpha, int op, M& c, int type=-1)
+    {
+        if( type == CV_8UC1 || type == -1 )
+        {
+            compare(a, alpha, c, op);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, alpha, op, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_MulDiv_
+{
+public:
+    MatOp_MulDiv_() {}
+
+    static void apply(const M& a, const M& b, double alpha, char op, M& c, int type=-1)
+    {
+        if( type == a.type() || type == -1 )
+        {
+            if( op == '*' )
+                multiply( a, b, c, alpha );
+            else
+                divide( a, b, c, alpha );
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, alpha, op, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_DivRS_
+{
+public:
+    MatOp_DivRS_() {}
+
+    static void apply(const M& a, double alpha, M& c, int type=-1)
+    {
+        if( type == a.type() || type == -1 )
+        {
+            c.create(a.rows, a.cols, a.type());
+            divide( alpha, a, c );
+        }
+        else
+        {
+            Mat temp;
+            apply(a, alpha, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+
+template<typename M> class CV_EXPORTS MatOp_Inv_
+{
+public:
+    MatOp_Inv_() {}
+
+    static void apply(const M& a, int method, M& c, int type=-1)
+    {
+        if( type == a.type() || type == -1 )
+        {
+            invert(a, c, method);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, method, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+
+template<typename M> class CV_EXPORTS MatOp_Solve_
+{
+public:
+    MatOp_Solve_() {}
+
+    static void apply(const M& a, const M& b, int method, M& c, int type=-1)
+    {
+        if( type == a.type() || type == -1 )
+        {
+            solve(a, b, c, method);
+        }
+        else
+        {
+            Mat temp;
+            apply(a, b, method, temp);
+            temp.convertTo(c, type);
+        }
+    }
+};
+
+template<typename M> class CV_EXPORTS MatOp_Set_
+{
+public:
+    MatOp_Set_() {}
+
+    static void apply(Size size, int type0, const Scalar& s, int mtype, M& c, int type=-1)
+    {
+        if( type < 0 )
+            type = type0;
+        c.create(size.height, size.width, type);
+        if( mtype == 0 )
+            c = Scalar(0);
+        else if( mtype == 1 )
+            c = s;
+        else if( mtype == 2 )
+            setIdentity(c, s);
+    }
+};
+
+template<typename A1, typename M, typename Op>
+class CV_EXPORTS MatExpr_Op1_
+{
+public:
+    MatExpr_Op1_(const A1& _a1) : a1(_a1) {}
+    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, (M&)m, type); }
+    operator M() const { M result; assignTo(result); return result; }
+
+    A1 a1;
+};
+
+template<typename A1, typename A2, typename M, typename Op>
+class CV_EXPORTS MatExpr_Op2_
+{
+public:
+    MatExpr_Op2_(const A1& _a1, const A2& _a2) : a1(_a1), a2(_a2) {}
+    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, (M&)m, type); }
+    operator M() const { M result; assignTo(result); return result; }
+
+    A1 a1; A2 a2;
+};
+
+template<typename A1, typename A2, typename A3, typename M, typename Op>
+class CV_EXPORTS MatExpr_Op3_
+{
+public:
+    MatExpr_Op3_(const A1& _a1, const A2& _a2, const A3& _a3) : a1(_a1), a2(_a2), a3(_a3) {}
+    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, (M&)m, type); }
+    operator M() const { M result; assignTo(result); return result; }
+
+    A1 a1; A2 a2; A3 a3;
+};
+
+template<typename A1, typename A2, typename A3, typename A4, typename M, typename Op>
+class CV_EXPORTS MatExpr_Op4_
+{
+public:
+    MatExpr_Op4_(const A1& _a1, const A2& _a2, const A3& _a3, const A4& _a4)
+        : a1(_a1), a2(_a2), a3(_a3), a4(_a4) {}
+    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, a4, (M&)m, type); }
+    operator M() const { M result; assignTo(result); return result; }
+
+    A1 a1; A2 a2; A3 a3; A4 a4;
+};
+
+template<typename A1, typename A2, typename A3, typename A4, typename A5, typename M, typename Op>
+class CV_EXPORTS MatExpr_Op5_
+{
+public:
+    MatExpr_Op5_(const A1& _a1, const A2& _a2, const A3& _a3, const A4& _a4, const A5& _a5)
+        : a1(_a1), a2(_a2), a3(_a3), a4(_a4), a5(_a5) {}
+    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, a4, a5, (M&)m, type); }
+    operator M() const { M result; assignTo(result); return result; }
+
+    A1 a1; A2 a2; A3 a3; A4 a4; A5 a5;
+};
+
+template<typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename M, typename Op>
+class CV_EXPORTS MatExpr_Op6_
+{
+public:
+    MatExpr_Op6_(const A1& _a1, const A2& _a2, const A3& _a3,
+                    const A4& _a4, const A5& _a5, const A6& _a6)
+        : a1(_a1), a2(_a2), a3(_a3), a4(_a4), a5(_a5), a6(_a6) {}
+    void assignTo(Mat& m, int type=-1) const { Op::apply(a1, a2, a3, a4, a5, a6, (M&)m, type); }
+    operator M() const { M result; assignTo(result); return result; }
+
+    A1 a1; A2 a2; A3 a3; A4 a4; A5 a5; A6 a6;
+};
+
+///////////////////////////////// Arithmetical Operations ///////////////////////////////////
+
+// A + B
+static inline MatExpr_<MatExpr_Op5_<Mat, double, Mat, double, double, Mat, MatOp_AddEx_<Mat> >, Mat>
+operator + (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op5_<Mat, double, Mat, double, double, Mat, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, 1, b, 1, 0));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op5_<Mat_<_Tp>, double, Mat_<_Tp>,
+double, double, Mat_<_Tp>, MatOp_AddEx_<Mat> >, Mat_<_Tp> >
+operator + (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op5_<Mat_<_Tp>, double, Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, 1, b, 1, 0));
+}
+
+// E1 + E2
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<M, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b )
+{
+    typedef MatExpr_Op5_<M, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, 1, (M)b, 1, 0));
+}
+
+// A - B
+static inline MatExpr_<MatExpr_Op2_<Mat, Mat, Mat, MatOp_Sub_<Mat> >, Mat>
+operator - (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op2_<Mat, Mat, Mat, MatOp_Sub_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Mat_<_Tp>, Mat_<_Tp>, MatOp_Sub_<Mat> >, Mat_<_Tp> >
+operator - (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op2_<Mat_<_Tp>, Mat_<_Tp>, Mat_<_Tp>, MatOp_Sub_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, b));
+}
+
+// E1 - E2
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, M, M, MatOp_Sub_<Mat> >, M>
+operator - (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b )
+{
+    typedef MatExpr_Op2_<M, M, M, MatOp_Sub_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, (M)b));
+}
+
+// -(E1 - E2)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op2_<B, A, M, MatOp_Sub_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op2_<B, A, M, MatOp_Sub_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a2, a.e.a1));
+}
+
+// (A - B)*alpha
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a,
+            double alpha)
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, alpha, a.e.a2, -alpha, 0));
+}
+
+// alpha*(A - B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator * (double alpha,
+            const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a)
+{ return a*alpha; }
+
+
+// A*alpha
+static inline
+MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>
+operator * (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha));
+}
+
+// A*alpha
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >
+operator * (const Mat_<_Tp>& a, double alpha)
+{
+    typedef MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, alpha));
+}
+
+// alpha*A
+static inline
+MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>
+operator * (double alpha, const Mat& a)
+{ return a*alpha; }
+
+// alpha*A
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >
+operator * (double alpha, const Mat_<_Tp>& a)
+{ return a*alpha; }
+
+// A/alpha
+static inline
+MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>
+operator / (const Mat& a, double alpha)
+{ return a*(1./alpha); }
+
+// A/alpha
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >
+operator / (const Mat_<_Tp>& a, double alpha)
+{ return a*(1./alpha); }
+
+// -A
+static inline
+MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>
+operator - (const Mat& a)
+{ return a*(-1); }
+
+// -A
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >
+operator - (const Mat_<_Tp>& a)
+{ return a*(-1); }
+
+// E*alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>
+operator * (const MatExpr_<A, M>& a, double alpha)
+{
+    typedef MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, alpha));
+}
+
+// alpha*E
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>
+operator * (double alpha, const MatExpr_<A, M>& a)
+{ return a*alpha; }
+
+// E/alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>
+operator / (const MatExpr_<A, M>& a, double alpha)
+{ return a*(1./alpha); }
+
+// (E*alpha)*beta ~ E*(alpha*beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            double beta)
+{ return a.e.a1*(a.e.a2*beta); }
+
+// beta*(E*alpha) ~ E*(alpha*beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>
+operator * (double beta,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return a.e.a1*(a.e.a2*beta); }
+
+// (E*alpha)/beta ~ E*(alpha/beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>
+operator / (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            double beta)
+{ return a.e.a1*(a.e.a2/beta); }
+
+// -E ~ E*(-1)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<MatExpr_<A, M>, double, M, MatOp_Scale_<Mat> >, M>
+operator - (const MatExpr_<A, M>& a)
+{ return a*(-1); }
+
+// -(E*alpha) ~ E*(-alpha)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return a.e.a1*(-a.e.a2); }
+
+// A + alpha
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >
+operator + (const Mat_<_Tp>& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>,
+        MatOp_ScaleAddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, 1, alpha));
+}
+
+// A + alpha
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>, MatOp_AddS_<Mat> >, Mat_<_Tp> >
+operator + (const Mat_<_Tp>& a, const Scalar& alpha)
+{
+    typedef MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>,
+        MatOp_AddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, alpha));
+}
+
+// alpha + A
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >
+operator + (double alpha, const Mat_<_Tp>& a)
+{ return a + alpha; }
+
+// alpha + A
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>, MatOp_AddS_<Mat> >, Mat_<_Tp> >
+operator + (const Scalar& alpha, const Mat_<_Tp>& a)
+{ return a + alpha; }
+
+// A - alpha
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >
+operator - (const Mat_<_Tp>& a, double alpha)
+{ return a + (-alpha); }
+
+// A - alpha
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, Scalar, Mat_<_Tp>, MatOp_AddS_<Mat> >, Mat_<_Tp> >
+operator - (const Mat_<_Tp>& a, const Scalar& alpha)
+{ return a + (-alpha); }
+
+// alpha - A
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>, MatOp_ScaleAddS_<Mat> >, Mat_<_Tp> >
+operator - (double alpha, const Mat_<_Tp>& a)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, double, double, Mat_<_Tp>,
+        MatOp_ScaleAddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, -1, alpha));
+}
+
+// E + alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator + (const MatExpr_<A, M>& a, double alpha)
+{
+    typedef MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, 1, alpha));
+}
+
+// E + alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> >, M>
+operator + (const MatExpr_<A, M>& a, const Scalar& alpha)
+{
+    typedef MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, alpha));
+}
+
+// alpha + E
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator + (double alpha, const MatExpr_<A, M>& a)
+{ return a + alpha; }
+
+// alpha + E
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> >, M>
+operator + (const Scalar& alpha, const MatExpr_<A, M>& a)
+{ return a + alpha; }
+
+// E - alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (const MatExpr_<A, M>& a, double alpha)
+{ return a + (-alpha); }
+
+// E - alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, Scalar, M, MatOp_AddS_<Mat> >, M>
+operator - (const MatExpr_<A, M>& a, const Scalar& alpha)
+{ return a + (-alpha); }
+
+// alpha - E
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (double alpha, const MatExpr_<A, M>& a)
+{
+    typedef MatExpr_Op3_<M, double, double, M, MatOp_ScaleAddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a, -1, alpha));
+}
+
+// E*alpha + beta
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            double beta)
+{
+    typedef MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, beta));
+}
+
+// beta + E*alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator + (double beta,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return a + beta; }
+
+// E*alpha - beta
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            double beta)
+{ return a + (-beta); }
+
+// beta - E*alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (double beta,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return (a.e.a1*(-a.e.a2)) + beta; }
+
+// (E*alpha + gamma) + beta ~ E*alpha + (gamma + beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            double beta)
+{ return a.e.a1*a.e.a2 + (a.e.a3 + beta); }
+
+// beta + (E*alpha + gamma)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator + (double beta, const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return a + beta; }
+
+// (E*alpha + gamma) - beta
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            double beta)
+{ return a + (-beta); }
+
+// beta - (E*alpha + gamma)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (double beta, const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return a.e.a1*(-a.e.a2) + (beta - a.e.a3); }
+
+// (E*alpha + gamma)*beta
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            double beta)
+{ return a.e.a1*(a.e.a2*beta) + (a.e.a3*beta); }
+
+// beta*(E*alpha + gamma)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator * (double beta, const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return a*beta; }
+
+// -(E*alpha + beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return a*(-1); }
+
+// (A*u + B*v + w) + beta
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a,
+            double beta )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, a.e.a3, a.e.a4, a.e.a5 + beta));
+}
+
+// beta + (A*u + B*v + w)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (double beta,
+            const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)
+{ return a + beta; }
+
+// (A*u + B*v + w) - beta
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a,
+            double beta)
+{ return a + (-beta); }
+
+// beta - (A*u + B*v + w)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (double beta,
+            const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, -a.e.a2, a.e.a3, -a.e.a4, -a.e.a5 + beta));
+}
+
+// (A*u + B*v + w)*beta
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a,
+            double beta )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1,
+        a.e.a2*beta, a.e.a3, a.e.a4*beta, a.e.a5*beta));
+}
+
+// beta*(A*u + B*v + w)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator * (double beta,
+            const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)
+{ return a * beta; }
+
+// -(A*u + B*v + w)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>& a)
+{ return a*(-1); }
+
+// A*alpha + B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const M& b )
+{
+    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, 1, 0));
+}
+
+// B + A*alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const M& b,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return a + b; }
+
+// (A*alpha + beta) + B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const M& b )
+{
+    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, 1, a.e.a3));
+}
+
+// B + (A*alpha + beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const M& b,
+            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return a + b; }
+
+
+// A*alpha + E
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const MatExpr_<B, M>& b )
+{ return a + (M)b; }
+
+// E + A*alpha
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<B, M>& b,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return a + (M)b; }
+
+// (A*alpha + beta) + E
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const MatExpr_<B, M>& b )
+{ return a + (M)b; }
+
+// E + (A*alpha + beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<B, M>& b,
+            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return a + b; }
+
+// A*alpha + B*beta
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, b.e.a2, 0));
+}
+
+// (A*alpha + beta) + B*gamma
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, b.e.a2, a.e.a3));
+}
+
+// B*gamma + (A*alpha + beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b,
+            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a )
+{ return a + b; }
+
+// (A*alpha + beta) + (B*gamma + theta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator + (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, b.e.a2, a.e.a3 + b.e.a3));
+}
+
+// A*alpha - B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const M& b )
+{
+    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, -1, 0));
+}
+
+// B - A*alpha
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const M& b,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{
+    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, -a.e.a2, b, 1, 0));
+}
+
+// (A*alpha + beta) - B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const M& b )
+{
+    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, -1, a.e.a3));
+}
+
+// B - (A*alpha + beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const M& b,
+            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{
+    typedef MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b, -1, a.e.a3));
+}
+
+// A*alpha - E
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const MatExpr_<B, M>& b )
+{ return a - (M)b; }
+
+// E - A*alpha
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<B, M>& b,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return (M)b - a; }
+
+// (A*alpha + beta) - E
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const MatExpr_<B, M>& b )
+{ return a - (M)b; }
+
+// E - (A*alpha + beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, M, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<B, M>& b,
+            const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a)
+{ return (M)b - a; }
+
+// A*alpha - B*beta
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, -b.e.a2, 0));
+}
+
+// (A*alpha + beta) - B*gamma
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, -b.e.a2, a.e.a3));
+}
+
+// B*gamma - (A*alpha + beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b,
+            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, -a.e.a2, b.e.a1, b.e.a2, -a.e.a3));
+}
+
+// (A*alpha + beta) - (B*gamma + theta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> >, M>
+operator - (const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op3_<B, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op5_<A, double, B, double, double, M, MatOp_AddEx_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, b.e.a1, -b.e.a2, a.e.a3 - b.e.a3));
+}
+
+/////////////////////////////// Mat Multiplication ///////////////////////////////////
+
+// A^t
+inline MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_T_<Mat> >, Mat>
+Mat::t() const
+{
+    typedef MatExpr_Op2_<Mat, double, Mat, MatOp_T_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, 1));
+}
+
+template<typename _Tp> inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_T_<Mat> >, Mat_<_Tp> >
+Mat_<_Tp>::t() const
+{
+    typedef MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_T_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, 1));
+}
+
+// A*B
+static inline
+MatExpr_<MatExpr_Op4_<Mat, Mat, double, int, Mat, MatOp_MatMul_<Mat> >, Mat>
+operator * ( const Mat& a, const Mat& b )
+{
+    typedef MatExpr_Op4_<Mat, Mat, double, int, Mat, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 1, 0));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, int, Mat_<_Tp>,
+MatOp_MatMul_<Mat> >, Mat_<_Tp> >
+operator * ( const Mat_<_Tp>& a, const Mat_<_Tp>& b )
+{
+    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, int, Mat_<_Tp>,
+        MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, b, 1, 0));
+}
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<A, M>& a, const MatExpr_<B, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a, (M)b, 1, 0));
+}
+
+// (A*alpha)*B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a, const M& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2, 0));
+}
+
+// A*(B*alpha)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const M& b, const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(b, (M)a.e.a1, a.e.a2, 0));
+}
+
+// A^t*B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& a, const M& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2, GEMM_1_T));
+}
+
+// A*B^t
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const M& a, const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a, (M)b.e.a1, b.e.a2, GEMM_2_T));
+}
+
+// (A*alpha)*(B*beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1, a.e.a2*b.e.a2, 0));
+}
+
+// A^t*(B*alpha)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1, a.e.a2*b.e.a2, GEMM_1_T));
+}
+
+// (A*alpha)*B^t
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1, a.e.a2*b.e.a2, GEMM_2_T));
+}
+
+// A^t*B^t
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1,
+        (M)b.e.a1, a.e.a2*b.e.a2, GEMM_1_T+GEMM_2_T));
+}
+
+// (A*B)*alpha
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             double alpha )
+{
+    typedef MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2, a.e.a3*alpha, a.e.a4));
+}
+
+// alpha*(A*B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator * ( double alpha,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{
+    return a*alpha;
+}
+
+// -(A*B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{
+    return a*(-1);
+}
+
+// (A*alpha + beta)*B
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& a, const M& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2, b, a.e.a3, 0));
+}
+
+// A*(B*alpha + beta)
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const M& a, const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a, (M)b.e.a1, b.e.a2, a, b.e.a3, 0));
+}
+
+// (A*alpha + beta)*(B*gamma)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,
+        a.e.a2*b.e.a2, (M)b.e.a1, a.e.a3*b.e.a2, 0));
+}
+
+// (A*gamma)*(B*alpha + beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,
+        a.e.a2*b.e.a2, (M)a.e.a1, a.e.a2*b.e.a3, 0));
+}
+
+// (A*alpha + beta)*B^t
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,
+        a.e.a2*b.e.a2, (M)b.e.a1, a.e.a3*b.e.a2, GEMM_2_T));
+}
+
+// A^t*(B*alpha + beta)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_T_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_ScaleAddS_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op4_<M, M, double, int, M, MatOp_MatMul_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)b.e.a1,
+        a.e.a2*b.e.a2, (M)a.e.a1, a.e.a2*b.e.a3, GEMM_1_T));
+}
+
+// (A*B + C)*alpha
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( const MatExpr_<MatExpr_Op6_<A, B, double, C,
+             double, int, M, MatOp_MatMulAdd_<Mat> >, M>& a, double alpha )
+{
+    typedef MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(a.e.a1, a.e.a2,
+        a.e.a3*alpha, a.e.a4, a.e.a5*alpha, a.e.a6));
+}
+
+// alpha*(A*B + C)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator * ( double alpha, const MatExpr_<MatExpr_Op6_<A, B, double, C,
+             double, int, M, MatOp_MatMulAdd_<Mat> >, M>& a )
+{ return a*alpha; }
+
+// -(A*B + C)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<A, B, double, C, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op6_<A, B, double, C,
+             double, int, M, MatOp_MatMulAdd_<Mat> >, M>& a )
+{ return a*(-1); }
+
+
+// (A*B) + C
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const M& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, b, 1, a.e.a4));
+}
+
+// C + (A*B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const M& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{ return a + b; }
+
+
+// (A*B) - C
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const M& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, b, -1, a.e.a4));
+}
+
+// C - (A*B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const M& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, -a.e.a3, b, 1, a.e.a4));
+}
+
+
+// (A*B) + C
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const MatExpr_<C, M>& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b, 1, a.e.a4));
+}
+
+// C + (A*B)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<C, M>& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{ return a + b; }
+
+
+// (A*B) - C
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const MatExpr_<C, M>& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b, -1, a.e.a4));
+}
+
+// C - (A*B)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<C, M>& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, -a.e.a3, (M)b, 1, a.e.a4));
+}
+
+
+// (A*B) + C*alpha
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4));
+}
+
+// C*alpha + (A*B)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{ return a + b; }
+
+
+// (A*B) - (C*alpha)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, -b.e.a2, a.e.a4));
+}
+
+// (C*alpha) - (A*B)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_Scale_<Mat> >, M>& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, -a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4));
+}
+
+
+// (A*B) + C^t
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4 + GEMM_3_T));
+}
+
+// C^t + (A*B)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator + ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{ return a + b; }
+
+
+// (A*B) - C^t
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a,
+             const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, a.e.a3, (M)b.e.a1, -b.e.a2, a.e.a4+GEMM_3_T));
+}
+
+// C^t - (A*B)
+template<typename A, typename B, typename C, typename M> static inline
+MatExpr_<MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> >, M>
+operator - ( const MatExpr_<MatExpr_Op2_<C, double, M, MatOp_T_<Mat> >, M>& b,
+             const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& a )
+{
+    typedef MatExpr_Op6_<M, M, double, M, double, int, M, MatOp_MatMulAdd_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp(
+        (M)a.e.a1, (M)a.e.a2, -a.e.a3, (M)b.e.a1, b.e.a2, a.e.a4+GEMM_3_T));
+}
+
+
+////////////////////////////// Augmenting algebraic operations //////////////////////////////////
+
+static inline Mat& operator += (const Mat& a, const Mat& b)
+{
+    add(a, b, (Mat&)a);
+    return (Mat&)a;
+}
+
+static inline Mat& operator -= (const Mat& a, const Mat& b)
+{
+    subtract(a, b, (Mat&)a);
+    return (Mat&)a;
+}
+
+static inline Mat& operator *= (const Mat& a, const Mat& b)
+{
+    gemm(a, b, 1, Mat(), 0, (Mat&)a, 0);
+    return (Mat&)a;
+}
+
+static inline Mat& operator *= (const Mat& a, double alpha)
+{
+    a.convertTo((Mat&)a, -1, alpha);
+    return (Mat&)a;
+}
+
+static inline Mat& operator += (const Mat& a, const Scalar& s)
+{
+    add(a, s, (Mat&)a);
+    return (Mat&)a;
+}
+
+static inline Mat& operator -= (const Mat& a, const Scalar& s)
+{ return (a += -s); }
+
+template<typename _Tp> static inline
+Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    (Mat&)a += (const Mat&)b;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline
+Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    (Mat&)a -= (const Mat&)b;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline
+Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    (Mat&)a *= (const Mat&)b;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline
+Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const Scalar& s)
+{
+    (Mat&)a += s;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline
+Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const Scalar& s)
+{
+    (Mat&)a -= s;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename A, typename M> static inline
+M& operator += (const M& a, const MatExpr_<A, M>& b)
+{ return (a += (M)b); }
+
+template<typename A, typename M> static inline
+M& operator -= (const M& a, const MatExpr_<A, M>& b)
+{ return (a -= (M)b); }
+
+template<typename A, typename M> static inline
+M& operator *= (const M& a, const MatExpr_<A, M>& b)
+{ return (a *= (M)b); }
+
+template<typename A, typename M> static inline
+M& operator += (const M& a,
+                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    scaleAdd( b.e.a1, Scalar(b.e.a2), _a, _a );
+    return _a;
+}
+
+template<typename A, typename M> static inline
+M& operator -= (const M& a,
+                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    scaleAdd( b.e.a1, -Scalar(b.e.a2), _a, _a );
+    return _a;
+}
+
+template<typename A, typename M> static inline
+M& operator += (const M& a,
+                const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_AddEx_<Mat>::apply( a, 1, (M)b.e.a1, b.e.a2, b.e.a3, _a );
+    return _a;
+}
+
+template<typename A, typename M> static inline
+M& operator -= (const M& a,
+                const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_AddEx_<Mat>::apply( a, 1, (M)b.e.a1, -b.e.a2, -b.e.a3, _a );
+    return _a;
+}
+
+template<typename A, typename B, typename M> static inline
+M& operator += (const M& a,
+                const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_MatMulAdd_<Mat>::apply( (M)b.e.a1, (M)b.e.a2, b.e.a3, a, 1, b.e.a4, _a );
+    return _a;
+}
+
+template<typename A, typename B, typename M> static inline
+M& operator -= (const M& a,
+                const MatExpr_<MatExpr_Op4_<A, B, double, int, M, MatOp_MatMul_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_MatMulAdd_<Mat>::apply( (M)b.e.a1, (M)b.e.a2, -b.e.a3, a, 1, b.e.a4, _a );
+    return _a;
+}
+
+template<typename A, typename M> static inline
+M& operator *= (const M& a,
+                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_MatMul_<Mat>::apply( a, (M)b.e.a1, b.e.a2, 0, _a );
+    return _a;
+}
+
+template<typename A, typename M> static inline
+M& operator *= (const M& a,
+                const MatExpr_<MatExpr_Op3_<A, double, double, M, MatOp_ScaleAddS_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_MatMulAdd_<Mat>::apply( a, (M)b.e.a1, b.e.a2, a, b.e.a3, 0, _a );
+    return _a;
+}
+
+template<typename A, typename M> static inline
+M& operator *= (const M& a,
+                const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_T_<Mat> >, M>& b)
+{
+    M& _a = (M&)a;
+    MatOp_MatMul_<Mat>::apply( a, (M)b.e.a1, b.e.a2, GEMM_2_T, _a );
+    return _a;
+}
+
+////////////////////////////// Logical operations ///////////////////////////////
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>
+operator & (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, '&'));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>
+operator | (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, '|'));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>
+operator ^ (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, '^'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+            MatOp_Bin_<Mat> >, Mat_<_Tp> >
+operator & (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+        MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(
+        a, b, '&'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+            MatOp_Bin_<Mat> >, Mat_<_Tp> >
+operator | (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+        MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(
+        a, b, '|'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+            MatOp_Bin_<Mat> >, Mat_<_Tp> >
+operator ^ (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+        MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(
+        a, b, '^'));
+}
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator & (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)
+{ return (M)a & (M)b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator & (const MatExpr_<A, M>& a, const M& b)
+{ return (M)a & b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator & (const M& a, const MatExpr_<A, M>& b)
+{ return a & (M)b; }
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator | (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)
+{ return (M)a | (M)b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator | (const MatExpr_<A, M>& a, const M& b)
+{ return (M)a | b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator | (const M& a, const MatExpr_<A, M>& b)
+{ return a | (M)b; }
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator ^ (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)
+{ return (M)a ^ (M)b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator ^ (const MatExpr_<A, M>& a, const M& b)
+{ return (M)a ^ b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+operator ^ (const M& a, const MatExpr_<A, M>& b)
+{ return a ^ (M)b; }
+
+static inline Mat& operator &= (const Mat& a, const Mat& b)
+{
+    MatOp_Bin_<Mat>::apply( a, b, '&', (Mat&)a );
+    return (Mat&)a;
+}
+
+static inline Mat& operator |= (const Mat& a, const Mat& b)
+{
+    MatOp_Bin_<Mat>::apply( a, b, '|', (Mat&)a );
+    return (Mat&)a;
+}
+
+static inline Mat& operator ^= (const Mat& a, const Mat& b)
+{
+    MatOp_Bin_<Mat>::apply( a, b, '^', (Mat&)a );
+    return (Mat&)a;
+}
+
+template<typename _Tp> static inline Mat_<_Tp>&
+operator &= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    (Mat&)a &= (const Mat&)b;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline Mat_<_Tp>&
+operator |= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    (Mat&)a |= (const Mat&)b;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline Mat_<_Tp>&
+operator ^= (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    (Mat&)a ^= (const Mat&)b;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename A, typename M> static inline M&
+operator &= (const M& a, const MatExpr_<A, M>& b)
+{ return (a &= (M)b); }
+
+template<typename A, typename M> static inline M&
+operator |= (const M& a, const MatExpr_<A, M>& b)
+{ return (a |= (M)b); }
+
+template<typename A, typename M> static inline M&
+operator ^= (const M& a, const MatExpr_<A, M>& b)
+{ return (a ^= (M)b); }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator & (const Mat& a, const Scalar& s)
+{
+    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, s, '&'));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator & (const Scalar& s, const Mat& a)
+{ return a & s; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator | (const Mat& a, const Scalar& s)
+{
+    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, s, '|'));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator | (const Scalar& s, const Mat& a)
+{ return a | s; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator ^ (const Mat& a, const Scalar& s)
+{
+    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, s, '^'));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator ^ (const Scalar& s, const Mat& a)
+{ return a ^ s; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+operator ~ (const Mat& a)
+{
+    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, Scalar(), '~'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator & (const Mat_<_Tp>& a, const Scalar& s)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, s, '&'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator & (const Scalar& s, const Mat_<_Tp>& a)
+{ return a & s; }
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator | (const Mat_<_Tp>& a, const Scalar& s)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, s, '|'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator | (const Scalar& s, const Mat_<_Tp>& a)
+{ return a | s; }
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator ^ (const Mat_<_Tp>& a, const Scalar& s)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, s, '^'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator ^ (const Scalar& s, const Mat_<_Tp>& a)
+{ return a ^ s; }
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> >, Mat_<_Tp> >
+operator ~ (const Mat_<_Tp>& a)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, Scalar(), '~'));
+}
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator & (const MatExpr_<A, M>& a, const Scalar& s)
+{ return (M)a & s; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator & (const Scalar& s, const MatExpr_<A, M>& a)
+{ return (M)a & s; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator | (const MatExpr_<A, M>& a, const Scalar& s)
+{ return (M)a | s; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator | (const Scalar& s, const MatExpr_<A, M>& a)
+{ return (M)a | s; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator ^ (const MatExpr_<A, M>& a, const Scalar& s)
+{ return (M)a ^ s; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator ^ (const Scalar& s, const MatExpr_<A, M>& a)
+{ return (M)a ^ s; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, Scalar, int, M, MatOp_BinS_<Mat> >, M >
+operator ~ (const MatExpr_<A, M>& a)
+{ return ~(M)a; }
+
+static inline Mat& operator &= (const Mat& a, const Scalar& s)
+{
+    MatOp_BinS_<Mat>::apply( a, s, '&', (Mat&)a );
+    return (Mat&)a;
+}
+
+static inline Mat& operator |= (const Mat& a, const Scalar& s)
+{
+    MatOp_BinS_<Mat>::apply( a, s, '|', (Mat&)a );
+    return (Mat&)a;
+}
+
+static inline Mat& operator ^= (const Mat& a, const Scalar& s)
+{
+    MatOp_BinS_<Mat>::apply( a, s, '^', (Mat&)a );
+    return (Mat&)a;
+}
+
+template<typename _Tp> static inline Mat_<_Tp>&
+operator &= (const Mat_<_Tp>& a, const Scalar& s)
+{
+    (Mat&)a &= s;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline Mat_<_Tp>&
+operator |= (const Mat_<_Tp>& a, const Scalar& s)
+{
+    (Mat&)a |= s;
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline Mat_<_Tp>&
+operator ^= (const Mat_<_Tp>& a, const Scalar& s)
+{
+    (Mat&)a ^= s;
+    return (Mat_<_Tp>&)a;
+}
+
+////////////////////////////// Comparison operations ///////////////////////////////
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>
+operator == (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_EQ));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>
+operator >= (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_GE));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>
+operator > (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_GT));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>
+operator <= (const Mat& a, const Mat& b)
+{ return b >= a; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>
+operator < (const Mat& a, const Mat& b)
+{ return b > a; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> >, Mat>
+operator != (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Cmp_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, CMP_NE));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator == (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_EQ));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator >= (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_GE));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator > (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_GT));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator <= (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_LE));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator < (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_LT));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator != (const Mat& a, double alpha)
+{
+    typedef MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha, CMP_NE));
+}
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator == (double alpha, const Mat& a)
+{ return a == alpha; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator >= (double alpha, const Mat& a)
+{ return a <= alpha; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator > (double alpha, const Mat& a)
+{ return a < alpha; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator <= (double alpha, const Mat& a)
+{ return a >= alpha; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator < (double alpha, const Mat& a)
+{ return a > alpha; }
+
+static inline MatExpr_<MatExpr_Op3_<Mat, double, int, Mat, MatOp_CmpS_<Mat> >, Mat>
+operator != (double alpha, const Mat& a)
+{ return a != alpha; }
+
+/////////////////////////////// Miscellaneous operations //////////////////////////////
+
+// max(A, B)
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>
+max(const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 'M'));
+}
+
+// min(A, B)
+static inline MatExpr_<MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> >, Mat>
+min(const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op3_<Mat, Mat, int, Mat, MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 'm'));
+}
+
+// abs(A)
+static inline MatExpr_<MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> >, Mat>
+abs(const Mat& a)
+{
+    typedef MatExpr_Op3_<Mat, Scalar, int, Mat, MatOp_BinS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, Scalar(0), 'a'));
+}
+
+// max(A, B)
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+            MatOp_Bin_<Mat> >, Mat_<_Tp> >
+max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+        MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(
+        a, b, 'M'));
+}
+
+// min(A, B)
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+            MatOp_Bin_<Mat> >, Mat_<_Tp> >
+min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Mat_<_Tp>, int, Mat_<_Tp>,
+        MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(
+        a, b, 'm'));
+}
+
+// abs(A)
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>,
+            MatOp_BinS_<Mat> >, Mat_<_Tp> >
+abs(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op3_<Mat_<_Tp>, Scalar, int, Mat_<_Tp>,
+        MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(
+        a, Scalar(0), 'a'));
+}
+
+// max(A, B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+max(const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)
+{ return max((M)a, (M)b); }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+max(const MatExpr_<A, M>& a, const M& b)
+{ return max((M)a, b); }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+max(const M& a, const MatExpr_<A, M>& b)
+{ return max(a, (M)b); }
+
+// min(A, B)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+min(const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)
+{ return min((M)a, (M)b); }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+min(const MatExpr_<A, M>& a, const M& b)
+{ return min((M)a, b); }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+min(const M& a, const MatExpr_<A, M>& b)
+{ return min(a, (M)b); }
+
+// abs(A)
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> >, M>
+abs(const MatExpr_<MatExpr_Op2_<A, B, M, MatOp_Sub_<Mat> >, M>& a)
+{
+    typedef MatExpr_Op3_<M, M, int, M, MatOp_Bin_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)a.e.a2, 'a'));
+}
+
+template<typename _Tp> void merge(const Vector<Mat_<_Tp> >& mv, Mat& dst)
+{ merge( (const Vector<Mat>&)mv, dst ); }
+
+template<typename _Tp> void split(const Mat& src, Vector<Mat_<_Tp> >& mv)
+{ split(src, (Vector<Mat>&)mv ); }
+
+///// Element-wise multiplication
+
+inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>
+Mat::mul(const Mat& m, double scale) const
+{
+    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, m, scale, '*'));
+}
+
+inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>
+Mat::mul(const MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_Scale_<Mat> >, Mat>& m, double scale) const
+{
+    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, m.e.a1, m.e.a2*scale, '*'));
+}
+
+inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>
+Mat::mul(const MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_DivRS_<Mat> >, Mat>& m, double scale) const
+{
+    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, m.e.a1, scale/m.e.a2, '/'));
+}
+
+template<typename _Tp> inline
+MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >
+Mat_<_Tp>::mul(const Mat_<_Tp>& m, double scale) const
+{
+    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, m, scale, '*'));
+}
+
+template<typename _Tp> inline
+MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >
+Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_Scale_<Mat> >, Mat_<_Tp> >& m, double scale) const
+{
+    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, m.e.a1, m.e.a2*scale, '*'));
+}
+
+template<typename _Tp> inline
+MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >
+Mat_<_Tp>::mul(const MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_DivRS_<Mat> >, Mat_<_Tp> >& m, double scale) const
+{
+    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double, char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, m.e.a1, scale/m.e.a2, '/'));
+}
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op4_<A, B, double, char, M, MatOp_MulDiv_<Mat> >, M>& a,
+            double alpha)
+{
+    typedef MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, (M)a.e.a2, a.e.a3*alpha, a.e.a4));
+}
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator * (double alpha,
+            const MatExpr_<MatExpr_Op4_<A, B, double, char, M, MatOp_MulDiv_<Mat> >, M>& a)
+{ return a*alpha; }
+
+
+////// Element-wise division
+
+static inline MatExpr_<MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> >, Mat>
+operator / (const Mat& a, const Mat& b)
+{
+    typedef MatExpr_Op4_<Mat, Mat, double, char, Mat, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, b, 1, '/'));
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double,
+char, Mat_<_Tp>, MatOp_MulDiv_<Mat> >, Mat_<_Tp> >
+operator / (const Mat_<_Tp>& a, const Mat_<_Tp>& b)
+{
+    typedef MatExpr_Op4_<Mat_<_Tp>, Mat_<_Tp>, double,
+        char, Mat_<_Tp>, MatOp_MulDiv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, b, 1, '/'));
+}
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const MatExpr_<A, M>& a, const MatExpr_<B, M>& b)
+{ return (M)a/(M)b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const MatExpr_<A, M>& a, const M& b)
+{ return (M)a/b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const M& a, const MatExpr_<A, M>& b)
+{ return a/(M)b; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const M& b)
+{ return ((M)a.e.a1/b)*a.e.a2; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const M& a,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& b)
+{ return (a/(M)b.e.a1)*(1./b.e.a2); }
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a,
+            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_Scale_<Mat> >, M>& b)
+{ return ((M)a.e.a1/(M)b.e.a1)*(a.e.a2/b.e.a2); }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const M& a,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_DivRS_<Mat> >, M>& b)
+{ return a.mul((M)b.e.a1, 1./b.e.a2); }
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op4_<M, M, double, char, M, MatOp_MulDiv_<Mat> >, M>
+operator / (const MatExpr_<A, M>& a,
+            const MatExpr_<MatExpr_Op2_<B, double, M, MatOp_DivRS_<Mat> >, M>& b)
+{ return ((M)a).mul((M)b.e.a1, 1./b.e.a2); }
+
+static inline
+MatExpr_<MatExpr_Op2_<Mat, double, Mat, MatOp_DivRS_<Mat> >, Mat >
+operator / (double alpha, const Mat& a)
+{
+    typedef MatExpr_Op2_<Mat, double, Mat, MatOp_DivRS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(a, alpha));
+}
+
+static inline Mat& operator /= (const Mat& a, double alpha)
+{
+    MatOp_Scale_<Mat>::apply( a, 1./alpha, (Mat&)a );
+    return (Mat&)a;
+}
+
+template<typename _Tp>
+static inline Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, double alpha)
+{
+    MatOp_Scale_<Mat>::apply( a, 1./alpha, (Mat&)a );
+    return (Mat_<_Tp>&)a;
+}
+
+template<typename _Tp> static inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>, MatOp_DivRS_<Mat> >, Mat_<_Tp> >
+operator / (double alpha, const Mat_<_Tp>& a)
+{
+    typedef MatExpr_Op2_<Mat_<_Tp>, double, Mat_<_Tp>,
+        MatOp_DivRS_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(a, alpha));
+}
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, double, M, MatOp_DivRS_<Mat> >, M>
+operator / (double alpha, const MatExpr_<A, M>& a)
+{ return alpha/(M)a; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, double, M, MatOp_DivRS_<Mat> >, M>
+operator / (double alpha,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_Scale_<Mat> >, M>& a)
+{ return (alpha/a.e.a2)/(M)a.e.a1; }
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op2_<M, double, M, MatOp_Scale_<Mat> >, M>
+operator / (double alpha,
+            const MatExpr_<MatExpr_Op2_<A, double, M, MatOp_DivRS_<Mat> >, M>& a)
+{ return (M)a.e.a1*(alpha/a.e.a2); }
+
+static inline Mat& operator /= (const Mat& a, const Mat& b)
+{
+    MatOp_MulDiv_<Mat>::apply( a, b, 1, '/', (Mat&)a );
+    return (Mat&)a;
+}
+
+template<typename A, typename M>
+static inline M& operator /= (const M& a, const MatExpr_<MatExpr_Op2_<A, double,
+                              M, MatOp_Scale_<Mat> >, M>& b)
+{
+    MatOp_MulDiv_<Mat>::apply( a, (M)b.e.a1, 1./b.e.a2, '/', (M&)a );
+    return (M&)a;
+}
+
+template<typename A, typename M>
+static inline M& operator /= (const M& a, const MatExpr_<MatExpr_Op2_<A, double,
+                              M, MatOp_DivRS_<Mat> >, M>& b)
+{
+    MatOp_MulDiv_<Mat>::apply( a, (M)b.e.a1, 1./b.e.a2, '*', (M&)a );
+    return (M&)a;
+}
+
+// Mat Inversion and solving linear systems
+
+inline MatExpr_<MatExpr_Op2_<Mat, int, Mat, MatOp_Inv_<Mat> >, Mat>
+Mat::inv(int method) const
+{
+    typedef MatExpr_Op2_<Mat, int, Mat, MatOp_Inv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(*this, method));
+}
+
+template<typename _Tp> inline
+MatExpr_<MatExpr_Op2_<Mat_<_Tp>, int, Mat_<_Tp>, MatOp_Inv_<Mat> >, Mat_<_Tp> >
+Mat_<_Tp>::inv(int method) const
+{
+    typedef MatExpr_Op2_<Mat_<_Tp>, int, Mat_<_Tp>, MatOp_Inv_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat_<_Tp> >(MatExpr_Temp(*this, method));
+}
+
+template<typename A, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Solve_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op2_<A, int, M, MatOp_Inv_<Mat> >, M>& a,
+            const M& b)
+{
+    typedef MatExpr_Op3_<M, M, int, M, MatOp_Solve_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, M>(MatExpr_Temp((M)a.e.a1, b, a.e.a2));
+}
+
+template<typename A, typename B, typename M> static inline
+MatExpr_<MatExpr_Op3_<M, M, int, M, MatOp_Solve_<Mat> >, M>
+operator * (const MatExpr_<MatExpr_Op2_<A, int, M, MatOp_Inv_<Mat> >, M>& a,
+            const MatExpr_<B, M>& b)
+{ return a*(M)b; }
+
+
+/////////////////////////////// Initialization ////////////////////////////////////////
+
+inline MatExpr_Initializer Mat::zeros(int rows, int cols, int type)
+{
+    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(Size(cols, rows), type, 0, 0));
+}
+
+inline MatExpr_Initializer Mat::zeros(Size size, int type)
+{
+    return zeros(size.height, size.width, type);
+}
+
+inline MatExpr_Initializer Mat::ones(int rows, int cols, int type)
+{
+    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;
+    return MatExpr_<MatExpr_Temp, Mat>(MatExpr_Temp(Size(cols, rows), type, 1, 1));
+}
+
+inline MatExpr_Initializer Mat::ones(Size size, int type)
+{
+    return ones(size.height, size.width, type);
+}
+
+inline MatExpr_Initializer Mat::eye(int rows, int cols, int type)
+{
+    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;
+    return MatExpr_Initializer(MatExpr_Temp(Size(cols, rows), type, 1, 2));
+}
+
+inline MatExpr_Initializer Mat::eye(Size size, int type)
+{
+    return eye(size.height, size.width, type);
+}
+
+static inline MatExpr_Initializer operator * (const MatExpr_Initializer& a, double alpha)
+{
+    typedef MatExpr_Op4_<Size, int, Scalar, int, Mat, MatOp_Set_<Mat> > MatExpr_Temp;
+    return MatExpr_Initializer(MatExpr_Temp(a.e.a1, a.e.a2, a.e.a3*alpha, a.e.a4));
+}
+
+static inline MatExpr_Initializer operator * (double alpha, MatExpr_Initializer& a)
+{ return a*alpha; }
+
+template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::zeros(int rows, int cols)
+{ return Mat::zeros(rows, cols, DataType<_Tp>::type); }
+
+template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::zeros(Size size)
+{ return Mat::zeros(size, DataType<_Tp>::type); }
+
+template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::ones(int rows, int cols)
+{ return Mat::ones(rows, cols, DataType<_Tp>::type); }
+
+template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::ones(Size size)
+{ return Mat::ones(size, DataType<_Tp>::type); }
+
+template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::eye(int rows, int cols)
+{ return Mat::eye(rows, cols, DataType<_Tp>::type); }
+
+template<typename _Tp> inline MatExpr_Initializer Mat_<_Tp>::eye(Size size)
+{ return Mat::eye(size, DataType<_Tp>::type); }
+
+
+//////////// Iterators & Comma initializers //////////////////
+
+template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_()
+    : m(0), ptr(0), sliceEnd(0) {}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m) : m(_m)
+{
+    if( !_m )
+        ptr = sliceEnd = 0;
+    else
+    {
+        ptr = (_Tp*)_m->data;
+        sliceEnd = ptr + (_m->isContinuous() ? _m->rows*_m->cols : _m->cols);
+    }
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>::
+    MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col) : m(_m)
+{
+    if( !_m )
+        ptr = sliceEnd = 0;
+    else
+    {
+        CV_DbgAssert( (unsigned)_row < _m->rows && (unsigned)_col < _m->cols );
+        ptr = (_Tp*)(_m->data + _m->step*_row);
+        sliceEnd = _m->isContinuous() ? (_Tp*)_m->data + _m->rows*_m->cols : ptr + _m->cols;
+        ptr += _col;
+    }
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>::
+    MatConstIterator_(const Mat_<_Tp>* _m, Point _pt) : m(_m)
+{
+    if( !_m )
+        ptr = sliceEnd = 0;
+    else
+    {
+        CV_DbgAssert( (unsigned)_pt.y < (unsigned)_m->rows && (unsigned)_pt.x < (unsigned)_m->cols );
+        ptr = (_Tp*)(_m->data + _m->step*_pt.y);
+        sliceEnd = _m->isContinuous() ? (_Tp*)_m->data + _m->rows*_m->cols : ptr + _m->cols;
+        ptr += _pt.x;
+    }
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>::
+    MatConstIterator_(const MatConstIterator_& it)
+    : m(it.m), ptr(it.ptr), sliceEnd(it.sliceEnd) {}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>&
+    MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
+{
+    m = it.m; ptr = it.ptr; sliceEnd = it.sliceEnd;
+    return *this;
+}
+
+template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator *() const { return *ptr; }
+
+template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (int ofs)
+{
+    if( !m || ofs == 0 )
+        return *this;
+    ptr += ofs;
+    if( m->isContinuous() )
+    {
+        if( ptr > sliceEnd )
+            ptr = sliceEnd;
+        else if( ptr < (_Tp*)m->data )
+            ptr = (_Tp*)m->data;
+    }
+    else if( ptr >= sliceEnd || ptr < sliceEnd - m->cols )
+    {
+        ptr -= ofs;
+        Point pt = pos();
+        int cols = m->cols;
+        ofs += pt.y*cols + pt.x;
+        if( ofs > cols*m->rows )
+            ofs = cols*m->rows;
+        else if( ofs < 0 )
+            ofs = 0;
+        pt.y = ofs/cols;
+        pt.x = ofs - pt.y*cols;
+        ptr = (_Tp*)(m->data + m->step*pt.y);
+        sliceEnd = ptr + cols;
+        ptr += pt.x;
+    }
+    return *this;
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (int ofs)
+{ return (*this += -ofs); }
+
+template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
+{ return (*this += -1); }
+
+template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
+{
+    MatConstIterator_ b = *this;
+    *this += -1;
+    return b;
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
+{
+    if( m && ++ptr >= sliceEnd )
+    {
+        --ptr;
+        *this += 1;
+    }
+    return *this;
+}
+
+template<typename _Tp> inline MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
+{
+    MatConstIterator_ b = *this;
+    if( m && ++ptr >= sliceEnd )
+    {
+        --ptr;
+        *this += 1;
+    }
+    return b;
+}
+
+template<typename _Tp> inline Point MatConstIterator_<_Tp>::pos() const
+{
+    if( !m )
+        return Point();
+    if( m->isContinuous() )
+    {
+        int ofs = ptr - (_Tp*)m->data, y = ofs / m->cols, x = ofs - y*m->cols;
+        return Point(x,y);
+    }
+    else
+    {
+        int stepT = m->stepT(), y = (ptr - (_Tp*)m->data)/stepT, x = (ptr - (_Tp*)m->data) - y*stepT;
+        return Point(x,y);
+    }
+}
+
+template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_() : MatConstIterator_<_Tp>() {}
+
+template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
+    : MatConstIterator_<_Tp>(_m) {}
+
+template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
+    : MatConstIterator_<_Tp>(_m, _row, _col) {}
+
+template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const Mat_<_Tp>* _m, Point _pt)
+    : MatConstIterator_<_Tp>(_m, _pt) {}
+
+template<typename _Tp> inline MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
+    : MatConstIterator_<_Tp>(it) {}
+
+template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
+{
+    this->m = it.m; this->ptr = it.ptr; this->sliceEnd = it.sliceEnd;
+    return *this;
+}
+
+template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator *() const { return *(this->ptr); }
+
+template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (int ofs)
+{
+    MatConstIterator_<_Tp>::operator += (ofs);
+    return *this;
+}
+
+template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (int ofs)
+{
+    MatConstIterator_<_Tp>::operator += (-ofs);
+    return *this;
+}
+
+template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
+{
+    MatConstIterator_<_Tp>::operator += (-1);
+    return *this;
+}
+
+template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
+{
+    MatIterator_ b = *this;
+    MatConstIterator_<_Tp>::operator += (-1);
+    return b;
+}
+
+template<typename _Tp> inline MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
+{
+    if( this->m && ++this->ptr >= this->sliceEnd )
+    {
+        --this->ptr;
+        MatConstIterator_<_Tp>::operator += (1);
+    }
+    return *this;
+}
+
+template<typename _Tp> inline MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
+{
+    MatIterator_ b = *this;
+    if( this->m && ++this->ptr >= this->sliceEnd )
+    {
+        --this->ptr;
+        MatConstIterator_<_Tp>::operator += (1);
+    }
+    return b;
+}
+
+template<typename _Tp> static inline bool
+operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
+{ return a.m == b.m && a.ptr == b.ptr; }
+
+template<typename _Tp> static inline bool
+operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
+{ return !(a == b); }
+
+template<typename _Tp> static inline bool
+operator < (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
+{ return a.ptr < b.ptr; }
+
+template<typename _Tp> static inline bool
+operator > (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
+{ return a.ptr > b.ptr; }
+
+template<typename _Tp> static inline bool
+operator <= (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
+{ return a.ptr <= b.ptr; }
+
+template<typename _Tp> static inline bool
+operator >= (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
+{ return a.ptr >= b.ptr; }
+
+template<typename _Tp> static inline int
+operator - (const MatConstIterator_<_Tp>& b, const MatConstIterator_<_Tp>& a)
+{
+    if( a.m != b.m )
+        return INT_MAX;
+    if( a.sliceEnd == b.sliceEnd )
+        return b.ptr - a.ptr;
+    {
+        Point ap = a.pos(), bp = b.pos();
+        if( bp.y > ap.y )
+            return (bp.y - ap.y - 1)*a.m->cols + (a.m->cols - ap.x) + bp.x;
+        if( bp.y < ap.y )
+            return -((ap.y - bp.y - 1)*a.m->cols + (a.m->cols - bp.x) + ap.x);
+        return bp.x - ap.x;
+    }
+}
+
+template<typename _Tp> static inline MatConstIterator_<_Tp>
+operator + (const MatConstIterator_<_Tp>& a, int ofs)
+{ MatConstIterator_<_Tp> b = a; return b += ofs; }
+
+template<typename _Tp> static inline MatConstIterator_<_Tp>
+operator + (int ofs, const MatConstIterator_<_Tp>& a)
+{ MatConstIterator_<_Tp> b = a; return b += ofs; }
+
+template<typename _Tp> static inline MatConstIterator_<_Tp>
+operator - (const MatConstIterator_<_Tp>& a, int ofs)
+{ MatConstIterator_<_Tp> b = a; return b += -ofs; }
+
+template<typename _Tp> inline _Tp MatConstIterator_<_Tp>::operator [](int i) const
+{ return *(*this + i); }
+
+template<typename _Tp> static inline MatIterator_<_Tp>
+operator + (const MatIterator_<_Tp>& a, int ofs)
+{ MatIterator_<_Tp> b = a; return b += ofs; }
+
+template<typename _Tp> static inline MatIterator_<_Tp>
+operator + (int ofs, const MatIterator_<_Tp>& a)
+{ MatIterator_<_Tp> b = a; return b += ofs; }
+
+template<typename _Tp> static inline MatIterator_<_Tp>
+operator - (const MatIterator_<_Tp>& a, int ofs)
+{ MatIterator_<_Tp> b = a; return b += -ofs; }
+
+template<typename _Tp> inline _Tp& MatIterator_<_Tp>::operator [](int i) const
+{ return *(*this + i); }
+
+template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
+{ return Mat::begin<_Tp>(); }
+
+template<typename _Tp> inline MatConstIterator_<_Tp> Mat_<_Tp>::end() const
+{ return Mat::end<_Tp>(); }
+
+template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::begin()
+{ return Mat::begin<_Tp>(); }
+
+template<typename _Tp> inline MatIterator_<_Tp> Mat_<_Tp>::end()
+{ return Mat::end<_Tp>(); }
+
+template<typename _Tp> class CV_EXPORTS MatOp_Iter_
+{
+    MatOp_Iter_() {}
+
+    static void apply(const MatIterator_<_Tp>& a, Mat& c, int type=-1)
+    {
+        if( type < 0 )
+            c = *a.m;
+        else
+            a.m->convertTo(c, type);
+    }
+};
+
+template<typename _Tp> inline MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m) :
+    MatExpr_<MatExpr_Op1_<MatIterator_<_Tp>, Mat_<_Tp>,
+        MatOp_Iter_<_Tp> >, Mat_<_Tp> >(MatIterator_<_Tp>(_m)) {}
+
+template<typename _Tp> template<typename T2> inline MatCommaInitializer_<_Tp>&
+MatCommaInitializer_<_Tp>::operator , (T2 v)
+{
+    CV_DbgAssert( this->e.a1 < this->e.a1.m->end() );
+    *this->e.a1 = _Tp(v); ++this->e.a1;
+    return *this;
+}
+
+template<typename _Tp> inline MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
+{
+    CV_DbgAssert( this->e.a1 == this->e.a1.m->end() );
+    return *this->e.a1.m;
+}
+
+template<typename _Tp> inline Mat_<_Tp> MatCommaInitializer_<_Tp>::operator *() const
+{
+    CV_DbgAssert( this->e.a1 == this->e.a1.m->end() );
+    return *this->e.a1.m;
+}
+
+template<typename _Tp> inline void
+MatCommaInitializer_<_Tp>::assignTo(Mat& m, int type) const
+{
+    Mat_<_Tp>(*this).assignTo(m, type);
+}
+
+template<typename _Tp, typename T2> static inline MatCommaInitializer_<_Tp>
+operator << (const Mat_<_Tp>& m, T2 val)
+{
+    MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
+    return (commaInitializer, val);
+}
+
+//////////////////////////////// MatND ////////////////////////////////
+
+inline MatND::MatND()
+ : flags(MAGIC_VAL), dims(0), refcount(0), data(0), datastart(0), dataend(0)
+{
+}
+
+inline MatND::MatND(const Vector<int>& _sizes, int _type)
+ : flags(MAGIC_VAL), dims(0), refcount(0), data(0), datastart(0), dataend(0)
+{
+    create(_sizes, _type);
+}
+
+inline MatND::MatND(const Vector<int>& _sizes, int _type, const Scalar& _s)
+ : flags(MAGIC_VAL), dims(0), refcount(0), data(0), datastart(0), dataend(0)
+{
+    create(_sizes, _type);
+    *this = _s;
+}
+
+inline MatND::MatND(const MatND& m)
+ : flags(m.flags), dims(m.dims), refcount(m.refcount),
+ data(m.data), datastart(m.datastart), dataend(m.dataend)
+{
+    int i, d = dims;
+    for( i = 0; i < d; i++ )
+    {
+        dim[i].size = m.dim[i].size;
+        dim[i].step = m.dim[i].step;
+    }
+    if( refcount )
+        ++*refcount;
+}
+
+inline MatND::MatND(const CvMatND* m, bool copyData)
+  : flags(MAGIC_VAL|(m->type & (CV_MAT_TYPE_MASK|CV_MAT_CONT_FLAG))),
+  dims(m->dims), refcount(0), data(m->data.ptr)
+{
+    int i, d = dims;
+    for( i = 0; i < d; i++ )
+    {
+        dim[i].size = m->dim[i].size;
+        dim[i].step = m->dim[i].step;
+    }
+    datastart = data;
+    dataend = datastart + dim[0].size*dim[0].step;
+    if( copyData )
+    {
+        MatND temp(*this);
+        temp.copyTo(*this);
+    }
+}
+
+inline MatND::~MatND() { release(); }
+
+inline MatND& MatND::operator = (const MatND& m)
+{
+    if( this != &m )
+    {
+        if( m.refcount )
+            ++*m.refcount;
+        release();
+        flags = m.flags;
+        dims = m.dims;
+        data = m.data;
+        datastart = m.datastart;
+        dataend = m.dataend;
+        refcount = m.refcount;
+        int i, d = dims;
+        for( i = 0; i < d; i++ )
+        {
+            dim[i].size = m.dim[i].size;
+            dim[i].step = m.dim[i].step;
+        }
+    }
+    return *this;
+}
+
+inline MatND MatND::clone() const
+{
+    MatND temp;
+    this->copyTo(temp);
+    return temp;
+}
+
+inline MatND MatND::operator()(const Vector<Range>& ranges) const
+{
+    return MatND(*this, ranges);
+}
+
+inline void MatND::assignTo( MatND& m, int type ) const
+{
+    if( type < 0 )
+        m = *this;
+    else
+        convertTo(m, type);
+}
+
+inline void MatND::addref()
+{
+    if( refcount ) ++*refcount;
+}
+
+inline void MatND::release()
+{
+    if( refcount && --*refcount == 0 )
+        fastFree(datastart);
+    dims = 0;
+    data = datastart = dataend = 0;
+    refcount = 0;
+}
+
+inline bool MatND::isContinuous() const { return (flags & CONTINUOUS_FLAG) != 0; }
+inline size_t MatND::elemSize() const { return getElemSize(flags); }
+inline size_t MatND::elemSize1() const { return CV_ELEM_SIZE1(flags); }
+inline int MatND::type() const { return CV_MAT_TYPE(flags); }
+inline int MatND::depth() const { return CV_MAT_DEPTH(flags); }
+inline int MatND::channels() const { return CV_MAT_CN(flags); }
+
+inline size_t MatND::step(int i) const { CV_DbgAssert((unsigned)i < (unsigned)dims); return dim[i].step; }
+inline size_t MatND::step1(int i) const
+{ CV_DbgAssert((unsigned)i < (unsigned)dims); return dim[i].step/elemSize1(); }
+inline Vector<int> MatND::size() const
+{
+    int i, d = dims;
+    Vector<int> sz(d);
+    for( i = 0; i < d; i++ )
+        sz[i] = dim[i].size;
+    return sz;
+}
+inline int MatND::size(int i) const  { CV_DbgAssert((unsigned)i < (unsigned)dims); return dim[i].size; }
+
+inline uchar* MatND::ptr(int i0)
+{
+    CV_DbgAssert( dims == 1 && data &&
+        (unsigned)i0 < (unsigned)dim[0].size );
+    return data + i0*dim[0].step;
+}
+
+inline const uchar* MatND::ptr(int i0) const
+{
+    CV_DbgAssert( dims == 1 && data &&
+        (unsigned)i0 < (unsigned)dim[0].size );
+    return data + i0*dim[0].step;
+}
+
+inline uchar* MatND::ptr(int i0, int i1)
+{
+    CV_DbgAssert( dims == 2 && data &&
+        (unsigned)i0 < (unsigned)dim[0].size &&
+        (unsigned)i1 < (unsigned)dim[1].size );
+    return data + i0*dim[0].step + i1*dim[1].step;
+}
+
+inline const uchar* MatND::ptr(int i0, int i1) const
+{
+    CV_DbgAssert( dims == 2 && data &&
+        (unsigned)i0 < (unsigned)dim[0].size &&
+        (unsigned)i1 < (unsigned)dim[1].size );
+    return data + i0*dim[0].step + i1*dim[1].step;
+}
+
+inline uchar* MatND::ptr(int i0, int i1, int i2)
+{
+    CV_DbgAssert( dims == 3 && data &&
+        (unsigned)i0 < (unsigned)dim[0].size &&
+        (unsigned)i1 < (unsigned)dim[1].size &&
+        (unsigned)i2 < (unsigned)dim[2].size );
+    return data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step;
+}
+
+inline const uchar* MatND::ptr(int i0, int i1, int i2) const
+{
+    CV_DbgAssert( dims == 3 && data &&
+        (unsigned)i0 < (unsigned)dim[0].size &&
+        (unsigned)i1 < (unsigned)dim[1].size &&
+        (unsigned)i2 < (unsigned)dim[2].size );
+    return data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step;
+}
+
+inline uchar* MatND::ptr(const int* idx)
+{
+    int i, d = dims;
+    uchar* p = data;
+    CV_DbgAssert( data );
+    for( i = 0; i < d; i++ )
+    {
+        CV_DbgAssert( (unsigned)idx[i] < (unsigned)dim[i].size );
+        p += idx[i]*dim[i].step;
+    }
+    return p;
+}
+
+inline const uchar* MatND::ptr(const int* idx) const
+{
+    int i, d = dims;
+    uchar* p = data;
+    CV_DbgAssert( data );
+    for( i = 0; i < d; i++ )
+    {
+        CV_DbgAssert( (unsigned)idx[i] < (unsigned)dim[i].size );
+        p += idx[i]*dim[i].step;
+    }
+    return p;
+}
+
+template<typename _Tp> inline _Tp& MatND::at(int i0)
+{ return *(_Tp*)ptr(i0); }
+template<typename _Tp> inline const _Tp& MatND::at(int i0) const
+{ return *(const _Tp*)ptr(i0); }
+template<typename _Tp> inline _Tp& MatND::at(int i0, int i1)
+{ return *(_Tp*)ptr(i0, i1); }
+template<typename _Tp> inline const _Tp& MatND::at(int i0, int i1) const
+{ return *(const _Tp*)ptr(i0, i1); }
+template<typename _Tp> inline _Tp& MatND::at(int i0, int i1, int i2)
+{ return *(_Tp*)ptr(i0, i1, i2); }
+template<typename _Tp> inline const _Tp& MatND::at(int i0, int i1, int i2) const
+{ return *(const _Tp*)ptr(i0, i1, i2); }
+template<typename _Tp> inline _Tp& MatND::at(const int* idx)
+{ return *(_Tp*)ptr(idx); }
+template<typename _Tp> inline const _Tp& MatND::at(const int* idx) const
+{ return *(const _Tp*)ptr(idx); }
+
+inline NAryMatNDIterator::NAryMatNDIterator()
+{
+}
+
+inline void subtract(const MatND& a, const Scalar& s, MatND& c, const MatND& mask=MatND())
+{
+    add(a, -s, c, mask);
+}
+
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_()
+{
+    flags = MAGIC_VAL | DataType<_Tp>::type;
+}
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_(const Vector<int>& _sizes)
+: MatND(_sizes, DataType<_Tp>::type)
+{
+}
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_(const Vector<int>& _sizes, const _Tp& _s)
+: MatND(_sizes, DataType<_Tp>::type, Scalar(_s))
+{
+}
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_(const MatND& m)
+{
+    if( m.type() == DataType<_Tp>::type )
+        *this = (const MatND_<_Tp>&)m;
+    else
+        m.convertTo(this, DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_(const MatND_<_Tp>& m) : MatND(m)
+{
+}
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_(const MatND_<_Tp>& m, const Vector<Range>& ranges)
+: MatND(m, ranges)
+{
+}
+
+template<typename _Tp> inline MatND_<_Tp>::MatND_(const CvMatND* m, bool copyData)
+{
+    *this = MatND(m, copyData || CV_MAT_TYPE(m->type) != DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline MatND_<_Tp>& MatND_<_Tp>::operator = (const MatND& m)
+{
+    if( DataType<_Tp>::type == m.type() )
+    {
+        Mat::operator = (m);
+        return *this;
+    }
+    if( DataType<_Tp>::depth == m.depth() )
+    {
+        return (*this = m.reshape(DataType<_Tp>::channels));
+    }
+    CV_DbgAssert(DataType<_Tp>::channels == m.channels());
+    m.convertTo(*this, DataType<_Tp>::type);
+    return *this;
+}
+
+template<typename _Tp> inline MatND_<_Tp>& MatND_<_Tp>::operator = (const MatND_<_Tp>& m)
+{
+    return ((MatND&)*this = m);
+}
+
+template<typename _Tp> inline MatND_<_Tp>& MatND_<_Tp>::operator = (const _Tp& s)
+{
+    return (MatND&)*this = Scalar(s);
+}
+
+template<typename _Tp> inline void MatND_<_Tp>::create(const Vector<int>& _sizes)
+{
+    MatND::create(_sizes);
+}
+
+template<typename _Tp> template<typename _Tp2> inline MatND_<_Tp>::operator MatND_<_Tp2>() const
+{
+    return MatND_<_Tp2>((const MatND&)*this);
+}
+
+template<typename _Tp> inline MatND_<_Tp> MatND_<_Tp>::clone() const
+{
+    MatND_<_Tp> temp;
+    this->copyTo(temp);
+    return temp;
+}
+
+template<typename _Tp> inline MatND_<_Tp>
+MatND_<_Tp>::operator()(const Vector<Range>& ranges) const
+{ return MatND_<_Tp>(*this, ranges); }
+
+template<typename _Tp> inline size_t MatND_<_Tp>::elemSize() const
+{ return CV_ELEM_SIZE(DataType<_Tp>::type); }
+
+template<typename _Tp> inline size_t MatND_<_Tp>::elemSize1() const
+{ return CV_ELEM_SIZE1(DataType<_Tp>::type); }
+
+template<typename _Tp> inline int MatND_<_Tp>::type() const
+{ return DataType<_Tp>::type; }
+
+template<typename _Tp> inline int MatND_<_Tp>::depth() const
+{ return DataType<_Tp>::depth; }
+
+template<typename _Tp> inline int MatND_<_Tp>::channels() const
+{ return DataType<_Tp>::channels; }
+
+template<typename _Tp> inline size_t MatND_<_Tp>::stepT(int i) const
+{
+    CV_DbgAssert( (unsigned)i < (unsigned)dims );
+    return dim[i].step/elemSize();
+}
+
+template<typename _Tp> inline size_t MatND_<_Tp>::step1(int i) const
+{
+    CV_DbgAssert( (unsigned)i < (unsigned)dims );
+    return dim[i].step/elemSize1();
+}
+
+template<typename _Tp> inline _Tp& MatND_<_Tp>::operator ()(const int* idx)
+{
+    uchar* ptr = data;
+    int i, d = dims;
+    for( i = 0; i < d; i++ )
+    {
+        int ii = idx[i];
+        CV_DbgAssert( (unsigned)ii < (unsigned)dim[i].size );
+        ptr += ii*dim[i].step;
+    }
+    return *(_Tp*)ptr;
+}
+
+template<typename _Tp> inline const _Tp& MatND_<_Tp>::operator ()(const int* idx) const
+{
+    const uchar* ptr = data;
+    int i, d = dims;
+    for( i = 0; i < d; i++ )
+    {
+        int ii = idx[i];
+        CV_DbgAssert( (unsigned)ii < (unsigned)dim[i].size );
+        ptr += ii*dim[i].step;
+    }
+    return *(const _Tp*)ptr;
+}
+
+template<typename _Tp> inline _Tp& MatND_<_Tp>::operator ()(int i0, int i1, int i2)
+{
+    CV_DbgAssert( dims == 3 &&
+        (unsigned)i0 < (unsigned)dim[0].size &&
+        (unsigned)i1 < (unsigned)dim[1].size &&
+        (unsigned)i2 < (unsigned)dim[2].size );
+
+    return *(_Tp*)(data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step);
+}
+
+template<typename _Tp> inline const _Tp& MatND_<_Tp>::operator ()(int i0, int i1, int i2) const
+{
+    CV_DbgAssert( dims == 3 &&
+        (unsigned)i0 < (unsigned)dim[0].size &&
+        (unsigned)i1 < (unsigned)dim[1].size &&
+        (unsigned)i2 < (unsigned)dim[2].size );
+
+    return *(const _Tp*)(data + i0*dim[0].step + i1*dim[1].step + i2*dim[2].step);
+}
+
+
+//////////////////////////////// SparseMat ////////////////////////////////
+
+inline SparseMat::SparseMat()
+: flags(MAGIC_VAL), hdr(0)
+{
+}
+
+inline SparseMat::SparseMat(const Vector<int>& _sizes, int _type)
+: flags(MAGIC_VAL), hdr(0)
+{
+    create(_sizes, _type);
+}
+
+inline SparseMat::SparseMat(const SparseMat& m)
+: flags(m.flags), hdr(m.hdr)
+{
+    addref();
+}
+
+inline SparseMat::~SparseMat()
+{
+    release();
+}
+
+inline SparseMat& SparseMat::operator = (const SparseMat& m)
+{
+    if( this != &m )
+    {
+        if( m.hdr )
+            ++m.hdr->refcount;
+        release();
+        flags = m.flags;
+        hdr = m.hdr;
+    }
+    return *this;
+}
+
+inline SparseMat& SparseMat::operator = (const Mat& m)
+{ return (*this = SparseMat(m)); }
+
+inline SparseMat& SparseMat::operator = (const MatND& m)
+{ return (*this = SparseMat(m)); }
+
+inline SparseMat SparseMat::clone() const
+{
+    SparseMat temp;
+    this->copyTo(temp);
+    return temp;
+}
+
+
+inline void SparseMat::assignTo( SparseMat& m, int type ) const
+{
+    if( type < 0 )
+        m = *this;
+    else
+        convertTo(m, type);
+}
+
+inline void SparseMat::addref()
+{ if( hdr ) ++hdr->refcount; }
+
+inline void SparseMat::release()
+{
+    if( hdr && --hdr->refcount == 0 )
+        delete hdr;
+    hdr = 0;
+}
+
+inline size_t SparseMat::elemSize() const
+{ return CV_ELEM_SIZE(flags); }
+
+inline size_t SparseMat::elemSize1() const
+{ return CV_ELEM_SIZE1(flags); }
+
+inline int SparseMat::type() const
+{ return CV_MAT_TYPE(flags); }
+
+inline int SparseMat::depth() const
+{ return CV_MAT_DEPTH(flags); }
+
+inline int SparseMat::channels() const
+{ return CV_MAT_CN(flags); }
+
+inline Vector<int> SparseMat::size() const
+{
+    if( !hdr )
+        return Vector<int>();
+    return Vector<int>(hdr->size, hdr->dims, true);
+}
+
+inline int SparseMat::size(int i) const
+{
+    if( hdr )
+    {
+        CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
+        return hdr->size[i];
+    }
+    return 0;
+}
+
+inline int SparseMat::dims() const
+{
+    return hdr ? hdr->dims : 0;
+}
+
+inline size_t SparseMat::nzcount() const
+{
+    return hdr ? hdr->nodeCount : 0;
+}
+
+inline size_t SparseMat::hash(int i0) const
+{
+    return (size_t)i0;
+}
+
+inline size_t SparseMat::hash(int i0, int i1) const
+{
+    return (size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1;
+}
+
+inline size_t SparseMat::hash(int i0, int i1, int i2) const
+{
+    return ((size_t)(unsigned)i0*HASH_SCALE + (unsigned)i1)*HASH_SCALE + (unsigned)i2;
+}
+
+inline size_t SparseMat::hash(const int* idx) const
+{
+    size_t h = (unsigned)idx[0];
+    if( !hdr )
+        return 0;
+    int i, d = hdr->dims;
+    for( i = 1; i < d; i++ )
+        h = h*HASH_SCALE + (unsigned)idx[i];
+    return h;
+}
+
+template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
+{ return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); }
+
+template<typename _Tp> inline _Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
+{ return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); }
+
+template<typename _Tp> inline _Tp& SparseMat::ref(const int* idx, size_t* hashval)
+{ return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval); }
+
+template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, size_t* hashval) const
+{
+    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
+    return p ? *p : _Tp();
+}
+
+template<typename _Tp> inline _Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
+{
+    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
+    return p ? *p : _Tp();
+}
+
+template<typename _Tp> inline _Tp SparseMat::value(const int* idx, size_t* hashval) const
+{
+    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
+    return p ? *p : _Tp();
+}
+
+template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
+{ return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval); }
+
+template<typename _Tp> inline const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
+{ return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval); }
+
+template<typename _Tp> inline const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
+{ return (const _Tp*)((SparseMat*)this)->ptr(idx, true, hashval); }
+
+template<typename _Tp> inline _Tp& SparseMat::value(Node* n)
+{ return *(_Tp*)((uchar*)n + hdr->valueOffset); }
+
+template<typename _Tp> inline const _Tp& SparseMat::value(const Node* n) const
+{ return *(const _Tp*)((const uchar*)n + hdr->valueOffset); }
+
+inline SparseMat::Node* SparseMat::node(size_t nidx)
+{ return (Node*)&hdr->pool[nidx]; }
+
+inline const SparseMat::Node* SparseMat::node(size_t nidx) const
+{ return (const Node*)&hdr->pool[nidx]; }
+
+inline SparseMatIterator SparseMat::begin()
+{ return SparseMatIterator(this); }
+
+inline SparseMatConstIterator SparseMat::begin() const
+{ return SparseMatConstIterator(this); }
+
+inline SparseMatIterator SparseMat::end()
+{
+    SparseMatIterator it;
+    it.m = this;
+    if( hdr )
+    {
+        it.hashidx = hdr->hashtab.size();
+        it.ptr = 0;
+    }
+    return it;
+}
+
+inline SparseMatConstIterator SparseMat::end() const
+{
+    SparseMatConstIterator it;
+    it.m = this;
+    if( hdr )
+    {
+        it.hashidx = hdr->hashtab.size();
+        it.ptr = 0;
+    }
+    return it;
+}
+
+inline SparseMatConstIterator::SparseMatConstIterator()
+: m(0), hashidx(0), ptr(0)
+{
+}
+
+inline SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
+: m(it.m), hashidx(it.hashidx), ptr(it.ptr)
+{
+}
+
+static inline bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
+{ return it1.m == it2.m && it1.hashidx == it2.hashidx && it1.ptr == it2.ptr; }
+
+static inline bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
+{ return !(it1 == it2); }
+
+
+inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
+{
+    if( this != &it )
+    {
+        m = it.m;
+        hashidx = it.hashidx;
+        ptr = it.ptr;
+    }
+    return *this;
+}
+
+template<typename _Tp> inline const _Tp& SparseMatConstIterator::value() const
+{ return *(_Tp*)ptr; }
+
+inline const SparseMat::Node* SparseMatConstIterator::node() const
+{
+    return ptr && m && m->hdr ?
+        (const SparseMat::Node*)(ptr - m->hdr->valueOffset) : 0;
+}
+
+inline SparseMatConstIterator SparseMatConstIterator::operator ++(int)
+{
+    SparseMatConstIterator it = *this;
+    ++*this;
+    return it;
+}
+
+inline SparseMatIterator::SparseMatIterator()
+{}
+
+inline SparseMatIterator::SparseMatIterator(SparseMat* _m)
+: SparseMatConstIterator(_m)
+{}
+
+inline SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
+: SparseMatConstIterator(it)
+{
+}
+
+inline SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
+{
+    (SparseMatConstIterator&)*this = it;
+    return *this;
+}
+
+template<typename _Tp> inline _Tp& SparseMatIterator::value() const
+{ return *(_Tp*)ptr; }
+
+inline SparseMat::Node* SparseMatIterator::node() const
+{
+    return (SparseMat::Node*)SparseMatConstIterator::node();
+}
+
+inline SparseMatIterator& SparseMatIterator::operator ++()
+{
+    SparseMatConstIterator::operator ++();
+    return *this;
+}
+
+inline SparseMatIterator SparseMatIterator::operator ++(int)
+{
+    SparseMatIterator it = *this;
+    ++*this;
+    return it;
+}
+
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_()
+{ flags = MAGIC_VAL | DataType<_Tp>::type; }
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Vector<int>& _sizes)
+: SparseMat(_sizes, DataType<_Tp>::type)
+{}
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
+{
+    if( m.type() == DataType<_Tp>::type )
+        *this = (const SparseMat_<_Tp>&)m;
+    else
+        m.convertTo(this, DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
+{
+    this->flags = m.flags;
+    this->hdr = m.hdr;
+    if( this->hdr )
+        ++this->hdr->refcount;
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const Mat& m)
+{
+    SparseMat sm(m);
+    *this = sm;
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const MatND& m)
+{
+    SparseMat sm(m);
+    *this = sm;
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>::SparseMat_(const CvSparseMat* m)
+{
+    SparseMat sm(m);
+    *this = sm;
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>&
+SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
+{
+    if( this != &m )
+    {
+        if( m.hdr ) ++m.hdr->refcount;
+        release();
+        flags = m.flags;
+        hdr = m.hdr;
+    }
+    return *this;
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>&
+SparseMat_<_Tp>::operator = (const SparseMat& m)
+{
+    if( m.type() == DataType<_Tp>::type )
+        return (*this = (const SparseMat_<_Tp>&)m);
+    m.convertTo(*this, DataType<_Tp>::type);
+    return *this;
+}
+
+template<typename _Tp> inline SparseMat_<_Tp>&
+SparseMat_<_Tp>::operator = (const Mat& m)
+{ return (*this = SparseMat(m)); }
+
+template<typename _Tp> inline SparseMat_<_Tp>&
+SparseMat_<_Tp>::operator = (const MatND& m)
+{ return (*this = SparseMat(m)); }
+
+template<typename _Tp> inline SparseMat_<_Tp>
+SparseMat_<_Tp>::clone() const
+{
+    SparseMat_<_Tp> m;
+    this->copyTo(m);
+    return m;
+}
+
+template<typename _Tp> inline void
+SparseMat_<_Tp>::create(const Vector<int>& _sizes)
+{
+    SparseMat::create(_sizes, DataType<_Tp>::type);
+}
+
+template<typename _Tp> inline
+SparseMat_<_Tp>::operator CvSparseMat*() const
+{
+    return SparseMat::operator CvSparseMat*();
+}
+
+template<typename _Tp> inline int SparseMat_<_Tp>::type() const
+{ return DataType<_Tp>::type; }
+
+template<typename _Tp> inline int SparseMat_<_Tp>::depth() const
+{ return DataType<_Tp>::depth; }
+
+template<typename _Tp> inline int SparseMat_<_Tp>::channels() const
+{ return DataType<_Tp>::channels; }
+
+template<typename _Tp> inline _Tp&
+SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
+{ return SparseMat::ref<_Tp>(i0, i1, hashval); }
+
+template<typename _Tp> inline _Tp
+SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
+{ return SparseMat::value<_Tp>(i0, i1, hashval); }
+
+template<typename _Tp> inline _Tp&
+SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
+{ return SparseMat::ref<_Tp>(i0, i1, i2, hashval); }
+
+template<typename _Tp> inline _Tp
+SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
+{ return SparseMat::value<_Tp>(i0, i1, i2, hashval); }
+
+template<typename _Tp> inline _Tp&
+SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
+{ return SparseMat::ref<_Tp>(idx, hashval); }
+
+template<typename _Tp> inline _Tp
+SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
+{ return SparseMat::value<_Tp>(idx, hashval); }
+
+template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
+{ return SparseMatIterator_<_Tp>(this); }
+
+template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
+{ return SparseMatConstIterator_<_Tp>(this); }
+
+template<typename _Tp> inline SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
+{
+    SparseMatIterator_<_Tp> it;
+    it.m = this;
+    if( hdr )
+    {
+        it.hashidx = hdr->hashtab.size();
+        it.ptr = 0;
+    }
+    return it;
+}
+
+template<typename _Tp> inline SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
+{
+    SparseMatConstIterator_<_Tp> it;
+    it.m = this;
+    if( hdr )
+    {
+        it.hashidx = hdr->hashtab.size();
+        it.ptr = 0;
+    }
+    return it;
+}
+
+
+template<typename _Tp> inline
+SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
+{}
+
+template<typename _Tp> inline
+SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
+: SparseMatConstIterator(_m)
+{}
+
+template<typename _Tp> inline
+SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
+: SparseMatConstIterator(it)
+{}
+
+template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
+SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
+{ return ((SparseMatConstIterator&)*this = it); }
+
+template<typename _Tp> inline const _Tp&
+SparseMatConstIterator_<_Tp>::operator *() const
+{ return *(const _Tp*)this->ptr; }
+
+template<typename _Tp> inline SparseMatConstIterator_<_Tp>&
+SparseMatConstIterator_<_Tp>::operator ++()
+{
+    SparseMatConstIterator::operator ++();
+    return *this;
+}
+
+template<typename _Tp> inline SparseMatConstIterator_<_Tp>
+SparseMatConstIterator_<_Tp>::operator ++(int)
+{
+    SparseMatConstIterator it = *this;
+    SparseMatConstIterator::operator ++();
+    return it;
+}
+
+template<typename _Tp> inline
+SparseMatIterator_<_Tp>::SparseMatIterator_()
+{}
+
+template<typename _Tp> inline
+SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
+: SparseMatConstIterator_<_Tp>(_m)
+{}
+
+template<typename _Tp> inline
+SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
+: SparseMatConstIterator_<_Tp>(it)
+{}
+
+template<typename _Tp> inline SparseMatIterator_<_Tp>&
+SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
+{ return ((SparseMatIterator&)*this = it); }
+
+template<typename _Tp> inline _Tp&
+SparseMatIterator_<_Tp>::operator *() const
+{ return *(_Tp*)this->ptr; }
+
+template<typename _Tp> inline SparseMatIterator_<_Tp>&
+SparseMatIterator_<_Tp>::operator ++()
+{
+    SparseMatConstIterator::operator ++();
+    return *this;
+}
+
+template<typename _Tp> inline SparseMatIterator_<_Tp>
+SparseMatIterator_<_Tp>::operator ++(int)
+{
+    SparseMatIterator it = *this;
+    SparseMatConstIterator::operator ++();
+    return it;
+}
+    
+}
+
+#endif
+#endif
index f4cc7b3e8dd7b0f3730838f71cbd18e5f2df004e..0e264a5ca075fc3d9f75b8e1ac050e5863e5442d 100644 (file)
-/*M///////////////////////////////////////////////////////////////////////////////////////\r
-//\r
-//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
-//\r
-//  By downloading, copying, installing or using the software you agree to this license.\r
-//  If you do not agree to this license, do not download, install,\r
-//  copy or use the software.\r
-//\r
-//\r
-//                           License Agreement\r
-//                For Open Source Computer Vision Library\r
-//\r
-// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
-// Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
-// Third party copyrights are property of their respective owners.\r
-//\r
-// Redistribution and use in source and binary forms, with or without modification,\r
-// are permitted provided that the following conditions are met:\r
-//\r
-//   * Redistribution's of source code must retain the above copyright notice,\r
-//     this list of conditions and the following disclaimer.\r
-//\r
-//   * Redistribution's in binary form must reproduce the above copyright notice,\r
-//     this list of conditions and the following disclaimer in the documentation\r
-//     and/or other materials provided with the distribution.\r
-//\r
-//   * The name of the copyright holders may not be used to endorse or promote products\r
-//     derived from this software without specific prior written permission.\r
-//\r
-// This software is provided by the copyright holders and contributors "as is" and\r
-// any express or implied warranties, including, but not limited to, the implied\r
-// warranties of merchantability and fitness for a particular purpose are disclaimed.\r
-// In no event shall the Intel Corporation or contributors be liable for any direct,\r
-// indirect, incidental, special, exemplary, or consequential damages\r
-// (including, but not limited to, procurement of substitute goods or services;\r
-// loss of use, data, or profits; or business interruption) however caused\r
-// and on any theory of liability, whether in contract, strict liability,\r
-// or tort (including negligence or otherwise) arising in any way out of\r
-// the use of this software, even if advised of the possibility of such damage.\r
-//\r
-//M*/\r
-\r
-/* The header is mostly for internal use and it is likely to change.\r
-   It contains some macro definitions that are used in cxcore, cv, cvaux\r
-   and, probably, other libraries. If you need some of this functionality,\r
-   the safe way is to copy it into your code and rename the macros.\r
-*/\r
-#ifndef _CXCORE_MISC_H_\r
-#define _CXCORE_MISC_H_\r
-\r
-#ifdef HAVE_CONFIG_H\r
-    #include "cvconfig.h"\r
-#endif\r
-\r
-#include <limits.h>\r
-#ifdef _OPENMP\r
-#include "omp.h"\r
-#endif\r
-\r
-/****************************************************************************************\\r
-*                              Compile-time tuning parameters                            *\r
-\****************************************************************************************/\r
-\r
-/* maximal size of vector to run matrix operations on it inline (i.e. w/o ipp calls) */\r
-#define  CV_MAX_INLINE_MAT_OP_SIZE  10\r
-\r
-/* maximal linear size of matrix to allocate it on stack. */\r
-#define  CV_MAX_LOCAL_MAT_SIZE  32\r
-\r
-/* maximal size of local memory storage */\r
-#define  CV_MAX_LOCAL_SIZE  \\r
-    (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))\r
-\r
-/* default image row align (in bytes) */\r
-#define  CV_DEFAULT_IMAGE_ROW_ALIGN  4\r
-\r
-/* matrices are continuous by default */\r
-#define  CV_DEFAULT_MAT_ROW_ALIGN  1\r
-\r
-/* maximum size of dynamic memory buffer.\r
-   cvAlloc reports an error if a larger block is requested. */\r
-#define  CV_MAX_ALLOC_SIZE    (((size_t)1 << (sizeof(size_t)*8-2)))\r
-\r
-/* the alignment of all the allocated buffers */\r
-#define  CV_MALLOC_ALIGN    32\r
-\r
-/* default alignment for dynamic data strucutures, resided in storages. */\r
-#define  CV_STRUCT_ALIGN    ((int)sizeof(double))\r
-\r
-/* default storage block size */\r
-#define  CV_STORAGE_BLOCK_SIZE   ((1<<16) - 128)\r
-\r
-/* default memory block for sparse array elements */\r
-#define  CV_SPARSE_MAT_BLOCK    (1<<12)\r
-\r
-/* initial hash table size */\r
-#define  CV_SPARSE_HASH_SIZE0    (1<<10)\r
-\r
-/* maximal average node_count/hash_size ratio beyond which hash table is resized */\r
-#define  CV_SPARSE_HASH_RATIO    3\r
-\r
-/* max length of strings */\r
-#define  CV_MAX_STRLEN  1024\r
-\r
-/* maximum possible number of threads in parallel implementations */\r
-#ifdef _OPENMP\r
-#define CV_MAX_THREADS 128\r
-#else\r
-#define CV_MAX_THREADS 1\r
-#endif\r
-\r
-#if 0 /*def  CV_CHECK_FOR_NANS*/\r
-    #define CV_CHECK_NANS( arr ) cvCheckArray((arr))\r
-#else\r
-    #define CV_CHECK_NANS( arr )\r
-#endif\r
-\r
-/****************************************************************************************\\r
-*                                  Common declarations                                   *\r
-\****************************************************************************************/\r
-\r
-/* get alloca declaration */\r
-#ifdef __GNUC__\r
-    #undef alloca\r
-    #define alloca __builtin_alloca\r
-#elif defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || \\r
-      defined WINCE || defined _MSC_VER || defined __BORLANDC__\r
-    #include <malloc.h>\r
-#elif defined HAVE_ALLOCA_H\r
-    #include <alloca.h>\r
-#elif defined HAVE_ALLOCA\r
-    #include <stdlib.h>\r
-#else\r
-    #error "No alloca!"\r
-#endif\r
-\r
-#ifdef __GNUC__\r
-#define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))\r
-#elif defined _MSC_VER\r
-#define CV_DECL_ALIGNED(x) __declspec(align(x))\r
-#else\r
-#define CV_DECL_ALIGNED(x)\r
-#endif\r
-\r
-/* ! DO NOT make it an inline function */\r
-#define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN )\r
-\r
-#if defined _MSC_VER || defined __BORLANDC__\r
-    #define CV_BIG_INT(n)   n##I64\r
-    #define CV_BIG_UINT(n)  n##UI64\r
-#else\r
-    #define CV_BIG_INT(n)   n##LL\r
-    #define CV_BIG_UINT(n)  n##ULL\r
-#endif\r
-\r
-#define CV_IMPL CV_EXTERN_C\r
-\r
-#define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; }\r
-\r
-/* default step, set in case of continuous data\r
-   to work around checks for valid step in some ipp functions */\r
-#define  CV_STUB_STEP     (1 << 30)\r
-\r
-#define  CV_SIZEOF_FLOAT ((int)sizeof(float))\r
-#define  CV_SIZEOF_SHORT ((int)sizeof(short))\r
-\r
-#define  CV_ORIGIN_TL  0\r
-#define  CV_ORIGIN_BL  1\r
-\r
-/* IEEE754 constants and macros */\r
-#define  CV_POS_INF       0x7f800000\r
-#define  CV_NEG_INF       0x807fffff /* CV_TOGGLE_FLT(0xff800000) */\r
-#define  CV_1F            0x3f800000\r
-#define  CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))\r
-#define  CV_TOGGLE_DBL(x) \\r
-    ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))\r
-\r
-#define  CV_NOP(a)      (a)\r
-#define  CV_ADD(a, b)   ((a) + (b))\r
-#define  CV_SUB(a, b)   ((a) - (b))\r
-#define  CV_MUL(a, b)   ((a) * (b))\r
-#define  CV_AND(a, b)   ((a) & (b))\r
-#define  CV_OR(a, b)    ((a) | (b))\r
-#define  CV_XOR(a, b)   ((a) ^ (b))\r
-#define  CV_ANDN(a, b)  (~(a) & (b))\r
-#define  CV_ORN(a, b)   (~(a) | (b))\r
-#define  CV_SQR(a)      ((a) * (a))\r
-\r
-#define  CV_LT(a, b)    ((a) < (b))\r
-#define  CV_LE(a, b)    ((a) <= (b))\r
-#define  CV_EQ(a, b)    ((a) == (b))\r
-#define  CV_NE(a, b)    ((a) != (b))\r
-#define  CV_GT(a, b)    ((a) > (b))\r
-#define  CV_GE(a, b)    ((a) >= (b))\r
-\r
-#define  CV_NONZERO(a)      ((a) != 0)\r
-#define  CV_NONZERO_FLT(a)  (((a)+(a)) != 0)\r
-\r
-/* general-purpose saturation macros */\r
-#define  CV_CAST_8U(t)  (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)\r
-#define  CV_CAST_8S(t)  (schar)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)\r
-#define  CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)\r
-#define  CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)\r
-#define  CV_CAST_32S(t) (int)(t)\r
-#define  CV_CAST_64S(t) (int64)(t)\r
-#define  CV_CAST_32F(t) (float)(t)\r
-#define  CV_CAST_64F(t) (double)(t)\r
-\r
-#define  CV_PASTE2(a,b) a##b\r
-#define  CV_PASTE(a,b)  CV_PASTE2(a,b)\r
-\r
-#define  CV_EMPTY\r
-#define  CV_MAKE_STR(a) #a\r
-\r
-#define  CV_DEFINE_MASK         \\r
-    float maskTab[2]; maskTab[0] = 0.f; maskTab[1] = 1.f;\r
-#define  CV_ANDMASK( m, x )     ((x) & (((m) == 0) - 1))\r
-\r
-/* (x) * ((m) == 1 ? 1.f : (m) == 0 ? 0.f : <ERR> */\r
-#define  CV_MULMASK( m, x )       (maskTab[(m) != 0]*(x))\r
-\r
-/* (x) * ((m) == -1 ? 1.f : (m) == 0 ? 0.f : <ERR> */\r
-#define  CV_MULMASK1( m, x )      (maskTab[(m)+1]*(x))\r
-\r
-#define  CV_ZERO_OBJ(x)  memset((x), 0, sizeof(*(x)))\r
-\r
-#define  CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))\r
-\r
-#define  cvUnsupportedFormat "Unsupported format"\r
-\r
-CV_INLINE void* cvAlignPtr( const void* ptr, int align CV_DEFAULT(32) )\r
-{\r
-    assert( (align & (align-1)) == 0 );\r
-    return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );\r
-}\r
-\r
-CV_INLINE int cvAlign( int size, int align )\r
-{\r
-    assert( (align & (align-1)) == 0 && size < INT_MAX );\r
-    return (size + align - 1) & -align;\r
-}\r
-\r
-CV_INLINE  CvSize  cvGetMatSize( const CvMat* mat )\r
-{\r
-    CvSize size;\r
-    size.width = mat->cols;\r
-    size.height = mat->rows;\r
-    return size;\r
-}\r
-\r
-#define  CV_DESCALE(x,n)     (((x) + (1 << ((n)-1))) >> (n))\r
-#define  CV_FLT_TO_FIX(x,n)  cvRound((x)*(1<<(n)))\r
-\r
-\r
-#define CV_MEMCPY_CHAR( dst, src, len )                                             \\r
-{                                                                                   \\r
-    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \\r
-    char* _icv_memcpy_dst_ = (char*)(dst);                                          \\r
-    const char* _icv_memcpy_src_ = (const char*)(src);                              \\r
-                                                                                    \\r
-    for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ )  \\r
-        _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];        \\r
-}\r
-\r
-\r
-#define CV_MEMCPY_INT( dst, src, len )                                              \\r
-{                                                                                   \\r
-    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \\r
-    int* _icv_memcpy_dst_ = (int*)(dst);                                            \\r
-    const int* _icv_memcpy_src_ = (const int*)(src);                                \\r
-    assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 &&                      \\r
-            ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                      \\r
-                                                                                    \\r
-    for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++)          \\r
-        _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];        \\r
-}\r
-\r
-\r
-#define CV_MEMCPY_AUTO( dst, src, len )                                             \\r
-{                                                                                   \\r
-    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \\r
-    char* _icv_memcpy_dst_ = (char*)(dst);                                          \\r
-    const char* _icv_memcpy_src_ = (const char*)(src);                              \\r
-    if( (_icv_memcpy_len_ & (sizeof(int)-1)) == 0 )                                 \\r
-    {                                                                               \\r
-        assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 &&                  \\r
-                ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                  \\r
-        for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_;                 \\r
-            _icv_memcpy_i_+=sizeof(int) )                                           \\r
-        {                                                                           \\r
-            *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) =                              \\r
-            *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_);                         \\r
-        }                                                                           \\r
-    }                                                                               \\r
-    else                                                                            \\r
-    {                                                                               \\r
-        for(_icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++)\\r
-            _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];    \\r
-    }                                                                               \\r
-}\r
-\r
-\r
-#define CV_ZERO_CHAR( dst, len )                                                    \\r
-{                                                                                   \\r
-    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \\r
-    char* _icv_memcpy_dst_ = (char*)(dst);                                          \\r
-                                                                                    \\r
-    for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ )  \\r
-        _icv_memcpy_dst_[_icv_memcpy_i_] = '\0';                                    \\r
-}\r
-\r
-\r
-#define CV_ZERO_INT( dst, len )                                                     \\r
-{                                                                                   \\r
-    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \\r
-    int* _icv_memcpy_dst_ = (int*)(dst);                                            \\r
-    assert( ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                      \\r
-                                                                                    \\r
-    for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++)          \\r
-        _icv_memcpy_dst_[_icv_memcpy_i_] = 0;                                       \\r
-}\r
-\r
-\r
-/****************************************************************************************\\r
-\r
-  Generic implementation of QuickSort algorithm.\r
-  ----------------------------------------------\r
-  Using this macro user can declare customized sort function that can be much faster\r
-  than built-in qsort function because of lower overhead on elements\r
-  comparison and exchange. The macro takes less_than (or LT) argument - a macro or function\r
-  that takes 2 arguments returns non-zero if the first argument should be before the second\r
-  one in the sorted sequence and zero otherwise.\r
-\r
-  Example:\r
-\r
-    Suppose that the task is to sort points by ascending of y coordinates and if\r
-    y's are equal x's should ascend.\r
-\r
-    The code is:\r
-    ------------------------------------------------------------------------------\r
-           #define cmp_pts( pt1, pt2 ) \\r
-               ((pt1).y < (pt2).y || ((pt1).y < (pt2).y && (pt1).x < (pt2).x))\r
-\r
-           [static] CV_IMPLEMENT_QSORT( icvSortPoints, CvPoint, cmp_pts )\r
-    ------------------------------------------------------------------------------\r
-\r
-    After that the function "void icvSortPoints( CvPoint* array, size_t total, int aux );"\r
-    is available to user.\r
-\r
-  aux is an additional parameter, which can be used when comparing elements.\r
-  The current implementation was derived from *BSD system qsort():\r
-\r
-    * Copyright (c) 1992, 1993\r
-    *  The Regents of the University of California.  All rights reserved.\r
-    *\r
-    * Redistribution and use in source and binary forms, with or without\r
-    * modification, are permitted provided that the following conditions\r
-    * are met:\r
-    * 1. Redistributions of source code must retain the above copyright\r
-    *    notice, this list of conditions and the following disclaimer.\r
-    * 2. Redistributions in binary form must reproduce the above copyright\r
-    *    notice, this list of conditions and the following disclaimer in the\r
-    *    documentation and/or other materials provided with the distribution.\r
-    * 3. All advertising materials mentioning features or use of this software\r
-    *    must display the following acknowledgement:\r
-    *  This product includes software developed by the University of\r
-    *  California, Berkeley and its contributors.\r
-    * 4. Neither the name of the University nor the names of its contributors\r
-    *    may be used to endorse or promote products derived from this software\r
-    *    without specific prior written permission.\r
-    *\r
-    * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
-    * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-    * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
-    * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
-    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
-    * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
-    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
-    * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
-    * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
-    * SUCH DAMAGE.\r
-\r
-\****************************************************************************************/\r
-\r
-#define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type )                   \\r
-void func_name( T *array, size_t total, user_data_type aux )                        \\r
-{                                                                                   \\r
-    int isort_thresh = 7;                                                           \\r
-    T t;                                                                            \\r
-    int sp = 0;                                                                     \\r
-                                                                                    \\r
-    struct                                                                          \\r
-    {                                                                               \\r
-        T *lb;                                                                      \\r
-        T *ub;                                                                      \\r
-    }                                                                               \\r
-    stack[48];                                                                      \\r
-                                                                                    \\r
-    aux = aux;                                                                      \\r
-                                                                                    \\r
-    if( total <= 1 )                                                                \\r
-        return;                                                                     \\r
-                                                                                    \\r
-    stack[0].lb = array;                                                            \\r
-    stack[0].ub = array + (total - 1);                                              \\r
-                                                                                    \\r
-    while( sp >= 0 )                                                                \\r
-    {                                                                               \\r
-        T* left = stack[sp].lb;                                                     \\r
-        T* right = stack[sp--].ub;                                                  \\r
-                                                                                    \\r
-        for(;;)                                                                     \\r
-        {                                                                           \\r
-            int i, n = (int)(right - left) + 1, m;                                  \\r
-            T* ptr;                                                                 \\r
-            T* ptr2;                                                                \\r
-                                                                                    \\r
-            if( n <= isort_thresh )                                                 \\r
-            {                                                                       \\r
-            insert_sort:                                                            \\r
-                for( ptr = left + 1; ptr <= right; ptr++ )                          \\r
-                {                                                                   \\r
-                    for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--)   \\r
-                        CV_SWAP( ptr2[0], ptr2[-1], t );                            \\r
-                }                                                                   \\r
-                break;                                                              \\r
-            }                                                                       \\r
-            else                                                                    \\r
-            {                                                                       \\r
-                T* left0;                                                           \\r
-                T* left1;                                                           \\r
-                T* right0;                                                          \\r
-                T* right1;                                                          \\r
-                T* pivot;                                                           \\r
-                T* a;                                                               \\r
-                T* b;                                                               \\r
-                T* c;                                                               \\r
-                int swap_cnt = 0;                                                   \\r
-                                                                                    \\r
-                left0 = left;                                                       \\r
-                right0 = right;                                                     \\r
-                pivot = left + (n/2);                                               \\r
-                                                                                    \\r
-                if( n > 40 )                                                        \\r
-                {                                                                   \\r
-                    int d = n / 8;                                                  \\r
-                    a = left, b = left + d, c = left + 2*d;                         \\r
-                    left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))     \\r
-                                      : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \\r
-                                                                                    \\r
-                    a = pivot - d, b = pivot, c = pivot + d;                        \\r
-                    pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))    \\r
-                                      : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \\r
-                                                                                    \\r
-                    a = right - 2*d, b = right - d, c = right;                      \\r
-                    right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))    \\r
-                                      : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \\r
-                }                                                                   \\r
-                                                                                    \\r
-                a = left, b = pivot, c = right;                                     \\r
-                pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))        \\r
-                                   : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));       \\r
-                if( pivot != left0 )                                                \\r
-                {                                                                   \\r
-                    CV_SWAP( *pivot, *left0, t );                                   \\r
-                    pivot = left0;                                                  \\r
-                }                                                                   \\r
-                left = left1 = left0 + 1;                                           \\r
-                right = right1 = right0;                                            \\r
-                                                                                    \\r
-                for(;;)                                                             \\r
-                {                                                                   \\r
-                    while( left <= right && !LT(*pivot, *left) )                    \\r
-                    {                                                               \\r
-                        if( !LT(*left, *pivot) )                                    \\r
-                        {                                                           \\r
-                            if( left > left1 )                                      \\r
-                                CV_SWAP( *left1, *left, t );                        \\r
-                            swap_cnt = 1;                                           \\r
-                            left1++;                                                \\r
-                        }                                                           \\r
-                        left++;                                                     \\r
-                    }                                                               \\r
-                                                                                    \\r
-                    while( left <= right && !LT(*right, *pivot) )                   \\r
-                    {                                                               \\r
-                        if( !LT(*pivot, *right) )                                   \\r
-                        {                                                           \\r
-                            if( right < right1 )                                    \\r
-                                CV_SWAP( *right1, *right, t );                      \\r
-                            swap_cnt = 1;                                           \\r
-                            right1--;                                               \\r
-                        }                                                           \\r
-                        right--;                                                    \\r
-                    }                                                               \\r
-                                                                                    \\r
-                    if( left > right )                                              \\r
-                        break;                                                      \\r
-                    CV_SWAP( *left, *right, t );                                    \\r
-                    swap_cnt = 1;                                                   \\r
-                    left++;                                                         \\r
-                    right--;                                                        \\r
-                }                                                                   \\r
-                                                                                    \\r
-                if( swap_cnt == 0 )                                                 \\r
-                {                                                                   \\r
-                    left = left0, right = right0;                                   \\r
-                    goto insert_sort;                                               \\r
-                }                                                                   \\r
-                                                                                    \\r
-                n = MIN( (int)(left1 - left0), (int)(left - left1) );               \\r
-                for( i = 0; i < n; i++ )                                            \\r
-                    CV_SWAP( left0[i], left[i-n], t );                              \\r
-                                                                                    \\r
-                n = MIN( (int)(right0 - right1), (int)(right1 - right) );           \\r
-                for( i = 0; i < n; i++ )                                            \\r
-                    CV_SWAP( left[i], right0[i-n+1], t );                           \\r
-                n = (int)(left - left1);                                            \\r
-                m = (int)(right1 - right);                                          \\r
-                if( n > 1 )                                                         \\r
-                {                                                                   \\r
-                    if( m > 1 )                                                     \\r
-                    {                                                               \\r
-                        if( n > m )                                                 \\r
-                        {                                                           \\r
-                            stack[++sp].lb = left0;                                 \\r
-                            stack[sp].ub = left0 + n - 1;                           \\r
-                            left = right0 - m + 1, right = right0;                  \\r
-                        }                                                           \\r
-                        else                                                        \\r
-                        {                                                           \\r
-                            stack[++sp].lb = right0 - m + 1;                        \\r
-                            stack[sp].ub = right0;                                  \\r
-                            left = left0, right = left0 + n - 1;                    \\r
-                        }                                                           \\r
-                    }                                                               \\r
-                    else                                                            \\r
-                        left = left0, right = left0 + n - 1;                        \\r
-                }                                                                   \\r
-                else if( m > 1 )                                                    \\r
-                    left = right0 - m + 1, right = right0;                          \\r
-                else                                                                \\r
-                    break;                                                          \\r
-            }                                                                       \\r
-        }                                                                           \\r
-    }                                                                               \\r
-}\r
-\r
-#define CV_IMPLEMENT_QSORT( func_name, T, cmp )  \\r
-    CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )\r
-\r
-/****************************************************************************************\\r
-*                     Structures and macros for integration with IPP                     *\r
-\****************************************************************************************/\r
-\r
-/* IPP-compatible return codes */\r
-typedef enum CvStatus\r
-{\r
-    CV_BADMEMBLOCK_ERR          = -113,\r
-    CV_INPLACE_NOT_SUPPORTED_ERR= -112,\r
-    CV_UNMATCHED_ROI_ERR        = -111,\r
-    CV_NOTFOUND_ERR             = -110,\r
-    CV_BADCONVERGENCE_ERR       = -109,\r
-\r
-    CV_BADDEPTH_ERR             = -107,\r
-    CV_BADROI_ERR               = -106,\r
-    CV_BADHEADER_ERR            = -105,\r
-    CV_UNMATCHED_FORMATS_ERR    = -104,\r
-    CV_UNSUPPORTED_COI_ERR      = -103,\r
-    CV_UNSUPPORTED_CHANNELS_ERR = -102,\r
-    CV_UNSUPPORTED_DEPTH_ERR    = -101,\r
-    CV_UNSUPPORTED_FORMAT_ERR   = -100,\r
-\r
-    CV_BADARG_ERR      = -49,  //ipp comp\r
-    CV_NOTDEFINED_ERR  = -48,  //ipp comp\r
-\r
-    CV_BADCHANNELS_ERR = -47,  //ipp comp\r
-    CV_BADRANGE_ERR    = -44,  //ipp comp\r
-    CV_BADSTEP_ERR     = -29,  //ipp comp\r
-\r
-    CV_BADFLAG_ERR     =  -12,\r
-    CV_DIV_BY_ZERO_ERR =  -11, //ipp comp\r
-    CV_BADCOEF_ERR     =  -10,\r
-\r
-    CV_BADFACTOR_ERR   =  -7,\r
-    CV_BADPOINT_ERR    =  -6,\r
-    CV_BADSCALE_ERR    =  -4,\r
-    CV_OUTOFMEM_ERR    =  -3,\r
-    CV_NULLPTR_ERR     =  -2,\r
-    CV_BADSIZE_ERR     =  -1,\r
-    CV_NO_ERR          =   0,\r
-    CV_OK              =   CV_NO_ERR\r
-}\r
-CvStatus;\r
-\r
-#define IPPI_CALL(stmt) CV_Assert((stmt) >= 0)\r
-\r
-#define CV_NOTHROW throw()\r
-\r
-typedef struct CvFuncTable\r
-{\r
-    void*   fn_2d[CV_DEPTH_MAX];\r
-}\r
-CvFuncTable;\r
-\r
-typedef struct CvBigFuncTable\r
-{\r
-    void*   fn_2d[CV_DEPTH_MAX*CV_CN_MAX];\r
-}\r
-CvBigFuncTable;\r
-\r
-#define CV_INIT_FUNC_TAB( tab, FUNCNAME, FLAG )         \\r
-    (tab).fn_2d[CV_8U] = (void*)FUNCNAME##_8u##FLAG;    \\r
-    (tab).fn_2d[CV_8S] = 0;                             \\r
-    (tab).fn_2d[CV_16U] = (void*)FUNCNAME##_16u##FLAG;  \\r
-    (tab).fn_2d[CV_16S] = (void*)FUNCNAME##_16s##FLAG;  \\r
-    (tab).fn_2d[CV_32S] = (void*)FUNCNAME##_32s##FLAG;  \\r
-    (tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG;  \\r
-    (tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG\r
-\r
-#endif /*_CXCORE_MISC_H_*/\r
+/*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, 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*/
+
+/* The header is mostly for internal use and it is likely to change.
+   It contains some macro definitions that are used in cxcore, cv, cvaux
+   and, probably, other libraries. If you need some of this functionality,
+   the safe way is to copy it into your code and rename the macros.
+*/
+#ifndef _CXCORE_MISC_H_
+#define _CXCORE_MISC_H_
+
+#ifdef HAVE_CONFIG_H
+    #include "cvconfig.h"
+#endif
+
+#include <limits.h>
+#ifdef _OPENMP
+#include "omp.h"
+#endif
+
+/****************************************************************************************\
+*                              Compile-time tuning parameters                            *
+\****************************************************************************************/
+
+/* maximal size of vector to run matrix operations on it inline (i.e. w/o ipp calls) */
+#define  CV_MAX_INLINE_MAT_OP_SIZE  10
+
+/* maximal linear size of matrix to allocate it on stack. */
+#define  CV_MAX_LOCAL_MAT_SIZE  32
+
+/* maximal size of local memory storage */
+#define  CV_MAX_LOCAL_SIZE  \
+    (CV_MAX_LOCAL_MAT_SIZE*CV_MAX_LOCAL_MAT_SIZE*(int)sizeof(double))
+
+/* default image row align (in bytes) */
+#define  CV_DEFAULT_IMAGE_ROW_ALIGN  4
+
+/* matrices are continuous by default */
+#define  CV_DEFAULT_MAT_ROW_ALIGN  1
+
+/* maximum size of dynamic memory buffer.
+   cvAlloc reports an error if a larger block is requested. */
+#define  CV_MAX_ALLOC_SIZE    (((size_t)1 << (sizeof(size_t)*8-2)))
+
+/* the alignment of all the allocated buffers */
+#define  CV_MALLOC_ALIGN    32
+
+/* default alignment for dynamic data strucutures, resided in storages. */
+#define  CV_STRUCT_ALIGN    ((int)sizeof(double))
+
+/* default storage block size */
+#define  CV_STORAGE_BLOCK_SIZE   ((1<<16) - 128)
+
+/* default memory block for sparse array elements */
+#define  CV_SPARSE_MAT_BLOCK    (1<<12)
+
+/* initial hash table size */
+#define  CV_SPARSE_HASH_SIZE0    (1<<10)
+
+/* maximal average node_count/hash_size ratio beyond which hash table is resized */
+#define  CV_SPARSE_HASH_RATIO    3
+
+/* max length of strings */
+#define  CV_MAX_STRLEN  1024
+
+/* maximum possible number of threads in parallel implementations */
+#ifdef _OPENMP
+#define CV_MAX_THREADS 128
+#else
+#define CV_MAX_THREADS 1
+#endif
+
+#if 0 /*def  CV_CHECK_FOR_NANS*/
+    #define CV_CHECK_NANS( arr ) cvCheckArray((arr))
+#else
+    #define CV_CHECK_NANS( arr )
+#endif
+
+/****************************************************************************************\
+*                                  Common declarations                                   *
+\****************************************************************************************/
+
+/* get alloca declaration */
+#ifdef __GNUC__
+    #undef alloca
+    #define alloca __builtin_alloca
+#elif defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64 || \
+      defined WINCE || defined _MSC_VER || defined __BORLANDC__
+    #include <malloc.h>
+#elif defined HAVE_ALLOCA_H
+    #include <alloca.h>
+#elif defined HAVE_ALLOCA
+    #include <stdlib.h>
+#else
+    #error "No alloca!"
+#endif
+
+#ifdef __GNUC__
+#define CV_DECL_ALIGNED(x) __attribute__ ((aligned (x)))
+#elif defined _MSC_VER
+#define CV_DECL_ALIGNED(x) __declspec(align(x))
+#else
+#define CV_DECL_ALIGNED(x)
+#endif
+
+/* ! DO NOT make it an inline function */
+#define cvStackAlloc(size) cvAlignPtr( alloca((size) + CV_MALLOC_ALIGN), CV_MALLOC_ALIGN )
+
+#if defined _MSC_VER || defined __BORLANDC__
+    #define CV_BIG_INT(n)   n##I64
+    #define CV_BIG_UINT(n)  n##UI64
+#else
+    #define CV_BIG_INT(n)   n##LL
+    #define CV_BIG_UINT(n)  n##ULL
+#endif
+
+#define CV_IMPL CV_EXTERN_C
+
+#define CV_DBG_BREAK() { volatile int* crashMe = 0; *crashMe = 0; }
+
+/* default step, set in case of continuous data
+   to work around checks for valid step in some ipp functions */
+#define  CV_STUB_STEP     (1 << 30)
+
+#define  CV_SIZEOF_FLOAT ((int)sizeof(float))
+#define  CV_SIZEOF_SHORT ((int)sizeof(short))
+
+#define  CV_ORIGIN_TL  0
+#define  CV_ORIGIN_BL  1
+
+/* IEEE754 constants and macros */
+#define  CV_POS_INF       0x7f800000
+#define  CV_NEG_INF       0x807fffff /* CV_TOGGLE_FLT(0xff800000) */
+#define  CV_1F            0x3f800000
+#define  CV_TOGGLE_FLT(x) ((x)^((int)(x) < 0 ? 0x7fffffff : 0))
+#define  CV_TOGGLE_DBL(x) \
+    ((x)^((int64)(x) < 0 ? CV_BIG_INT(0x7fffffffffffffff) : 0))
+
+#define  CV_NOP(a)      (a)
+#define  CV_ADD(a, b)   ((a) + (b))
+#define  CV_SUB(a, b)   ((a) - (b))
+#define  CV_MUL(a, b)   ((a) * (b))
+#define  CV_AND(a, b)   ((a) & (b))
+#define  CV_OR(a, b)    ((a) | (b))
+#define  CV_XOR(a, b)   ((a) ^ (b))
+#define  CV_ANDN(a, b)  (~(a) & (b))
+#define  CV_ORN(a, b)   (~(a) | (b))
+#define  CV_SQR(a)      ((a) * (a))
+
+#define  CV_LT(a, b)    ((a) < (b))
+#define  CV_LE(a, b)    ((a) <= (b))
+#define  CV_EQ(a, b)    ((a) == (b))
+#define  CV_NE(a, b)    ((a) != (b))
+#define  CV_GT(a, b)    ((a) > (b))
+#define  CV_GE(a, b)    ((a) >= (b))
+
+#define  CV_NONZERO(a)      ((a) != 0)
+#define  CV_NONZERO_FLT(a)  (((a)+(a)) != 0)
+
+/* general-purpose saturation macros */
+#define  CV_CAST_8U(t)  (uchar)(!((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
+#define  CV_CAST_8S(t)  (schar)(!(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128)
+#define  CV_CAST_16U(t) (ushort)(!((t) & ~65535) ? (t) : (t) > 0 ? 65535 : 0)
+#define  CV_CAST_16S(t) (short)(!(((t)+32768) & ~65535) ? (t) : (t) > 0 ? 32767 : -32768)
+#define  CV_CAST_32S(t) (int)(t)
+#define  CV_CAST_64S(t) (int64)(t)
+#define  CV_CAST_32F(t) (float)(t)
+#define  CV_CAST_64F(t) (double)(t)
+
+#define  CV_PASTE2(a,b) a##b
+#define  CV_PASTE(a,b)  CV_PASTE2(a,b)
+
+#define  CV_EMPTY
+#define  CV_MAKE_STR(a) #a
+
+#define  CV_DIM(static_array) ((int)(sizeof(static_array)/sizeof((static_array)[0])))
+
+#define  cvUnsupportedFormat "Unsupported format"
+
+CV_INLINE void* cvAlignPtr( const void* ptr, int align CV_DEFAULT(32) )
+{
+    assert( (align & (align-1)) == 0 );
+    return (void*)( ((size_t)ptr + align - 1) & ~(size_t)(align-1) );
+}
+
+CV_INLINE int cvAlign( int size, int align )
+{
+    assert( (align & (align-1)) == 0 && size < INT_MAX );
+    return (size + align - 1) & -align;
+}
+
+CV_INLINE  CvSize  cvGetMatSize( const CvMat* mat )
+{
+    CvSize size;
+    size.width = mat->cols;
+    size.height = mat->rows;
+    return size;
+}
+
+#define  CV_DESCALE(x,n)     (((x) + (1 << ((n)-1))) >> (n))
+#define  CV_FLT_TO_FIX(x,n)  cvRound((x)*(1<<(n)))
+
+/****************************************************************************************\
+
+  Generic implementation of QuickSort algorithm.
+  ----------------------------------------------
+  Using this macro user can declare customized sort function that can be much faster
+  than built-in qsort function because of lower overhead on elements
+  comparison and exchange. The macro takes less_than (or LT) argument - a macro or function
+  that takes 2 arguments returns non-zero if the first argument should be before the second
+  one in the sorted sequence and zero otherwise.
+
+  Example:
+
+    Suppose that the task is to sort points by ascending of y coordinates and if
+    y's are equal x's should ascend.
+
+    The code is:
+    ------------------------------------------------------------------------------
+           #define cmp_pts( pt1, pt2 ) \
+               ((pt1).y < (pt2).y || ((pt1).y < (pt2).y && (pt1).x < (pt2).x))
+
+           [static] CV_IMPLEMENT_QSORT( icvSortPoints, CvPoint, cmp_pts )
+    ------------------------------------------------------------------------------
+
+    After that the function "void icvSortPoints( CvPoint* array, size_t total, int aux );"
+    is available to user.
+
+  aux is an additional parameter, which can be used when comparing elements.
+  The current implementation was derived from *BSD system qsort():
+
+    * Copyright (c) 1992, 1993
+    *  The Regents of the University of California.  All rights reserved.
+    *
+    * Redistribution and use in source and binary forms, with or without
+    * modification, are permitted provided that the following conditions
+    * are met:
+    * 1. Redistributions of source code must retain the above copyright
+    *    notice, this list of conditions and the following disclaimer.
+    * 2. Redistributions 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.
+    * 3. All advertising materials mentioning features or use of this software
+    *    must display the following acknowledgement:
+    *  This product includes software developed by the University of
+    *  California, Berkeley and its contributors.
+    * 4. Neither the name of the University nor the names of its contributors
+    *    may be used to endorse or promote products derived from this software
+    *    without specific prior written permission.
+    *
+    * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+
+\****************************************************************************************/
+
+#define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type )                   \
+void func_name( T *array, size_t total, user_data_type aux )                        \
+{                                                                                   \
+    int isort_thresh = 7;                                                           \
+    T t;                                                                            \
+    int sp = 0;                                                                     \
+                                                                                    \
+    struct                                                                          \
+    {                                                                               \
+        T *lb;                                                                      \
+        T *ub;                                                                      \
+    }                                                                               \
+    stack[48];                                                                      \
+                                                                                    \
+    aux = aux;                                                                      \
+                                                                                    \
+    if( total <= 1 )                                                                \
+        return;                                                                     \
+                                                                                    \
+    stack[0].lb = array;                                                            \
+    stack[0].ub = array + (total - 1);                                              \
+                                                                                    \
+    while( sp >= 0 )                                                                \
+    {                                                                               \
+        T* left = stack[sp].lb;                                                     \
+        T* right = stack[sp--].ub;                                                  \
+                                                                                    \
+        for(;;)                                                                     \
+        {                                                                           \
+            int i, n = (int)(right - left) + 1, m;                                  \
+            T* ptr;                                                                 \
+            T* ptr2;                                                                \
+                                                                                    \
+            if( n <= isort_thresh )                                                 \
+            {                                                                       \
+            insert_sort:                                                            \
+                for( ptr = left + 1; ptr <= right; ptr++ )                          \
+                {                                                                   \
+                    for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--)   \
+                        CV_SWAP( ptr2[0], ptr2[-1], t );                            \
+                }                                                                   \
+                break;                                                              \
+            }                                                                       \
+            else                                                                    \
+            {                                                                       \
+                T* left0;                                                           \
+                T* left1;                                                           \
+                T* right0;                                                          \
+                T* right1;                                                          \
+                T* pivot;                                                           \
+                T* a;                                                               \
+                T* b;                                                               \
+                T* c;                                                               \
+                int swap_cnt = 0;                                                   \
+                                                                                    \
+                left0 = left;                                                       \
+                right0 = right;                                                     \
+                pivot = left + (n/2);                                               \
+                                                                                    \
+                if( n > 40 )                                                        \
+                {                                                                   \
+                    int d = n / 8;                                                  \
+                    a = left, b = left + d, c = left + 2*d;                         \
+                    left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))     \
+                                      : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \
+                                                                                    \
+                    a = pivot - d, b = pivot, c = pivot + d;                        \
+                    pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))    \
+                                      : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \
+                                                                                    \
+                    a = right - 2*d, b = right - d, c = right;                      \
+                    right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))    \
+                                      : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));    \
+                }                                                                   \
+                                                                                    \
+                a = left, b = pivot, c = right;                                     \
+                pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))        \
+                                   : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));       \
+                if( pivot != left0 )                                                \
+                {                                                                   \
+                    CV_SWAP( *pivot, *left0, t );                                   \
+                    pivot = left0;                                                  \
+                }                                                                   \
+                left = left1 = left0 + 1;                                           \
+                right = right1 = right0;                                            \
+                                                                                    \
+                for(;;)                                                             \
+                {                                                                   \
+                    while( left <= right && !LT(*pivot, *left) )                    \
+                    {                                                               \
+                        if( !LT(*left, *pivot) )                                    \
+                        {                                                           \
+                            if( left > left1 )                                      \
+                                CV_SWAP( *left1, *left, t );                        \
+                            swap_cnt = 1;                                           \
+                            left1++;                                                \
+                        }                                                           \
+                        left++;                                                     \
+                    }                                                               \
+                                                                                    \
+                    while( left <= right && !LT(*right, *pivot) )                   \
+                    {                                                               \
+                        if( !LT(*pivot, *right) )                                   \
+                        {                                                           \
+                            if( right < right1 )                                    \
+                                CV_SWAP( *right1, *right, t );                      \
+                            swap_cnt = 1;                                           \
+                            right1--;                                               \
+                        }                                                           \
+                        right--;                                                    \
+                    }                                                               \
+                                                                                    \
+                    if( left > right )                                              \
+                        break;                                                      \
+                    CV_SWAP( *left, *right, t );                                    \
+                    swap_cnt = 1;                                                   \
+                    left++;                                                         \
+                    right--;                                                        \
+                }                                                                   \
+                                                                                    \
+                if( swap_cnt == 0 )                                                 \
+                {                                                                   \
+                    left = left0, right = right0;                                   \
+                    goto insert_sort;                                               \
+                }                                                                   \
+                                                                                    \
+                n = MIN( (int)(left1 - left0), (int)(left - left1) );               \
+                for( i = 0; i < n; i++ )                                            \
+                    CV_SWAP( left0[i], left[i-n], t );                              \
+                                                                                    \
+                n = MIN( (int)(right0 - right1), (int)(right1 - right) );           \
+                for( i = 0; i < n; i++ )                                            \
+                    CV_SWAP( left[i], right0[i-n+1], t );                           \
+                n = (int)(left - left1);                                            \
+                m = (int)(right1 - right);                                          \
+                if( n > 1 )                                                         \
+                {                                                                   \
+                    if( m > 1 )                                                     \
+                    {                                                               \
+                        if( n > m )                                                 \
+                        {                                                           \
+                            stack[++sp].lb = left0;                                 \
+                            stack[sp].ub = left0 + n - 1;                           \
+                            left = right0 - m + 1, right = right0;                  \
+                        }                                                           \
+                        else                                                        \
+                        {                                                           \
+                            stack[++sp].lb = right0 - m + 1;                        \
+                            stack[sp].ub = right0;                                  \
+                            left = left0, right = left0 + n - 1;                    \
+                        }                                                           \
+                    }                                                               \
+                    else                                                            \
+                        left = left0, right = left0 + n - 1;                        \
+                }                                                                   \
+                else if( m > 1 )                                                    \
+                    left = right0 - m + 1, right = right0;                          \
+                else                                                                \
+                    break;                                                          \
+            }                                                                       \
+        }                                                                           \
+    }                                                                               \
+}
+
+#define CV_IMPLEMENT_QSORT( func_name, T, cmp )  \
+    CV_IMPLEMENT_QSORT_EX( func_name, T, cmp, int )
+
+/****************************************************************************************\
+*                     Structures and macros for integration with IPP                     *
+\****************************************************************************************/
+
+/* IPP-compatible return codes */
+typedef enum CvStatus
+{
+    CV_BADMEMBLOCK_ERR          = -113,
+    CV_INPLACE_NOT_SUPPORTED_ERR= -112,
+    CV_UNMATCHED_ROI_ERR        = -111,
+    CV_NOTFOUND_ERR             = -110,
+    CV_BADCONVERGENCE_ERR       = -109,
+
+    CV_BADDEPTH_ERR             = -107,
+    CV_BADROI_ERR               = -106,
+    CV_BADHEADER_ERR            = -105,
+    CV_UNMATCHED_FORMATS_ERR    = -104,
+    CV_UNSUPPORTED_COI_ERR      = -103,
+    CV_UNSUPPORTED_CHANNELS_ERR = -102,
+    CV_UNSUPPORTED_DEPTH_ERR    = -101,
+    CV_UNSUPPORTED_FORMAT_ERR   = -100,
+
+    CV_BADARG_ERR      = -49,  //ipp comp
+    CV_NOTDEFINED_ERR  = -48,  //ipp comp
+
+    CV_BADCHANNELS_ERR = -47,  //ipp comp
+    CV_BADRANGE_ERR    = -44,  //ipp comp
+    CV_BADSTEP_ERR     = -29,  //ipp comp
+
+    CV_BADFLAG_ERR     =  -12,
+    CV_DIV_BY_ZERO_ERR =  -11, //ipp comp
+    CV_BADCOEF_ERR     =  -10,
+
+    CV_BADFACTOR_ERR   =  -7,
+    CV_BADPOINT_ERR    =  -6,
+    CV_BADSCALE_ERR    =  -4,
+    CV_OUTOFMEM_ERR    =  -3,
+    CV_NULLPTR_ERR     =  -2,
+    CV_BADSIZE_ERR     =  -1,
+    CV_NO_ERR          =   0,
+    CV_OK              =   CV_NO_ERR
+}
+CvStatus;
+
+#define IPPI_CALL(stmt) CV_Assert((stmt) >= 0)
+
+#define CV_NOTHROW throw()
+
+typedef struct CvFuncTable
+{
+    void*   fn_2d[CV_DEPTH_MAX];
+}
+CvFuncTable;
+
+typedef struct CvBigFuncTable
+{
+    void*   fn_2d[CV_DEPTH_MAX*CV_CN_MAX];
+}
+CvBigFuncTable;
+
+#define CV_INIT_FUNC_TAB( tab, FUNCNAME, FLAG )         \
+    (tab).fn_2d[CV_8U] = (void*)FUNCNAME##_8u##FLAG;    \
+    (tab).fn_2d[CV_8S] = 0;                             \
+    (tab).fn_2d[CV_16U] = (void*)FUNCNAME##_16u##FLAG;  \
+    (tab).fn_2d[CV_16S] = (void*)FUNCNAME##_16s##FLAG;  \
+    (tab).fn_2d[CV_32S] = (void*)FUNCNAME##_32s##FLAG;  \
+    (tab).fn_2d[CV_32F] = (void*)FUNCNAME##_32f##FLAG;  \
+    (tab).fn_2d[CV_64F] = (void*)FUNCNAME##_64f##FLAG
+
+#endif /*_CXCORE_MISC_H_*/
index 282f561b00fd43dacbd665ffa83bbbbbc782958e..950262c60d168733b008accc1f723dbcfeef13ef 100644 (file)
@@ -1333,10 +1333,27 @@ template<typename _Tp> inline Ptr<_Tp>::operator const _Tp*() const { return obj
 
 template<typename _Tp> inline bool Ptr<_Tp>::empty() const { return obj == 0; }
 
-//////////////////////////////////////// XML & YAML I/O ////////////////////////////////////
+//// specializied implementations of Ptr::delete_obj() for classic OpenCV types
+
+template<> inline void Ptr<CvMat>::delete_obj()
+{ cvReleaseMat(&obj); }   
+
+template<> inline void Ptr<IplImage>::delete_obj()
+{ cvReleaseImage(&obj); }
+    
+template<> inline void Ptr<CvMatND>::delete_obj()
+{ cvReleaseMatND(&obj); }
+    
+template<> inline void Ptr<CvSparseMat>::delete_obj()
+{ cvReleaseSparseMat(&obj); }
+    
+template<> inline void Ptr<CvMemStorage>::delete_obj()
+{ cvReleaseMemStorage(&obj); }
 
 template<> inline void Ptr<CvFileStorage>::delete_obj()
 { cvReleaseFileStorage(&obj); }
+    
+//////////////////////////////////////// XML & YAML I/O ////////////////////////////////////
 
 static inline void write( FileStorage& fs, const String& name, int value )
 { cvWriteInt( *fs, name.size() ? name.c_str() : 0, value ); }
index 2f54f17ecb06cf099ac3bae4e84b9bdac39b7094..16468afec0f6d1deb08a2a579ce06cdc0fbd066d 100644 (file)
@@ -73,6 +73,53 @@ CV_EXPORTS bool imencode( const String& ext, const Mat& img,
 
 CV_EXPORTS int waitKey(int delay=0);
 
+
+template<> inline void Ptr<CvCapture>::delete_obj()
+{ cvReleaseCapture(&obj); }
+    
+template<> inline void Ptr<CvVideoWriter>::delete_obj()
+{ cvReleaseVideoWriter(&obj); }
+
+class CV_EXPORTS VideoCapture
+{
+public:
+    VideoCapture();
+    VideoCapture(const String& filename);
+    VideoCapture(int device);
+    
+    virtual ~VideoCapture();
+    virtual bool open(const String& filename);
+    virtual bool open(int device);
+    virtual bool isOpened() const;
+    virtual void release();
+    
+    virtual bool grab();
+    virtual bool retrieve(Mat& image, int channel=0);
+    virtual VideoCapture& operator >> (Mat& image);
+    
+    virtual bool set(int propId, double value);
+    virtual double get(int propId);
+    
+protected:
+    Ptr<CvCapture> cap;
+};
+
+    
+class CV_EXPORTS VideoWriter
+{
+public:    
+    VideoWriter();
+    VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor=true);
+    
+    virtual ~VideoWriter();
+    virtual bool open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor=true);
+    virtual bool isOpened() const;
+    virtual VideoWriter& operator << (const Mat& image);
+    
+protected:
+    Ptr<CvVideoWriter> writer;
+};
+    
 }
 
 #endif
index 60ba21b710296e9a3e4782364b8f2b9a796ea33a..b67ad1f2658e9d0129f1e812e8aae151ecfa9621 100644 (file)
@@ -1,43 +1,99 @@
-// The short example shows how to use new-style image classes declared in cxcore.hpp.
-// There is also a very similar matrix class (CvMatrix) - a wrapper for CvMat
-#include "cv.h"
+#include "cv.h" // include standard OpenCV headers, same as before
 #include "highgui.h"
 
+using namespace cv; // all the new API is put into "cv" namespace. Export its content
+
+// enable/disable use of mixed API in the code below.
+#define DEMO_MIXED_API_USE 1
+
 int main( int argc, char** argv )
 {
-    // load image in constructor: the image can be loaded either from bitmap (see cvLoadImage),
-    // or from XML/YAML (see cvLoad)
-    CvImage img(argc > 1 ? argv[1] : "lena.jpg", 0, CV_LOAD_IMAGE_COLOR),
-        img_yuv, y, noise;
-    CvRNG rng = cvRNG(-1);
-
-    if( !img.data() ) // check if the image has been loaded properly
+    const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
+#if DEMO_MIXED_API_USE
+    Ptr<IplImage> iplimg = cvLoadImage(imagename); // Ptr<T> is safe ref-conting pointer class
+    Mat img(iplimg); // cv::Mat replaces the CvMat and IplImage, but it's easy to convert
+    // between the old and the new data structures (by default, only the header
+    // is converted, while the data is shared)
+#else
+    Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
+#endif
+    
+    if( !img.data ) // check if the image has been loaded properly
         return -1;
-
-    img_yuv = img.clone(); // clone the image
-                           // (although, later the content will be replaced with cvCvtColor,
-                           // clone() is used for simplicity and for the illustration)
-    cvCvtColor( img, img_yuv, CV_BGR2YCrCb ); // simply call OpenCV functions and pass the class instances there
-
-    y.create( img.size(), IPL_DEPTH_8U, 1 ); // another method to create an image - from scratch
-    noise.create( img.size(), IPL_DEPTH_32F, 1 );
-
-    cvSplit( img_yuv, y, 0, 0, 0 );
-    cvRandArr( &rng, noise, CV_RAND_NORMAL, cvScalarAll(0), cvScalarAll(20) );
-    cvSmooth( noise, noise, CV_GAUSSIAN, 5, 5, 1, 1 );
-    cvAcc( y, noise );
-    cvConvert( noise, y );
-    cvMerge( y, 0, 0, 0, img_yuv );
-    cvCvtColor( img_yuv, img, CV_YCrCb2BGR );
-
-    cvNamedWindow( "image with grain", CV_WINDOW_AUTOSIZE );
-    img.show( "image with grain" ); // .show method is the conveninient form of cvShowImage
-    cvWaitKey();
-
+    
+    Mat img_yuv;
+    cvtColor(img, img_yuv, CV_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically
+    
+    Vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
+    split(img_yuv, planes); // split the image into separate color planes
+    
+#if 1
+    // method 1. process Y plane using an iterator
+    MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();
+    for(; it != it_end; ++it)
+    {
+        double v = *it*1.7 + rand()%21-10;
+        *it = saturate_cast<uchar>(v*v/255.);
+    }
+    
+    // method 2. process the first chroma plane using pre-stored row pointer.
+    // method 3. process the second chroma plane using individual element access
+    for( int y = 0; y < img_yuv.rows; y++ )
+    {
+        uchar* Uptr = planes[1].ptr<uchar>(y);
+        for( int x = 0; x < img_yuv.cols; x++ )
+        {
+            Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);
+            uchar& Vxy = planes[2].at<uchar>(y, x);
+            Vxy = saturate_cast<uchar>((Vxy-128)/2 + 128);
+        }
+    }
+    
+#else
+    Mat noise(img.size(), CV_8U); // another Mat constructor; allocates a matrix of the specified size and type
+    randn(noise, Scalar::all(128), Scalar::all(20)); // fills the matrix with normally distributed random values;
+                                                     // there is also randu() for uniformly distributed random number generation
+    GaussianBlur(noise, noise, Size(3, 3), 0.5, 0.5); // blur the noise a bit, kernel size is 3x3 and both sigma's are set to 0.5
+    
+    const double brightness_gain = 0;
+    const double contrast_gain = 1.7;
+#if DEMO_MIXED_API_USE
+    // it's easy to pass the new matrices to the functions that only work with IplImage or CvMat:
+    // step 1) - convert the headers, data will not be copied
+    IplImage cv_planes_0 = planes[0], cv_noise = noise;
+    // step 2) call the function; do not forget unary "&" to form pointers
+    cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
+#else
+    addWeighted(planes[0], constrast_gain, noise, 1, -128 + brightness_gain, planes[0]);
+#endif
+    const double color_scale = 0.5;
+    // Mat::convertTo() replaces cvConvertScale. One must explicitly specify the output matrix type (we keep it intact - planes[1].type())
+    planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));
+    // alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).
+    // This expression will not create any temporary arrays and should be almost as fast as the above variant
+    planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));
+    
+    // Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.
+    planes[0] = planes[0].mul(planes[0], 1./255);
+#endif
+    
+    // now merge the results back
+    merge(planes, img_yuv);
+    // and produce the output RGB image
+    cvtColor(img_yuv, img, CV_YCrCb2BGR);
+    
+    // this is counterpart for cvNamedWindow
+    namedWindow("image with grain", CV_WINDOW_AUTOSIZE);
+#if DEMO_MIXED_API_USE
+    // this is to demonstrate that img and iplimg really share the data - the result of the above
+    // processing is stored in img and thus in iplimg too.
+    cvShowImage("image with grain", iplimg);
+#else
+    imshow("image with grain", img);
+#endif
+    waitKey();
+    
     return 0;
-    // all the images will be released automatically
+    // all the memory will automatically be released by Vector<>, Mat and Ptr<> destructors.
 }
 
-
-
-
index f624e227faab2418452826723b144505602762d8..0d7bcd1c7b5a62b180e02c42a34147febd1e1fe2 100644 (file)
@@ -73,7 +73,8 @@ cvSampleLine( const void* img, CvPoint pt1, CvPoint pt2,
     pix_size = CV_ELEM_SIZE(mat->type);
     for( i = 0; i < count; i++ )
     {
-        CV_MEMCPY_AUTO( buffer, iterator.ptr, pix_size );
+        for( int j = 0; j < pix_size; j++ )
+            buffer[j] = iterator.ptr[j];
         buffer += pix_size;
         CV_NEXT_LINE_POINT( iterator );
     }
index 01e49f66ae5301e989dbfebd928b20e14983d267..fad07e46880f383e4d093fcabe812d10f0d1101d 100644 (file)
@@ -99,9 +99,78 @@ typedef unsigned long ulong;
 #include "ipp.h"
 #endif
 
+
+#define CV_MEMCPY_CHAR( dst, src, len )                 \
+{                                                       \
+    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);    \
+    char* _icv_memcpy_dst_ = (char*)(dst);              \
+    const char* _icv_memcpy_src_ = (const char*)(src);  \
+                                                        \
+    for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ )  \
+        _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];        \
+}
+
+
+#define CV_MEMCPY_INT( dst, src, len )                  \
+{                                                       \
+    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);    \
+    int* _icv_memcpy_dst_ = (int*)(dst);                \
+    const int* _icv_memcpy_src_ = (const int*)(src);    \
+    assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
+    ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );  \
+                                                        \
+    for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++)  \
+        _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];\
+}
+
+
+#define CV_MEMCPY_AUTO( dst, src, len )                                             \
+{                                                                                   \
+    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \
+    char* _icv_memcpy_dst_ = (char*)(dst);                                          \
+    const char* _icv_memcpy_src_ = (const char*)(src);                              \
+    if( (_icv_memcpy_len_ & (sizeof(int)-1)) == 0 )                                 \
+    {                                                                               \
+        assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 &&                  \
+                ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                  \
+        for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_;                 \
+            _icv_memcpy_i_+=sizeof(int) )                                           \
+        {                                                                           \
+            *(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) =                              \
+            *(const int*)(_icv_memcpy_src_+_icv_memcpy_i_);                         \
+        }                                                                           \
+    }                                                                               \
+    else                                                                            \
+    {                                                                               \
+        for(_icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++)\
+            _icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_];    \
+    }                                                                               \
+}
+
+
+#define CV_ZERO_CHAR( dst, len )                        \
+{                                                       \
+    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);    \
+    char* _icv_memcpy_dst_ = (char*)(dst);              \
+                                                        \
+    for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ )  \
+        _icv_memcpy_dst_[_icv_memcpy_i_] = '\0';        \
+}
+
+
+#define CV_ZERO_INT( dst, len )                                                     \
+{                                                                                   \
+    size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len);                                \
+    int* _icv_memcpy_dst_ = (int*)(dst);                                            \
+    assert( ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 );                      \
+                                                                                    \
+    for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++)          \
+        _icv_memcpy_dst_[_icv_memcpy_i_] = 0;                                       \
+}
+
 namespace cv
 {
-
+    
 // -128.f ... 255.f
 extern const float g_8x32fTab[];
 #define CV_8TO32F(x)  cv::g_8x32fTab[(x)+128]
index 5fa972ca4c4cf539a551f8d01b296aff5d49e349..490f8849e06084a34d09918a66e8d225ee4b58d4 100644 (file)
@@ -321,3 +321,118 @@ CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
         *pwriter = 0;
     }
 }
+
+namespace cv
+{
+
+VideoCapture::VideoCapture()
+{}
+        
+VideoCapture::VideoCapture(const String& filename)
+{
+    open(filename);
+}
+    
+VideoCapture::VideoCapture(int device)
+{
+    open(device);
+}
+
+VideoCapture::~VideoCapture()
+{
+    cap.release();
+}
+    
+bool VideoCapture::open(const String& filename)
+{
+    cap = cvCreateFileCapture(filename.c_str());
+    return isOpened();
+}
+    
+bool VideoCapture::open(int device)
+{
+    cap = cvCreateCameraCapture(device);
+    return isOpened();
+}
+    
+bool VideoCapture::isOpened() const { return !cap.empty(); }
+    
+void VideoCapture::release()
+{
+    cap.release();
+}
+
+bool VideoCapture::grab()
+{
+    return cvGrabFrame(cap) != 0;
+}
+    
+bool VideoCapture::retrieve(Mat& image, int channel)
+{
+    IplImage* _img = cvRetrieveFrame(cap, channel);
+    if( !_img )
+    {
+        image.release();
+        return false;
+    }
+    if(_img->origin == IPL_ORIGIN_TL)
+        image = Mat(_img);
+    else
+    {
+        Mat temp(_img);
+        flip(temp, image, 0);
+    }
+    return true;
+}
+    
+VideoCapture& VideoCapture::operator >> (Mat& image)
+{
+    if(!grab())
+        image.release();
+    else
+        retrieve(image);
+    return *this;
+}
+    
+bool VideoCapture::set(int propId, double value)
+{
+    return cvSetCaptureProperty(cap, propId, value) != 0;
+}
+    
+double VideoCapture::get(int propId)
+{
+    return cvGetCaptureProperty(cap, propId);
+}
+
+VideoWriter::VideoWriter()
+{}
+    
+VideoWriter::VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor)
+{
+    open(filename, fourcc, fps, frameSize, isColor);
+}
+
+VideoWriter::~VideoWriter()
+{
+    writer.release();
+}
+    
+bool VideoWriter::open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor)
+{
+    writer = cvCreateVideoWriter(filename.c_str(), fourcc, fps, frameSize, isColor);
+    return isOpened();
+}
+
+bool VideoWriter::isOpened() const
+{
+    return !writer.empty();
+}    
+    
+VideoWriter& VideoWriter::operator << (const Mat& image)
+{
+    IplImage _img = image;
+    cvWriteFrame(writer, &_img);
+    return *this;    
+}
+
+}