Skip to content

Commit

Permalink
get and set BGR pixels as BGR without scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Yay295 committed Apr 13, 2024
1 parent de18f55 commit 1ed6419
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
4 changes: 0 additions & 4 deletions Tests/test_image_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_putdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 2 additions & 4 deletions Tests/test_lib_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/libImaging/Access.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ 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
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
Expand Down

0 comments on commit 1ed6419

Please sign in to comment.