diff --git a/src/PIL/VtfImagePlugin.py b/src/PIL/VtfImagePlugin.py index 60086c0b571..f6f9ca46367 100644 --- a/src/PIL/VtfImagePlugin.py +++ b/src/PIL/VtfImagePlugin.py @@ -142,28 +142,30 @@ class VtfPF(IntEnum): 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,22 +264,21 @@ def _open(self): else: raise VTFException(f"Unsupported VTF pixel format: {pixel_format}") self.tile = [tile] - # fmt: on def _save(im, fp, filename): im: Image.Image 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)) + encoderinfo = im.encoderinfo + pixel_format = VtfPF(encoderinfo.get("pixel_format", VtfPF.RGBA8888)) + version = encoderinfo.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'))]) - - 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) + 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)) 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('mode, 'A') == NULL) + if (strchr(im->mode, 'A') == NULL) { no_alpha = 1; + } UINT32 block_count = (im->xsize * im->ysize) / 16; if (block_count * sizeof(bc1_color) > bytes) { state->errcode = IMAGING_CODEC_MEMORY; @@ -185,8 +189,9 @@ encode_bc1(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) { UINT16 c0 = 0, c1 = 0; pick_2_major_colors(unique_colors, color_frequency, unique_count, &c0, &c1); - if (c0 < c1 && no_alpha) + if (c0 < c1 && no_alpha) { SWAP(UINT16, c0, c1); + } UINT16 palette[4] = {c0, c1, 0, 0}; if (no_alpha) { @@ -203,10 +208,11 @@ encode_bc1(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) { UINT32 color_id; for (color_id = 0; color_id < 16; ++color_id) { UINT8 bc_color_id; - if (opaque[color_id] || no_alpha) + if (opaque[color_id] || no_alpha) { bc_color_id = get_closest_color_index(palette, all_colors[color_id]); - else + } else { bc_color_id = 3; + } SET_BITS(block->lut, color_id * 2, 2, bc_color_id); } }