Repository navigation
Blend I;16, I and F pixels as whole values when pasting with a mask - #10378
waw666waw666 wants to merge 4 commits into
Conversation
Muguett-DBY
left a comment
There was a problem hiding this comment.
I found an endpoint case in the new float blending path; details are inline.
| if (premultiplied) { | ||
| return in + out * (255 - a) / 255.0; | ||
| } | ||
| return (in * a + out * (255 - a)) / 255.0; |
There was a problem hiding this comment.
Could this preserve the fully transparent / fully opaque cases when an F pixel is NaN or infinity? Multiplying the ignored value by zero still produces NaN:
from PIL import Image
dst = Image.new("F", (1, 1), 1.5)
src = Image.new("F", (1, 1), float("nan"))
dst.paste(src, (0, 0), Image.new("L", (1, 1), 0))
print(dst.getpixel((0, 0))) # base: 1.5; this PR: nanThe reverse also occurs with a NaN destination, finite source and mask 255; infinity produces the same result. I reproduced both image and scalar-color pastes on Windows/Python 3.12, comparing 0634558 with its parent 86399b1. The PR's 42 wide-mode tests and both builds' selftests passed. These were in-memory tests; the local builds had JPEG and zlib disabled.
Would short-circuiting the L-mask endpoints before the arithmetic be appropriate? Tests with non-finite values on the ignored side would catch this.
There was a problem hiding this comment.
Thanks for catching this! Fixed in ba533da.
blend_wide() now returns the source as it is when alpha is 255, and the destination as it is when alpha is 0 (L, LA and RGBA masks), before any arithmetic. With an RGBa mask, alpha 0 still adds the premultiplied source like PREBLEND, so only the opaque end is short-circuited there. This covers both image and color pastes.
Added test_mask_wide_non_finite_ignored, with NaN, inf and -inf on the ignored side for each mask mode; it fails on 0634558 and passes now. Tests/test_image_paste.py passes (115), and the full suite shows no new failures compared to 0634558 (my local build has zlib and JPEG disabled, so those tests fail on both). ruff, black and clang-format are clean.
…ix-paste-mask-wide-modes Resolve Paste.c conflicts in fill() and fill_mask_1(): keep this branch's I;16 byte-copy fill (ink is already swapped into the mode's byte order) and take main's mode-1 mask pointer arithmetic.
paste()with an L, LA, RGBA or RGBa mask blends I;16, I and F images one byte at a time, as if each byte of a pixel were a separate 8-bit channel. Any partly transparent pixel ends up with an unrelated value:For I;16, the L mask path also steps one byte per pixel, so it only covers the first half of each row. Pasting a color has similar problems: without a mask or with a 1 mask,
im.paste(4660, box)on an I;16 image fills with 13364 (the low byte repeated), and with an L mask the ink is not put in the byte order of I;16B.Changes proposed in this pull request:
paste_mask_wide()andfill_mask_wide(), used for I;16, I and F images with L, LA, RGBA and RGBa masks. They read each pixel as a number (in the byte order of the mode for I;16), blend it the same way asBLEND/PREBLEND, then round and clip to the range of the mode.