Skip to content

Commit

Permalink
Invert alpha bit for map depth 16
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere authored and Yay295 committed Jun 25, 2024
1 parent f91a573 commit 4b7464d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
Binary file modified Tests/images/p_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions Tests/test_file_tga.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions src/PIL/TgaImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ def _open(self) -> None:
# 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(
Expand Down

0 comments on commit 4b7464d

Please sign in to comment.