Skip to content

Commit

Permalink
Merge pull request #8146 from Yay295/jp2_parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jun 26, 2024
2 parents d2d03a1 + 88b21e7 commit 2756cd5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Binary file added Tests/images/unknown_mode.j2k
Binary file not shown.
6 changes: 6 additions & 0 deletions Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ def test_issue_6194() -> None:
assert im.getpixel((5, 5)) == 31


def test_unknown_j2k_mode() -> None:
with pytest.raises(UnidentifiedImageError):
with Image.open("Tests/images/unknown_mode.j2k"):
pass


def test_unbound_local() -> None:
# prepatch, a malformed jp2 file could cause an UnboundLocalError exception.
with pytest.raises(UnidentifiedImageError):
Expand Down
9 changes: 3 additions & 6 deletions src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def next_box_type(self) -> bytes:
return tbox


def _parse_codestream(fp):
def _parse_codestream(fp) -> tuple[tuple[int, int], str]:
"""Parse the JPEG 2000 codestream to extract the size and component
count from the SIZ marker segment, returning a PIL (size, mode) tuple."""

Expand All @@ -122,7 +122,8 @@ def _parse_codestream(fp):
elif csiz == 4:
mode = "RGBA"
else:
mode = ""
msg = "unable to determine J2K image mode"
raise SyntaxError(msg)

return size, mode

Expand Down Expand Up @@ -237,10 +238,6 @@ def _open(self) -> None:
msg = "not a JPEG 2000 file"
raise SyntaxError(msg)

if self.size is None or not self.mode:
msg = "unable to determine size/mode"
raise SyntaxError(msg)

self._reduce = 0
self.layers = 0

Expand Down

0 comments on commit 2756cd5

Please sign in to comment.