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

When converting, clip I;16 to be unsigned, not signed #6112

Merged
merged 1 commit into from Mar 10, 2022

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Mar 5, 2022

Resolves #5967

>>> from PIL import Image
>>> Image.new("I;16", (1, 1), 65535).load()[0, 0]
65535
>>> Image.new("I;16", (1, 1), 65536).load()[0, 0]
0

So we can agree that the maximum for "I;16" is 65535, a.k.a 2**16 - 1, as expected.

When converting from "I" to "I;16", there is clipping. So you would expect that clipping to limit the values of "I", the 32-bit mode, to the maximum value of "I;16", the 16-bit mode.

Except it doesn't.

>>> im = Image.new("I", (1, 1), 65535)
>>> im.convert("I;16").load()[0, 0]
32767

The following line incorrectly imagines that we need to clip to an signed range, not an unsigned range.

#define CLIP16(v) ((v) <= -32768 ? -32768 : (v) >= 32767 ? 32767 : (v))

This PR corrects it.

@hugovk hugovk merged commit 652542c into python-pillow:main Mar 10, 2022
@radarhere radarhere deleted the i_i16_conversion branch March 10, 2022 20:34
@radarhere radarhere changed the title Clip I;16 to be unsigned, not signed When converting, clip I;16 to be unsigned, not signed Mar 10, 2022
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.

Brightness clipped on I;16 images loaded from png
2 participants