From de2b4be9b951108ab3768ce7522ab6d1fba0836f Mon Sep 17 00:00:00 2001 From: REDxEYE Date: Sat, 6 Aug 2022 23:22:16 +0300 Subject: [PATCH] Add Tile namedtuple for code readability. Add typing info to Image.tile --- src/PIL/DdsImagePlugin.py | 34 +++++++++++++++++----------------- src/PIL/Image.py | 10 ++++++++++ src/PIL/ImageFile.py | 13 +++++++------ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index 5069fc233..d211cc994 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -343,24 +343,24 @@ class DdsImageFile(ImageFile.ImageFile): data_start = header_size + 4 if fourcc == D3DFMT.DXT1: self.pixel_format = "DXT1" - tile = ("bcn", (0, 0) + self.size, data_start, (1, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (1, self.pixel_format)) elif fourcc == D3DFMT.DXT3: self.pixel_format = "DXT3" - tile = ("bcn", (0, 0) + self.size, data_start, (2, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (2, self.pixel_format)) elif fourcc == D3DFMT.DXT5: self.pixel_format = "DXT5" - tile = ("bcn", (0, 0) + self.size, data_start, (3, self.pixel_format)) - elif fourcc == D3DFMT.BC5S: - self.pixel_format = "BC5S" - tile = ("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) - self.mode = "RGB" + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (3, self.pixel_format)) elif fourcc == D3DFMT.ATI1: self.pixel_format = "BC4" - tile = ("bcn", (0, 0) + self.size, data_start, (4, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (4, self.pixel_format)) self.mode = "L" + elif fourcc == D3DFMT.BC5S: + self.pixel_format = "BC5S" + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) + self.mode = "RGB" elif fourcc == D3DFMT.ATI2: self.pixel_format = "BC5" - tile = ("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) self.mode = "RGB" elif fourcc == D3DFMT.DX10: data_start += 20 @@ -369,33 +369,33 @@ class DdsImageFile(ImageFile.ImageFile): self.fp.read(16) if dxgi_format in (DXGI_FORMAT.BC5_TYPELESS, DXGI_FORMAT.BC5_UNORM): self.pixel_format = "BC5" - tile = ("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) self.mode = "RGB" elif dxgi_format == DXGI_FORMAT.BC5_SNORM: self.pixel_format = "BC5S" - tile = ("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (5, self.pixel_format)) self.mode = "RGB" elif dxgi_format == DXGI_FORMAT.BC6H_UF16: self.pixel_format = "BC6H" - tile = ("bcn", (0, 0) + self.size, data_start, (6, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (6, self.pixel_format)) self.mode = "RGB" elif dxgi_format == DXGI_FORMAT.BC6H_SF16: self.pixel_format = "BC6HS" - tile = ("bcn", (0, 0) + self.size, data_start, (6, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (6, self.pixel_format)) self.mode = "RGB" elif dxgi_format in (DXGI_FORMAT.BC7_TYPELESS, DXGI_FORMAT.BC7_UNORM): self.pixel_format = "BC7" - tile = ("bcn", (0, 0) + self.size, data_start, (7, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (7, self.pixel_format)) elif dxgi_format == DXGI_FORMAT.BC7_UNORM_SRGB: self.pixel_format = "BC7" self.info["gamma"] = 1 / 2.2 - tile = ("bcn", (0, 0) + self.size, data_start, (7, self.pixel_format)) + tile = Image.Tile("bcn", (0, 0) + self.size, data_start, (7, self.pixel_format)) elif dxgi_format in ( DXGI_FORMAT.R8G8B8A8_TYPELESS, DXGI_FORMAT.R8G8B8A8_UNORM, DXGI_FORMAT.R8G8B8A8_UNORM_SRGB, ): - tile = ("raw", (0, 0) + self.size, 0, ("RGBA", 0, 1)) + tile = Image.Tile("raw", (0, 0) + self.size, 0, ("RGBA", 0, 1)) if dxgi_format == DXGI_FORMAT.R8G8B8A8_UNORM_SRGB: self.info["gamma"] = 1 / 2.2 else: @@ -457,7 +457,7 @@ def _save(im, fp, filename): if im.mode == "RGBA": r, g, b, a = im.split() im = Image.merge("RGBA", (a, r, g, b)) - ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))]) + ImageFile._save(im, fp, [Image.Tile("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))]) def _accept(prefix): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a785292f8..15d368acf 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -38,6 +38,7 @@ import warnings from collections.abc import Callable, MutableMapping from enum import IntEnum from pathlib import Path +from typing import NamedTuple, Tuple, List try: import defusedxml.ElementTree as ElementTree @@ -206,6 +207,14 @@ if hasattr(core, "DEFAULT_STRATEGY"): RLE = core.RLE FIXED = core.FIXED +Tile = NamedTuple('Tile', + [ + ('encoder_name', str), + ('extents', Tuple[int, int, int, int]), + ('offset', int), + ('tile_args', Tuple) + ] + ) # -------------------------------------------------------------------- # Registries @@ -692,6 +701,7 @@ class Image: def __setstate__(self, state): Image.__init__(self) + self.tile: List[Tile] = [] info, mode, size, palette, data = state self.info = info self.mode = mode diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 8e4f7dfb2..d3ab1126c 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -31,6 +31,7 @@ import io import itertools import struct import sys +from typing import List from . import Image from ._util import is_path @@ -521,13 +522,13 @@ def _save(im, fp, tile, bufsize=0): fp.flush() -def _encode_tile(im, fp, tile, bufsize, fh, exc=None): - for e, b, o, a in tile: - if o > 0: - fp.seek(o) - encoder = Image._getencoder(im.mode, e, a, im.encoderconfig) +def _encode_tile(im, fp, tile: List[Image.Tile], bufsize, fh, exc=None): + for encoder_name, extents, offset, tile_args in tile: + if offset > 0: + fp.seek(offset) + encoder = Image._getencoder(im.mode, encoder_name, tile_args, im.encoderconfig) try: - encoder.setimage(im.im, b) + encoder.setimage(im.im, extents) if encoder.pushes_fd: encoder.setfd(fp) errcode = encoder.encode_to_pyfd()[1]