Add Tile namedtuple for code readability.

Add typing info to Image.tile
This commit is contained in:
REDxEYE 2022-08-06 23:22:16 +03:00
parent f2d91fcdaa
commit de2b4be9b9
3 changed files with 34 additions and 23 deletions

View File

@ -343,24 +343,24 @@ class DdsImageFile(ImageFile.ImageFile):
data_start = header_size + 4 data_start = header_size + 4
if fourcc == D3DFMT.DXT1: if fourcc == D3DFMT.DXT1:
self.pixel_format = "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: elif fourcc == D3DFMT.DXT3:
self.pixel_format = "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: elif fourcc == D3DFMT.DXT5:
self.pixel_format = "DXT5" self.pixel_format = "DXT5"
tile = ("bcn", (0, 0) + self.size, data_start, (3, self.pixel_format)) tile = Image.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"
elif fourcc == D3DFMT.ATI1: elif fourcc == D3DFMT.ATI1:
self.pixel_format = "BC4" 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" 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: elif fourcc == D3DFMT.ATI2:
self.pixel_format = "BC5" 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" self.mode = "RGB"
elif fourcc == D3DFMT.DX10: elif fourcc == D3DFMT.DX10:
data_start += 20 data_start += 20
@ -369,33 +369,33 @@ class DdsImageFile(ImageFile.ImageFile):
self.fp.read(16) self.fp.read(16)
if dxgi_format in (DXGI_FORMAT.BC5_TYPELESS, DXGI_FORMAT.BC5_UNORM): if dxgi_format in (DXGI_FORMAT.BC5_TYPELESS, DXGI_FORMAT.BC5_UNORM):
self.pixel_format = "BC5" 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" self.mode = "RGB"
elif dxgi_format == DXGI_FORMAT.BC5_SNORM: elif dxgi_format == DXGI_FORMAT.BC5_SNORM:
self.pixel_format = "BC5S" 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" self.mode = "RGB"
elif dxgi_format == DXGI_FORMAT.BC6H_UF16: elif dxgi_format == DXGI_FORMAT.BC6H_UF16:
self.pixel_format = "BC6H" 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" self.mode = "RGB"
elif dxgi_format == DXGI_FORMAT.BC6H_SF16: elif dxgi_format == DXGI_FORMAT.BC6H_SF16:
self.pixel_format = "BC6HS" 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" self.mode = "RGB"
elif dxgi_format in (DXGI_FORMAT.BC7_TYPELESS, DXGI_FORMAT.BC7_UNORM): elif dxgi_format in (DXGI_FORMAT.BC7_TYPELESS, DXGI_FORMAT.BC7_UNORM):
self.pixel_format = "BC7" 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: elif dxgi_format == DXGI_FORMAT.BC7_UNORM_SRGB:
self.pixel_format = "BC7" self.pixel_format = "BC7"
self.info["gamma"] = 1 / 2.2 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 ( elif dxgi_format in (
DXGI_FORMAT.R8G8B8A8_TYPELESS, DXGI_FORMAT.R8G8B8A8_TYPELESS,
DXGI_FORMAT.R8G8B8A8_UNORM, DXGI_FORMAT.R8G8B8A8_UNORM,
DXGI_FORMAT.R8G8B8A8_UNORM_SRGB, 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: if dxgi_format == DXGI_FORMAT.R8G8B8A8_UNORM_SRGB:
self.info["gamma"] = 1 / 2.2 self.info["gamma"] = 1 / 2.2
else: else:
@ -457,7 +457,7 @@ def _save(im, fp, filename):
if im.mode == "RGBA": if im.mode == "RGBA":
r, g, b, a = im.split() r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b)) 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): def _accept(prefix):

View File

@ -38,6 +38,7 @@ import warnings
from collections.abc import Callable, MutableMapping from collections.abc import Callable, MutableMapping
from enum import IntEnum from enum import IntEnum
from pathlib import Path from pathlib import Path
from typing import NamedTuple, Tuple, List
try: try:
import defusedxml.ElementTree as ElementTree import defusedxml.ElementTree as ElementTree
@ -206,6 +207,14 @@ if hasattr(core, "DEFAULT_STRATEGY"):
RLE = core.RLE RLE = core.RLE
FIXED = core.FIXED FIXED = core.FIXED
Tile = NamedTuple('Tile',
[
('encoder_name', str),
('extents', Tuple[int, int, int, int]),
('offset', int),
('tile_args', Tuple)
]
)
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Registries # Registries
@ -692,6 +701,7 @@ class Image:
def __setstate__(self, state): def __setstate__(self, state):
Image.__init__(self) Image.__init__(self)
self.tile: List[Tile] = []
info, mode, size, palette, data = state info, mode, size, palette, data = state
self.info = info self.info = info
self.mode = mode self.mode = mode

View File

@ -31,6 +31,7 @@ import io
import itertools import itertools
import struct import struct
import sys import sys
from typing import List
from . import Image from . import Image
from ._util import is_path from ._util import is_path
@ -521,13 +522,13 @@ def _save(im, fp, tile, bufsize=0):
fp.flush() fp.flush()
def _encode_tile(im, fp, tile, bufsize, fh, exc=None): def _encode_tile(im, fp, tile: List[Image.Tile], bufsize, fh, exc=None):
for e, b, o, a in tile: for encoder_name, extents, offset, tile_args in tile:
if o > 0: if offset > 0:
fp.seek(o) fp.seek(offset)
encoder = Image._getencoder(im.mode, e, a, im.encoderconfig) encoder = Image._getencoder(im.mode, encoder_name, tile_args, im.encoderconfig)
try: try:
encoder.setimage(im.im, b) encoder.setimage(im.im, extents)
if encoder.pushes_fd: if encoder.pushes_fd:
encoder.setfd(fp) encoder.setfd(fp)
errcode = encoder.encode_to_pyfd()[1] errcode = encoder.encode_to_pyfd()[1]