Pillow/Tests/test_file_dds.py

136 lines
3.8 KiB
Python
Raw Normal View History

2016-01-16 00:34:36 +03:00
from io import BytesIO
2016-01-06 11:59:37 +03:00
from .helper import PillowTestCase
2016-01-06 11:59:37 +03:00
from PIL import Image, DdsImagePlugin
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"
2016-08-26 10:26:03 +03:00
TEST_FILE_DX10_BC7 = "Tests/images/bc7-argb-8bpp_MipMaps-1.dds"
2019-02-22 22:44:06 +03:00
TEST_FILE_UNCOMPRESSED_RGB = "Tests/images/uncompressed_rgb.dds"
2016-01-06 11:59:37 +03:00
2016-01-06 13:26:58 +03:00
class TestFileDds(PillowTestCase):
"""Test DdsImagePlugin"""
2016-01-06 11:59:37 +03:00
def test_sanity_dxt1(self):
2016-01-06 13:26:58 +03:00
"""Check DXT1 images can be opened"""
target = Image.open(TEST_FILE_DXT1.replace('.dds', '.png'))
2016-01-06 11:59:37 +03:00
im = Image.open(TEST_FILE_DXT1)
im.load()
2016-02-05 01:57:13 +03:00
2016-01-06 12:24:09 +03:00
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
2016-01-06 11:59:37 +03:00
self.assertEqual(im.size, (256, 256))
2016-01-08 18:59:18 +03:00
self.assert_image_equal(target.convert('RGBA'), im)
2016-01-06 11:59:37 +03:00
def test_sanity_dxt5(self):
2016-01-06 13:26:58 +03:00
"""Check DXT5 images can be opened"""
target = Image.open(TEST_FILE_DXT5.replace('.dds', '.png'))
2016-01-06 11:59:37 +03:00
im = Image.open(TEST_FILE_DXT5)
im.load()
2016-01-06 11:59:37 +03:00
2016-01-06 12:24:09 +03:00
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
2016-01-06 11:59:37 +03:00
self.assertEqual(im.size, (256, 256))
2016-08-26 04:12:44 +03:00
self.assert_image_equal(target, im)
2016-01-08 18:59:18 +03:00
2016-01-06 12:59:47 +03:00
def test_sanity_dxt3(self):
2016-08-26 04:12:44 +03:00
"""Check DXT3 images can be opened"""
target = Image.open(TEST_FILE_DXT3.replace('.dds', '.png'))
im = Image.open(TEST_FILE_DXT3)
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (256, 256))
self.assert_image_equal(target, im)
2016-01-06 12:59:47 +03:00
2016-08-26 10:26:03 +03:00
def test_dx10_bc7(self):
"""Check DX10 images can be opened"""
target = Image.open(TEST_FILE_DX10_BC7.replace('.dds', '.png'))
im = Image.open(TEST_FILE_DX10_BC7)
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (256, 256))
self.assert_image_equal(target, im)
2019-02-23 00:53:45 +03:00
def test_unimplemented_dxgi_format(self):
self.assertRaises(NotImplementedError, Image.open,
"Tests/images/unimplemented_dxgi_format.dds")
2019-02-22 22:44:06 +03:00
def test_uncompressed_rgb(self):
"""Check uncompressed RGB images can be opened"""
target = Image.open(TEST_FILE_UNCOMPRESSED_RGB.replace('.dds', '.png'))
im = Image.open(TEST_FILE_UNCOMPRESSED_RGB)
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (800, 600))
self.assert_image_equal(target, im)
2016-01-06 11:59:37 +03:00
def test__validate_true(self):
2016-01-06 13:26:58 +03:00
"""Check valid prefix"""
2016-01-06 11:59:37 +03:00
# Arrange
prefix = b"DDS etc"
# Act
output = DdsImagePlugin._validate(prefix)
# Assert
self.assertTrue(output)
def test__validate_false(self):
2016-01-06 13:26:58 +03:00
"""Check invalid prefix"""
2016-01-06 11:59:37 +03:00
# Arrange
prefix = b"something invalid"
# Act
output = DdsImagePlugin._validate(prefix)
# Assert
self.assertFalse(output)
2016-01-16 00:34:36 +03:00
def test_short_header(self):
""" Check a short header"""
with open(TEST_FILE_DXT5, 'rb') as f:
img_file = f.read()
def short_header():
2016-02-05 01:57:13 +03:00
Image.open(BytesIO(img_file[:119]))
2016-01-16 00:34:36 +03:00
self.assertRaises(IOError, short_header)
def test_short_file(self):
""" Check that the appropriate error is thrown for a short file"""
2016-02-05 01:57:13 +03:00
2016-01-16 00:34:36 +03:00
with open(TEST_FILE_DXT5, 'rb') as f:
img_file = f.read()
def short_file():
im = Image.open(BytesIO(img_file[:-100]))
im.load()
2016-01-16 00:34:36 +03:00
self.assertRaises(IOError, short_file)
2019-02-23 00:53:45 +03:00
def test_unimplemented_pixel_format(self):
self.assertRaises(NotImplementedError, Image.open,
"Tests/images/unimplemented_pixel_format.dds")