Increase similiarity between test_plt_marker and _parse_comment

This commit is contained in:
Andrew Murray 2023-03-29 20:14:29 +11:00
parent 2f66d2d6a1
commit 9a7a448219
2 changed files with 15 additions and 14 deletions

View File

@ -411,29 +411,30 @@ def test_crashes(test_file):
@skip_unless_feature_version("jpg_2000", "2.4.0") @skip_unless_feature_version("jpg_2000", "2.4.0")
def test_plt_marker(): def test_plt_marker():
# Search the start of the codesteam for the PLT box (id 0xFF58) # Search the start of the codesteam for PLT
out = BytesIO() out = BytesIO()
test_card.save(out, "JPEG2000", no_jp2=True, plt=True) test_card.save(out, "JPEG2000", no_jp2=True, plt=True)
out.seek(0) out.seek(0)
while True: while True:
box_bytes = out.read(2) marker = out.read(2)
if not box_bytes: if not marker:
# End of steam encountered and no PLT or SOD # End of steam encountered and no PLT or SOD
break break
jp2_boxid = _binary.i16be(box_bytes) jp2_boxid = _binary.i16be(marker)
if jp2_boxid == 0xFF4F: if jp2_boxid == 0xFF4F:
# No length specifier for main header # SOC has no length
continue continue
elif jp2_boxid == 0xFF58: elif jp2_boxid == 0xFF58:
# This is the PLT box we're looking for # PLT
return return
elif jp2_boxid == 0xFF93: elif jp2_boxid == 0xFF93:
# SOD box encountered and no PLT, so it wasn't found # SOD without finding PLT first
break break
jp2_boxlength = _binary.i16be(out.read(2)) hdr = out.read(2)
out.seek(jp2_boxlength - 2, os.SEEK_CUR) length = _binary.i16be(hdr)
out.seek(length - 2, os.SEEK_CUR)
# The PLT box wasn't found # PLT wasn't found
raise ValueError raise ValueError

View File

@ -17,7 +17,7 @@ import io
import os import os
import struct import struct
from . import Image, ImageFile from . import Image, ImageFile, _binary
class BoxReader: class BoxReader:
@ -99,7 +99,7 @@ def _parse_codestream(fp):
count from the SIZ marker segment, returning a PIL (size, mode) tuple.""" count from the SIZ marker segment, returning a PIL (size, mode) tuple."""
hdr = fp.read(2) hdr = fp.read(2)
lsiz = struct.unpack(">H", hdr)[0] lsiz = _binary.i16be(hdr)
siz = hdr + fp.read(lsiz - 2) siz = hdr + fp.read(lsiz - 2)
lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, _, _, _, _, csiz = struct.unpack_from( lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, _, _, _, _, csiz = struct.unpack_from(
">HHIIIIIIIIH", siz ">HHIIIIIIIIH", siz
@ -258,7 +258,7 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
def _parse_comment(self): def _parse_comment(self):
hdr = self.fp.read(2) hdr = self.fp.read(2)
length = struct.unpack(">H", hdr)[0] length = _binary.i16be(hdr)
self.fp.seek(length - 2, os.SEEK_CUR) self.fp.seek(length - 2, os.SEEK_CUR)
while True: while True:
@ -270,7 +270,7 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
# Start of tile or end of codestream # Start of tile or end of codestream
break break
hdr = self.fp.read(2) hdr = self.fp.read(2)
length = struct.unpack(">H", hdr)[0] length = _binary.i16be(hdr)
if typ == 0x64: if typ == 0x64:
# Comment # Comment
self.info["comment"] = self.fp.read(length - 2)[2:] self.info["comment"] = self.fp.read(length - 2)[2:]