Skip to content

Commit

Permalink
Merge branch 'main' into context_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere authored Dec 28, 2024
2 parents 68e9ee5 + 41a89ea commit dae3154
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,9 @@ def test_pclr() -> None:


def test_comment() -> None:
with Image.open("Tests/images/comment.jp2") as im:
assert im.info["comment"] == b"Created by OpenJPEG version 2.5.0"
for path in ("Tests/images/9bit.j2k", "Tests/images/comment.jp2"):
with Image.open(path) as im:
assert im.info["comment"] == b"Created by OpenJPEG version 2.5.0"

# Test an image that is truncated partway through a codestream
with open("Tests/images/comment.jp2", "rb") as fp:
Expand Down
6 changes: 6 additions & 0 deletions docs/releasenotes/11.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ zlib library, and what version of zlib-ng is being used::
Other Changes
=============

Reading JPEG 2000 comments
^^^^^^^^^^^^^^^^^^^^^^^^^^

When opening a JPEG 2000 image, the comment may now be read into
:py:attr:`~PIL.Image.Image.info` for J2K images, not just JP2 images.

zlib-ng in wheels
^^^^^^^^^^^^^^^^^

Expand Down
8 changes: 4 additions & 4 deletions src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def _open(self) -> None:
if sig == b"\xff\x4f\xff\x51":
self.codec = "j2k"
self._size, self._mode = _parse_codestream(self.fp)
self._parse_comment()
else:
sig = sig + self.fp.read(8)

Expand All @@ -263,6 +264,9 @@ def _open(self) -> None:
if dpi is not None:
self.info["dpi"] = dpi
if self.fp.read(12).endswith(b"jp2c\xff\x4f\xff\x51"):
hdr = self.fp.read(2)
length = _binary.i16be(hdr)
self.fp.seek(length - 2, os.SEEK_CUR)
self._parse_comment()
else:
msg = "not a JPEG 2000 file"
Expand Down Expand Up @@ -298,10 +302,6 @@ def _open(self) -> None:

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

while True:
marker = self.fp.read(2)
if not marker:
Expand Down
5 changes: 2 additions & 3 deletions src/libImaging/Jpeg2KDecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) {
opj_dparameters_t params;
OPJ_COLOR_SPACE color_space;
j2k_unpacker_t unpack = NULL;
size_t buffer_size = 0, tile_bytes = 0;
size_t tile_bytes = 0;
unsigned n, tile_height, tile_width;
int subsampling;
int total_component_width = 0;
Expand Down Expand Up @@ -870,7 +870,7 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) {
tile_info.data_size = tile_bytes;
}

if (buffer_size < tile_info.data_size) {
if (tile_info.data_size > 0) {
/* malloc check ok, overflow and tile size sanity check above */
UINT8 *new = realloc(state->buffer, tile_info.data_size);
if (!new) {
Expand All @@ -883,7 +883,6 @@ j2k_decode_entry(Imaging im, ImagingCodecState state) {
to valgrind errors. */
memset(new, 0, tile_info.data_size);
state->buffer = new;
buffer_size = tile_info.data_size;
}

if (!opj_decode_tile_data(
Expand Down

0 comments on commit dae3154

Please sign in to comment.