File: test_bmp_reference.py

package info (click to toggle)
pillow 8.1.2%2Bdfsg-0.3%2Bdeb11u2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 65,628 kB
  • sloc: python: 35,630; ansic: 31,009; makefile: 388; javascript: 114; sh: 77
file content (111 lines) | stat: -rw-r--r-- 3,447 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os

import pytest

from PIL import Image

from .helper import assert_image_similar

base = os.path.join("Tests", "images", "bmp")


def get_files(d, ext=".bmp"):
    return [
        os.path.join(base, d, f) for f in os.listdir(os.path.join(base, d)) if ext in f
    ]


def test_bad():
    """These shouldn't crash/dos, but they shouldn't return anything
    either"""
    for f in get_files("b"):

        def open(f):
            try:
                with Image.open(f) as im:
                    im.load()
            except Exception:  # as msg:
                pass

        # Assert that there is no unclosed file warning
        pytest.warns(None, open, f)


def test_questionable():
    """These shouldn't crash/dos, but it's not well defined that these
    are in spec"""
    supported = [
        "pal8os2v2.bmp",
        "rgb24prof.bmp",
        "pal1p1.bmp",
        "pal8offs.bmp",
        "rgb24lprof.bmp",
        "rgb32fakealpha.bmp",
        "rgb24largepal.bmp",
        "pal8os2sp.bmp",
        "rgb32bf-xbgr.bmp",
    ]
    for f in get_files("q"):
        try:
            with Image.open(f) as im:
                im.load()
            if os.path.basename(f) not in supported:
                print(f"Please add {f} to the partially supported bmp specs.")
        except Exception:  # as msg:
            if os.path.basename(f) in supported:
                raise


def test_good():
    """These should all work. There's a set of target files in the
    html directory that we can compare against."""

    # Target files, if they're not just replacing the extension
    file_map = {
        "pal1wb.bmp": "pal1.png",
        "pal4rle.bmp": "pal4.png",
        "pal8-0.bmp": "pal8.png",
        "pal8rle.bmp": "pal8.png",
        "pal8topdown.bmp": "pal8.png",
        "pal8nonsquare.bmp": "pal8nonsquare-v.png",
        "pal8os2.bmp": "pal8.png",
        "pal8os2sp.bmp": "pal8.png",
        "pal8os2v2.bmp": "pal8.png",
        "pal8os2v2-16.bmp": "pal8.png",
        "pal8v4.bmp": "pal8.png",
        "pal8v5.bmp": "pal8.png",
        "rgb16-565pal.bmp": "rgb16-565.png",
        "rgb24pal.bmp": "rgb24.png",
        "rgb32.bmp": "rgb24.png",
        "rgb32bf.bmp": "rgb24.png",
    }

    def get_compare(f):
        name = os.path.split(f)[1]
        if name in file_map:
            return os.path.join(base, "html", file_map[name])
        name = os.path.splitext(name)[0]
        return os.path.join(base, "html", f"{name}.png")

    for f in get_files("g"):
        try:
            with Image.open(f) as im:
                im.load()
                with Image.open(get_compare(f)) as compare:
                    compare.load()
                    if im.mode == "P":
                        # assert image similar doesn't really work
                        # with paletized image, since the palette might
                        # be differently ordered for an equivalent image.
                        im = im.convert("RGBA")
                        compare = im.convert("RGBA")
                    assert_image_similar(im, compare, 5)

        except Exception as msg:
            # there are three here that are unsupported:
            unsupported = (
                os.path.join(base, "g", "rgb32bf.bmp"),
                os.path.join(base, "g", "pal8rle.bmp"),
                os.path.join(base, "g", "pal4rle.bmp"),
            )
            assert f in unsupported, f"Unsupported Image {f}: {msg}"