diff --git a/Tests/images/unimplemented_fourcc.dds b/Tests/images/unimplemented_fourcc.dds new file mode 100755 index 000000000..479557364 Binary files /dev/null and b/Tests/images/unimplemented_fourcc.dds differ diff --git a/Tests/images/unimplemented_pfflags.dds b/Tests/images/unimplemented_pfflags.dds new file mode 100755 index 000000000..e3fc8344d Binary files /dev/null and b/Tests/images/unimplemented_pfflags.dds differ diff --git a/Tests/images/unimplemented_pixel_format.dds b/Tests/images/unknown_fourcc.dds similarity index 100% rename from Tests/images/unimplemented_pixel_format.dds rename to Tests/images/unknown_fourcc.dds diff --git a/Tests/test_file_dds.py b/Tests/test_file_dds.py index 98d03e3ca..004a2d156 100644 --- a/Tests/test_file_dds.py +++ b/Tests/test_file_dds.py @@ -289,9 +289,18 @@ def test_dxt5_colorblock_alpha_issue_4142(): assert px[2] != 0 -def test_unimplemented_pixel_format(): +@pytest.mark.parametrize( + "test_file", + ( + "Tests/images/unknown_fourcc.dds", + "Tests/images/unimplemented_fourcc.dds", + "Tests/images/unimplemented_dxgi_format.dds", + "Tests/images/unimplemented_pfflags.dds", + ), +) +def test_not_implemented(test_file): with pytest.raises(NotImplementedError): - with Image.open("Tests/images/unimplemented_pixel_format.dds"): + with Image.open(test_file): pass diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index 69bfb8f2a..6e9e1cada 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -184,11 +184,6 @@ class DXGI_FORMAT(IntEnum): V408 = 132 SAMPLER_FEEDBACK_MIN_MIP_OPAQUE = 133 SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE = 134 - INVALID = -1 - - @classmethod - def _missing_(cls, value: object): - return cls.INVALID class D3DFMT(IntEnum): @@ -262,11 +257,6 @@ class D3DFMT(IntEnum): ATI1 = i32(b"ATI1") ATI2 = i32(b"ATI2") MULTI2_ARGB8 = i32(b"MET1") - INVALID = -1 - - @classmethod - def _missing_(cls, value: object): - return cls.INVALID class DdsImageFile(ImageFile.ImageFile): @@ -297,7 +287,6 @@ class DdsImageFile(ImageFile.ImageFile): # pixel format pfsize, pfflags_, fourcc_, bitcount = struct.unpack("<4I", header.read(16)) pfflags = DDPF(pfflags_) - fourcc = D3DFMT(fourcc_) masks = struct.unpack("<4I", header.read(16)) if flags & DDSD.CAPS: header.seek(20, io.SEEK_CUR) @@ -333,6 +322,10 @@ class DdsImageFile(ImageFile.ImageFile): raise OSError(msg) elif pfflags & DDPF.FOURCC: data_offs = header_size + 4 + try: + fourcc = D3DFMT(fourcc_) + except ValueError: + raise NotImplementedError(f"Unimplemented pixel format {repr(fourcc_)}") if fourcc == D3DFMT.DXT1: self.mode = "RGBA" self.pixel_format = "DXT1"