Repository navigation
Make ImageDraw line and polygon drawing translation-invariant for negative coordinates - #10564
Closed
tony1233200 wants to merge 2 commits into
Closed
tony1233200 wants to merge 2 commits into
tony1233200 wants to merge 2 commits into
Conversation
…ative coordinates Line and polygon coordinates were converted from float to int by truncation toward zero, and wide-line edges were rounded around zero, so a shape extending past the left or top edge of an image rasterised differently from the same shape shifted by an integer offset: geometry lying entirely at negative coordinates could still set pixels in column/row 0. Convert line and polygon coordinates with floor() instead, and round wide-line edges half away from -inf, so rasterisation only depends on the fractional position of the geometry. Fixes python-pillow#10228.
for more information, see https://pre-commit.ci
Member
|
Closing, as #10575 was created by the author of the issue, and contains tests that do not pass in this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10228.
The problem
ImageDrawconverted float coordinates to integers by truncation towardzero, and wide-line edges were rounded "around zero" (
ROUND_UP/ROUND_DOWNin
Draw.cmirrored the result for negative values). Rasterisation thereforedepended on where the origin sat relative to the shape: a polygon lying
entirely left of
x = 0(from -0.9 to -0.2) still set 7 pixels in column 0,while the same polygon shifted right by 10 px set none. Anyone drawing tiled
geometry (map tiles, cropped views) gets different pixels depending on the
tile offset.
Reproduction from the issue, before this change:
After this change all four cases are identical (
differ: 0).The change
src/_imaging.c: line, wide-line and polygon coordinates are convertedwith
floor()(IMAGING_FLOOR) instead of a truncating(int)cast.src/libImaging/Draw.c:ROUND_UP(f)is nowfloor(f + 0.5)andROUND_DOWN(f)isceil(f - 0.5)at every position, not mirrored aroundzero, so wide-line edge rounding is translation-invariant across the
origin.
ellipse, arc and pieslice coordinate handling is unchanged.
docs/releasenotes/13.1.0.rst.Tests
test_negative_coordinates_translation_invarianceinTests/test_imagedraw.py, covering the four shapes from the issue. Itfails on the old code and passes with this change.
python3 selftest.py: 59 tests passed.Tests/test_imagedraw.py: 273 passed, 1 xfailed (pre-existing).Tests/test_imagedraw2.py+Tests/test_imagepath.py: 92 passed.Tests/test_imagedraw_i16.py+Tests/test_psdraw.py: 60 passed.Disclosure: this fix was prepared with AI assistance.