Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Dec 25, 2024

#6909 added reading of JP2 comments. This adds reading of J2K comments as well.

JP2 comments are read from a "Contiguous codestream box" that "contains a valid and complete JPEG 2000 codestream"

if self.fp.read(12).endswith(b"jp2c\xff\x4f\xff\x51"):
self._parse_comment()

and starts by skipping hdr.

def _parse_comment(self) -> None:
hdr = self.fp.read(2)
length = _binary.i16be(hdr)
self.fp.seek(length - 2, os.SEEK_CUR)

Reading from a J2K image,

if sig == b"\xff\x4f\xff\x51":
self.codec = "j2k"
self._size, self._mode = _parse_codestream(self.fp)

the hdr is already read.

def _parse_codestream(fp: IO[bytes]) -> 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."""
hdr = fp.read(2)
lsiz = _binary.i16be(hdr)
siz = hdr + fp.read(lsiz - 2)

So this PR moves the hdr handling out of _parse_comment.

@radarhere radarhere added the JPEG label Dec 25, 2024
Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#6909 added reading of JP2 comments. This adds reading of J2K comments as well.

Let's also add release notes like that one did.

@hugovk hugovk merged commit 41a89ea into python-pillow:main Dec 28, 2024
48 checks passed
@radarhere radarhere deleted the jpeg2000_comment branch December 28, 2024 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants