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

Added support for decoding plain PPM formats #5242

Merged
merged 22 commits into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Binary file added Tests/images/hopper_16bit.pgm
Binary file not shown.
4 changes: 4 additions & 0 deletions Tests/images/hopper_16bit_plain.pgm

Large diffs are not rendered by default.

Binary file added Tests/images/hopper_1bit.pbm
Binary file not shown.
14 changes: 14 additions & 0 deletions Tests/images/hopper_1bit_plain.pbm

Large diffs are not rendered by default.

Binary file added Tests/images/hopper_32bit.pgm
Binary file not shown.
4 changes: 4 additions & 0 deletions Tests/images/hopper_32bit_plain.pgm

Large diffs are not rendered by default.

Binary file added Tests/images/hopper_8bit.pgm
Binary file not shown.
Binary file added Tests/images/hopper_8bit.ppm
Binary file not shown.
4 changes: 4 additions & 0 deletions Tests/images/hopper_8bit_plain.pgm

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Tests/images/hopper_8bit_plain.ppm

Large diffs are not rendered by default.

142 changes: 133 additions & 9 deletions Tests/test_file_ppm.py
Expand Up @@ -3,7 +3,7 @@

import pytest

from PIL import Image, UnidentifiedImageError
from PIL import Image, PpmImagePlugin

from .helper import assert_image_equal_tofile, assert_image_similar, hopper

Expand Down Expand Up @@ -50,20 +50,144 @@ def test_pnm(tmp_path):
assert_image_equal_tofile(im, f)


def test_magic(tmp_path):
def test_plain_pbm(tmp_path):
with Image.open("Tests/images/hopper_1bit_plain.pbm") as im:
assert_image_equal_tofile(im, "Tests/images/hopper_1bit.pbm")


def test_8bit_plain_pgm(tmp_path):
with Image.open("Tests/images/hopper_8bit_plain.pgm") as im:
assert_image_equal_tofile(im, "Tests/images/hopper_8bit.pgm")


def test_8bit_plain_ppm(tmp_path):
with Image.open("Tests/images/hopper_8bit_plain.ppm") as im:
assert_image_equal_tofile(im, "Tests/images/hopper_8bit.ppm")


def test_16bit_plain_pgm(tmp_path):
with Image.open("Tests/images/hopper_16bit_plain.pgm") as im:
assert im.mode == "I"
assert im.size == (128, 128)
assert im.get_format_mimetype() == "image/x-portable-graymap"

assert_image_equal_tofile(im, "Tests/images/hopper_16bit.pgm")


def test_32bit_plain_pgm(tmp_path):
with Image.open("Tests/images/hopper_32bit_plain.pgm") as im:
assert im.mode == "I"
assert im.size == (128, 128)
assert im.get_format_mimetype() == "image/x-portable-graymap"

assert_image_equal_tofile(im, "Tests/images/hopper_32bit.pgm")


def test_plain_pbm_data_with_comments(tmp_path):
path1 = str(tmp_path / "temp1.ppm")
path2 = str(tmp_path / "temp2.ppm")
comment = b"# veeery long comment" * 10**6
with open(path1, "wb") as f1, open(path2, "wb") as f2:
f1.write(b"P1\n2 2\n\n1010")
f2.write(b"P1\n2 2\n" + comment + b"\n1010" + comment)

with Image.open(path1) as im:
assert_image_equal_tofile(im, path2)


def test_plain_pbm_truncated_data(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"PyInvalid")
f.write(b"P1\n128 128\n")

with pytest.raises(UnidentifiedImageError):
with Image.open(path):
pass
with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_pbm_invalid_data(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P1\n128 128\n1009")

with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_data_with_comments(tmp_path):
path1 = str(tmp_path / "temp1.ppm")
path2 = str(tmp_path / "temp2.ppm")
comment = b"# veeery long comment" * 10**6
with open(path1, "wb") as f1, open(path2, "wb") as f2:
f1.write(b"P3\n2 2\n255\n0 0 0 001 1 1 2 2 2 255 255 255")
f2.write(
b"P3\n2 2\n255\n" + comment + b"\n0 0 0 001 1 1 2 2 2 255 255 255" + comment
)

with Image.open(path1) as im:
assert_image_equal_tofile(im, path2)


def test_plain_ppm_truncated_data(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n")

with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_invalid_data(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n100A")

with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_half_token_too_long(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n012345678910")

with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_token_too_long(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n012345678910 0")

with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_plain_ppm_value_too_large(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P3\n128 128\n255\n256")

with Image.open(path) as im:
with pytest.raises(ValueError):
im.load()


def test_magic(tmp_path):
with pytest.raises(SyntaxError):
PpmImagePlugin.PpmImageFile(fp=BytesIO(b"PyInvalid"))


def test_header_with_comments(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P6 #comment\n#comment\r12#comment\r8\n128 #comment\n255\n")
f.write(b"P6 #comment\n#comment\r 12#comment\r8\n128 #comment\n255\n")

with Image.open(path) as im:
assert im.size == (128, 128)
Expand All @@ -79,7 +203,7 @@ def test_non_integer_token(tmp_path):
pass


def test_token_too_long(tmp_path):
def test_header_token_too_long(tmp_path):
path = str(tmp_path / "temp.ppm")
with open(path, "wb") as f:
f.write(b"P6\n 01234567890")
Expand All @@ -103,7 +227,7 @@ def test_too_many_colors(tmp_path):
assert str(e.value) == "Too many colors for band: 1000"


def test_truncated_file(tmp_path):
def test_truncated_header(tmp_path):
path = str(tmp_path / "temp.pgm")
with open(path, "w") as f:
f.write("P6")
Expand Down