BlpImagePlugin.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. """
  2. Blizzard Mipmap Format (.blp)
  3. Jerome Leclanche <jerome@leclan.ch>
  4. The contents of this file are hereby released in the public domain (CC0)
  5. Full text of the CC0 license:
  6. https://creativecommons.org/publicdomain/zero/1.0/
  7. BLP1 files, used mostly in Warcraft III, are not fully supported.
  8. All types of BLP2 files used in World of Warcraft are supported.
  9. The BLP file structure consists of a header, up to 16 mipmaps of the
  10. texture
  11. Texture sizes must be powers of two, though the two dimensions do
  12. not have to be equal; 512x256 is valid, but 512x200 is not.
  13. The first mipmap (mipmap #0) is the full size image; each subsequent
  14. mipmap halves both dimensions. The final mipmap should be 1x1.
  15. BLP files come in many different flavours:
  16. * JPEG-compressed (type == 0) - only supported for BLP1.
  17. * RAW images (type == 1, encoding == 1). Each mipmap is stored as an
  18. array of 8-bit values, one per pixel, left to right, top to bottom.
  19. Each value is an index to the palette.
  20. * DXT-compressed (type == 1, encoding == 2):
  21. - DXT1 compression is used if alpha_encoding == 0.
  22. - An additional alpha bit is used if alpha_depth == 1.
  23. - DXT3 compression is used if alpha_encoding == 1.
  24. - DXT5 compression is used if alpha_encoding == 7.
  25. """
  26. import os
  27. import struct
  28. from enum import IntEnum
  29. from io import BytesIO
  30. from . import Image, ImageFile
  31. class Format(IntEnum):
  32. JPEG = 0
  33. class Encoding(IntEnum):
  34. UNCOMPRESSED = 1
  35. DXT = 2
  36. UNCOMPRESSED_RAW_BGRA = 3
  37. class AlphaEncoding(IntEnum):
  38. DXT1 = 0
  39. DXT3 = 1
  40. DXT5 = 7
  41. def unpack_565(i):
  42. return ((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3
  43. def decode_dxt1(data, alpha=False):
  44. """
  45. input: one "row" of data (i.e. will produce 4*width pixels)
  46. """
  47. blocks = len(data) // 8 # number of blocks in row
  48. ret = (bytearray(), bytearray(), bytearray(), bytearray())
  49. for block in range(blocks):
  50. # Decode next 8-byte block.
  51. idx = block * 8
  52. color0, color1, bits = struct.unpack_from("<HHI", data, idx)
  53. r0, g0, b0 = unpack_565(color0)
  54. r1, g1, b1 = unpack_565(color1)
  55. # Decode this block into 4x4 pixels
  56. # Accumulate the results onto our 4 row accumulators
  57. for j in range(4):
  58. for i in range(4):
  59. # get next control op and generate a pixel
  60. control = bits & 3
  61. bits = bits >> 2
  62. a = 0xFF
  63. if control == 0:
  64. r, g, b = r0, g0, b0
  65. elif control == 1:
  66. r, g, b = r1, g1, b1
  67. elif control == 2:
  68. if color0 > color1:
  69. r = (2 * r0 + r1) // 3
  70. g = (2 * g0 + g1) // 3
  71. b = (2 * b0 + b1) // 3
  72. else:
  73. r = (r0 + r1) // 2
  74. g = (g0 + g1) // 2
  75. b = (b0 + b1) // 2
  76. elif control == 3:
  77. if color0 > color1:
  78. r = (2 * r1 + r0) // 3
  79. g = (2 * g1 + g0) // 3
  80. b = (2 * b1 + b0) // 3
  81. else:
  82. r, g, b, a = 0, 0, 0, 0
  83. if alpha:
  84. ret[j].extend([r, g, b, a])
  85. else:
  86. ret[j].extend([r, g, b])
  87. return ret
  88. def decode_dxt3(data):
  89. """
  90. input: one "row" of data (i.e. will produce 4*width pixels)
  91. """
  92. blocks = len(data) // 16 # number of blocks in row
  93. ret = (bytearray(), bytearray(), bytearray(), bytearray())
  94. for block in range(blocks):
  95. idx = block * 16
  96. block = data[idx : idx + 16]
  97. # Decode next 16-byte block.
  98. bits = struct.unpack_from("<8B", block)
  99. color0, color1 = struct.unpack_from("<HH", block, 8)
  100. (code,) = struct.unpack_from("<I", block, 12)
  101. r0, g0, b0 = unpack_565(color0)
  102. r1, g1, b1 = unpack_565(color1)
  103. for j in range(4):
  104. high = False # Do we want the higher bits?
  105. for i in range(4):
  106. alphacode_index = (4 * j + i) // 2
  107. a = bits[alphacode_index]
  108. if high:
  109. high = False
  110. a >>= 4
  111. else:
  112. high = True
  113. a &= 0xF
  114. a *= 17 # We get a value between 0 and 15
  115. color_code = (code >> 2 * (4 * j + i)) & 0x03
  116. if color_code == 0:
  117. r, g, b = r0, g0, b0
  118. elif color_code == 1:
  119. r, g, b = r1, g1, b1
  120. elif color_code == 2:
  121. r = (2 * r0 + r1) // 3
  122. g = (2 * g0 + g1) // 3
  123. b = (2 * b0 + b1) // 3
  124. elif color_code == 3:
  125. r = (2 * r1 + r0) // 3
  126. g = (2 * g1 + g0) // 3
  127. b = (2 * b1 + b0) // 3
  128. ret[j].extend([r, g, b, a])
  129. return ret
  130. def decode_dxt5(data):
  131. """
  132. input: one "row" of data (i.e. will produce 4 * width pixels)
  133. """
  134. blocks = len(data) // 16 # number of blocks in row
  135. ret = (bytearray(), bytearray(), bytearray(), bytearray())
  136. for block in range(blocks):
  137. idx = block * 16
  138. block = data[idx : idx + 16]
  139. # Decode next 16-byte block.
  140. a0, a1 = struct.unpack_from("<BB", block)
  141. bits = struct.unpack_from("<6B", block, 2)
  142. alphacode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24)
  143. alphacode2 = bits[0] | (bits[1] << 8)
  144. color0, color1 = struct.unpack_from("<HH", block, 8)
  145. (code,) = struct.unpack_from("<I", block, 12)
  146. r0, g0, b0 = unpack_565(color0)
  147. r1, g1, b1 = unpack_565(color1)
  148. for j in range(4):
  149. for i in range(4):
  150. # get next control op and generate a pixel
  151. alphacode_index = 3 * (4 * j + i)
  152. if alphacode_index <= 12:
  153. alphacode = (alphacode2 >> alphacode_index) & 0x07
  154. elif alphacode_index == 15:
  155. alphacode = (alphacode2 >> 15) | ((alphacode1 << 1) & 0x06)
  156. else: # alphacode_index >= 18 and alphacode_index <= 45
  157. alphacode = (alphacode1 >> (alphacode_index - 16)) & 0x07
  158. if alphacode == 0:
  159. a = a0
  160. elif alphacode == 1:
  161. a = a1
  162. elif a0 > a1:
  163. a = ((8 - alphacode) * a0 + (alphacode - 1) * a1) // 7
  164. elif alphacode == 6:
  165. a = 0
  166. elif alphacode == 7:
  167. a = 255
  168. else:
  169. a = ((6 - alphacode) * a0 + (alphacode - 1) * a1) // 5
  170. color_code = (code >> 2 * (4 * j + i)) & 0x03
  171. if color_code == 0:
  172. r, g, b = r0, g0, b0
  173. elif color_code == 1:
  174. r, g, b = r1, g1, b1
  175. elif color_code == 2:
  176. r = (2 * r0 + r1) // 3
  177. g = (2 * g0 + g1) // 3
  178. b = (2 * b0 + b1) // 3
  179. elif color_code == 3:
  180. r = (2 * r1 + r0) // 3
  181. g = (2 * g1 + g0) // 3
  182. b = (2 * b1 + b0) // 3
  183. ret[j].extend([r, g, b, a])
  184. return ret
  185. class BLPFormatError(NotImplementedError):
  186. pass
  187. def _accept(prefix):
  188. return prefix[:4] in (b"BLP1", b"BLP2")
  189. class BlpImageFile(ImageFile.ImageFile):
  190. """
  191. Blizzard Mipmap Format
  192. """
  193. format = "BLP"
  194. format_description = "Blizzard Mipmap Format"
  195. def _open(self):
  196. self.magic = self.fp.read(4)
  197. self.fp.seek(5, os.SEEK_CUR)
  198. (self._blp_alpha_depth,) = struct.unpack("<b", self.fp.read(1))
  199. self.fp.seek(2, os.SEEK_CUR)
  200. self._size = struct.unpack("<II", self.fp.read(8))
  201. if self.magic in (b"BLP1", b"BLP2"):
  202. decoder = self.magic.decode()
  203. else:
  204. msg = f"Bad BLP magic {repr(self.magic)}"
  205. raise BLPFormatError(msg)
  206. self._mode = "RGBA" if self._blp_alpha_depth else "RGB"
  207. self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))]
  208. class _BLPBaseDecoder(ImageFile.PyDecoder):
  209. _pulls_fd = True
  210. def decode(self, buffer):
  211. try:
  212. self._read_blp_header()
  213. self._load()
  214. except struct.error as e:
  215. msg = "Truncated BLP file"
  216. raise OSError(msg) from e
  217. return -1, 0
  218. def _read_blp_header(self):
  219. self.fd.seek(4)
  220. (self._blp_compression,) = struct.unpack("<i", self._safe_read(4))
  221. (self._blp_encoding,) = struct.unpack("<b", self._safe_read(1))
  222. (self._blp_alpha_depth,) = struct.unpack("<b", self._safe_read(1))
  223. (self._blp_alpha_encoding,) = struct.unpack("<b", self._safe_read(1))
  224. self.fd.seek(1, os.SEEK_CUR) # mips
  225. self.size = struct.unpack("<II", self._safe_read(8))
  226. if isinstance(self, BLP1Decoder):
  227. # Only present for BLP1
  228. (self._blp_encoding,) = struct.unpack("<i", self._safe_read(4))
  229. self.fd.seek(4, os.SEEK_CUR) # subtype
  230. self._blp_offsets = struct.unpack("<16I", self._safe_read(16 * 4))
  231. self._blp_lengths = struct.unpack("<16I", self._safe_read(16 * 4))
  232. def _safe_read(self, length):
  233. return ImageFile._safe_read(self.fd, length)
  234. def _read_palette(self):
  235. ret = []
  236. for i in range(256):
  237. try:
  238. b, g, r, a = struct.unpack("<4B", self._safe_read(4))
  239. except struct.error:
  240. break
  241. ret.append((b, g, r, a))
  242. return ret
  243. def _read_bgra(self, palette):
  244. data = bytearray()
  245. _data = BytesIO(self._safe_read(self._blp_lengths[0]))
  246. while True:
  247. try:
  248. (offset,) = struct.unpack("<B", _data.read(1))
  249. except struct.error:
  250. break
  251. b, g, r, a = palette[offset]
  252. d = (r, g, b)
  253. if self._blp_alpha_depth:
  254. d += (a,)
  255. data.extend(d)
  256. return data
  257. class BLP1Decoder(_BLPBaseDecoder):
  258. def _load(self):
  259. if self._blp_compression == Format.JPEG:
  260. self._decode_jpeg_stream()
  261. elif self._blp_compression == 1:
  262. if self._blp_encoding in (4, 5):
  263. palette = self._read_palette()
  264. data = self._read_bgra(palette)
  265. self.set_as_raw(bytes(data))
  266. else:
  267. msg = f"Unsupported BLP encoding {repr(self._blp_encoding)}"
  268. raise BLPFormatError(msg)
  269. else:
  270. msg = f"Unsupported BLP compression {repr(self._blp_encoding)}"
  271. raise BLPFormatError(msg)
  272. def _decode_jpeg_stream(self):
  273. from .JpegImagePlugin import JpegImageFile
  274. (jpeg_header_size,) = struct.unpack("<I", self._safe_read(4))
  275. jpeg_header = self._safe_read(jpeg_header_size)
  276. self._safe_read(self._blp_offsets[0] - self.fd.tell()) # What IS this?
  277. data = self._safe_read(self._blp_lengths[0])
  278. data = jpeg_header + data
  279. data = BytesIO(data)
  280. image = JpegImageFile(data)
  281. Image._decompression_bomb_check(image.size)
  282. if image.mode == "CMYK":
  283. decoder_name, extents, offset, args = image.tile[0]
  284. image.tile = [(decoder_name, extents, offset, (args[0], "CMYK"))]
  285. r, g, b = image.convert("RGB").split()
  286. image = Image.merge("RGB", (b, g, r))
  287. self.set_as_raw(image.tobytes())
  288. class BLP2Decoder(_BLPBaseDecoder):
  289. def _load(self):
  290. palette = self._read_palette()
  291. self.fd.seek(self._blp_offsets[0])
  292. if self._blp_compression == 1:
  293. # Uncompressed or DirectX compression
  294. if self._blp_encoding == Encoding.UNCOMPRESSED:
  295. data = self._read_bgra(palette)
  296. elif self._blp_encoding == Encoding.DXT:
  297. data = bytearray()
  298. if self._blp_alpha_encoding == AlphaEncoding.DXT1:
  299. linesize = (self.size[0] + 3) // 4 * 8
  300. for yb in range((self.size[1] + 3) // 4):
  301. for d in decode_dxt1(
  302. self._safe_read(linesize), alpha=bool(self._blp_alpha_depth)
  303. ):
  304. data += d
  305. elif self._blp_alpha_encoding == AlphaEncoding.DXT3:
  306. linesize = (self.size[0] + 3) // 4 * 16
  307. for yb in range((self.size[1] + 3) // 4):
  308. for d in decode_dxt3(self._safe_read(linesize)):
  309. data += d
  310. elif self._blp_alpha_encoding == AlphaEncoding.DXT5:
  311. linesize = (self.size[0] + 3) // 4 * 16
  312. for yb in range((self.size[1] + 3) // 4):
  313. for d in decode_dxt5(self._safe_read(linesize)):
  314. data += d
  315. else:
  316. msg = f"Unsupported alpha encoding {repr(self._blp_alpha_encoding)}"
  317. raise BLPFormatError(msg)
  318. else:
  319. msg = f"Unknown BLP encoding {repr(self._blp_encoding)}"
  320. raise BLPFormatError(msg)
  321. else:
  322. msg = f"Unknown BLP compression {repr(self._blp_compression)}"
  323. raise BLPFormatError(msg)
  324. self.set_as_raw(bytes(data))
  325. class BLPEncoder(ImageFile.PyEncoder):
  326. _pushes_fd = True
  327. def _write_palette(self):
  328. data = b""
  329. palette = self.im.getpalette("RGBA", "RGBA")
  330. for i in range(len(palette) // 4):
  331. r, g, b, a = palette[i * 4 : (i + 1) * 4]
  332. data += struct.pack("<4B", b, g, r, a)
  333. while len(data) < 256 * 4:
  334. data += b"\x00" * 4
  335. return data
  336. def encode(self, bufsize):
  337. palette_data = self._write_palette()
  338. offset = 20 + 16 * 4 * 2 + len(palette_data)
  339. data = struct.pack("<16I", offset, *((0,) * 15))
  340. w, h = self.im.size
  341. data += struct.pack("<16I", w * h, *((0,) * 15))
  342. data += palette_data
  343. for y in range(h):
  344. for x in range(w):
  345. data += struct.pack("<B", self.im.getpixel((x, y)))
  346. return len(data), 0, data
  347. def _save(im, fp, filename):
  348. if im.mode != "P":
  349. msg = "Unsupported BLP image mode"
  350. raise ValueError(msg)
  351. magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2"
  352. fp.write(magic)
  353. fp.write(struct.pack("<i", 1)) # Uncompressed or DirectX compression
  354. fp.write(struct.pack("<b", Encoding.UNCOMPRESSED))
  355. fp.write(struct.pack("<b", 1 if im.palette.mode == "RGBA" else 0))
  356. fp.write(struct.pack("<b", 0)) # alpha encoding
  357. fp.write(struct.pack("<b", 0)) # mips
  358. fp.write(struct.pack("<II", *im.size))
  359. if magic == b"BLP1":
  360. fp.write(struct.pack("<i", 5))
  361. fp.write(struct.pack("<i", 0))
  362. ImageFile._save(im, fp, [("BLP", (0, 0) + im.size, 0, im.mode)])
  363. Image.register_open(BlpImageFile.format, BlpImageFile, _accept)
  364. Image.register_extension(BlpImageFile.format, ".blp")
  365. Image.register_decoder("BLP1", BLP1Decoder)
  366. Image.register_decoder("BLP2", BLP2Decoder)
  367. Image.register_save(BlpImageFile.format, _save)
  368. Image.register_encoder("BLP", BLPEncoder)