Merge pull request #5274 from radarhere/gradient

Fixed linear_gradient and radial_gradient I and F modes
This commit is contained in:
wiredfool 2021-03-28 14:35:22 +01:00 committed by GitHub
commit 9a683db339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 6 deletions

View File

@ -524,7 +524,7 @@ class TestImage:
# 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)
@ -550,7 +550,7 @@ class TestImage:
# 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)

View File

@ -76,9 +76,22 @@ ImagingFillLinearGradient(const char *mode) {
return NULL;
}
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) {
IMAGING_PIXEL_FLOAT32(im, x, y) = y;
} else {
IMAGING_PIXEL_INT32(im, x, y) = y;
}
}
}
}
return im;
}
@ -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;
}
}
}
}