""" A Pillow loader for .dds files (S3TC-compressed aka DXTC) Jerome Leclanche Documentation: https://web.archive.org/web/20170802060935/http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_compression_s3tc.txt 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 struct from io import BytesIO from . import Image, ImageFile, ImagePalette from ._binary import o8 from ._binary import o32le as o32 # Magic ("DDS ") DDS_MAGIC = 0x20534444 # DDS flags DDSD_CAPS = 0x1 DDSD_HEIGHT = 0x2 DDSD_WIDTH = 0x4 DDSD_PITCH = 0x8 DDSD_PIXELFORMAT = 0x1000 DDSD_MIPMAPCOUNT = 0x20000 DDSD_LINEARSIZE = 0x80000 DDSD_DEPTH = 0x800000 # DDS caps DDSCAPS_COMPLEX = 0x8 DDSCAPS_TEXTURE = 0x1000 DDSCAPS_MIPMAP = 0x400000 DDSCAPS2_CUBEMAP = 0x200 DDSCAPS2_CUBEMAP_POSITIVEX = 0x400 DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800 DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000 DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000 DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000 DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000 DDSCAPS2_VOLUME = 0x200000 # Pixel Format DDPF_ALPHAPIXELS = 0x1 DDPF_ALPHA = 0x2 DDPF_FOURCC = 0x4 DDPF_PALETTEINDEXED8 = 0x20 DDPF_RGB = 0x40 DDPF_LUMINANCE = 0x20000 # dds.h DDS_FOURCC = DDPF_FOURCC DDS_RGB = DDPF_RGB DDS_RGBA = DDPF_RGB | DDPF_ALPHAPIXELS 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_HEADER_FLAGS_MIPMAP = DDSD_MIPMAPCOUNT DDS_HEADER_FLAGS_VOLUME = DDSD_DEPTH DDS_HEADER_FLAGS_PITCH = DDSD_PITCH DDS_HEADER_FLAGS_LINEARSIZE = DDSD_LINEARSIZE 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 DXT1_FOURCC = 0x31545844 # DXT3 DXT3_FOURCC = 0x33545844 # DXT5 DXT5_FOURCC = 0x35545844 # dxgiformat.h DXGI_FORMAT_R8G8B8A8_TYPELESS = 27 DXGI_FORMAT_R8G8B8A8_UNORM = 28 DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29 DXGI_FORMAT_BC5_TYPELESS = 82 DXGI_FORMAT_BC5_UNORM = 83 DXGI_FORMAT_BC5_SNORM = 84 DXGI_FORMAT_BC6H_UF16 = 95 DXGI_FORMAT_BC6H_SF16 = 96 DXGI_FORMAT_BC7_TYPELESS = 97 DXGI_FORMAT_BC7_UNORM = 98 DXGI_FORMAT_BC7_UNORM_SRGB = 99 class DdsImageFile(ImageFile.ImageFile): format = "DDS" format_description = "DirectDraw Surface" def _open(self): if not _accept(self.fp.read(4)): msg = "not a DDS file" raise SyntaxError(msg) (header_size,) = struct.unpack("> (offset + 1) << (offset + 1) == mask: offset += 1 mask_offsets.append(offset) mask_totals.append(mask >> offset) data = bytearray() bytecount = bitcount // 8 while len(data) < self.state.xsize * self.state.ysize * len(masks): value = int.from_bytes(self.fd.read(bytecount), "little") for i, mask in enumerate(masks): masked_value = value & mask # Remove the zero padding, and scale it to 8 bits data += o8( int(((masked_value >> mask_offsets[i]) / mask_totals[i]) * 255) ) self.set_as_raw(bytes(data)) return -1, 0 def _save(im, fp, filename): if im.mode not in ("RGB", "RGBA", "L", "LA"): msg = f"cannot write mode {im.mode} as DDS" raise OSError(msg) rawmode = im.mode masks = [0xFF0000, 0xFF00, 0xFF] if im.mode in ("L", "LA"): pixel_flags = DDPF_LUMINANCE else: pixel_flags = DDPF_RGB rawmode = rawmode[::-1] if im.mode in ("LA", "RGBA"): pixel_flags |= DDPF_ALPHAPIXELS masks.append(0xFF000000) bitcount = len(masks) * 8 while len(masks) < 4: masks.append(0) fp.write( o32(DDS_MAGIC) + o32(124) # header size + o32( DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH | DDSD_PIXELFORMAT ) # flags + o32(im.height) + o32(im.width) + o32((im.width * bitcount + 7) // 8) # pitch + o32(0) # depth + o32(0) # mipmaps + o32(0) * 11 # reserved + o32(32) # pfsize + o32(pixel_flags) # pfflags + o32(0) # fourcc + o32(bitcount) # bitcount + b"".join(o32(mask) for mask in masks) # rgbabitmask + o32(DDSCAPS_TEXTURE) # dwCaps + o32(0) # dwCaps2 + o32(0) # dwCaps3 + o32(0) # dwCaps4 + o32(0) # dwReserved2 ) if im.mode == "RGBA": r, g, b, a = im.split() im = Image.merge("RGBA", (a, r, g, b)) ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))]) def _accept(prefix): return prefix[:4] == b"DDS " Image.register_open(DdsImageFile.format, DdsImageFile, _accept) Image.register_decoder("dds_rgb", DdsRgbDecoder) Image.register_save(DdsImageFile.format, _save) Image.register_extension(DdsImageFile.format, ".dds")