From 5491bd5eda3b1c349d96e79248d53cfbe8693372 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 21 Mar 2021 00:33:54 +1100 Subject: [PATCH] Merge parallel horizontal edges in ImagingDrawPolygon --- src/libImaging/Draw.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index b6f63b7e8..bec8f2582 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -734,7 +734,7 @@ ImagingDrawRectangle( int ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int op) { - int i, n; + int i, n, x0, y0, x1, y1; DRAW *draw; INT32 ink; @@ -753,10 +753,28 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i return -1; } for (i = n = 0; i < count - 1; i++) { - add_edge(&e[n++], xy[i + i], xy[i + i + 1], xy[i + i + 2], xy[i + i + 3]); + x0 = xy[i * 2]; + y0 = xy[i * 2 + 1]; + x1 = xy[i * 2 + 2]; + y1 = xy[i * 2 + 3]; + if (y0 == y1 && i != 0 && y0 == xy[i * 2 - 1]) { + // This is a horizontal line, + // that immediately follows another horizontal line + Edge *last_e = &e[n-1]; + if (x1 > x0 && x0 > xy[i * 2 - 2]) { + // They are both increasing in x + last_e->xmax = x1; + continue; + } else if (x1 < x0 && x0 < xy[i * 2 - 2]) { + // They are both decreasing in x + last_e->xmin = x1; + continue; + } + } + add_edge(&e[n++], x0, y0, x1, y1); } - if (xy[i + i] != xy[0] || xy[i + i + 1] != xy[1]) { - add_edge(&e[n++], xy[i + i], xy[i + i + 1], xy[0], xy[1]); + if (xy[i * 2] != xy[0] || xy[i * 2 + 1] != xy[1]) { + add_edge(&e[n++], xy[i * 2], xy[i * 2 + 1], xy[0], xy[1]); } draw->polygon(im, n, e, ink, 0); free(e); @@ -764,9 +782,9 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i } else { /* Outline */ for (i = 0; i < count - 1; i++) { - draw->line(im, xy[i + i], xy[i + i + 1], xy[i + i + 2], xy[i + i + 3], ink); + draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink); } - draw->line(im, xy[i + i], xy[i + i + 1], xy[0], xy[1], ink); + draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink); } return 0;