Invert alpha bit for map depth 16

This commit is contained in:
Andrew Murray 2024-06-25 20:50:17 +10:00 committed by Yay295
parent e5c6d883d4
commit 3840255486
3 changed files with 8 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 378 B

After

Width:  |  Height:  |  Size: 414 B

View File

@ -72,12 +72,13 @@ def test_palette_depth_8(tmp_path: Path) -> None:
def test_palette_depth_16(tmp_path: Path) -> None:
with Image.open("Tests/images/p_16.tga") as im:
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/p_16.png")
assert im.palette.mode == "RGBA"
assert_image_equal_tofile(im.convert("RGBA"), "Tests/images/p_16.png")
out = str(tmp_path / "temp.png")
im.save(out)
with Image.open(out) as reloaded:
assert_image_equal_tofile(reloaded.convert("RGB"), "Tests/images/p_16.png")
assert_image_equal_tofile(reloaded.convert("RGBA"), "Tests/images/p_16.png")
def test_id_field() -> None:

View File

@ -117,9 +117,11 @@ class TgaImageFile(ImageFile.ImageFile):
# read palette
start, size, mapdepth = i16(s, 3), i16(s, 5), s[7]
if mapdepth == 16:
self.palette = ImagePalette.raw(
"BGRA;15", b"\0" * 2 * start + self.fp.read(2 * size)
)
colormap = self.fp.read(2 * size)
palette_data = bytearray(b"\0" * 2 * start)
for a, b in zip(colormap[::2], colormap[1::2]):
palette_data += bytearray((a, b ^ 128))
self.palette = ImagePalette.raw("BGRA;15", bytes(palette_data))
self.palette.mode = "RGBA"
elif mapdepth == 24:
self.palette = ImagePalette.raw(