From ce060a8e8bc7714220f5747d70de9b3ab55b34e6 Mon Sep 17 00:00:00 2001 From: scaramallion Date: Sun, 24 Mar 2024 15:40:11 +1100 Subject: [PATCH 1/5] Fix 9-bit JPEG 2000 images not using I;16 --- Tests/images/9bit.j2k | Bin 0 -> 9412 bytes Tests/test_file_jpeg2k.py | 6 ++++++ src/PIL/Jpeg2KImagePlugin.py | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Tests/images/9bit.j2k diff --git a/Tests/images/9bit.j2k b/Tests/images/9bit.j2k new file mode 100644 index 0000000000000000000000000000000000000000..174f565fc64f2791a9b4aef3cc09240b402ddafe GIT binary patch literal 9412 zcmV;#Bs<&xPybN>DF6Tf004jh004jh0000000000004jh004jh00000000000SEyB z|55-90000100jgD00IA8024q+P*7M!N>D{dAa-SPb7^mGATlmBE-?R)015yA001OP00IA#-G&3gCBj;cgC$CPgRMSv5Gs7sudvAA$@&1P~l?ynbSck_kHyWt|K&fPE36?kd1HN5UDO(-y@!JQ)ZoG%$B>z1i2yq*_={1eK@hKD9mP7D#^> zr5bi^h`^Z3qe5lxD7fd`$lLeHT8Fi5?j>6We$POqOgQNSA`kG#(w5b+Ci+nI6YU%c zwd4`e15K%96Le_L$Khy4;cdh4w}30-*pz}#b~Po&ePV>@03+S+f_8j_7sd%5Ka6cG zxhH@DiKw9;hG~v*W>yUNruY1WgPu&)w*IMqEL}@)xn%*s5F}mT1zA$Bdmq*Essew5 z2mC4?GQnH5GQ8_!YWeVmD?P-~^5p7B$%IcBlj--mdIh}Y({Z|)%!fhH31-RZ!lpV&r0odvNoP8}xz8;@a^ip#{1xhz+ z@y-s|fNrz(3|`6nu>Ark9kVC;tUqhrc5^$`^5c}Gq>E?D_d)zQpW6>yuUVM6G`Uip z5j)@iMC#>sqSo!;Mm*olK(~c6v@r!}&LbrsvHH2r{12~Pdv!MqGSw~IfN|C$eGnsBpFcYF@z^qTI$Hi%sXgB`SRlQ* zNzYB0B4| zMmFRwzgvh?xk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41wfs2f;pIs!GH5cduC4y*8d+NA~FJY(ec7{NTRxUn-H=d-tH|xV&om%_eeRuuHP15)ZK=*;(5J0 zrJbEn$XCV2bM{HDSFrL#H%3P26?&d#wDBvh)hu8^yw*bMUuC9#+CI+7UKB3F)uYEiD;~j*c5A!6lQ+)*;>K5jl)k%nJdUTQj^-jVB zw3fX+0xlIlO{UkFQL}rM86*<+anQ7Z2oWtqvoBgD;|NcZ&+PhLH7n||NTvBvFW1Z9 zIloTm7EP!){P98%en>I0u8b57%om79SM<^~3SRdHTKs`lh^IVa!R}1O?YUI3Seu_W~jt{m6tE3_6r(e+BK@H$3k z%jl#%T$?7?V3(yFVms7Ep1OCKowL$NWHl7}lL&r5_IOGVkc9#Z4M?9q7Sn_0>Fte6 zj`Q2_;)y~$@n|f4_f@<44MMXsykkonkDS^7aQ$J*p!))ov@?$8^{VF$m^5h)1YgW` z4zv~p6zyKdrk!FUZqOf_tIvu<7i(6=eQ{f}G+Ge4;-V-(8EVijG&eu{;5zU7Zc!M4 zG3~q7`Ey;s(2PuIHKCeJ^-(#D-U}yX=Ipf3moU`-7=a)$nn6Y!7#?YadC830CLT4aua#)8KXA5$_!*zmv(*8mO} zp&0xKZkSlPX(9L}|kXdTu6BLjtoI*>XmJvF&x&bDA$stvvh7 zuCmJ3RI}4*lOqiX%u>|-E8*1IBksJI~J4j?CjVB zqUTPpB9o5&6WPJp2~&7l^l_jq(nx)P_Ll^Px0a&NllR5G1>wLgeLK7iiVd None: hdr = out.read(2) length = _binary.i16be(hdr) out.seek(length - 2, os.SEEK_CUR) + +def test_9bit(): + # Test 9-bit images are decoded to I;16 + with Image.open("Tests/images/9bit.j2k") as im: + assert im.mode == "I;16" + assert im.size == (128, 128) diff --git a/src/PIL/Jpeg2KImagePlugin.py b/src/PIL/Jpeg2KImagePlugin.py index 27f8fbbd0..925a9148c 100644 --- a/src/PIL/Jpeg2KImagePlugin.py +++ b/src/PIL/Jpeg2KImagePlugin.py @@ -114,7 +114,7 @@ def _parse_codestream(fp): size = (xsiz - xosiz, ysiz - yosiz) if csiz == 1: - if (yrsiz[0] & 0x7F) > 8: + if (yrsiz[0] & 0x7F) + 1 > 8: mode = "I;16" else: mode = "L" From 901c13c4b195f2e03a8779fe8d54e0e00551b579 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Mar 2024 04:47:24 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_file_jpeg2k.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 00eeb0cf9..ecdda104a 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -447,6 +447,7 @@ def test_plt_marker() -> None: length = _binary.i16be(hdr) out.seek(length - 2, os.SEEK_CUR) + def test_9bit(): # Test 9-bit images are decoded to I;16 with Image.open("Tests/images/9bit.j2k") as im: From 1f182ce8da03b375986c6bdf78f1337e3872bd0b Mon Sep 17 00:00:00 2001 From: scaramallion Date: Mon, 25 Mar 2024 17:39:21 +1100 Subject: [PATCH 3/5] Fix parsing the J2K header --- src/PIL/Jpeg2KImagePlugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/Jpeg2KImagePlugin.py b/src/PIL/Jpeg2KImagePlugin.py index 925a9148c..d5734feac 100644 --- a/src/PIL/Jpeg2KImagePlugin.py +++ b/src/PIL/Jpeg2KImagePlugin.py @@ -110,11 +110,11 @@ def _parse_codestream(fp): 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) + ssiz[i], xrsiz[i], yrsiz[i] = struct.unpack_from(">BBB", siz, 38 + 3 * i) size = (xsiz - xosiz, ysiz - yosiz) if csiz == 1: - if (yrsiz[0] & 0x7F) + 1 > 8: + if (ssiz[0] & 0x7F) + 1 > 8: mode = "I;16" else: mode = "L" From 0f49eaf23041e7b27c3a7b8d3b62433b2178fa08 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 25 Mar 2024 18:23:52 +1100 Subject: [PATCH 4/5] Simplified code --- src/PIL/Jpeg2KImagePlugin.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/PIL/Jpeg2KImagePlugin.py b/src/PIL/Jpeg2KImagePlugin.py index d5734feac..be000c351 100644 --- a/src/PIL/Jpeg2KImagePlugin.py +++ b/src/PIL/Jpeg2KImagePlugin.py @@ -106,14 +106,10 @@ 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, 38 + 3 * i) size = (xsiz - xosiz, ysiz - yosiz) if csiz == 1: + ssiz = struct.unpack_from(">B", siz, 38) if (ssiz[0] & 0x7F) + 1 > 8: mode = "I;16" else: From 7ca9bddf938537c4f4621e80c9212456b83be414 Mon Sep 17 00:00:00 2001 From: scaramallion Date: Mon, 25 Mar 2024 20:13:45 +1100 Subject: [PATCH 5/5] Update Tests/test_file_jpeg2k.py Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_file_jpeg2k.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index ecdda104a..81f75cc72 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -449,7 +449,6 @@ def test_plt_marker() -> None: def test_9bit(): - # Test 9-bit images are decoded to I;16 with Image.open("Tests/images/9bit.j2k") as im: assert im.mode == "I;16" assert im.size == (128, 128)