From 9165771d5e2a34148f13963e3545ec76f40aeb21 Mon Sep 17 00:00:00 2001 From: REDxEYE Date: Thu, 15 Jun 2023 03:08:04 +0300 Subject: [PATCH] Add BC5U support. Seen in Amnesia: The Bunker --- src/PIL/DdsImagePlugin.py | 11 +++++++++-- src/PIL/ImageFile.py | 3 +-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index 6e9e1cada..8c8662499 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -254,6 +254,7 @@ class D3DFMT(IntEnum): DXT5 = i32(b"DXT5") DX10 = i32(b"DX10") BC5S = i32(b"BC5S") + BC5U = i32(b"BC5U") ATI1 = i32(b"ATI1") ATI2 = i32(b"ATI2") MULTI2_ARGB8 = i32(b"MET1") @@ -325,7 +326,8 @@ class DdsImageFile(ImageFile.ImageFile): try: fourcc = D3DFMT(fourcc_) except ValueError: - raise NotImplementedError(f"Unimplemented pixel format {repr(fourcc_)}") + msg = f"Unimplemented pixel format {repr(fourcc_)}" + raise NotImplementedError(msg) if fourcc == D3DFMT.DXT1: self.mode = "RGBA" self.pixel_format = "DXT1" @@ -346,6 +348,10 @@ class DdsImageFile(ImageFile.ImageFile): self.mode = "RGB" self.pixel_format = "BC5S" tile = Image.Tile("bcn", extents, data_offs, (5, self.pixel_format)) + elif fourcc == D3DFMT.BC5U: + self.mode = "RGB" + self.pixel_format = "BC5U" + tile = Image.Tile("bcn", extents, data_offs, (5, self.pixel_format)) elif fourcc == D3DFMT.ATI2: self.mode = "RGB" self.pixel_format = "BC5" @@ -407,7 +413,8 @@ class DdsImageFile(ImageFile.ImageFile): def _save(im, fp, filename): if im.mode not in ("RGB", "RGBA", "L", "LA"): - raise OSError(f"cannot write mode {im.mode} as DDS") + msg = f"cannot write mode {im.mode} as DDS" + raise OSError(msg) pixel_flags = DDPF.RGB if im.mode == "RGB": diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 4194a7c35..716b2f06b 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -546,8 +546,7 @@ def _encode_tile(im, fp, tile: List[Image.Tile], bufsize, fh, exc=None): # slight speedup: compress to real file object errcode = encoder.encode_to_file(fh, bufsize) if errcode < 0: - msg = - f"encoder error {errcode} when writing image file" + msg = f"encoder error {errcode} when writing image file" raise OSError(msg) from exc finally: encoder.cleanup()