cvsumpixels.cpp.svn-base

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

SVN-BASE
803
字号
/*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 "_cv.h"

/****************************************************************************************\
*                             Find sum of pixels in the ROI                              *
\****************************************************************************************/


#define ICV_DEF_SUM_1D_CASE_COI( __op__, src, len, sum, cn )                \
{                                                                           \
    int i;                                                                  \
                                                                            \
    for( i = 0; i <= (len) - 4*(cn); i += 4*(cn) )                          \
        (sum)[0] += __op__((src)[i]) + __op__((src)[i+(cn)]) +              \
                    __op__((src)[i+(cn)*2]) + __op__((src)[i+(cn)*3]);      \
                                                                            \
    for( ; i < (len); i += (cn) )                                           \
        (sum)[0] += __op__((src)[i]);                                       \
}                                                                           \


#define ICV_DEF_SUM_1D_CASE_C1( __op__, src, len, sum ) \
    ICV_DEF_SUM_1D_CASE_COI( __op__, src, len, sum, 1 )


#define ICV_DEF_SUM_1D_CASE_C2( __op__, src, len, sum )                     \
{                                                                           \
    int i;                                                                  \
                                                                            \
    for( i = 0; i <= (len) - 8; i += 8 )                                    \
    {                                                                       \
        (sum)[0] += __op__((src)[i]) + __op__((src)[i+2]) +                 \
                    __op__((src)[i+4]) + __op__((src)[i+6]);                \
        (sum)[1] += __op__((src)[i+1]) + __op__((src)[i+3]) +               \
                    __op__((src)[i+5]) + __op__((src)[i+7]);                \
    }                                                                       \
                                                                            \
    for( ; i < (len); i += 2 )                                              \
    {                                                                       \
        (sum)[0] += __op__((src)[i]);                                       \
        (sum)[1] += __op__((src)[i+1]);                                     \
    }                                                                       \
}                                                                           \


#define ICV_DEF_SUM_1D_CASE_C3( __op__, src, len, sum )                     \
{                                                                           \
    int i;                                                                  \
                                                                            \
    for( i = 0; i <= (len) - 12; i += 12 )                                  \
    {                                                                       \
        (sum)[0] += __op__((src)[i]) + __op__((src)[i+3]) +                 \
                    __op__((src)[i+6]) + __op__((src)[i+9]);                \
        (sum)[1] += __op__((src)[i+1]) + __op__((src)[i+4]) +               \
                    __op__((src)[i+7]) + __op__((src)[i+10]);               \
        (sum)[2] += __op__((src)[i+2]) + __op__((src)[i+5]) +               \
                    __op__((src)[i+8]) + __op__((src)[i+11]);               \
    }                                                                       \
                                                                            \
    for( ; i < (len); i += 3 )                                              \
    {                                                                       \
        (sum)[0] += __op__((src)[i]);                                       \
        (sum)[1] += __op__((src)[i+1]);                                     \
        (sum)[2] += __op__((src)[i+2]);                                     \
    }                                                                       \
}


#define ICV_DEF_SUM_1D_CASE_C4( __op__, src, len, sum )                     \
{                                                                           \
    int i;                                                                  \
                                                                            \
    for( i = 0; i <= (len) - 16; i += 16 )                                  \
    {                                                                       \
        (sum)[0] += __op__((src)[i]) + __op__((src)[i+4]) +                 \
                    __op__((src)[i+8]) + __op__((src)[i+12]);               \
        (sum)[1] += __op__((src)[i+1]) + __op__((src)[i+5]) +               \
                    __op__((src)[i+9]) + __op__((src)[i+13]);               \
        (sum)[2] += __op__((src)[i+2]) + __op__((src)[i+6]) +               \
                    __op__((src)[i+10]) + __op__((src)[i+14]);              \
        (sum)[3] += __op__((src)[i+3]) + __op__((src)[i+7]) +               \
                    __op__((src)[i+11]) + __op__((src)[i+15]);              \
    }                                                                       \
                                                                            \
    for( ; i < (len); i += 4 )                                              \
    {                                                                       \
        (sum)[0] += __op__((src)[i]);                                       \
        (sum)[1] += __op__((src)[i+1]);                                     \
        (sum)[2] += __op__((src)[i+2]);                                     \
        (sum)[3] += __op__((src)[i+3]);                                     \
    }                                                                       \
}


#define CV_SUM_ENTRY_C1( sumtype ) \
    sumtype temp[1] = { 0 };

#define CV_SUM_ENTRY_C2( sumtype ) \
    sumtype temp[2] = { 0, 0 };

#define CV_SUM_ENTRY_C3( sumtype ) \
    sumtype temp[3] = { 0, 0, 0 };

#define CV_SUM_ENTRY_C4( sumtype ) \
    sumtype temp[4] = { 0, 0, 0, 0 };

#define CV_SUM_EXIT_C1(sumtype_final) \
    sum[0] = (sumtype_final)temp[0]

#define CV_SUM_EXIT_C2(sumtype_final) \
    CV_SUM_EXIT_C1(sumtype_final), sum[1] = (sumtype_final)temp[1]

#define CV_SUM_EXIT_C3(sumtype_final) \
    CV_SUM_EXIT_C2(sumtype_final), sum[2] = (sumtype_final)temp[2]

#define CV_SUM_EXIT_C4(sumtype_final) \
    CV_SUM_EXIT_C3(sumtype_final), sum[3] = (sumtype_final)temp[3]


#define ICV_DEF_SUM_FUNC_2D( __op__, _entry_, _exit_, name, flavor,         \
                             cn, srctype, sumtype, sumtype_final )          \
IPCVAPI_IMPL(CvStatus, icv##name##_##flavor##_C##cn##R,( const srctype* src,\
                                    int step, CvSize size, sumtype_final* sum ))\
{                                                                           \
    _entry_( sumtype );                                                     \
    size.width *= cn;                                                       \
                                                                            \
    for( int y = 0; y < size.height; y++, (char*&)src += step )             \
    {                                                                       \
        ICV_DEF_SUM_1D_CASE_C##cn( __op__, src, size.width, temp );         \
    }                                                                       \
                                                                            \
    _exit_(sumtype_final);                                                  \
                                                                            \
    return CV_OK;                                                           \
}


#define ICV_DEF_SUM_0D_CASE_C1( __op__, src, sum )                          \
{                                                                           \
    (sum)[0] += __op__((src)[0]);                                           \
}                                                                           \


#define ICV_DEF_SUM_0D_CASE_C2( __op__, src, sum )                          \
{                                                                           \
    (sum)[0] += __op__((src)[0]);                                           \
    (sum)[1] += __op__((src)[1]);                                           \
}                                                                           \


#define ICV_DEF_SUM_FUNC_2D_EX( __op__, _entry_, _exit_, name, flavor,      \
                                cn, srctype, sumtype, sumtype_final )       \
IPCVAPI_IMPL(CvStatus, icv##name##_##flavor##_C##cn##R,( const srctype* src,\
                                    int step, CvSize size, sumtype_final* sum )) \
{                                                                           \
    _entry_( sumtype );                                                     \
                                                                            \
    if( size.width == 1 )                                                   \
    {                                                                       \
        for( int y = 0; y < size.height; y++, (char*&)src += step )         \
        {                                                                   \
            ICV_DEF_SUM_0D_CASE_C##cn( __op__, src, temp );                 \
        }                                                                   \
    }                                                                       \
    else                                                                    \
    {                                                                       \
        size.width *= cn;                                                   \
                                                                            \
        for( int y = 0; y < size.height; y++, (char*&)src += step )         \
        {                                                                   \
            ICV_DEF_SUM_1D_CASE_C##cn( __op__, src, size.width, temp );     \
        }                                                                   \
    }                                                                       \
                                                                            \
    _exit_(sumtype_final);                                                  \
                                                                            \
    return CV_OK;                                                           \
}



#define ICV_DEF_SUM_FUNC_2D_COI( __op__, name, flavor, srctype,             \
                                 sumtype, sumtype_final )                   \
IPCVAPI_IMPL(CvStatus, icv##name##_##flavor##_CnCR, ( const srctype* src,   \
                    int step, CvSize size, int cn, int coi, sumtype_final* sum )) \
{                                                                           \
    CV_SUM_ENTRY_C1( sumtype );                                             \
                                                                            \
    size.width *= cn;                                                       \
    src += coi - 1;                                                         \
                                                                            \
    for( int y = 0; y < size.height; y++, (char*&)src += step )             \
    {                                                                       \
        ICV_DEF_SUM_1D_CASE_COI( __op__, src, size.width, temp, cn );       \
    }                                                                       \
                                                                            \
    CV_SUM_EXIT_C1(sumtype_final);                                          \
                                                                            \
    return CV_OK;                                                           \
}


#define ICV_DEF_SUM_ALL( __op__, name, flavor, srctype, sumtype, sumtype_final )\
    ICV_DEF_SUM_FUNC_2D( __op__, CV_SUM_ENTRY_C1, CV_SUM_EXIT_C1,               \
                         name, flavor, 1, srctype, sumtype, sumtype_final )     \
    ICV_DEF_SUM_FUNC_2D( __op__, CV_SUM_ENTRY_C2, CV_SUM_EXIT_C2,               \
                         name, flavor, 2, srctype, sumtype, sumtype_final )     \
    ICV_DEF_SUM_FUNC_2D( __op__, CV_SUM_ENTRY_C3, CV_SUM_EXIT_C3,               \
                         name, flavor, 3, srctype, sumtype, sumtype_final )     \
    ICV_DEF_SUM_FUNC_2D( __op__, CV_SUM_ENTRY_C4, CV_SUM_EXIT_C4,               \
                         name, flavor, 4, srctype, sumtype, sumtype_final )     \
    ICV_DEF_SUM_FUNC_2D_COI( __op__, name, flavor, srctype, sumtype, sumtype_final )


ICV_DEF_SUM_ALL( CV_NOP, Sum, 8u, uchar, int64, double )
ICV_DEF_SUM_ALL( CV_NOP, Sum, 8s, char, int64, double )
ICV_DEF_SUM_ALL( CV_NOP, Sum, 16s, short, int64, double )
ICV_DEF_SUM_ALL( CV_CAST_64S, Sum, 32s, int, int64, double )
ICV_DEF_SUM_ALL( CV_NOP, Sum, 32f, float, double, double )
ICV_DEF_SUM_ALL( CV_NOP, Sum, 64f, double, double, double )

CV_DEF_INIT_BIG_FUNC_TAB( Sum, R )
CV_DEF_INIT_FUNC_TAB_2D( Sum, CnCR )

CV_IMPL CvScalar
cvSum( const CvArr* arr )
{

⌨️ 快捷键说明

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