Skip to content

Commit

Permalink
Added BLP1 saving
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 25, 2022
1 parent db8489a commit d61470b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 11 additions & 9 deletions Tests/test_file_blp.py
Expand Up @@ -31,19 +31,21 @@ def test_load_blp2_dxt1a():


def test_save(tmp_path):
im = hopper("P")
f = str(tmp_path / "temp.blp")
im.save(f)

with Image.open(f) as reloaded:
assert_image_equal(im.convert("RGB"), reloaded)

with Image.open("Tests/images/transparent.png") as im:
f = str(tmp_path / "temp.blp")
im.convert("P").save(f)
for version in ("BLP1", "BLP2"):
im = hopper("P")
im.save(f, blp_version=version)

with Image.open(f) as reloaded:
assert_image_similar(im, reloaded, 8)
assert_image_equal(im.convert("RGB"), reloaded)

with Image.open("Tests/images/transparent.png") as im:
f = str(tmp_path / "temp.blp")
im.convert("P").save(f, blp_version=version)

with Image.open(f) as reloaded:
assert_image_similar(im, reloaded, 8)

im = hopper()
with pytest.raises(ValueError):
Expand Down
7 changes: 6 additions & 1 deletion src/PIL/BlpImagePlugin.py
Expand Up @@ -479,13 +479,18 @@ def _save(im, fp, filename, save_all=False):
if im.mode != "P":
raise ValueError("Unsupported BLP image mode")

fp.write(b"BLP2")
magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2"
fp.write(magic)

fp.write(struct.pack("<i", 1)) # Uncompressed or DirectX compression
fp.write(struct.pack("<b", Encoding.UNCOMPRESSED))
fp.write(struct.pack("<b", 1 if im.palette.mode == "RGBA" else 0))
fp.write(struct.pack("<b", 0)) # alpha encoding
fp.write(struct.pack("<b", 0)) # mips
fp.write(struct.pack("<II", *im.size))
if magic == b"BLP1":
fp.write(struct.pack("<i", 5))
fp.write(struct.pack("<i", 0))

ImageFile._save(im, fp, [("BLP2", (0, 0) + im.size, 0, im.mode)])

Expand Down

0 comments on commit d61470b

Please sign in to comment.