|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import io |
3 | 4 | import os |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
7 | | -from PIL import Image, SunImagePlugin |
| 8 | +from PIL import Image, SunImagePlugin, _binary |
8 | 9 |
|
9 | 10 | from .helper import assert_image_equal_tofile, assert_image_similar, hopper |
10 | 11 |
|
@@ -33,6 +34,60 @@ def test_im1() -> None: |
33 | 34 | assert_image_equal_tofile(im, "Tests/images/sunraster.im1.png") |
34 | 35 |
|
35 | 36 |
|
| 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 | + |
36 | 91 | @pytest.mark.skipif( |
37 | 92 | not os.path.exists(EXTRA_DIR), reason="Extra image files not installed" |
38 | 93 | ) |
|
0 commit comments