diff --git a/src/PIL/VtfImagePlugin.py b/src/PIL/VtfImagePlugin.py index 60086c0b5..d6e9aae97 100644 --- a/src/PIL/VtfImagePlugin.py +++ b/src/PIL/VtfImagePlugin.py @@ -142,22 +142,24 @@ LA_FORMATS = ( VtfPF.UV88, ) -BLOCK_COMPRESSED = ( - VtfPF.DXT1, - VtfPF.DXT1_ONEBITALPHA, - VtfPF.DXT3, - VtfPF.DXT5) +BLOCK_COMPRESSED = (VtfPF.DXT1, VtfPF.DXT1_ONEBITALPHA, VtfPF.DXT3, VtfPF.DXT5) SUPPORTED_FORMATS = RGBA_FORMATS + RGB_FORMATS + LA_FORMATS + L_FORMATS -HEADER_V70 = '> mip_id, min_res) mip_height = max(header.height >> mip_id, min_res) @@ -256,7 +264,6 @@ class VtfImageFile(ImageFile.ImageFile): else: raise VTFException(f"Unsupported VTF pixel format: {pixel_format}") self.tile = [tile] - # fmt: on def _save(im, fp, filename): @@ -264,14 +271,14 @@ def _save(im, fp, filename): if im.mode not in ("RGB", "RGBA"): raise OSError(f"cannot write mode {im.mode} as VTF") arguments = im.encoderinfo - pixel_format = VtfPF(arguments.get('pixel_format', VtfPF.RGBA8888)) - version = arguments.get('version', (7, 4)) + pixel_format = VtfPF(arguments.get("pixel_format", VtfPF.RGBA8888)) + version = arguments.get("version", (7, 4)) flags = CompiledVtfFlags(0) - if 'A' in im.mode: + if "A" in im.mode: if pixel_format == VtfPF.DXT1_ONEBITALPHA: flags |= CompiledVtfFlags.ONEBITALPHA elif pixel_format == VtfPF.DXT1: - im = im.convert('RGB') + im = im.convert("RGB") else: flags |= CompiledVtfFlags.EIGHTBITALPHA @@ -281,21 +288,32 @@ def _save(im, fp, filename): mipmap_count = _get_mipmap_count(width, height) thumb_buffer = BytesIO() - thumb = im.convert('RGB') + thumb = im.convert("RGB") thumb.thumbnail(((min(16, width)), (min(16, height)))) thumb = thumb.resize((closest_power(thumb.width), closest_power(thumb.height))) - ImageFile._save(thumb, thumb_buffer, [("bcn", (0, 0) + thumb.size, 0, (1, 'DXT1'))]) + ImageFile._save(thumb, thumb_buffer, [("bcn", (0, 0) + thumb.size, 0, (1, "DXT1"))]) - header = VTFHeader(0, width, height, flags, - 1, 0, 1.0, 1.0, 1.0, - 1.0, pixel_format, mipmap_count, VtfPF.DXT1, - thumb.width, thumb.height, - 1, 2) - - fp.write( - b"VTF\x00" - + struct.pack('<2I', *version) + header = VTFHeader( + 0, + width, + height, + flags, + 1, + 0, + 1.0, + 1.0, + 1.0, + 1.0, + pixel_format, + mipmap_count, + VtfPF.DXT1, + thumb.width, + thumb.height, + 1, + 2, ) + + fp.write(b"VTF\x00" + struct.pack("<2I", *version)) if version < (7, 2): size = struct.calcsize(HEADER_V70) + 12 header = header._replace(header_size=size + (16 - size % 16)) @@ -309,15 +327,15 @@ def _save(im, fp, filename): header = header._replace(header_size=size + (16 - size % 16)) fp.write(struct.pack(HEADER_V73, *header)) else: - raise VTFException(f'Unsupported version {version}') + raise VTFException(f"Unsupported version {version}") if version > (7, 2): - fp.write(b'\x01\x00\x00\x00') - fp.write(struct.pack('