Skip to content

Commit 1126733

Browse files
authored
Merge pull request #8762 from radarhere/sun
2 parents fe1cab6 + 5d40e6a commit 1126733

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

Tests/test_file_sun.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from __future__ import annotations
22

3+
import io
34
import os
45

56
import pytest
67

7-
from PIL import Image, SunImagePlugin
8+
from PIL import Image, SunImagePlugin, _binary
89

910
from .helper import assert_image_equal_tofile, assert_image_similar, hopper
1011

@@ -33,6 +34,60 @@ def test_im1() -> None:
3334
assert_image_equal_tofile(im, "Tests/images/sunraster.im1.png")
3435

3536

37+
def _sun_header(
38+
depth: int = 0, file_type: int = 0, palette_length: int = 0
39+
) -> io.BytesIO:
40+
return io.BytesIO(
41+
_binary.o32be(0x59A66A95)
42+
+ b"\x00" * 8
43+
+ _binary.o32be(depth)
44+
+ b"\x00" * 4
45+
+ _binary.o32be(file_type)
46+
+ b"\x00" * 4
47+
+ _binary.o32be(palette_length)
48+
)
49+
50+
51+
def test_unsupported_mode_bit_depth() -> None:
52+
with pytest.raises(SyntaxError, match="Unsupported Mode/Bit Depth"):
53+
with SunImagePlugin.SunImageFile(_sun_header()):
54+
pass
55+
56+
57+
def test_unsupported_color_palette_length() -> None:
58+
with pytest.raises(SyntaxError, match="Unsupported Color Palette Length"):
59+
with SunImagePlugin.SunImageFile(_sun_header(depth=1, palette_length=1025)):
60+
pass
61+
62+
63+
def test_unsupported_palette_type() -> None:
64+
with pytest.raises(SyntaxError, match="Unsupported Palette Type"):
65+
with SunImagePlugin.SunImageFile(_sun_header(depth=1, palette_length=1)):
66+
pass
67+
68+
69+
def test_unsupported_file_type() -> None:
70+
with pytest.raises(SyntaxError, match="Unsupported Sun Raster file type"):
71+
with SunImagePlugin.SunImageFile(_sun_header(depth=1, file_type=6)):
72+
pass
73+
74+
75+
@pytest.mark.skipif(
76+
not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
77+
)
78+
def test_rgbx() -> None:
79+
with open(os.path.join(EXTRA_DIR, "32bpp.ras"), "rb") as fp:
80+
data = fp.read()
81+
82+
# Set file type to 3
83+
data = data[:20] + _binary.o32be(3) + data[24:]
84+
85+
with Image.open(io.BytesIO(data)) as im:
86+
r, g, b = im.split()
87+
im = Image.merge("RGB", (b, g, r))
88+
assert_image_equal_tofile(im, os.path.join(EXTRA_DIR, "32bpp.png"))
89+
90+
3691
@pytest.mark.skipif(
3792
not os.path.exists(EXTRA_DIR), reason="Extra image files not installed"
3893
)

0 commit comments

Comments
 (0)