Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,3 +1846,38 @@ def test_dash_empty() -> None:

with pytest.raises(ValueError, match="dash must be a non-empty tuple of ints"):
draw.rectangle([10, 10, 90, 90], dash=())


def test_negative_coordinates_translation_invariance() -> None:
# Drawing a shape must not depend on where the origin is: a shape drawn
# at a negative offset and the same shape drawn shifted by an integer
# offset, then cropped back, must produce identical pixels.
shift = 10
width = height = 12

def draw(
kind: str, pts: list[tuple[float, float]], dx: int, line_width: int = 0
) -> Image.Image:
im = Image.new("L", (width + shift, height), 0)
d = ImageDraw.Draw(im)
shifted = [(x + dx, y) for x, y in pts]
if kind == "polygon":
d.polygon(shifted, fill=255, outline=255)
else:
d.line(shifted, fill=255, width=line_width)
return im.crop((dx, 0, dx + width, height))

shapes: list[tuple[str, list[tuple[float, float]], int]] = [
# A sliver lying entirely left of x = 0
("polygon", [(-0.9, 2), (-0.2, 2), (-0.2, 8), (-0.9, 8)], 0),
# A box whose left edge is at x = -0.3
("polygon", [(-0.3, 2), (5.5, 2), (5.5, 8), (-0.3, 8)], 0),
# A vertical line, 1 px wide, centred at x = -0.6
("line", [(-0.6, 2), (-0.6, 9)], 1),
# A vertical line, 3 px wide, centred at x = -1.6
("line", [(-1.6, 2), (-1.6, 9)], 3),
]
for kind, pts, line_width in shapes:
assert_image_equal(
draw(kind, pts, 0, line_width), draw(kind, pts, shift, line_width)
)
9 changes: 9 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ Two new image processing operations are available in ImageOps.
Other changes
=============

Negative ImageDraw coordinates
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

``ImageDraw`` line and polygon coordinates are now rounded toward negative
infinity instead of toward zero, and wide line edges are rounded in the same
way at every position. Drawing a shape that extends past the left or top edge
of an image now produces the same pixels as drawing it shifted into the image
and cropping back.

Rounding crop coordinates
^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
30 changes: 19 additions & 11 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@
#include <math.h>
#include <stddef.h>

/* ImageDraw coordinates are rounded toward -inf, so that drawing a shape
does not depend on where the origin sits relative to the shape. */
#define IMAGING_FLOOR(v) ((int)floor(v))

#define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1])
#define L16(p, i) ((((int)p[(i) + 1]) << 8) + p[(i)])
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))
Expand Down Expand Up @@ -3783,10 +3787,10 @@ _draw_lines(ImagingDrawObject *self, PyObject *args) {
p = &xy[i + i];
if (ImagingDrawLine(
self->image->image,
(int)p[0],
(int)p[1],
(int)p[2],
(int)p[3],
IMAGING_FLOOR(p[0]),
IMAGING_FLOOR(p[1]),
IMAGING_FLOOR(p[2]),
IMAGING_FLOOR(p[3]),
&ink,
self->blend,
dash,
Expand All @@ -3798,18 +3802,22 @@ _draw_lines(ImagingDrawObject *self, PyObject *args) {
}
if (p) { /* draw last point */
ImagingDrawPoint(
self->image->image, (int)p[2], (int)p[3], &ink, self->blend
self->image->image,
IMAGING_FLOOR(p[2]),
IMAGING_FLOOR(p[3]),
&ink,
self->blend
);
}
} else {
for (i = 0; i < n - 1; i++) {
double *p = &xy[i + i];
if (ImagingDrawWideLine(
self->image->image,
(int)p[0],
(int)p[1],
(int)p[2],
(int)p[3],
IMAGING_FLOOR(p[0]),
IMAGING_FLOOR(p[1]),
IMAGING_FLOOR(p[2]),
IMAGING_FLOOR(p[3]),
&ink,
width,
self->blend,
Expand Down Expand Up @@ -3990,8 +3998,8 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) {
}

for (i = 0; i < n; i++) {
ixy[i + i] = (int)xy[i + i];
ixy[i + i + 1] = (int)xy[i + i + 1];
ixy[i + i] = IMAGING_FLOOR(xy[i + i]);
ixy[i + i + 1] = IMAGING_FLOOR(xy[i + i + 1]);
}

free(xy);
Expand Down
9 changes: 5 additions & 4 deletions src/libImaging/Draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ ink16(Imaging im, const void *ink_) {
}

/*
* Rounds around zero (up=away from zero, down=towards zero)
* This guarantees that ROUND_UP|DOWN(f) == -ROUND_UP|DOWN(-f)
* Rounds half away from -inf (up = floor(f + 0.5), down = ceil(f - 0.5)),
* so the result only depends on the fractional position of f, not on its
* sign. This keeps rasterisation translation-invariant across the origin.
*/
#define ROUND_UP(f) ((int)((f) >= 0.0 ? floor((f) + 0.5F) : -floor(fabs(f) + 0.5F)))
#define ROUND_DOWN(f) ((int)((f) >= 0.0 ? ceil((f) - 0.5F) : -ceil(fabs(f) - 0.5F)))
#define ROUND_UP(f) ((int)floor((f) + 0.5F))
#define ROUND_DOWN(f) ((int)ceil((f) - 0.5F))

/* -------------------------------------------------------------------- */
/* Primitives */
Expand Down
Loading