Skip to content

Port insertion sort improvement of libgd 2.0.26 #17342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions ext/gd/libgd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2715,8 +2715,6 @@ void gdImageOpenPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
}
}

int gdCompareInt (const void *a, const void *b);

/* THANKS to Kirsten Schulz for the polygon fixes! */

/* The intersection finding technique of this code could be improved
Expand All @@ -2728,6 +2726,8 @@ int gdCompareInt (const void *a, const void *b);
void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
{
int i;
int j;
int index;
int y;
int miny, maxy, pmaxy;
int x1, y1;
Expand Down Expand Up @@ -2832,8 +2832,21 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
im->polyInts[ints++] = x2;
}
}
qsort(im->polyInts, ints, sizeof(int), gdCompareInt);

/*
2.0.26: polygons pretty much always have less than 100 points,
and most of the time they have considerably less. For such trivial
cases, insertion sort is a good choice. Also a good choice for
future implementations that may wish to indirect through a table.
*/
for (i = 1; (i < ints); i++) {
index = im->polyInts[i];
j = i;
while ((j > 0) && (im->polyInts[j - 1] > index)) {
im->polyInts[j] = im->polyInts[j - 1];
j--;
}
im->polyInts[j] = index;
}
for (i = 0; i < ints - 1; i += 2) {
gdImageLine(im, im->polyInts[i], y, im->polyInts[i + 1], y, fill_color);
}
Expand All @@ -2845,11 +2858,6 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
}
}

int gdCompareInt (const void *a, const void *b)
{
return (*(const int *) a) - (*(const int *) b);
}

void gdImageSetStyle (gdImagePtr im, int *style, int noOfPixels)
{
if (im->style) {
Expand Down
Loading