Merge pull request #5654 from radarhere/ihdr

This commit is contained in:
Hugo van Kemenade 2021-08-24 15:08:23 +03:00 committed by GitHub
commit 57c672efcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 38 deletions

Binary file not shown.

View File

@ -159,6 +159,16 @@ def test_load_dpi():
assert "dpi" not in im.info
def test_restricted_icc_profile():
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
# JPEG2000 image with a restricted ICC profile and a known colorspace
with Image.open("Tests/images/balloon_eciRGBv2_aware.jp2") as im:
assert im.mode == "RGB"
finally:
ImageFile.LOAD_TRUNCATED_IMAGES = False
def test_header_errors():
for path in (
"Tests/images/invalid_header_length.jp2",

View File

@ -159,50 +159,23 @@ def _parse_jp2_header(fp):
bpc = None
nc = None
dpi = None # 2-tuple of DPI info, or None
unkc = 0 # Colorspace information unknown
while header.has_next_box():
tbox = header.next_box_type()
if tbox == b"ihdr":
height, width, nc, bpc, c, unkc, ipr = header.read_fields(">IIHBBBB")
height, width, nc, bpc = header.read_fields(">IIHB")
size = (width, height)
if unkc:
if nc == 1 and (bpc & 0x7F) > 8:
mode = "I;16"
elif nc == 1:
mode = "L"
elif nc == 2:
mode = "LA"
elif nc == 3:
mode = "RGB"
elif nc == 4:
mode = "RGBA"
elif tbox == b"colr":
meth, prec, approx = header.read_fields(">BBB")
if meth == 1 and unkc == 0:
cs = header.read_fields(">I")[0]
if cs == 16: # sRGB
if nc == 1 and (bpc & 0x7F) > 8:
mode = "I;16"
elif nc == 1:
mode = "L"
elif nc == 3:
mode = "RGB"
elif nc == 4:
mode = "RGBA"
elif cs == 17: # grayscale
if nc == 1 and (bpc & 0x7F) > 8:
mode = "I;16"
elif nc == 1:
mode = "L"
elif nc == 2:
mode = "LA"
elif cs == 18: # sYCC
if nc == 3:
mode = "RGB"
elif nc == 4:
mode = "RGBA"
if nc == 1 and (bpc & 0x7F) > 8:
mode = "I;16"
elif nc == 1:
mode = "L"
elif nc == 2:
mode = "LA"
elif nc == 3:
mode = "RGB"
elif nc == 4:
mode = "RGBA"
elif tbox == b"res ":
res = header.read_boxes()
while res.has_next_box():