Skip to content

Commit

Permalink
Merge pull request #7900 from scaramallion/dev-j2k-precision
Browse files Browse the repository at this point in the history
Use I;16 mode for 9-bit JPEG 2000 images
  • Loading branch information
radarhere committed Mar 25, 2024
2 parents 5e448b3 + 144959b commit e0b1b38
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
Binary file added Tests/images/9bit.j2k
Binary file not shown.
6 changes: 6 additions & 0 deletions Tests/test_file_jpeg2k.py
Expand Up @@ -446,3 +446,9 @@ def test_plt_marker() -> None:
hdr = out.read(2)
length = _binary.i16be(hdr)
out.seek(length - 2, os.SEEK_CUR)


def test_9bit():
with Image.open("Tests/images/9bit.j2k") as im:
assert im.mode == "I;16"
assert im.size == (128, 128)
8 changes: 2 additions & 6 deletions src/PIL/Jpeg2KImagePlugin.py
Expand Up @@ -106,15 +106,11 @@ def _parse_codestream(fp):
lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, _, _, _, _, csiz = struct.unpack_from(
">HHIIIIIIIIH", siz
)
ssiz = [None] * csiz
xrsiz = [None] * csiz
yrsiz = [None] * csiz
for i in range(csiz):
ssiz[i], xrsiz[i], yrsiz[i] = struct.unpack_from(">BBB", siz, 36 + 3 * i)

size = (xsiz - xosiz, ysiz - yosiz)
if csiz == 1:
if (yrsiz[0] & 0x7F) > 8:
ssiz = struct.unpack_from(">B", siz, 38)
if (ssiz[0] & 0x7F) + 1 > 8:
mode = "I;16"
else:
mode = "L"
Expand Down

0 comments on commit e0b1b38

Please sign in to comment.