2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2021-03-07 21:04:25 +03:00
|
|
|
import pytest
|
2018-02-17 03:46:29 +03:00
|
|
|
|
2023-04-02 21:19:11 +03:00
|
|
|
from PIL import Image
|
2021-04-01 17:41:46 +03:00
|
|
|
|
2022-02-25 08:54:53 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
|
|
|
assert_image_equal_tofile,
|
|
|
|
assert_image_similar,
|
|
|
|
hopper,
|
|
|
|
)
|
2018-02-17 03:46:29 +03:00
|
|
|
|
|
|
|
|
2022-02-17 14:20:11 +03:00
|
|
|
def test_load_blp1():
|
|
|
|
with Image.open("Tests/images/blp/blp1_jpeg.blp") as im:
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/blp/blp1_jpeg.png")
|
|
|
|
|
2022-10-15 14:30:53 +03:00
|
|
|
with Image.open("Tests/images/blp/blp1_jpeg2.blp") as im:
|
|
|
|
im.load()
|
|
|
|
|
2022-02-17 14:20:11 +03:00
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
def test_load_blp2_raw():
|
|
|
|
with Image.open("Tests/images/blp/blp2_raw.blp") as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/blp/blp2_raw.png")
|
2018-02-18 07:24:39 +03:00
|
|
|
|
|
|
|
|
2020-02-25 12:57:27 +03:00
|
|
|
def test_load_blp2_dxt1():
|
|
|
|
with Image.open("Tests/images/blp/blp2_dxt1.blp") as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/blp/blp2_dxt1.png")
|
2020-02-25 12:57:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_load_blp2_dxt1a():
|
|
|
|
with Image.open("Tests/images/blp/blp2_dxt1a.blp") as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/blp/blp2_dxt1a.png")
|
2021-03-07 21:00:17 +03:00
|
|
|
|
|
|
|
|
2022-02-25 08:53:53 +03:00
|
|
|
def test_save(tmp_path):
|
|
|
|
f = str(tmp_path / "temp.blp")
|
|
|
|
|
2022-02-25 15:58:13 +03:00
|
|
|
for version in ("BLP1", "BLP2"):
|
|
|
|
im = hopper("P")
|
|
|
|
im.save(f, blp_version=version)
|
2022-02-25 08:54:53 +03:00
|
|
|
|
|
|
|
with Image.open(f) as reloaded:
|
2022-02-25 15:58:13 +03:00
|
|
|
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)
|
2022-02-25 08:54:53 +03:00
|
|
|
|
2022-02-25 08:53:53 +03:00
|
|
|
im = hopper()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.save(f)
|
|
|
|
|
|
|
|
|
2021-03-07 21:00:17 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_file",
|
|
|
|
[
|
|
|
|
"Tests/images/timeout-060745d3f534ad6e4128c51d336ea5489182c69d.blp",
|
|
|
|
"Tests/images/timeout-31c8f86233ea728339c6e586be7af661a09b5b98.blp",
|
|
|
|
"Tests/images/timeout-60d8b7c8469d59fc9ffff6b3a3dc0faeae6ea8ee.blp",
|
|
|
|
"Tests/images/timeout-8073b430977660cdd48d96f6406ddfd4114e69c7.blp",
|
|
|
|
"Tests/images/timeout-bba4f2e026b5786529370e5dfe9a11b1bf991f07.blp",
|
|
|
|
"Tests/images/timeout-d6ec061c4afdef39d3edf6da8927240bb07fe9b7.blp",
|
|
|
|
"Tests/images/timeout-ef9112a065e7183fa7faa2e18929b03e44ee16bf.blp",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_crashes(test_file):
|
|
|
|
with open(test_file, "rb") as f:
|
|
|
|
with Image.open(f) as im:
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
im.load()
|