From 83ae7b2f653180ec047489bc8dd606c059177e28 Mon Sep 17 00:00:00 2001 From: W <184799977+waw666waw666@users.noreply.github.com> Date: Sun, 1 Nov 2026 17:53:09 +0800 Subject: [PATCH] Use 16-bit filters when transforming I;16 images with BILINEAR or BICUBIC --- Tests/test_image_transform.py | 19 +++++++ docs/releasenotes/13.1.0.rst | 8 +++ src/libImaging/Geometry.c | 104 +++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 2 deletions(-) diff --git a/Tests/test_image_transform.py b/Tests/test_image_transform.py index 576310b7fd2..ae3fcd12319 100644 --- a/Tests/test_image_transform.py +++ b/Tests/test_image_transform.py @@ -222,6 +222,25 @@ def op(im: Image.Image, sz: tuple[int, int]) -> Image.Image: self._test_nearest(op, mode) + @pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N")) + @pytest.mark.parametrize( + "resample", (Image.Resampling.BILINEAR, Image.Resampling.BICUBIC) + ) + def test_i16(self, mode: str, resample: Image.Resampling) -> None: + # Values above 255, so both bytes of each pixel matter + im = Image.new("I", (8, 8)) + im.putdata([i * 1000 for i in range(64)]) + + def op(im: Image.Image) -> Image.Image: + return im.transform( + (8, 8), + Image.Transform.AFFINE, + (0.9, 0.2, 0.3, -0.1, 1.1, 0.4), + resample, + ) + + assert_image_equal(op(im.convert(mode)), op(im).convert(mode)) + def test_blank_fill(self) -> None: # attempting to hit # https://github.com/python-pillow/Pillow/issues/254 reported diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 7782570bba8..029d4775d7f 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -66,6 +66,14 @@ On little-endian systems, :py:meth:`~PIL.Image.Image.resize` read and wrote I;16B and I;16N pixel values in the wrong byte order when using a resampling filter other than ``Resampling.NEAREST``. This has been fixed. +Transforming I;16 images +^^^^^^^^^^^^^^^^^^^^^^^^ + +:py:meth:`~PIL.Image.Image.transform` and :py:meth:`~PIL.Image.Image.rotate` +treated each byte of an I;16, I;16L, I;16B or I;16N pixel as a separate 8-bit +pixel when using ``Resampling.BILINEAR`` or ``Resampling.BICUBIC``, returning +corrupted images. This has been fixed. + Blurring I;16 images ^^^^^^^^^^^^^^^^^^^^ diff --git a/src/libImaging/Geometry.c b/src/libImaging/Geometry.c index 72276b0182c..d11e9996d40 100644 --- a/src/libImaging/Geometry.c +++ b/src/libImaging/Geometry.c @@ -477,6 +477,58 @@ bilinear_filter8(void *out, Imaging im, double xin, double yin) { return 1; } +/* I;16 modes keep each pixel in two bytes, in the byte order of the mode */ +static int +is_bigendian16(Imaging im) { + return im->mode == IMAGING_MODE_I_16B +#ifdef WORDS_BIGENDIAN + || im->mode == IMAGING_MODE_I_16N +#endif + ; +} + +#define GET16(in, x) \ + (bigendian ? ((in)[(x) * 2] << 8) + (in)[(x) * 2 + 1] \ + : (in)[(x) * 2] + ((in)[(x) * 2 + 1] << 8)) + +static void +put16(void *out, double v, int bigendian) { + int k; + if (v <= 0.0) { + k = 0; + } else if (v >= 65535.0) { + k = 65535; + } else { + k = (int)v; + } + if (bigendian) { + ((UINT8 *)out)[0] = (UINT8)(k >> 8); + ((UINT8 *)out)[1] = (UINT8)k; + } else { + ((UINT8 *)out)[0] = (UINT8)k; + ((UINT8 *)out)[1] = (UINT8)(k >> 8); + } +} + +static int +bilinear_filter16(void *out, Imaging im, double xin, double yin) { + int bigendian = is_bigendian16(im); + BILINEAR_HEAD(UINT8); + in = (UINT8 *)im->image8[YCLIP(y)]; + x0 = XCLIP(x + 0); + x1 = XCLIP(x + 1); + BILINEAR(v1, GET16(in, x0), GET16(in, x1), dx); + if (y + 1 >= 0 && y + 1 < ysize) { + in = (UINT8 *)im->image8[y + 1]; + BILINEAR(v2, GET16(in, x0), GET16(in, x1), dx); + } else { + v2 = v1; + } + BILINEAR(v1, v1, v2, dy); + put16(out, v1, bigendian); + return 1; +} + static int bilinear_filter32I(void *out, Imaging im, double xin, double yin) { INT32 k; @@ -596,6 +648,43 @@ bicubic_filter8(void *out, Imaging im, double xin, double yin) { return 1; } +#define BICUBIC_ROW16(v, row) \ + { \ + in = (UINT8 *)im->image8[row]; \ + BICUBIC(v, GET16(in, x0), GET16(in, x1), GET16(in, x2), GET16(in, x3), dx); \ + } + +static int +bicubic_filter16(void *out, Imaging im, double xin, double yin) { + int bigendian = is_bigendian16(im); + BICUBIC_HEAD(UINT8); + x0 = XCLIP(x + 0); + x1 = XCLIP(x + 1); + x2 = XCLIP(x + 2); + x3 = XCLIP(x + 3); + BICUBIC_ROW16(v1, YCLIP(y)); + if (y + 1 >= 0 && y + 1 < ysize) { + BICUBIC_ROW16(v2, y + 1); + } else { + v2 = v1; + } + if (y + 2 >= 0 && y + 2 < ysize) { + BICUBIC_ROW16(v3, y + 2); + } else { + v3 = v2; + } + if (y + 3 >= 0 && y + 3 < ysize) { + BICUBIC_ROW16(v4, y + 3); + } else { + v4 = v3; + } + BICUBIC(v1, v1, v2, v3, v4, dy); + put16(out, v1, bigendian); + return 1; +} + +#undef BICUBIC_ROW16 + static int bicubic_filter32I(void *out, Imaging im, double xin, double yin) { INT32 k; @@ -664,6 +753,7 @@ bicubic_filter32RGB(void *out, Imaging im, double xin, double yin) { #undef BICUBIC #undef BICUBIC_HEAD #undef BICUBIC_BODY +#undef GET16 static ImagingTransformFilter getfilter(Imaging im, ImagingResamplingType filterid) { @@ -682,7 +772,12 @@ getfilter(Imaging im, ImagingResamplingType filterid) { break; case IMAGING_TRANSFORM_BILINEAR: if (im->image8) { - return bilinear_filter8; + switch (im->type) { + case IMAGING_TYPE_UINT8: + return bilinear_filter8; + case IMAGING_TYPE_I16: + return bilinear_filter16; + } } else if (im->image32) { switch (im->type) { case IMAGING_TYPE_UINT8: @@ -700,7 +795,12 @@ getfilter(Imaging im, ImagingResamplingType filterid) { break; case IMAGING_TRANSFORM_BICUBIC: if (im->image8) { - return bicubic_filter8; + switch (im->type) { + case IMAGING_TYPE_UINT8: + return bicubic_filter8; + case IMAGING_TYPE_I16: + return bicubic_filter16; + } } else if (im->image32) { switch (im->type) { case IMAGING_TYPE_UINT8: