cvdrawing.cpp.svn-base

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

SVN-BASE
2,174
字号
    CV_FUNCNAME("icvPolyLineAA");

    __BEGIN__;
    
    CvMat mat = *img;
    int i0 = closed ? count - 1 : 0, i;

    assert( img && (unsigned) scale <= XY_SHIFT ); 

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

    if( CV_MAT_DEPTH( img->type ) != CV_8U )
        CV_ERROR( CV_BadDepth, icvUnsupportedFormat );

    if( CV_MAT_CN( img->type ) != 1 && CV_MAT_CN( img->type ) != 3 )
        CV_ERROR( CV_BadNumChannels, icvUnsupportedFormat );

    mat.width -= 4;
    mat.height -= 4;

    if( mat.width > 0 && mat.height > 0 )
    {
        mat.data.ptr += (mat.step + CV_MAT_CN(mat.type)) * 2;

        for( i = !closed; i < count; i0 = i, i++ )
        {
            icvLineAA( &mat,
                       cvPoint( (v[i0].x << (XY_SHIFT - scale)) - 2 * XY_ONE,
                                (v[i0].y << (XY_SHIFT - scale)) - 2 * XY_ONE ),
                       cvPoint( (v[i].x << (XY_SHIFT - scale)) - 2 * XY_ONE,
                                (v[i].y << (XY_SHIFT - scale)) - 2 * XY_ONE ), color );
        }
    }

    __END__;
}


/****************************************************************************************\
*                              External functions                                        *
\****************************************************************************************/

void
icvExtractColor( double color, int type, void* buffer )
{
    CV_FUNCNAME( "icvExtractColor" );

    __BEGIN__;

    if( CV_MAT_DEPTH( type ) <= CV_8S )
    {
        int icolor = cvRound( color );

        if( CV_MAT_CN( type ) > 1 )
        {
            ((uchar*)buffer)[0] = (uchar)icolor;
            ((uchar*)buffer)[1] = (uchar)(icolor >> 8);
            ((uchar*)buffer)[2] = (uchar)(icolor >> 16);
            ((uchar*)buffer)[3] = (uchar)(icolor >> 24);
        }
        else if( CV_MAT_DEPTH( type ) == CV_8U )
            ((uchar*)buffer)[0] = CV_CAST_8U( icolor );
        else
            ((char*)buffer)[0] = CV_CAST_8S( icolor );
    }
    else
    {
		int i, cn = CV_MAT_CN( type );
        /*if( CV_MAT_CN( type ) > 1 )
            CV_ERROR( CV_BadNumChannels,
            "Multi-channel images with depth different from 8bit "
            "are not supported by drawing functions" );*/

        switch( CV_MAT_DEPTH( type ))
        {
        case CV_16S:
            i = cvRound( color );
			((short*)buffer)[0] = CV_CAST_16S(i);
			for( i = 1; i < cn; i++ )
				((short*)buffer)[i] = ((short*)buffer)[0];
            break;
        case CV_32S:
            ((int*)buffer)[0] = cvRound( color );
			for( i = 1; i < cn; i++ )
				((int*)buffer)[i] = ((int*)buffer)[0];
            break;
        case CV_32F:
            ((float*)buffer)[0] = (float)color;
			for( i = 1; i < cn; i++ )
				((float*)buffer)[i] = ((float*)buffer)[0];
            break;
        case CV_64F:
            ((double*)buffer)[0] = (double)color;
			for( i = 1; i < cn; i++ )
				((double*)buffer)[i] = ((double*)buffer)[0];
            break;
        default:
            assert(0);
            CV_ERROR( CV_BadDepth, "" );
        }
    }

    __END__;
}


CV_IMPL void
cvLine( void* img, CvPoint pt1, CvPoint pt2, double color,
        int thickness, int connectivity )
{
    CV_FUNCNAME( "cvLine" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( (unsigned)thickness > 255  )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));
    icvThickLine( mat, pt1, pt2, thickness, buf, connectivity );

    __END__;
}



CV_IMPL void
cvLineAA( void *img, CvPoint pt1, CvPoint pt2, double color, int scale )
{
    CV_FUNCNAME( "cvLineAA" );

    __BEGIN__;

    int coi = 0, cn;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

    if( mat != &stub )
    {
        stub = *mat;
        mat = &stub;
    }

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

    if( scale < 0 || scale > XY_SHIFT )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    cn = CV_MAT_CN( mat->type );

    if( CV_MAT_DEPTH( mat->type) != CV_8U )
        CV_ERROR( CV_BadDepth, icvUnsupportedFormat );

    if( cn != 1 && cn != 3 )
        CV_ERROR( CV_BadNumChannels, icvUnsupportedFormat );

    /* shrink window because antialiased line is 3-pixel width */
    mat->width -= 4;
    mat->height -= 4;
    mat->data.ptr += (mat->step + cn)*2;

    if( mat->width > 0 && mat->height > 0 )
    {
        pt1.x = (pt1.x << (XY_SHIFT - scale)) - 2 * XY_ONE;
        pt1.y = (pt1.y << (XY_SHIFT - scale)) - 2 * XY_ONE;
        pt2.x = (pt2.x << (XY_SHIFT - scale)) - 2 * XY_ONE;
        pt2.y = (pt2.y << (XY_SHIFT - scale)) - 2 * XY_ONE;

        icvLineAA( mat, pt1, pt2, buf );
    }

    __END__;
}


CV_IMPL void
cvRectangle( void* img, CvPoint pt1, CvPoint pt2,
             double color, int thickness )
{
    CvPoint pt_arr[4];
    CvPoint *pt = pt_arr;
    int count = 4;

    CV_FUNCNAME("cvRectangle");

    __BEGIN__;

    if( thickness < -1 || thickness > 255 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    pt_arr[0] = pt1;
    pt_arr[1].x = pt2.x;
    pt_arr[1].y = pt1.y;
    pt_arr[2] = pt2;
    pt_arr[3].x = pt1.x;
    pt_arr[3].y = pt2.y;

    if( thickness >= 0 )
    {
        cvPolyLine( img, &pt, &count, 1, 1, color, thickness );
    }
    else
    {
        cvFillConvexPoly( img, pt, count, color );
    }

    __END__;
}


CV_IMPL void
cvCircle( void *img, CvPoint center, int radius, double color, int thickness )
{
    CV_FUNCNAME( "cvCircle" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( radius < 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    if( thickness < -1 || thickness > 255 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    if( thickness > 1 )
    {
        icvEllipseEx( mat, center, cvSize( radius, radius ), 0, 0, 360,
                      Ellipse_Simple, 0, thickness, buf );
    }
    else if( thickness >= 0 )
    {
        icvCircle( mat, center, radius, buf );
    }
    else
    {
        icvFillCircle( mat, center, radius, buf );
    }

    __END__;
}



CV_IMPL void
cvCircleAA( void *img, CvPoint center, int radius,
            double color, int scale )
{
    CV_FUNCNAME( "cvCircleAA" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( radius < 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    if( radius < 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    if( scale < 0 || scale > XY_SHIFT )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvEllipseEx( mat, center, cvSize( radius , radius ),
                           0, 0, 360, Ellipse_Antialiazed, scale,
                           1, buf ));

    __END__;
}



CV_IMPL void
cvEllipse( void *img, CvPoint center, CvSize axes,
           double angle, double startAngle, double endAngle,
           double color, int thickness )
{
    CV_FUNCNAME( "cvEllipse" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( axes.width < 0 || axes.height < 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    if( thickness < -1 || thickness > 255 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    {
        CvEllipseKind kind = Ellipse_Simple;
        if( thickness < 0 )
        {
            kind = Ellipse_Filled;
            thickness = 1;
        }

        CV_CALL( icvEllipseEx( mat, center, axes, cvRound( angle ),
                               cvRound( startAngle ), cvRound( endAngle ),
                               kind, 0, thickness, buf ));
    }

    __END__;
}


CV_IMPL void
cvEllipseAA( void *img, CvPoint center, CvSize axes,
             double angle, double startAngle, double endAngle,
             double color, int scale )
{
    CV_FUNCNAME( "cvEllipseAA" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( scale < 0 || scale > XY_SHIFT )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    if( axes.width < 0 || axes.height < 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    CV_CALL( icvEllipseEx( mat, center, axes, cvRound( angle ),
                           cvRound( startAngle ), cvRound( endAngle ),
                           Ellipse_Antialiazed, scale, 1, buf ));

    __END__;
}


CV_IMPL void
cvFillConvexPoly( void *img, CvPoint *pts, int npts, double color )
{
    CV_FUNCNAME( "cvFillConvexPoly" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( !pts )
        CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR );

    if( npts <= 0 )
        CV_ERROR_FROM_STATUS( CV_BADRANGE_ERR );

    CV_CALL( icvExtractColor( color, mat->type, buf ));

    icvFillConvexPoly( mat, pts, npts, buf );

    __END__;
}


CV_IMPL void
cvFillPoly( void *img, CvPoint **pts, int *npts, int contours, double color )
{
    CvMemStorage* st = 0;
    
    CV_FUNCNAME( "cvFillPoly" );

    __BEGIN__;

    int coi = 0;
    CvMat stub, *mat = (CvMat*)img;
    double buf[4];

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

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

    if( contours <= 0 )
        CV_ERROR( CV_StsBadArg, "" );

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

⌨️ 快捷键说明

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