From b040240ecb8f07e4240135d5f1d51b493a47d53d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 4 Jan 2025 14:55:26 +1100 Subject: [PATCH] Improved connecting discontiguous corners --- .../discontiguous_corners_polygon.png | Bin 533 -> 533 bytes Tests/test_imagedraw.py | 2 +- src/libImaging/Draw.c | 58 ++++++++---------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/Tests/images/imagedraw/discontiguous_corners_polygon.png b/Tests/images/imagedraw/discontiguous_corners_polygon.png index 1b58889c8f3ae45243a7509c907f1928534bcbde..8992a165758676b9ba7777347fb09a3b8c453e3d 100644 GIT binary patch delta 498 zcmV!!QU#x&NVZwfxr}B*1oAc++Wu^hDyMmFv3h`#y7( z^bX*wh={0kNqVm~ZJk<@{7}`frq38OUF0%$Ri9_ST)mw}n@*lB8`kG(v?=7-vMC9z zPn)FNOFlq0L$jBBfNY*-7kRd9wq_%Fw(Ja(TpUFHEepqypH5182bmeUaqnc-s_XgQ{+#-eC+wU`t#9JT*W=9jlj!u-K3$uKcEi$mwNKY+qig+H7V^#a7ga8(`}`_7Z^}zP zQ9q<-sx8egsg|Yw#I!ktma!{-pYw9{cA9Otc{FxdpQqV|m`7tnCY0Yc zh}>&F0UN>WHJ^ZuV|JNGW22dk=F!+W%xd#!?5C`Y&7-j;iGLTEM`LSqPco0j_UtvF zc|TaBnTP~M<};~0<^#KGm-vZS{wjg@Oab*XJUP5=3P_!s&8re=9eQD8B{9Jc+$-AK5ydi~mnpf?M*P557 z@N)B#eer(t%M?yAKiL-tnSV;*IP>a^$~!1re}Az#u=?k@{eksw9&mD^TA$$8<{7%T zU(d|ucb|eKly1+L^El`HZd84>FK6@E9$Xr)_T`*5+iTxtF+aS1Q}Pz|J-^GG59Kvq z$RE;k|Gt3?V-1fyE-1*Ls!QC1{o@oTsl+9J2Pa? o!@AA+ej3>3+2wVTOis1_06el#wiqRwUjP6A07*qoM6N<$f>{3oE&u=k diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 5fc1c27661a..17a61116f91 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -1683,7 +1683,7 @@ def test_discontiguous_corners_polygon() -> None: BLACK, ) expected = os.path.join(IMAGES_PATH, "discontiguous_corners_polygon.png") - assert_image_similar_tofile(img, expected, 1) + assert_image_equal_tofile(img, expected) def test_polygon2() -> None: diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index ea6f8805efb..d5aff8709f5 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -501,55 +501,49 @@ polygon_generic( // Needed to draw consistent polygons xx[j] = xx[j - 1]; j++; - } else if (current->dx != 0 && j % 2 == 1 && - roundf(xx[j - 1]) == xx[j - 1]) { + } else if ((ymin == current->ymin || ymin == current->ymax) && + current->dx != 0) { // Connect discontiguous corners for (k = 0; k < i; k++) { Edge *other_edge = edge_table[k]; - if ((current->dx > 0 && other_edge->dx <= 0) || - (current->dx < 0 && other_edge->dx >= 0)) { + if ((ymin != other_edge->ymin && ymin != other_edge->ymax) || + other_edge->dx == 0) { continue; } // Check if the two edges join to make a corner - if (xx[j - 1] == - (ymin - other_edge->y0) * other_edge->dx + other_edge->x0) { + if (roundf(xx[j - 1]) == + roundf( + (ymin - other_edge->y0) * other_edge->dx + + other_edge->x0 + )) { // Determine points from the edges on the next row // Or if this is the last row, check the previous row - int offset = ymin == ymax ? -1 : 1; + int offset = ymin == current->ymax ? -1 : 1; adjacent_line_x = (ymin + offset - current->y0) * current->dx + current->x0; - adjacent_line_x_other_edge = - (ymin + offset - other_edge->y0) * other_edge->dx + - other_edge->x0; - if (ymin == current->ymax) { - if (current->dx > 0) { - xx[k] = - fmax( + if (ymin + offset >= other_edge->ymin && + ymin + offset <= other_edge->ymax) { + adjacent_line_x_other_edge = + (ymin + offset - other_edge->y0) * other_edge->dx + + other_edge->x0; + if (xx[j - 1] > adjacent_line_x + 1 && + xx[j - 1] > adjacent_line_x_other_edge + 1) { + xx[j - 1] = + roundf(fmax( adjacent_line_x, adjacent_line_x_other_edge - ) + + )) + 1; - } else { - xx[k] = - fmin( + } else if (xx[j - 1] < adjacent_line_x - 1 && + xx[j - 1] < adjacent_line_x_other_edge - 1) { + xx[j - 1] = + roundf(fmin( adjacent_line_x, adjacent_line_x_other_edge - ) - - 1; - } - } else { - if (current->dx > 0) { - xx[k] = fmin( - adjacent_line_x, adjacent_line_x_other_edge - ); - } else { - xx[k] = - fmax( - adjacent_line_x, adjacent_line_x_other_edge - ) + + )) - 1; } + break; } - break; } } }