Pillow/Tests/test_file_dds.py

231 lines
6.1 KiB
Python
Raw Normal View History

2020-02-12 19:29:19 +03:00
"""Test DdsImagePlugin"""
2016-01-16 00:34:36 +03:00
from io import BytesIO
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
import pytest
from PIL import DdsImagePlugin, Image
from .helper import assert_image_equal, assert_image_equal_tofile
2016-01-06 11:59:37 +03:00
TEST_FILE_DXT1 = "Tests/images/dxt1-rgb-4bbp-noalpha_MipMaps-1.dds"
2016-01-06 12:59:47 +03:00
TEST_FILE_DXT3 = "Tests/images/dxt3-argb-8bbp-explicitalpha_MipMaps-1.dds"
2016-01-06 11:59:37 +03:00
TEST_FILE_DXT5 = "Tests/images/dxt5-argb-8bbp-interpolatedalpha_MipMaps-1.dds"
2021-05-17 11:57:23 +03:00
TEST_FILE_DX10_BC5 = "Tests/images/bc5_unorm.dds"
2016-08-26 10:26:03 +03:00
TEST_FILE_DX10_BC7 = "Tests/images/bc7-argb-8bpp_MipMaps-1.dds"
TEST_FILE_DX10_BC7_UNORM_SRGB = "Tests/images/DXGI_FORMAT_BC7_UNORM_SRGB.dds"
2020-07-06 11:55:06 +03:00
TEST_FILE_DX10_R8G8B8A8 = "Tests/images/argb-32bpp_MipMaps-1.dds"
TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB = "Tests/images/DXGI_FORMAT_R8G8B8A8_UNORM_SRGB.dds"
TEST_FILE_UNCOMPRESSED_RGB = "Tests/images/hopper.dds"
TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA = "Tests/images/uncompressed_rgb.dds"
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
def test_sanity_dxt1():
"""Check DXT1 images can be opened"""
with Image.open(TEST_FILE_DXT1.replace(".dds", ".png")) as target:
target = target.convert("RGBA")
with Image.open(TEST_FILE_DXT1) as im:
im.load()
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)
assert_image_equal(im, target)
def test_sanity_dxt5():
"""Check DXT5 images can be opened"""
with Image.open(TEST_FILE_DXT5) as im:
im.load()
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)
2016-02-05 01:57:13 +03:00
assert_image_equal_tofile(im, TEST_FILE_DXT5.replace(".dds", ".png"))
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
def test_sanity_dxt3():
"""Check DXT3 images can be opened"""
with Image.open(TEST_FILE_DXT3) as im:
im.load()
2016-01-06 11:59:37 +03:00
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)
assert_image_equal_tofile(im, TEST_FILE_DXT3.replace(".dds", ".png"))
2016-01-08 18:59:18 +03:00
2016-08-26 04:12:44 +03:00
2021-05-17 11:57:23 +03:00
def test_dx10_bc5():
"""Check DX10 images can be opened"""
with Image.open(TEST_FILE_DX10_BC5) as im:
im.load()
assert im.format == "DDS"
assert im.mode == "RGB"
assert im.size == (256, 256)
assert_image_equal_tofile(im, TEST_FILE_DX10_BC5.replace(".dds", ".png"))
2020-02-12 19:29:19 +03:00
def test_dx10_bc7():
"""Check DX10 images can be opened"""
2016-08-26 04:12:44 +03:00
2020-02-12 19:29:19 +03:00
with Image.open(TEST_FILE_DX10_BC7) as im:
im.load()
2016-08-26 04:12:44 +03:00
2020-02-12 19:29:19 +03:00
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)
2016-01-06 12:59:47 +03:00
assert_image_equal_tofile(im, TEST_FILE_DX10_BC7.replace(".dds", ".png"))
2016-08-26 10:26:03 +03:00
2020-02-12 19:29:19 +03:00
def test_dx10_bc7_unorm_srgb():
"""Check DX10 unsigned normalized integer images can be opened"""
2016-08-26 10:26:03 +03:00
2020-02-12 19:29:19 +03:00
with Image.open(TEST_FILE_DX10_BC7_UNORM_SRGB) as im:
im.load()
2016-08-26 10:26:03 +03:00
2020-02-12 19:29:19 +03:00
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (16, 16)
assert im.info["gamma"] == 1 / 2.2
assert_image_equal_tofile(
im, TEST_FILE_DX10_BC7_UNORM_SRGB.replace(".dds", ".png")
)
2020-07-06 11:55:06 +03:00
def test_dx10_r8g8b8a8():
"""Check DX10 images can be opened"""
with Image.open(TEST_FILE_DX10_R8G8B8A8) as im:
im.load()
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (256, 256)
assert_image_equal_tofile(im, TEST_FILE_DX10_R8G8B8A8.replace(".dds", ".png"))
2020-07-06 11:55:06 +03:00
def test_dx10_r8g8b8a8_unorm_srgb():
"""Check DX10 unsigned normalized integer images can be opened"""
with Image.open(TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB) as im:
im.load()
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (16, 16)
assert im.info["gamma"] == 1 / 2.2
assert_image_equal_tofile(
im, TEST_FILE_DX10_R8G8B8A8_UNORM_SRGB.replace(".dds", ".png")
)
2020-07-06 11:55:06 +03:00
2020-02-12 19:29:19 +03:00
def test_unimplemented_dxgi_format():
with pytest.raises(NotImplementedError):
2021-02-11 13:43:54 +03:00
with Image.open("Tests/images/unimplemented_dxgi_format.dds"):
pass
2019-02-23 00:53:45 +03:00
2020-02-12 19:29:19 +03:00
def test_uncompressed_rgb():
"""Check uncompressed RGB images can be opened"""
2019-02-22 22:44:06 +03:00
# convert -format dds -define dds:compression=none hopper.jpg hopper.dds
2020-02-12 19:29:19 +03:00
with Image.open(TEST_FILE_UNCOMPRESSED_RGB) as im:
assert im.format == "DDS"
assert im.mode == "RGB"
assert im.size == (128, 128)
assert_image_equal_tofile(im, "Tests/images/hopper.png")
2020-02-12 19:29:19 +03:00
# Test image with alpha
with Image.open(TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA) as im:
2020-02-12 19:29:19 +03:00
assert im.format == "DDS"
assert im.mode == "RGBA"
assert im.size == (800, 600)
assert_image_equal_tofile(
im, TEST_FILE_UNCOMPRESSED_RGB_WITH_ALPHA.replace(".dds", ".png")
)
2019-02-22 22:44:06 +03:00
def test__accept_true():
2020-02-12 19:29:19 +03:00
"""Check valid prefix"""
# Arrange
prefix = b"DDS etc"
2019-02-22 22:44:06 +03:00
2020-02-12 19:29:19 +03:00
# Act
output = DdsImagePlugin._accept(prefix)
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
# Assert
assert output
2016-01-06 11:59:37 +03:00
def test__accept_false():
2020-02-12 19:29:19 +03:00
"""Check invalid prefix"""
# Arrange
prefix = b"something invalid"
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
# Act
output = DdsImagePlugin._accept(prefix)
2016-01-06 11:59:37 +03:00
2020-02-12 19:29:19 +03:00
# Assert
assert not output
2016-01-06 11:59:37 +03:00
2016-01-16 00:34:36 +03:00
2020-02-12 19:29:19 +03:00
def test_short_header():
""" Check a short header"""
with open(TEST_FILE_DXT5, "rb") as f:
img_file = f.read()
2016-01-16 00:34:36 +03:00
2020-02-12 19:29:19 +03:00
def short_header():
2021-02-11 13:43:54 +03:00
with Image.open(BytesIO(img_file[:119])):
2021-02-13 23:58:16 +03:00
pass # pragma: no cover
2016-01-16 00:34:36 +03:00
with pytest.raises(OSError):
2020-02-12 19:29:19 +03:00
short_header()
2016-02-05 01:57:13 +03:00
2016-01-16 00:34:36 +03:00
2020-02-12 19:29:19 +03:00
def test_short_file():
""" Check that the appropriate error is thrown for a short file"""
with open(TEST_FILE_DXT5, "rb") as f:
img_file = f.read()
def short_file():
with Image.open(BytesIO(img_file[:-100])) as im:
im.load()
with pytest.raises(OSError):
2020-02-12 19:29:19 +03:00
short_file()
2016-01-16 00:34:36 +03:00
2019-02-23 00:53:45 +03:00
2020-01-09 06:06:48 +03:00
def test_dxt5_colorblock_alpha_issue_4142():
""" Check that colorblocks are decoded correctly in DXT5"""
with Image.open("Tests/images/dxt5-colorblock-alpha-issue-4142.dds") as im:
px = im.getpixel((0, 0))
assert px[0] != 0
assert px[1] != 0
assert px[2] != 0
px = im.getpixel((1, 0))
assert px[0] != 0
assert px[1] != 0
assert px[2] != 0
2020-02-12 19:29:19 +03:00
def test_unimplemented_pixel_format():
with pytest.raises(NotImplementedError):
2021-02-11 13:43:54 +03:00
with Image.open("Tests/images/unimplemented_pixel_format.dds"):
pass