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

Fix incorrect color blending for overlapping glyphs #7497

Merged
Merged
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f97570f
Blend colors with alpha when pasting
ZachNagengast Oct 27, 2023
49fd211
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2023
76f758e
Merge branch 'main' into fix-alpha-for-overlapping-glyphs
radarhere Oct 27, 2023
bb0eff4
Update blending logic
ZachNagengast Nov 3, 2023
a7f805d
Merge branch 'fix-alpha-for-overlapping-glyphs' of ssh://github.com/Z…
ZachNagengast Nov 3, 2023
e1aaec3
Merge branch 'main' of ssh://github.com/python-pillow/Pillow into fix…
ZachNagengast Nov 3, 2023
b15b2d4
Update src/_imagingft.c
ZachNagengast Nov 7, 2023
fdecfca
Update gray glyph blending logic and tests
ZachNagengast Nov 7, 2023
8ecf2e9
Merge branch 'fix-alpha-for-overlapping-glyphs' of ssh://github.com/Z…
ZachNagengast Nov 7, 2023
11bea8f
Merge branch 'main' of ssh://github.com/python-pillow/Pillow into fix…
ZachNagengast Nov 7, 2023
d127600
Update test images for overlapping text
ZachNagengast Nov 7, 2023
0a33b30
Update caron_below_ttb test image
ZachNagengast Nov 12, 2023
29ca3fc
Update caron_below_ttb_lb test image
ZachNagengast Nov 12, 2023
f3b3442
add test for glyph alpha blending
nulano Nov 27, 2023
0cef9f2
fix drawing text alpha on RGBA image on big-endian platforms
nulano Nov 27, 2023
38992f6
Merge pull request #1 from nulano/fix-alpha-for-overlapping-glyphs
ZachNagengast Nov 27, 2023
9c60e85
Apply suggestions from code review
ZachNagengast Nov 27, 2023
78f78d2
Update src/_imagingft.c
ZachNagengast Nov 28, 2023
e800026
Update Tests/test_imagefont.py
ZachNagengast Dec 1, 2023
bd2977c
Update src/PIL/ImageDraw.py
ZachNagengast Dec 2, 2023
a6a612c
Merge branch 'main' into fix-alpha-for-overlapping-glyphs
radarhere Dec 2, 2023
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
29 changes: 19 additions & 10 deletions src/_imagingft.c
Expand Up @@ -1059,16 +1059,25 @@ font_render(FontObject *self, PyObject *args) {
if (color && bitmap.pixel_mode == FT_PIXEL_MODE_BGRA) {
/* paste color glyph */
for (k = x0; k < x1; k++) {
if (target[k * 4 + 3] < source[k * 4 + 3]) {
/* unpremultiply BGRa to RGBA */
target[k * 4 + 0] = CLIP8(
(255 * (int)source[k * 4 + 2]) / source[k * 4 + 3]);
target[k * 4 + 1] = CLIP8(
(255 * (int)source[k * 4 + 1]) / source[k * 4 + 3]);
target[k * 4 + 2] = CLIP8(
(255 * (int)source[k * 4 + 0]) / source[k * 4 + 3]);
target[k * 4 + 3] = source[k * 4 + 3];
}
float src_alpha = (float) source[k * 4 + 3] / 255.0;
float dst_alpha = (float) target[k * 4 + 3] / 255.0;
float out_alpha = src_alpha + dst_alpha * (1.0 - src_alpha);

/* unpremultiply BGRa to RGBA */
int src_red = source[k * 4 + 0];
int src_grn = source[k * 4 + 1];
int src_blu = source[k * 4 + 2];

int dst_blu = target[k * 4 + 0];
int dst_grn = target[k * 4 + 1];
int dst_red = target[k * 4 + 2];
ZachNagengast marked this conversation as resolved.
Show resolved Hide resolved

/* blend color values */
target[k * 4 + 0] = CLIP8((src_blu * src_alpha + dst_blu * dst_alpha * (1.0 - src_alpha)) / out_alpha);
target[k * 4 + 1] = CLIP8((src_grn * src_alpha + dst_grn * dst_alpha * (1.0 - src_alpha)) / out_alpha);
target[k * 4 + 2] = CLIP8((src_red * src_alpha + dst_red * dst_alpha * (1.0 - src_alpha)) / out_alpha);

target[k * 4 + 3] = CLIP8(out_alpha * 255.0);
}
} else if (bitmap.pixel_mode == FT_PIXEL_MODE_GRAY) {
if (color) {
Expand Down