Skip to content
Open
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_image_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,41 @@ def test_box_filter_correct_range(self) -> None:
ref = Image.new("RGB", (100, 100), "#1688ff")
assert_image_equal(im, ref)

def test_box_downscale_includes_boundary_pixels(self) -> None:
# 13→6 places source x=6 on a bin edge; old range rounding dropped it.
# See #9939 (2755→688 dropped the central column for the same reason).
im = Image.new("L", (13, 1), 255)
im.putpixel((6, 0), 0)
out = im.resize((6, 1), Image.Resampling.BOX)
assert out.get_flattened_data() == (255, 255, 170, 255, 255, 255)

im = Image.new("L", (1, 13), 255)
im.putpixel((0, 6), 0)
out = im.resize((1, 6), Image.Resampling.BOX)
assert out.get_flattened_data() == (255, 255, 170, 255, 255, 255)

im = Image.new("L", (2755, 1), 255)
im.putpixel((1377, 0), 0)
out = im.resize((688, 1), Image.Resampling.BOX)
assert not all(value == 255 for value in out.get_flattened_data())

im = Image.new("L", (1, 1837), 255)
im.putpixel((0, 918), 0)
out = im.resize((1, 306), Image.Resampling.BOX)
assert not all(value == 255 for value in out.get_flattened_data())

def test_box_downscale_counts_each_pixel_once(self) -> None:
# 15→14 used to count the central source pixel in two outputs.
im = Image.new("L", (15, 1))
im.putpixel((7, 0), 255)
out = im.resize((14, 1), Image.Resampling.BOX)
assert [value for value in out.get_flattened_data() if value] == [128]

im = Image.new("L", (1, 15))
im.putpixel((0, 7), 255)
out = im.resize((1, 14), Image.Resampling.BOX)
assert [value for value in out.get_flattened_data() if value] == [128]


class TestCoreResampleConsistency:
def make_case(
Expand Down
9 changes: 9 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ TODO
Other changes
=============

BOX downscale includes boundary source pixels
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:data:`PIL.Image.Resampling.BOX` downscaling could skip a source pixel whose center
landed on an output bin edge, due to range rounding and the discontinuous box kernel.
Those pixels are now assigned to exactly one output pixel, matching the documented
behavior that "Each pixel of the source image contributes to one pixel of the
destination image with identical weights"

Writing to read-only images
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
35 changes: 30 additions & 5 deletions src/libImaging/Resample.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ precompute_coeffs(
/* maximum number of coeffs */
ksize = (int)ceil(support) * 2 + 1;

/* For BOX, each source pixel contributes to one output pixel with equal weights.
When downscaling, (int)(center ± support + 0.5) and the discontinuous box kernel
can both miss a pixel whose center lands on a bin edge (issue #9939).
Tile bins as (left, right] instead. */
int box_downscale = filterp == &BOX && scale > 1.0;
if (box_downscale) {
/* One extra source pixel per output when a bin edge hits a center. */
int needed = (int)scale + 2;
if (needed > ksize) {
ksize = needed;
}
}

// check for overflow
if (outSize > INT_MAX / (ksize * (int)sizeof(double))) {
ImagingError_MemoryError();
Expand All @@ -276,20 +289,32 @@ precompute_coeffs(
for (xx = 0; xx < outSize; xx++) {
double center = in0 + (xx + 0.5) * scale;
double ww = 0.0;
// Round the value
xmin = (int)(center - support + 0.5);
if (box_downscale) {
double left = in0 + (double)xx * scale;
double right = in0 + (double)(xx + 1) * scale;
xmin = (int)floor(left - 0.5) + 1;
xmax = (int)floor(right - 0.5) + 1;
} else {
// Round the value
xmin = (int)(center - support + 0.5);
// Round the value
xmax = (int)(center + support + 0.5);
}
if (xmin < 0) {
xmin = 0;
}
// Round the value
xmax = (int)(center + support + 0.5);
if (xmax > inSize) {
xmax = inSize;
}
xmax -= xmin;
k = &kk[xx * ksize];
for (x = 0; x < xmax; x++) {
double w = filterp->filter((x + xmin - center + 0.5) * inv_filterscale);
double w;
if (box_downscale) {
w = 1.0;
} else {
w = filterp->filter((x + xmin - center + 0.5) * inv_filterscale);
}
k[x] = w;
ww += w;
}
Expand Down
Loading