DdsImagePlugin.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. """
  2. A Pillow loader for .dds files (S3TC-compressed aka DXTC)
  3. Jerome Leclanche <jerome@leclan.ch>
  4. Documentation:
  5. https://web.archive.org/web/20170802060935/http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_compression_s3tc.txt
  6. The contents of this file are hereby released in the public domain (CC0)
  7. Full text of the CC0 license:
  8. https://creativecommons.org/publicdomain/zero/1.0/
  9. """
  10. import struct
  11. from io import BytesIO
  12. from . import Image, ImageFile
  13. from ._binary import o32le as o32
  14. # Magic ("DDS ")
  15. DDS_MAGIC = 0x20534444
  16. # DDS flags
  17. DDSD_CAPS = 0x1
  18. DDSD_HEIGHT = 0x2
  19. DDSD_WIDTH = 0x4
  20. DDSD_PITCH = 0x8
  21. DDSD_PIXELFORMAT = 0x1000
  22. DDSD_MIPMAPCOUNT = 0x20000
  23. DDSD_LINEARSIZE = 0x80000
  24. DDSD_DEPTH = 0x800000
  25. # DDS caps
  26. DDSCAPS_COMPLEX = 0x8
  27. DDSCAPS_TEXTURE = 0x1000
  28. DDSCAPS_MIPMAP = 0x400000
  29. DDSCAPS2_CUBEMAP = 0x200
  30. DDSCAPS2_CUBEMAP_POSITIVEX = 0x400
  31. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800
  32. DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000
  33. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000
  34. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000
  35. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000
  36. DDSCAPS2_VOLUME = 0x200000
  37. # Pixel Format
  38. DDPF_ALPHAPIXELS = 0x1
  39. DDPF_ALPHA = 0x2
  40. DDPF_FOURCC = 0x4
  41. DDPF_PALETTEINDEXED8 = 0x20
  42. DDPF_RGB = 0x40
  43. DDPF_LUMINANCE = 0x20000
  44. # dds.h
  45. DDS_FOURCC = DDPF_FOURCC
  46. DDS_RGB = DDPF_RGB
  47. DDS_RGBA = DDPF_RGB | DDPF_ALPHAPIXELS
  48. DDS_LUMINANCE = DDPF_LUMINANCE
  49. DDS_LUMINANCEA = DDPF_LUMINANCE | DDPF_ALPHAPIXELS
  50. DDS_ALPHA = DDPF_ALPHA
  51. DDS_PAL8 = DDPF_PALETTEINDEXED8
  52. DDS_HEADER_FLAGS_TEXTURE = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT
  53. DDS_HEADER_FLAGS_MIPMAP = DDSD_MIPMAPCOUNT
  54. DDS_HEADER_FLAGS_VOLUME = DDSD_DEPTH
  55. DDS_HEADER_FLAGS_PITCH = DDSD_PITCH
  56. DDS_HEADER_FLAGS_LINEARSIZE = DDSD_LINEARSIZE
  57. DDS_HEIGHT = DDSD_HEIGHT
  58. DDS_WIDTH = DDSD_WIDTH
  59. DDS_SURFACE_FLAGS_TEXTURE = DDSCAPS_TEXTURE
  60. DDS_SURFACE_FLAGS_MIPMAP = DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
  61. DDS_SURFACE_FLAGS_CUBEMAP = DDSCAPS_COMPLEX
  62. DDS_CUBEMAP_POSITIVEX = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
  63. DDS_CUBEMAP_NEGATIVEX = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
  64. DDS_CUBEMAP_POSITIVEY = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
  65. DDS_CUBEMAP_NEGATIVEY = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
  66. DDS_CUBEMAP_POSITIVEZ = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
  67. DDS_CUBEMAP_NEGATIVEZ = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
  68. # DXT1
  69. DXT1_FOURCC = 0x31545844
  70. # DXT3
  71. DXT3_FOURCC = 0x33545844
  72. # DXT5
  73. DXT5_FOURCC = 0x35545844
  74. # dxgiformat.h
  75. DXGI_FORMAT_R8G8B8A8_TYPELESS = 27
  76. DXGI_FORMAT_R8G8B8A8_UNORM = 28
  77. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29
  78. DXGI_FORMAT_BC5_TYPELESS = 82
  79. DXGI_FORMAT_BC5_UNORM = 83
  80. DXGI_FORMAT_BC5_SNORM = 84
  81. DXGI_FORMAT_BC7_TYPELESS = 97
  82. DXGI_FORMAT_BC7_UNORM = 98
  83. DXGI_FORMAT_BC7_UNORM_SRGB = 99
  84. class DdsImageFile(ImageFile.ImageFile):
  85. format = "DDS"
  86. format_description = "DirectDraw Surface"
  87. def _open(self):
  88. magic, header_size = struct.unpack("<II", self.fp.read(8))
  89. if header_size != 124:
  90. raise OSError(f"Unsupported header size {repr(header_size)}")
  91. header_bytes = self.fp.read(header_size - 4)
  92. if len(header_bytes) != 120:
  93. raise OSError(f"Incomplete header: {len(header_bytes)} bytes")
  94. header = BytesIO(header_bytes)
  95. flags, height, width = struct.unpack("<3I", header.read(12))
  96. self._size = (width, height)
  97. self.mode = "RGBA"
  98. pitch, depth, mipmaps = struct.unpack("<3I", header.read(12))
  99. struct.unpack("<11I", header.read(44)) # reserved
  100. # pixel format
  101. pfsize, pfflags = struct.unpack("<2I", header.read(8))
  102. fourcc = header.read(4)
  103. (bitcount,) = struct.unpack("<I", header.read(4))
  104. masks = struct.unpack("<4I", header.read(16))
  105. if pfflags & DDPF_RGB:
  106. # Texture contains uncompressed RGB data
  107. masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)}
  108. rawmode = ""
  109. if bitcount == 32:
  110. rawmode += masks[0xFF000000]
  111. else:
  112. self.mode = "RGB"
  113. rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF]
  114. self.tile = [("raw", (0, 0) + self.size, 0, (rawmode[::-1], 0, 1))]
  115. else:
  116. data_start = header_size + 4
  117. n = 0
  118. if fourcc == b"DXT1":
  119. self.pixel_format = "DXT1"
  120. n = 1
  121. elif fourcc == b"DXT3":
  122. self.pixel_format = "DXT3"
  123. n = 2
  124. elif fourcc == b"DXT5":
  125. self.pixel_format = "DXT5"
  126. n = 3
  127. elif fourcc == b"BC5S":
  128. self.pixel_format = "BC5S"
  129. n = 5
  130. self.mode = "RGB"
  131. elif fourcc == b"DX10":
  132. data_start += 20
  133. # ignoring flags which pertain to volume textures and cubemaps
  134. (dxgi_format,) = struct.unpack("<I", self.fp.read(4))
  135. self.fp.read(16)
  136. if dxgi_format in (DXGI_FORMAT_BC5_TYPELESS, DXGI_FORMAT_BC5_UNORM):
  137. self.pixel_format = "BC5"
  138. n = 5
  139. self.mode = "RGB"
  140. elif dxgi_format == DXGI_FORMAT_BC5_SNORM:
  141. self.pixel_format = "BC5S"
  142. n = 5
  143. self.mode = "RGB"
  144. elif dxgi_format in (DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM):
  145. self.pixel_format = "BC7"
  146. n = 7
  147. elif dxgi_format == DXGI_FORMAT_BC7_UNORM_SRGB:
  148. self.pixel_format = "BC7"
  149. self.info["gamma"] = 1 / 2.2
  150. n = 7
  151. elif dxgi_format in (
  152. DXGI_FORMAT_R8G8B8A8_TYPELESS,
  153. DXGI_FORMAT_R8G8B8A8_UNORM,
  154. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
  155. ):
  156. self.tile = [("raw", (0, 0) + self.size, 0, ("RGBA", 0, 1))]
  157. if dxgi_format == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
  158. self.info["gamma"] = 1 / 2.2
  159. return
  160. else:
  161. raise NotImplementedError(
  162. f"Unimplemented DXGI format {dxgi_format}"
  163. )
  164. else:
  165. raise NotImplementedError(f"Unimplemented pixel format {repr(fourcc)}")
  166. self.tile = [
  167. ("bcn", (0, 0) + self.size, data_start, (n, self.pixel_format))
  168. ]
  169. def load_seek(self, pos):
  170. pass
  171. def _save(im, fp, filename):
  172. if im.mode not in ("RGB", "RGBA"):
  173. raise OSError(f"cannot write mode {im.mode} as DDS")
  174. fp.write(
  175. o32(DDS_MAGIC)
  176. + o32(124) # header size
  177. + o32(
  178. DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PITCH | DDSD_PIXELFORMAT
  179. ) # flags
  180. + o32(im.height)
  181. + o32(im.width)
  182. + o32((im.width * (32 if im.mode == "RGBA" else 24) + 7) // 8) # pitch
  183. + o32(0) # depth
  184. + o32(0) # mipmaps
  185. + o32(0) * 11 # reserved
  186. + o32(32) # pfsize
  187. + o32(DDS_RGBA if im.mode == "RGBA" else DDPF_RGB) # pfflags
  188. + o32(0) # fourcc
  189. + o32(32 if im.mode == "RGBA" else 24) # bitcount
  190. + o32(0xFF0000) # rbitmask
  191. + o32(0xFF00) # gbitmask
  192. + o32(0xFF) # bbitmask
  193. + o32(0xFF000000 if im.mode == "RGBA" else 0) # abitmask
  194. + o32(DDSCAPS_TEXTURE) # dwCaps
  195. + o32(0) # dwCaps2
  196. + o32(0) # dwCaps3
  197. + o32(0) # dwCaps4
  198. + o32(0) # dwReserved2
  199. )
  200. if im.mode == "RGBA":
  201. r, g, b, a = im.split()
  202. im = Image.merge("RGBA", (a, r, g, b))
  203. ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (im.mode[::-1], 0, 1))])
  204. def _accept(prefix):
  205. return prefix[:4] == b"DDS "
  206. Image.register_open(DdsImageFile.format, DdsImageFile, _accept)
  207. Image.register_save(DdsImageFile.format, _save)
  208. Image.register_extension(DdsImageFile.format, ".dds")