[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2022-08-13 22:41:15 +00:00 committed by REDxEYE
parent f368f30597
commit 53e1831617
2 changed files with 79 additions and 73 deletions

View File

@ -3,86 +3,90 @@ from typing import Tuple
import pytest import pytest
from PIL import Image from PIL import Image
from PIL.VtfImagePlugin import _closest_power, _get_mipmap_count, _get_texture_size from PIL.VtfImagePlugin import (
from PIL.VtfImagePlugin import VtfPF VtfPF,
from .helper import assert_image_similar, assert_image_equal _closest_power,
_get_mipmap_count,
_get_texture_size,
)
from .helper import assert_image_equal, assert_image_similar
@pytest.mark.parametrize(("size", "expected_size"), @pytest.mark.parametrize(
[ ("size", "expected_size"),
(8, 8), [
(7, 8), (8, 8),
(9, 8), (7, 8),
(192, 256), (9, 8),
(1, 1), (192, 256),
(2000, 2048), (1, 1),
], ) (2000, 2048),
],
)
def test_closest_power(size: int, expected_size: int): def test_closest_power(size: int, expected_size: int):
assert _closest_power(size) == expected_size assert _closest_power(size) == expected_size
@pytest.mark.parametrize(("size", "expected_count"), @pytest.mark.parametrize(
[ ("size", "expected_count"),
((1, 1), 1), [
((2, 2), 2), ((1, 1), 1),
((4, 4), 3), ((2, 2), 2),
((8, 8), 4), ((4, 4), 3),
((128, 128), 8), ((8, 8), 4),
((256, 256), 9), ((128, 128), 8),
((512, 512), 10), ((256, 256), 9),
((1024, 1024), 11), ((512, 512), 10),
((1024, 1), 11), ((1024, 1024), 11),
], ) ((1024, 1), 11),
],
)
def test_get_mipmap_count(size: Tuple[int, int], expected_count: int): def test_get_mipmap_count(size: Tuple[int, int], expected_count: int):
assert _get_mipmap_count(*size) == expected_count assert _get_mipmap_count(*size) == expected_count
@pytest.mark.parametrize(("pixel_format", "size", "expected_size"), @pytest.mark.parametrize(
[ ("pixel_format", "size", "expected_size"),
(VtfPF.DXT1, (16, 16), (16 * 16) // 2), [
(VtfPF.DXT1_ONEBITALPHA, (16, 16), (16 * 16) // 2), (VtfPF.DXT1, (16, 16), (16 * 16) // 2),
(VtfPF.DXT3, (16, 16), 16 * 16), (VtfPF.DXT1_ONEBITALPHA, (16, 16), (16 * 16) // 2),
(VtfPF.DXT5, (16, 16), 16 * 16), (VtfPF.DXT3, (16, 16), 16 * 16),
(VtfPF.BGR888, (16, 16), 16 * 16 * 3), (VtfPF.DXT5, (16, 16), 16 * 16),
(VtfPF.RGB888, (16, 16), 16 * 16 * 3), (VtfPF.BGR888, (16, 16), 16 * 16 * 3),
(VtfPF.RGBA8888, (16, 16), 16 * 16 * 4), (VtfPF.RGB888, (16, 16), 16 * 16 * 3),
(VtfPF.UV88, (16, 16), 16 * 16 * 2), (VtfPF.RGBA8888, (16, 16), 16 * 16 * 4),
(VtfPF.A8, (16, 16), 16 * 16), (VtfPF.UV88, (16, 16), 16 * 16 * 2),
(VtfPF.I8, (16, 16), 16 * 16), (VtfPF.A8, (16, 16), 16 * 16),
(VtfPF.IA88, (16, 16), 16 * 16 * 2), (VtfPF.I8, (16, 16), 16 * 16),
], ) (VtfPF.IA88, (16, 16), 16 * 16 * 2),
def test_get_texture_size(pixel_format: VtfPF, size: Tuple[int, int], ],
expected_size: int): )
def test_get_texture_size(
pixel_format: VtfPF, size: Tuple[int, int], expected_size: int
):
assert _get_texture_size(pixel_format, *size) == expected_size assert _get_texture_size(pixel_format, *size) == expected_size
@pytest.mark.parametrize(("etalon_path", "file_path", "expected_mode", "epsilon"), @pytest.mark.parametrize(
[ ("etalon_path", "file_path", "expected_mode", "epsilon"),
("Tests/images/vtf_i8.png", "Tests/images/vtf_i8.vtf", "L", [
0.0), ("Tests/images/vtf_i8.png", "Tests/images/vtf_i8.vtf", "L", 0.0),
("Tests/images/vtf_a8.png", "Tests/images/vtf_a8.vtf", "L", ("Tests/images/vtf_a8.png", "Tests/images/vtf_a8.vtf", "L", 0.0),
0.0), ("Tests/images/vtf_ia88.png", "Tests/images/vtf_ia88.vtf", "LA", 0.0),
( # (
"Tests/images/vtf_ia88.png", # "Tests/images/vtf_RG.png",
"Tests/images/vtf_ia88.vtf", "LA", # "Tests/images/vtf_RG.vtf", "RGB",
0.0), # 0.0),
# ( ("Tests/images/vtf_rgb888.png", "Tests/images/vtf_rgb888.vtf", "RGB", 0.0),
# "Tests/images/vtf_RG.png", ("Tests/images/vtf_bgr888.png", "Tests/images/vtf_bgr888.vtf", "RGB", 0.0),
# "Tests/images/vtf_RG.vtf", "RGB", ("Tests/images/vtf_dxt1.png", "Tests/images/vtf_dxt1.vtf", "RGBA", 3.0),
# 0.0), ("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA", 0.1),
("Tests/images/vtf_rgb888.png", "Tests/images/vtf_rgb888.vtf", ],
"RGB", )
0.0), def test_vtf_loading(
("Tests/images/vtf_bgr888.png", "Tests/images/vtf_bgr888.vtf", etalon_path: str, file_path: str, expected_mode: str, epsilon: float
"RGB", ):
0.0),
("Tests/images/vtf_dxt1.png", "Tests/images/vtf_dxt1.vtf", "RGBA",
3.0),
("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA",
0.1),
], )
def test_vtf_loading(etalon_path: str, file_path: str, expected_mode: str,
epsilon: float):
e = Image.open(etalon_path) e = Image.open(etalon_path)
f = Image.open(file_path) f = Image.open(file_path)
assert f.mode == expected_mode assert f.mode == expected_mode

View File

@ -125,10 +125,12 @@ VTFHeader = NamedTuple(
("resource_count", int), ("resource_count", int),
], ],
) )
RGB_FORMATS = (VtfPF.DXT1, RGB_FORMATS = (
VtfPF.RGB888, VtfPF.DXT1,
VtfPF.BGR888, VtfPF.RGB888,
VtfPF.UV88,) VtfPF.BGR888,
VtfPF.UV88,
)
RGBA_FORMATS = ( RGBA_FORMATS = (
VtfPF.DXT1_ONEBITALPHA, VtfPF.DXT1_ONEBITALPHA,
VtfPF.DXT3, VtfPF.DXT3,
@ -230,11 +232,11 @@ def _write_image(fp: BufferedIOBase, im: Image.Image, pixel_format: VtfPF):
encoder = "raw" encoder = "raw"
if im.mode == "RGB" or im.mode == "RGBA": if im.mode == "RGB" or im.mode == "RGBA":
r, g, *_ = im.split() r, g, *_ = im.split()
im = Image.merge('LA', (r, g)) im = Image.merge("LA", (r, g))
elif im.mode == "LA": elif im.mode == "LA":
pass pass
else: else:
raise VTFException(f'Cannot encode {im.mode} as {pixel_format}') raise VTFException(f"Cannot encode {im.mode} as {pixel_format}")
encoder_args = ("LA", 0, 0) encoder_args = ("LA", 0, 0)
else: else:
raise VTFException(f"Unsupported pixel format: {pixel_format!r}") raise VTFException(f"Unsupported pixel format: {pixel_format!r}")