Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and REDxEYE committed Dec 26, 2022
1 parent c3457f1 commit 906890b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
11 changes: 5 additions & 6 deletions Tests/test_file_vtf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_get_mipmap_count(size: Tuple[int, int], expected_count: int):
],
)
def test_get_texture_size(
pixel_format: VtfPF, size: Tuple[int, int], expected_size: int
pixel_format: VtfPF, size: Tuple[int, int], expected_size: int
):
assert _get_texture_size(pixel_format, *size) == expected_size

Expand All @@ -82,9 +82,7 @@ def test_get_texture_size(
("Tests/images/vtf_rgba8888.png", "Tests/images/vtf_rgba8888.vtf", "RGBA", 0),
],
)
def test_vtf_read(
etalon_path: str, file_path: str, expected_mode: str, epsilon: float
):
def test_vtf_read(etalon_path: str, file_path: str, expected_mode: str, epsilon: float):
e = Image.open(etalon_path)
f = Image.open(file_path)
assert f.mode == expected_mode
Expand All @@ -109,8 +107,9 @@ def test_vtf_read(
(VtfPF.RGBA8888, "Tests/images/vtf_rgba8888.png", "RGBA", 0),
],
)
def test_vtf_save(pixel_format: VtfPF, file_path: str,
expected_mode: str, epsilon: float, tmp_path):
def test_vtf_save(
pixel_format: VtfPF, file_path: str, expected_mode: str, epsilon: float, tmp_path
):
f: Image.Image = Image.open(file_path)
out = (tmp_path / "tmp.vtf").as_posix()
f.save(out, pixel_format=pixel_format)
Expand Down
29 changes: 22 additions & 7 deletions src/PIL/VtfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ def _get_texture_size(pixel_format: VtfPF, width, height):
return width * height // 2
elif pixel_format in (VtfPF.DXT3, VtfPF.DXT5):
return width * height
elif pixel_format in (VtfPF.A8, VtfPF.I8,):
elif pixel_format in (
VtfPF.A8,
VtfPF.I8,
):
return width * height
elif pixel_format in (VtfPF.UV88, VtfPF.IA88):
return width * height * 2
Expand Down Expand Up @@ -205,7 +208,7 @@ def _write_image(fp: BufferedIOBase, im: Image.Image, pixel_format: VtfPF):

def _closest_power(x):
possible_results = round(log(x, 2)), ceil(log(x, 2))
return 2 ** min(possible_results, key=lambda z: abs(x - 2 ** z))
return 2 ** min(possible_results, key=lambda z: abs(x - 2**z))


class VtfImageFile(ImageFile.ImageFile):
Expand Down Expand Up @@ -246,8 +249,15 @@ def _open(self):
# flags = CompiledVtfFlags(header.flags)
pixel_format = VtfPF(header.pixel_format)
low_format = VtfPF(header.low_pixel_format)
if pixel_format in (VtfPF.DXT1_ONEBITALPHA, VtfPF.DXT1, VtfPF.DXT3, VtfPF.DXT5,
VtfPF.RGBA8888, VtfPF.BGRA8888,VtfPF.A8):
if pixel_format in (
VtfPF.DXT1_ONEBITALPHA,
VtfPF.DXT1,
VtfPF.DXT3,
VtfPF.DXT5,
VtfPF.RGBA8888,
VtfPF.BGRA8888,
VtfPF.A8,
):
self.mode = "RGBA"
elif pixel_format in (VtfPF.RGB888, VtfPF.BGR888, VtfPF.UV88):
self.mode = "RGB"
Expand Down Expand Up @@ -309,9 +319,14 @@ def _save(im, fp, filename):

if pixel_format == VtfPF.DXT1_ONEBITALPHA:
flags |= CompiledVtfFlags.ONEBITALPHA
elif pixel_format in (VtfPF.DXT3, VtfPF.DXT5,
VtfPF.RGBA8888, VtfPF.BGRA8888,
VtfPF.A8, VtfPF.IA88):
elif pixel_format in (
VtfPF.DXT3,
VtfPF.DXT5,
VtfPF.RGBA8888,
VtfPF.BGRA8888,
VtfPF.A8,
VtfPF.IA88,
):
flags |= CompiledVtfFlags.EIGHTBITALPHA
else:
pass
Expand Down

0 comments on commit 906890b

Please sign in to comment.