Restored formatting

This commit is contained in:
Andrew Murray 2022-10-08 22:33:49 +11:00 committed by REDxEYE
parent eda4192618
commit b87d4e075e

View File

@ -277,7 +277,6 @@ class DdsImageFile(ImageFile.ImageFile):
format = "DDS"
format_description = "DirectDraw Surface"
# fmt: off
def _open(self):
if not _accept(self.fp.read(4)):
msg = "not a DDS file"
@ -316,10 +315,12 @@ class DdsImageFile(ImageFile.ImageFile):
self.tile = [("raw", extents, 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", extents, 0, (rawmode[::-1], 0, 1))]
else:
raise OSError(f"Unsupported bitcount {bitcount} for {pfflags}")
@ -409,13 +410,10 @@ class DdsImageFile(ImageFile.ImageFile):
msg = f"Unknown pixel format flags {repr(pfflags)}"
raise NotImplementedError(msg)
# fmt: on
def load_seek(self, pos):
pass
# fmt: off
def _save(im, fp, filename):
if im.mode not in ("RGB", "RGBA", "L", "LA"):
raise OSError(f"cannot write mode {im.mode} as DDS")
@ -430,7 +428,7 @@ def _save(im, fp, filename):
bit_count = 32
r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b))
elif im.mode == 'LA':
elif im.mode == "LA":
pixel_flags = DDPF.LUMINANCE | DDPF.ALPHAPIXELS
rgba_mask = struct.pack("<4I", 0x000000FF, 0x000000FF, 0x000000FF, 0x0000FF00)
bit_count = 16
@ -444,8 +442,16 @@ def _save(im, fp, filename):
stride = (im.width * bit_count + 7) // 8
fp.write(
o32(DDS_MAGIC)
# header size, flags, height, width, pitch, depth, mipmaps
+ struct.pack("<IIIIIII", 124, flags, im.height, im.width, stride, 0, 0, )
+ struct.pack(
"<IIIIIII",
124, # header size
flags, # flags
im.height,
im.width,
stride, # pitch
0, # depth
0, # mipmaps
)
+ struct.pack("11I", *((0,) * 11)) # reserved
# pfsize, pfflags, fourcc, bitcount
+ struct.pack("<IIII", 32, pixel_flags, 0, bit_count)
@ -456,9 +462,6 @@ def _save(im, fp, filename):
ImageFile._save(im, fp, [Image.Tile("raw", (0, 0) + im.size, 0, (mode, 0, 1))])
# fmt: on
def _accept(prefix):
return prefix[:4] == b"DDS "