From 5aadeb5004588f2b4e9be73e61e978586b5192f5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 2 Dec 2023 01:24:29 +1100 Subject: [PATCH] Moved _Tile to ImageFile --- src/PIL/DdsImagePlugin.py | 19 ++++++++++++------- src/PIL/Image.py | 10 ---------- src/PIL/ImageFile.py | 10 +++++++++- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index bdcb8da36..e8d5a8b0e 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -9,6 +9,7 @@ The contents of this file are hereby released in the public domain (CC0) Full text of the CC0 license: https://creativecommons.org/publicdomain/zero/1.0/ """ + import io import struct import sys @@ -460,9 +461,11 @@ class DdsImageFile(ImageFile.ImageFile): extents = (0, 0) + self.size if n: - self.tile = [Image._Tile("bcn", extents, offset, (n, self.pixel_format))] + self.tile = [ + ImageFile._Tile("bcn", extents, offset, (n, self.pixel_format)) + ] else: - self.tile = [Image._Tile("raw", extents, 0, rawmode or self.mode)] + self.tile = [ImageFile._Tile("raw", extents, 0, rawmode or self.mode)] def load_seek(self, pos): pass @@ -494,8 +497,8 @@ def _save(im, fp, filename): rgba_mask.append(0xFF000000 if alpha else 0) flags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PITCH | DDSD.PIXELFORMAT - bit_count = len(im.getbands()) * 8 - stride = (im.width * bit_count + 7) // 8 + bitcount = len(im.getbands()) * 8 + pitch = (im.width * bitcount + 7) // 8 fp.write( o32(DDS_MAGIC) @@ -505,17 +508,19 @@ def _save(im, fp, filename): flags, # flags im.height, im.width, - stride, # pitch + pitch, 0, # depth 0, # mipmaps ) + struct.pack("11I", *((0,) * 11)) # reserved # pfsize, pfflags, fourcc, bitcount - + struct.pack("<4I", 32, pixel_flags, 0, bit_count) + + struct.pack("<4I", 32, pixel_flags, 0, bitcount) + struct.pack("<4I", *rgba_mask) # dwRGBABitMask + struct.pack("<5I", DDSCAPS.TEXTURE, 0, 0, 0, 0) ) - ImageFile._save(im, fp, [Image._Tile("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))]) + ImageFile._save( + im, fp, [ImageFile._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 b8b8fcd81..2853bd596 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -23,7 +23,6 @@ # # See the README file for information on usage and redistribution. # -from __future__ import annotations import atexit import builtins @@ -39,7 +38,6 @@ import warnings from collections.abc import Callable, MutableMapping from enum import IntEnum from pathlib import Path -from typing import NamedTuple try: from defusedxml import ElementTree @@ -208,13 +206,6 @@ if hasattr(core, "DEFAULT_STRATEGY"): FIXED = core.FIXED -class _Tile(NamedTuple): - encoder_name: str - extents: tuple[int, int, int, int] - offset: int - args: tuple | str | None - - # -------------------------------------------------------------------- # Registries @@ -706,7 +697,6 @@ 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 e8455d064..8bca19a4b 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -32,6 +32,7 @@ import io import itertools import struct import sys +from typing import NamedTuple from . import Image from ._util import is_path @@ -78,6 +79,13 @@ def _tilesort(t): return t[2] +class _Tile(NamedTuple): + encoder_name: str + extents: tuple[int, int, int, int] + offset: int + args: tuple | str | None + + # # -------------------------------------------------------------------- # ImageFile base class @@ -521,7 +529,7 @@ def _save(im, fp, tile, bufsize=0): fp.flush() -def _encode_tile(im, fp, tile: list[Image._Tile], bufsize, fh, exc=None): +def _encode_tile(im, fp, tile: list[_Tile], bufsize, fh, exc=None): for encoder_name, extents, offset, args in tile: if offset > 0: fp.seek(offset)