From 7c25e0bbdae1dcd36507423f128b60f259d0a977 Mon Sep 17 00:00:00 2001 From: REDxEYE Date: Wed, 24 Aug 2022 21:18:00 +0300 Subject: [PATCH] Small refactor --- src/PIL/DdsImagePlugin.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index 4c228bca1..2744045b1 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -308,17 +308,9 @@ class DdsImageFile(ImageFile.ImageFile): (caps1_, caps2_, caps3, caps4, _,) = struct.unpack("<5I", header.read(20)) else: (caps1_, caps2_, caps3, caps4, _,) = (0, 0, 0, 0, 0,) - caps1 = DDSCAPS(caps1_) - caps2 = DDSCAPS2(caps2_) - if pfflags & DDPF.LUMINANCE: - # Texture contains uncompressed L or LA data - if pfflags & DDPF.ALPHAPIXELS: - self.mode = "LA" - else: - self.mode = "L" - - self.tile = [("raw", (0, 0) + self.size, 0, (self.mode, 0, 1))] - elif pfflags & DDPF.RGB: + _ = DDSCAPS(caps1_) + _ = DDSCAPS2(caps2_) + if pfflags & DDPF.RGB: # Texture contains uncompressed RGB data masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)} if bitcount == 24: @@ -327,7 +319,10 @@ class DdsImageFile(ImageFile.ImageFile): self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))] elif bitcount == 32 and pfflags & DDPF.ALPHAPIXELS: self.mode = "RGBA" - rawmode = (masks[0xFF000000] + masks[0x00FF0000] + masks[0x0000FF00] + masks[0x000000FF]) + rawmode = (masks[0xFF000000] + + masks[0x00FF0000] + + masks[0x0000FF00] + + masks[0x000000FF]) self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))] else: raise OSError(f"Unsupported bitcount {bitcount} for {pfflags} DDS texture") @@ -449,12 +444,14 @@ def _save(im, fp, filename): flags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PITCH | DDSD.PIXELFORMAT + stride = (im.width * bit_count + 7) // 8 fp.write( o32(DDS_MAGIC) # header size, flags, height, width, pith, depth, mipmaps - + struct.pack("