Skip to content

Commit e367746

Browse files
committed
Added BLP1 saving
1 parent 1859bc3 commit e367746

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

Tests/test_file_blp.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ def test_load_blp2_dxt1a():
3131

3232

3333
def test_save(tmp_path):
34-
im = hopper("P")
3534
f = str(tmp_path / "temp.blp")
36-
im.save(f)
3735

38-
with Image.open(f) as reloaded:
39-
assert_image_equal(im.convert("RGB"), reloaded)
40-
41-
with Image.open("Tests/images/transparent.png") as im:
42-
f = str(tmp_path / "temp.blp")
43-
im.convert("P").save(f)
36+
for version in ("BLP1", "BLP2"):
37+
im = hopper("P")
38+
im.save(f, blp_version=version)
4439

4540
with Image.open(f) as reloaded:
46-
assert_image_similar(im, reloaded, 8)
41+
assert_image_equal(im.convert("RGB"), reloaded)
42+
43+
with Image.open("Tests/images/transparent.png") as im:
44+
f = str(tmp_path / "temp.blp")
45+
im.convert("P").save(f, blp_version=version)
46+
47+
with Image.open(f) as reloaded:
48+
assert_image_similar(im, reloaded, 8)
4749

4850
im = hopper()
4951
with pytest.raises(ValueError):

docs/handbook/image-file-formats.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ Fully supported formats
2626

2727
.. contents::
2828

29+
BLP
30+
^^^
31+
32+
BLP is the Blizzard Mipmap Format, a texture format used in World of
33+
Warcraft. Pillow supports reading ``JPEG`` Compressed or raw ``BLP1``
34+
images, and all types of ``BLP2`` images.
35+
36+
Pillow supports writing BLP images. The :py:meth:`~PIL.Image.Image.save` method
37+
can take the following keyword arguments:
38+
39+
**blp_version**
40+
If present and set to "BLP1", images will be saved as BLP1. Otherwise, images
41+
will be saved as BLP2.
42+
2943
BMP
3044
^^^
3145

@@ -1042,13 +1056,6 @@ Pillow reads and writes X bitmap files (mode ``1``).
10421056
Read-only formats
10431057
-----------------
10441058

1045-
BLP
1046-
^^^
1047-
1048-
BLP is the Blizzard Mipmap Format, a texture format used in World of
1049-
Warcraft. Pillow supports reading ``JPEG`` Compressed or raw ``BLP1``
1050-
images, and all types of ``BLP2`` images.
1051-
10521059
CUR
10531060
^^^
10541061

src/PIL/BlpImagePlugin.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def _load(self):
439439
self.set_as_raw(bytes(data))
440440

441441

442-
class BLP2Encoder(ImageFile.PyEncoder):
442+
class BLPEncoder(ImageFile.PyEncoder):
443443
_pushes_fd = True
444444

445445
def _write_palette(self):
@@ -472,15 +472,20 @@ def _save(im, fp, filename, save_all=False):
472472
if im.mode != "P":
473473
raise ValueError("Unsupported BLP image mode")
474474

475-
fp.write(b"BLP2")
475+
magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2"
476+
fp.write(magic)
477+
476478
fp.write(struct.pack("<i", 1)) # Uncompressed or DirectX compression
477479
fp.write(struct.pack("<b", Encoding.UNCOMPRESSED))
478480
fp.write(struct.pack("<b", 1 if im.palette.mode == "RGBA" else 0))
479481
fp.write(struct.pack("<b", 0)) # alpha encoding
480482
fp.write(struct.pack("<b", 0)) # mips
481483
fp.write(struct.pack("<II", *im.size))
484+
if magic == b"BLP1":
485+
fp.write(struct.pack("<i", 5))
486+
fp.write(struct.pack("<i", 0))
482487

483-
ImageFile._save(im, fp, [("BLP2", (0, 0) + im.size, 0, im.mode)])
488+
ImageFile._save(im, fp, [("BLP", (0, 0) + im.size, 0, im.mode)])
484489

485490

486491
Image.register_open(BlpImageFile.format, BlpImageFile, _accept)
@@ -489,4 +494,4 @@ def _save(im, fp, filename, save_all=False):
489494
Image.register_decoder("BLP2", BLP2Decoder)
490495

491496
Image.register_save(BlpImageFile.format, _save)
492-
Image.register_encoder("BLP2", BLP2Encoder)
497+
Image.register_encoder("BLP", BLPEncoder)

0 commit comments

Comments
 (0)