2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2025-02-18 12:47:17 +03:00
|
|
|
import io
|
2019-07-06 23:40:53 +03:00
|
|
|
import os
|
2014-07-16 15:24:23 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2025-02-18 12:47:17 +03:00
|
|
|
from PIL import Image, SunImagePlugin, _binary
|
2014-07-16 15:24:23 +04:00
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
from .helper import assert_image_equal_tofile, assert_image_similar, hopper
|
2016-11-22 17:47:33 +03:00
|
|
|
|
2019-06-13 18:54:11 +03:00
|
|
|
EXTRA_DIR = "Tests/images/sunraster"
|
2014-07-16 15:24:23 +04:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
# Created with ImageMagick: convert hopper.jpg hopper.ras
|
|
|
|
test_file = "Tests/images/hopper.ras"
|
2014-07-16 15:24:23 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
with Image.open(test_file) as im:
|
|
|
|
# Assert
|
|
|
|
assert im.size == (128, 128)
|
2014-07-16 15:24:23 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(im, hopper(), 5) # visually verified
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
invalid_file = "Tests/images/flower.jpg"
|
|
|
|
with pytest.raises(SyntaxError):
|
|
|
|
SunImagePlugin.SunImageFile(invalid_file)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2016-11-22 17:47:33 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_im1() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/sunraster.im1") as im:
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/sunraster.im1.png")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2025-02-18 12:47:17 +03:00
|
|
|
def _sun_header(
|
|
|
|
depth: int = 0, file_type: int = 0, palette_length: int = 0
|
|
|
|
) -> io.BytesIO:
|
|
|
|
return io.BytesIO(
|
|
|
|
_binary.o32be(0x59A66A95)
|
|
|
|
+ b"\x00" * 8
|
|
|
|
+ _binary.o32be(depth)
|
|
|
|
+ b"\x00" * 4
|
|
|
|
+ _binary.o32be(file_type)
|
|
|
|
+ b"\x00" * 4
|
|
|
|
+ _binary.o32be(palette_length)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsupported_mode_bit_depth() -> None:
|
|
|
|
with pytest.raises(SyntaxError, match="Unsupported Mode/Bit Depth"):
|
|
|
|
with SunImagePlugin.SunImageFile(_sun_header()):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsupported_color_palette_length() -> None:
|
|
|
|
with pytest.raises(SyntaxError, match="Unsupported Color Palette Length"):
|
|
|
|
with SunImagePlugin.SunImageFile(_sun_header(depth=1, palette_length=1025)):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsupported_palette_type() -> None:
|
|
|
|
with pytest.raises(SyntaxError, match="Unsupported Palette Type"):
|
|
|
|
with SunImagePlugin.SunImageFile(_sun_header(depth=1, palette_length=1)):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_unsupported_file_type() -> None:
|
|
|
|
with pytest.raises(SyntaxError, match="Unsupported Sun Raster file type"):
|
|
|
|
with SunImagePlugin.SunImageFile(_sun_header(depth=1, file_type=6)):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2025-02-18 12:29:35 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
|
|
|
|
)
|
|
|
|
def test_rgbx() -> None:
|
|
|
|
with open(os.path.join(EXTRA_DIR, "32bpp.ras"), "rb") as fp:
|
|
|
|
data = fp.read()
|
|
|
|
|
|
|
|
# Set file type to 3
|
|
|
|
data = data[:20] + _binary.o32be(3) + data[24:]
|
|
|
|
|
|
|
|
with Image.open(io.BytesIO(data)) as im:
|
|
|
|
r, g, b = im.split()
|
|
|
|
im = Image.merge("RGB", (b, g, r))
|
|
|
|
assert_image_equal_tofile(im, os.path.join(EXTRA_DIR, "32bpp.png"))
|
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
|
|
|
|
)
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_others() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
files = (
|
|
|
|
os.path.join(EXTRA_DIR, f)
|
|
|
|
for f in os.listdir(EXTRA_DIR)
|
|
|
|
if os.path.splitext(f)[1] in (".sun", ".SUN", ".ras")
|
|
|
|
)
|
|
|
|
for path in files:
|
|
|
|
with Image.open(path) as im:
|
|
|
|
im.load()
|
|
|
|
assert isinstance(im, SunImagePlugin.SunImageFile)
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, f"{os.path.splitext(path)[0]}.png")
|