cvmat.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 1,075 行 · 第 1/2 页

SVN-BASE
1,075
字号
/*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.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, 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 Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "_cvaux.h"

#if (_MSC_VER>=1200) || defined __BORLANDC__

double CvMAT::get( const uchar* ptr, int type, int coi )
{
    double t = 0;
    assert( (unsigned)coi < (unsigned)CV_MAT_CN(type) );

    switch( CV_MAT_DEPTH(type) )
    {
    case CV_8U:
        t = ((uchar*)ptr)[coi];
        break;
    case CV_8S:
        t = ((char*)ptr)[coi];
        break;
    case CV_16S:
        t = ((short*)ptr)[coi];
        break;
    case CV_32S:
        t = ((int*)ptr)[coi];
        break;
    case CV_32F:
        t = ((float*)ptr)[coi];
        break;
    case CV_64F:
        t = ((double*)ptr)[coi];
        break;
    }

    return t;
}


#define CV_CAST_8U(t)    (uchar)( !((t) & ~255) ? (t) : (t) > 0 ? 255 : 0)
#define CV_CAST_8S(t)    (char)( !(((t)+128) & ~255) ? (t) : (t) > 0 ? 127 : -128 )
#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)


void CvMAT::set( uchar* ptr, int type, int coi, double d )
{
    int i;
    assert( (unsigned)coi < (unsigned)CV_MAT_CN(type) );

    switch( CV_MAT_DEPTH(type))
    {
    case CV_8U:
        i = cvRound(d);
        ((uchar*)ptr)[coi] = CV_CAST_8U(i);
        break;
    case CV_8S:
        i = cvRound(d);
        ((char*)ptr)[coi] = CV_CAST_8S(i);
        break;
    case CV_16S:
        i = cvRound(d);
        ((short*)ptr)[coi] = CV_CAST_16S(i);
        break;
    case CV_32S:
        i = cvRound(d);
        ((int*)ptr)[coi] = CV_CAST_32S(i);
        break;
    case CV_32F:
        ((float*)ptr)[coi] = (float)d;
        break;
    case CV_64F:
        ((double*)ptr)[coi] = d;
        break;
    }
}


void CvMAT::set( uchar* ptr, int type, int coi, int i )
{
    assert( (unsigned)coi < (unsigned)CV_MAT_CN(type) );

    switch( CV_MAT_DEPTH(type))
    {
    case CV_8U:
        ((uchar*)ptr)[coi] = CV_CAST_8U(i);
        break;
    case CV_8S:
        ((char*)ptr)[coi] = CV_CAST_8S(i);
        break;
    case CV_16S:
        ((short*)ptr)[coi] = CV_CAST_16S(i);
        break;
    case CV_32S:
        ((int*)ptr)[coi] = i;
        break;
    case CV_32F:
        ((float*)ptr)[coi] = (float)i;
        break;
    case CV_64F:
        ((double*)ptr)[coi] = (double)i;
        break;
    }
}


void CvMAT::set( uchar* ptr, int type, double d )
{
    int i, cn = CV_MAT_CN(type);

    switch( CV_MAT_DEPTH(type))
    {
    case CV_8U:
        i = cvRound(d);
        ((uchar*)ptr)[0] = CV_CAST_8U(i);
        i = cn;
        while( --i ) ((uchar*)ptr)[i] = 0;
        break;
    case CV_8S:
        i = cvRound(d);
        ((char*)ptr)[0] = CV_CAST_8S(i);
        i = cn;
        while( --i ) ((char*)ptr)[i] = 0;
        break;
    case CV_16S:
        i = cvRound(d);
        ((short*)ptr)[0] = CV_CAST_16S(i);
        i = cn;
        while( --i ) ((short*)ptr)[i] = 0;
        break;
    case CV_32S:
        i = cvRound(d);
        ((int*)ptr)[0] = i;
        i = cn;
        while( --i ) ((int*)ptr)[i] = 0;
        break;
    case CV_32F:
        ((float*)ptr)[0] = (float)d;
        i = cn;
        while( --i ) ((float*)ptr)[i] = 0;
        break;
    case CV_64F:
        ((double*)ptr)[0] = d;
        i = cn;
        while( --i ) ((double*)ptr)[i] = 0;
        break;
    }
}


void CvMAT::set( uchar* ptr, int type, int i )
{
    int cn = CV_MAT_CN(type);

    switch( CV_MAT_DEPTH(type))
    {
    case CV_8U:
        ((uchar*)ptr)[0] = CV_CAST_8U(i);
        i = cn;
        while( --i ) ((uchar*)ptr)[i] = 0;
        break;
    case CV_8S:
        ((char*)ptr)[0] = CV_CAST_8S(i);
        i = cn;
        while( --i ) ((char*)ptr)[i] = 0;
        break;
    case CV_16S:
        ((short*)ptr)[0] = CV_CAST_16S(i);
        i = cn;
        while( --i ) ((short*)ptr)[i] = 0;
        break;
    case CV_32S:
        ((int*)ptr)[0] = i;
        i = cn;
        while( --i ) ((int*)ptr)[i] = 0;
        break;
    case CV_32F:
        ((float*)ptr)[0] = (float)i;
        i = cn;
        while( --i ) ((float*)ptr)[i] = 0;
        break;
    case CV_64F:
        ((double*)ptr)[0] = (double)i;
        i = cn;
        while( --i ) ((double*)ptr)[i] = 0;
        break;
    }
}


CvMAT::CvMAT( const _CvMAT_T_& mat_t )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = mat_t;
}


CvMAT::CvMAT( const _CvMAT_ADD_& mat_add )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = mat_add;
}


CvMAT::CvMAT( const _CvMAT_ADD_EX_& mat_add )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = mat_add;
}


CvMAT::CvMAT( const _CvMAT_SCALE_& scale_mat )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = scale_mat;
}


CvMAT::CvMAT( const _CvMAT_SCALE_SHIFT_& scale_shift_mat )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = scale_shift_mat;
}


CvMAT::CvMAT( const _CvMAT_MUL_& mmul )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = mmul;
}


CvMAT::CvMAT( const _CvMAT_MUL_ADD_& mmuladd )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = mmuladd;
}


CvMAT::CvMAT( const _CvMAT_INV_& inv_mat )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = inv_mat;
}


CvMAT::CvMAT( const _CvMAT_NOT_& not_mat )
{
    type = 0;
    data.ptr = 0;
    refcount = 0;
    *this = not_mat;
}


CvMAT::CvMAT( const _CvMAT_UN_LOGIC_& mat_logic )
{
    type = 0;
    data.ptr = 0;
    refcount = 0;
    *this = mat_logic;
}


CvMAT::CvMAT( const _CvMAT_LOGIC_& mat_logic )
{
    type = 0;
    data.ptr = 0;
    refcount = 0;
    *this = mat_logic;
}


CvMAT::CvMAT( const _CvMAT_COPY_& mat_copy )
{
    CvMAT* src = (CvMAT*)mat_copy.a;
    if( src->istemp() )
    {
        memcpy( this, src, sizeof(*this));
        type &= ~CV_MAT_TEMP_FLAG;
        assert( refcount );
        refcount[0]++;
    }
    else
    {
        create( src->height, src->width, src->type );
        cvCopy( src, this );
    }
}


CvMAT::CvMAT( const _CvMAT_CVT_& mat_cvt )
{
    type = 0;
    data.ptr = 0;
    refcount = 0;
    *this = mat_cvt;
}


CvMAT::CvMAT( const _CvMAT_DOT_OP_& dot_op )
{
    data.ptr = 0;
    type = 0;
    refcount = 0;
    *this = dot_op;
}


CvMAT::CvMAT( const _CvMAT_SOLVE_& solve_mat )
{
    type = 0;
    data.ptr = 0;
    refcount = 0;
    *this = solve_mat;
}


CvMAT::CvMAT( const _CvMAT_CMP_& cmp_mat )
{
    type = 0;
    data.ptr = 0;
    refcount = 0;
    *this = cmp_mat;
}


/****************************************************************************************\
*                                  CvMAT::operator =                                     *
\****************************************************************************************/

CvMAT& CvMAT::operator = ( const _CvMAT_T_& mat_t )
{
    CvMAT* src = (CvMAT*)&mat_t.a;
    if( !data.ptr )
    {
        if( src->istemp() && src->width == src->height )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
        }
        else
            create( src->width, src->height, src->type );
    }

    cvTranspose( src, this );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_ADD_& mat_add )
{
    CvMAT* a = mat_add.a;
    CvMAT* b = mat_add.b;

    if( !data.ptr )
    {
        if( a->istemp() )
            memcpy( this, a, sizeof(*this));
        else if( b->istemp() )
            memcpy( this, b, sizeof(*this));
        else
            create( a->height, a->width, a->type );
        type &= ~CV_MAT_TEMP_FLAG;
    }

    if( mat_add.beta == 1 )
    {
        cvAdd( a, b, this );
        return *this;
    }

    if( mat_add.beta == -1 )
    {
        cvSub( a, b, this );
        return *this;
    }
    
    if( CV_MAT_DEPTH(a->type) >= CV_32F && CV_MAT_CN(a->type) <= 2 )
        cvScaleAdd( b, cvScalar(mat_add.beta), a, this );
    else
        cvAddWeighted( a, 1, b, mat_add.beta, 0, this );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_ADD_EX_& mat_add )
{
    CvMAT* a = mat_add.a;
    CvMAT* b = mat_add.b;

    if( !data.ptr )
    {
        if( a->istemp() )
            memcpy( this, a, sizeof(*this));
        else if( b->istemp() )
            memcpy( this, b, sizeof(*this));
        else
            create( a->height, a->width, a->type );
        type &= ~CV_MAT_TEMP_FLAG;
    }

    cvAddWeighted( a, mat_add.alpha, b, mat_add.beta, mat_add.gamma, this );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_SCALE_& scale_mat )
{
    CvMAT* src = scale_mat.a;

    if( !data.ptr )
    {
        if( src->istemp() )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
        }
        else
            create( src->height, src->width, src->type );
    }

    cvConvertScale( src, this, scale_mat.alpha, 0 );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_SCALE_SHIFT_& scale_shift_mat )
{
    CvMAT* src = scale_shift_mat.a;
    
    if( !data.ptr )
    {
        if( src->istemp() )
        {
            memcpy( this, src, sizeof(*this));
            type &= ~CV_MAT_TEMP_FLAG;
            assert( refcount );
            refcount[0]++;
        }
        else
            create( src->height, src->width, src->type );
    }

    cvConvertScale( src, this, scale_shift_mat.alpha, scale_shift_mat.beta );
    return *this;
}


CvMAT& CvMAT::operator = ( const _CvMAT_MUL_& mmul )
{
    CvMAT* a = mmul.a;
    CvMAT* b = mmul.b;
    int t_a = mmul.t_ab & 1;
    int t_b = (mmul.t_ab & 2) != 0;
    int m = (&(a->rows))[t_a];
    int l = (&(a->rows))[t_a ^ 1];
    int n = (&(b->rows))[t_b ^ 1];
    CvMAT* src = 0;
    /* this(m x n) = (a^o1(t))(m x l) * (b^o2(t))(l x n) */

    if( !data.ptr )
    {
        if( a->istemp() && l == n )
            src = a;
        else if( b->istemp() && l == m )
            src = b;

        if( src )
        {
            if( src->rows == m )
            {
                memcpy( this, src, sizeof(*this));
                type &= ~CV_MAT_TEMP_FLAG;
            }
            else
            {
                cvInitMatHeader( this, m, n, src->type, src->data.ptr );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?