📄 cxdrawing.cpp
字号:
//////////////////////////////////////////////////////////////////////////////////////////// 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 For Embedded Computer Vision Library//// Copyright (c) 2008, EMCV Project,// Copyright (c) 2000-2007, 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://// * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer.// * 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.// * Neither the name of the copyright holders nor the names of their contributors // may 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 COPYRIGHT OWNER 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.//// Contributors:// * Shiqi Yu (Shenzhen Institute of Advanced Technology, Chinese Academy of Sciences)#include "_cxcore.h"#define XY_SHIFT 16#define XY_ONE (1 << XY_SHIFT)#define CV_DRAWING_STORAGE_BLOCK ((1 << 12) - 256)typedef struct CvPolyEdge{ int x, dx; union { struct CvPolyEdge *next; int y0; }; int y1;}CvPolyEdge;static voidicvPolyLine( CvMat* img, CvPoint *v, int count, int closed, const void* color, int thickness, int line_type, int shift );static voidicvFillConvexPoly( CvMat* img, CvPoint* v, int npts, const void* color, int line_type, int shift );/****************************************************************************************\* Lines *\****************************************************************************************/CV_IMPL intcvClipLine( CvSize img_size, CvPoint* pt1, CvPoint* pt2 ){ int result = 0; CV_FUNCNAME( "cvClipLine" ); __BEGIN__; int x1, y1, x2, y2; int c1, c2; int right = img_size.width-1, bottom = img_size.height-1; if( !pt1 || !pt2 ) CV_ERROR( CV_StsNullPtr, "One of point pointers is NULL" ); if( right < 0 || bottom < 0 ) CV_ERROR( CV_StsOutOfRange, "Image width or height are negative" ); x1 = pt1->x; y1 = pt1->y; x2 = pt2->x; y2 = pt2->y; c1 = (x1 < 0) + (x1 > right) * 2 + (y1 < 0) * 4 + (y1 > bottom) * 8; c2 = (x2 < 0) + (x2 > right) * 2 + (y2 < 0) * 4 + (y2 > bottom) * 8; if( (c1 & c2) == 0 && (c1 | c2) != 0 ) { int a; if( c1 & 12 ) { a = c1 < 8 ? 0 : bottom; x1 += (int) (( (a - y1)) * (x2 - x1) / (y2 - y1)); y1 = a; c1 = (x1 < 0) + (x1 > right) * 2; } if( c2 & 12 ) { a = c2 < 8 ? 0 : bottom; x2 += (int) (( (a - y2)) * (x2 - x1) / (y2 - y1)); y2 = a; c2 = (x2 < 0) + (x2 > right) * 2; } if( (c1 & c2) == 0 && (c1 | c2) != 0 ) { if( c1 ) { a = c1 == 1 ? 0 : right; y1 += (int) (( (a - x1)) * (y2 - y1) / (x2 - x1)); x1 = a; c1 = 0; } if( c2 ) { a = c2 == 1 ? 0 : right; y2 += (int) (( (a - x2)) * (y2 - y1) / (x2 - x1)); x2 = a; c2 = 0; } } assert( (c1 & c2) != 0 || (x1 | y1 | x2 | y2) >= 0 ); pt1->x = x1; pt1->y = y1; pt2->x = x2; pt2->y = y2; } result = ( c1 | c2 ) == 0; __END__; return result;}/* Initializes line iterator. Returns number of points on the line or negative number if error.*/CV_IMPL intcvInitLineIterator( const CvArr* img, CvPoint pt1, CvPoint pt2, CvLineIterator* iterator, int connectivity, int left_to_right ){ int count = -1; CV_FUNCNAME( "cvInitLineIterator" ); __BEGIN__; CvMat stub, *mat = (CvMat*)img; int dx, dy, s; int bt_pix, bt_pix0, step; if( !CV_IS_MAT(mat) ) CV_CALL( mat = cvGetMat( mat, &stub )); if( !iterator ) CV_ERROR( CV_StsNullPtr, "Pointer to the iterator state is NULL" ); if( connectivity != 8 && connectivity != 4 ) CV_ERROR( CV_StsBadArg, "Connectivity must be 8 or 4" ); 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, "One of the ending points is outside of the image, use cvClipLine" ); bt_pix0 = bt_pix = CV_ELEM_SIZE(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; count = 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; count = dx + dy + 1; } __END__; return count;}static voidicvLine( CvMat* mat, CvPoint pt1, CvPoint pt2, const void* color, int connectivity = 8 ){ if( cvClipLine( cvGetMatSize(mat), &pt1, &pt2 )) { CvLineIterator iterator; int pix_size = CV_ELEM_SIZE(mat->type); int i, count; if( connectivity == 0 ) connectivity = 8; if( connectivity == 1 ) connectivity = 4; count = cvInitLineIterator( mat, pt1, pt2, &iterator, connectivity, 1 ); for( i = 0; i < count; i++ ) { CV_MEMCPY_AUTO( iterator.ptr, color, pix_size ); CV_NEXT_LINE_POINT( iterator ); } }}/* Correction table depent on the slope */static const uchar icvSlopeCorrTable[] = { 181, 181, 181, 182, 182, 183, 184, 185, 187, 188, 190, 192, 194, 196, 198, 201, 203, 206, 209, 211, 214, 218, 221, 224, 227, 231, 235, 238, 242, 246, 250, 254};/* Gaussian for antialiasing filter */static const int icvFilterTable[] = { 168, 177, 185, 194, 202, 210, 218, 224, 231, 236, 241, 246, 249, 252, 254, 254, 254, 254, 252, 249, 246, 241, 236, 231, 224, 218, 210, 202, 194, 185, 177, 168, 158, 149, 140, 131, 122, 114, 105, 97, 89, 82, 75, 68, 62, 56, 50, 45, 40, 36, 32, 28, 25, 22, 19, 16, 14, 12, 11, 9, 8, 7, 5, 5};static voidicvLineAA( CvMat* img, CvPoint pt1, CvPoint pt2, const void* color ){ int dx, dy; int ecount, scount = 0; int slope; int ax, ay; int x_step, y_step; int i, j; int ep_table[9]; int cb = ((uchar*)color)[0], cg = ((uchar*)color)[1], cr = ((uchar*)color)[2]; int _cb, _cg, _cr; int nch = CV_MAT_CN( img->type ); uchar* ptr = (uchar*)(img->data.ptr); int step = img->step; CvSize size = cvGetMatSize( img ); assert( img && (nch == 1 || nch == 3) && CV_MAT_DEPTH(img->type) == CV_8U ); pt1.x -= XY_ONE*2; pt1.y -= XY_ONE*2; pt2.x -= XY_ONE*2; pt2.y -= XY_ONE*2; ptr += img->step*2 + 2*nch; size.width = ((size.width - 5) << XY_SHIFT) + 1; size.height = ((size.height - 5) << XY_SHIFT) + 1; if( !cvClipLine( size, &pt1, &pt2 )) return; dx = pt2.x - pt1.x; dy = pt2.y - pt1.y; j = dx < 0 ? -1 : 0; ax = (dx ^ j) - j; i = dy < 0 ? -1 : 0; ay = (dy ^ i) - i; if( ax > ay ) { dx = ax; dy = (dy ^ j) - j; pt1.x ^= pt2.x & j; pt2.x ^= pt1.x & j; pt1.x ^= pt2.x & j; pt1.y ^= pt2.y & j; pt2.y ^= pt1.y & j; pt1.y ^= pt2.y & j; x_step = XY_ONE; y_step = (int) (( dy << XY_SHIFT) / (ax | 1)); pt2.x += XY_ONE; ecount = (pt2.x >> XY_SHIFT) - (pt1.x >> XY_SHIFT); j = -(pt1.x & (XY_ONE - 1)); pt1.y += (int) (((y_step) * j) >> XY_SHIFT) + (XY_ONE >> 1); slope = (y_step >> (XY_SHIFT - 5)) & 0x3f; slope ^= (y_step < 0 ? 0x3f : 0); /* Get 4-bit fractions for end-point adjustments */ i = (pt1.x >> (XY_SHIFT - 7)) & 0x78; j = (pt2.x >> (XY_SHIFT - 7)) & 0x78; } else { dy = ay; dx = (dx ^ i) - i; pt1.x ^= pt2.x & i; pt2.x ^= pt1.x & i; pt1.x ^= pt2.x & i; pt1.y ^= pt2.y & i; pt2.y ^= pt1.y & i; pt1.y ^= pt2.y & i; x_step = (int) (( dx << XY_SHIFT) / (ay | 1)); y_step = XY_ONE; pt2.y += XY_ONE; ecount = (pt2.y >> XY_SHIFT) - (pt1.y >> XY_SHIFT); j = -(pt1.y & (XY_ONE - 1)); pt1.x += (int) ((( x_step) * j) >> XY_SHIFT) + (XY_ONE >> 1); slope = (x_step >> (XY_SHIFT - 5)) & 0x3f; slope ^= (x_step < 0 ? 0x3f : 0); /* Get 4-bit fractions for end-point adjustments */ i = (pt1.y >> (XY_SHIFT - 7)) & 0x78; j = (pt2.y >> (XY_SHIFT - 7)) & 0x78; } slope = (slope & 0x20) ? 0x100 : icvSlopeCorrTable[slope]; /* Calc end point correction table */ { int t0 = slope << 7; int t1 = ((0x78 - i) | 4) * slope; int t2 = (j | 4) * slope; ep_table[0] = 0; ep_table[8] = slope;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -