Skip to content

Blend I;16, I and F pixels as whole values when pasting with a mask - #10378

Open
waw666waw666 wants to merge 4 commits into
python-pillow:mainfrom
waw666waw666:fix-paste-mask-wide-modes
Open

waw666waw666 wants to merge 4 commits into
python-pillow:mainfrom
waw666waw666:fix-paste-mask-wide-modes

Conversation

@waw666waw666

Copy link
Copy Markdown

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:

>>> mask = Image.new("L", (1, 1), 128)
>>> im = Image.new("F", (1, 1), 1.0)
>>> im.paste(Image.new("F", (1, 1), 2.0), (0, 0), mask)
>>> im.getpixel((0, 0))
3.0
>>> im = Image.new("I", (1, 1), -10)
>>> im.paste(Image.new("I", (1, 1), 10), (0, 0), mask)
>>> im.getpixel((0, 0))
2139062144
>>> im = Image.new("I;16", (1, 1), 255)
>>> im.paste(Image.new("I;16", (1, 1), 256), (0, 0), mask)
>>> im.getpixel((0, 0))
127

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:

  • Add paste_mask_wide() and fill_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 as BLEND / PREBLEND, then round and clip to the range of the mode.
  • Put the I;16 ink in the byte order of the mode, and write two bytes per pixel when filling I;16 without a mask or with a 1 mask.
  • Add tests for each mode and mask, and a release note.

@Muguett-DBY Muguett-DBY left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I found an endpoint case in the new float blending path; details are inline.

Comment thread src/libImaging/Paste.c
if (premultiplied) {
return in + out * (255 - a) / 255.0;
}
return (in * a + out * (255 - a)) / 255.0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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: nan

The 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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants