Skip to content

Commit a84a5e8

Browse files
committed
Merge pull request opencv#8936 from terfendail:clipline_fix
2 parents 8bd2e98 + 3d7fd41 commit a84a5e8

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

modules/imgproc/src/drawing.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ bool clipLine( Size2l img_size, Point2l& pt1, Point2l& pt2 )
111111
if( c1 & 12 )
112112
{
113113
a = c1 < 8 ? 0 : bottom;
114-
x1 += (a - y1) * (x2 - x1) / (y2 - y1);
114+
x1 += (int64)((double)(a - y1) * (x2 - x1) / (y2 - y1));
115115
y1 = a;
116116
c1 = (x1 < 0) + (x1 > right) * 2;
117117
}
118118
if( c2 & 12 )
119119
{
120120
a = c2 < 8 ? 0 : bottom;
121-
x2 += (a - y2) * (x2 - x1) / (y2 - y1);
121+
x2 += (int64)((double)(a - y2) * (x2 - x1) / (y2 - y1));
122122
y2 = a;
123123
c2 = (x2 < 0) + (x2 > right) * 2;
124124
}
@@ -127,14 +127,14 @@ bool clipLine( Size2l img_size, Point2l& pt1, Point2l& pt2 )
127127
if( c1 )
128128
{
129129
a = c1 == 1 ? 0 : right;
130-
y1 += (a - x1) * (y2 - y1) / (x2 - x1);
130+
y1 += (int64)((double)(a - x1) * (y2 - y1) / (x2 - x1));
131131
x1 = a;
132132
c1 = 0;
133133
}
134134
if( c2 )
135135
{
136136
a = c2 == 1 ? 0 : right;
137-
y2 += (a - x2) * (y2 - y1) / (x2 - x1);
137+
y2 += (int64)((double)(a - x2) * (y2 - y1) / (x2 - x1));
138138
x2 = a;
139139
c2 = 0;
140140
}
@@ -314,7 +314,7 @@ LineAA( Mat& img, Point2l pt1, Point2l pt2, const void* color )
314314

315315
if( !((nch == 1 || nch == 3 || nch == 4) && img.depth() == CV_8U) )
316316
{
317-
Line(img, Point((int)(pt1.x<<XY_SHIFT), (int)(pt1.y<<XY_SHIFT)), Point((int)(pt2.x<<XY_SHIFT), (int)(pt2.y<<XY_SHIFT)), color);
317+
Line(img, Point((int)(pt1.x>>XY_SHIFT), (int)(pt1.y>>XY_SHIFT)), Point((int)(pt2.x>>XY_SHIFT), (int)(pt2.y>>XY_SHIFT)), color);
318318
return;
319319
}
320320

modules/imgproc/test/test_drawing.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,31 @@ TEST(Drawing, polylines)
709709
ASSERT_EQ(cnt, 21);
710710
}
711711

712+
TEST(Drawing, longline)
713+
{
714+
Mat mat = Mat::zeros(256, 256, CV_8UC1);
715+
716+
line(mat, cv::Point(34, 204), cv::Point(46400, 47400), cv::Scalar(255), 3);
717+
EXPECT_EQ(310, cv::countNonZero(mat));
718+
719+
Point pt[6];
720+
pt[0].x = 32;
721+
pt[0].y = 204;
722+
pt[1].x = 34;
723+
pt[1].y = 202;
724+
pt[2].x = 87;
725+
pt[2].y = 255;
726+
pt[3].x = 82;
727+
pt[3].y = 255;
728+
pt[4].x = 37;
729+
pt[4].y = 210;
730+
pt[5].x = 37;
731+
pt[5].y = 209;
732+
fillConvexPoly(mat, pt, 6, cv::Scalar(0));
733+
734+
EXPECT_EQ(0, cv::countNonZero(mat));
735+
}
736+
712737

713738
TEST(Drawing, putText_no_garbage)
714739
{

0 commit comments

Comments
 (0)