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
19 changes: 19 additions & 0 deletions Tests/test_image_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^

Expand Down
104 changes: 102 additions & 2 deletions src/libImaging/Geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -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:
Expand Down