DdsImagePlugin.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. # Magic ("DDS ")
  14. DDS_MAGIC = 0x20534444
  15. # DDS flags
  16. DDSD_CAPS = 0x1
  17. DDSD_HEIGHT = 0x2
  18. DDSD_WIDTH = 0x4
  19. DDSD_PITCH = 0x8
  20. DDSD_PIXELFORMAT = 0x1000
  21. DDSD_MIPMAPCOUNT = 0x20000
  22. DDSD_LINEARSIZE = 0x80000
  23. DDSD_DEPTH = 0x800000
  24. # DDS caps
  25. DDSCAPS_COMPLEX = 0x8
  26. DDSCAPS_TEXTURE = 0x1000
  27. DDSCAPS_MIPMAP = 0x400000
  28. DDSCAPS2_CUBEMAP = 0x200
  29. DDSCAPS2_CUBEMAP_POSITIVEX = 0x400
  30. DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800
  31. DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000
  32. DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000
  33. DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000
  34. DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000
  35. DDSCAPS2_VOLUME = 0x200000
  36. # Pixel Format
  37. DDPF_ALPHAPIXELS = 0x1
  38. DDPF_ALPHA = 0x2
  39. DDPF_FOURCC = 0x4
  40. DDPF_PALETTEINDEXED8 = 0x20
  41. DDPF_RGB = 0x40
  42. DDPF_LUMINANCE = 0x20000
  43. # dds.h
  44. DDS_FOURCC = DDPF_FOURCC
  45. DDS_RGB = DDPF_RGB
  46. DDS_RGBA = DDPF_RGB | DDPF_ALPHAPIXELS
  47. DDS_LUMINANCE = DDPF_LUMINANCE
  48. DDS_LUMINANCEA = DDPF_LUMINANCE | DDPF_ALPHAPIXELS
  49. DDS_ALPHA = DDPF_ALPHA
  50. DDS_PAL8 = DDPF_PALETTEINDEXED8
  51. DDS_HEADER_FLAGS_TEXTURE = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT
  52. DDS_HEADER_FLAGS_MIPMAP = DDSD_MIPMAPCOUNT
  53. DDS_HEADER_FLAGS_VOLUME = DDSD_DEPTH
  54. DDS_HEADER_FLAGS_PITCH = DDSD_PITCH
  55. DDS_HEADER_FLAGS_LINEARSIZE = DDSD_LINEARSIZE
  56. DDS_HEIGHT = DDSD_HEIGHT
  57. DDS_WIDTH = DDSD_WIDTH
  58. DDS_SURFACE_FLAGS_TEXTURE = DDSCAPS_TEXTURE
  59. DDS_SURFACE_FLAGS_MIPMAP = DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
  60. DDS_SURFACE_FLAGS_CUBEMAP = DDSCAPS_COMPLEX
  61. DDS_CUBEMAP_POSITIVEX = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEX
  62. DDS_CUBEMAP_NEGATIVEX = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEX
  63. DDS_CUBEMAP_POSITIVEY = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEY
  64. DDS_CUBEMAP_NEGATIVEY = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEY
  65. DDS_CUBEMAP_POSITIVEZ = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_POSITIVEZ
  66. DDS_CUBEMAP_NEGATIVEZ = DDSCAPS2_CUBEMAP | DDSCAPS2_CUBEMAP_NEGATIVEZ
  67. # DXT1
  68. DXT1_FOURCC = 0x31545844
  69. # DXT3
  70. DXT3_FOURCC = 0x33545844
  71. # DXT5
  72. DXT5_FOURCC = 0x35545844
  73. # dxgiformat.h
  74. DXGI_FORMAT_BC7_TYPELESS = 97
  75. DXGI_FORMAT_BC7_UNORM = 98
  76. DXGI_FORMAT_BC7_UNORM_SRGB = 99
  77. class DdsImageFile(ImageFile.ImageFile):
  78. format = "DDS"
  79. format_description = "DirectDraw Surface"
  80. def _open(self):
  81. magic, header_size = struct.unpack("<II", self.fp.read(8))
  82. if header_size != 124:
  83. raise OSError(f"Unsupported header size {repr(header_size)}")
  84. header_bytes = self.fp.read(header_size - 4)
  85. if len(header_bytes) != 120:
  86. raise OSError(f"Incomplete header: {len(header_bytes)} bytes")
  87. header = BytesIO(header_bytes)
  88. flags, height, width = struct.unpack("<3I", header.read(12))
  89. self._size = (width, height)
  90. self.mode = "RGBA"
  91. pitch, depth, mipmaps = struct.unpack("<3I", header.read(12))
  92. struct.unpack("<11I", header.read(44)) # reserved
  93. # pixel format
  94. pfsize, pfflags = struct.unpack("<2I", header.read(8))
  95. fourcc = header.read(4)
  96. (bitcount,) = struct.unpack("<I", header.read(4))
  97. masks = struct.unpack("<4I", header.read(16))
  98. if pfflags & 0x40:
  99. # DDPF_RGB - Texture contains uncompressed RGB data
  100. masks = {mask: ["R", "G", "B", "A"][i] for i, mask in enumerate(masks)}
  101. rawmode = ""
  102. if bitcount == 32:
  103. rawmode += masks[0xFF000000]
  104. rawmode += masks[0xFF0000] + masks[0xFF00] + masks[0xFF]
  105. self.tile = [("raw", (0, 0) + self.size, 0, (rawmode, 0, 1))]
  106. else:
  107. data_start = header_size + 4
  108. n = 0
  109. if fourcc == b"DXT1":
  110. self.pixel_format = "DXT1"
  111. n = 1
  112. elif fourcc == b"DXT3":
  113. self.pixel_format = "DXT3"
  114. n = 2
  115. elif fourcc == b"DXT5":
  116. self.pixel_format = "DXT5"
  117. n = 3
  118. elif fourcc == b"DX10":
  119. data_start += 20
  120. # ignoring flags which pertain to volume textures and cubemaps
  121. dxt10 = BytesIO(self.fp.read(20))
  122. dxgi_format, dimension = struct.unpack("<II", dxt10.read(8))
  123. if dxgi_format in (DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM):
  124. self.pixel_format = "BC7"
  125. n = 7
  126. elif dxgi_format == DXGI_FORMAT_BC7_UNORM_SRGB:
  127. self.pixel_format = "BC7"
  128. self.info["gamma"] = 1 / 2.2
  129. n = 7
  130. else:
  131. raise NotImplementedError(
  132. f"Unimplemented DXGI format {dxgi_format}"
  133. )
  134. else:
  135. raise NotImplementedError(f"Unimplemented pixel format {repr(fourcc)}")
  136. self.tile = [("bcn", (0, 0) + self.size, data_start, (n))]
  137. def load_seek(self, pos):
  138. pass
  139. def _validate(prefix):
  140. return prefix[:4] == b"DDS "
  141. Image.register_open(DdsImageFile.format, DdsImageFile, _validate)
  142. Image.register_extension(DdsImageFile.format, ".dds")