From 7743e6268d6cf4e19bd165ef7fa627bb0e7c21da Mon Sep 17 00:00:00 2001 From: Yay295 Date: Fri, 12 Apr 2024 19:08:31 -0500 Subject: [PATCH] get and set BGR pixels as BGR without scaling --- Tests/test_image_access.py | 4 ---- Tests/test_image_putdata.py | 2 +- Tests/test_lib_pack.py | 6 ++---- src/_imaging.c | 12 ++++++------ src/libImaging/Access.c | 12 ++++++------ 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index 2cd2e0c34..03c4c7c02 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -137,10 +137,6 @@ class TestImageGetPixel(AccessTest): 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 73145faac..a4babbcfe 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 f34ff7d02..a47b9e6e1 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -362,10 +362,8 @@ class TestLibUnpack: ) 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 9b521f552..1970c6de0 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 04618df09..596dd10ea 100644 --- a/src/libImaging/Access.c +++ b/src/libImaging/Access.c @@ -86,9 +86,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 @@ -96,9 +96,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