From f2eb18a299b08dcb8159b3e335e3c66b456cc152 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:02:40 +0000 Subject: [PATCH 1/3] Test converting F to I;16 modes --- Tests/test_mode_i16.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Tests/test_mode_i16.py b/Tests/test_mode_i16.py index d066a50589b..ee128e0c897 100644 --- a/Tests/test_mode_i16.py +++ b/Tests/test_mode_i16.py @@ -110,3 +110,12 @@ def test_convert_i_f(mode: str) -> None: im = Image.new("I", (1, 1), 4660) assert im.convert(mode).getpixel((0, 0)) == 4660 + + +@pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N")) +@pytest.mark.parametrize( + "value, expected", ((-1, 0), (4660.5, 4660), (65535, 65535), (70000, 65535)) +) +def test_convert_from_f(mode: str, value: float, expected: int) -> None: + im = Image.new("F", (1, 1), value) + assert im.convert(mode).getpixel((0, 0)) == expected From f7ea24be51450f94febc071eee1062b3c4d0c455 Mon Sep 17 00:00:00 2001 From: usmanmateen <74776275+usmanmateen@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:02:40 +0000 Subject: [PATCH 2/3] Convert F to I;16 modes without clipping to 255 There was no F to I;16, I;16L, I;16B or I;16N converter, so convert() fell back to converting through L, which clips to 0..255. The new converters clip to 0..65535 and truncate like the F to I and F to L converters. --- src/libImaging/Convert.c | 49 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 2c524002070..23fa387c0f8 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -788,6 +788,42 @@ I16B_F(UINT8 *out_, const UINT8 *in, int xsize) { } } +static void +F_I16L(UINT8 *out, const UINT8 *in_, int xsize) { + int x, v; + for (x = 0; x < xsize; x++, in_ += 4) { + FLOAT32 f; + memcpy(&f, in_, sizeof(f)); + if (f <= 0.0) { + v = 0; + } else if (f >= 65535.0) { + v = 65535; + } else { + v = (int)f; + } + *out++ = (UINT8)v; + *out++ = (UINT8)(v >> 8); + } +} + +static void +F_I16B(UINT8 *out, const UINT8 *in_, int xsize) { + int x, v; + for (x = 0; x < xsize; x++, in_ += 4) { + FLOAT32 f; + memcpy(&f, in_, sizeof(f)); + if (f <= 0.0) { + v = 0; + } else if (f >= 65535.0) { + v = 65535; + } else { + v = (int)f; + } + *out++ = (UINT8)(v >> 8); + *out++ = (UINT8)v; + } +} + static void L_I16L(UINT8 *out, const UINT8 *in, int xsize) { int x; @@ -1564,9 +1600,18 @@ static struct { {IMAGING_MODE_I_16L, IMAGING_MODE_F, I16L_F}, {IMAGING_MODE_I_16B, IMAGING_MODE_F, I16B_F}, #ifdef WORDS_BIGENDIAN - {IMAGING_MODE_I_16N, IMAGING_MODE_F, I16B_F} + {IMAGING_MODE_I_16N, IMAGING_MODE_F, I16B_F}, +#else + {IMAGING_MODE_I_16N, IMAGING_MODE_F, I16L_F}, +#endif + + {IMAGING_MODE_F, IMAGING_MODE_I_16, F_I16L}, + {IMAGING_MODE_F, IMAGING_MODE_I_16L, F_I16L}, + {IMAGING_MODE_F, IMAGING_MODE_I_16B, F_I16B}, +#ifdef WORDS_BIGENDIAN + {IMAGING_MODE_F, IMAGING_MODE_I_16N, F_I16B} #else - {IMAGING_MODE_I_16N, IMAGING_MODE_F, I16L_F} + {IMAGING_MODE_F, IMAGING_MODE_I_16N, F_I16L} #endif }; From a25724180a852c2bcfa1cd15b0d5cfab24566293 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 29 Oct 2026 12:28:36 +1100 Subject: [PATCH 3/3] Simplify code --- Tests/test_mode_i16.py | 10 ++++------ src/libImaging/Convert.c | 12 ++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Tests/test_mode_i16.py b/Tests/test_mode_i16.py index ee128e0c897..d5bd35a6c8a 100644 --- a/Tests/test_mode_i16.py +++ b/Tests/test_mode_i16.py @@ -103,7 +103,7 @@ def test_convert() -> None: @pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N")) -def test_convert_i_f(mode: str) -> None: +def test_convert_i16_f(mode: str) -> None: im = Image.new(mode, (1, 1), 4660) assert im.convert("I").getpixel((0, 0)) == 4660 assert im.convert("F").getpixel((0, 0)) == 4660.0 @@ -113,9 +113,7 @@ def test_convert_i_f(mode: str) -> None: @pytest.mark.parametrize("mode", ("I;16", "I;16L", "I;16B", "I;16N")) -@pytest.mark.parametrize( - "value, expected", ((-1, 0), (4660.5, 4660), (65535, 65535), (70000, 65535)) -) -def test_convert_from_f(mode: str, value: float, expected: int) -> None: +@pytest.mark.parametrize("value", (-1, 4660.4, 65535, 70000)) +def test_convert_f_i16(mode: str, value: float) -> None: im = Image.new("F", (1, 1), value) - assert im.convert(mode).getpixel((0, 0)) == expected + assert im.convert(mode).getpixel((0, 0)) == min(max(0, int(value)), 65535) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 23fa387c0f8..a3c60c97796 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -789,11 +789,11 @@ I16B_F(UINT8 *out_, const UINT8 *in, int xsize) { } static void -F_I16L(UINT8 *out, const UINT8 *in_, int xsize) { +F_I16L(UINT8 *out, const UINT8 *in, int xsize) { int x, v; - for (x = 0; x < xsize; x++, in_ += 4) { + for (x = 0; x < xsize; x++, in += 4) { FLOAT32 f; - memcpy(&f, in_, sizeof(f)); + memcpy(&f, in, sizeof(f)); if (f <= 0.0) { v = 0; } else if (f >= 65535.0) { @@ -807,11 +807,11 @@ F_I16L(UINT8 *out, const UINT8 *in_, int xsize) { } static void -F_I16B(UINT8 *out, const UINT8 *in_, int xsize) { +F_I16B(UINT8 *out, const UINT8 *in, int xsize) { int x, v; - for (x = 0; x < xsize; x++, in_ += 4) { + for (x = 0; x < xsize; x++, in += 4) { FLOAT32 f; - memcpy(&f, in_, sizeof(f)); + memcpy(&f, in, sizeof(f)); if (f <= 0.0) { v = 0; } else if (f >= 65535.0) {