Skip to content
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

Fixed linear_gradient and radial_gradient I and F modes #5274

Merged
merged 1 commit into from Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Tests/test_image.py
Expand Up @@ -519,7 +519,7 @@ def test_linear_gradient(self):

# Arrange
target_file = "Tests/images/linear_gradient.png"
for mode in ["L", "P"]:
for mode in ["L", "P", "I", "F"]:

# Act
im = Image.linear_gradient(mode)
Expand All @@ -545,7 +545,7 @@ def test_radial_gradient(self):

# Arrange
target_file = "Tests/images/radial_gradient.png"
for mode in ["L", "P"]:
for mode in ["L", "P", "I", "F"]:

# Act
im = Image.radial_gradient(mode)
Expand Down
28 changes: 24 additions & 4 deletions src/libImaging/Fill.c
Expand Up @@ -76,8 +76,21 @@ ImagingFillLinearGradient(const char *mode) {
return NULL;
}

for (y = 0; y < 256; y++) {
memset(im->image8[y], (unsigned char)y, 256);
if (im->image8) {
for (y = 0; y < 256; y++) {
memset(im->image8[y], (unsigned char)y, 256);
}
} else {
int x;
for (y = 0; y < 256; y++) {
for (x = 0; x < 256; x++) {
if (im->type == IMAGING_TYPE_FLOAT32) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does refactoring this check outside the 256 * 256 loop make any sort of performance difference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Branch prediction should be bang on for this one. And I'd even expect that the compiler would hoist this.

IMAGING_PIXEL_FLOAT32(im, x, y) = y;
} else {
IMAGING_PIXEL_INT32(im, x, y) = y;
}
}
}
}

return im;
Expand All @@ -103,9 +116,16 @@ ImagingFillRadialGradient(const char *mode) {
d = (int)sqrt(
(double)((x - 128) * (x - 128) + (y - 128) * (y - 128)) * 2.0);
if (d >= 255) {
im->image8[y][x] = 255;
} else {
d = 255;
}
if (im->image8) {
im->image8[y][x] = d;
} else {
if (im->type == IMAGING_TYPE_FLOAT32) {
IMAGING_PIXEL_FLOAT32(im, x, y) = d;
} else {
IMAGING_PIXEL_INT32(im, x, y) = d;
}
}
}
}
Expand Down