Merge pull request #6 from radarhere/improved_dds

Simplified tile creation
This commit is contained in:
REDxEYE 2023-10-21 18:41:01 +03:00 committed by GitHub
commit 2ca9adb79d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 77 deletions

View File

Before

Width:  |  Height:  |  Size: 969 B

After

Width:  |  Height:  |  Size: 969 B

View File

@ -10,8 +10,8 @@ from .helper import assert_image_equal, assert_image_equal_tofile, hopper
TEST_FILE_DXT1 = "Tests/images/dxt1-rgb-4bbp-noalpha_MipMaps-1.dds" TEST_FILE_DXT1 = "Tests/images/dxt1-rgb-4bbp-noalpha_MipMaps-1.dds"
TEST_FILE_DXT3 = "Tests/images/dxt3-argb-8bbp-explicitalpha_MipMaps-1.dds" TEST_FILE_DXT3 = "Tests/images/dxt3-argb-8bbp-explicitalpha_MipMaps-1.dds"
TEST_FILE_DXT5 = "Tests/images/dxt5-argb-8bbp-interpolatedalpha_MipMaps-1.dds" TEST_FILE_DXT5 = "Tests/images/dxt5-argb-8bbp-interpolatedalpha_MipMaps-1.dds"
TEST_FILE_ATI1 = "Tests/images/mode-ati1.dds" TEST_FILE_ATI1 = "Tests/images/ati1.dds"
TEST_FILE_ATI2 = "Tests/images/mode-ati2.dds" TEST_FILE_ATI2 = "Tests/images/ati2.dds"
TEST_FILE_DX10_BC5_TYPELESS = "Tests/images/bc5_typeless.dds" TEST_FILE_DX10_BC5_TYPELESS = "Tests/images/bc5_typeless.dds"
TEST_FILE_DX10_BC5_UNORM = "Tests/images/bc5_unorm.dds" TEST_FILE_DX10_BC5_UNORM = "Tests/images/bc5_unorm.dds"
TEST_FILE_DX10_BC5_SNORM = "Tests/images/bc5_snorm.dds" TEST_FILE_DX10_BC5_SNORM = "Tests/images/bc5_snorm.dds"
@ -347,7 +347,7 @@ def test_open(mode, test_file):
("LA", "Tests/images/uncompressed_la.png"), ("LA", "Tests/images/uncompressed_la.png"),
("RGB", "Tests/images/hopper.png"), ("RGB", "Tests/images/hopper.png"),
("RGBA", "Tests/images/pil123rgba.png"), ("RGBA", "Tests/images/pil123rgba.png"),
("L", "Tests/images/mode-ati1.dds"), ("L", TEST_FILE_ATI1),
], ],
) )
def test_save(mode, test_file, tmp_path): def test_save(mode, test_file, tmp_path):

View File

@ -348,14 +348,14 @@ class DdsImageFile(ImageFile.ImageFile):
masks = struct.unpack("<4I", header.read(16)) masks = struct.unpack("<4I", header.read(16))
if flags & DDSD.CAPS: if flags & DDSD.CAPS:
header.seek(20, io.SEEK_CUR) header.seek(20, io.SEEK_CUR)
extents = (0, 0) + self.size n = 0
rawmode = None
if pfflags & DDPF.RGB: if pfflags & DDPF.RGB:
# Texture contains uncompressed RGB data # Texture contains uncompressed RGB data
masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)} masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)}
if bitcount == 24: if bitcount == 24:
rawmode = masks[0x00FF0000] + masks[0x0000FF00] + masks[0x000000FF]
self._mode = "RGB" self._mode = "RGB"
self.tile = [("raw", extents, 0, (rawmode[::-1], 0, 1))] rawmode = masks[0x00FF0000] + masks[0x0000FF00] + masks[0x000000FF]
elif bitcount == 32 and pfflags & DDPF.ALPHAPIXELS: elif bitcount == 32 and pfflags & DDPF.ALPHAPIXELS:
self._mode = "RGBA" self._mode = "RGBA"
rawmode = ( rawmode = (
@ -364,61 +364,53 @@ class DdsImageFile(ImageFile.ImageFile):
+ masks[0x0000FF00] + masks[0x0000FF00]
+ masks[0x000000FF] + masks[0x000000FF]
) )
self.tile = [("raw", extents, 0, (rawmode[::-1], 0, 1))]
else: else:
msg = f"Unsupported bitcount {bitcount} for {pfflags}" msg = f"Unsupported bitcount {bitcount} for {pfflags}"
raise OSError(msg) raise OSError(msg)
rawmode = rawmode[::-1]
elif pfflags & DDPF.ALPHA: elif pfflags & DDPF.ALPHA:
if bitcount == 8: if bitcount == 8:
self._mode = "L" self._mode = "L"
self.tile = [("raw", extents, 0, ("L", 0, 1))]
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.LUMINANCE: elif pfflags & DDPF.LUMINANCE:
if bitcount == 8: if bitcount == 8:
self._mode = "L" self._mode = "L"
self.tile = [("raw", extents, 0, ("L", 0, 1))]
elif bitcount == 16 and pfflags & DDPF.ALPHAPIXELS: elif bitcount == 16 and pfflags & DDPF.ALPHAPIXELS:
self._mode = "LA" self._mode = "LA"
self.tile = [("raw", extents, 0, ("LA", 0, 1))]
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")]
elif pfflags & DDPF.FOURCC: elif pfflags & DDPF.FOURCC:
data_offs = header_size + 4 data_offs = header_size + 4
if fourcc == D3DFMT.DXT1: if fourcc == D3DFMT.DXT1:
self._mode = "RGBA" self._mode = "RGBA"
self.pixel_format = "DXT1" self.pixel_format = "DXT1"
tile = Image._Tile("bcn", extents, data_offs, (1, self.pixel_format)) n = 1
elif fourcc == D3DFMT.DXT3: elif fourcc == D3DFMT.DXT3:
self._mode = "RGBA" self._mode = "RGBA"
self.pixel_format = "DXT3" self.pixel_format = "DXT3"
tile = Image._Tile("bcn", extents, data_offs, (2, self.pixel_format)) n = 2
elif fourcc == D3DFMT.DXT5: elif fourcc == D3DFMT.DXT5:
self._mode = "RGBA" self._mode = "RGBA"
self.pixel_format = "DXT5" self.pixel_format = "DXT5"
tile = Image._Tile("bcn", extents, data_offs, (3, self.pixel_format)) n = 3
elif fourcc == D3DFMT.ATI1 or fourcc == D3DFMT.BC4U: elif fourcc in (D3DFMT.BC4U, D3DFMT.ATI1):
self._mode = "L" self._mode = "L"
self.pixel_format = "BC4" self.pixel_format = "BC4"
tile = Image._Tile("bcn", extents, data_offs, (4, self.pixel_format)) n = 4
elif fourcc == D3DFMT.BC5S: elif fourcc == D3DFMT.BC5S:
self._mode = "RGB" self._mode = "RGB"
self.pixel_format = "BC5S" self.pixel_format = "BC5S"
tile = Image._Tile("bcn", extents, data_offs, (5, self.pixel_format)) n = 5
elif fourcc == D3DFMT.BC5U: elif fourcc in (D3DFMT.BC5U, D3DFMT.ATI2):
self._mode = "RGB"
self.pixel_format = "BC5U"
tile = Image._Tile("bcn", extents, data_offs, (5, self.pixel_format))
elif fourcc == D3DFMT.ATI2:
self._mode = "RGB" self._mode = "RGB"
self.pixel_format = "BC5" self.pixel_format = "BC5"
tile = Image._Tile("bcn", extents, data_offs, (5, self.pixel_format)) n = 5
elif fourcc == D3DFMT.DX10: elif fourcc == D3DFMT.DX10:
data_offs += 20 data_offs += 20
# ignoring flags which pertain to volume textures and cubemaps # ignoring flags which pertain to volume textures and cubemaps
@ -431,53 +423,39 @@ class DdsImageFile(ImageFile.ImageFile):
): ):
self._mode = "RGBA" self._mode = "RGBA"
self.pixel_format = "BC1" self.pixel_format = "BC1"
tile = Image._Tile( n = 1
"bcn", extents, data_offs, (1, self.pixel_format)
)
elif dxgi_format in (DXGI_FORMAT.BC5_TYPELESS, DXGI_FORMAT.BC5_UNORM): elif dxgi_format in (DXGI_FORMAT.BC5_TYPELESS, DXGI_FORMAT.BC5_UNORM):
self._mode = "RGB" self._mode = "RGB"
self.pixel_format = "BC5" self.pixel_format = "BC5"
tile = Image._Tile( n = 5
"bcn", extents, data_offs, (5, self.pixel_format)
)
elif dxgi_format == DXGI_FORMAT.BC5_SNORM: elif dxgi_format == DXGI_FORMAT.BC5_SNORM:
self._mode = "RGB" self._mode = "RGB"
self.pixel_format = "BC5S" self.pixel_format = "BC5S"
tile = Image._Tile( n = 5
"bcn", extents, data_offs, (5, self.pixel_format)
)
elif dxgi_format == DXGI_FORMAT.BC6H_UF16: elif dxgi_format == DXGI_FORMAT.BC6H_UF16:
self._mode = "RGB" self._mode = "RGB"
self.pixel_format = "BC6H" self.pixel_format = "BC6H"
tile = Image._Tile( n = 6
"bcn", extents, data_offs, (6, self.pixel_format)
)
elif dxgi_format == DXGI_FORMAT.BC6H_SF16: elif dxgi_format == DXGI_FORMAT.BC6H_SF16:
self._mode = "RGB" self._mode = "RGB"
self.pixel_format = "BC6HS" self.pixel_format = "BC6HS"
tile = Image._Tile( n = 6
"bcn", extents, data_offs, (6, self.pixel_format) elif dxgi_format in (
) DXGI_FORMAT.BC7_TYPELESS,
elif dxgi_format in (DXGI_FORMAT.BC7_TYPELESS, DXGI_FORMAT.BC7_UNORM): DXGI_FORMAT.BC7_UNORM,
DXGI_FORMAT.BC7_UNORM_SRGB,
):
self._mode = "RGBA" self._mode = "RGBA"
self.pixel_format = "BC7" self.pixel_format = "BC7"
tile = Image._Tile( n = 7
"bcn", extents, data_offs, (7, self.pixel_format) if dxgi_format == DXGI_FORMAT.BC7_UNORM_SRGB:
) self.info["gamma"] = 1 / 2.2
elif dxgi_format == DXGI_FORMAT.BC7_UNORM_SRGB:
self._mode = "RGBA"
self.pixel_format = "BC7"
self.info["gamma"] = 1 / 2.2
tile = Image._Tile(
"bcn", extents, data_offs, (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,
): ):
self._mode = "RGBA" self._mode = "RGBA"
tile = Image._Tile("raw", extents, 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:
@ -486,12 +464,16 @@ class DdsImageFile(ImageFile.ImageFile):
else: else:
msg = f"Unimplemented pixel format {repr(fourcc)}" msg = f"Unimplemented pixel format {repr(fourcc)}"
raise NotImplementedError(msg) raise NotImplementedError(msg)
self.tile = [tile]
else: else:
msg = f"Unknown pixel format flags {repr(pfflags)}" msg = f"Unknown pixel format flags {pfflags}"
raise NotImplementedError(msg) raise NotImplementedError(msg)
extents = (0, 0) + self.size
if n:
self.tile = [Image._Tile("bcn", extents, data_offs, (n, self.pixel_format))]
else:
self.tile = [Image._Tile("raw", extents, 0, rawmode or self.mode)]
def load_seek(self, pos): def load_seek(self, pos):
pass pass
@ -501,32 +483,34 @@ def _save(im, fp, filename):
msg = f"cannot write mode {im.mode} as DDS" msg = f"cannot write mode {im.mode} as DDS"
raise OSError(msg) raise OSError(msg)
pixel_flags = DDPF.RGB alpha = im.mode[-1] == "A"
if im.mode == "RGB": if im.mode[0] == "L":
rgba_mask = struct.pack("<4I", 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000)
bit_count = 24
elif im.mode == "RGBA":
pixel_flags |= DDPF.ALPHAPIXELS
rgba_mask = struct.pack("<4I", 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)
bit_count = 32
r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b))
elif im.mode == "LA":
pixel_flags = DDPF.LUMINANCE | DDPF.ALPHAPIXELS
rgba_mask = struct.pack("<4I", 0x000000FF, 0x000000FF, 0x000000FF, 0x0000FF00)
bit_count = 16
else: # im.mode == "L"
pixel_flags = DDPF.LUMINANCE pixel_flags = DDPF.LUMINANCE
rgba_mask = struct.pack("<4I", 0xFF000000, 0xFF000000, 0xFF000000, 0x00000000) rawmode = im.mode
bit_count = 8 if alpha:
rgba_mask = [0x000000FF, 0x000000FF, 0x000000FF]
else:
rgba_mask = [0xFF000000, 0xFF000000, 0xFF000000]
else:
pixel_flags = DDPF.RGB
rawmode = im.mode[::-1]
rgba_mask = [0x00FF0000, 0x0000FF00, 0x000000FF]
if alpha:
r, g, b, a = im.split()
im = Image.merge("RGBA", (a, r, g, b))
if alpha:
pixel_flags |= DDPF.ALPHAPIXELS
rgba_mask.append(0xFF000000 if alpha else 0)
flags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PITCH | DDSD.PIXELFORMAT flags = DDSD.CAPS | DDSD.HEIGHT | DDSD.WIDTH | DDSD.PITCH | DDSD.PIXELFORMAT
bit_count = len(im.getbands()) * 8
stride = (im.width * bit_count + 7) // 8 stride = (im.width * bit_count + 7) // 8
fp.write( fp.write(
o32(DDS_MAGIC) o32(DDS_MAGIC)
+ struct.pack( + struct.pack(
"<IIIIIII", "<7I",
124, # header size 124, # header size
flags, # flags flags, # flags
im.height, im.height,
@ -537,12 +521,11 @@ def _save(im, fp, filename):
) )
+ struct.pack("11I", *((0,) * 11)) # reserved + struct.pack("11I", *((0,) * 11)) # reserved
# pfsize, pfflags, fourcc, bitcount # pfsize, pfflags, fourcc, bitcount
+ struct.pack("<IIII", 32, pixel_flags, 0, bit_count) + struct.pack("<4I", 32, pixel_flags, 0, bit_count)
+ rgba_mask # dwRGBABitMask + struct.pack("<4I", *rgba_mask) # dwRGBABitMask
+ struct.pack("<IIIII", DDSCAPS.TEXTURE, 0, 0, 0, 0) + struct.pack("<5I", DDSCAPS.TEXTURE, 0, 0, 0, 0)
) )
mode = "LA" if im.mode == "LA" else im.mode[::-1] ImageFile._save(im, fp, [Image._Tile("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))])
ImageFile._save(im, fp, [Image._Tile("raw", (0, 0) + im.size, 0, (mode, 0, 1))])
def _accept(prefix): def _accept(prefix):