From 63987b7abaf6907a8985b867be154debfef0ec1b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 14 Feb 2024 06:55:13 +1100 Subject: [PATCH 1/2] Set mode to L if palette is missing --- Tests/test_file_tga.py | 5 +++++ src/PIL/TgaImagePlugin.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index bd8e522c7b5..87a59ff3d7a 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -133,6 +133,11 @@ def test_small_palette(tmp_path: Path) -> None: assert reloaded.getpalette() == colors +def test_missing_palette() -> None: + with Image.open("Tests/images/dilation4.lut") as im: + assert im.mode == "L" + + def test_save_wrong_mode(tmp_path: Path) -> None: im = hopper("PA") out = str(tmp_path / "temp.tga") diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 584932d2c7d..5d48275251e 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -85,7 +85,7 @@ def _open(self) -> None: elif depth == 16: self._mode = "LA" elif imagetype in (1, 9): - self._mode = "P" + self._mode = "P" if colormaptype else "L" elif imagetype in (2, 10): self._mode = "RGB" if depth == 32: From 818500b329555969cdb852c81d667cc70faaed94 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 14 Feb 2024 07:10:44 +1100 Subject: [PATCH 2/2] Raise an error if map depth is unknown --- Tests/images/p_8.tga | Bin 0 -> 18 bytes Tests/test_file_tga.py | 7 ++++++- src/PIL/TgaImagePlugin.py | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Tests/images/p_8.tga diff --git a/Tests/images/p_8.tga b/Tests/images/p_8.tga new file mode 100644 index 0000000000000000000000000000000000000000..73759a2822419fd38134be80acf7918c0a437e43 GIT binary patch literal 18 Wcmb1RWMp7qVB`Q23=KfY!2kdU!vQh? literal 0 HcmV?d00001 diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index 87a59ff3d7a..75c592da26b 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -7,7 +7,7 @@ import pytest -from PIL import Image +from PIL import Image, UnidentifiedImageError from .helper import assert_image_equal, assert_image_equal_tofile, hopper @@ -65,6 +65,11 @@ def roundtrip(original_im) -> None: roundtrip(original_im) +def test_palette_depth_8(tmp_path: Path) -> None: + with pytest.raises(UnidentifiedImageError): + Image.open("Tests/images/p_8.tga") + + 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") diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 5d48275251e..b6748c25ed2 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -128,6 +128,9 @@ def _open(self) -> None: self.palette = ImagePalette.raw( "BGRA", b"\0" * 4 * start + self.fp.read(4 * size) ) + else: + msg = "unknown TGA map depth" + raise SyntaxError(msg) # setup tile descriptor try: