Repository navigation
Clip F to I conversion to the INT32 range - #10373
Conversation
f2i cast floats to INT32 with no range check. Out-of-range values and NaN are undefined behavior in C, and on x86 they all became -2147483648. Clip to the INT32 range like clip32() in Filter.c, and map NaN to 0 like the F to L and F to I;16 converters.
|
At the moment in main, NaN is rejected by L mode images, but not F. >>> from PIL import Image
>>> Image.new("L", (1, 1), float("nan"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "PIL/Image.py", line 3277, in new
return im._new(core.fill(mode, size, color))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: color must be int or single-element tuple
>>> Image.new("F", (1, 1), float("nan"))
<PIL.Image.Image image mode=F size=1x1 at 0x105DCE3D0>I've pushed a commit to reject it for F mode as well, and also rearranged the code to be more in line with Pillow/src/libImaging/Convert.c Line 585 in 22e8391 See what you think. |
|
While my last commit added helpful validation, somehow, I missed that it did not actually raise an error for NaN. I've created #10441 to follow up. |
Ah, sorry, forgot about this. Just as well that I missed it then. |
|
Here are the current results with the merged form of this PR. Let me know what you think. >>> from PIL import Image
>>> Image.new("F", (1, 1), float("inf")).convert("I").getpixel((0, 0))
2147483647
>>> Image.new("F", (1, 1), float("nan")).convert("I").getpixel((0, 0))
-2147483648
>>> Image.new("F", (1, 1), float("-inf")).convert("I").getpixel((0, 0))
-2147483648 |
Fixes #10372.
Changes proposed in this pull request:
FtoIconversion to theINT32range inf2i, and map NaN to 0. The cast had no range check, so on x86 every value above the range,infandNaNbecame -2147483648, and anIimage at 2147483647 converted toFand back did too. These casts are undefined behavior in C, so the result also depended on the CPU.inf,-infandNaN.With this change, it is 2147483647, the same clipping
clip32()inFilter.cdoes. NaN becomes 0, which is whatFtoLandFtoI;16give.