cvsamplers.cpp.svn-base

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

SVN-BASE
1,027
字号
/*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"
#include "_cvutils.h"

/* 
   Initializes line iterator.
   Returns number of points on the line or negative number if error.
*/
int
icvInitLineIterator( const CvMat* mat, CvPoint pt1, CvPoint pt2,
                     CvLineIterator* iterator, int connectivity,
                     int left_to_right )
{
    int dx, dy, s;
    int bt_pix, bt_pix0, step;

    assert( connectivity == 4 || connectivity == 8 );

    bt_pix0 = bt_pix = icvPixSize[CV_MAT_TYPE(mat->type)];
    step = mat->step;

    dx = pt2.x - pt1.x;
    dy = pt2.y - pt1.y;
    s = dx < 0 ? -1 : 0;

    if( left_to_right )
    {
        dx = (dx ^ s) - s;
        dy = (dy ^ s) - s;
        pt1.x ^= (pt1.x ^ pt2.x) & s;
        pt1.y ^= (pt1.y ^ pt2.y) & s;
    }
    else
    {
        dx = (dx ^ s) - s;
        bt_pix = (bt_pix ^ s) - s;
    }

    iterator->ptr = (uchar*)(mat->data.ptr + pt1.y * step + pt1.x * bt_pix0);

    s = dy < 0 ? -1 : 0;
    dy = (dy ^ s) - s;
    step = (step ^ s) - s;

    s = dy > dx ? -1 : 0;
    
    /* conditional swaps */
    dx ^= dy & s;
    dy ^= dx & s;
    dx ^= dy & s;

    bt_pix ^= step & s;
    step ^= bt_pix & s;
    bt_pix ^= step & s;

    if( connectivity == 8 )
    {
        assert( dx >= 0 && dy >= 0 );
        
        iterator->err = dx - (dy + dy);
        iterator->plus_delta = dx + dx;
        iterator->minus_delta = -(dy + dy);
        iterator->plus_step = step;
        iterator->minus_step = bt_pix;
        s = dx + 1;
    }
    else /* connectivity == 4 */
    {
        assert( dx >= 0 && dy >= 0 );
        
        iterator->err = 0;
        iterator->plus_delta = (dx + dx) + (dy + dy);
        iterator->minus_delta = -(dy + dy);
        iterator->plus_step = step - bt_pix;
        iterator->minus_step = bt_pix;
        s = dx + dy + 1;
    }

    return s;
}


/* 
   Initializes line iterator.
   Returns number of points on the line or negative number if error.
*/
CV_IMPL int
cvInitLineIterator( const void* img, CvPoint pt1, CvPoint pt2,
                    CvLineIterator* iterator, int connectivity )
{
    int count = -1;
    
    CV_FUNCNAME( "cvInitLineIterator" );

    __BEGIN__;

    CvMat stub, *mat = (CvMat*)img;

    CV_CALL( mat = cvGetMat( mat, &stub ));

    if( !iterator )
        CV_ERROR( CV_StsNullPtr, "" );

    if( connectivity != 8 && connectivity != 4 )
        CV_ERROR( CV_StsBadArg, "" );

    if( (unsigned)pt1.x >= (unsigned)(mat->width) ||
        (unsigned)pt2.x >= (unsigned)(mat->width) ||
        (unsigned)pt1.y >= (unsigned)(mat->height) ||
        (unsigned)pt2.y >= (unsigned)(mat->height) )
        CV_ERROR( CV_StsBadPoint, "" );

    count = icvInitLineIterator( mat, pt1, pt2, iterator, connectivity );

    __END__;

    return count;
}


/**************************************************************************************\
*                                   line samplers                                      *
\**************************************************************************************/

////////////////////////////////////////////////////////////////////////////////////////

#define  ICV_DEF_SAMPLE_LINE( flavor, arrtype, pix_size )                  \
IPCVAPI_IMPL( CvStatus, icvSampleLine_##flavor,                            \
( CvLineIterator* iterator, arrtype* buffer, int count ))                  \
{                                                                          \
    for( int i = 0; i < count; i++ )                                       \
    {                                                                      \
        memcpy( buffer, iterator->ptr, pix_size );                         \
        buffer += pix_size;                                                \
        CV_NEXT_LINE_POINT( *iterator );                                   \
    }                                                                      \
                                                                           \
    return CV_OK;                                                          \
}


ICV_DEF_SAMPLE_LINE( 8u_C1R, uchar, 1 )
ICV_DEF_SAMPLE_LINE( 8u_C2R, uchar, 2 )
ICV_DEF_SAMPLE_LINE( 8u_C3R, uchar, 3 )
ICV_DEF_SAMPLE_LINE( 32f_C1R, float, 4 )
ICV_DEF_SAMPLE_LINE( 32f_C2R, float, 8 )
ICV_DEF_SAMPLE_LINE( 32f_C3R, float, 12 )

#define icvSampleLine_16u_C2R   icvSampleLine_32f_C1R
#define icvSampleLine_16u_C3R   0
#define icvSampleLine_32s_C2R   icvSampleLine_32f_C2R
#define icvSampleLine_32s_C3R   icvSampleLine_32f_C3R
#define icvSampleLine_64s_C2R   0
#define icvSampleLine_64s_C3R   0
#define icvSampleLine_64s_C4R   0


static CvStatus  icvSampleLine( CvLineIterator* iterator, void* buffer,
                                int count, int pix_size )
{
    for( int i = 0; i < count; i++ )
    {
        memcpy( buffer, iterator->ptr, pix_size );
        (char*&)buffer += pix_size;
        CV_NEXT_LINE_POINT( *iterator );
    }

    return CV_OK;
}


CV_DEF_INIT_PIXSIZE_TAB_2D( SampleLine, R )

typedef  CvStatus (*CvLineFunc)( CvLineIterator* iterator, void* buffer, int count );

CV_IMPL int
cvSampleLine( const void* img, CvPoint pt1, CvPoint pt2,
              void* buffer, int connectivity )
{
    static  CvBtFuncTable  sl_tab;
    static  int inittab = 1;
    int count = -1;
    
    CV_FUNCNAME( "cvSampleLine" );

    __BEGIN__;
    
    int coi = 0, pix_size;
    CvMat stub, *mat = (CvMat*)img;
    CvLineIterator iterator;
    CvLineFunc func = 0;

    if( !inittab )
    {
        icvInitSampleLineRTable( &sl_tab );
        inittab = 1;
    }

    CV_CALL( mat = cvGetMat( mat, &stub, &coi ));

    if( coi != 0 )
        CV_ERROR( CV_BadCOI, "" );

    if( !buffer )
        CV_ERROR( CV_StsNullPtr, "" );

    CV_CALL( count = cvInitLineIterator( mat, pt1, pt2, &iterator, connectivity ));

    pix_size = icvPixSize[CV_MAT_TYPE(mat->type)];
    func = (CvLineFunc)sl_tab.fn_2d[pix_size];

    if( func )
    {
        IPPI_CALL( func( &iterator, buffer, count ));
    }
    else
    {
        icvSampleLine( &iterator, buffer, count, pix_size );

⌨️ 快捷键说明

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