Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rearranged test code #1

Merged
merged 2 commits into from Jun 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 22 additions & 56 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -168,81 +168,47 @@ def test_layers():
assert_image_similar(im, test_card, 0.4)


def test_no_jp2():
out = BytesIO()
out.name = "foo.j2k"
test_card.save(out, "JPEG2000")
out.seek(0)
assert out.read(2) == b"\xff\x4f"

out = BytesIO()
out.name = "foo.jp2"
test_card.save(out, "JPEG2000")
out.seek(4)
assert out.read(2) == b"jP"

out = BytesIO()
test_card.save(out, "JPEG2000", no_jp2=True)
out.seek(0)
assert out.read(2) == b"\xff\x4f"

out = BytesIO()
test_card.save(out, "JPEG2000", no_jp2=True)
out.seek(0)
assert out.read(2) == b"\xff\x4f"

out = BytesIO()
out.name = "foo.j2k"
test_card.save(out, "JPEG2000", no_jp2=True)
out.seek(0)
assert out.read(2) == b"\xff\x4f"

out = BytesIO()
out.name = "foo.jp2"
test_card.save(out, "JPEG2000", no_jp2=True)
out.seek(0)
assert out.read(2) == b"\xff\x4f"

# Use the filename extension to determine format
out = BytesIO()
out.name = "foo.j2k"
test_card.save(out, "JPEG2000", no_jp2=False)
out.seek(0)
assert out.read(2) == b"\xff\x4f"

out = BytesIO()
out.name = "foo.jp2"
test_card.save(out, "JPEG2000", no_jp2=False)
out.seek(4)
assert out.read(2) == b"jP"

# Default to JP2 if no filename
@pytest.mark.parametrize(
"name, args, offset, data",
(
("foo.j2k", {}, 0, b"\xff\x4f"),
("foo.jp2", {}, 4, b"jP"),
(None, {"no_jp2": True}, 0, b"\xff\x4f"),
("foo.j2k", {"no_jp2": True}, 0, b"\xff\x4f"),
("foo.jp2", {"no_jp2": True}, 0, b"\xff\x4f"),
("foo.j2k", {"no_jp2": False}, 0, b"\xff\x4f"),
("foo.jp2", {"no_jp2": False}, 4, b"jP"),
("foo.jp2", {"no_jp2": False}, 4, b"jP"),
),
)
def test_no_jp2(name, args, offset, data):
out = BytesIO()
test_card.save(out, "JPEG2000", no_jp2=False)
out.seek(4)
assert out.read(2) == b"jP"
if name:
out.name = name
test_card.save(out, "JPEG2000", **args)
out.seek(offset)
assert out.read(2) == data


def test_mct():
# Three component
for val in (0, 1):
out = BytesIO()
test_card.save(out, "JPEG2000", mct=val, no_jp2=True)
out.seek(0)

assert out.getvalue()[59] == val
with Image.open(out) as im:
assert_image_similar(im, test_card, 1.0e-3)
assert out.getvalue()[59] == val

# Single component should have MCT disabled
for val in (0, 1):
out = BytesIO()
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
jp2.save(out, "JPEG2000", mct=val, no_jp2=True)

out.seek(0)
assert out.getvalue()[53] == 0
with Image.open(out) as im:
assert_image_similar(im, jp2, 1.0e-3)
assert out.getvalue()[53] == 0


def test_rgba():
Expand Down