From 98181e9d7f3477adf3e4c945887c47c3be5b9354 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 7 Dec 2016 03:43:02 +0300 Subject: [PATCH] imgproc/drawing: minor refactoring in FillConvexPoly --- modules/imgproc/src/drawing.cpp | 62 ++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index bd838c6c5d..113f499d98 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -1099,7 +1099,7 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin edge[2]; int delta = 1 << shift >> 1; - int i, y, imin = 0, left = 0, right = 1; + int i, y, imin = 0; int edges = npts; int64 xmin, xmax, ymin, ymax; uchar* ptr = img.ptr(); @@ -1181,44 +1181,46 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin { if( y >= edge[i].ye ) { - int idx = edge[i].idx, di = edge[i].di; - int64 xs = 0, xe; + int idx0 = edge[i].idx, di = edge[i].di; + int idx = idx0 + di; + if (idx >= npts) idx -= npts; int ty = 0; - for(;;) + for (; edges-- > 0; ) { ty = (int)((v[idx].y + delta) >> shift); - if( ty > y || edges == 0 ) + if (ty > y) + { + int64 xs = v[idx0].x; + int64 xe = v[idx].x; + if (shift != XY_SHIFT) + { + xs <<= XY_SHIFT - shift; + xe <<= XY_SHIFT - shift; + } + + edge[i].ye = ty; + edge[i].dx = ((xe - xs)*2 + (ty - y)) / (2 * (ty - y)); + edge[i].x = xs; + edge[i].idx = idx; break; - xs = v[idx].x; + } + idx0 = idx; idx += di; - idx -= ((idx < npts) - 1) & npts; /* idx -= idx >= npts ? npts : 0 */ - edges--; + if (idx >= npts) idx -= npts; } - - xs <<= XY_SHIFT - shift; - xe = v[idx].x << (XY_SHIFT - shift); - - /* no more edges */ - if( y >= ty) - return; - - edge[i].ye = ty; - edge[i].dx = ((xe - xs)*2 + (ty - y)) / (2 * (ty - y)); - edge[i].x = xs; - edge[i].idx = idx; } } } - if( edge[left].x > edge[right].x ) + if (y >= 0) { - left ^= 1; - right ^= 1; - } + int left = 0, right = 1; + if (edge[0].x > edge[1].x) + { + left = 1, right = 0; + } - if( y >= 0 ) - { int xx1 = (int)((edge[left].x + delta1) >> XY_SHIFT); int xx2 = (int)((edge[right].x + delta2) >> XY_SHIFT); @@ -1231,9 +1233,13 @@ FillConvexPoly( Mat& img, const Point2l* v, int npts, const void* color, int lin ICV_HLINE( ptr, xx1, xx2, color, pix_size ); } } + else + { + // TODO optimize scan for negative y + } - edge[left].x += edge[left].dx; - edge[right].x += edge[right].dx; + edge[0].x += edge[0].dx; + edge[1].x += edge[1].dx; ptr += img.step; } while( ++y <= (int)ymax );