From 88e50108e8bfa5adf2cfa614babbe1b95f8ca07c Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Mon, 23 Nov 2026 18:59:07 -0500 Subject: [PATCH 1/3] Clip the ModeFilter window to the image Only count pixels inside the image, rather than visiting every position in the window and skipping those outside it. Co-Authored-By: Claude Opus 5.5 --- Tests/test_image_filter.py | 5 +++++ src/libImaging/ModeFilter.c | 23 +++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index 8681bcfa5b4..c01181501ca 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -112,6 +112,11 @@ def test_modefilter( assert (mod, mod2) == expected +def test_modefilter_large_size() -> None: + im = Image.new("L", (3, 3), 128) + assert im.filter(ImageFilter.ModeFilter(2**31 - 1)).getpixel((1, 1)) == 128 + + @pytest.mark.parametrize( "mode, expected", ( diff --git a/src/libImaging/ModeFilter.c b/src/libImaging/ModeFilter.c index 757cbc3fb86..8eccc81cc30 100644 --- a/src/libImaging/ModeFilter.c +++ b/src/libImaging/ModeFilter.c @@ -17,9 +17,11 @@ Imaging ImagingModeFilter(Imaging im, int size) { + ImagingSectionCookie cookie; Imaging imOut; int x, y, i; int xx, yy; + int x0, x1, y0, y1; int maxcount; UINT8 maxpixel; int histogram[256]; @@ -35,9 +37,16 @@ ImagingModeFilter(Imaging im, int size) { size = size / 2; + ImagingSectionEnter(&cookie); + for (y = 0; y < imOut->ysize; y++) { UINT8 *out = &IMAGING_PIXEL_L(imOut, 0, y); + y0 = (size > y) ? 0 : y - size; + y1 = (size >= imOut->ysize - 1 - y) ? imOut->ysize - 1 : y + size; for (x = 0; x < imOut->xsize; x++) { + x0 = (size > x) ? 0 : x - size; + x1 = (size >= imOut->xsize - 1 - x) ? imOut->xsize - 1 : x + size; + /* calculate histogram over current area */ /* FIXME: brute force! to improve, update the histogram @@ -46,14 +55,10 @@ ImagingModeFilter(Imaging im, int size) { the added complexity... */ memset(histogram, 0, sizeof(histogram)); - for (yy = y - size; yy <= y + size; yy++) { - if (yy >= 0 && yy < imOut->ysize) { - UINT8 *in = &IMAGING_PIXEL_L(im, 0, yy); - for (xx = x - size; xx <= x + size; xx++) { - if (xx >= 0 && xx < imOut->xsize) { - histogram[in[xx]]++; - } - } + for (yy = y0; yy <= y1; yy++) { + UINT8 *in = &IMAGING_PIXEL_L(im, 0, yy); + for (xx = x0; xx <= x1; xx++) { + histogram[in[xx]]++; } } @@ -75,6 +80,8 @@ ImagingModeFilter(Imaging im, int size) { } } + ImagingSectionLeave(&cookie); + ImagingCopyPalette(imOut, im); return imOut; From d88844c5a815a0d058d9b65482714ea698e38eee Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 24 Nov 2026 11:17:00 +1100 Subject: [PATCH 2/3] Rearrange code --- src/libImaging/ModeFilter.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libImaging/ModeFilter.c b/src/libImaging/ModeFilter.c index 8eccc81cc30..9f603caf1ff 100644 --- a/src/libImaging/ModeFilter.c +++ b/src/libImaging/ModeFilter.c @@ -41,11 +41,11 @@ ImagingModeFilter(Imaging im, int size) { for (y = 0; y < imOut->ysize; y++) { UINT8 *out = &IMAGING_PIXEL_L(imOut, 0, y); - y0 = (size > y) ? 0 : y - size; - y1 = (size >= imOut->ysize - 1 - y) ? imOut->ysize - 1 : y + size; + y0 = size > y ? 0 : y - size; + y1 = size >= imOut->ysize - 1 - y ? imOut->ysize - 1 : y + size; for (x = 0; x < imOut->xsize; x++) { - x0 = (size > x) ? 0 : x - size; - x1 = (size >= imOut->xsize - 1 - x) ? imOut->xsize - 1 : x + size; + x0 = size > x ? 0 : x - size; + x1 = size >= imOut->xsize - 1 - x ? imOut->xsize - 1 : x + size; /* calculate histogram over current area */ From 9bf845ebef453c98ac44f48e0cb770ecf76ef0d7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 24 Nov 2026 11:18:18 +1100 Subject: [PATCH 3/3] Remove test that passes in main --- Tests/test_image_filter.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index c01181501ca..8681bcfa5b4 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -112,11 +112,6 @@ def test_modefilter( assert (mod, mod2) == expected -def test_modefilter_large_size() -> None: - im = Image.new("L", (3, 3), 128) - assert im.filter(ImageFilter.ModeFilter(2**31 - 1)).getpixel((1, 1)) == 128 - - @pytest.mark.parametrize( "mode, expected", (