various DDSFile tweaks

This commit is contained in:
Andrew Cassidy 2021-03-23 02:23:48 -07:00
parent 023be6d770
commit c86eca293c

View File

@ -14,6 +14,7 @@ class DDSFlags(enum.IntFlag):
MIPMAPCOUNT = 0x20000 MIPMAPCOUNT = 0x20000
LINEAR_SIZE = 0x80000 LINEAR_SIZE = 0x80000
DEPTH = 0x800000 DEPTH = 0x800000
REQUIRED = CAPS | HEIGHT | WIDTH | PIXEL_FORMAT
class DDSPixelFlags(enum.IntFlag): class DDSPixelFlags(enum.IntFlag):
@ -94,12 +95,12 @@ class DDSHeader:
"""The size of a DDS header in bytes.""" """The size of a DDS header in bytes."""
def __init__(self): def __init__(self):
self.flags: DDSFlags = DDSFlags(DDSFlags.CAPS | DDSFlags.HEIGHT | DDSFlags.WIDTH | DDSFlags.PIXEL_FORMAT) self.flags: DDSFlags = DDSFlags.REQUIRED
self.image_size: typing.Tuple[int, int] = (0, 0) self.dimensions: typing.Tuple[int, int] = (0, 0)
self.linear_size: int = 0 self.linear_size: int = 0
self.depth = 0 self.depth: int = 0
self.mipmap_count = 0 self.mipmap_count: int = 0
self.pixel_format = PixelFormat() self.pixel_format: PixelFormat = PixelFormat()
self.caps = (0, 0, 0, 0) self.caps = (0, 0, 0, 0)
@staticmethod @staticmethod
@ -117,12 +118,15 @@ class DDSHeader:
header = DDSHeader() header = DDSHeader()
header.flags = DDSFlags(unpacked[1]) header.flags = DDSFlags(unpacked[1])
header.image_size = unpacked[2:4:-1] header.dimensions = unpacked[2:4:-1]
header.linear_size, header.depth, header.mipmapcount = unpacked[4:7] header.linear_size, header.depth, header.mipmapcount = unpacked[4:7]
header.pixel_format = PixelFormat.from_bytes(data[72:104]) header.pixel_format = PixelFormat.from_bytes(data[72:104])
header.caps = struct.unpack('<4I4x', data[104:124]) header.caps = struct.unpack('<4I4x', data[104:124])
assert all([val > 0 for val in header.dimensions]), "Image size is zero"
return header return header
@staticmethod @staticmethod
@ -144,7 +148,7 @@ class DDSHeader:
""" """
data = b'' data = b''
# write header # write header
data += struct.pack('<7I44x', DDSHeader.size, int(self.flags), self.image_size[1], self.image_size[0], data += struct.pack('<7I44x', DDSHeader.size, int(self.flags), self.dimensions[1], self.dimensions[0],
self.linear_size, self.depth, self.mipmap_count) self.linear_size, self.depth, self.mipmap_count)
data += self.pixel_format.to_bytes() data += self.pixel_format.to_bytes()
data += struct.pack('<4I4x', *self.caps) data += struct.pack('<4I4x', *self.caps)
@ -163,6 +167,9 @@ class DDSFile:
magic = b'DDS ' magic = b'DDS '
"""Magic bytes at the start of every DDS file.""" """Magic bytes at the start of every DDS file."""
extension = 'dds'
"""Extension for a DDS file."""
def __init__(self): def __init__(self):
self.header: DDSHeader = DDSHeader() self.header: DDSHeader = DDSHeader()
"""The DDS file's header object""" """The DDS file's header object"""