You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.0 KiB

4 years ago
  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 |
  52. 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_BC7_TYPELESS = 97
  76. DXGI_FORMAT_BC7_UNORM = 98
  77. DXGI_FORMAT_BC7_UNORM_SRGB = 99
  78. class DdsImageFile(ImageFile.ImageFile):
  79. format = "DDS"
  80. format_description = "DirectDraw Surface"
  81. def _open(self):
  82. magic, header_size = struct.unpack("<II", self.fp.read(8))
  83. if header_size != 124:
  84. raise IOError("Unsupported header size %r" % (header_size))
  85. header_bytes = self.fp.read(header_size - 4)
  86. if len(header_bytes) != 120:
  87. raise IOError("Incomplete header: %s bytes" % len(header_bytes))
  88. header = BytesIO(header_bytes)
  89. flags, height, width = struct.unpack("<3I", header.read(12))
  90. self._size = (width, height)
  91. self.mode = "RGBA"
  92. pitch, depth, mipmaps = struct.unpack("<3I", header.read(12))
  93. reserved = struct.unpack("<11I", header.read(44))
  94. # pixel format
  95. pfsize, pfflags = struct.unpack("<2I", header.read(8))
  96. fourcc = header.read(4)
  97. bitcount, rmask, gmask, bmask, amask = struct.unpack("<5I",
  98. header.read(20))
  99. data_start = header_size + 4
  100. n = 0
  101. if fourcc == b"DXT1":
  102. self.pixel_format = "DXT1"
  103. n = 1
  104. elif fourcc == b"DXT3":
  105. self.pixel_format = "DXT3"
  106. n = 2
  107. elif fourcc == b"DXT5":
  108. self.pixel_format = "DXT5"
  109. n = 3
  110. elif fourcc == b"DX10":
  111. data_start += 20
  112. # ignoring flags which pertain to volume textures and cubemaps
  113. dxt10 = BytesIO(self.fp.read(20))
  114. dxgi_format, dimension = struct.unpack("<II", dxt10.read(8))
  115. if dxgi_format in (DXGI_FORMAT_BC7_TYPELESS,
  116. DXGI_FORMAT_BC7_UNORM):
  117. self.pixel_format = "BC7"
  118. n = 7
  119. elif dxgi_format == DXGI_FORMAT_BC7_UNORM_SRGB:
  120. self.pixel_format = "BC7"
  121. self.im_info["gamma"] = 1/2.2
  122. n = 7
  123. else:
  124. raise NotImplementedError("Unimplemented DXGI format %d" %
  125. (dxgi_format))
  126. else:
  127. raise NotImplementedError("Unimplemented pixel format %r" %
  128. (fourcc))
  129. self.tile = [
  130. ("bcn", (0, 0) + self.size, data_start, (n))
  131. ]
  132. def load_seek(self, pos):
  133. pass
  134. def _validate(prefix):
  135. return prefix[:4] == b"DDS "
  136. Image.register_open(DdsImageFile.format, DdsImageFile, _validate)
  137. Image.register_extension(DdsImageFile.format, ".dds")