Fixed reading uncompressed BLP2 with alpha

This commit is contained in:
Andrew Murray 2022-02-25 16:54:53 +11:00
parent 169025df6c
commit 3ec928251c
2 changed files with 17 additions and 2 deletions

View File

@ -2,7 +2,12 @@ import pytest
from PIL import BlpImagePlugin, Image
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
from .helper import (
assert_image_equal,
assert_image_equal_tofile,
assert_image_similar,
hopper,
)
def test_load_blp1():
@ -33,6 +38,13 @@ def test_save(tmp_path):
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)
with Image.open(f) as reloaded:
assert_image_similar(im, reloaded, 8)
im = hopper()
with pytest.raises(ValueError):
im.save(f)

View File

@ -406,7 +406,10 @@ class BLP2Decoder(_BLPBaseDecoder):
except struct.error:
break
b, g, r, a = palette[offset]
data.extend((r, g, b))
d = (r, g, b)
if self._blp_alpha_depth:
d += (a,)
data.extend(d)
elif self._blp_encoding == Encoding.DXT:
if self._blp_alpha_encoding == AlphaEncoding.DXT1: