diff --git a/src/img2pdf.py b/src/img2pdf.py index 2c90c20..9dd7725 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -1874,7 +1874,11 @@ def read_images( imgformat = ImageFormat.JBIG2 if rawdata[:24] != b"\x97\x4a\x42\x32\x0d\x0a\x1a\x0a\x01\x00\x00\x00\x01\x00\x00\x00\x00\x30\x00\x01\x00\x00\x00\x13": raise ImageOpenError( - "Unsupported JBIG2 format; only single-page generic coding is supported (e.g. from `jbig2enc`)" + "Unsupported JBIG2 format; only single-page generic coding is supported (e.g. from `jbig2enc`)." + ) + if rawdata[-22:] != b"\x00\x00\x00\x021\x00\x01\x00\x00\x00\x00\x00\x00\x00\x033\x00\x01\x00\x00\x00\x00": + raise ImageOpenError( + "Unsupported JBIG2 format; we expect end-of-page and end-of-file segments at the end (e.g. from `jbig2enc`)." ) elif rawdata[:14].lower() == b"id=imagemagick": # image is in MIFF format @@ -2126,7 +2130,7 @@ def read_images( color, ndpi, imgwidthpx, imgheightpx, rotation, iccp = get_imgmetadata( imgdata, imgformat, default_dpi, colorspace, rawdata, rot ) - streamdata = rawdata[13:] # Strip file header + streamdata = rawdata[13:-22] # Strip file header and footer return [ ( color, diff --git a/src/img2pdf_test.py b/src/img2pdf_test.py index 9238c75..6141072 100755 --- a/src/img2pdf_test.py +++ b/src/img2pdf_test.py @@ -7062,7 +7062,7 @@ def test_general(general_input, engine): if imgprops.Filter in ["/DCTDecode", "/JPXDecode"]: assert cur_page.Resources.XObject.Im0.read_raw_bytes() == orig_imgdata elif imgprops.Filter == "/JBIG2Decode": - assert cur_page.Resources.XObject.Im0.read_raw_bytes() == orig_imgdata[13:] # Strip file header + assert cur_page.Resources.XObject.Im0.read_raw_bytes() == orig_imgdata[13:-22] # Strip file header and footer. elif imgprops.Filter == pikepdf.Array([pikepdf.Name.CCITTFaxDecode]): tiff_header = tiff_header_for_ccitt( int(imgprops.Width), int(imgprops.Height), int(imgprops.Length), 4