diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 8c42da57a37..ec5b6988e83 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -137,10 +137,6 @@ def color(mode: str) -> int | tuple[int, ...]: bands = Image.getmodebands(mode) if bands == 1: return 1 - if mode in ("BGR;15", "BGR;16"): - # These modes have less than 8 bits per band - # So (1, 2, 3) cannot be roundtripped - return (16, 32, 49) return tuple(range(1, bands + 1)) def check(self, mode: str, expected_color_int: int | None = None) -> None: diff --git a/Tests/test_image_putdata.py b/Tests/test_image_putdata.py index 73145faac15..a4babbcfe62 100644 --- a/Tests/test_image_putdata.py +++ b/Tests/test_image_putdata.py @@ -80,7 +80,7 @@ def test_mode_F() -> None: @pytest.mark.parametrize("mode", ("BGR;15", "BGR;16", "BGR;24")) def test_mode_BGR(mode: str) -> None: - data = [(16, 32, 49), (32, 32, 98)] + data = [(1, 2, 3), (10, 11, 12)] im = Image.new(mode, (1, 2)) im.putdata(data) diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index 6a0e704b89a..47fb88610e8 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -359,10 +359,8 @@ def test_RGB(self) -> None: ) def test_BGR(self) -> None: - self.assert_unpack("BGR;15", "BGR;15", 3, (8, 131, 0), (24, 0, 8), (41, 131, 8)) - self.assert_unpack( - "BGR;16", "BGR;16", 3, (8, 64, 0), (24, 129, 0), (41, 194, 0) - ) + self.assert_unpack("BGR;15", "BGR;15", 3, (0, 16, 1), (1, 0, 3), (1, 16, 5)) + self.assert_unpack("BGR;16", "BGR;16", 3, (0, 16, 1), (0, 32, 3), (0, 48, 5)) self.assert_unpack("BGR;24", "BGR;24", 3, (1, 2, 3), (4, 5, 6), (7, 8, 9)) def test_RGBA(self) -> None: diff --git a/src/_imaging.c b/src/_imaging.c index 520e5079346..714321b7965 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -605,18 +605,18 @@ getink(PyObject *color, Imaging im, char *ink) { return NULL; } if (!strcmp(im->mode, "BGR;15")) { - UINT16 v = ((((UINT16)r) << 7) & 0x7c00) + - ((((UINT16)g) << 2) & 0x03e0) + - ((((UINT16)b) >> 3) & 0x001f); + UINT16 v = ((((UINT16)b) << 10) & 0x7c00) + + ((((UINT16)g) << 5) & 0x03e0) + + ((((UINT16)r) >> 0) & 0x001f); ink[0] = (UINT8)v; ink[1] = (UINT8)(v >> 8); ink[2] = ink[3] = 0; return ink; } else if (!strcmp(im->mode, "BGR;16")) { - UINT16 v = ((((UINT16)r) << 8) & 0xf800) + - ((((UINT16)g) << 3) & 0x07e0) + - ((((UINT16)b) >> 3) & 0x001f); + UINT16 v = ((((UINT16)b) << 11) & 0xf800) + + ((((UINT16)g) << 5) & 0x07e0) + + ((((UINT16)r) >> 0) & 0x001f); ink[0] = (UINT8)v; ink[1] = (UINT8)(v >> 8); ink[2] = ink[3] = 0; diff --git a/src/libImaging/Access.c b/src/libImaging/Access.c index 091c84e18fa..8297195b573 100644 --- a/src/libImaging/Access.c +++ b/src/libImaging/Access.c @@ -92,9 +92,9 @@ get_pixel_BGR15(Imaging im, int x, int y, void *color) { UINT8 *in = (UINT8 *)&im->image8[y][x * 2]; UINT16 pixel = in[0] + (in[1] << 8); char *out = color; - out[0] = (pixel & 31) * 255 / 31; - out[1] = ((pixel >> 5) & 31) * 255 / 31; - out[2] = ((pixel >> 10) & 31) * 255 / 31; + out[0] = (pixel >> 10) & 31; + out[1] = (pixel >> 5) & 31; + out[2] = pixel & 31; } static void @@ -102,9 +102,9 @@ get_pixel_BGR16(Imaging im, int x, int y, void *color) { UINT8 *in = (UINT8 *)&im->image8[y][x * 2]; UINT16 pixel = in[0] + (in[1] << 8); char *out = color; - out[0] = (pixel & 31) * 255 / 31; - out[1] = ((pixel >> 5) & 63) * 255 / 63; - out[2] = ((pixel >> 11) & 31) * 255 / 31; + out[0] = (pixel >> 11) & 31; + out[1] = (pixel >> 5) & 63; + out[2] = pixel & 31; } static void