Merge pull request #4 from radarhere/improved_dds

Loop over enums to create constants
This commit is contained in:
REDxEYE 2023-10-17 13:34:30 +03:00 committed by GitHub
commit f59c9ae348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 55 deletions

View File

@ -11,6 +11,7 @@ Full text of the CC0 license:
""" """
import io import io
import struct import struct
import sys
from enum import IntEnum, IntFlag from enum import IntEnum, IntFlag
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
@ -263,62 +264,43 @@ class D3DFMT(IntEnum):
# Backward compat layer # Backward compat layer
DDSD_CAPS = DDSD.CAPS module = sys.modules[__name__]
DDSD_HEIGHT = DDSD.HEIGHT for item in DDSD:
DDSD_WIDTH = DDSD.WIDTH setattr(module, "DDSD_" + item.name, item.value)
DDSD_PITCH = DDSD.PITCH for item in DDSCAPS:
DDSD_PIXELFORMAT = DDSD.PIXELFORMAT setattr(module, "DDSCAPS_" + item.name, item.value)
DDSD_MIPMAPCOUNT = DDSD.MIPMAPCOUNT for item in DDSCAPS2:
DDSD_LINEARSIZE = DDSD.LINEARSIZE setattr(module, "DDSCAPS2_" + item.name, item.value)
DDSD_DEPTH = DDSD.DEPTH for item in DDPF:
setattr(module, "DDPF_" + item.name, item.value)
DDSCAPS_COMPLEX = DDSCAPS.COMPLEX DDS_FOURCC = DDPF.FOURCC
DDSCAPS_TEXTURE = DDSCAPS.TEXTURE DDS_RGB = DDPF.RGB
DDSCAPS_MIPMAP = DDSCAPS.MIPMAP DDS_RGBA = DDPF.RGB | DDPF.ALPHAPIXELS
DDS_LUMINANCE = DDPF.LUMINANCE
DDS_LUMINANCEA = DDPF.LUMINANCE | DDPF.ALPHAPIXELS
DDS_ALPHA = DDPF.ALPHA
DDS_PAL8 = DDPF.PALETTEINDEXED8
DDSCAPS2_CUBEMAP = DDSCAPS2.CUBEMAP DDS_HEADER_FLAGS_TEXTURE = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PIXELFORMAT
DDSCAPS2_CUBEMAP_POSITIVEX = DDSCAPS2.CUBEMAP_POSITIVEX DDS_HEADER_FLAGS_MIPMAP = DDSD.MIPMAPCOUNT
DDSCAPS2_CUBEMAP_NEGATIVEX = DDSCAPS2.CUBEMAP_NEGATIVEX DDS_HEADER_FLAGS_VOLUME = DDSD.DEPTH
DDSCAPS2_CUBEMAP_POSITIVEY = DDSCAPS2.CUBEMAP_POSITIVEY DDS_HEADER_FLAGS_PITCH = DDSD.PITCH
DDSCAPS2_CUBEMAP_NEGATIVEY = DDSCAPS2.CUBEMAP_NEGATIVEY DDS_HEADER_FLAGS_LINEARSIZE = DDSD.LINEARSIZE
DDSCAPS2_CUBEMAP_POSITIVEZ = DDSCAPS2.CUBEMAP_POSITIVEZ
DDSCAPS2_CUBEMAP_NEGATIVEZ = DDSCAPS2.CUBEMAP_NEGATIVEZ
DDSCAPS2_VOLUME = DDSCAPS2.VOLUME
DDPF_ALPHAPIXELS = DDPF.ALPHAPIXELS DDS_HEIGHT = DDSD.HEIGHT
DDPF_ALPHA = DDPF.ALPHA DDS_WIDTH = DDSD.WIDTH
DDPF_FOURCC = DDPF.FOURCC
DDPF_PALETTEINDEXED8 = DDPF.PALETTEINDEXED8
DDPF_RGB = DDPF.RGB
DDPF_LUMINANCE = DDPF.LUMINANCE
DDS_FOURCC = DDPF_FOURCC DDS_SURFACE_FLAGS_TEXTURE = DDSCAPS.TEXTURE
DDS_RGB = DDPF_RGB DDS_SURFACE_FLAGS_MIPMAP = DDSCAPS.COMPLEX | DDSCAPS.MIPMAP
DDS_RGBA = DDPF_RGB | DDPF_ALPHAPIXELS DDS_SURFACE_FLAGS_CUBEMAP = DDSCAPS.COMPLEX
DDS_LUMINANCE = DDPF_LUMINANCE
DDS_LUMINANCEA = DDPF_LUMINANCE | DDPF_ALPHAPIXELS
DDS_ALPHA = DDPF_ALPHA
DDS_PAL8 = DDPF_PALETTEINDEXED8
DDS_HEADER_FLAGS_TEXTURE = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT DDS_CUBEMAP_POSITIVEX = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_POSITIVEX
DDS_HEADER_FLAGS_MIPMAP = DDSD_MIPMAPCOUNT DDS_CUBEMAP_NEGATIVEX = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_NEGATIVEX
DDS_HEADER_FLAGS_VOLUME = DDSD_DEPTH DDS_CUBEMAP_POSITIVEY = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_POSITIVEY
DDS_HEADER_FLAGS_PITCH = DDSD_PITCH DDS_CUBEMAP_NEGATIVEY = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_NEGATIVEY
DDS_HEADER_FLAGS_LINEARSIZE = DDSD_LINEARSIZE DDS_CUBEMAP_POSITIVEZ = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_POSITIVEZ
DDS_CUBEMAP_NEGATIVEZ = DDSCAPS2.CUBEMAP | DDSCAPS2.CUBEMAP_NEGATIVEZ
DDS_HEIGHT = DDSD_HEIGHT
DDS_WIDTH = DDSD_WIDTH
DDS_SURFACE_FLAGS_TEXTURE = DDSCAPS_TEXTURE
DDS_SURFACE_FLAGS_MIPMAP = DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
DDS_SURFACE_FLAGS_CUBEMAP = DDSCAPS_COMPLEX
DDS_CUBEMAP_POSITIVEX = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
DDS_CUBEMAP_NEGATIVEX = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
DDS_CUBEMAP_POSITIVEY = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
DDS_CUBEMAP_NEGATIVEY = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
DDS_CUBEMAP_POSITIVEZ = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
DDS_CUBEMAP_NEGATIVEZ = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
DXT1_FOURCC = D3DFMT.DXT1 DXT1_FOURCC = D3DFMT.DXT1
DXT3_FOURCC = D3DFMT.DXT3 DXT3_FOURCC = D3DFMT.DXT3
@ -405,7 +387,7 @@ class DdsImageFile(ImageFile.ImageFile):
else: else:
msg = f"Unsupported bitcount {bitcount} for {pfflags_}" msg = f"Unsupported bitcount {bitcount} for {pfflags_}"
raise OSError(msg) raise OSError(msg)
elif pfflags & DDPF_PALETTEINDEXED8: elif pfflags & DDPF.PALETTEINDEXED8:
self._mode = "P" self._mode = "P"
self.palette = ImagePalette.raw("RGBA", self.fp.read(1024)) self.palette = ImagePalette.raw("RGBA", self.fp.read(1024))
self.tile = [("raw", (0, 0) + self.size, 0, "L")] self.tile = [("raw", (0, 0) + self.size, 0, "L")]

View File

@ -212,7 +212,7 @@ class _Tile(NamedTuple):
encoder_name: str encoder_name: str
extents: tuple[int, int, int, int] extents: tuple[int, int, int, int]
offset: int offset: int
tile_args: tuple args: tuple
# -------------------------------------------------------------------- # --------------------------------------------------------------------

View File

@ -523,10 +523,10 @@ def _save(im, fp, tile, bufsize=0):
def _encode_tile(im, fp, tile: list[Image._Tile], bufsize, fh, exc=None): def _encode_tile(im, fp, tile: list[Image._Tile], bufsize, fh, exc=None):
for encoder_name, extents, offset, tile_args in tile: for encoder_name, extents, offset, args in tile:
if offset > 0: if offset > 0:
fp.seek(offset) fp.seek(offset)
encoder = Image._getencoder(im.mode, encoder_name, tile_args, im.encoderconfig) encoder = Image._getencoder(im.mode, encoder_name, args, im.encoderconfig)
try: try:
encoder.setimage(im.im, extents) encoder.setimage(im.im, extents)
if encoder.pushes_fd: if encoder.pushes_fd: