Skip to content

Commit 981459d

Browse files
committed
Fix high byte of I;16* images being truncated by ImageFilter
Follows up on #8438 (418ae7c)
1 parent b39b964 commit 981459d

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Tests/test_image_filter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ def test_consistency_5x5(mode: str) -> None:
210210
assert_image_equal(source.filter(kernel), reference)
211211

212212

213+
@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N"))
214+
def test_consistency_i16_high_byte(mode: str) -> None:
215+
# Exercise filters with a 16bpc image that has content in the high byte, too.
216+
im = Image.new(mode, (8, 8), 1000)
217+
pixel = im.filter(ImageFilter.SMOOTH).getpixel((4, 4))
218+
assert isinstance(pixel, (int, float))
219+
# Allow for kernel rounding errors.
220+
assert abs(pixel - 1000) <= 1
221+
222+
213223
@pytest.mark.parametrize(
214224
"radius",
215225
(

src/libImaging/Filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ kernel_i16(int size, UINT8 *in0, int x, const float *kernel, int bigendian) {
114114
for (i = 0; i < size; i++) {
115115
int x1 = x + i - half_size;
116116
result += (float)(in0[x1 * 2 + (bigendian ? 1 : 0)] +
117-
(in0[x1 * 2 + (bigendian ? 0 : 1)] >> 8)) *
117+
(in0[x1 * 2 + (bigendian ? 0 : 1)] << 8)) *
118118
kernel[i];
119119
}
120120
return result;

0 commit comments

Comments
 (0)